blob: 640c08533194e72281c4b4ee335328785c4822e9 [file] [log] [blame]
Shawn McCarney78488f62019-11-05 15:29:54 -06001/**
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.hpp"
17#include "action_environment.hpp"
18#include "action_utils.hpp"
19#include "id_map.hpp"
20#include "mock_action.hpp"
Bob King73eacee2020-10-23 13:58:02 +080021#include "mock_services.hpp"
Shawn McCarney78488f62019-11-05 15:29:54 -060022
23#include <exception>
24#include <memory>
25#include <stdexcept>
26#include <utility>
27#include <vector>
28
29#include <gmock/gmock.h>
30#include <gtest/gtest.h>
31
32using namespace phosphor::power::regulators;
33
34using ::testing::Return;
35using ::testing::Throw;
36
37TEST(ActionUtilsTests, Execute)
38{
39 // Create ActionEnvironment
40 IDMap idMap{};
Bob King73eacee2020-10-23 13:58:02 +080041 // Create mock services.
42 MockServices services{};
43 ActionEnvironment env{idMap, "", services};
Shawn McCarney78488f62019-11-05 15:29:54 -060044
45 // Test where vector is empty
46 try
47 {
48 std::vector<std::unique_ptr<Action>> actions{};
49 EXPECT_EQ(action_utils::execute(actions, env), true);
50 }
51 catch (const std::exception& error)
52 {
53 ADD_FAILURE() << "Should not have caught exception.";
54 }
55
56 // Test where action throws an exception
57 try
58 {
59 std::vector<std::unique_ptr<Action>> actions{};
60 std::unique_ptr<MockAction> action;
61
62 // First action will throw an exception
63 action = std::make_unique<MockAction>();
64 EXPECT_CALL(*action, execute)
65 .Times(1)
66 .WillOnce(Throw(std::logic_error{"Communication error"}));
67 actions.push_back(std::move(action));
68
69 // Second action should not get executed
70 action = std::make_unique<MockAction>();
71 EXPECT_CALL(*action, execute).Times(0);
72 actions.push_back(std::move(action));
73
74 action_utils::execute(actions, env);
75 ADD_FAILURE() << "Should not have reached this line.";
76 }
77 catch (const std::exception& error)
78 {
79 EXPECT_STREQ(error.what(), "Communication error");
80 }
81
82 // Test where last action returns false
83 try
84 {
85 std::vector<std::unique_ptr<Action>> actions{};
86 std::unique_ptr<MockAction> action;
87
88 // First action will return true
89 action = std::make_unique<MockAction>();
90 EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(true));
91 actions.push_back(std::move(action));
92
93 // Second action will return false
94 action = std::make_unique<MockAction>();
95 EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(false));
96 actions.push_back(std::move(action));
97
98 EXPECT_EQ(action_utils::execute(actions, env), false);
99 }
100 catch (const std::exception& error)
101 {
102 ADD_FAILURE() << "Should not have caught exception.";
103 }
104
105 // Test where last action returns true
106 try
107 {
108 std::vector<std::unique_ptr<Action>> actions{};
109 std::unique_ptr<MockAction> action;
110
111 // First action will return false
112 action = std::make_unique<MockAction>();
113 EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(false));
114 actions.push_back(std::move(action));
115
116 // Second action will return true
117 action = std::make_unique<MockAction>();
118 EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(true));
119 actions.push_back(std::move(action));
120
121 EXPECT_EQ(action_utils::execute(actions, env), true);
122 }
123 catch (const std::exception& error)
124 {
125 ADD_FAILURE() << "Should not have caught exception.";
126 }
127}