regulators: Implement ComparePresenceAction class

The ComparePresenceAction::execute() method reads the presence value
using the Services->getPresenceService()->isPresent() method.
Then it compares the actual value to the expected value.
It returns true if they match and false if they don't.

Signed-off-by: Bob King <Bob_King@wistron.com>
Change-Id: Ie6a17191313b2c80a0ecd7a7d7357448985d0673
diff --git a/phosphor-regulators/src/actions/compare_presence_action.cpp b/phosphor-regulators/src/actions/compare_presence_action.cpp
new file mode 100644
index 0000000..0d3b28d
--- /dev/null
+++ b/phosphor-regulators/src/actions/compare_presence_action.cpp
@@ -0,0 +1,59 @@
+/**
+ * Copyright © 2020 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 "compare_presence_action.hpp"
+
+#include "action_error.hpp"
+
+#include <exception>
+#include <ios>
+#include <sstream>
+
+namespace phosphor::power::regulators
+{
+
+bool ComparePresenceAction::execute(ActionEnvironment& environment)
+{
+    bool isEqual{false};
+    try
+    {
+        // Get actual presence value for FRU.
+        bool isPresent =
+            environment.getServices().getPresenceService().isPresent(fru);
+
+        // Check if actual presence value equals expected presence value.
+        isEqual = (isPresent == value);
+    }
+    catch (const std::exception& e)
+    {
+        // Nest exception within an ActionError so the caller will have both the
+        // low level error information and the action information.
+        std::throw_with_nested(ActionError(*this));
+    }
+    return isEqual;
+}
+
+std::string ComparePresenceAction::toString() const
+{
+    std::ostringstream ss;
+    ss << "compare_presence: { ";
+    ss << "fru: " << fru << ", ";
+    ss << "value: " << std::boolalpha << value << " }";
+
+    return ss.str();
+}
+
+} // namespace phosphor::power::regulators
diff --git a/phosphor-regulators/src/actions/compare_presence_action.hpp b/phosphor-regulators/src/actions/compare_presence_action.hpp
index b2c7880..b16c98f 100644
--- a/phosphor-regulators/src/actions/compare_presence_action.hpp
+++ b/phosphor-regulators/src/actions/compare_presence_action.hpp
@@ -18,8 +18,6 @@
 #include "action.hpp"
 #include "action_environment.hpp"
 
-#include <ios>
-#include <sstream>
 #include <string>
 
 namespace phosphor::power::regulators
@@ -57,16 +55,10 @@
     /**
      * Executes this action.
      *
-     * TODO: Not implemented yet
-     *
      * @param environment Action execution environment.
      * @return true
      */
-    virtual bool execute(ActionEnvironment& /* environment */) override
-    {
-        // TODO: Not implemented yet
-        return true;
-    }
+    virtual bool execute(ActionEnvironment& environment) override;
 
     /**
      * Returns the Field-Replaceable Unit (FRU).
@@ -93,17 +85,7 @@
      *
      * @return description of action
      */
