blob: f6e5d82e2be80f5d45348cd3b8ad4dcfee2d72a3 [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"
Shawn McCarneya2461b32019-10-24 18:53:01 -050020#include "rule.hpp"
21
Shawn McCarney9fc08e72019-11-07 13:18:55 -060022#include <exception>
23#include <memory>
24#include <stdexcept>
25#include <utility>
26#include <vector>
27
28#include <gmock/gmock.h>
Shawn McCarneya2461b32019-10-24 18:53:01 -050029#include <gtest/gtest.h>
30
31using namespace phosphor::power::regulators;
32
Shawn McCarney9fc08e72019-11-07 13:18:55 -060033using ::testing::Return;
34using ::testing::Throw;
35
Shawn McCarneya2461b32019-10-24 18:53:01 -050036TEST(RuleTests, Constructor)
37{
Shawn McCarney9fc08e72019-11-07 13:18:55 -060038 // Build vector of actions
39 std::vector<std::unique_ptr<Action>> actions{};
40 actions.push_back(std::make_unique<MockAction>());
41 actions.push_back(std::make_unique<MockAction>());
42
43 // Create rule and verify data members
44 Rule rule("set_voltage_rule", std::move(actions));
Shawn McCarney4afb2852019-10-27 18:28:53 -050045 EXPECT_EQ(rule.getID(), "set_voltage_rule");
Shawn McCarney9fc08e72019-11-07 13:18:55 -060046 EXPECT_EQ(rule.getActions().size(), 2);
47}
48
49TEST(RuleTests, Execute)
50{
51 // Create ActionEnvironment
52 IDMap idMap{};
53 ActionEnvironment env{idMap, ""};
54
55 // Test where an action throws an exception
56 try
57 {
58 std::vector<std::unique_ptr<Action>> actions{};
59 std::unique_ptr<MockAction> action = std::make_unique<MockAction>();
60 EXPECT_CALL(*action, execute)
61 .Times(1)
62 .WillOnce(Throw(std::logic_error{"Communication error"}));
63 actions.push_back(std::move(action));
64
65 Rule rule("set_voltage_rule", std::move(actions));
66 rule.execute(env);
67 ADD_FAILURE() << "Should not have reached this line.";
68 }
69 catch (const std::exception& error)
70 {
71 EXPECT_STREQ(error.what(), "Communication error");
72 }
73
74 // Test where all actions are executed
75 try
76 {
77 std::vector<std::unique_ptr<Action>> actions{};
78 std::unique_ptr<MockAction> action;
79
80 // First action will return true
81 action = std::make_unique<MockAction>();
82 EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(true));
83 actions.push_back(std::move(action));
84
85 // Second action will return false
86 action = std::make_unique<MockAction>();
87 EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(false));
88 actions.push_back(std::move(action));
89
90 Rule rule("set_voltage_rule", std::move(actions));
91 EXPECT_EQ(rule.execute(env), false);
92 }
93 catch (const std::exception& error)
94 {
95 ADD_FAILURE() << "Should not have caught exception.";
96 }
97}
98
99TEST(RuleTests, GetActions)
100{
101 std::vector<std::unique_ptr<Action>> actions{};
102
103 MockAction* action1 = new MockAction{};
104 actions.push_back(std::unique_ptr<MockAction>{action1});
105
106 MockAction* action2 = new MockAction{};
107 actions.push_back(std::unique_ptr<MockAction>{action2});
108
109 Rule rule("set_voltage_rule", std::move(actions));
110 EXPECT_EQ(rule.getActions().size(), 2);
111 EXPECT_EQ(rule.getActions()[0].get(), action1);
112 EXPECT_EQ(rule.getActions()[1].get(), action2);
Shawn McCarneya2461b32019-10-24 18:53:01 -0500113}
114
Shawn McCarney4afb2852019-10-27 18:28:53 -0500115TEST(RuleTests, GetID)
Shawn McCarneya2461b32019-10-24 18:53:01 -0500116{
Shawn McCarney9fc08e72019-11-07 13:18:55 -0600117 Rule rule("read_sensor_values", std::vector<std::unique_ptr<Action>>{});
Shawn McCarney4afb2852019-10-27 18:28:53 -0500118 EXPECT_EQ(rule.getID(), "read_sensor_values");
Shawn McCarneya2461b32019-10-24 18:53:01 -0500119}