blob: 5d47d7d9686f153dfc3312a827c25ec4d19006c9 [file] [log] [blame]
Shawn McCarney3daeb912019-10-28 15:04:17 -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 */
Shawn McCarney494ef032019-11-07 15:56:50 -060016#include "device.hpp"
Shawn McCarney3daeb912019-10-28 15:04:17 -050017#include "id_map.hpp"
Shawn McCarney494ef032019-11-07 15:56:50 -060018#include "rail.hpp"
19#include "rule.hpp"
Shawn McCarney3daeb912019-10-28 15:04:17 -050020
21#include <exception>
22#include <stdexcept>
23#include <string>
24
25#include <gtest/gtest.h>
26
27using namespace phosphor::power::regulators;
28
29TEST(IDMapTests, AddDevice)
30{
31 IDMap idMap{};
32
33 // Create device
34 std::string id{"vio_reg"};
35 Device device{id};
36
37 // Verify device is not initially in map
38 EXPECT_THROW(idMap.getDevice(id), std::invalid_argument);
39
40 // Add device to map
41 idMap.addDevice(device);
42
43 // Verify device is now in map
44 try
45 {
46 Device& deviceFound = idMap.getDevice(id);
47 EXPECT_EQ(deviceFound.getID(), id);
48 EXPECT_EQ(&deviceFound, &device);
49 }
50 catch (const std::exception& error)
51 {
52 ADD_FAILURE() << "Should not have caught exception.";
53 }
54
55 // Verify different device is not in map
56 EXPECT_THROW(idMap.getDevice("vio_reg2"), std::invalid_argument);
57}
58
59TEST(IDMapTests, AddRail)
60{
61 IDMap idMap{};
62
63 // Create rail
64 std::string id{"vio0"};
65 Rail rail{id};
66
67 // Verify rail is not initially in map
68 EXPECT_THROW(idMap.getRail(id), std::invalid_argument);
69
70 // Add rail to map
71 idMap.addRail(rail);
72
73 // Verify rail is now in map
74 try
75 {
76 Rail& railFound = idMap.getRail(id);
77 EXPECT_EQ(railFound.getID(), id);
78 EXPECT_EQ(&railFound, &rail);
79 }
80 catch (const std::exception& error)
81 {
82 ADD_FAILURE() << "Should not have caught exception.";
83 }
84
85 // Verify different rail is not in map
86 EXPECT_THROW(idMap.getRail("vcs0"), std::invalid_argument);
87}
88
89TEST(IDMapTests, AddRule)
90{
91 IDMap idMap{};
92
93 // Create rule
94 std::string id{"set_voltage_rule"};
95 Rule rule{id};
96
97 // Verify rule is not initially in map
98 EXPECT_THROW(idMap.getRule(id), std::invalid_argument);
99
100 // Add rule to map
101 idMap.addRule(rule);
102
103 // Verify rule is now in map
104 try
105 {
106 Rule& ruleFound = idMap.getRule(id);
107 EXPECT_EQ(ruleFound.getID(), id);
108 EXPECT_EQ(&ruleFound, &rule);
109 }
110 catch (const std::exception& error)
111 {
112 ADD_FAILURE() << "Should not have caught exception.";
113 }
114
115 // Verify different rule is not in map
116 EXPECT_THROW(idMap.getRule("set_voltage_rule_page0"),
117 std::invalid_argument);
118}
119
120TEST(IDMapTests, GetDevice)
121{
122 IDMap idMap{};
123
124 // Add a device to the map
125 std::string id{"vio_reg"};
126 Device device{id};
127 idMap.addDevice(device);
128
129 // Test where ID found in map
130 try
131 {
132 Device& deviceFound = idMap.getDevice(id);
133 EXPECT_EQ(deviceFound.getID(), id);
134 EXPECT_EQ(&deviceFound, &device);
135 }
136 catch (const std::exception& error)
137 {
138 ADD_FAILURE() << "Should not have caught exception.";
139 }
140
141 // Test where ID not found in map
142 try
143 {
144 idMap.getDevice("vcs_reg");
145 ADD_FAILURE() << "Should not have reached this line.";
146 }
147 catch (const std::invalid_argument& ia_error)
148 {
149 EXPECT_STREQ(ia_error.what(),
150 "Unable to find device with ID \"vcs_reg\"");
151 }
152 catch (const std::exception& error)
153 {
154 ADD_FAILURE() << "Should not have caught exception.";
155 }
156}
157
158TEST(IDMapTests, GetRail)
159{
160 IDMap idMap{};
161
162 // Add a rail to the map
163 std::string id{"vio0"};
164 Rail rail{id};
165 idMap.addRail(rail);
166
167 // Test where ID found in map
168 try
169 {
170 Rail& railFound = idMap.getRail(id);
171 EXPECT_EQ(railFound.getID(), id);
172 EXPECT_EQ(&railFound, &rail);
173 }
174 catch (const std::exception& error)
175 {
176 ADD_FAILURE() << "Should not have caught exception.";
177 }
178
179 // Test where ID not found in map
180 try
181 {
182 idMap.getRail("vcs0");
183 ADD_FAILURE() << "Should not have reached this line.";
184 }
185 catch (const std::invalid_argument& ia_error)
186 {
187 EXPECT_STREQ(ia_error.what(), "Unable to find rail with ID \"vcs0\"");
188 }
189 catch (const std::exception& error)
190 {
191 ADD_FAILURE() << "Should not have caught exception.";
192 }
193}
194
195TEST(IDMapTests, GetRule)
196{
197 IDMap idMap{};
198
199 // Add a rule to the map
200 std::string id{"set_voltage_rule"};
201 Rule rule{id};
202 idMap.addRule(rule);
203
204 // Test where ID found in map
205 try
206 {
207 Rule& ruleFound = idMap.getRule(id);
208 EXPECT_EQ(ruleFound.getID(), id);
209 EXPECT_EQ(&ruleFound, &rule);
210 }
211 catch (const std::exception& error)
212 {
213 ADD_FAILURE() << "Should not have caught exception.";
214 }
215
216 // Test where ID not found in map
217 try
218 {
219 idMap.getRule("read_sensors_rule");
220 ADD_FAILURE() << "Should not have reached this line.";
221 }
222 catch (const std::invalid_argument& ia_error)
223 {
224 EXPECT_STREQ(ia_error.what(),
225 "Unable to find rule with ID \"read_sensors_rule\"");
226 }
227 catch (const std::exception& error)
228 {
229 ADD_FAILURE() << "Should not have caught exception.";
230 }
231}