Create regulators action_utils namespace

Create the action_utils namespace for utility functions related to the
regulators action framework.

Create a function in that namespace that executes a vector of actions.

Signed-off-by: Shawn McCarney <shawnmm@us.ibm.com>
Change-Id: I22297435daad0d5f6ad61157c094938f0488f076
diff --git a/phosphor-regulators/src/actions/action_utils.hpp b/phosphor-regulators/src/actions/action_utils.hpp
new file mode 100644
index 0000000..9ddff4f
--- /dev/null
+++ b/phosphor-regulators/src/actions/action_utils.hpp
@@ -0,0 +1,64 @@
+/**
+ * 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 <vector>
+
+namespace phosphor
+{
+namespace power
+{
+namespace regulators
+{
+namespace action_utils
+{
+
+/**
+ * This file contains utility functions related to the regulators action
+ * framework.
+ */
+
+/**
+ * Executes one or more actions in sequential order.
+ *
+ * Returns the return value from the last action in the vector.
+ *
+ * Throws an exception if an error occurs and an action cannot be
+ * successfully executed.
+ *
+ * @param actions actions to execute
+ * @param environment action execution environment
+ * @return return value from last action in vector
+ */
+inline bool execute(std::vector<std::unique_ptr<Action>>& actions,
+                    ActionEnvironment& environment)
+{
+    bool returnValue{true};
+    for (std::unique_ptr<Action>& action : actions)
+    {
+        returnValue = action->execute(environment);
+    }
+    return returnValue;
+}
+
+} // namespace action_utils
+} // namespace regulators
+} // namespace power
+} // namespace phosphor
diff --git a/phosphor-regulators/test/actions/action_utils_tests.cpp b/phosphor-regulators/test/actions/action_utils_tests.cpp
new file mode 100644
index 0000000..ede5fa8
--- /dev/null
+++ b/phosphor-regulators/test/actions/action_utils_tests.cpp
@@ -0,0 +1,124 @@
+/**
+ * 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 "action_utils.hpp"
+#include "id_map.hpp"
+#include "mock_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(ActionUtilsTests, Execute)
+{
+    // Create ActionEnvironment
+    IDMap idMap{};
+    ActionEnvironment env{idMap, ""};
+
+    // Test where vector is empty
+    try
+    {
+        std::vector<std::unique_ptr<Action>> actions{};
+        EXPECT_EQ(action_utils::execute(actions, env), true);
+    }
+    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));
+
+        action_utils::execute(actions, env);
+        ADD_FAILURE() << "Should not have reached this line.";
+    }
+    catch (const std::exception& error)
+    {
+        EXPECT_STREQ(error.what(), "Communication error");
+    }
+
+    // Test where last action returns false
+    try
+    {
+        std::vector<std::unique_ptr<Action>> actions{};
+        std::unique_ptr<MockAction> action;
+
+        // First action will return true
+        action = std::make_unique<MockAction>();
+        EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(true));
+        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));
+
+        EXPECT_EQ(action_utils::execute(actions, env), false);
+    }
+    catch (const std::exception& error)
+    {
+        ADD_FAILURE() << "Should not have caught exception.";
+    }
+
+    // Test where last 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));
+
+        EXPECT_EQ(action_utils::execute(actions, env), true);
+    }
+    catch (const std::exception& error)
+    {
+        ADD_FAILURE() << "Should not have caught exception.";
+    }
+}
diff --git a/phosphor-regulators/test/meson.build b/phosphor-regulators/test/meson.build
index 6d6df82..a60249f 100644
--- a/phosphor-regulators/test/meson.build
+++ b/phosphor-regulators/test/meson.build
@@ -10,6 +10,7 @@
     'rule_tests.cpp',
 
     'actions/action_environment_tests.cpp',
+    'actions/action_utils_tests.cpp',
     'actions/set_device_action_tests.cpp'
 ]