xref: /openbmc/phosphor-pid-control/test/json_parse_unittest.cpp (revision 5426c3493733aea04415f4e820dbfbae10845243)
1*5426c349SPatrick Venture #include "build/buildjson.hpp"
2*5426c349SPatrick Venture #include "errors/exception.hpp"
3*5426c349SPatrick Venture 
4*5426c349SPatrick Venture #include <gmock/gmock.h>
5*5426c349SPatrick Venture #include <gtest/gtest.h>
6*5426c349SPatrick Venture 
7*5426c349SPatrick Venture TEST(ConfigurationVerificationTest, VerifyHappy)
8*5426c349SPatrick Venture {
9*5426c349SPatrick Venture     /* Verify a happy configuration throws no exceptions. */
10*5426c349SPatrick Venture     auto j2 = R"(
11*5426c349SPatrick Venture       {
12*5426c349SPatrick Venture         "sensors": [{
13*5426c349SPatrick Venture           "name": "fan1",
14*5426c349SPatrick Venture           "type": "fan",
15*5426c349SPatrick Venture           "readPath": "/xyz/openbmc_project/sensors/fan_tach/fan1"
16*5426c349SPatrick Venture         }],
17*5426c349SPatrick Venture         "zones": [{
18*5426c349SPatrick Venture           "id": 1,
19*5426c349SPatrick Venture           "minThermalRpm": 3000.0,
20*5426c349SPatrick Venture           "failsafePercent": 75.0,
21*5426c349SPatrick Venture           "pids": [{
22*5426c349SPatrick Venture             "name": "fan1-5",
23*5426c349SPatrick Venture             "type": "fan",
24*5426c349SPatrick Venture             "inputs": ["fan1", "fan5"],
25*5426c349SPatrick Venture             "setpoint": 90.0,
26*5426c349SPatrick Venture             "pid": {
27*5426c349SPatrick Venture               "samplePeriod": 0.1,
28*5426c349SPatrick Venture               "proportionalCoeff": 0.0,
29*5426c349SPatrick Venture               "integralCoeff": 0.0,
30*5426c349SPatrick Venture               "feedFwdOffOffsetCoeff": 0.0,
31*5426c349SPatrick Venture               "feedFwdGainCoeff": 0.010,
32*5426c349SPatrick Venture               "integralLimit_min": 0.0,
33*5426c349SPatrick Venture               "integralLimit_max": 0.0,
34*5426c349SPatrick Venture               "outLim_min": 30.0,
35*5426c349SPatrick Venture               "outLim_max": 100.0,
36*5426c349SPatrick Venture               "slewNeg": 0.0,
37*5426c349SPatrick Venture               "slewPos": 0.0
38*5426c349SPatrick Venture             }
39*5426c349SPatrick Venture           }]
40*5426c349SPatrick Venture         }]
41*5426c349SPatrick Venture       }
42*5426c349SPatrick Venture     )"_json;
43*5426c349SPatrick Venture 
44*5426c349SPatrick Venture     validateJson(j2);
45*5426c349SPatrick Venture }
46*5426c349SPatrick Venture 
47*5426c349SPatrick Venture TEST(ConfigurationVerificationTest, VerifyNoSensorKey)
48*5426c349SPatrick Venture {
49*5426c349SPatrick Venture     /* Verify the sensors key must be present. */
50*5426c349SPatrick Venture     auto j2 = R"(
51*5426c349SPatrick Venture       {
52*5426c349SPatrick Venture         "zones": [{
53*5426c349SPatrick Venture           "id": 1,
54*5426c349SPatrick Venture           "minThermalRpm": 3000.0,
55*5426c349SPatrick Venture           "failsafePercent": 75.0,
56*5426c349SPatrick Venture           "pids": [{
57*5426c349SPatrick Venture             "name": "fan1-5",
58*5426c349SPatrick Venture             "type": "fan",
59*5426c349SPatrick Venture             "inputs": ["fan1", "fan5"],
60*5426c349SPatrick Venture             "setpoint": 90.0,
61*5426c349SPatrick Venture             "pid": {
62*5426c349SPatrick Venture               "samplePeriod": 0.1,
63*5426c349SPatrick Venture               "proportionalCoeff": 0.0,
64*5426c349SPatrick Venture               "integralCoeff": 0.0,
65*5426c349SPatrick Venture               "feedFwdOffOffsetCoeff": 0.0,
66*5426c349SPatrick Venture               "feedFwdGainCoeff": 0.010,
67*5426c349SPatrick Venture               "integralLimit_min": 0.0,
68*5426c349SPatrick Venture               "integralLimit_max": 0.0,
69*5426c349SPatrick Venture               "outLim_min": 30.0,
70*5426c349SPatrick Venture               "outLim_max": 100.0,
71*5426c349SPatrick Venture               "slewNeg": 0.0,
72*5426c349SPatrick Venture               "slewPos": 0.0
73*5426c349SPatrick Venture             }
74*5426c349SPatrick Venture           }]
75*5426c349SPatrick Venture         }]
76*5426c349SPatrick Venture       }
77*5426c349SPatrick Venture     )"_json;
78*5426c349SPatrick Venture 
79*5426c349SPatrick Venture     EXPECT_THROW(validateJson(j2), ConfigurationException);
80*5426c349SPatrick Venture }
81*5426c349SPatrick Venture 
82*5426c349SPatrick Venture TEST(ConfigurationVerificationTest, VerifyNoZoneKey)
83*5426c349SPatrick Venture {
84*5426c349SPatrick Venture     /* Verify the zones key must be present. */
85*5426c349SPatrick Venture     auto j2 = R"(
86*5426c349SPatrick Venture       {
87*5426c349SPatrick Venture         "sensors": [{
88*5426c349SPatrick Venture           "name": "fan1",
89*5426c349SPatrick Venture           "type": "fan",
90*5426c349SPatrick Venture           "readPath": "/xyz/openbmc_project/sensors/fan_tach/fan1"
91*5426c349SPatrick Venture         }]
92*5426c349SPatrick Venture       }
93*5426c349SPatrick Venture     )"_json;
94*5426c349SPatrick Venture 
95*5426c349SPatrick Venture     EXPECT_THROW(validateJson(j2), ConfigurationException);
96*5426c349SPatrick Venture }
97*5426c349SPatrick Venture 
98*5426c349SPatrick Venture TEST(ConfigurationVerificationTest, VerifyNoSensor)
99*5426c349SPatrick Venture {
100*5426c349SPatrick Venture     /* Verify that there needs to be at least one sensor in the sensors key. */
101*5426c349SPatrick Venture     auto j2 = R"(
102*5426c349SPatrick Venture       {
103*5426c349SPatrick Venture         "sensors": [],
104*5426c349SPatrick Venture         "zones": [{
105*5426c349SPatrick Venture           "id": 1,
106*5426c349SPatrick Venture           "minThermalRpm": 3000.0,
107*5426c349SPatrick Venture           "failsafePercent": 75.0,
108*5426c349SPatrick Venture           "pids": [{
109*5426c349SPatrick Venture             "name": "fan1-5",
110*5426c349SPatrick Venture             "type": "fan",
111*5426c349SPatrick Venture             "inputs": ["fan1", "fan5"],
112*5426c349SPatrick Venture             "setpoint": 90.0,
113*5426c349SPatrick Venture             "pid": {
114*5426c349SPatrick Venture               "samplePeriod": 0.1,
115*5426c349SPatrick Venture               "proportionalCoeff": 0.0,
116*5426c349SPatrick Venture               "integralCoeff": 0.0,
117*5426c349SPatrick Venture               "feedFwdOffOffsetCoeff": 0.0,
118*5426c349SPatrick Venture               "feedFwdGainCoeff": 0.010,
119*5426c349SPatrick Venture               "integralLimit_min": 0.0,
120*5426c349SPatrick Venture               "integralLimit_max": 0.0,
121*5426c349SPatrick Venture               "outLim_min": 30.0,
122*5426c349SPatrick Venture               "outLim_max": 100.0,
123*5426c349SPatrick Venture               "slewNeg": 0.0,
124*5426c349SPatrick Venture               "slewPos": 0.0
125*5426c349SPatrick Venture             }
126*5426c349SPatrick Venture           }]
127*5426c349SPatrick Venture         }]
128*5426c349SPatrick Venture       }
129*5426c349SPatrick Venture     )"_json;
130*5426c349SPatrick Venture 
131*5426c349SPatrick Venture     EXPECT_THROW(validateJson(j2), ConfigurationException);
132*5426c349SPatrick Venture }
133*5426c349SPatrick Venture 
134*5426c349SPatrick Venture TEST(ConfigurationVerificationTest, VerifyNoPidInZone)
135*5426c349SPatrick Venture {
136*5426c349SPatrick Venture     /* Verify that there needs to be at least one PID in the zone. */
137*5426c349SPatrick Venture     auto j2 = R"(
138*5426c349SPatrick Venture       {
139*5426c349SPatrick Venture         "sensors": [{
140*5426c349SPatrick Venture           "name": "fan1",
141*5426c349SPatrick Venture           "type": "fan",
142*5426c349SPatrick Venture           "readPath": "/xyz/openbmc_project/sensors/fan_tach/fan1"
143*5426c349SPatrick Venture         }],
144*5426c349SPatrick Venture         "zones": [{
145*5426c349SPatrick Venture           "id": 1,
146*5426c349SPatrick Venture           "minThermalRpm": 3000.0,
147*5426c349SPatrick Venture           "failsafePercent": 75.0,
148*5426c349SPatrick Venture           "pids": []
149*5426c349SPatrick Venture         }]
150*5426c349SPatrick Venture       }
151*5426c349SPatrick Venture     )"_json;
152*5426c349SPatrick Venture 
153*5426c349SPatrick Venture     EXPECT_THROW(validateJson(j2), ConfigurationException);
154*5426c349SPatrick Venture }
155