xref: /openbmc/phosphor-pid-control/test/json_parse_unittest.cpp (revision 903b04276b98c072b3399ac20101ad44daf335cd)
15426c349SPatrick Venture #include "build/buildjson.hpp"
25426c349SPatrick Venture #include "errors/exception.hpp"
35426c349SPatrick Venture 
45426c349SPatrick Venture #include <gmock/gmock.h>
55426c349SPatrick Venture #include <gtest/gtest.h>
65426c349SPatrick Venture 
75426c349SPatrick Venture TEST(ConfigurationVerificationTest, VerifyHappy)
85426c349SPatrick Venture {
95426c349SPatrick Venture     /* Verify a happy configuration throws no exceptions. */
105426c349SPatrick Venture     auto j2 = R"(
115426c349SPatrick Venture       {
125426c349SPatrick Venture         "sensors": [{
135426c349SPatrick Venture           "name": "fan1",
145426c349SPatrick Venture           "type": "fan",
155426c349SPatrick Venture           "readPath": "/xyz/openbmc_project/sensors/fan_tach/fan1"
165426c349SPatrick Venture         }],
175426c349SPatrick Venture         "zones": [{
185426c349SPatrick Venture           "id": 1,
195426c349SPatrick Venture           "minThermalRpm": 3000.0,
205426c349SPatrick Venture           "failsafePercent": 75.0,
215426c349SPatrick Venture           "pids": [{
225426c349SPatrick Venture             "name": "fan1-5",
235426c349SPatrick Venture             "type": "fan",
245426c349SPatrick Venture             "inputs": ["fan1", "fan5"],
255426c349SPatrick Venture             "setpoint": 90.0,
265426c349SPatrick Venture             "pid": {
275426c349SPatrick Venture               "samplePeriod": 0.1,
285426c349SPatrick Venture               "proportionalCoeff": 0.0,
295426c349SPatrick Venture               "integralCoeff": 0.0,
30*903b0427SPatrick Venture               "feedFwdOffsetCoeff": 0.0,
315426c349SPatrick Venture               "feedFwdGainCoeff": 0.010,
325426c349SPatrick Venture               "integralLimit_min": 0.0,
335426c349SPatrick Venture               "integralLimit_max": 0.0,
345426c349SPatrick Venture               "outLim_min": 30.0,
355426c349SPatrick Venture               "outLim_max": 100.0,
365426c349SPatrick Venture               "slewNeg": 0.0,
375426c349SPatrick Venture               "slewPos": 0.0
385426c349SPatrick Venture             }
395426c349SPatrick Venture           }]
405426c349SPatrick Venture         }]
415426c349SPatrick Venture       }
425426c349SPatrick Venture     )"_json;
435426c349SPatrick Venture 
445426c349SPatrick Venture     validateJson(j2);
455426c349SPatrick Venture }
465426c349SPatrick Venture 
475426c349SPatrick Venture TEST(ConfigurationVerificationTest, VerifyNoSensorKey)
485426c349SPatrick Venture {
495426c349SPatrick Venture     /* Verify the sensors key must be present. */
505426c349SPatrick Venture     auto j2 = R"(
515426c349SPatrick Venture       {
525426c349SPatrick Venture         "zones": [{
535426c349SPatrick Venture           "id": 1,
545426c349SPatrick Venture           "minThermalRpm": 3000.0,
555426c349SPatrick Venture           "failsafePercent": 75.0,
565426c349SPatrick Venture           "pids": [{
575426c349SPatrick Venture             "name": "fan1-5",
585426c349SPatrick Venture             "type": "fan",
595426c349SPatrick Venture             "inputs": ["fan1", "fan5"],
605426c349SPatrick Venture             "setpoint": 90.0,
615426c349SPatrick Venture             "pid": {
625426c349SPatrick Venture               "samplePeriod": 0.1,
635426c349SPatrick Venture               "proportionalCoeff": 0.0,
645426c349SPatrick Venture               "integralCoeff": 0.0,
65*903b0427SPatrick Venture               "feedFwdOffsetCoeff": 0.0,
665426c349SPatrick Venture               "feedFwdGainCoeff": 0.010,
675426c349SPatrick Venture               "integralLimit_min": 0.0,
685426c349SPatrick Venture               "integralLimit_max": 0.0,
695426c349SPatrick Venture               "outLim_min": 30.0,
705426c349SPatrick Venture               "outLim_max": 100.0,
715426c349SPatrick Venture               "slewNeg": 0.0,
725426c349SPatrick Venture               "slewPos": 0.0
735426c349SPatrick Venture             }
745426c349SPatrick Venture           }]
755426c349SPatrick Venture         }]
765426c349SPatrick Venture       }
775426c349SPatrick Venture     )"_json;
785426c349SPatrick Venture 
795426c349SPatrick Venture     EXPECT_THROW(validateJson(j2), ConfigurationException);
805426c349SPatrick Venture }
815426c349SPatrick Venture 
825426c349SPatrick Venture TEST(ConfigurationVerificationTest, VerifyNoZoneKey)
835426c349SPatrick Venture {
845426c349SPatrick Venture     /* Verify the zones key must be present. */
855426c349SPatrick Venture     auto j2 = R"(
865426c349SPatrick Venture       {
875426c349SPatrick Venture         "sensors": [{
885426c349SPatrick Venture           "name": "fan1",
895426c349SPatrick Venture           "type": "fan",
905426c349SPatrick Venture           "readPath": "/xyz/openbmc_project/sensors/fan_tach/fan1"
915426c349SPatrick Venture         }]
925426c349SPatrick Venture       }
935426c349SPatrick Venture     )"_json;
945426c349SPatrick Venture 
955426c349SPatrick Venture     EXPECT_THROW(validateJson(j2), ConfigurationException);
965426c349SPatrick Venture }
975426c349SPatrick Venture 
985426c349SPatrick Venture TEST(ConfigurationVerificationTest, VerifyNoSensor)
995426c349SPatrick Venture {
1005426c349SPatrick Venture     /* Verify that there needs to be at least one sensor in the sensors key. */
1015426c349SPatrick Venture     auto j2 = R"(
1025426c349SPatrick Venture       {
1035426c349SPatrick Venture         "sensors": [],
1045426c349SPatrick Venture         "zones": [{
1055426c349SPatrick Venture           "id": 1,
1065426c349SPatrick Venture           "minThermalRpm": 3000.0,
1075426c349SPatrick Venture           "failsafePercent": 75.0,
1085426c349SPatrick Venture           "pids": [{
1095426c349SPatrick Venture             "name": "fan1-5",
1105426c349SPatrick Venture             "type": "fan",
1115426c349SPatrick Venture             "inputs": ["fan1", "fan5"],
1125426c349SPatrick Venture             "setpoint": 90.0,
1135426c349SPatrick Venture             "pid": {
1145426c349SPatrick Venture               "samplePeriod": 0.1,
1155426c349SPatrick Venture               "proportionalCoeff": 0.0,
1165426c349SPatrick Venture               "integralCoeff": 0.0,
117*903b0427SPatrick Venture               "feedFwdOffsetCoeff": 0.0,
1185426c349SPatrick Venture               "feedFwdGainCoeff": 0.010,
1195426c349SPatrick Venture               "integralLimit_min": 0.0,
1205426c349SPatrick Venture               "integralLimit_max": 0.0,
1215426c349SPatrick Venture               "outLim_min": 30.0,
1225426c349SPatrick Venture               "outLim_max": 100.0,
1235426c349SPatrick Venture               "slewNeg": 0.0,
1245426c349SPatrick Venture               "slewPos": 0.0
1255426c349SPatrick Venture             }
1265426c349SPatrick Venture           }]
1275426c349SPatrick Venture         }]
1285426c349SPatrick Venture       }
1295426c349SPatrick Venture     )"_json;
1305426c349SPatrick Venture 
1315426c349SPatrick Venture     EXPECT_THROW(validateJson(j2), ConfigurationException);
1325426c349SPatrick Venture }
1335426c349SPatrick Venture 
1345426c349SPatrick Venture TEST(ConfigurationVerificationTest, VerifyNoPidInZone)
1355426c349SPatrick Venture {
1365426c349SPatrick Venture     /* Verify that there needs to be at least one PID in the zone. */
1375426c349SPatrick Venture     auto j2 = R"(
1385426c349SPatrick Venture       {
1395426c349SPatrick Venture         "sensors": [{
1405426c349SPatrick Venture           "name": "fan1",
1415426c349SPatrick Venture           "type": "fan",
1425426c349SPatrick Venture           "readPath": "/xyz/openbmc_project/sensors/fan_tach/fan1"
1435426c349SPatrick Venture         }],
1445426c349SPatrick Venture         "zones": [{
1455426c349SPatrick Venture           "id": 1,
1465426c349SPatrick Venture           "minThermalRpm": 3000.0,
1475426c349SPatrick Venture           "failsafePercent": 75.0,
1485426c349SPatrick Venture           "pids": []
1495426c349SPatrick Venture         }]
1505426c349SPatrick Venture       }
1515426c349SPatrick Venture     )"_json;
1525426c349SPatrick Venture 
1535426c349SPatrick Venture     EXPECT_THROW(validateJson(j2), ConfigurationException);
1545426c349SPatrick Venture }
155