regulators: Add test cases for action.

Add test cases for action:
  * Valid: Comments property not specified
  * Valid: Comments property specified
  * Valid: and action type specified
  * Valid: compare_presence action type specified
  * Valid: compare_vpd action type specified
  * Valid: i2c_compare_bit action type specified
  * Valid: i2c_compare_byte action type specified
  * Valid: i2c_compare_bytes action type specified
  * Valid: i2c_write_bit action type specified
  * Valid: i2c_write_byte action type specified
  * Valid: i2c_write_bytes action type specified
  * Valid: if action type specified
  * Valid: not action type specified
  * Valid: or action type specified
  * Valid: pmbus_read_sensor and pmbus_write_vout_command action type specified
  * Valid: run_rule action type specified
  * Valid: set_device action type specified
  * Invalid: Wrong data type for comments (should be array of string)
  * Invalid: Wrong data type for action type
  * Invalid: Empty comments array
  * Invalid: Comments array has wrong element type (should be string)
  * Invalid: No action type specified
  * Invalid: Multiple action types specified
  * Invalid: Unexpected property specified

Signed-off-by: Bob King <Bob_King@wistron.com>
Change-Id: I81dfb50cf46a3091753f43266af8dc01527d621d
diff --git a/phosphor-regulators/test/validate-regulators-config_tests.cpp b/phosphor-regulators/test/validate-regulators-config_tests.cpp
index 5f8f68c..2b915b9 100644
--- a/phosphor-regulators/test/validate-regulators-config_tests.cpp
+++ b/phosphor-regulators/test/validate-regulators-config_tests.cpp
@@ -256,6 +256,223 @@
     EXPECT_EQ(outputMessage, expectedOutputMessage);
 }
 
