Create regulators SetDeviceAction class
Create the SetDeviceAction class that implements the set_device action
in the JSON config file.
Signed-off-by: Shawn McCarney <shawnmm@us.ibm.com>
Change-Id: I31b8fbc144aa2cb2b4b3373fda559b0404d271cb
diff --git a/phosphor-regulators/src/actions/set_device_action.hpp b/phosphor-regulators/src/actions/set_device_action.hpp
new file mode 100644
index 0000000..3c61208
--- /dev/null
+++ b/phosphor-regulators/src/actions/set_device_action.hpp
@@ -0,0 +1,91 @@
+/**
+ * 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 <string>
+
+namespace phosphor
+{
+namespace power
+{
+namespace regulators
+{
+
+/**
+ * @class SetDeviceAction
+ *
+ * Sets the device that will be used by subsequent actions.
+ *
+ * Implements the set_device action in the JSON config file.
+ */
+class SetDeviceAction : public Action
+{
+ public:
+ // Specify which compiler-generated methods we want
+ SetDeviceAction() = delete;
+ SetDeviceAction(const SetDeviceAction&) = delete;
+ SetDeviceAction(SetDeviceAction&&) = delete;
+ SetDeviceAction& operator=(const SetDeviceAction&) = delete;
+ SetDeviceAction& operator=(SetDeviceAction&&) = delete;
+ virtual ~SetDeviceAction() = default;
+
+ /**
+ * Constructor.
+ *
+ * @param deviceID device ID
+ */
+ explicit SetDeviceAction(const std::string& deviceID) : deviceID{deviceID}
+ {
+ }
+
+ /**
+ * Executes this action.
+ *
+ * Sets the current device ID in the ActionEnvironment. This causes
+ * subsequent actions to use that device.
+ *
+ * @param environment Action execution environment.
+ * @return true
+ */
+ virtual bool execute(ActionEnvironment& environment) override
+ {
+ environment.setDeviceID(deviceID);
+ return true;
+ }
+
+ /**
+ * Returns the device ID.
+ *
+ * @return device ID
+ */
+ const std::string& getDeviceID() const
+ {
+ return deviceID;
+ }
+
+ private:
+ /**
+ * Device ID.
+ */
+ const std::string deviceID{};
+};
+
+} // namespace regulators
+} // namespace power
+} // namespace phosphor
diff --git a/phosphor-regulators/test/actions/meson.build b/phosphor-regulators/test/actions/meson.build
index 8c3d2b8..d6821de 100644
--- a/phosphor-regulators/test/actions/meson.build
+++ b/phosphor-regulators/test/actions/meson.build
@@ -1,6 +1,7 @@
test('phosphor-regulators-actions-tests',
executable('phosphor-regulators-actions-tests',
'action_environment_tests.cpp',
+ 'set_device_action_tests.cpp',
dependencies: [
gmock,
gtest,
diff --git a/phosphor-regulators/test/actions/set_device_action_tests.cpp b/phosphor-regulators/test/actions/set_device_action_tests.cpp
new file mode 100644
index 0000000..6164a46
--- /dev/null
+++ b/phosphor-regulators/test/actions/set_device_action_tests.cpp
@@ -0,0 +1,65 @@
+/**
+ * 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_environment.hpp"
+#include "device.hpp"
+#include "id_map.hpp"
+#include "set_device_action.hpp"
+
+#include <exception>
+
+#include <gtest/gtest.h>
+
+using namespace phosphor::power::regulators;
+
+TEST(SetDeviceActionTests, Constructor)
+{
+ SetDeviceAction action{"regulator1"};
+ EXPECT_EQ(action.getDeviceID(), "regulator1");
+}
+
+TEST(SetDeviceActionTests, Execute)
+{
+ // Create IDMap
+ IDMap idMap{};
+ Device reg1{"regulator1"};
+ idMap.addDevice(reg1);
+ Device reg2{"regulator2"};
+ idMap.addDevice(reg2);
+
+ // Create ActionEnvironment
+ ActionEnvironment env{idMap, "regulator1"};
+
+ // Create action
+ SetDeviceAction action{"regulator2"};
+
+ // Execute action
+ try
+ {
+ EXPECT_EQ(env.getDeviceID(), "regulator1");
+ EXPECT_EQ(action.execute(env), true);
+ EXPECT_EQ(env.getDeviceID(), "regulator2");
+ }
+ catch (const std::exception& error)
+ {
+ ADD_FAILURE() << "Should not have caught exception.";
+ }
+}
+
+TEST(SetDeviceActionTests, GetDeviceID)
+{
+ SetDeviceAction action{"io_expander_0"};
+ EXPECT_EQ(action.getDeviceID(), "io_expander_0");
+}