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