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"
2073eaceebSBob King #include "mock_services.hpp"
21afb7fc3fSShawn McCarney #include "mocked_i2c_interface.hpp"
221e5f993dSShawn McCarney #include "set_device_action.hpp"
231e5f993dSShawn McCarney 
241e5f993dSShawn McCarney #include <exception>
25afb7fc3fSShawn McCarney #include <memory>
26afb7fc3fSShawn McCarney #include <utility>
271e5f993dSShawn McCarney 
281e5f993dSShawn McCarney #include <gtest/gtest.h>
291e5f993dSShawn McCarney 
301e5f993dSShawn McCarney using namespace phosphor::power::regulators;
311e5f993dSShawn McCarney 
TEST(SetDeviceActionTests,Constructor)321e5f993dSShawn McCarney TEST(SetDeviceActionTests, Constructor)
331e5f993dSShawn McCarney {
341e5f993dSShawn McCarney     SetDeviceAction action{"regulator1"};
351e5f993dSShawn McCarney     EXPECT_EQ(action.getDeviceID(), "regulator1");
361e5f993dSShawn McCarney }
371e5f993dSShawn McCarney 
TEST(SetDeviceActionTests,Execute)381e5f993dSShawn McCarney TEST(SetDeviceActionTests, Execute)
391e5f993dSShawn McCarney {
401e5f993dSShawn McCarney     // Create IDMap
411e5f993dSShawn McCarney     IDMap idMap{};
42afb7fc3fSShawn McCarney 
4373eaceebSBob King     // Create MockServices.
4473eaceebSBob King     MockServices services{};
4573eaceebSBob King 
46afb7fc3fSShawn McCarney     // Create Device regulator1 and add to IDMap
47afb7fc3fSShawn McCarney     std::unique_ptr<i2c::I2CInterface> i2cInterface =
48afb7fc3fSShawn McCarney         i2c::create(1, 0x70, i2c::I2CInterface::InitialState::CLOSED);
49a76898f1SBob King     Device reg1{
50a76898f1SBob King         "regulator1", true,
51a76898f1SBob King         "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
52afb7fc3fSShawn McCarney         std::move(i2cInterface)};
531e5f993dSShawn McCarney     idMap.addDevice(reg1);
54afb7fc3fSShawn McCarney 
55afb7fc3fSShawn McCarney     // Create Device regulator2 and add to IDMap
56*48781aefSPatrick Williams     i2cInterface = i2c::create(1, 0x72,
57*48781aefSPatrick Williams                                i2c::I2CInterface::InitialState::CLOSED);
58a76898f1SBob King     Device reg2{
59a76898f1SBob King         "regulator2", true,
60a76898f1SBob King         "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
61afb7fc3fSShawn McCarney         std::move(i2cInterface)};
621e5f993dSShawn McCarney     idMap.addDevice(reg2);
631e5f993dSShawn McCarney 
641e5f993dSShawn McCarney     // Create ActionEnvironment
6573eaceebSBob King     ActionEnvironment env{idMap, "regulator1", services};
661e5f993dSShawn McCarney 
671e5f993dSShawn McCarney     // Create action
681e5f993dSShawn McCarney     SetDeviceAction action{"regulator2"};
691e5f993dSShawn McCarney 
701e5f993dSShawn McCarney     // Execute action
711e5f993dSShawn McCarney     try
721e5f993dSShawn McCarney     {
731e5f993dSShawn McCarney         EXPECT_EQ(env.getDeviceID(), "regulator1");
741e5f993dSShawn McCarney         EXPECT_EQ(action.execute(env), true);
751e5f993dSShawn McCarney         EXPECT_EQ(env.getDeviceID(), "regulator2");
761e5f993dSShawn McCarney     }
771e5f993dSShawn McCarney     catch (const std::exception& error)
781e5f993dSShawn McCarney     {
791e5f993dSShawn McCarney         ADD_FAILURE() << "Should not have caught exception.";
801e5f993dSShawn McCarney     }
811e5f993dSShawn McCarney }
821e5f993dSShawn McCarney 
TEST(SetDeviceActionTests,GetDeviceID)831e5f993dSShawn McCarney TEST(SetDeviceActionTests, GetDeviceID)
841e5f993dSShawn McCarney {
851e5f993dSShawn McCarney     SetDeviceAction action{"io_expander_0"};
861e5f993dSShawn McCarney     EXPECT_EQ(action.getDeviceID(), "io_expander_0");
871e5f993dSShawn McCarney }
888a3db36aSShawn McCarney 
TEST(SetDeviceActionTests,ToString)898a3db36aSShawn McCarney TEST(SetDeviceActionTests, ToString)
908a3db36aSShawn McCarney {
918a3db36aSShawn McCarney     SetDeviceAction action{"regulator1"};
928a3db36aSShawn McCarney     EXPECT_EQ(action.toString(), "set_device: regulator1");
938a3db36aSShawn McCarney }
94