+TEST(ValidateRegulatorsConfigTest, Action)
+{
+    // Valid: Comments property not specified
+    {
+        json configFile = validConfigFile;
+        EXPECT_JSON_VALID(configFile);
+    }
+    // Valid: Comments property specified
+    {
+        json configFile = validConfigFile;
+        configFile["rules"][0]["actions"][0]["comments"][0] =
+            "Set VOUT_COMMAND";
+        EXPECT_JSON_VALID(configFile);
+    }
+    // Valid: and action type specified
+    {
+        json configFile = validConfigFile;
+        json andAction =
+            R"(
+                {
+                 "and": [
+                    { "i2c_compare_byte": { "register": "0xA0", "value": "0x00" } },
+                    { "i2c_compare_byte": { "register": "0xA1", "value": "0x00" } }
+                  ]
+                }
+            )"_json;
+        configFile["rules"][0]["actions"].push_back(andAction);
+        EXPECT_JSON_VALID(configFile);
+    }
+    // Valid: compare_presence action type specified
+    {
+        json configFile = validConfigFile;
+        configFile["rules"][0]["actions"][1]["compare_presence"]["fru"] =
+            "/system/chassis/motherboard/regulator2";
+        configFile["rules"][0]["actions"][1]["compare_presence"]["value"] =
+            true;
+        EXPECT_JSON_VALID(configFile);
+    }
+    // Valid: compare_vpd action type specified
+    {
+        json configFile = validConfigFile;
+        configFile["rules"][0]["actions"][1]["compare_vpd"]["fru"] =
+            "/system/chassis/motherboard/regulator2";
+        configFile["rules"][0]["actions"][1]["compare_vpd"]["keyword"] = "CCIN";
+        configFile["rules"][0]["actions"][1]["compare_vpd"]["value"] = "2D35";
+        EXPECT_JSON_VALID(configFile);
+    }
+    // Valid: i2c_compare_bit action type specified
+    {
+        json configFile = validConfigFile;
+        configFile["rules"][0]["actions"][1]["i2c_compare_bit"]["register"] =
+            "0xA0";
+        configFile["rules"][0]["actions"][1]["i2c_compare_bit"]["position"] = 3;
+        configFile["rules"][0]["actions"][1]["i2c_compare_bit"]["value"] = 1;
+        EXPECT_JSON_VALID(configFile);
+    }
+    // Valid: i2c_compare_byte action type specified
+    {
+        json configFile = validConfigFile;
+        configFile["rules"][0]["actions"][1]["i2c_compare_byte"]["register"] =
+            "0x82";
+        configFile["rules"][0]["actions"][1]["i2c_compare_byte"]["value"] =
+            "0x40";
+        configFile["rules"][0]["actions"][1]["i2c_compare_byte"]["mask"] =
+            "0x7F";
+        EXPECT_JSON_VALID(configFile);
+    }
+    // Valid: i2c_compare_bytes action type specified
+    {
+        json configFile = validConfigFile;
+        configFile["rules"][0]["actions"][1]["i2c_compare_bytes"]["register"] =
+            "0x82";
+        configFile["rules"][0]["actions"][1]["i2c_compare_bytes"]["values"] = {
+            "0x02", "0x73"};
+        configFile["rules"][0]["actions"][1]["i2c_compare_bytes"]["masks"] = {
+            "0x7F", "0x7F"};
+        EXPECT_JSON_VALID(configFile);
+    }
+    // Valid: i2c_write_bit action type specified
+    {
+        json configFile = validConfigFile;
+        configFile["rules"][0]["actions"][1]["i2c_write_bit"]["register"] =
+            "0xA0";
+        configFile["rules"][0]["actions"][1]["i2c_write_bit"]["position"] = 3;
+        configFile["rules"][0]["actions"][1]["i2c_write_bit"]["value"] = 1;
+        EXPECT_JSON_VALID(configFile);
+    }
+    // Valid: i2c_write_byte action type specified
+    {
+        json configFile = validConfigFile;
+        configFile["rules"][0]["actions"][1]["i2c_write_byte"]["register"] =
+            "0x82";
+        configFile["rules"][0]["actions"][1]["i2c_write_byte"]["value"] =
+            "0x40";
+        configFile["rules"][0]["actions"][1]["i2c_write_byte"]["mask"] = "0x7F";
+        EXPECT_JSON_VALID(configFile);
+    }
+    // Valid: i2c_write_bytes action type specified
+    {
+        json configFile = validConfigFile;
+        configFile["rules"][0]["actions"][1]["i2c_write_bytes"]["register"] =
+            "0x82";
+        configFile["rules"][0]["actions"][1]["i2c_write_bytes"]["values"] = {
+            "0x02", "0x73"};
+        configFile["rules"][0]["actions"][1]["i2c_write_bytes"]["masks"] = {
+            "0x7F", "0x7F"};
+        EXPECT_JSON_VALID(configFile);
+    }
+    // Valid: if action type specified
+    {
+        json configFile = validConfigFile;
+        configFile["rules"][2]["actions"][0]["if"]["condition"]["run_rule"] =
+            "set_voltage_rule";
+        configFile["rules"][2]["actions"][0]["if"]["then"][0]["run_rule"] =
+            "read_sensors_rule";
+        configFile["rules"][2]["actions"][0]["if"]["else"][0]["run_rule"] =
+            "read_sensors_rule";
+        configFile["rules"][2]["id"] = "rule_if";
+        EXPECT_JSON_VALID(configFile);
+    }
+    // Valid: not action type specified
+    {
+        json configFile = validConfigFile;
+        configFile["rules"][0]["actions"][1]["not"]["i2c_compare_byte"]
+                  ["register"] = "0xA0";
+        configFile["rules"][0]["actions"][1]["not"]["i2c_compare_byte"]
+                  ["value"] = "0xFF";
+        EXPECT_JSON_VALID(configFile);
+    }
+    // Valid: or action type specified
+    {
+        json configFile = validConfigFile;
+        configFile["rules"][0]["actions"][1]["or"][0]["i2c_compare_byte"]
+                  ["register"] = "0xA0";
+        configFile["rules"][0]["actions"][1]["or"][0]["i2c_compare_byte"]
+                  ["value"] = "0x00";
+        configFile["rules"][0]["actions"][1]["or"][1]["i2c_compare_byte"]
+                  ["register"] = "0xA1";
+        configFile["rules"][0]["actions"][1]["or"][1]["i2c_compare_byte"]
+                  ["value"] = "0x00";
+        EXPECT_JSON_VALID(configFile);
+    }
+    // Valid: pmbus_read_sensor and pmbus_write_vout_command action type
+    // specified
+    {
+        EXPECT_JSON_VALID(validConfigFile);
+    }
+    // Valid: run_rule action type specified
+    {
+        json configFile = validConfigFile;
+        configFile["rules"][0]["actions"][1]["run_rule"] = "read_sensors_rule";
+        EXPECT_JSON_VALID(configFile);
+    }
+    // Valid: set_device action type specified
+    {
+        json configFile = validConfigFile;
+        configFile["rules"][0]["actions"][1]["set_device"] = "vdd_regulator";
+        EXPECT_JSON_VALID(configFile);
+    }
+    // Invalid: Wrong data type for comments (should be array of string)
+    {
+        json configFile = validConfigFile;
+        configFile["rules"][0]["actions"][0]["comments"] = true;
+        EXPECT_JSON_INVALID(configFile, "Validation failed.",
+                            "True is not of type 'array'");
+    }
+    // Invalid: Wrong data type for action type (such as "i2c_write_byte": true)
+    {
+        json configFile = validConfigFile;
+        configFile["rules"][0]["actions"][1]["i2c_write_byte"] = true;
+        EXPECT_JSON_INVALID(configFile, "Validation failed.",
+                            "True is not of type 'object'");
+    }
+    // Invalid: Empty comments array
+    {
+        json configFile = validConfigFile;
+        configFile["rules"][0]["actions"][0]["comments"] = json::array();
+        EXPECT_JSON_INVALID(configFile, "Validation failed.",
+                            "[] is too short");
+    }
+    // Invalid: Comments array has wrong element type (should be string)
+    {
+        json configFile = validConfigFile;
+        configFile["rules"][0]["actions"][0]["comments"][0] = true;
+        EXPECT_JSON_INVALID(configFile, "Validation failed.",
+                            "True is not of type 'string'");
+    }
+    // Invalid: No action type specified
+    {
+        json configFile = validConfigFile;
+        configFile["rules"][0]["actions"][1]["comments"][0] =
+            "Check if bit 3 is on";
+        EXPECT_JSON_INVALID(configFile, "Validation failed.",
+                            "'and' is a required property");
+    }
+    // Invalid: Multiple action types specified (such as both 'compare_presence'
+    // and 'pmbus_write_vout_command')
+    {
+        json configFile = validConfigFile;
+        configFile["rules"][0]["actions"][0]["compare_presence"]["value"] =
+            true;
+        EXPECT_JSON_INVALID(
+            configFile, "Validation failed.",
+            "{'compare_presence': {'value': True}, 'pmbus_write_vout_command': "
+            "{'format': 'linear'}} is valid under each of {'required': "
+            "['pmbus_write_vout_command']}, {'required': "
+            "['compare_presence']}");
+    }
+    // Invalid: Unexpected property specified (like 'foo')
+    {
+        json configFile = validConfigFile;
+        configFile["rules"][0]["actions"][1]["foo"] = "foo";
+        EXPECT_JSON_INVALID(
+            configFile, "Validation failed.",
+            "Additional properties are not allowed ('foo' was unexpected)");
+    }
+}
 TEST(ValidateRegulatorsConfigTest, And)
 {
     // Valid.