blob: 5561705a968c71b6db7cd5d64ed3de2f37b4dbad [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"
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060018#include "i2c_interface.hpp"
Shawn McCarneyc69a2752019-10-30 17:37:30 -050019#include "id_map.hpp"
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060020#include "mocked_i2c_interface.hpp"
Shawn McCarneyc69a2752019-10-30 17:37:30 -050021#include "rule.hpp"
22
23#include <cstddef> // for size_t
24#include <exception>
Shawn McCarney2134ca62019-11-11 13:06:18 -060025#include <memory>
Shawn McCarneyc69a2752019-10-30 17:37:30 -050026#include <stdexcept>
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060027#include <utility>
Shawn McCarney2134ca62019-11-11 13:06:18 -060028#include <vector>
Shawn McCarneyc69a2752019-10-30 17:37:30 -050029
30#include <gtest/gtest.h>
31
32using namespace phosphor::power::regulators;
33
34TEST(ActionEnvironmentTests, Constructor)
35{
36 // Create IDMap
37 IDMap idMap{};
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060038
39 // Create Device and add to IDMap
40 std::unique_ptr<i2c::I2CInterface> i2cInterface =
41 i2c::create(1, 0x70, i2c::I2CInterface::InitialState::CLOSED);
42 Device reg1{"regulator1", true, "/system/chassis/motherboard/reg1",
43 std::move(i2cInterface)};
Shawn McCarneyc69a2752019-10-30 17:37:30 -050044 idMap.addDevice(reg1);
45
46 // Verify object state after constructor
47 try
48 {
49 ActionEnvironment env{idMap, "regulator1"};
50 EXPECT_EQ(env.getDevice().getID(), "regulator1");
51 EXPECT_EQ(env.getDeviceID(), "regulator1");
52 EXPECT_EQ(env.getRuleDepth(), 0);
53 EXPECT_THROW(env.getVolts(), std::logic_error);
54 EXPECT_EQ(env.hasVolts(), false);
55 }
56 catch (const std::exception& error)
57 {
58 ADD_FAILURE() << "Should not have caught exception.";
59 }
60}
61
62TEST(ActionEnvironmentTests, DecrementRuleDepth)
63{
64 IDMap idMap{};
65 ActionEnvironment env{idMap, ""};
66 EXPECT_EQ(env.getRuleDepth(), 0);
Shawn McCarney2134ca62019-11-11 13:06:18 -060067 env.incrementRuleDepth("set_voltage_rule");
68 env.incrementRuleDepth("set_voltage_rule");
Shawn McCarneyc69a2752019-10-30 17:37:30 -050069 EXPECT_EQ(env.getRuleDepth(), 2);
70 env.decrementRuleDepth();
71 EXPECT_EQ(env.getRuleDepth(), 1);
72 env.decrementRuleDepth();
73 EXPECT_EQ(env.getRuleDepth(), 0);
74 env.decrementRuleDepth();
75 EXPECT_EQ(env.getRuleDepth(), 0);
76}
77
78TEST(ActionEnvironmentTests, GetDevice)
79{
80 // Create IDMap
81 IDMap idMap{};
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060082
83 // Create Device and add to IDMap
84 std::unique_ptr<i2c::I2CInterface> i2cInterface =
85 i2c::create(1, 0x70, i2c::I2CInterface::InitialState::CLOSED);
86 Device reg1{"regulator1", true, "/system/chassis/motherboard/reg1",
87 std::move(i2cInterface)};
Shawn McCarneyc69a2752019-10-30 17:37:30 -050088 idMap.addDevice(reg1);
89
90 ActionEnvironment env{idMap, "regulator1"};
91
92 // Test where current device ID is in the IDMap
93 try
94 {
95 Device& device = env.getDevice();
96 EXPECT_EQ(device.getID(), "regulator1");
97 EXPECT_EQ(&device, &reg1);
98 }
99 catch (const std::exception& error)
100 {
101 ADD_FAILURE() << "Should not have caught exception.";
102 }
103
104 // Test where current device ID is not in the IDMap
105 env.setDeviceID("regulator2");
106 try
107 {
108 env.getDevice();
109 ADD_FAILURE() << "Should not have reached this line.";
110 }
111 catch (const std::invalid_argument& ia_error)
112 {
113 EXPECT_STREQ(ia_error.what(),
114 "Unable to find device with ID \"regulator2\"");
115 }
116 catch (const std::exception& error)
117 {
118 ADD_FAILURE() << "Should not have caught exception.";
119 }
120}
121
122TEST(ActionEnvironmentTests, GetDeviceID)
123{
124 IDMap idMap{};
125 ActionEnvironment env{idMap, ""};
126 EXPECT_EQ(env.getDeviceID(), "");
127 env.setDeviceID("regulator1");
128 EXPECT_EQ(env.getDeviceID(), "regulator1");
129}
130
131TEST(ActionEnvironmentTests, GetRule)
132{
133 // Create IDMap
134 IDMap idMap{};
Shawn McCarney9fc08e72019-11-07 13:18:55 -0600135 Rule setVoltageRule{"set_voltage_rule",
136 std::vector<std::unique_ptr<Action>>{}};
Shawn McCarneyc69a2752019-10-30 17:37:30 -0500137 idMap.addRule(setVoltageRule);
138
139 ActionEnvironment env{idMap, ""};
140
141 // Test where rule ID is in the IDMap
142 try
143 {
144 Rule& rule = env.getRule("set_voltage_rule");
145 EXPECT_EQ(rule.getID(), "set_voltage_rule");
146 EXPECT_EQ(&rule, &setVoltageRule);
147 }
148 catch (const std::exception& error)
149 {
150 ADD_FAILURE() << "Should not have caught exception.";
151 }
152
153 // Test where rule ID is not in the IDMap
154 try
155 {
156 env.getRule("set_voltage_rule2");
157 ADD_FAILURE() << "Should not have reached this line.";
158 }
159 catch (const std::invalid_argument& ia_error)
160 {
161 EXPECT_STREQ(ia_error.what(),
162 "Unable to find rule with ID \"set_voltage_rule2\"");
163 }
164 catch (const std::exception& error)
165 {
166 ADD_FAILURE() << "Should not have caught exception.";
167 }
168}
169
170TEST(ActionEnvironmentTests, GetRuleDepth)
171{
172 IDMap idMap{};
173 ActionEnvironment env{idMap, ""};
174 EXPECT_EQ(env.getRuleDepth(), 0);
Shawn McCarney2134ca62019-11-11 13:06:18 -0600175 env.incrementRuleDepth("set_voltage_rule");
Shawn McCarneyc69a2752019-10-30 17:37:30 -0500176 EXPECT_EQ(env.getRuleDepth(), 1);
Shawn McCarney2134ca62019-11-11 13:06:18 -0600177 env.incrementRuleDepth("set_voltage_rule");
Shawn McCarneyc69a2752019-10-30 17:37:30 -0500178 EXPECT_EQ(env.getRuleDepth(), 2);
179 env.decrementRuleDepth();
180 EXPECT_EQ(env.getRuleDepth(), 1);
181 env.decrementRuleDepth();
182 EXPECT_EQ(env.getRuleDepth(), 0);
183}
184
185TEST(ActionEnvironmentTests, GetVolts)
186{
187 IDMap idMap{};
188 ActionEnvironment env{idMap, ""};
189 EXPECT_EQ(env.hasVolts(), false);
190
191 // Test where a volts value has not been set
192 try
193 {
194 env.getVolts();
195 }
196 catch (const std::logic_error& l_error)
197 {
198 EXPECT_STREQ(l_error.what(), "No volts value has been set.");
199 }
200 catch (const std::exception& error)
201 {
202 ADD_FAILURE() << "Should not have caught exception.";
203 }
204
205 // Test where a volts value has been set
206 env.setVolts(1.31);
207 try
208 {
209 double volts = env.getVolts();
210 EXPECT_EQ(volts, 1.31);
211 }
212 catch (const std::exception& error)
213 {
214 ADD_FAILURE() << "Should not have caught exception.";
215 }
216}
217
218TEST(ActionEnvironmentTests, HasVolts)
219{
220 IDMap idMap{};
221 ActionEnvironment env{idMap, ""};
222 EXPECT_EQ(env.hasVolts(), false);
223 env.setVolts(1.31);
224 EXPECT_EQ(env.hasVolts(), true);
225}
226
227TEST(ActionEnvironmentTests, IncrementRuleDepth)
228{
229 IDMap idMap{};
230 ActionEnvironment env{idMap, ""};
231 EXPECT_EQ(env.getRuleDepth(), 0);
232
233 // Test where rule depth has not exceeded maximum
234 try
235 {
236 for (size_t i = 1; i <= env.maxRuleDepth; ++i)
237 {
Shawn McCarney2134ca62019-11-11 13:06:18 -0600238 env.incrementRuleDepth("set_voltage_rule");
Shawn McCarneyc69a2752019-10-30 17:37:30 -0500239 EXPECT_EQ(env.getRuleDepth(), i);
240 }
241 }
242 catch (const std::exception& error)
243 {
244 ADD_FAILURE() << "Should not have caught exception.";
245 }
246
247 // Test where rule depth has exceeded maximum
248 try
249 {
Shawn McCarney2134ca62019-11-11 13:06:18 -0600250 env.incrementRuleDepth("set_voltage_rule");
Shawn McCarneyc69a2752019-10-30 17:37:30 -0500251 }
252 catch (const std::runtime_error& r_error)
253 {
Shawn McCarney2134ca62019-11-11 13:06:18 -0600254 EXPECT_STREQ(r_error.what(),
255 "Maximum rule depth exceeded by rule set_voltage_rule.");
Shawn McCarneyc69a2752019-10-30 17:37:30 -0500256 }
257 catch (const std::exception& error)
258 {
259 ADD_FAILURE() << "Should not have caught exception.";
260 }
261}
262
263TEST(ActionEnvironmentTests, SetDeviceID)
264{
265 IDMap idMap{};
Shawn McCarneyc69a2752019-10-30 17:37:30 -0500266 ActionEnvironment env{idMap, "regulator1"};
Shawn McCarneyc69a2752019-10-30 17:37:30 -0500267 EXPECT_EQ(env.getDeviceID(), "regulator1");
268 env.setDeviceID("regulator2");
269 EXPECT_EQ(env.getDeviceID(), "regulator2");
270}
271
272TEST(ActionEnvironmentTests, SetVolts)
273{
274 try
275 {
276 IDMap idMap{};
277 ActionEnvironment env{idMap, ""};
278 EXPECT_EQ(env.hasVolts(), false);
279 env.setVolts(2.35);
280 EXPECT_EQ(env.hasVolts(), true);
281 EXPECT_EQ(env.getVolts(), 2.35);
282 }
283 catch (const std::exception& error)
284 {
285 ADD_FAILURE() << "Should not have caught exception.";
286 }
287}