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