-    virtual std::string toString() const override
-    {
-        std::ostringstream ss;
-        ss << "compare_presence: { ";
-
-        ss << "fru: " << fru << ", ";
-
-        ss << "value: " << std::boolalpha << value << " }";
-
-        return ss.str();
-    }
+    virtual std::string toString() const override;
 
   private:
     /**
diff --git a/phosphor-regulators/src/meson.build b/phosphor-regulators/src/meson.build
index bfcacb1..8238d93 100644
--- a/phosphor-regulators/src/meson.build
+++ b/phosphor-regulators/src/meson.build
@@ -21,6 +21,7 @@
     'system.cpp',
     'temporary_file.cpp',
 
+    'actions/compare_presence_action.cpp',
     'actions/if_action.cpp',
     'actions/i2c_compare_bit_action.cpp',
     'actions/i2c_compare_byte_action.cpp',
diff --git a/phosphor-regulators/test/actions/compare_presence_action_tests.cpp b/phosphor-regulators/test/actions/compare_presence_action_tests.cpp
index aafdecc..5bb7ec7 100644
--- a/phosphor-regulators/test/actions/compare_presence_action_tests.cpp
+++ b/phosphor-regulators/test/actions/compare_presence_action_tests.cpp
@@ -13,21 +13,30 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-#include "action.hpp"
 #include "action_environment.hpp"
+#include "action_error.hpp"
 #include "compare_presence_action.hpp"
+#include "device.hpp"
+#include "i2c_interface.hpp"
 #include "id_map.hpp"
 #include "mock_action.hpp"
+#include "mock_presence_service.hpp"
+#include "mock_services.hpp"
+#include "mocked_i2c_interface.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(ComparePresenceActionTests, Constructor)
 {
     ComparePresenceAction action{
@@ -39,7 +48,156 @@
 
 TEST(ComparePresenceActionTests, Execute)
 {
-    // TODO: Not implemented yet
+    // Test where works: actual value is true.
+    try
+    {
+        // Create mock I2CInterface
+        std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
+            std::make_unique<i2c::MockedI2CInterface>();
+
+        // Create Device, IDMap, MockServices and ActionEnvironment
+        // Actual value isPresent() is true
+        Device device{
+            "reg1", true,
+            "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
+            std::move(i2cInterface)};
+        IDMap idMap{};
+        idMap.addDevice(device);
+        MockServices services{};
+        MockPresenceService& presenceService =
+            services.getMockPresenceService();
+        EXPECT_CALL(presenceService,
+                    isPresent("/xyz/openbmc_project/inventory/system/chassis/"
+                              "motherboard/cpu2"))
+            .Times(2)
+            .WillRepeatedly(Return(true));
+        ActionEnvironment env{idMap, "reg1", services};
+
+        // Test where works: expected value is true, return value is true.
+        {
+            ComparePresenceAction action{"/xyz/openbmc_project/inventory/"
+                                         "system/chassis/motherboard/cpu2",
+                                         true};
+            EXPECT_EQ(action.execute(env), true);
+        }
+
+        // Test where works: expected value is false, return value is false.
+        {
+            ComparePresenceAction action{"/xyz/openbmc_project/inventory/"
+                                         "system/chassis/motherboard/cpu2",
+                                         false};
+            EXPECT_EQ(action.execute(env), false);
+        }
+    }
+    catch (...)
+    {
+        ADD_FAILURE() << "Should not have caught exception.";
+    }
+
+    // Test where actual value is false.
+    try
+    {
+        // Create mock I2CInterface
+        std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
+            std::make_unique<i2c::MockedI2CInterface>();
+
+        // Create Device, IDMap, MockServices and ActionEnvironment
+        // Actual value isPresent() is false
+        Device device{
+            "reg1", true,
+            "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
+            std::move(i2cInterface)};
+        IDMap idMap{};
+        idMap.addDevice(device);
+        MockServices services{};
+        MockPresenceService& presenceService =
+            services.getMockPresenceService();
+        EXPECT_CALL(presenceService,
+                    isPresent("/xyz/openbmc_project/inventory/system/chassis/"
+                              "motherboard/cpu2"))
+            .Times(2)
+            .WillRepeatedly(Return(false));
+        ActionEnvironment env{idMap, "reg1", services};
+
+        // Test where works: expected value is true, return value is false.
+        {
+            ComparePresenceAction action{"/xyz/openbmc_project/inventory/"
+                                         "system/chassis/motherboard/cpu2",
+                                         true};
+            EXPECT_EQ(action.execute(env), false);
+        }
+
+        // Test where works: expected value is false, return value is true.
+        {
+            ComparePresenceAction action{"/xyz/openbmc_project/inventory/"
+                                         "system/chassis/motherboard/cpu2",
+                                         false};
+            EXPECT_EQ(action.execute(env), true);
+        }
+    }
+    catch (...)
+    {
+        ADD_FAILURE() << "Should not have caught exception.";
+    }
+
+    // Test where fails. Reading presence fails.
+    try
+    {
+        // Create mock I2CInterface
+        std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
+            std::make_unique<i2c::MockedI2CInterface>();
+
+        // Create Device, IDMap, MockServices and ActionEnvironment
+        // PresenceService cannot get the presence
+        Device device{
+            "reg1", true,
+            "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
+            std::move(i2cInterface)};
+        IDMap idMap{};
+        idMap.addDevice(device);
+        MockServices services{};
+        MockPresenceService& presenceService =
+            services.getMockPresenceService();
+        EXPECT_CALL(presenceService,
+                    isPresent("/xyz/openbmc_project/inventory/system/chassis/"
+                              "motherboard/cpu2"))
+            .Times(1)
+            .WillOnce(Throw(std::runtime_error(
+                "PresenceService cannot get the presence.")));
+
+        ActionEnvironment env{idMap, "reg1", services};
+
+        ComparePresenceAction action{"/xyz/openbmc_project/inventory/"
+                                     "system/chassis/motherboard/cpu2",
+                                     true};
+        action.execute(env);
+        ADD_FAILURE() << "Should not have reached this line.";
+    }
+    catch (const ActionError& e)
+    {
+        EXPECT_STREQ(e.what(), "ActionError: compare_presence: { fru: "
+                               "/xyz/openbmc_project/inventory/system/chassis/"
+                               "motherboard/cpu2, value: true }");
+        try
+        {
+            // Re-throw inner exception from MockPresenceService.
+            std::rethrow_if_nested(e);
+            ADD_FAILURE() << "Should not have reached this line.";
+        }
+        catch (const std::runtime_error& r_error)
+        {
+            EXPECT_STREQ(r_error.what(),
+                         "PresenceService cannot get the presence.");
+        }
+        catch (...)
+        {
+            ADD_FAILURE() << "Should not have caught exception.";
+        }
+    }
+    catch (...)
+    {
+        ADD_FAILURE() << "Should not have caught exception.";
+    }
 }
 
 TEST(ComparePresenceActionTests, GetFRU)