blob: 0e5b8db94195b28b2b4fbda193916851c4c1261c [file] [log] [blame]
Shawn McCarneyc3991f12020-04-05 13:16:06 -05001/**
2 * Copyright © 2020 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 McCarneyc3991f12020-04-05 13:16:06 -050016#include "chassis.hpp"
Shawn McCarneydb0b8332020-04-06 14:13:04 -050017#include "device.hpp"
18#include "id_map.hpp"
19#include "rail.hpp"
Shawn McCarneyc3991f12020-04-05 13:16:06 -050020#include "rule.hpp"
21#include "system.hpp"
Shawn McCarneydb0b8332020-04-06 14:13:04 -050022#include "test_utils.hpp"
Shawn McCarneyc3991f12020-04-05 13:16:06 -050023
24#include <memory>
Shawn McCarneydb0b8332020-04-06 14:13:04 -050025#include <stdexcept>
Shawn McCarneyc3991f12020-04-05 13:16:06 -050026#include <utility>
27#include <vector>
28
29#include <gtest/gtest.h>
30
31using namespace phosphor::power::regulators;
Shawn McCarneydb0b8332020-04-06 14:13:04 -050032using namespace phosphor::power::regulators::test_utils;
Shawn McCarneyc3991f12020-04-05 13:16:06 -050033
34TEST(SystemTests, Constructor)
35{
36 // Create Rules
37 std::vector<std::unique_ptr<Rule>> rules{};
Shawn McCarneydb0b8332020-04-06 14:13:04 -050038 rules.emplace_back(createRule("set_voltage_rule"));
Shawn McCarneyc3991f12020-04-05 13:16:06 -050039
40 // Create Chassis
41 std::vector<std::unique_ptr<Chassis>> chassis{};
Shawn McCarneydb0b8332020-04-06 14:13:04 -050042 std::vector<std::unique_ptr<Device>> devices{};
43 devices.emplace_back(createDevice("reg1", {"rail1"}));
44 chassis.emplace_back(std::make_unique<Chassis>(1, std::move(devices)));
Shawn McCarneyc3991f12020-04-05 13:16:06 -050045
46 // Create System
47 System system{std::move(rules), std::move(chassis)};
48 EXPECT_EQ(system.getChassis().size(), 1);
49 EXPECT_EQ(system.getChassis()[0]->getNumber(), 1);
Shawn McCarneydb0b8332020-04-06 14:13:04 -050050 EXPECT_NO_THROW(system.getIDMap().getRule("set_voltage_rule"));
51 EXPECT_NO_THROW(system.getIDMap().getDevice("reg1"));
52 EXPECT_NO_THROW(system.getIDMap().getRail("rail1"));
53 EXPECT_THROW(system.getIDMap().getRail("rail2"), std::invalid_argument);
Shawn McCarneyc3991f12020-04-05 13:16:06 -050054 EXPECT_EQ(system.getRules().size(), 1);
55 EXPECT_EQ(system.getRules()[0]->getID(), "set_voltage_rule");
56}
57
58TEST(SystemTests, GetChassis)
59{
60 // Specify an empty rules vector
61 std::vector<std::unique_ptr<Rule>> rules{};
62
63 // Create Chassis
64 std::vector<std::unique_ptr<Chassis>> chassis{};
65 chassis.emplace_back(std::make_unique<Chassis>(1));
66 chassis.emplace_back(std::make_unique<Chassis>(3));
67
68 // Create System
69 System system{std::move(rules), std::move(chassis)};
70 EXPECT_EQ(system.getChassis().size(), 2);
71 EXPECT_EQ(system.getChassis()[0]->getNumber(), 1);
72 EXPECT_EQ(system.getChassis()[1]->getNumber(), 3);
73}
74
75TEST(SystemTests, GetIDMap)
76{
Shawn McCarneydb0b8332020-04-06 14:13:04 -050077 // Create Rules
78 std::vector<std::unique_ptr<Rule>> rules{};
79 rules.emplace_back(createRule("set_voltage_rule"));
80 rules.emplace_back(createRule("read_sensors_rule"));
81
82 // Create Chassis
83 std::vector<std::unique_ptr<Chassis>> chassis{};
84 {
85 // Chassis 1
86 std::vector<std::unique_ptr<Device>> devices{};
87 devices.emplace_back(createDevice("reg1", {"rail1"}));
88 devices.emplace_back(createDevice("reg2", {"rail2a", "rail2b"}));
89 chassis.emplace_back(std::make_unique<Chassis>(1, std::move(devices)));
90 }
91 {
92 // Chassis 2
93 std::vector<std::unique_ptr<Device>> devices{};
94 devices.emplace_back(createDevice("reg3", {"rail3a", "rail3b"}));
95 devices.emplace_back(createDevice("reg4"));
96 chassis.emplace_back(std::make_unique<Chassis>(2, std::move(devices)));
97 }
98
99 // Create System
100 System system{std::move(rules), std::move(chassis)};
101 const IDMap& idMap = system.getIDMap();
102
103 // Verify all Rules are in the IDMap
104 EXPECT_NO_THROW(idMap.getRule("set_voltage_rule"));
105 EXPECT_NO_THROW(idMap.getRule("read_sensors_rule"));
106 EXPECT_THROW(idMap.getRule("set_voltage_rule2"), std::invalid_argument);
107
108 // Verify all Devices are in the IDMap
109 EXPECT_NO_THROW(idMap.getDevice("reg1"));
110 EXPECT_NO_THROW(idMap.getDevice("reg2"));
111 EXPECT_NO_THROW(idMap.getDevice("reg3"));
112 EXPECT_NO_THROW(idMap.getDevice("reg4"));
113 EXPECT_THROW(idMap.getDevice("reg5"), std::invalid_argument);
114
115 // Verify all Rails are in the IDMap
116 EXPECT_NO_THROW(idMap.getRail("rail1"));
117 EXPECT_NO_THROW(idMap.getRail("rail2a"));
118 EXPECT_NO_THROW(idMap.getRail("rail2b"));
119 EXPECT_NO_THROW(idMap.getRail("rail3a"));
120 EXPECT_NO_THROW(idMap.getRail("rail3b"));
121 EXPECT_THROW(idMap.getRail("rail4"), std::invalid_argument);
Shawn McCarneyc3991f12020-04-05 13:16:06 -0500122}
123
124TEST(SystemTests, GetRules)
125{
126 // Create Rules
127 std::vector<std::unique_ptr<Rule>> rules{};
Shawn McCarneydb0b8332020-04-06 14:13:04 -0500128 rules.emplace_back(createRule("set_voltage_rule"));
129 rules.emplace_back(createRule("read_sensors_rule"));
Shawn McCarneyc3991f12020-04-05 13:16:06 -0500130
131 // Create Chassis
132 std::vector<std::unique_ptr<Chassis>> chassis{};
133 chassis.emplace_back(std::make_unique<Chassis>(1));
134
135 // Create System
136 System system{std::move(rules), std::move(chassis)};
137 EXPECT_EQ(system.getRules().size(), 2);
138 EXPECT_EQ(system.getRules()[0]->getID(), "set_voltage_rule");
139 EXPECT_EQ(system.getRules()[1]->getID(), "read_sensors_rule");
140}