Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 1 | /** |
| 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 McCarney | 9fc08e7 | 2019-11-07 13:18:55 -0600 | [diff] [blame] | 16 | #include "action.hpp" |
| 17 | #include "action_environment.hpp" |
| 18 | #include "id_map.hpp" |
| 19 | #include "mock_action.hpp" |
Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 20 | #include "rule.hpp" |
| 21 | |
Shawn McCarney | 9fc08e7 | 2019-11-07 13:18:55 -0600 | [diff] [blame] | 22 | #include <exception> |
| 23 | #include <memory> |
| 24 | #include <stdexcept> |
| 25 | #include <utility> |
| 26 | #include <vector> |
| 27 | |
| 28 | #include <gmock/gmock.h> |
Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 29 | #include <gtest/gtest.h> |
| 30 | |
| 31 | using namespace phosphor::power::regulators; |
| 32 | |
Shawn McCarney | 9fc08e7 | 2019-11-07 13:18:55 -0600 | [diff] [blame] | 33 | using ::testing::Return; |
| 34 | using ::testing::Throw; |
| 35 | |
Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 36 | TEST(RuleTests, Constructor) |
| 37 | { |
Shawn McCarney | 9fc08e7 | 2019-11-07 13:18:55 -0600 | [diff] [blame] | 38 | // 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 McCarney | 4afb285 | 2019-10-27 18:28:53 -0500 | [diff] [blame] | 45 | EXPECT_EQ(rule.getID(), "set_voltage_rule"); |
Shawn McCarney | 9fc08e7 | 2019-11-07 13:18:55 -0600 | [diff] [blame] | 46 | EXPECT_EQ(rule.getActions().size(), 2); |
| 47 | } |
| 48 | |
| 49 | TEST(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 | |
| 99 | TEST(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 McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 113 | } |
| 114 | |
Shawn McCarney | 4afb285 | 2019-10-27 18:28:53 -0500 | [diff] [blame] | 115 | TEST(RuleTests, GetID) |
Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 116 | { |
Shawn McCarney | 9fc08e7 | 2019-11-07 13:18:55 -0600 | [diff] [blame] | 117 | Rule rule("read_sensor_values", std::vector<std::unique_ptr<Action>>{}); |
Shawn McCarney | 4afb285 | 2019-10-27 18:28:53 -0500 | [diff] [blame] | 118 | EXPECT_EQ(rule.getID(), "read_sensor_values"); |
Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 119 | } |