Create regulators NotAction class

Create the NotAction class that implements the "not" action in the JSON
config file.

Signed-off-by: Shawn McCarney <shawnmm@us.ibm.com>
Change-Id: I2ceb4c4c623ac11f2af5f1eabe5c83244c38d0cf
diff --git a/phosphor-regulators/src/actions/not_action.hpp b/phosphor-regulators/src/actions/not_action.hpp
new file mode 100644
index 0000000..824a763
--- /dev/null
+++ b/phosphor-regulators/src/actions/not_action.hpp
@@ -0,0 +1,89 @@
+/**
+ * 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>
+
+namespace phosphor::power::regulators
+{
+
+/**
+ * @class NotAction
+ *
+ * Executes an action and negates its return value.
+ *
+ * Implements the "not" action in the JSON config file.
+ */
+class NotAction : public Action
+{
+  public:
+    // Specify which compiler-generated methods we want
+    NotAction() = delete;
+    NotAction(const NotAction&) = delete;
+    NotAction(NotAction&&) = delete;
+    NotAction& operator=(const NotAction&) = delete;
+    NotAction& operator=(NotAction&&) = delete;
+    virtual ~NotAction() = default;
+
+    /**
+     * Constructor.
+     *
+     * @param action action to execute
+     */
+    explicit NotAction(std::unique_ptr<Action> action) :
+        action{std::move(action)}
+    {
+    }
+
+    /**
+     * Executes the action specified in the constructor.
+     *
+     * Returns the opposite of the return value from the action.  For example,
+     * if the action returned true, then false will be returned.
+     *
+     * Throws an exception if an error occurs and the action cannot be
+     * successfully executed.
+     *
+     * @param environment action execution environment
+     * @return negated return value from action executed
+     */
+    virtual bool execute(ActionEnvironment& environment) override
+    {
+        return !(action->execute(environment));
+    }
+
+    /**
+     * Returns the action to execute.
+     *
+     * @return action
+     */
+    const std::unique_ptr<Action>& getAction() const
+    {
+        return action;
+    }
+
+  private:
+    /**
+     * Action to execute.
+     */
+    std::unique_ptr<Action> action;
+};
+
+} // namespace phosphor::power::regulators
diff --git a/phosphor-regulators/test/actions/not_action_tests.cpp b/phosphor-regulators/test/actions/not_action_tests.cpp
new file mode 100644
index 0000000..c53df7f
--- /dev/null
+++ b/phosphor-regulators/test/actions/not_action_tests.cpp
@@ -0,0 +1,98 @@
+/**
+ * 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 "not_action.hpp"
+
+#include <exception>
+#include <memory>
+#include <stdexcept>
+#include <utility>
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+using namespace phosphor::power::regulators;
+
+using ::testing::Return;
+using ::testing::Throw;
+
+TEST(NotActionTests, Constructor)
+{
+    NotAction notAction{std::make_unique<MockAction>()};
+    EXPECT_NE(notAction.getAction().get(), nullptr);
+}
+
+TEST(NotActionTests, Execute)
+{
+    // Create ActionEnvironment
+    IDMap idMap{};
+    ActionEnvironment env{idMap, ""};
+
+    // Test where negated action throws an exception
+    try
+    {
+        std::unique_ptr<MockAction> action = std::make_unique<MockAction>();
+        EXPECT_CALL(*action, execute)
+            .Times(1)
+            .WillOnce(Throw(std::logic_error{"Communication error"}));
+
+        NotAction notAction{std::move(action)};
+        notAction.execute(env);
+        ADD_FAILURE() << "Should not have reached this line.";
+    }
+    catch (const std::exception& error)
+    {
+        EXPECT_STREQ(error.what(), "Communication error");
+    }
+
+    // Test where negated action returns true
+    try
+    {
+        std::unique_ptr<MockAction> action = std::make_unique<MockAction>();
+        EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(true));
+
+        NotAction notAction{std::move(action)};
+        EXPECT_EQ(notAction.execute(env), false);
+    }
+    catch (const std::exception& error)
+    {
+        ADD_FAILURE() << "Should not have caught exception.";
+    }
+
+    // Test where negated action returns false
+    try
+    {
+        std::unique_ptr<MockAction> action = std::make_unique<MockAction>();
+        EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(false));
+
+        NotAction notAction{std::move(action)};
+        EXPECT_EQ(notAction.execute(env), true);
+    }
+    catch (const std::exception& error)
+    {
+        ADD_FAILURE() << "Should not have caught exception.";
+    }
+}
+
+TEST(NotActionTests, GetAction)
+{
+    MockAction* action = new MockAction{};
+    NotAction notAction{std::unique_ptr<Action>{action}};
+    EXPECT_EQ(notAction.getAction().get(), action);
+}
diff --git a/phosphor-regulators/test/meson.build b/phosphor-regulators/test/meson.build
index 7157fd6..830564c 100644
--- a/phosphor-regulators/test/meson.build
+++ b/phosphor-regulators/test/meson.build
@@ -11,6 +11,7 @@
 
     'actions/action_environment_tests.cpp',
     'actions/action_utils_tests.cpp',
+    'actions/not_action_tests.cpp',
     'actions/run_rule_action_tests.cpp',
     'actions/set_device_action_tests.cpp'
 ]