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