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 */ 16 #include "action.hpp" 17 #include "action_environment.hpp" 18 #include "id_map.hpp" 19 #include "mock_action.hpp" 20 #include "rule.hpp" 21 22 #include <exception> 23 #include <memory> 24 #include <stdexcept> 25 #include <utility> 26 #include <vector> 27 28 #include <gmock/gmock.h> 29 #include <gtest/gtest.h> 30 31 using namespace phosphor::power::regulators; 32 33 using ::testing::Return; 34 using ::testing::Throw; 35 36 TEST(RuleTests, Constructor) 37 { 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)); 45 EXPECT_EQ(rule.getID(), "set_voltage_rule"); 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); 113 } 114 115 TEST(RuleTests, GetID) 116 { 117 Rule rule("read_sensor_values", std::vector<std::unique_ptr<Action>>{}); 118 EXPECT_EQ(rule.getID(), "read_sensor_values"); 119 } 120