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{"regulator1", true, "/system/chassis/motherboard/reg1",
46                 std::move(i2cInterface)};
47     idMap.addDevice(reg1);
48 
49     // Create Device regulator2 and add to IDMap
50     i2cInterface =
51         i2c::create(1, 0x72, i2c::I2CInterface::InitialState::CLOSED);
52     Device reg2{"regulator2", true, "/system/chassis/motherboard/reg2",
53                 std::move(i2cInterface)};
54     idMap.addDevice(reg2);
55 
56     // Create ActionEnvironment
57     ActionEnvironment env{idMap, "regulator1"};
58 
59     // Create action
60     SetDeviceAction action{"regulator2"};
61 
62     // Execute action
63     try
64     {
65         EXPECT_EQ(env.getDeviceID(), "regulator1");
66         EXPECT_EQ(action.execute(env), true);
67         EXPECT_EQ(env.getDeviceID(), "regulator2");
68     }
69     catch (const std::exception& error)
70     {
71         ADD_FAILURE() << "Should not have caught exception.";
72     }
73 }
74 
75 TEST(SetDeviceActionTests, GetDeviceID)
76 {
77     SetDeviceAction action{"io_expander_0"};
78     EXPECT_EQ(action.getDeviceID(), "io_expander_0");
79 }
80