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