regulators: Implements support for presence_detection

Enhance the configuration file parser to support the
presence_detection element.

Signed-off-by: Bob King <Bob_King@wistron.com>
Change-Id: I40ed2e849b6baaebdfd1899000fc389429d46543
diff --git a/phosphor-regulators/test/config_file_parser_tests.cpp b/phosphor-regulators/test/config_file_parser_tests.cpp
index 5223e54..42c1e58 100644
--- a/phosphor-regulators/test/config_file_parser_tests.cpp
+++ b/phosphor-regulators/test/config_file_parser_tests.cpp
@@ -1088,7 +1088,6 @@
 
     // Test where works: All properties specified
     {
-        // TODO : add presence_detection property
         const json element = R"(
             {
               "id": "vdd_regulator",
@@ -1103,6 +1102,10 @@
               {
                   "rule_id": "configure_ir35221_rule"
               },
+              "presence_detection":
+              {
+                  "rule_id": "is_foobar_backplane_installed_rule"
+              },
               "rails":
               [
                 {
@@ -1116,7 +1119,7 @@
         EXPECT_EQ(device->isRegulator(), true);
         EXPECT_EQ(device->getFRU(), "/system/chassis/motherboard/regulator2");
         EXPECT_NE(&(device->getI2CInterface()), nullptr);
-        // EXPECT_NE(device->getPresenceDetection(), nullptr);
+        EXPECT_NE(device->getPresenceDetection(), nullptr);
         EXPECT_NE(device->getConfiguration(), nullptr);
         EXPECT_EQ(device->getRails().size(), 1);
     }
@@ -2955,6 +2958,136 @@
     }
 }
 
+TEST(ConfigFileParserTests, ParsePresenceDetection)
+{
+    // Test where works: actions property specified
+    {
+        const json element = R"(
+            {
+              "actions": [
+                { "run_rule": "read_sensors_rule" }
+              ]
+            }
+        )"_json;
+        std::unique_ptr<PresenceDetection> presenceDetection =
+            parsePresenceDetection(element);
+        EXPECT_EQ(presenceDetection->getActions().size(), 1);
+    }
+
+    // Test where works: rule_id property specified
+    {
+        const json element = R"(
+            {
+              "comments": [ "comments property" ],
+              "rule_id": "set_voltage_rule"
+            }
+        )"_json;
+        std::unique_ptr<PresenceDetection> presenceDetection =
+            parsePresenceDetection(element);
+        EXPECT_EQ(presenceDetection->getActions().size(), 1);
+    }
+
+    // Test where fails: actions object is invalid
+    try
+    {
+        const json element = R"(
+            {
+              "actions": 1
+            }
+        )"_json;
+        parsePresenceDetection(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 where fails: rule_id value is invalid
+    try
+    {
+        const json element = R"(
+            {
+              "rule_id": 1
+            }
+        )"_json;
+        parsePresenceDetection(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: Required actions or rule_id property not specified
+    try
+    {
+        const json element = R"(
+            {
+              "comments": [ "comments property" ]
+            }
+        )"_json;
+        parsePresenceDetection(element);
+        ADD_FAILURE() << "Should not have reached this line.";
+    }
+    catch (const std::invalid_argument& e)
+    {
+        EXPECT_STREQ(e.what(), "Invalid property combination: Must contain "
+                               "either rule_id or actions");
+    }
+
+    // Test where fails: Required actions or rule_id property both specified
+    try
+    {
+        const json element = R"(
+            {
+              "rule_id": "set_voltage_rule",
+              "actions": [
+                { "run_rule": "read_sensors_rule" }
+              ]
+            }
+        )"_json;
+        parsePresenceDetection(element);
+        ADD_FAILURE() << "Should not have reached this line.";
+    }
+    catch (const std::invalid_argument& e)
+    {
+        EXPECT_STREQ(e.what(), "Invalid property combination: Must contain "
+                               "either rule_id or actions");
+    }
+
+    // Test where fails: Element is not an object
+    try
+    {
+        const json element = R"( [ "foo", "bar" ] )"_json;
+        parsePresenceDetection(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"(
+            {
+              "foo": "bar",
+              "actions": [
+                { "run_rule": "read_sensors_rule" }
+              ]
+            }
+        )"_json;
+        parsePresenceDetection(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(ConfigFileParserTests, ParseRail)
 {
     // Test where works: Only required properties specified