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 "not_action.hpp" 21 22 #include <exception> 23 #include <memory> 24 #include <stdexcept> 25 #include <utility> 26 27 #include <gmock/gmock.h> 28 #include <gtest/gtest.h> 29 30 using namespace phosphor::power::regulators; 31 32 using ::testing::Return; 33 using ::testing::Throw; 34 35 TEST(NotActionTests, Constructor) 36 { 37 NotAction notAction{std::make_unique<MockAction>()}; 38 EXPECT_NE(notAction.getAction().get(), nullptr); 39 } 40 41 TEST(NotActionTests, Execute) 42 { 43 // Create ActionEnvironment 44 IDMap idMap{}; 45 ActionEnvironment env{idMap, ""}; 46 47 // Test where negated action throws an exception 48 try 49 { 50 std::unique_ptr<MockAction> action = std::make_unique<MockAction>(); 51 EXPECT_CALL(*action, execute) 52 .Times(1) 53 .WillOnce(Throw(std::logic_error{"Communication error"})); 54 55 NotAction notAction{std::move(action)}; 56 notAction.execute(env); 57 ADD_FAILURE() << "Should not have reached this line."; 58 } 59 catch (const std::exception& error) 60 { 61 EXPECT_STREQ(error.what(), "Communication error"); 62 } 63 64 // Test where negated action returns true 65 try 66 { 67 std::unique_ptr<MockAction> action = std::make_unique<MockAction>(); 68 EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(true)); 69 70 NotAction notAction{std::move(action)}; 71 EXPECT_EQ(notAction.execute(env), false); 72 } 73 catch (const std::exception& error) 74 { 75 ADD_FAILURE() << "Should not have caught exception."; 76 } 77 78 // Test where negated action returns false 79 try 80 { 81 std::unique_ptr<MockAction> action = std::make_unique<MockAction>(); 82 EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(false)); 83 84 NotAction notAction{std::move(action)}; 85 EXPECT_EQ(notAction.execute(env), true); 86 } 87 catch (const std::exception& error) 88 { 89 ADD_FAILURE() << "Should not have caught exception."; 90 } 91 } 92 93 TEST(NotActionTests, GetAction) 94 { 95 MockAction* action = new MockAction{}; 96 NotAction notAction{std::unique_ptr<Action>{action}}; 97 EXPECT_EQ(notAction.getAction().get(), action); 98 } 99 100 TEST(NotActionTests, ToString) 101 { 102 NotAction notAction{std::make_unique<MockAction>()}; 103 EXPECT_EQ(notAction.toString(), "not: { ... }"); 104 } 105