blob: 1aa6cfcb570ebb8db9acc79fa941f3e21db2e8db [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"
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060020#include "mocked_i2c_interface.hpp"
Shawn McCarney1e5f9932019-10-31 12:03:27 -050021#include "set_device_action.hpp"
22
23#include <exception>
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060024#include <memory>
25#include <utility>
Shawn McCarney1e5f9932019-10-31 12:03:27 -050026
27#include <gtest/gtest.h>
28
29using namespace phosphor::power::regulators;
30
31TEST(SetDeviceActionTests, Constructor)
32{
33 SetDeviceAction action{"regulator1"};
34 EXPECT_EQ(action.getDeviceID(), "regulator1");
35}
36
37TEST(SetDeviceActionTests, Execute)
38{
39 // Create IDMap
40 IDMap idMap{};
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060041
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)};
Shawn McCarney1e5f9932019-10-31 12:03:27 -050047 idMap.addDevice(reg1);
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060048
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)};
Shawn McCarney1e5f9932019-10-31 12:03:27 -050054 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
75TEST(SetDeviceActionTests, GetDeviceID)
76{
77 SetDeviceAction action{"io_expander_0"};
78 EXPECT_EQ(action.getDeviceID(), "io_expander_0");
79}