regulators: Enhance IDMap to detect duplicate IDs

The IDMap class provides a mapping from unique string IDs to the
corresponding Device, Rail, and Rule objects.

Enhance IDMap to detect duplicate IDs.  Throw an exception if the caller
tries to add a Device, Rail, or Rule whose ID already exists in the map.

Signed-off-by: Shawn McCarney <shawnmm@us.ibm.com>
Change-Id: I81176fae1415cd5a89dc4ff47f80d1bc70e0e004
diff --git a/phosphor-regulators/test/id_map_tests.cpp b/phosphor-regulators/test/id_map_tests.cpp
index d3053d7..991949b 100644
--- a/phosphor-regulators/test/id_map_tests.cpp
+++ b/phosphor-regulators/test/id_map_tests.cpp
@@ -62,6 +62,21 @@
 
     // Verify different device is not in map
     EXPECT_THROW(idMap.getDevice("vio_reg2"), std::invalid_argument);
+
+    // Test where device ID already exists in map
+    try
+    {
+        i2cInterface =
+            i2c::create(1, 0x72, i2c::I2CInterface::InitialState::CLOSED);
+        Device device2{"vio_reg", true, "/system/chassis/motherboard/vio_reg2",
+                       std::move(i2cInterface)};
+        idMap.addDevice(device2);
+    }
+    catch (const std::invalid_argument& error)
+    {
+        EXPECT_STREQ(error.what(),
+                     "Unable to add device: Duplicate ID \"vio_reg\"");
+    }
 }
 
 TEST(IDMapTests, AddRail)
@@ -92,6 +107,17 @@
 
     // Verify different rail is not in map
     EXPECT_THROW(idMap.getRail("vcs0"), std::invalid_argument);
+
+    // Test where rail ID already exists in map
+    try
+    {
+        Rail rail2{"vio0"};
+        idMap.addRail(rail2);
+    }
+    catch (const std::invalid_argument& error)
+    {
+        EXPECT_STREQ(error.what(), "Unable to add rail: Duplicate ID \"vio0\"");
+    }
 }
 
 TEST(IDMapTests, AddRule)
@@ -123,6 +149,18 @@
     // Verify different rule is not in map
     EXPECT_THROW(idMap.getRule("set_voltage_rule_page0"),
                  std::invalid_argument);
+
+    // Test where rule ID already exists in map
+    try
+    {
+        Rule rule2{"set_voltage_rule", std::vector<std::unique_ptr<Action>>{}};
+        idMap.addRule(rule2);
+    }
+    catch (const std::invalid_argument& error)
+    {
+        EXPECT_STREQ(error.what(),
+                     "Unable to add rule: Duplicate ID \"set_voltage_rule\"");
+    }
 }
 
 TEST(IDMapTests, GetDevice)