blob: 8e8274fcfe3846a42ab0e69b4c6c041f14adaa29 [file] [log] [blame]
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +01001#include "dbus_environment.hpp"
Szymon Dompke1cdd7e42022-06-08 14:43:13 +02002#include "helpers.hpp"
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +01003#include "messages/update_report_ind.hpp"
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +01004#include "trigger_actions.hpp"
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +01005#include "utils/messanger.hpp"
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +01006
7#include <stdexcept>
8
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +01009#include <gmock/gmock.h>
10
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +010011using namespace testing;
12
13namespace action
14{
Szymon Dompkef763c9e2021-03-12 09:19:22 +010015namespace numeric
16{
Michal Orzel8018a3b2024-06-26 14:14:16 +020017using LogParam = std::tuple<::numeric::Type, double, TriggerValue>;
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +010018
Szymon Dompkef763c9e2021-03-12 09:19:22 +010019static auto getCorrectParams()
20{
Michal Orzel8018a3b2024-06-26 14:14:16 +020021 return Values(
22 std::make_tuple(::numeric::Type::upperCritical, 91.1,
23 TriggerValue(90.0)),
24 std::make_tuple(::numeric::Type::upperCritical, 90, TriggerValue(91.1)),
25 std::make_tuple(::numeric::Type::lowerCritical, 91.2,
26 TriggerValue(90.0)),
27 std::make_tuple(::numeric::Type::lowerCritical, 90, TriggerValue(91.2)),
28 std::make_tuple(::numeric::Type::upperWarning, 88.5,
29 TriggerValue(90.0)),
30 std::make_tuple(::numeric::Type::upperWarning, 90, TriggerValue(88.5)),
31 std::make_tuple(::numeric::Type::lowerWarning, 88.6,
32 TriggerValue(90.0)),
33 std::make_tuple(::numeric::Type::lowerWarning, 90, TriggerValue(88.6)));
Szymon Dompkef763c9e2021-03-12 09:19:22 +010034}
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +010035
Szymon Dompkef763c9e2021-03-12 09:19:22 +010036static auto getIncorrectParams()
37{
Michal Orzel8018a3b2024-06-26 14:14:16 +020038 return Values(std::make_tuple(::numeric::Type::upperCritical, 90.0,
39 TriggerValue(90.0)),
40 std::make_tuple(static_cast<::numeric::Type>(-1), 88.0,
41 TriggerValue(90.0)),
42 std::make_tuple(static_cast<::numeric::Type>(123), 123.0,
43 TriggerValue(90.0)),
44 std::make_tuple(::numeric::Type::upperCritical, 90.0,
45 TriggerValue("numeric")));
Szymon Dompkef763c9e2021-03-12 09:19:22 +010046}
47
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020048template <typename ActionType>
49class TestActionNumeric : public Test, public WithParamInterface<LogParam>
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +010050{
51 public:
52 void SetUp() override
53 {
Szymon Dompkef763c9e2021-03-12 09:19:22 +010054 auto [type, threshold, value] = GetParam();
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020055 sut = std::make_unique<ActionType>(type, threshold);
Szymon Dompkef763c9e2021-03-12 09:19:22 +010056 commmitValue = value;
57 }
58
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020059 void commit()
60 {
61 sut->commit("MyTrigger", std::nullopt, "MySensor",
62 Milliseconds{100'000}, commmitValue);
63 }
64
65 std::unique_ptr<ActionType> sut;
Michal Orzel8018a3b2024-06-26 14:14:16 +020066 TriggerValue commmitValue;
Szymon Dompkef763c9e2021-03-12 09:19:22 +010067};
68
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020069class TestLogToJournalNumeric : public TestActionNumeric<LogToJournal>
70{};
71
Szymon Dompkef763c9e2021-03-12 09:19:22 +010072INSTANTIATE_TEST_SUITE_P(LogToJournalNumericParams, TestLogToJournalNumeric,
73 getCorrectParams());
74
75TEST_P(TestLogToJournalNumeric, commitAnActionDoesNotThrow)
76{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020077 EXPECT_NO_THROW(commit());
Szymon Dompkef763c9e2021-03-12 09:19:22 +010078}
79
80class TestLogToJournalNumericThrow : public TestLogToJournalNumeric
81{};
82
83INSTANTIATE_TEST_SUITE_P(_, TestLogToJournalNumericThrow, getIncorrectParams());
84
85TEST_P(TestLogToJournalNumericThrow, commitAnActionExpectThrow)
86{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020087 EXPECT_ANY_THROW(commit());
Szymon Dompkef763c9e2021-03-12 09:19:22 +010088}
89
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020090class TestLogToRedfishEventLogNumeric :
91 public TestActionNumeric<LogToRedfishEventLog>
Szymon Dompkef763c9e2021-03-12 09:19:22 +010092{};
93
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020094INSTANTIATE_TEST_SUITE_P(LogToRedfishEventLogNumericParams,
95 TestLogToRedfishEventLogNumeric, getCorrectParams());
Szymon Dompkef763c9e2021-03-12 09:19:22 +010096
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020097TEST_P(TestLogToRedfishEventLogNumeric, commitExpectNoThrow)
Szymon Dompkef763c9e2021-03-12 09:19:22 +010098{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020099 EXPECT_NO_THROW(commit());
100}
101
102class TestLogToRedfishEventLogNumericThrow :
103 public TestLogToRedfishEventLogNumeric
104{};
105
106INSTANTIATE_TEST_SUITE_P(_, TestLogToRedfishEventLogNumericThrow,
107 getIncorrectParams());
108
109TEST_P(TestLogToRedfishEventLogNumericThrow, commitExpectToThrow)
110{
111 EXPECT_ANY_THROW(commit());
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100112}
113
114} // namespace numeric
115
116namespace discrete
117{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200118using LogParam = std::tuple<::discrete::Severity, TriggerValue>;
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100119
120static auto getCorrectParams()
121{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200122 return Values(
123 std::make_tuple(::discrete::Severity::critical,
124 TriggerValue("DiscreteVal")),
125 std::make_tuple(::discrete::Severity::warning, TriggerValue("On")),
126 std::make_tuple(::discrete::Severity::ok, TriggerValue("Off")));
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100127}
128
129static auto getIncorrectParams()
130{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200131 return Values(
132 std::make_tuple(static_cast<::discrete::Severity>(-1),
133 TriggerValue("DiscreteVal42")),
134 std::make_tuple(static_cast<::discrete::Severity>(42),
135 TriggerValue("On")),
136 std::make_tuple(::discrete::Severity::critical, TriggerValue(42.0)),
137 std::make_tuple(::discrete::Severity::warning, TriggerValue(0.0)),
138 std::make_tuple(::discrete::Severity::ok, TriggerValue(0.1)));
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100139}
140
Michal Orzel8018a3b2024-06-26 14:14:16 +0200141class TestLogToJournalDiscrete :
142 public Test,
143 public WithParamInterface<LogParam>
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100144{
145 public:
146 void SetUp() override
147 {
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200148 auto [severity, value] = GetParam();
Michal Orzel8018a3b2024-06-26 14:14:16 +0200149 sut = std::make_unique<LogToJournal>(severity);
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200150 commitValue = value;
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100151 }
152
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200153 void commit()
154 {
155 std::string thresholdName = "MyThreshold";
156 sut->commit("MyTrigger", std::cref(thresholdName), "MySensor",
157 Milliseconds{100'000}, commitValue);
158 }
159
160 TriggerValue commitValue;
Michal Orzel8018a3b2024-06-26 14:14:16 +0200161 std::unique_ptr<LogToJournal> sut;
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100162};
163
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100164INSTANTIATE_TEST_SUITE_P(LogToJournalDiscreteParams, TestLogToJournalDiscrete,
165 getCorrectParams());
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100166
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200167TEST_P(TestLogToJournalDiscrete, commitAnActionWIthDiscreteValueDoesNotThrow)
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100168{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200169 EXPECT_NO_THROW(commit());
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100170}
171
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100172class TestLogToJournalDiscreteThrow : public TestLogToJournalDiscrete
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100173{};
174
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100175INSTANTIATE_TEST_SUITE_P(_, TestLogToJournalDiscreteThrow,
176 getIncorrectParams());
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100177
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100178TEST_P(TestLogToJournalDiscreteThrow, commitAnActionExpectThrow)
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100179{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200180 EXPECT_ANY_THROW(commit());
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100181}
182
Michal Orzel8018a3b2024-06-26 14:14:16 +0200183class TestLogToRedfishEventLogDiscrete : public Test
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200184{
Michal Orzel8018a3b2024-06-26 14:14:16 +0200185 public:
186 void SetUp()
187 {
188 sut = std::make_unique<LogToRedfishEventLog>();
189 }
190
191 void commit(TriggerValue value)
192 {
193 std::string thresholdName = "MyThreshold";
194 sut->commit("MyTrigger", std::cref(thresholdName), "MySensor",
195 Milliseconds{100'000}, value);
196 }
197
198 std::unique_ptr<LogToRedfishEventLog> sut;
199};
200
201TEST_F(TestLogToRedfishEventLogDiscrete, commitDiscreteValueExpectNoThrow)
202{
203 EXPECT_NO_THROW(commit("DiscreteVal"));
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200204}
205
Michal Orzel8018a3b2024-06-26 14:14:16 +0200206TEST_F(TestLogToRedfishEventLogDiscrete, commitNumericValueExpectToThrow)
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100207{
Michal Orzel8018a3b2024-06-26 14:14:16 +0200208 EXPECT_ANY_THROW(commit(42.0));
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100209}
210
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100211namespace onChange
212{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200213
214template <typename ActionType>
215class TestActionOnChange : public Test
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100216{
217 public:
218 void SetUp() override
219 {
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200220 sut = std::make_unique<ActionType>();
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100221 }
222
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200223 void commit(TriggerValue value)
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100224 {
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200225 sut->commit("MyTrigger", std::nullopt, "MySensor",
226 Milliseconds{100'000}, value);
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100227 }
228
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200229 std::unique_ptr<ActionType> sut;
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100230};
231
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200232class TestLogToJournalDiscreteOnChange : public TestActionOnChange<LogToJournal>
233{};
234
235TEST_F(TestLogToJournalDiscreteOnChange, commitNumericValueExpectNoThrow)
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100236{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200237 EXPECT_NO_THROW(commit(90.0));
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100238}
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200239
240TEST_F(TestLogToJournalDiscreteOnChange, commitDiscreteValueExpectNoThrow)
241{
242 EXPECT_NO_THROW(commit("Off"));
243}
244
245class TestLogToRedfishEventLogDiscreteOnChange :
246 public TestActionOnChange<LogToRedfishEventLog>
247{};
248
249TEST_F(TestLogToRedfishEventLogDiscreteOnChange,
250 commitNumericValueExpectNoThrow)
251{
252 EXPECT_NO_THROW(commit(90.0));
253}
254
255TEST_F(TestLogToRedfishEventLogDiscreteOnChange,
256 commitDiscreteValueExpectNoThrow)
257{
258 EXPECT_NO_THROW(commit("Off"));
259}
260
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100261} // namespace onChange
262} // namespace discrete
263
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100264class TestUpdateReport : public Test
265{
266 public:
Patrick Williams3a1c2972023-05-10 07:51:04 -0500267 TestUpdateReport() : messanger(DbusEnvironment::getIoc()) {}
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100268
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100269 void make(std::vector<std::string> names)
270 {
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100271 messanger.on_receive<messages::UpdateReportInd>(
272 [this](const auto& msg) { updateReport.Call(msg); });
273
Szymon Dompke94f71c52021-12-10 07:16:33 +0100274 sut = std::make_unique<UpdateReport>(
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100275 DbusEnvironment::getIoc(),
Szymon Dompke94f71c52021-12-10 07:16:33 +0100276 std::make_shared<std::vector<std::string>>(std::move(names)));
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100277 }
278
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200279 void commit(TriggerValue value)
280 {
281 sut->commit(triggerId, std::nullopt, "MySensor", Milliseconds{100'000},
282 value);
283 }
284
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100285 utils::Messanger messanger;
286 NiceMock<MockFunction<void(const messages::UpdateReportInd&)>> updateReport;
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100287 std::unique_ptr<UpdateReport> sut;
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200288 std::string triggerId = "MyTrigger";
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100289};
290
291TEST_F(TestUpdateReport, commitWhenReportNameIsEmptyExpectNoReportUpdate)
292{
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100293 EXPECT_CALL(updateReport, Call(_)).Times(0);
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100294
295 make({});
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200296 commit(90.0);
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100297}
298
299TEST_F(TestUpdateReport, commitExpectReportUpdate)
300{
301 std::vector<std::string> names = {"Report1", "Report2", "Report3"};
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100302 EXPECT_CALL(updateReport,
303 Call(FieldsAre(UnorderedElementsAreArray(names))));
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100304
305 make(names);
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200306 commit(90.0);
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100307}
308
309} // namespace action