1 #include "dbus_environment.hpp"
2 #include "helpers.hpp"
3 #include "messages/update_report_ind.hpp"
4 #include "trigger_actions.hpp"
5 #include "utils/messanger.hpp"
6
7 #include <stdexcept>
8
9 #include <gmock/gmock.h>
10
11 using namespace testing;
12
13 namespace action
14 {
15 namespace numeric
16 {
17 using LogParam = std::tuple<::numeric::Type, double, double>;
18
getCorrectParams()19 static auto getCorrectParams()
20 {
21 return Values(std::make_tuple(::numeric::Type::upperCritical, 91.1, 90),
22 std::make_tuple(::numeric::Type::lowerCritical, 91.2, 90),
23 std::make_tuple(::numeric::Type::upperWarning, 88.5, 90),
24 std::make_tuple(::numeric::Type::lowerWarning, 88.6, 90));
25 }
26
getIncorrectParams()27 static auto getIncorrectParams()
28 {
29 return Values(
30 std::make_tuple(::numeric::Type::upperCritical, 90.0, 90),
31 std::make_tuple(static_cast<::numeric::Type>(-1), 88.0, 90),
32 std::make_tuple(static_cast<::numeric::Type>(123), 123.0, 90));
33 }
34
35 template <typename ActionType>
36 class TestActionNumeric : public Test, public WithParamInterface<LogParam>
37 {
38 public:
SetUp()39 void SetUp() override
40 {
41 auto [type, threshold, value] = GetParam();
42 sut = std::make_unique<ActionType>(type, threshold);
43 commmitValue = value;
44 }
45
commit()46 void commit()
47 {
48 sut->commit("MyTrigger", std::nullopt, "MySensor",
49 Milliseconds{100'000}, commmitValue);
50 }
51
52 std::unique_ptr<ActionType> sut;
53 double commmitValue;
54 };
55
56 class TestLogToJournalNumeric : public TestActionNumeric<LogToJournal>
57 {};
58
59 INSTANTIATE_TEST_SUITE_P(LogToJournalNumericParams, TestLogToJournalNumeric,
60 getCorrectParams());
61
TEST_P(TestLogToJournalNumeric,commitAnActionDoesNotThrow)62 TEST_P(TestLogToJournalNumeric, commitAnActionDoesNotThrow)
63 {
64 EXPECT_NO_THROW(commit());
65 }
66
67 class TestLogToJournalNumericThrow : public TestLogToJournalNumeric
68 {};
69
70 INSTANTIATE_TEST_SUITE_P(_, TestLogToJournalNumericThrow, getIncorrectParams());
71
TEST_P(TestLogToJournalNumericThrow,commitAnActionExpectThrow)72 TEST_P(TestLogToJournalNumericThrow, commitAnActionExpectThrow)
73 {
74 EXPECT_ANY_THROW(commit());
75 }
76
77 class TestLogToRedfishEventLogNumeric :
78 public TestActionNumeric<LogToRedfishEventLog>
79 {};
80
81 INSTANTIATE_TEST_SUITE_P(LogToRedfishEventLogNumericParams,
82 TestLogToRedfishEventLogNumeric, getCorrectParams());
83
TEST_P(TestLogToRedfishEventLogNumeric,commitExpectNoThrow)84 TEST_P(TestLogToRedfishEventLogNumeric, commitExpectNoThrow)
85 {
86 EXPECT_NO_THROW(commit());
87 }
88
89 class TestLogToRedfishEventLogNumericThrow :
90 public TestLogToRedfishEventLogNumeric
91 {};
92
93 INSTANTIATE_TEST_SUITE_P(_, TestLogToRedfishEventLogNumericThrow,
94 getIncorrectParams());
95
TEST_P(TestLogToRedfishEventLogNumericThrow,commitExpectToThrow)96 TEST_P(TestLogToRedfishEventLogNumericThrow, commitExpectToThrow)
97 {
98 EXPECT_ANY_THROW(commit());
99 }
100
101 } // namespace numeric
102
103 namespace discrete
104 {
105 using LogParam = std::tuple<::discrete::Severity, TriggerValue>;
106
getCorrectParams()107 static auto getCorrectParams()
108 {
109 return Values(
110 std::make_tuple(::discrete::Severity::critical,
111 TriggerValue("DiscreteVal")),
112 std::make_tuple(::discrete::Severity::warning, TriggerValue("On")),
113 std::make_tuple(::discrete::Severity::ok, TriggerValue("Off")));
114 }
115
getIncorrectParams()116 static auto getIncorrectParams()
117 {
118 return Values(
119 std::make_tuple(static_cast<::discrete::Severity>(-1),
120 TriggerValue("DiscreteVal42")),
121 std::make_tuple(static_cast<::discrete::Severity>(42),
122 TriggerValue("On")),
123 std::make_tuple(::discrete::Severity::critical, TriggerValue(42.0)),
124 std::make_tuple(::discrete::Severity::warning, TriggerValue(0.0)),
125 std::make_tuple(::discrete::Severity::ok, TriggerValue(0.1)));
126 }
127
128 template <typename ActionType>
129 class TestActionDiscrete : public Test, public WithParamInterface<LogParam>
130 {
131 public:
SetUp()132 void SetUp() override
133 {
134 auto [severity, value] = GetParam();
135 sut = std::make_unique<ActionType>(severity);
136 commitValue = value;
137 }
138
commit()139 void commit()
140 {
141 std::string thresholdName = "MyThreshold";
142 sut->commit("MyTrigger", std::cref(thresholdName), "MySensor",
143 Milliseconds{100'000}, commitValue);
144 }
145
146 TriggerValue commitValue;
147 std::unique_ptr<ActionType> sut;
148 };
149
150 class TestLogToJournalDiscrete : public TestActionDiscrete<LogToJournal>
151 {};
152
153 INSTANTIATE_TEST_SUITE_P(LogToJournalDiscreteParams, TestLogToJournalDiscrete,
154 getCorrectParams());
155
TEST_P(TestLogToJournalDiscrete,commitAnActionWIthDiscreteValueDoesNotThrow)156 TEST_P(TestLogToJournalDiscrete, commitAnActionWIthDiscreteValueDoesNotThrow)
157 {
158 EXPECT_NO_THROW(commit());
159 }
160
161 class TestLogToJournalDiscreteThrow : public TestLogToJournalDiscrete
162 {};
163
164 INSTANTIATE_TEST_SUITE_P(_, TestLogToJournalDiscreteThrow,
165 getIncorrectParams());
166
TEST_P(TestLogToJournalDiscreteThrow,commitAnActionExpectThrow)167 TEST_P(TestLogToJournalDiscreteThrow, commitAnActionExpectThrow)
168 {
169 EXPECT_ANY_THROW(commit());
170 }
171
172 class TestLogToRedfishEventLogDiscrete :
173 public TestActionDiscrete<LogToRedfishEventLog>
174 {};
175
176 INSTANTIATE_TEST_SUITE_P(LogToRedfishEventLogDiscreteParams,
177 TestLogToRedfishEventLogDiscrete, getCorrectParams());
178
TEST_P(TestLogToRedfishEventLogDiscrete,commitExpectNoThrow)179 TEST_P(TestLogToRedfishEventLogDiscrete, commitExpectNoThrow)
180 {
181 EXPECT_NO_THROW(commit());
182 }
183
184 class TestLogToRedfishEventLogDiscreteThrow :
185 public TestLogToRedfishEventLogDiscrete
186 {};
187
188 INSTANTIATE_TEST_SUITE_P(_, TestLogToRedfishEventLogDiscreteThrow,
189 getIncorrectParams());
190
TEST_P(TestLogToRedfishEventLogDiscreteThrow,commitExpectToThrow)191 TEST_P(TestLogToRedfishEventLogDiscreteThrow, commitExpectToThrow)
192 {
193 EXPECT_ANY_THROW(commit());
194 }
195
196 namespace onChange
197 {
198
199 template <typename ActionType>
200 class TestActionOnChange : public Test
201 {
202 public:
SetUp()203 void SetUp() override
204 {
205 sut = std::make_unique<ActionType>();
206 }
207
commit(TriggerValue value)208 void commit(TriggerValue value)
209 {
210 sut->commit("MyTrigger", std::nullopt, "MySensor",
211 Milliseconds{100'000}, value);
212 }
213
214 std::unique_ptr<ActionType> sut;
215 };
216
217 class TestLogToJournalDiscreteOnChange : public TestActionOnChange<LogToJournal>
218 {};
219
TEST_F(TestLogToJournalDiscreteOnChange,commitNumericValueExpectNoThrow)220 TEST_F(TestLogToJournalDiscreteOnChange, commitNumericValueExpectNoThrow)
221 {
222 EXPECT_NO_THROW(commit(90.0));
223 }
224
TEST_F(TestLogToJournalDiscreteOnChange,commitDiscreteValueExpectNoThrow)225 TEST_F(TestLogToJournalDiscreteOnChange, commitDiscreteValueExpectNoThrow)
226 {
227 EXPECT_NO_THROW(commit("Off"));
228 }
229
230 class TestLogToRedfishEventLogDiscreteOnChange :
231 public TestActionOnChange<LogToRedfishEventLog>
232 {};
233
TEST_F(TestLogToRedfishEventLogDiscreteOnChange,commitNumericValueExpectNoThrow)234 TEST_F(TestLogToRedfishEventLogDiscreteOnChange,
235 commitNumericValueExpectNoThrow)
236 {
237 EXPECT_NO_THROW(commit(90.0));
238 }
239
TEST_F(TestLogToRedfishEventLogDiscreteOnChange,commitDiscreteValueExpectNoThrow)240 TEST_F(TestLogToRedfishEventLogDiscreteOnChange,
241 commitDiscreteValueExpectNoThrow)
242 {
243 EXPECT_NO_THROW(commit("Off"));
244 }
245
246 } // namespace onChange
247 } // namespace discrete
248
249 class TestUpdateReport : public Test
250 {
251 public:
TestUpdateReport()252 TestUpdateReport() : messanger(DbusEnvironment::getIoc()) {}
253
make(std::vector<std::string> names)254 void make(std::vector<std::string> names)
255 {
256 messanger.on_receive<messages::UpdateReportInd>(
257 [this](const auto& msg) { updateReport.Call(msg); });
258
259 sut = std::make_unique<UpdateReport>(
260 DbusEnvironment::getIoc(),
261 std::make_shared<std::vector<std::string>>(std::move(names)));
262 }
263
commit(TriggerValue value)264 void commit(TriggerValue value)
265 {
266 sut->commit(triggerId, std::nullopt, "MySensor", Milliseconds{100'000},
267 value);
268 }
269
270 utils::Messanger messanger;
271 NiceMock<MockFunction<void(const messages::UpdateReportInd&)>> updateReport;
272 std::unique_ptr<UpdateReport> sut;
273 std::string triggerId = "MyTrigger";
274 };
275
TEST_F(TestUpdateReport,commitWhenReportNameIsEmptyExpectNoReportUpdate)276 TEST_F(TestUpdateReport, commitWhenReportNameIsEmptyExpectNoReportUpdate)
277 {
278 EXPECT_CALL(updateReport, Call(_)).Times(0);
279
280 make({});
281 commit(90.0);
282 }
283
TEST_F(TestUpdateReport,commitExpectReportUpdate)284 TEST_F(TestUpdateReport, commitExpectReportUpdate)
285 {
286 std::vector<std::string> names = {"Report1", "Report2", "Report3"};
287 EXPECT_CALL(updateReport,
288 Call(FieldsAre(UnorderedElementsAreArray(names))));
289
290 make(names);
291 commit(90.0);
292 }
293
294 } // namespace action
295