regulators: Implements support for compare_presence action

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

Signed-off-by: Bob King <Bob_King@wistron.com>
Change-Id: I9bfa3d260e0a6f7d8f41bed473b436148141ea50
diff --git a/phosphor-regulators/src/config_file_parser.cpp b/phosphor-regulators/src/config_file_parser.cpp
index b94e308..7a3d497 100644
--- a/phosphor-regulators/src/config_file_parser.cpp
+++ b/phosphor-regulators/src/config_file_parser.cpp
@@ -72,9 +72,8 @@
     }
     else if (element.contains("compare_presence"))
     {
-        // TODO: Not implemented yet
-        // action = parseComparePresence(element["compare_presence"]);
-        // ++propertyCount;
+        action = parseComparePresence(element["compare_presence"]);
+        ++propertyCount;
     }
     else if (element.contains("compare_vpd"))
     {
@@ -232,6 +231,27 @@
     return chassis;
 }
 
+std::unique_ptr<ComparePresenceAction> parseComparePresence(const json& element)
+{
+    verifyIsObject(element);
+    unsigned int propertyCount{0};
+
+    // Required fru property
+    const json& fruElement = getRequiredProperty(element, "fru");
+    std::string fru = parseString(fruElement);
+    ++propertyCount;
+
+    // Required value property
+    const json& valueElement = getRequiredProperty(element, "value");
+    bool value = parseBoolean(valueElement);
+    ++propertyCount;
+
+    // Verify no invalid properties exist
+    verifyPropertyCount(element, propertyCount);
+
+    return std::make_unique<ComparePresenceAction>(fru, value);
+}
+
 std::unique_ptr<Configuration> parseConfiguration(const json& element)
 {
     verifyIsObject(element);
diff --git a/phosphor-regulators/src/config_file_parser.hpp b/phosphor-regulators/src/config_file_parser.hpp
index e3f6bde..706c609 100644
--- a/phosphor-regulators/src/config_file_parser.hpp
+++ b/phosphor-regulators/src/config_file_parser.hpp
@@ -18,6 +18,7 @@
 #include "action.hpp"
 #include "and_action.hpp"
 #include "chassis.hpp"
+#include "compare_presence_action.hpp"
 #include "configuration.hpp"
 #include "device.hpp"
 #include "i2c_compare_bit_action.hpp"
@@ -223,6 +224,19 @@
     parseChassisArray(const nlohmann::json& element);
 
 /**
+ * Parses a JSON element containing a compare_presence action.
+ *
+ * Returns the corresponding C++ ComparePresenceAction object.
+ *
+ * Throws an exception if parsing fails.
+ *
+ * @param element JSON element
+ * @return ComparePresenceAction object
+ */
+std::unique_ptr<ComparePresenceAction>
+    parseComparePresence(const nlohmann::json& element);
+
+/**
  * Parses a JSON element containing a configuration.
  *
  * Returns the corresponding C++ Configuration object.
diff --git a/phosphor-regulators/test/config_file_parser_tests.cpp b/phosphor-regulators/test/config_file_parser_tests.cpp
index f5d683a..875b0a1 100644
--- a/phosphor-regulators/test/config_file_parser_tests.cpp
+++ b/phosphor-regulators/test/config_file_parser_tests.cpp
@@ -16,6 +16,7 @@
 #include "action.hpp"
 #include "and_action.hpp"
 #include "chassis.hpp"
+#include "compare_presence_action.hpp"
 #include "config_file_parser.hpp"
 #include "config_file_parser_error.hpp"
 #include "configuration.hpp"
@@ -258,7 +259,19 @@
     }
 
     // Test where works: compare_presence action type specified
-    // TODO: Not implemented yet
+    {
+        const json element = R"(
+            {
+              "compare_presence":
+              {
+                "fru": "/system/chassis/motherboard/cpu3",
+                "value": true
+              }
+            }
+        )"_json;
+        std::unique_ptr<Action> action = parseAction(element);
+        EXPECT_NE(action.get(), nullptr);
+    }
 
     // Test where works: compare_vpd action type specified
     // TODO: Not implemented yet
@@ -888,6 +901,119 @@
     }
 }
 
+TEST(ConfigFileParserTests, ParseComparePresence)
+{
+    // Test where works
+    {
+        const json element = R"(
+            {
+              "fru": "/system/chassis/motherboard/cpu3",
+              "value": true
+            }
+        )"_json;
+        std::unique_ptr<ComparePresenceAction> action =
+            parseComparePresence(element);
+        EXPECT_EQ(action->getFRU(), "/system/chassis/motherboard/cpu3");
+        EXPECT_EQ(action->getValue(), true);
+    }
+
+    // Test where fails: Element is not an object
+    try
+    {
+        const json element = R"( [ "0xFF", "0x01" ] )"_json;
+        parseComparePresence(element);
+        ADD_FAILURE() << "Should not have reached this line.";
+    }
+    catch (const std::invalid_argument& e)
+    {
+        EXPECT_STREQ(e.what(), "Element is not an object");
+    }
+
+    // Test where fails: Invalid property specified
+    try
+    {
+        const json element = R"(
+            {
+              "fru": "/system/chassis/motherboard/cpu3",
+              "value": true,
+              "foo" : true
+            }
+        )"_json;
+        parseComparePresence(element);
+        ADD_FAILURE() << "Should not have reached this line.";
+    }
+    catch (const std::invalid_argument& e)
+    {
+        EXPECT_STREQ(e.what(), "Element contains an invalid property");
+    }
+
+    // Test where fails: Required fru property not specified
+    try
+    {
+        const json element = R"(
+            {
+              "value": true
+            }
+        )"_json;
+        parseComparePresence(element);
+        ADD_FAILURE() << "Should not have reached this line.";
+    }
+    catch (const std::invalid_argument& e)
+    {
+        EXPECT_STREQ(e.what(), "Required property missing: fru");
+    }
+
+    // Test where fails: Required value property not specified
+    try
+    {
+        const json element = R"(
+            {
+              "fru": "/system/chassis/motherboard/cpu3"
+            }
+        )"_json;
+        parseComparePresence(element);
+        ADD_FAILURE() << "Should not have reached this line.";
+    }
+    catch (const std::invalid_argument& e)
+    {
+        EXPECT_STREQ(e.what(), "Required property missing: value");
+    }
+
+    // Test where fails: fru value is invalid
+    try
+    {
+        const json element = R"(
+            {
+              "fru": 1,
+              "value": true
+            }
+        )"_json;
+        parseComparePresence(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: value value is invalid
+    try
+    {
+        const json element = R"(
+            {
+              "fru": "/system/chassis/motherboard/cpu3",
+              "value": 1
+            }
+        )"_json;
+        parseComparePresence(element);
+        ADD_FAILURE() << "Should not have reached this line.";
+    }
+    catch (const std::invalid_argument& e)
+    {
+        EXPECT_STREQ(e.what(), "Element is not a boolean");
+    }
+}
+
 TEST(ConfigFileParserTests, ParseConfiguration)
 {
     // Test where works: actions required property specified