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 "mock_services.hpp"
21 #include "mocked_i2c_interface.hpp"
22 #include "set_device_action.hpp"
23 
24 #include <exception>
25 #include <memory>
26 #include <utility>
27 
28 #include <gtest/gtest.h>
29 
30 using namespace phosphor::power::regulators;
31 
TEST(SetDeviceActionTests,Constructor)32 TEST(SetDeviceActionTests, Constructor)
33 {
34     SetDeviceAction action{"regulator1"};
35     EXPECT_EQ(action.getDeviceID(), "regulator1");
36 }
37 
TEST(SetDeviceActionTests,Execute)38 TEST(SetDeviceActionTests, Execute)
39 {
40     // Create IDMap
41     IDMap idMap{};
42 
43     // Create MockServices.
44     MockServices services{};
45 
46     // Create Device regulator1 and add to IDMap
47     std::unique_ptr<i2c::I2CInterface> i2cInterface =
48         i2c::create(1, 0x70, i2c::I2CInterface::InitialState::CLOSED);
49     Device reg1{
50         "regulator1", true,
51         "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
52         std::move(i2cInterface)};
53     idMap.addDevice(reg1);
54 
55     // Create Device regulator2 and add to IDMap
56     i2cInterface =
57         i2c::create(1, 0x72, i2c::I2CInterface::InitialState::CLOSED);
58     Device reg2{
59         "regulator2", true,
60         "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
61         std::move(i2cInterface)};
62     idMap.addDevice(reg2);
63 
64     // Create ActionEnvironment
65     ActionEnvironment env{idMap, "regulator1", services};
66 
67     // Create action
68     SetDeviceAction action{"regulator2"};
69 
70     // Execute action
71     try
72     {
73         EXPECT_EQ(env.getDeviceID(), "regulator1");
74         EXPECT_EQ(action.execute(env), true);
75         EXPECT_EQ(env.getDeviceID(), "regulator2");
76     }
77     catch (const std::exception& error)
78     {
79         ADD_FAILURE() << "Should not have caught exception.";
80     }
81 }
82 
TEST(SetDeviceActionTests,GetDeviceID)83 TEST(SetDeviceActionTests, GetDeviceID)
84 {
85     SetDeviceAction action{"io_expander_0"};
86     EXPECT_EQ(action.getDeviceID(), "io_expander_0");
87 }
88 
TEST(SetDeviceActionTests,ToString)89 TEST(SetDeviceActionTests, ToString)
90 {
91     SetDeviceAction action{"regulator1"};
92     EXPECT_EQ(action.toString(), "set_device: regulator1");
93 }
94