11e5f993dSShawn McCarney /**
21e5f993dSShawn McCarney  * Copyright © 2019 IBM Corporation
31e5f993dSShawn McCarney  *
41e5f993dSShawn McCarney  * Licensed under the Apache License, Version 2.0 (the "License");
51e5f993dSShawn McCarney  * you may not use this file except in compliance with the License.
61e5f993dSShawn McCarney  * You may obtain a copy of the License at
71e5f993dSShawn McCarney  *
81e5f993dSShawn McCarney  *     http://www.apache.org/licenses/LICENSE-2.0
91e5f993dSShawn McCarney  *
101e5f993dSShawn McCarney  * Unless required by applicable law or agreed to in writing, software
111e5f993dSShawn McCarney  * distributed under the License is distributed on an "AS IS" BASIS,
121e5f993dSShawn McCarney  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131e5f993dSShawn McCarney  * See the License for the specific language governing permissions and
141e5f993dSShawn McCarney  * limitations under the License.
151e5f993dSShawn McCarney  */
161e5f993dSShawn McCarney #include "action_environment.hpp"
171e5f993dSShawn McCarney #include "device.hpp"
18afb7fc3fSShawn McCarney #include "i2c_interface.hpp"
191e5f993dSShawn McCarney #include "id_map.hpp"
20afb7fc3fSShawn McCarney #include "mocked_i2c_interface.hpp"
211e5f993dSShawn McCarney #include "set_device_action.hpp"
221e5f993dSShawn McCarney 
231e5f993dSShawn McCarney #include <exception>
24afb7fc3fSShawn McCarney #include <memory>
25afb7fc3fSShawn McCarney #include <utility>
261e5f993dSShawn McCarney 
271e5f993dSShawn McCarney #include <gtest/gtest.h>
281e5f993dSShawn McCarney 
291e5f993dSShawn McCarney using namespace phosphor::power::regulators;
301e5f993dSShawn McCarney 
311e5f993dSShawn McCarney TEST(SetDeviceActionTests, Constructor)
321e5f993dSShawn McCarney {
331e5f993dSShawn McCarney     SetDeviceAction action{"regulator1"};
341e5f993dSShawn McCarney     EXPECT_EQ(action.getDeviceID(), "regulator1");
351e5f993dSShawn McCarney }
361e5f993dSShawn McCarney 
371e5f993dSShawn McCarney TEST(SetDeviceActionTests, Execute)
381e5f993dSShawn McCarney {
391e5f993dSShawn McCarney     // Create IDMap
401e5f993dSShawn McCarney     IDMap idMap{};
41afb7fc3fSShawn McCarney 
42afb7fc3fSShawn McCarney     // Create Device regulator1 and add to IDMap
43afb7fc3fSShawn McCarney     std::unique_ptr<i2c::I2CInterface> i2cInterface =
44afb7fc3fSShawn McCarney         i2c::create(1, 0x70, i2c::I2CInterface::InitialState::CLOSED);
45afb7fc3fSShawn McCarney     Device reg1{"regulator1", true, "/system/chassis/motherboard/reg1",
46afb7fc3fSShawn McCarney                 std::move(i2cInterface)};
471e5f993dSShawn McCarney     idMap.addDevice(reg1);
48afb7fc3fSShawn McCarney 
49afb7fc3fSShawn McCarney     // Create Device regulator2 and add to IDMap
50afb7fc3fSShawn McCarney     i2cInterface =
51afb7fc3fSShawn McCarney         i2c::create(1, 0x72, i2c::I2CInterface::InitialState::CLOSED);
52afb7fc3fSShawn McCarney     Device reg2{"regulator2", true, "/system/chassis/motherboard/reg2",
53afb7fc3fSShawn McCarney                 std::move(i2cInterface)};
541e5f993dSShawn McCarney     idMap.addDevice(reg2);
551e5f993dSShawn McCarney 
561e5f993dSShawn McCarney     // Create ActionEnvironment
571e5f993dSShawn McCarney     ActionEnvironment env{idMap, "regulator1"};
581e5f993dSShawn McCarney 
591e5f993dSShawn McCarney     // Create action
601e5f993dSShawn McCarney     SetDeviceAction action{"regulator2"};
611e5f993dSShawn McCarney 
621e5f993dSShawn McCarney     // Execute action
631e5f993dSShawn McCarney     try
641e5f993dSShawn McCarney     {
651e5f993dSShawn McCarney         EXPECT_EQ(env.getDeviceID(), "regulator1");
661e5f993dSShawn McCarney         EXPECT_EQ(action.execute(env), true);
671e5f993dSShawn McCarney         EXPECT_EQ(env.getDeviceID(), "regulator2");
681e5f993dSShawn McCarney     }
691e5f993dSShawn McCarney     catch (const std::exception& error)
701e5f993dSShawn McCarney     {
711e5f993dSShawn McCarney         ADD_FAILURE() << "Should not have caught exception.";
721e5f993dSShawn McCarney     }
731e5f993dSShawn McCarney }
741e5f993dSShawn McCarney 
751e5f993dSShawn McCarney TEST(SetDeviceActionTests, GetDeviceID)
761e5f993dSShawn McCarney {
771e5f993dSShawn McCarney     SetDeviceAction action{"io_expander_0"};
781e5f993dSShawn McCarney     EXPECT_EQ(action.getDeviceID(), "io_expander_0");
791e5f993dSShawn McCarney }
80*8a3db36aSShawn McCarney 
81*8a3db36aSShawn McCarney TEST(SetDeviceActionTests, ToString)
82*8a3db36aSShawn McCarney {
83*8a3db36aSShawn McCarney     SetDeviceAction action{"regulator1"};
84*8a3db36aSShawn McCarney     EXPECT_EQ(action.toString(), "set_device: regulator1");
85*8a3db36aSShawn McCarney }
86