blob: ff13c8373a7c28b3bcb50f02a0c3422c219bfa76 [file] [log] [blame]
Shawn McCarneyc69a2752019-10-30 17:37:30 -05001/**
2 * Copyright © 2019 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#include "action_environment.hpp"
17#include "device.hpp"
18#include "id_map.hpp"
19#include "rule.hpp"
20
21#include <cstddef> // for size_t
22#include <exception>
23#include <stdexcept>
24
25#include <gtest/gtest.h>
26
27using namespace phosphor::power::regulators;
28
29TEST(ActionEnvironmentTests, Constructor)
30{
31 // Create IDMap
32 IDMap idMap{};
33 Device reg1{"regulator1"};
34 idMap.addDevice(reg1);
35
36 // Verify object state after constructor
37 try
38 {
39 ActionEnvironment env{idMap, "regulator1"};
40 EXPECT_EQ(env.getDevice().getID(), "regulator1");
41 EXPECT_EQ(env.getDeviceID(), "regulator1");
42 EXPECT_EQ(env.getRuleDepth(), 0);
43 EXPECT_THROW(env.getVolts(), std::logic_error);
44 EXPECT_EQ(env.hasVolts(), false);
45 }
46 catch (const std::exception& error)
47 {
48 ADD_FAILURE() << "Should not have caught exception.";
49 }
50}
51
52TEST(ActionEnvironmentTests, DecrementRuleDepth)
53{
54 IDMap idMap{};
55 ActionEnvironment env{idMap, ""};
56 EXPECT_EQ(env.getRuleDepth(), 0);
57 env.incrementRuleDepth();
58 env.incrementRuleDepth();
59 EXPECT_EQ(env.getRuleDepth(), 2);
60 env.decrementRuleDepth();
61 EXPECT_EQ(env.getRuleDepth(), 1);
62 env.decrementRuleDepth();
63 EXPECT_EQ(env.getRuleDepth(), 0);
64 env.decrementRuleDepth();
65 EXPECT_EQ(env.getRuleDepth(), 0);
66}
67
68TEST(ActionEnvironmentTests, GetDevice)
69{
70 // Create IDMap
71 IDMap idMap{};
72 Device reg1{"regulator1"};
73 idMap.addDevice(reg1);
74
75 ActionEnvironment env{idMap, "regulator1"};
76
77 // Test where current device ID is in the IDMap
78 try
79 {
80 Device& device = env.getDevice();
81 EXPECT_EQ(device.getID(), "regulator1");
82 EXPECT_EQ(&device, &reg1);
83 }
84 catch (const std::exception& error)
85 {
86 ADD_FAILURE() << "Should not have caught exception.";
87 }
88
89 // Test where current device ID is not in the IDMap
90 env.setDeviceID("regulator2");
91 try
92 {
93 env.getDevice();
94 ADD_FAILURE() << "Should not have reached this line.";
95 }
96 catch (const std::invalid_argument& ia_error)
97 {
98 EXPECT_STREQ(ia_error.what(),
99 "Unable to find device with ID \"regulator2\"");
100 }
101 catch (const std::exception& error)
102 {
103 ADD_FAILURE() << "Should not have caught exception.";
104 }
105}
106
107TEST(ActionEnvironmentTests, GetDeviceID)
108{
109 IDMap idMap{};
110 ActionEnvironment env{idMap, ""};
111 EXPECT_EQ(env.getDeviceID(), "");
112 env.setDeviceID("regulator1");
113 EXPECT_EQ(env.getDeviceID(), "regulator1");
114}
115
116TEST(ActionEnvironmentTests, GetRule)
117{
118 // Create IDMap
119 IDMap idMap{};
Shawn McCarney9fc08e72019-11-07 13:18:55 -0600120 Rule setVoltageRule{"set_voltage_rule",
121 std::vector<std::unique_ptr<Action>>{}};
Shawn McCarneyc69a2752019-10-30 17:37:30 -0500122 idMap.addRule(setVoltageRule);
123
124 ActionEnvironment env{idMap, ""};
125
126 // Test where rule ID is in the IDMap
127 try
128 {
129 Rule& rule = env.getRule("set_voltage_rule");
130 EXPECT_EQ(rule.getID(), "set_voltage_rule");
131 EXPECT_EQ(&rule, &setVoltageRule);
132 }
133 catch (const std::exception& error)
134 {
135 ADD_FAILURE() << "Should not have caught exception.";
136 }
137
138 // Test where rule ID is not in the IDMap
139 try
140 {
141 env.getRule("set_voltage_rule2");
142 ADD_FAILURE() << "Should not have reached this line.";
143 }
144 catch (const std::invalid_argument& ia_error)
145 {
146 EXPECT_STREQ(ia_error.what(),
147 "Unable to find rule with ID \"set_voltage_rule2\"");
148 }
149 catch (const std::exception& error)
150 {
151 ADD_FAILURE() << "Should not have caught exception.";
152 }
153}
154
155TEST(ActionEnvironmentTests, GetRuleDepth)
156{
157 IDMap idMap{};
158 ActionEnvironment env{idMap, ""};
159 EXPECT_EQ(env.getRuleDepth(), 0);
160 env.incrementRuleDepth();
161 EXPECT_EQ(env.getRuleDepth(), 1);
162 env.incrementRuleDepth();
163 EXPECT_EQ(env.getRuleDepth(), 2);
164 env.decrementRuleDepth();
165 EXPECT_EQ(env.getRuleDepth(), 1);
166 env.decrementRuleDepth();
167 EXPECT_EQ(env.getRuleDepth(), 0);
168}
169
170TEST(ActionEnvironmentTests, GetVolts)
171{
172 IDMap idMap{};
173 ActionEnvironment env{idMap, ""};
174 EXPECT_EQ(env.hasVolts(), false);
175
176 // Test where a volts value has not been set
177 try
178 {
179 env.getVolts();
180 }
181 catch (const std::logic_error& l_error)
182 {
183 EXPECT_STREQ(l_error.what(), "No volts value has been set.");
184 }
185 catch (const std::exception& error)
186 {
187 ADD_FAILURE() << "Should not have caught exception.";
188 }
189
190 // Test where a volts value has been set
191 env.setVolts(1.31);
192 try
193 {
194 double volts = env.getVolts();
195 EXPECT_EQ(volts, 1.31);
196 }
197 catch (const std::exception& error)
198 {
199 ADD_FAILURE() << "Should not have caught exception.";
200 }
201}
202
203TEST(ActionEnvironmentTests, HasVolts)
204{
205 IDMap idMap{};
206 ActionEnvironment env{idMap, ""};
207 EXPECT_EQ(env.hasVolts(), false);
208 env.setVolts(1.31);
209 EXPECT_EQ(env.hasVolts(), true);
210}
211
212TEST(ActionEnvironmentTests, IncrementRuleDepth)
213{
214 IDMap idMap{};
215 ActionEnvironment env{idMap, ""};
216 EXPECT_EQ(env.getRuleDepth(), 0);
217
218 // Test where rule depth has not exceeded maximum
219 try
220 {
221 for (size_t i = 1; i <= env.maxRuleDepth; ++i)
222 {
223 env.incrementRuleDepth();
224 EXPECT_EQ(env.getRuleDepth(), i);
225 }
226 }
227 catch (const std::exception& error)
228 {
229 ADD_FAILURE() << "Should not have caught exception.";
230 }
231
232 // Test where rule depth has exceeded maximum
233 try
234 {
235 env.incrementRuleDepth();
236 }
237 catch (const std::runtime_error& r_error)
238 {
239 EXPECT_STREQ(r_error.what(), "Maximum rule depth exceeded.");
240 }
241 catch (const std::exception& error)
242 {
243 ADD_FAILURE() << "Should not have caught exception.";
244 }
245}
246
247TEST(ActionEnvironmentTests, SetDeviceID)
248{
249 IDMap idMap{};
250 Device reg1{"regulator1"};
251 idMap.addDevice(reg1);
252 ActionEnvironment env{idMap, "regulator1"};
253
254 EXPECT_EQ(env.getDeviceID(), "regulator1");
255 env.setDeviceID("regulator2");
256 EXPECT_EQ(env.getDeviceID(), "regulator2");
257}
258
259TEST(ActionEnvironmentTests, SetVolts)
260{
261 try
262 {
263 IDMap idMap{};
264 ActionEnvironment env{idMap, ""};
265 EXPECT_EQ(env.hasVolts(), false);
266 env.setVolts(2.35);
267 EXPECT_EQ(env.hasVolts(), true);
268 EXPECT_EQ(env.getVolts(), 2.35);
269 }
270 catch (const std::exception& error)
271 {
272 ADD_FAILURE() << "Should not have caught exception.";
273 }
274}