Create regulators OrAction class

Create the OrAction class that implements the "or" action in the JSON
config file.

Signed-off-by: Shawn McCarney <shawnmm@us.ibm.com>
Change-Id: I6df3da246f03fd0ac4de16de5ccf1436e3b2d63e
diff --git a/phosphor-regulators/src/actions/or_action.hpp b/phosphor-regulators/src/actions/or_action.hpp
new file mode 100644
index 0000000..7adcc6f
--- /dev/null
+++ b/phosphor-regulators/src/actions/or_action.hpp
@@ -0,0 +1,103 @@
+/**
+ * Copyright © 2019 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#pragma once
+
+#include "action.hpp"
+#include "action_environment.hpp"
+
+#include <memory>
+#include <utility>
+#include <vector>
+
+namespace phosphor::power::regulators
+{
+
+/**
+ * @class OrAction
+ *
+ * Executes a sequence of actions and tests whether any of them returned true.
+ *
+ * Implements the "or" action in the JSON config file.
+ */
+class OrAction : public Action
+{
+  public:
+    // Specify which compiler-generated methods we want
+    OrAction() = delete;
+    OrAction(const OrAction&) = delete;
+    OrAction(OrAction&&) = delete;
+    OrAction& operator=(const OrAction&) = delete;
+    OrAction& operator=(OrAction&&) = delete;
+    virtual ~OrAction() = default;
+
+    /**
+     * Constructor.
+     *
+     * @param actions actions to execute
+     */
+    explicit OrAction(std::vector<std::unique_ptr<Action>> actions) :
+        actions{std::move(actions)}
+    {
+    }
+
+    /**
+     * Executes the actions specified in the constructor.
+     *
+     * Returns true if any of the actions returned true, otherwise returns
+     * false.
+     *
+     * Note: All of the actions will be executed even if an action before the
+     * end returns true.  This ensures that actions with beneficial side-effects
+     * are always executed, such as a register read that clears latched fault
+     * bits.
+     *
+     * Throws an exception if an error occurs and an action cannot be
+     * successfully executed.
+     *
+     * @param environment action execution environment
+     * @return true if any actions returned true, otherwise returns false
+     */
+    virtual bool execute(ActionEnvironment& environment) override
+    {
+        bool returnValue{false};
+        for (std::unique_ptr<Action>& action : actions)
+        {
+            if (action->execute(environment) == true)
+            {
+                returnValue = true;
+            }
+        }
+        return returnValue;
+    }
+
+    /**
+     * Returns the actions to execute.
+     *
+     * @return actions to execute
+     */
+    const std::vector<std::unique_ptr<Action>>& getActions() const
+    {
+        return actions;
+    }
+
+  private:
+    /**
+     * Actions to execute.
+     */
+    std::vector<std::unique_ptr<Action>> actions{};
+};
+
+} // namespace phosphor::power::regulators
diff --git a/phosphor-regulators/test/actions/or_action_tests.cpp b/phosphor-regulators/test/actions/or_action_tests.cpp
new file mode 100644
index 0000000..6e33d69
--- /dev/null
+++ b/phosphor-regulators/test/actions/or_action_tests.cpp
@@ -0,0 +1,164 @@
+/**
+ * Copyright © 2019 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "action.hpp"
+#include "action_environment.hpp"
+#include "id_map.hpp"
+#include "mock_action.hpp"
+#include "or_action.hpp"
+
+#include <exception>
+#include <memory>
+#include <stdexcept>
+#include <utility>
+#include <vector>
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+using namespace phosphor::power::regulators;
+
+using ::testing::Return;
+using ::testing::Throw;
+
+TEST(OrActionTests, Constructor)
+{
+    std::vector<std::unique_ptr<Action>> actions{};
+    actions.push_back(std::make_unique<MockAction>());
+    actions.push_back(std::make_unique<MockAction>());
+
+    OrAction orAction{std::move(actions)};
+    EXPECT_EQ(orAction.getActions().size(), 2);
+}
+
+TEST(OrActionTests, Execute)
+{
+    // Create ActionEnvironment
+    IDMap idMap{};
+    ActionEnvironment env{idMap, ""};
+
+    // Test where empty vector of actions is specified
+    try
+    {
+        std::vector<std::unique_ptr<Action>> actions{};
+        OrAction orAction{std::move(actions)};
+        EXPECT_EQ(orAction.execute(env), false);
+    }
+    catch (const std::exception& error)
+    {
+        ADD_FAILURE() << "Should not have caught exception.";
+    }
+
+    // Test where action throws an exception
+    try
+    {
+        std::vector<std::unique_ptr<Action>> actions{};
+        std::unique_ptr<MockAction> action;
+
+        // First action will throw an exception
+        action = std::make_unique<MockAction>();
+        EXPECT_CALL(*action, execute)
+            .Times(1)
+            .WillOnce(Throw(std::logic_error{"Communication error"}));
+        actions.push_back(std::move(action));
+
+        // Second action should not get executed
+        action = std::make_unique<MockAction>();
+        EXPECT_CALL(*action, execute).Times(0);
+        actions.push_back(std::move(action));
+
+        OrAction orAction{std::move(actions)};
+        orAction.execute(env);
+        ADD_FAILURE() << "Should not have reached this line.";
+    }
+    catch (const std::exception& error)
+    {
+        EXPECT_STREQ(error.what(), "Communication error");
+    }
+
+    // Test where middle action returns true
+    try
+    {
+        std::vector<std::unique_ptr<Action>> actions{};
+        std::unique_ptr<MockAction> action;
+
+        // First action will return false
+        action = std::make_unique<MockAction>();
+        EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(false));
+        actions.push_back(std::move(action));
+
+        // Second action will return true
+        action = std::make_unique<MockAction>();
+        EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(true));
+        actions.push_back(std::move(action));
+
+        // Third action will return false
+        action = std::make_unique<MockAction>();
+        EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(false));
+        actions.push_back(std::move(action));
+
+        OrAction orAction{std::move(actions)};
+        EXPECT_EQ(orAction.execute(env), true);
+    }
+    catch (const std::exception& error)
+    {
+        ADD_FAILURE() << "Should not have caught exception.";
+    }
+
+    // Test where all actions return false
+    try
+    {
+        std::vector<std::unique_ptr<Action>> actions{};
+        std::unique_ptr<MockAction> action;
+
+        // First action will return false
+        action = std::make_unique<MockAction>();
+        EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(false));
+        actions.push_back(std::move(action));
+
+        // Second action will return false
+        action = std::make_unique<MockAction>();
+        EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(false));
+        actions.push_back(std::move(action));
+
+        // Third action will return false
+        action = std::make_unique<MockAction>();
+        EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(false));
+        actions.push_back(std::move(action));
+
+        OrAction orAction{std::move(actions)};
+        EXPECT_EQ(orAction.execute(env), false);
+    }
+    catch (const std::exception& error)
+    {
+        ADD_FAILURE() << "Should not have caught exception.";
+    }
+}
+
+TEST(OrActionTests, GetActions)
+{
+    std::vector<std::unique_ptr<Action>> actions{};
+
+    MockAction* action1 = new MockAction{};
+    actions.push_back(std::unique_ptr<MockAction>{action1});
+
+    MockAction* action2 = new MockAction{};
+    actions.push_back(std::unique_ptr<MockAction>{action2});
+
+    OrAction orAction{std::move(actions)};
+    EXPECT_EQ(orAction.getActions().size(), 2);
+    EXPECT_EQ(orAction.getActions()[0].get(), action1);
+    EXPECT_EQ(orAction.getActions()[1].get(), action2);
+}
diff --git a/phosphor-regulators/test/meson.build b/phosphor-regulators/test/meson.build
index 175ac06..5a9eea0 100644
--- a/phosphor-regulators/test/meson.build
+++ b/phosphor-regulators/test/meson.build
@@ -13,6 +13,7 @@
     'actions/action_utils_tests.cpp',
     'actions/and_action_tests.cpp',
     'actions/not_action_tests.cpp',
+    'actions/or_action_tests.cpp',
     'actions/run_rule_action_tests.cpp',
     'actions/set_device_action_tests.cpp'
 ]