blob: 68526860fc6a17fd7c1516672a2d8e90cee848ee [file] [log] [blame]
Shawn McCarneya2461b32019-10-24 18:53:01 -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 */
Shawn McCarney9fc08e72019-11-07 13:18:55 -060016#include "action.hpp"
17#include "action_environment.hpp"
18#include "id_map.hpp"
19#include "mock_action.hpp"
Bob King73eacee2020-10-23 13:58:02 +080020#include "mock_services.hpp"
Shawn McCarneya2461b32019-10-24 18:53:01 -050021#include "rule.hpp"
22
Shawn McCarney9fc08e72019-11-07 13:18:55 -060023#include <exception>
24#include <memory>
25#include <stdexcept>
26#include <utility>
27#include <vector>
28
29#include <gmock/gmock.h>
Shawn McCarneya2461b32019-10-24 18:53:01 -050030#include <gtest/gtest.h>
31
32using namespace phosphor::power::regulators;
33
Shawn McCarney9fc08e72019-11-07 13:18:55 -060034using ::testing::Return;
35using ::testing::Throw;
36
Shawn McCarneya2461b32019-10-24 18:53:01 -050037TEST(RuleTests, Constructor)
38{
Shawn McCarney9fc08e72019-11-07 13:18:55 -060039 // Build vector of actions
40 std::vector<std::unique_ptr<Action>> actions{};
41 actions.push_back(std::make_unique<MockAction>());
42 actions.push_back(std::make_unique<MockAction>());
43
44 // Create rule and verify data members
45 Rule rule("set_voltage_rule", std::move(actions));
Shawn McCarney4afb2852019-10-27 18:28:53 -050046 EXPECT_EQ(rule.getID(), "set_voltage_rule");
Shawn McCarney9fc08e72019-11-07 13:18:55 -060047 EXPECT_EQ(rule.getActions().size(), 2);
48}
49
50TEST(RuleTests, Execute)
51{
52 // Create ActionEnvironment
53 IDMap idMap{};
Bob King73eacee2020-10-23 13:58:02 +080054 MockServices services{};
55 ActionEnvironment env{idMap, "", services};
Shawn McCarney9fc08e72019-11-07 13:18:55 -060056
57 // Test where an action throws an exception
58 try
59 {
60 std::vector<std::unique_ptr<Action>> actions{};
61 std::unique_ptr<MockAction> action = std::make_unique<MockAction>();
62 EXPECT_CALL(*action, execute)
63 .Times(1)
64 .WillOnce(Throw(std::logic_error{"Communication error"}));
65 actions.push_back(std::move(action));
66
67 Rule rule("set_voltage_rule", std::move(actions));
68 rule.execute(env);
69 ADD_FAILURE() << "Should not have reached this line.";
70 }
71 catch (const std::exception& error)
72 {
73 EXPECT_STREQ(error.what(), "Communication error");
74 }
75
76 // Test where all actions are executed
77 try
78 {
79 std::vector<std::unique_ptr<Action>> actions{};
80 std::unique_ptr<MockAction> action;
81
82 // First action will return true
83 action = std::make_unique<MockAction>();
84 EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(true));
85 actions.push_back(std::move(action));
86
87 // Second action will return false
88 action = std::make_unique<MockAction>();
89 EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(false));
90 actions.push_back(std::move(action));
91
92 Rule rule("set_voltage_rule", std::move(actions));
93 EXPECT_EQ(rule.execute(env), false);
94 }
95 catch (const std::exception& error)
96 {
97 ADD_FAILURE() << "Should not have caught exception.";
98 }
99}
100
101TEST(RuleTests, GetActions)
102{
103 std::vector<std::unique_ptr<Action>> actions{};
104
105 MockAction* action1 = new MockAction{};
106 actions.push_back(std::unique_ptr<MockAction>{action1});
107
108 MockAction* action2 = new MockAction{};
109 actions.push_back(std::unique_ptr<MockAction>{action2});
110
111 Rule rule("set_voltage_rule", std::move(actions));
112 EXPECT_EQ(rule.getActions().size(), 2);
113 EXPECT_EQ(rule.getActions()[0].get(), action1);
114 EXPECT_EQ(rule.getActions()[1].get(), action2);
Shawn McCarneya2461b32019-10-24 18:53:01 -0500115}
116
Shawn McCarney4afb2852019-10-27 18:28:53 -0500117TEST(RuleTests, GetID)
Shawn McCarneya2461b32019-10-24 18:53:01 -0500118{
Shawn McCarney9fc08e72019-11-07 13:18:55 -0600119 Rule rule("read_sensor_values", std::vector<std::unique_ptr<Action>>{});
Shawn McCarney4afb2852019-10-27 18:28:53 -0500120 EXPECT_EQ(rule.getID(), "read_sensor_values");
Shawn McCarneya2461b32019-10-24 18:53:01 -0500121}