blob: 2651f136081d953bd70611443bf5bd39d07fd4c4 [file] [log] [blame]
Shawn McCarney1e5f9932019-10-31 12:03:27 -05001/**
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"
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060018#include "i2c_interface.hpp"
Shawn McCarney1e5f9932019-10-31 12:03:27 -050019#include "id_map.hpp"
Bob King73eacee2020-10-23 13:58:02 +080020#include "mock_services.hpp"
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060021#include "mocked_i2c_interface.hpp"
Shawn McCarney1e5f9932019-10-31 12:03:27 -050022#include "set_device_action.hpp"
23
24#include <exception>
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060025#include <memory>
26#include <utility>
Shawn McCarney1e5f9932019-10-31 12:03:27 -050027
28#include <gtest/gtest.h>
29
30using namespace phosphor::power::regulators;
31
32TEST(SetDeviceActionTests, Constructor)
33{
34 SetDeviceAction action{"regulator1"};
35 EXPECT_EQ(action.getDeviceID(), "regulator1");
36}
37
38TEST(SetDeviceActionTests, Execute)
39{
40 // Create IDMap
41 IDMap idMap{};
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060042
Bob King73eacee2020-10-23 13:58:02 +080043 // Create MockServices.
44 MockServices services{};
45
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060046 // Create Device regulator1 and add to IDMap
47 std::unique_ptr<i2c::I2CInterface> i2cInterface =
48 i2c::create(1, 0x70, i2c::I2CInterface::InitialState::CLOSED);
Bob Kinga76898f2020-10-13 15:08:33 +080049 Device reg1{
50 "regulator1", true,
51 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
52 std::move(i2cInterface)};
Shawn McCarney1e5f9932019-10-31 12:03:27 -050053 idMap.addDevice(reg1);
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060054
55 // Create Device regulator2 and add to IDMap
56 i2cInterface =
57 i2c::create(1, 0x72, i2c::I2CInterface::InitialState::CLOSED);
Bob Kinga76898f2020-10-13 15:08:33 +080058 Device reg2{
59 "regulator2", true,
60 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
61 std::move(i2cInterface)};
Shawn McCarney1e5f9932019-10-31 12:03:27 -050062 idMap.addDevice(reg2);
63
64 // Create ActionEnvironment
Bob King73eacee2020-10-23 13:58:02 +080065 ActionEnvironment env{idMap, "regulator1", services};
Shawn McCarney1e5f9932019-10-31 12:03:27 -050066
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
83TEST(SetDeviceActionTests, GetDeviceID)
84{
85 SetDeviceAction action{"io_expander_0"};
86 EXPECT_EQ(action.getDeviceID(), "io_expander_0");
87}
Shawn McCarney8a3db362020-02-05 16:24:16 -060088
89TEST(SetDeviceActionTests, ToString)
90{
91 SetDeviceAction action{"regulator1"};
92 EXPECT_EQ(action.toString(), "set_device: regulator1");
93}