blob: 3204b19e9d85646a0f0c88664210f4954dfa6b2f [file] [log] [blame]
Wludzik, Jozef76833cb2020-12-21 14:42:41 +01001#include "dbus_environment.hpp"
2#include "helpers.hpp"
Cezary Zwolaka4e67612021-02-18 13:16:16 +01003#include "mocks/json_storage_mock.hpp"
Wludzik, Jozef76833cb2020-12-21 14:42:41 +01004#include "mocks/trigger_factory_mock.hpp"
5#include "mocks/trigger_mock.hpp"
6#include "params/trigger_params.hpp"
Cezary Zwolak4416fce2021-03-17 03:21:06 +01007#include "trigger.hpp"
Wludzik, Jozef76833cb2020-12-21 14:42:41 +01008#include "trigger_manager.hpp"
Cezary Zwolak4416fce2021-03-17 03:21:06 +01009#include "utils/conversion_trigger.hpp"
Szymon Dompke32305f12022-07-05 15:37:21 +020010#include "utils/dbus_path_utils.hpp"
11#include "utils/string_utils.hpp"
Cezary Zwolak4416fce2021-03-17 03:21:06 +010012#include "utils/transform.hpp"
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010013
14using namespace testing;
Szymon Dompke1cdd7e42022-06-08 14:43:13 +020015using sdbusplus::message::object_path;
Szymon Dompke32305f12022-07-05 15:37:21 +020016using namespace std::literals::string_literals;
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010017
18class TestTriggerManager : public Test
19{
20 public:
Szymon Dompke32305f12022-07-05 15:37:21 +020021 TriggerParams triggerParams;
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010022 std::pair<boost::system::error_code, std::string>
23 addTrigger(const TriggerParams& params)
24 {
Cezary Zwolak4416fce2021-03-17 03:21:06 +010025 const auto sensorInfos =
26 utils::fromLabeledSensorsInfo(params.sensors());
Ed Tanous2efa95d2024-10-19 11:36:53 -070027 const std::vector<numeric::LabeledThresholdParam> numThresh =
28 params.numericThresholdParams();
29 const std::vector<discrete::LabeledThresholdParam> discThresh =
30 params.discreteThresholdParams();
31 const std::vector<numeric::ThresholdParam> numThresh2 = std::get<0>(
32 utils::FromLabeledThresholdParamConversion()(numThresh));
33 const auto discThresh2 = std::get<1>(
34 utils::FromLabeledThresholdParamConversion()(discThresh));
Cezary Zwolak4416fce2021-03-17 03:21:06 +010035
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010036 std::promise<std::pair<boost::system::error_code, std::string>>
37 addTriggerPromise;
38 DbusEnvironment::getBus()->async_method_call(
39 [&addTriggerPromise](boost::system::error_code ec,
40 const std::string& path) {
Patrick Williams3a1c2972023-05-10 07:51:04 -050041 addTriggerPromise.set_value({ec, path});
Patrick Williamsc7935fa2023-10-20 11:19:30 -050042 },
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010043 DbusEnvironment::serviceName(), TriggerManager::triggerManagerPath,
Szymon Dompkee28aa532021-10-27 12:33:12 +020044 TriggerManager::triggerManagerIfaceName, "AddTrigger", params.id(),
Szymon Dompke94f71c52021-12-10 07:16:33 +010045 params.name(),
46 utils::transform(
47 params.triggerActions(),
48 [](const auto& action) { return actionToString(action); }),
Ed Tanous2efa95d2024-10-19 11:36:53 -070049 sensorInfos, params.reports(), numThresh2, discThresh2);
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010050 return DbusEnvironment::waitForFuture(addTriggerPromise.get_future());
51 }
52
Cezary Zwolak4416fce2021-03-17 03:21:06 +010053 std::unique_ptr<TriggerManager> makeTriggerManager()
54 {
55 return std::make_unique<TriggerManager>(
56 std::move(triggerFactoryMockPtr), std::move(storageMockPtr),
57 DbusEnvironment::getObjServer());
58 }
59
60 void SetUp() override
61 {
62 sut = makeTriggerManager();
63 }
64
Cezary Zwolaka4e67612021-02-18 13:16:16 +010065 std::unique_ptr<StorageMock> storageMockPtr =
66 std::make_unique<NiceMock<StorageMock>>();
67 StorageMock& storageMock = *storageMockPtr;
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010068 std::unique_ptr<TriggerFactoryMock> triggerFactoryMockPtr =
69 std::make_unique<NiceMock<TriggerFactoryMock>>();
70 TriggerFactoryMock& triggerFactoryMock = *triggerFactoryMockPtr;
71 std::unique_ptr<TriggerMock> triggerMockPtr =
Szymon Dompkee28aa532021-10-27 12:33:12 +020072 std::make_unique<NiceMock<TriggerMock>>(TriggerParams().id());
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010073 TriggerMock& triggerMock = *triggerMockPtr;
Cezary Zwolak4416fce2021-03-17 03:21:06 +010074 std::unique_ptr<TriggerManager> sut;
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010075 MockFunction<void(std::string)> checkPoint;
76};
77
78TEST_F(TestTriggerManager, addTrigger)
79{
Cezary Zwolak4416fce2021-03-17 03:21:06 +010080 triggerFactoryMock.expectMake(TriggerParams(), Ref(*sut), Ref(storageMock))
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010081 .WillOnce(Return(ByMove(std::move(triggerMockPtr))));
82
Cezary Zwolak4416fce2021-03-17 03:21:06 +010083 auto [ec, path] = addTrigger(TriggerParams());
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010084 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
85 EXPECT_THAT(path, Eq(triggerMock.getPath()));
86}
87
Szymon Dompkef763c9e2021-03-12 09:19:22 +010088TEST_F(TestTriggerManager, addTriggerWithDiscreteThresholds)
89{
90 TriggerParams triggerParamsDiscrete;
Cezary Zwolak4416fce2021-03-17 03:21:06 +010091 auto thresholds = std::vector<discrete::LabeledThresholdParam>{
Szymon Dompke9f346792021-07-14 21:07:11 +020092 {"discrete_threshold1", discrete::Severity::ok, 10, "11.0"},
93 {"discrete_threshold2", discrete::Severity::warning, 10, "12.0"},
94 {"discrete_threshold3", discrete::Severity::critical, 10, "13.0"}};
Szymon Dompkef763c9e2021-03-12 09:19:22 +010095
Szymon Dompke20013012021-07-23 09:54:20 +020096 triggerParamsDiscrete.thresholdParams(thresholds);
Szymon Dompkef763c9e2021-03-12 09:19:22 +010097
98 auto [ec, path] = addTrigger(triggerParamsDiscrete);
99 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
100 EXPECT_THAT(path, Eq(triggerMock.getPath()));
101}
102
103TEST_F(TestTriggerManager, addDiscreteTriggerWithoutThresholds)
104{
105 TriggerParams triggerParamsDiscrete;
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100106 auto thresholds = std::vector<discrete::LabeledThresholdParam>();
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100107
Szymon Dompke20013012021-07-23 09:54:20 +0200108 triggerParamsDiscrete.thresholdParams(thresholds);
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100109
110 auto [ec, path] = addTrigger(triggerParamsDiscrete);
111 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
112 EXPECT_THAT(path, Eq(triggerMock.getPath()));
113}
114
Michal Orzelb47b7db2024-09-16 10:02:29 +0200115TEST_F(TestTriggerManager, failToAddTriggerTwice)
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100116{
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100117 triggerFactoryMock.expectMake(TriggerParams(), Ref(*sut), Ref(storageMock))
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100118 .WillOnce(Return(ByMove(std::move(triggerMockPtr))));
119
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100120 addTrigger(TriggerParams());
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100121
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100122 auto [ec, path] = addTrigger(TriggerParams());
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100123 EXPECT_THAT(ec.value(), Eq(boost::system::errc::file_exists));
124 EXPECT_THAT(path, Eq(std::string()));
125}
126
Michal Orzelb47b7db2024-09-16 10:02:29 +0200127TEST_F(TestTriggerManager, failToAddTriggerWithInvalidId)
Szymon Dompkee28aa532021-10-27 12:33:12 +0200128{
129 triggerFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
130 .Times(0);
131
132 auto [ec, path] = addTrigger(TriggerParams().id("not valid?"));
133 EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
134 EXPECT_THAT(path, Eq(std::string()));
135}
136
Michal Orzelb47b7db2024-09-16 10:02:29 +0200137TEST_F(TestTriggerManager, failToAddTriggerWithDuplicatesInReportsIds)
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100138{
139 triggerFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
140 .Times(0);
141
142 auto [ec, path] = addTrigger(
143 TriggerParams().reportIds({"trigger1", "trigger2", "trigger1"}));
144 EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
145 EXPECT_THAT(path, Eq(std::string()));
146}
147
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200148TEST_F(TestTriggerManager, addTriggerWithProperReportPaths)
149{
150 auto [ec, path] = addTrigger(TriggerParams().reports(
151 {object_path("/xyz/openbmc_project/Telemetry/Reports/MyReport"),
152 object_path(
153 "/xyz/openbmc_project/Telemetry/Reports/MyPrefix/MyReport")}));
154 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
155 EXPECT_THAT(path, Eq(triggerMock.getPath()));
156}
157
Michal Orzelb47b7db2024-09-16 10:02:29 +0200158TEST_F(TestTriggerManager, failToAddTriggerWithBadReportsPath)
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200159{
160 triggerFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
161 .Times(0);
162
163 auto [ec, path] = addTrigger(TriggerParams().reports(
164 {object_path("/xyz/openbmc_project/Telemetry/NotReports/MyReport")}));
165 EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
166 EXPECT_THAT(path, Eq(std::string()));
167}
168
Michal Orzelb47b7db2024-09-16 10:02:29 +0200169TEST_F(TestTriggerManager, failToAddTriggerWithTooManyReportPrefixes)
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200170{
171 triggerFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
172 .Times(0);
173
174 auto [ec, path] = addTrigger(TriggerParams().reports({object_path(
175 "/xyz/openbmc_project/Telemetry/Reports/P1/P2/MyReport")}));
176 EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
177 EXPECT_THAT(path, Eq(std::string()));
178}
179
Szymon Dompkee28aa532021-10-27 12:33:12 +0200180TEST_F(TestTriggerManager, addTriggerWithoutIdAndName)
181{
182 triggerFactoryMock
183 .expectMake(TriggerParams()
184 .id(TriggerManager::triggerNameDefault)
185 .name(TriggerManager::triggerNameDefault),
186 Ref(*sut), Ref(storageMock))
187 .WillOnce(Return(ByMove(std::move(triggerMockPtr))));
188
189 auto [ec, path] = addTrigger(TriggerParams().id("").name(""));
190 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
191 EXPECT_THAT(path, Not(Eq("")));
192}
193
194TEST_F(TestTriggerManager, addTriggerWithPrefixId)
195{
196 triggerFactoryMock
197 .expectMake(TriggerParams()
198 .id("TelemetryService/HackyName")
199 .name("Hacky/Name!@#$"),
200 Ref(*sut), Ref(storageMock))
201 .WillOnce(Return(ByMove(std::move(triggerMockPtr))));
202
203 auto [ec, path] = addTrigger(
204 TriggerParams().id("TelemetryService/").name("Hacky/Name!@#$"));
205 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
206 EXPECT_THAT(path, Not(Eq("")));
207}
208
209TEST_F(TestTriggerManager, addTriggerWithoutIdTwice)
210{
211 addTrigger(TriggerParams().id(""));
212
213 auto [ec, path] = addTrigger(TriggerParams().id(""));
214 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
215 EXPECT_THAT(path, Not(Eq("")));
216}
217
218TEST_F(TestTriggerManager, addTriggerWithoutIdAndWithLongNameTwice)
219{
Szymon Dompke32305f12022-07-05 15:37:21 +0200220 std::string longName = utils::string_utils::getMaxName();
221 addTrigger(TriggerParams().id("").name(longName));
Szymon Dompkee28aa532021-10-27 12:33:12 +0200222
Szymon Dompke32305f12022-07-05 15:37:21 +0200223 auto [ec, path] = addTrigger(TriggerParams().id("").name(longName));
Szymon Dompkee28aa532021-10-27 12:33:12 +0200224 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
225 EXPECT_THAT(path, Not(Eq("")));
226}
227
228TEST_F(TestTriggerManager, addTriggerWithMaxLengthId)
229{
Szymon Dompke32305f12022-07-05 15:37:21 +0200230 std::string reportId = utils::string_utils::getMaxId();
231 triggerParams.id(reportId);
232 triggerFactoryMock.expectMake(triggerParams, Ref(*sut), Ref(storageMock));
Szymon Dompkee28aa532021-10-27 12:33:12 +0200233
234 auto [ec, path] = addTrigger(triggerParams);
235
236 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
Szymon Dompke32305f12022-07-05 15:37:21 +0200237 EXPECT_THAT(path, Eq("/"s + reportId));
238}
239
240TEST_F(TestTriggerManager, addTriggerWithMaxLengthPrefix)
241{
242 std::string reportId = utils::string_utils::getMaxPrefix() + "/MyId";
243 triggerParams.id(reportId);
244 triggerFactoryMock.expectMake(triggerParams, Ref(*sut), Ref(storageMock));
245
246 auto [ec, path] = addTrigger(triggerParams);
247
248 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
249 EXPECT_THAT(path, Eq("/"s + reportId));
250}
251
252TEST_F(TestTriggerManager, addTriggerWithMaxLengthName)
253{
254 triggerParams.name(utils::string_utils::getMaxName());
255 triggerFactoryMock.expectMake(triggerParams, Ref(*sut), Ref(storageMock));
256
257 auto [ec, path] = addTrigger(triggerParams);
258
259 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
260 EXPECT_THAT(path, Eq("/"s + triggerParams.id()));
261}
262
263TEST_F(TestTriggerManager, addTriggerWithMaxLengthDiscreteThresholdName)
264{
265 namespace ts = utils::tstring;
266
267 triggerParams =
268 TriggerParams()
269 .id("DiscreteTrigger")
270 .name("My Discrete Trigger")
271 .thresholdParams(std::vector<discrete::LabeledThresholdParam>{
272 discrete::LabeledThresholdParam{
273 utils::string_utils::getMaxName(),
274 discrete::Severity::warning, Milliseconds(10).count(),
275 "15.2"}});
276
277 triggerFactoryMock.expectMake(triggerParams, Ref(*sut), Ref(storageMock));
278
279 auto [ec, path] = addTrigger(triggerParams);
280
281 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
282 EXPECT_THAT(path, Eq("/"s + triggerParams.id()));
283}
284
Michal Orzelb47b7db2024-09-16 10:02:29 +0200285TEST_F(TestTriggerManager, failToAddTriggerWithTooLongFullId)
Szymon Dompke32305f12022-07-05 15:37:21 +0200286{
287 triggerFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
288 .Times(0);
289
290 triggerParams.id(
291 std::string(utils::constants::maxReportFullIdLength + 1, 'z'));
292
293 auto [ec, path] = addTrigger(triggerParams);
294
295 EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
296 EXPECT_THAT(path, Eq(std::string()));
Szymon Dompkee28aa532021-10-27 12:33:12 +0200297}
298
Michal Orzelb47b7db2024-09-16 10:02:29 +0200299TEST_F(TestTriggerManager, failToAddTriggerWithTooLongId)
Szymon Dompkee28aa532021-10-27 12:33:12 +0200300{
Szymon Dompke32305f12022-07-05 15:37:21 +0200301 triggerFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
302 .Times(0);
303
304 triggerParams.id(utils::string_utils::getTooLongId());
305
306 auto [ec, path] = addTrigger(triggerParams);
307
308 EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
309 EXPECT_THAT(path, Eq(std::string()));
310}
311
Michal Orzelb47b7db2024-09-16 10:02:29 +0200312TEST_F(TestTriggerManager, failToAddTriggerWithTooLongPrefix)
Szymon Dompke32305f12022-07-05 15:37:21 +0200313{
314 triggerFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
315 .Times(0);
316
317 triggerParams.id(utils::string_utils::getTooLongPrefix() + "/MyId");
318
319 auto [ec, path] = addTrigger(triggerParams);
320
321 EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
322 EXPECT_THAT(path, Eq(std::string()));
323}
324
Michal Orzelb47b7db2024-09-16 10:02:29 +0200325TEST_F(TestTriggerManager, failToAddTriggerWithTooManyPrefixes)
Szymon Dompke32305f12022-07-05 15:37:21 +0200326{
327 triggerFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
328 .Times(0);
329
330 std::string reportId;
331 for (size_t i = 0; i < utils::constants::maxPrefixesInId + 1; i++)
332 {
333 reportId += "prefix/";
334 }
335 reportId += "MyId";
336
337 triggerParams.id(reportId);
338
339 auto [ec, path] = addTrigger(triggerParams);
340
341 EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
342 EXPECT_THAT(path, Eq(std::string()));
343}
344
Michal Orzelb47b7db2024-09-16 10:02:29 +0200345TEST_F(TestTriggerManager, failToAddTriggerWithTooLongName)
Szymon Dompke32305f12022-07-05 15:37:21 +0200346{
347 triggerFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
348 .Times(0);
349
350 triggerParams.name(utils::string_utils::getTooLongName());
351
352 auto [ec, path] = addTrigger(triggerParams);
353
354 EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
355 EXPECT_THAT(path, Eq(std::string()));
356}
357
Michal Orzelb47b7db2024-09-16 10:02:29 +0200358TEST_F(TestTriggerManager, failToAddTriggerWithTooLongMetricId)
Szymon Dompke32305f12022-07-05 15:37:21 +0200359{
360 namespace ts = utils::tstring;
361
362 triggerParams =
363 TriggerParams()
364 .id("DiscreteTrigger")
365 .name("My Discrete Trigger")
366 .thresholdParams(std::vector<discrete::LabeledThresholdParam>{
367 discrete::LabeledThresholdParam{
368 utils::string_utils::getTooLongName(),
369 discrete::Severity::warning, Milliseconds(10).count(),
370 "15.2"}});
Szymon Dompkee28aa532021-10-27 12:33:12 +0200371
372 triggerFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
373 .Times(0);
374
375 auto [ec, path] = addTrigger(triggerParams);
376
377 EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
378 EXPECT_THAT(path, Eq(std::string()));
379}
380
Michal Orzelb47b7db2024-09-16 10:02:29 +0200381TEST_F(TestTriggerManager, failToAddTriggerWhenMaxTriggerIsReached)
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100382{
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100383 auto triggerParams = TriggerParams();
384
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100385 triggerFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100386 .Times(TriggerManager::maxTriggers);
387
388 for (size_t i = 0; i < TriggerManager::maxTriggers; i++)
389 {
Szymon Dompkee28aa532021-10-27 12:33:12 +0200390 triggerParams.id(TriggerParams().id() + std::to_string(i));
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100391
392 auto [ec, path] = addTrigger(triggerParams);
393 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
394 }
395
Szymon Dompkee28aa532021-10-27 12:33:12 +0200396 triggerParams.id(TriggerParams().id() +
397 std::to_string(TriggerManager::maxTriggers));
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100398 auto [ec, path] = addTrigger(triggerParams);
399 EXPECT_THAT(ec.value(), Eq(boost::system::errc::too_many_files_open));
400 EXPECT_THAT(path, Eq(std::string()));
401}
402
403TEST_F(TestTriggerManager, removeTrigger)
404{
405 {
406 InSequence seq;
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100407 triggerFactoryMock
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100408 .expectMake(TriggerParams(), Ref(*sut), Ref(storageMock))
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100409 .WillOnce(Return(ByMove(std::move(triggerMockPtr))));
410 EXPECT_CALL(triggerMock, Die());
411 EXPECT_CALL(checkPoint, Call("end"));
412 }
413
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100414 addTrigger(TriggerParams());
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100415 sut->removeTrigger(&triggerMock);
416 checkPoint.Call("end");
417}
418
419TEST_F(TestTriggerManager, removingTriggerThatIsNotInContainerHasNoEffect)
420{
421 {
422 InSequence seq;
423 EXPECT_CALL(checkPoint, Call("end"));
424 EXPECT_CALL(triggerMock, Die());
425 }
426
427 sut->removeTrigger(&triggerMock);
428 checkPoint.Call("end");
429}
430
431TEST_F(TestTriggerManager, removingSameTriggerTwiceHasNoSideEffect)
432{
433 {
434 InSequence seq;
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100435 triggerFactoryMock
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100436 .expectMake(TriggerParams(), Ref(*sut), Ref(storageMock))
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100437 .WillOnce(Return(ByMove(std::move(triggerMockPtr))));
438 EXPECT_CALL(triggerMock, Die());
439 EXPECT_CALL(checkPoint, Call("end"));
440 }
441
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100442 addTrigger(TriggerParams());
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100443 sut->removeTrigger(&triggerMock);
444 sut->removeTrigger(&triggerMock);
445 checkPoint.Call("end");
446}
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100447class TestTriggerManagerStorage : public TestTriggerManager
448{
449 public:
450 using FilePath = interfaces::JsonStorage::FilePath;
451 using DirectoryPath = interfaces::JsonStorage::DirectoryPath;
452
453 void SetUp() override
454 {
455 ON_CALL(storageMock, list())
456 .WillByDefault(Return(std::vector<FilePath>{
457 {FilePath("trigger1")}, {FilePath("trigger2")}}));
458
459 ON_CALL(storageMock, load(FilePath("trigger1")))
460 .WillByDefault(InvokeWithoutArgs([this] { return data1; }));
461
Szymon Dompkee28aa532021-10-27 12:33:12 +0200462 data2["Id"] = "Trigger2";
463 data2["Name"] = "Second Trigger";
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100464 ON_CALL(storageMock, load(FilePath("trigger2")))
465 .WillByDefault(InvokeWithoutArgs([this] { return data2; }));
466 }
467
468 nlohmann::json data1 = nlohmann::json{
469 {"Version", Trigger::triggerVersion},
Szymon Dompkee28aa532021-10-27 12:33:12 +0200470 {"Id", TriggerParams().id()},
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100471 {"Name", TriggerParams().name()},
472 {"ThresholdParamsDiscriminator",
473 TriggerParams().thresholdParams().index()},
Patrick Williamsc7935fa2023-10-20 11:19:30 -0500474 {"TriggerActions", utils::transform(TriggerParams().triggerActions(),
475 [](const auto& action) {
476 return actionToString(action);
477 })},
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100478 {"ThresholdParams", utils::labeledThresholdParamsToJson(
479 TriggerParams().thresholdParams())},
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100480 {"ReportIds", TriggerParams().reportIds()},
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100481 {"Sensors", TriggerParams().sensors()}};
482
483 nlohmann::json data2 = data1;
484};
485
486TEST_F(TestTriggerManagerStorage, triggerManagerCtorAddTriggerFromStorage)
487{
488 triggerFactoryMock.expectMake(TriggerParams(), _, Ref(storageMock));
Szymon Dompkee28aa532021-10-27 12:33:12 +0200489 triggerFactoryMock.expectMake(
490 TriggerParams().id("Trigger2").name("Second Trigger"), _,
491 Ref(storageMock));
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100492 EXPECT_CALL(storageMock, remove(_)).Times(0);
493
494 sut = makeTriggerManager();
495}
496
497TEST_F(TestTriggerManagerStorage,
498 triggerManagerCtorRemoveDiscreteTriggerFromStorage)
499{
500 LabeledTriggerThresholdParams thresholdParams =
501 std::vector<discrete::LabeledThresholdParam>{
Szymon Dompke9f346792021-07-14 21:07:11 +0200502 {"userId1", discrete::Severity::warning, 15, "10.0"},
503 {"userId2", discrete::Severity::critical, 5, "20.0"}};
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100504
505 data1["ThresholdParamsDiscriminator"] = thresholdParams.index();
506
507 data1["ThresholdParams"] =
508 utils::labeledThresholdParamsToJson(thresholdParams);
509
510 EXPECT_CALL(storageMock, remove(FilePath("trigger1"))).Times(0);
511
512 sut = makeTriggerManager();
513}
514
515TEST_F(TestTriggerManagerStorage,
516 triggerManagerCtorRemoveDiscreteTriggerFromStorage2)
517{
518 data1["IsDiscrete"] = true;
519
520 EXPECT_CALL(storageMock, remove(FilePath("trigger1"))).Times(0);
521
522 sut = makeTriggerManager();
523}
524
525TEST_F(TestTriggerManagerStorage,
526 triggerManagerCtorAddProperRemoveInvalidTriggerFromStorage)
527{
528 data1["Version"] = Trigger::triggerVersion - 1;
529
Szymon Dompkee28aa532021-10-27 12:33:12 +0200530 triggerFactoryMock.expectMake(
531 TriggerParams().id("Trigger2").name("Second Trigger"), _,
532 Ref(storageMock));
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100533 EXPECT_CALL(storageMock, remove(FilePath("trigger1")));
534
535 sut = makeTriggerManager();
536}