regulators: Implements support for or action

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

Signed-off-by: Bob King <Bob_King@wistron.com>
Change-Id: I6e6dee26c23a2ea414e96a0cda3525b91b806509
diff --git a/phosphor-regulators/src/config_file_parser.cpp b/phosphor-regulators/src/config_file_parser.cpp
index 5f0181e..8c6dc12 100644
--- a/phosphor-regulators/src/config_file_parser.cpp
+++ b/phosphor-regulators/src/config_file_parser.cpp
@@ -125,9 +125,8 @@
     }
     else if (element.contains("or"))
     {
-        // TODO: Not implemented yet
-        // action = parseOr(element["or"]);
-        // ++propertyCount;
+        action = parseOr(element["or"]);
+        ++propertyCount;
     }
     else if (element.contains("pmbus_read_sensor"))
     {
@@ -583,6 +582,21 @@
     return std::make_unique<NotAction>(std::move(action));
 }
 
+std::unique_ptr<OrAction> parseOr(const json& element)
+{
+    verifyIsArray(element);
+
+    // Verify if array size less than 2
+    if (element.size() < 2)
+    {
+        throw std::invalid_argument{"Array must contain two or more actions"};
+    }
+    // Array of two or more actions
+    std::vector<std::unique_ptr<Action>> actions = parseActionArray(element);
+
+    return std::make_unique<OrAction>(std::move(actions));
+}
+
 std::unique_ptr<PMBusWriteVoutCommandAction>
     parsePMBusWriteVoutCommand(const json& element)
 {
diff --git a/phosphor-regulators/src/config_file_parser.hpp b/phosphor-regulators/src/config_file_parser.hpp
index e2476a2..7104356 100644
--- a/phosphor-regulators/src/config_file_parser.hpp
+++ b/phosphor-regulators/src/config_file_parser.hpp
@@ -28,6 +28,7 @@
 #include "i2c_write_byte_action.hpp"
 #include "i2c_write_bytes_action.hpp"
 #include "not_action.hpp"
+#include "or_action.hpp"
 #include "pmbus_write_vout_command_action.hpp"
 #include "presence_detection.hpp"
 #include "rail.hpp"
@@ -453,6 +454,18 @@
 std::unique_ptr<NotAction> parseNot(const nlohmann::json& element);
 
 /**
+ * Parses a JSON element containing an or action.
+ *
+ * Returns the corresponding C++ OrAction object.
+ *
+ * Throws an exception if parsing fails.
+ *
+ * @param element JSON element
+ * @return OrAction object
+ */
+std::unique_ptr<OrAction> parseOr(const nlohmann::json& element);
+
+/**
  * Parses a JSON element containing a pmbus_write_vout_command action.
  *
  * Returns the corresponding C++ PMBusWriteVoutCommandAction object.
diff --git a/phosphor-regulators/test/config_file_parser_tests.cpp b/phosphor-regulators/test/config_file_parser_tests.cpp
index 4dfc45e..4c53c6c 100644
--- a/phosphor-regulators/test/config_file_parser_tests.cpp
+++ b/phosphor-regulators/test/config_file_parser_tests.cpp
@@ -28,6 +28,7 @@
 #include "i2c_write_byte_action.hpp"
 #include "i2c_write_bytes_action.hpp"
 #include "not_action.hpp"
+#include "or_action.hpp"
 #include "pmbus_utils.hpp"
 #include "pmbus_write_vout_command_action.hpp"
 #include "presence_detection.hpp"
@@ -363,7 +364,18 @@
     }
 
     // Test where works: or action type specified
-    // TODO: Not implemented yet
+    {
+        const json element = R"(
+            {
+              "or": [
+                { "i2c_compare_byte": { "register": "0xA0", "value": "0x00" } },
+                { "i2c_compare_byte": { "register": "0xA1", "value": "0x00" } }
+              ]
+            }
+        )"_json;
+        std::unique_ptr<Action> action = parseAction(element);
+        EXPECT_NE(action.get(), nullptr);
+    }
 
     // Test where works: pmbus_read_sensor action type specified
     // TODO: Not implemented yet
@@ -2581,6 +2593,53 @@
     }
 }
 
+TEST(ConfigFileParserTests, ParseOr)
+{
+    // Test where works: Element is an array with 2 actions
+    {
+        const json element = R"(
+            [
+              { "i2c_compare_byte": { "register": "0xA0", "value": "0x00" } },
+              { "i2c_compare_byte": { "register": "0xA1", "value": "0x00" } }
+            ]
+        )"_json;
+        std::unique_ptr<OrAction> action = parseOr(element);
+        EXPECT_EQ(action->getActions().size(), 2);
+    }
+
+    // Test where fails: Element is an array with 1 action
+    try
+    {
+        const json element = R"(
+            [
+              { "i2c_compare_byte": { "register": "0xA0", "value": "0x00" } }
+            ]
+        )"_json;
+        parseOr(element);
+        ADD_FAILURE() << "Should not have reached this line.";
+    }
+    catch (const std::invalid_argument& e)
+    {
+        EXPECT_STREQ(e.what(), "Array must contain two or more actions");
+    }
+
+    // Test where fails: Element is not an array
+    try
+    {
+        const json element = R"(
+            {
+              "foo": "bar"
+            }
+        )"_json;
+        parseOr(element);
+        ADD_FAILURE() << "Should not have reached this line.";
+    }
+    catch (const std::invalid_argument& e)
+    {
+        EXPECT_STREQ(e.what(), "Element is not an array");
+    }
+}
+
 TEST(ConfigFileParserTests, ParsePMBusWriteVoutCommand)
 {
     // Test where works: Only required properties specified