blob: e8c48d44d28988406fb3c0490f043acc4c351ce9 [file] [log] [blame]
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +01001#include "dbus_environment.hpp"
2#include "messages/update_report_ind.hpp"
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +01003#include "trigger_actions.hpp"
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +01004#include "utils/messanger.hpp"
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +01005
6#include <stdexcept>
7
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +01008#include <gmock/gmock.h>
9
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +010010using namespace testing;
11
12namespace action
13{
Szymon Dompkef763c9e2021-03-12 09:19:22 +010014namespace numeric
15{
16using LogParam = std::tuple<::numeric::Type, double, double>;
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +010017
Szymon Dompkef763c9e2021-03-12 09:19:22 +010018static auto getCorrectParams()
19{
20 return Values(std::make_tuple(::numeric::Type::upperCritical, 91.1, 90),
21 std::make_tuple(::numeric::Type::lowerCritical, 91.2, 90),
22 std::make_tuple(::numeric::Type::upperWarning, 88.5, 90),
23 std::make_tuple(::numeric::Type::lowerWarning, 88.6, 90));
24}
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +010025
Szymon Dompkef763c9e2021-03-12 09:19:22 +010026static auto getIncorrectParams()
27{
28 return Values(
29 std::make_tuple(::numeric::Type::upperCritical, 90.0, 90),
30 std::make_tuple(static_cast<::numeric::Type>(-1), 88.0, 90),
31 std::make_tuple(static_cast<::numeric::Type>(123), 123.0, 90));
32}
33
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020034template <typename ActionType>
35class TestActionNumeric : public Test, public WithParamInterface<LogParam>
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +010036{
37 public:
38 void SetUp() override
39 {
Szymon Dompkef763c9e2021-03-12 09:19:22 +010040 auto [type, threshold, value] = GetParam();
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020041 sut = std::make_unique<ActionType>(type, threshold);
Szymon Dompkef763c9e2021-03-12 09:19:22 +010042 commmitValue = value;
43 }
44
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020045 void commit()
46 {
47 sut->commit("MyTrigger", std::nullopt, "MySensor",
48 Milliseconds{100'000}, commmitValue);
49 }
50
51 std::unique_ptr<ActionType> sut;
Szymon Dompkef763c9e2021-03-12 09:19:22 +010052 double commmitValue;
53};
54
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020055class TestLogToJournalNumeric : public TestActionNumeric<LogToJournal>
56{};
57
Szymon Dompkef763c9e2021-03-12 09:19:22 +010058INSTANTIATE_TEST_SUITE_P(LogToJournalNumericParams, TestLogToJournalNumeric,
59 getCorrectParams());
60
61TEST_P(TestLogToJournalNumeric, commitAnActionDoesNotThrow)
62{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020063 EXPECT_NO_THROW(commit());
Szymon Dompkef763c9e2021-03-12 09:19:22 +010064}
65
66class TestLogToJournalNumericThrow : public TestLogToJournalNumeric
67{};
68
69INSTANTIATE_TEST_SUITE_P(_, TestLogToJournalNumericThrow, getIncorrectParams());
70
71TEST_P(TestLogToJournalNumericThrow, commitAnActionExpectThrow)
72{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020073 EXPECT_ANY_THROW(commit());
Szymon Dompkef763c9e2021-03-12 09:19:22 +010074}
75
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020076class TestLogToRedfishEventLogNumeric :
77 public TestActionNumeric<LogToRedfishEventLog>
Szymon Dompkef763c9e2021-03-12 09:19:22 +010078{};
79
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020080INSTANTIATE_TEST_SUITE_P(LogToRedfishEventLogNumericParams,
81 TestLogToRedfishEventLogNumeric, getCorrectParams());
Szymon Dompkef763c9e2021-03-12 09:19:22 +010082
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020083TEST_P(TestLogToRedfishEventLogNumeric, commitExpectNoThrow)
Szymon Dompkef763c9e2021-03-12 09:19:22 +010084{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020085 EXPECT_NO_THROW(commit());
86}
87
88class TestLogToRedfishEventLogNumericThrow :
89 public TestLogToRedfishEventLogNumeric
90{};
91
92INSTANTIATE_TEST_SUITE_P(_, TestLogToRedfishEventLogNumericThrow,
93 getIncorrectParams());
94
95TEST_P(TestLogToRedfishEventLogNumericThrow, commitExpectToThrow)
96{
97 EXPECT_ANY_THROW(commit());
Szymon Dompkef763c9e2021-03-12 09:19:22 +010098}
99
100} // namespace numeric
101
102namespace discrete
103{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200104using LogParam = std::tuple<::discrete::Severity, TriggerValue>;
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100105
106static auto getCorrectParams()
107{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200108 return Values(
109 std::make_tuple(::discrete::Severity::critical,
110 TriggerValue("DiscreteVal")),
111 std::make_tuple(::discrete::Severity::warning, TriggerValue("On")),
112 std::make_tuple(::discrete::Severity::ok, TriggerValue("Off")));
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100113}
114
115static auto getIncorrectParams()
116{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200117 return Values(
118 std::make_tuple(static_cast<::discrete::Severity>(-1),
119 TriggerValue("DiscreteVal42")),
120 std::make_tuple(static_cast<::discrete::Severity>(42),
121 TriggerValue("On")),
122 std::make_tuple(::discrete::Severity::critical, TriggerValue(42.0)),
123 std::make_tuple(::discrete::Severity::warning, TriggerValue(0.0)),
124 std::make_tuple(::discrete::Severity::ok, TriggerValue(0.1)));
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100125}
126
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200127template <typename ActionType>
128class TestActionDiscrete : public Test, public WithParamInterface<LogParam>
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100129{
130 public:
131 void SetUp() override
132 {
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200133 auto [severity, value] = GetParam();
134 sut = std::make_unique<ActionType>(severity);
135 commitValue = value;
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100136 }
137
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200138 void commit()
139 {
140 std::string thresholdName = "MyThreshold";
141 sut->commit("MyTrigger", std::cref(thresholdName), "MySensor",
142 Milliseconds{100'000}, commitValue);
143 }
144
145 TriggerValue commitValue;
146 std::unique_ptr<ActionType> sut;
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100147};
148
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200149class TestLogToJournalDiscrete : public TestActionDiscrete<LogToJournal>
150{};
151
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100152INSTANTIATE_TEST_SUITE_P(LogToJournalDiscreteParams, TestLogToJournalDiscrete,
153 getCorrectParams());
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100154
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200155TEST_P(TestLogToJournalDiscrete, commitAnActionWIthDiscreteValueDoesNotThrow)
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100156{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200157 EXPECT_NO_THROW(commit());
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100158}
159
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100160class TestLogToJournalDiscreteThrow : public TestLogToJournalDiscrete
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100161{};
162
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100163INSTANTIATE_TEST_SUITE_P(_, TestLogToJournalDiscreteThrow,
164 getIncorrectParams());
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100165
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100166TEST_P(TestLogToJournalDiscreteThrow, commitAnActionExpectThrow)
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100167{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200168 EXPECT_ANY_THROW(commit());
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100169}
170
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200171class TestLogToRedfishEventLogDiscrete :
172 public TestActionDiscrete<LogToRedfishEventLog>
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100173{};
174
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200175INSTANTIATE_TEST_SUITE_P(LogToRedfishEventLogDiscreteParams,
176 TestLogToRedfishEventLogDiscrete, getCorrectParams());
177
178TEST_P(TestLogToRedfishEventLogDiscrete, commitExpectNoThrow)
179{
180 EXPECT_NO_THROW(commit());
181}
182
183class TestLogToRedfishEventLogDiscreteThrow :
184 public TestLogToRedfishEventLogDiscrete
185{};
186
187INSTANTIATE_TEST_SUITE_P(_, TestLogToRedfishEventLogDiscreteThrow,
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100188 getIncorrectParams());
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100189
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200190TEST_P(TestLogToRedfishEventLogDiscreteThrow, commitExpectToThrow)
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100191{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200192 EXPECT_ANY_THROW(commit());
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100193}
194
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100195namespace onChange
196{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200197
198template <typename ActionType>
199class TestActionOnChange : public Test
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100200{
201 public:
202 void SetUp() override
203 {
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200204 sut = std::make_unique<ActionType>();
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100205 }
206
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200207 void commit(TriggerValue value)
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100208 {
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200209 sut->commit("MyTrigger", std::nullopt, "MySensor",
210 Milliseconds{100'000}, value);
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100211 }
212
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200213 std::unique_ptr<ActionType> sut;
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100214};
215
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200216class TestLogToJournalDiscreteOnChange : public TestActionOnChange<LogToJournal>
217{};
218
219TEST_F(TestLogToJournalDiscreteOnChange, commitNumericValueExpectNoThrow)
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100220{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200221 EXPECT_NO_THROW(commit(90.0));
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100222}
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200223
224TEST_F(TestLogToJournalDiscreteOnChange, commitDiscreteValueExpectNoThrow)
225{
226 EXPECT_NO_THROW(commit("Off"));
227}
228
229class TestLogToRedfishEventLogDiscreteOnChange :
230 public TestActionOnChange<LogToRedfishEventLog>
231{};
232
233TEST_F(TestLogToRedfishEventLogDiscreteOnChange,
234 commitNumericValueExpectNoThrow)
235{
236 EXPECT_NO_THROW(commit(90.0));
237}
238
239TEST_F(TestLogToRedfishEventLogDiscreteOnChange,
240 commitDiscreteValueExpectNoThrow)
241{
242 EXPECT_NO_THROW(commit("Off"));
243}
244
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100245} // namespace onChange
246} // namespace discrete
247
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100248class TestUpdateReport : public Test
249{
250 public:
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100251 TestUpdateReport() : messanger(DbusEnvironment::getIoc())
252 {}
253
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100254 void make(std::vector<std::string> names)
255 {
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100256 messanger.on_receive<messages::UpdateReportInd>(
257 [this](const auto& msg) { updateReport.Call(msg); });
258
Szymon Dompke94f71c52021-12-10 07:16:33 +0100259 sut = std::make_unique<UpdateReport>(
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100260 DbusEnvironment::getIoc(),
Szymon Dompke94f71c52021-12-10 07:16:33 +0100261 std::make_shared<std::vector<std::string>>(std::move(names)));
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100262 }
263
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200264 void commit(TriggerValue value)
265 {
266 sut->commit(triggerId, std::nullopt, "MySensor", Milliseconds{100'000},
267 value);
268 }
269
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100270 utils::Messanger messanger;
271 NiceMock<MockFunction<void(const messages::UpdateReportInd&)>> updateReport;
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100272 std::unique_ptr<UpdateReport> sut;
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200273 std::string triggerId = "MyTrigger";
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100274};
275
276TEST_F(TestUpdateReport, commitWhenReportNameIsEmptyExpectNoReportUpdate)
277{
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100278 EXPECT_CALL(updateReport, Call(_)).Times(0);
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100279
280 make({});
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200281 commit(90.0);
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100282}
283
284TEST_F(TestUpdateReport, commitExpectReportUpdate)
285{
286 std::vector<std::string> names = {"Report1", "Report2", "Report3"};
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100287 EXPECT_CALL(updateReport,
288 Call(FieldsAre(UnorderedElementsAreArray(names))));
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100289
290 make(names);
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200291 commit(90.0);
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100292}
293
294} // namespace action