regulators: Implements support for set_device action

Enhance the configuration file parser to support the set_device
action element.

Signed-off-by: Bob King <Bob_King@wistron.com>
Change-Id: If9d1e25dc35370536fab9ab4ef7f2bf269e46d20
diff --git a/phosphor-regulators/src/config_file_parser.cpp b/phosphor-regulators/src/config_file_parser.cpp
index 14bf942..b94e308 100644
--- a/phosphor-regulators/src/config_file_parser.cpp
+++ b/phosphor-regulators/src/config_file_parser.cpp
@@ -146,9 +146,8 @@
     }
     else if (element.contains("set_device"))
     {
-        // TODO: Not implemented yet
-        // action = parseSetDevice(element["set_device"]);
-        // ++propertyCount;
+        action = parseSetDevice(element["set_device"]);
+        ++propertyCount;
     }
     else
     {
@@ -881,6 +880,14 @@
     return std::make_unique<SensorMonitoring>(std::move(actions));
 }
 
+std::unique_ptr<SetDeviceAction> parseSetDevice(const json& element)
+{
+    // String deviceID
+    std::string deviceID = parseString(element);
+
+    return std::make_unique<SetDeviceAction>(deviceID);
+}
+
 } // namespace internal
 
 } // namespace phosphor::power::regulators::config_file_parser
diff --git a/phosphor-regulators/src/config_file_parser.hpp b/phosphor-regulators/src/config_file_parser.hpp
index 771c699..e3f6bde 100644
--- a/phosphor-regulators/src/config_file_parser.hpp
+++ b/phosphor-regulators/src/config_file_parser.hpp
@@ -36,6 +36,7 @@
 #include "rule.hpp"
 #include "run_rule_action.hpp"
 #include "sensor_monitoring.hpp"
+#include "set_device_action.hpp"
 
 #include <nlohmann/json.hpp>
 
@@ -613,6 +614,18 @@
     parseSensorMonitoring(const nlohmann::json& element);
 
 /**
+ * Parses a JSON element containing a set_device action.
+ *
+ * Returns the corresponding C++ SetDeviceAction object.
+ *
+ * Throws an exception if parsing fails.
+ *
+ * @param element JSON element
+ * @return SetDeviceAction object
+ */
+std::unique_ptr<SetDeviceAction> parseSetDevice(const nlohmann::json& element);
+
+/**
  * Parses a JSON element containing a string.
  *
  * Returns the corresponding C++ string.
diff --git a/phosphor-regulators/test/config_file_parser_tests.cpp b/phosphor-regulators/test/config_file_parser_tests.cpp
index 42c1e58..f5d683a 100644
--- a/phosphor-regulators/test/config_file_parser_tests.cpp
+++ b/phosphor-regulators/test/config_file_parser_tests.cpp
@@ -36,6 +36,7 @@
 #include "rule.hpp"
 #include "run_rule_action.hpp"
 #include "sensor_monitoring.hpp"
+#include "set_device_action.hpp"
 #include "tmp_file.hpp"
 
 #include <sys/stat.h> // for chmod()
@@ -418,7 +419,15 @@
     }
 
     // Test where works: set_device action type specified
-    // TODO: Not implemented yet
+    {
+        const json element = R"(
+            {
+              "set_device": "io_expander2"
+            }
+        )"_json;
+        std::unique_ptr<Action> action = parseAction(element);
+        EXPECT_NE(action.get(), nullptr);
+    }
 
     // Test where fails: Element is not an object
     try
@@ -3848,6 +3857,40 @@
     }
 }
 
+TEST(ConfigFileParserTests, ParseSetDevice)
+{
+    // Test where works
+    {
+        const json element = "regulator1";
+        std::unique_ptr<SetDeviceAction> action = parseSetDevice(element);
+        EXPECT_EQ(action->getDeviceID(), "regulator1");
+    }
+
+    // Test where fails: Element is not a string
+    try
+    {
+        const json element = 1;
+        parseSetDevice(element);
+        ADD_FAILURE() << "Should not have reached this line.";
+    }
+    catch (const std::invalid_argument& e)
+    {
+        EXPECT_STREQ(e.what(), "Element is not a string");
+    }
+
+    // Test where fails: Empty string
+    try
+    {
+        const json element = "";
+        parseSetDevice(element);
+        ADD_FAILURE() << "Should not have reached this line.";
+    }
+    catch (const std::invalid_argument& e)
+    {
+        EXPECT_STREQ(e.what(), "Element contains an empty string");
+    }
+}
+
 TEST(ConfigFileParserTests, ParseString)
 {
     // Test where works: Empty string