regulators: Add toString() method to Action class

Add a toString() method to the Action class and all child classes.

This method returns a string description of the action.  The description
will be used in journal entries and error logs when an action fails
(such as due to an I2C error).

Signed-off-by: Shawn McCarney <shawnmm@us.ibm.com>
Change-Id: I083496d23bf9c3df5be1b082650a9def24be1b00
diff --git a/phosphor-regulators/src/actions/action.hpp b/phosphor-regulators/src/actions/action.hpp
index 4cbe76d..81b7b4d 100644
--- a/phosphor-regulators/src/actions/action.hpp
+++ b/phosphor-regulators/src/actions/action.hpp
@@ -17,6 +17,8 @@
 
 #include "action_environment.hpp"
 
+#include <string>
+
 namespace phosphor::power::regulators
 {
 
@@ -54,6 +56,18 @@
      *         indicate if the action was successfully executed.
      */
     virtual bool execute(ActionEnvironment& environment) = 0;
+
+    /**
+     * Returns a string description of this action.
+     *
+     * The description should include the action name, properties, and property
+     * values.
+     *
+     * The description is used in journal entries and error logs.
+     *
+     * @return description of action
+     */
+    virtual std::string toString() const = 0;
 };
 
 } // namespace phosphor::power::regulators
diff --git a/phosphor-regulators/src/actions/and_action.hpp b/phosphor-regulators/src/actions/and_action.hpp
index cb934b4..f4c3fca 100644
--- a/phosphor-regulators/src/actions/and_action.hpp
+++ b/phosphor-regulators/src/actions/and_action.hpp
@@ -19,6 +19,7 @@
 #include "action_environment.hpp"
 
 #include <memory>
+#include <string>
 #include <utility>
 #include <vector>
 
@@ -93,6 +94,16 @@
         return actions;
     }
 
