regulators: Implements support for run_rule

Implements support for parsing the run_rule JSON elements in the
configuration file parser.

Signed-off-by: Bob King <Bob_King@wistron.com>
Change-Id: I9b8d96716a7710e3829fc346d89ef418cc091858
diff --git a/phosphor-regulators/src/config_file_parser.cpp b/phosphor-regulators/src/config_file_parser.cpp
index 3959978..1d64c96 100644
--- a/phosphor-regulators/src/config_file_parser.cpp
+++ b/phosphor-regulators/src/config_file_parser.cpp
@@ -148,9 +148,8 @@
     }
     else if (element.contains("run_rule"))
     {
-        // TODO: Not implemented yet
-        // action = parseRunRule(element["run_rule"]);
-        // ++propertyCount;
+        action = parseRunRule(element["run_rule"]);
+        ++propertyCount;
     }
     else if (element.contains("set_device"))
     {
@@ -422,6 +421,14 @@
     return rules;
 }
 
+std::unique_ptr<RunRuleAction> parseRunRule(const json& element)
+{
+    // String ruleID
+    std::string ruleID = parseString(element);
+
+    return std::make_unique<RunRuleAction>(ruleID);
+}
+
 } // 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 a8286f1..0a931d8 100644
--- a/phosphor-regulators/src/config_file_parser.hpp
+++ b/phosphor-regulators/src/config_file_parser.hpp
@@ -22,6 +22,7 @@
 #include "i2c_write_bytes_action.hpp"
 #include "pmbus_write_vout_command_action.hpp"
 #include "rule.hpp"
+#include "run_rule_action.hpp"
 
 #include <nlohmann/json.hpp>
 
@@ -367,6 +368,18 @@
     parseRuleArray(const nlohmann::json& element);
 
 /**
+ * Parses a JSON element containing a run_rule action.
+ *
+ * Returns the corresponding C++ RunRuleAction object.
+ *
+ * Throws an exception if parsing fails.
+ *
+ * @param element JSON element
+ * @return RunRuleAction object
+ */
+std::unique_ptr<RunRuleAction> parseRunRule(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 df2d87e..9942c04 100644
--- a/phosphor-regulators/test/config_file_parser_tests.cpp
+++ b/phosphor-regulators/test/config_file_parser_tests.cpp
@@ -24,6 +24,7 @@
 #include "pmbus_utils.hpp"
 #include "pmbus_write_vout_command_action.hpp"
 #include "rule.hpp"
+#include "run_rule_action.hpp"
 #include "tmp_file.hpp"
 
 #include <sys/stat.h> // for chmod()
@@ -318,7 +319,15 @@
     }
 
     // Test where works: run_rule action type specified
-    // TODO: Not implemented yet
+    {
+        const json element = R"(
+            {
+              "run_rule": "set_voltage_rule"
+            }
+        )"_json;
+        std::unique_ptr<Action> action = parseAction(element);
+        EXPECT_NE(action.get(), nullptr);
+    }
 
     // Test where works: set_device action type specified
     // TODO: Not implemented yet
@@ -1673,6 +1682,40 @@
     }
 }
 
+TEST(ConfigFileParserTests, ParseRunRule)
+{
+    // Test where works
+    {
+        const json element = "vdd_regulator";
+        std::unique_ptr<RunRuleAction> action = parseRunRule(element);
+        EXPECT_EQ(action->getRuleID(), "vdd_regulator");
+    }
+
+    // Test where fails: Element is not a string
+    try
+    {
+        const json element = 1;
+        parseRunRule(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 = "";
+        parseRunRule(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