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);
45*a76898f1SBob King     Device reg1{
46*a76898f1SBob King         "regulator1", true,
47*a76898f1SBob King         "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
48afb7fc3fSShawn McCarney         std::move(i2cInterface)};
491e5f993dSShawn McCarney     idMap.addDevice(reg1);
50afb7fc3fSShawn McCarney 
51afb7fc3fSShawn McCarney     // Create Device regulator2 and add to IDMap
52afb7fc3fSShawn McCarney     i2cInterface =
53afb7fc3fSShawn McCarney         i2c::create(1, 0x72, i2c::I2CInterface::InitialState::CLOSED);
54*a76898f1SBob King     Device reg2{
55*a76898f1SBob King         "regulator2", true,
56*a76898f1SBob King         "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
57afb7fc3fSShawn McCarney         std::move(i2cInterface)};
581e5f993dSShawn McCarney     idMap.addDevice(reg2);
591e5f993dSShawn McCarney 
601e5f993dSShawn McCarney     // Create ActionEnvironment
611e5f993dSShawn McCarney     ActionEnvironment env{idMap, "regulator1"};
621e5f993dSShawn McCarney 
631e5f993dSShawn McCarney     // Create action
641e5f993dSShawn McCarney     SetDeviceAction action{"regulator2"};
651e5f993dSShawn McCarney 
661e5f993dSShawn McCarney     // Execute action
671e5f993dSShawn McCarney     try
681e5f993dSShawn McCarney     {
691e5f993dSShawn McCarney         EXPECT_EQ(env.getDeviceID(), "regulator1");
701e5f993dSShawn McCarney         EXPECT_EQ(action.execute(env), true);
711e5f993dSShawn McCarney         EXPECT_EQ(env.getDeviceID(), "regulator2");
721e5f993dSShawn McCarney     }
731e5f993dSShawn McCarney     catch (const std::exception& error)
741e5f993dSShawn McCarney     {
751e5f993dSShawn McCarney         ADD_FAILURE() << "Should not have caught exception.";
761e5f993dSShawn McCarney     }
771e5f993dSShawn McCarney }
781e5f993dSShawn McCarney 
791e5f993dSShawn McCarney TEST(SetDeviceActionTests, GetDeviceID)
801e5f993dSShawn McCarney {
811e5f993dSShawn McCarney     SetDeviceAction action{"io_expander_0"};
821e5f993dSShawn McCarney     EXPECT_EQ(action.getDeviceID(), "io_expander_0");
831e5f993dSShawn McCarney }
848a3db36aSShawn McCarney 
858a3db36aSShawn McCarney TEST(SetDeviceActionTests, ToString)
868a3db36aSShawn McCarney {
878a3db36aSShawn McCarney     SetDeviceAction action{"regulator1"};
888a3db36aSShawn McCarney     EXPECT_EQ(action.toString(), "set_device: regulator1");
898a3db36aSShawn McCarney }
90