+    /**
+     * Returns a string description of this action.
+     *
+     * @return description of action
+     */
+    virtual std::string toString() const override
+    {
+        return "and: [ ... ]";
+    }
+
   private:
     /**
      * Actions to execute.
diff --git a/phosphor-regulators/src/actions/if_action.hpp b/phosphor-regulators/src/actions/if_action.hpp
index 13b9860..10b2f5b 100644
--- a/phosphor-regulators/src/actions/if_action.hpp
+++ b/phosphor-regulators/src/actions/if_action.hpp
@@ -19,6 +19,7 @@
 #include "action_environment.hpp"
 
 #include <memory>
+#include <string>
 #include <utility>
 #include <vector>
 
@@ -126,6 +127,22 @@
         return elseActions;
     }
 
+    /**
+     * Returns a string description of this action.
+     *
+     * @return description of action
+     */
+    virtual std::string toString() const override
+    {
+        std::string description{"if: { condition: { ... }, then: [ ... ]"};
+        if (elseActions.size() > 0)
+        {
+            description += ", else: [ ... ]";
+        }
+        description += " }";
+        return description;
+    }
+
   private:
     /**
      * Action that tests whether the condition is true.
diff --git a/phosphor-regulators/src/actions/not_action.hpp b/phosphor-regulators/src/actions/not_action.hpp
index 824a763..762f5b5 100644
--- a/phosphor-regulators/src/actions/not_action.hpp
+++ b/phosphor-regulators/src/actions/not_action.hpp
@@ -19,6 +19,7 @@
 #include "action_environment.hpp"
 
 #include <memory>
+#include <string>
 #include <utility>
 
 namespace phosphor::power::regulators
@@ -79,6 +80,16 @@
         return action;
     }
 
+    /**
+     * Returns a string description of this action.
+     *
+     * @return description of action
+     */
+    virtual std::string toString() const override
+    {
+        return "not: { ... }";
+    }
+
   private:
     /**
      * Action to execute.
diff --git a/phosphor-regulators/src/actions/or_action.hpp b/phosphor-regulators/src/actions/or_action.hpp
index 7adcc6f..5dcff7a 100644
--- a/phosphor-regulators/src/actions/or_action.hpp
+++ b/phosphor-regulators/src/actions/or_action.hpp
@@ -19,6 +19,7 @@
 #include "action_environment.hpp"
 
 #include <memory>
+#include <string>
 #include <utility>
 #include <vector>
 
@@ -93,6 +94,16 @@
         return actions;
     }
 
+    /**
+     * Returns a string description of this action.
+     *
+     * @return description of action
+     */
+    virtual std::string toString() const override
+    {
+        return "or: [ ... ]";
+    }
+
   private:
     /**
      * Actions to execute.
diff --git a/phosphor-regulators/src/actions/run_rule_action.hpp b/phosphor-regulators/src/actions/run_rule_action.hpp
index 8e84a89..6cf63ac 100644
--- a/phosphor-regulators/src/actions/run_rule_action.hpp
+++ b/phosphor-regulators/src/actions/run_rule_action.hpp
@@ -89,6 +89,16 @@
         return ruleID;
     }
 
+    /**
+     * Returns a string description of this action.
+     *
+     * @return description of action
+     */
+    virtual std::string toString() const override
+    {
+        return "run_rule: " + ruleID;
+    }
+
   private:
     /**
      * Rule ID.
diff --git a/phosphor-regulators/src/actions/set_device_action.hpp b/phosphor-regulators/src/actions/set_device_action.hpp
index 7942840..955891e 100644
--- a/phosphor-regulators/src/actions/set_device_action.hpp
+++ b/phosphor-regulators/src/actions/set_device_action.hpp
@@ -75,6 +75,16 @@
         return deviceID;
     }
 
+    /**
+     * Returns a string description of this action.
+     *
+     * @return description of action
+     */
+    virtual std::string toString() const override
+    {
+        return "set_device: " + deviceID;
+    }
+
   private:
     /**
      * Device ID.
diff --git a/phosphor-regulators/test/actions/and_action_tests.cpp b/phosphor-regulators/test/actions/and_action_tests.cpp
index 65f91e6..3b62273 100644
--- a/phosphor-regulators/test/actions/and_action_tests.cpp
+++ b/phosphor-regulators/test/actions/and_action_tests.cpp
@@ -162,3 +162,12 @@
     EXPECT_EQ(andAction.getActions()[0].get(), action1);
     EXPECT_EQ(andAction.getActions()[1].get(), action2);
 }
+
+TEST(AndActionTests, ToString)
+{
+    std::vector<std::unique_ptr<Action>> actions{};
+    actions.push_back(std::make_unique<MockAction>());
+
+    AndAction andAction{std::move(actions)};
+    EXPECT_EQ(andAction.toString(), "and: [ ... ]");
+}
diff --git a/phosphor-regulators/test/actions/if_action_tests.cpp b/phosphor-regulators/test/actions/if_action_tests.cpp
index 1f67f27..87f2f9e 100644
--- a/phosphor-regulators/test/actions/if_action_tests.cpp
+++ b/phosphor-regulators/test/actions/if_action_tests.cpp
@@ -298,3 +298,36 @@
     EXPECT_EQ(ifAction.getElseActions()[0].get(), elseAction1);
     EXPECT_EQ(ifAction.getElseActions()[1].get(), elseAction2);
 }
+
+TEST(IfActionTests, ToString)
+{
+    // Test where else clause is not specified
+    {
+        std::unique_ptr<Action> conditionAction =
+            std::make_unique<MockAction>();
+
+        std::vector<std::unique_ptr<Action>> thenActions{};
+        thenActions.push_back(std::make_unique<MockAction>());
+
+        IfAction ifAction{std::move(conditionAction), std::move(thenActions)};
+        EXPECT_EQ(ifAction.toString(),
+                  "if: { condition: { ... }, then: [ ... ] }");
+    }
+
+    // Test where else clause is specified
+    {
+        std::unique_ptr<Action> conditionAction =
+            std::make_unique<MockAction>();
+
+        std::vector<std::unique_ptr<Action>> thenActions{};
+        thenActions.push_back(std::make_unique<MockAction>());
+
+        std::vector<std::unique_ptr<Action>> elseActions{};
+        elseActions.push_back(std::make_unique<MockAction>());
+
+        IfAction ifAction{std::move(conditionAction), std::move(thenActions),
+                          std::move(elseActions)};
+        EXPECT_EQ(ifAction.toString(),
+                  "if: { condition: { ... }, then: [ ... ], else: [ ... ] }");
+    }
+}
diff --git a/phosphor-regulators/test/actions/mock_action.hpp b/phosphor-regulators/test/actions/mock_action.hpp
index 84455dd..bb48960 100644
--- a/phosphor-regulators/test/actions/mock_action.hpp
+++ b/phosphor-regulators/test/actions/mock_action.hpp
@@ -18,6 +18,8 @@
 #include "action.hpp"
 #include "action_environment.hpp"
 
+#include <string>
+
 #include <gmock/gmock.h>
 
 namespace phosphor::power::regulators
@@ -35,6 +37,7 @@
     virtual ~MockAction() = default;
 
     MOCK_METHOD(bool, execute, (ActionEnvironment & environment), (override));
+    MOCK_METHOD(std::string, toString, (), (const, override));
 };
 
 } // namespace phosphor::power::regulators
diff --git a/phosphor-regulators/test/actions/not_action_tests.cpp b/phosphor-regulators/test/actions/not_action_tests.cpp
index c53df7f..8e89862 100644
--- a/phosphor-regulators/test/actions/not_action_tests.cpp
+++ b/phosphor-regulators/test/actions/not_action_tests.cpp
@@ -96,3 +96,9 @@
     NotAction notAction{std::unique_ptr<Action>{action}};
     EXPECT_EQ(notAction.getAction().get(), action);
 }
+
+TEST(NotActionTests, ToString)
+{
+    NotAction notAction{std::make_unique<MockAction>()};
+    EXPECT_EQ(notAction.toString(), "not: { ... }");
+}
diff --git a/phosphor-regulators/test/actions/or_action_tests.cpp b/phosphor-regulators/test/actions/or_action_tests.cpp
index 6e33d69..b2c89d1 100644
--- a/phosphor-regulators/test/actions/or_action_tests.cpp
+++ b/phosphor-regulators/test/actions/or_action_tests.cpp
@@ -162,3 +162,12 @@
     EXPECT_EQ(orAction.getActions()[0].get(), action1);
     EXPECT_EQ(orAction.getActions()[1].get(), action2);
 }
+
+TEST(OrActionTests, ToString)
+{
+    std::vector<std::unique_ptr<Action>> actions{};
+    actions.push_back(std::make_unique<MockAction>());
+
+    OrAction orAction{std::move(actions)};
+    EXPECT_EQ(orAction.toString(), "or: [ ... ]");
+}
diff --git a/phosphor-regulators/test/actions/run_rule_action_tests.cpp b/phosphor-regulators/test/actions/run_rule_action_tests.cpp
index be94e52..b688a78 100644
--- a/phosphor-regulators/test/actions/run_rule_action_tests.cpp
+++ b/phosphor-regulators/test/actions/run_rule_action_tests.cpp
@@ -187,3 +187,9 @@
     RunRuleAction action{"read_sensors_rule"};
     EXPECT_EQ(action.getRuleID(), "read_sensors_rule");
 }
+
+TEST(RunRuleActionTests, ToString)
+{
+    RunRuleAction action{"set_voltage_rule"};
+    EXPECT_EQ(action.toString(), "run_rule: set_voltage_rule");
+}
diff --git a/phosphor-regulators/test/actions/set_device_action_tests.cpp b/phosphor-regulators/test/actions/set_device_action_tests.cpp
index 1aa6cfc..c39b9b0 100644
--- a/phosphor-regulators/test/actions/set_device_action_tests.cpp
+++ b/phosphor-regulators/test/actions/set_device_action_tests.cpp
@@ -77,3 +77,9 @@
     SetDeviceAction action{"io_expander_0"};
     EXPECT_EQ(action.getDeviceID(), "io_expander_0");
 }
+
+TEST(SetDeviceActionTests, ToString)
+{
+    SetDeviceAction action{"regulator1"};
+    EXPECT_EQ(action.toString(), "set_device: regulator1");
+}