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