1 /**
2  * Copyright © 2019 IBM Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "action_environment.hpp"
17 #include "device.hpp"
18 #include "i2c_interface.hpp"
19 #include "id_map.hpp"
20 #include "mocked_i2c_interface.hpp"
21 #include "set_device_action.hpp"
22 
23 #include <exception>
24 #include <memory>
25 #include <utility>
26 
27 #include <gtest/gtest.h>
28 
29 using namespace phosphor::power::regulators;
30 
31 TEST(SetDeviceActionTests, Constructor)
32 {
33     SetDeviceAction action{"regulator1"};
34     EXPECT_EQ(action.getDeviceID(), "regulator1");
35 }
36 
37 TEST(SetDeviceActionTests, Execute)
38 {
39     // Create IDMap
40     IDMap idMap{};
41 
42     // Create Device regulator1 and add to IDMap
43     std::unique_ptr<i2c::I2CInterface> i2cInterface =
44         i2c::create(1, 0x70, i2c::I2CInterface::InitialState::CLOSED);
45     Device reg1{
46         "regulator1", true,
47         "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
48         std::move(i2cInterface)};
49     idMap.addDevice(reg1);
50 
51     // Create Device regulator2 and add to IDMap
52     i2cInterface =
53         i2c::create(1, 0x72, i2c::I2CInterface::InitialState::CLOSED);
54     Device reg2{
55         "regulator2", true,
56         "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
57         std::move(i2cInterface)};
58     idMap.addDevice(reg2);
59 
60     // Create ActionEnvironment
61     ActionEnvironment env{idMap, "regulator1"};
62 
63     // Create action
64     SetDeviceAction action{"regulator2"};
65 
66     // Execute action
67     try
68     {
69         EXPECT_EQ(env.getDeviceID(), "regulator1");
70         EXPECT_EQ(action.execute(env), true);
71         EXPECT_EQ(env.getDeviceID(), "regulator2");
72     }
73     catch (const std::exception& error)
74     {
75         ADD_FAILURE() << "Should not have caught exception.";
76     }
77 }
78 
79 TEST(SetDeviceActionTests, GetDeviceID)
80 {
81     SetDeviceAction action{"io_expander_0"};
82     EXPECT_EQ(action.getDeviceID(), "io_expander_0");
83 }
84 
85 TEST(SetDeviceActionTests, ToString)
86 {
87     SetDeviceAction action{"regulator1"};
88     EXPECT_EQ(action.toString(), "set_device: regulator1");
89 }
90