1 #include "dbus_environment.hpp"
2 #include "helpers.hpp"
3 #include "messages/collect_trigger_id.hpp"
4 #include "messages/trigger_presence_changed_ind.hpp"
5 #include "mocks/json_storage_mock.hpp"
6 #include "mocks/report_manager_mock.hpp"
7 #include "mocks/sensor_mock.hpp"
8 #include "mocks/threshold_mock.hpp"
9 #include "mocks/trigger_factory_mock.hpp"
10 #include "mocks/trigger_manager_mock.hpp"
11 #include "params/trigger_params.hpp"
12 #include "trigger.hpp"
13 #include "trigger_manager.hpp"
14 #include "utils/conversion_trigger.hpp"
15 #include "utils/dbus_path_utils.hpp"
16 #include "utils/messanger.hpp"
17 #include "utils/string_utils.hpp"
18 #include "utils/transform.hpp"
19 #include "utils/tstring.hpp"
20 
21 #include <boost/range/combine.hpp>
22 
23 using namespace testing;
24 using namespace std::literals::string_literals;
25 using sdbusplus::message::object_path;
26 
27 static constexpr size_t expectedTriggerVersion = 2;
28 
29 class TestTrigger : public Test
30 {
31   public:
32     TriggerParams triggerParams;
33     TriggerParams triggerDiscreteParams =
34         TriggerParams()
35             .id("DiscreteTrigger")
36             .name("My Discrete Trigger")
37             .thresholdParams(std::vector<discrete::LabeledThresholdParam>{
38                 discrete::LabeledThresholdParam{
39                     "userId", discrete::Severity::warning,
40                     Milliseconds(10).count(), "15.2"},
41                 discrete::LabeledThresholdParam{
42                     "userId_2", discrete::Severity::critical,
43                     Milliseconds(5).count(), "32.7"},
44             });
45 
46     std::unique_ptr<ReportManagerMock> reportManagerMockPtr =
47         std::make_unique<NiceMock<ReportManagerMock>>();
48     std::unique_ptr<TriggerManagerMock> triggerManagerMockPtr =
49         std::make_unique<NiceMock<TriggerManagerMock>>();
50     std::unique_ptr<TriggerFactoryMock> triggerFactoryMockPtr =
51         std::make_unique<NiceMock<TriggerFactoryMock>>();
52     testing::NiceMock<StorageMock> storageMock;
53     NiceMock<MockFunction<void(const messages::TriggerPresenceChangedInd)>>
54         triggerPresenceChanged;
55     std::vector<std::shared_ptr<interfaces::Threshold>> thresholdMocks;
56     utils::Messanger messanger;
57     std::unique_ptr<Trigger> sut;
58 
TestTrigger()59     TestTrigger() : messanger(DbusEnvironment::getIoc())
60     {
61         messanger.on_receive<messages::TriggerPresenceChangedInd>(
62             [this](const auto& msg) { triggerPresenceChanged.Call(msg); });
63     }
64 
SetUp()65     void SetUp() override
66     {
67         sut = makeTrigger(triggerParams);
68     }
69 
70     static std::vector<LabeledSensorInfo>
convertToLabeledSensor(const SensorsInfo & sensorsInfo)71         convertToLabeledSensor(const SensorsInfo& sensorsInfo)
72     {
73         return utils::transform(sensorsInfo, [](const auto& sensorInfo) {
74             const auto& [sensorPath, sensorMetadata] = sensorInfo;
75             return LabeledSensorInfo("service1", sensorPath, sensorMetadata);
76         });
77     }
78 
makeTrigger(const TriggerParams & params)79     std::unique_ptr<Trigger> makeTrigger(const TriggerParams& params)
80     {
81         thresholdMocks =
82             ThresholdMock::makeThresholds(params.thresholdParams());
83 
84         auto id = std::make_unique<const std::string>(params.id());
85 
86         return std::make_unique<Trigger>(
87             DbusEnvironment::getIoc(), DbusEnvironment::getObjServer(),
88             std::move(id), params.name(), params.triggerActions(),
89             std::make_shared<std::vector<std::string>>(
90                 params.reportIds().begin(), params.reportIds().end()),
91             std::vector<std::shared_ptr<interfaces::Threshold>>(thresholdMocks),
92             *triggerManagerMockPtr, storageMock, *triggerFactoryMockPtr,
93             SensorMock::makeSensorMocks(params.sensors()));
94     }
95 
to_file_path(std::string name)96     static interfaces::JsonStorage::FilePath to_file_path(std::string name)
97     {
98         return interfaces::JsonStorage::FilePath(
99             std::to_string(std::hash<std::string>{}(name)));
100     }
101 
102     template <class T>
getProperty(const std::string & path,const std::string & property)103     static T getProperty(const std::string& path, const std::string& property)
104     {
105         return DbusEnvironment::getProperty<T>(path, Trigger::triggerIfaceName,
106                                                property);
107     }
108 
109     template <class T>
setProperty(const std::string & path,const std::string & property,const T & newValue)110     static boost::system::error_code setProperty(const std::string& path,
111                                                  const std::string& property,
112                                                  const T& newValue)
113     {
114         return DbusEnvironment::setProperty<T>(path, Trigger::triggerIfaceName,
115                                                property, newValue);
116     }
117 
118     template <class T>
119     struct ChangePropertyParams
120     {
121         Matcher<T> valueBefore = _;
122         T newValue;
123         Matcher<boost::system::error_code> ec =
124             Eq(boost::system::errc::success);
125         Matcher<T> valueAfter = Eq(newValue);
126     };
127 
128     template <class T>
changeProperty(const std::string & path,const std::string & property,ChangePropertyParams<T> p)129     static void changeProperty(const std::string& path,
130                                const std::string& property,
131                                ChangePropertyParams<T> p)
132     {
133         ASSERT_THAT(getProperty<T>(path, property), p.valueBefore);
134         ASSERT_THAT(setProperty<T>(path, property, p.newValue), p.ec);
135         EXPECT_THAT(getProperty<T>(path, property), p.valueAfter);
136     }
137 
deleteTrigger(const std::string & path)138     boost::system::error_code deleteTrigger(const std::string& path)
139     {
140         std::promise<boost::system::error_code> methodPromise;
141         DbusEnvironment::getBus()->async_method_call(
142             [&methodPromise](boost::system::error_code ec) {
143             methodPromise.set_value(ec);
144         }, DbusEnvironment::serviceName(), path, Trigger::deleteIfaceName,
145             "Delete");
146         return DbusEnvironment::waitForFuture(methodPromise.get_future());
147     }
148 };
149 
TEST_F(TestTrigger,checkIfPropertiesAreSet)150 TEST_F(TestTrigger, checkIfPropertiesAreSet)
151 {
152     EXPECT_THAT(getProperty<std::string>(sut->getPath(), "Name"),
153                 Eq(triggerParams.name()));
154     EXPECT_THAT(getProperty<bool>(sut->getPath(), "Persistent"), Eq(true));
155     EXPECT_THAT(
156         getProperty<std::vector<std::string>>(sut->getPath(), "TriggerActions"),
157         Eq(utils::transform(
158             triggerParams.triggerActions(),
159             [](const auto& action) { return actionToString(action); })));
160     EXPECT_THAT((getProperty<SensorsInfo>(sut->getPath(), "Sensors")),
161                 Eq(utils::fromLabeledSensorsInfo(triggerParams.sensors())));
162     EXPECT_THAT(
163         getProperty<std::vector<object_path>>(sut->getPath(), "Reports"),
164         Eq(triggerParams.reports()));
165     EXPECT_THAT(
166         getProperty<bool>(sut->getPath(), "Discrete"),
167         Eq(isTriggerThresholdDiscrete(triggerParams.thresholdParams())));
168     EXPECT_THAT(
169         getProperty<TriggerThresholdParams>(sut->getPath(), "Thresholds"),
170         Eq(std::visit(utils::FromLabeledThresholdParamConversion(),
171                       triggerParams.thresholdParams())));
172 }
173 
TEST_F(TestTrigger,checkBasicGetters)174 TEST_F(TestTrigger, checkBasicGetters)
175 {
176     EXPECT_THAT(sut->getId(), Eq(triggerParams.id()));
177     EXPECT_THAT(sut->getPath(),
178                 Eq(utils::constants::triggerDirPath.str + triggerParams.id()));
179 }
180 
TEST_F(TestTrigger,setPropertyNameToCorrectValue)181 TEST_F(TestTrigger, setPropertyNameToCorrectValue)
182 {
183     std::string name = "custom name 1234 %^#5";
184     EXPECT_THAT(setProperty(sut->getPath(), "Name", name),
185                 Eq(boost::system::errc::success));
186     EXPECT_THAT(getProperty<std::string>(sut->getPath(), "Name"), Eq(name));
187 }
188 
TEST_F(TestTrigger,setPropertyReportNames)189 TEST_F(TestTrigger, setPropertyReportNames)
190 {
191     std::vector<object_path> newNames = {
192         utils::constants::reportDirPath / "abc",
193         utils::constants::reportDirPath / "one",
194         utils::constants::reportDirPath / "prefix" / "two"};
195     EXPECT_THAT(setProperty(sut->getPath(), "Reports", newNames),
196                 Eq(boost::system::errc::success));
197     EXPECT_THAT(
198         getProperty<std::vector<object_path>>(sut->getPath(), "Reports"),
199         Eq(newNames));
200 }
201 
TEST_F(TestTrigger,sendsUpdateWhenReportNamesChanges)202 TEST_F(TestTrigger, sendsUpdateWhenReportNamesChanges)
203 {
204     std::vector<object_path> newPropertyVal = {
205         utils::constants::reportDirPath / "abc",
206         utils::constants::reportDirPath / "one",
207         utils::constants::reportDirPath / "two"};
208 
209     EXPECT_CALL(triggerPresenceChanged,
210                 Call(FieldsAre(messages::Presence::Exist, triggerParams.id(),
211                                UnorderedElementsAre("abc", "one", "two"))));
212 
213     EXPECT_THAT(setProperty(sut->getPath(), "Reports", newPropertyVal),
214                 Eq(boost::system::errc::success));
215 }
216 
TEST_F(TestTrigger,sendsUpdateWhenReportNamesChangesToSameValue)217 TEST_F(TestTrigger, sendsUpdateWhenReportNamesChangesToSameValue)
218 {
219     const std::vector<object_path> newPropertyVal = triggerParams.reports();
220 
221     EXPECT_CALL(
222         triggerPresenceChanged,
223         Call(FieldsAre(messages::Presence::Exist, triggerParams.id(),
224                        UnorderedElementsAreArray(triggerParams.reportIds()))));
225 
226     EXPECT_THAT(setProperty(sut->getPath(), "Reports", newPropertyVal),
227                 Eq(boost::system::errc::success));
228 }
229 
TEST_F(TestTrigger,settingPropertyReportNamesThrowsExceptionWhenDuplicateReportIds)230 TEST_F(TestTrigger,
231        settingPropertyReportNamesThrowsExceptionWhenDuplicateReportIds)
232 {
233     std::vector<object_path> newPropertyVal{
234         utils::constants::reportDirPath / "report1",
235         utils::constants::reportDirPath / "report2",
236         utils::constants::reportDirPath / "report1"};
237 
238     EXPECT_CALL(triggerPresenceChanged, Call(_)).Times(0);
239 
240     EXPECT_THAT(setProperty(sut->getPath(), "Reports", newPropertyVal),
241                 Eq(boost::system::errc::invalid_argument));
242 }
243 
TEST_F(TestTrigger,settingPropertyReportNamesThrowsExceptionWhenReportWithTooManyPrefixes)244 TEST_F(TestTrigger,
245        settingPropertyReportNamesThrowsExceptionWhenReportWithTooManyPrefixes)
246 {
247     std::vector<object_path> newPropertyVal{
248         object_path("/xyz/openbmc_project/Telemetry/Reports/P1/P2/MyReport")};
249 
250     EXPECT_CALL(triggerPresenceChanged, Call(_)).Times(0);
251 
252     EXPECT_THAT(setProperty(sut->getPath(), "Reports", newPropertyVal),
253                 Eq(boost::system::errc::invalid_argument));
254 }
255 
TEST_F(TestTrigger,settingPropertyReportNamesThrowsExceptionWhenReportWithTooLongPrefix)256 TEST_F(TestTrigger,
257        settingPropertyReportNamesThrowsExceptionWhenReportWithTooLongPrefix)
258 {
259     std::vector<object_path> newPropertyVal{
260         object_path("/xyz/openbmc_project/Telemetry/Reports/" +
261                     utils::string_utils::getTooLongPrefix() + "/MyReport")};
262 
263     EXPECT_CALL(triggerPresenceChanged, Call(_)).Times(0);
264 
265     EXPECT_THAT(setProperty(sut->getPath(), "Reports", newPropertyVal),
266                 Eq(boost::system::errc::invalid_argument));
267 }
268 
TEST_F(TestTrigger,settingPropertyReportNamesThrowsExceptionWhenReportWithTooLongId)269 TEST_F(TestTrigger,
270        settingPropertyReportNamesThrowsExceptionWhenReportWithTooLongId)
271 {
272     std::vector<object_path> newPropertyVal{
273         object_path("/xyz/openbmc_project/Telemetry/Reports/Prefix/" +
274                     utils::string_utils::getTooLongId())};
275 
276     EXPECT_CALL(triggerPresenceChanged, Call(_)).Times(0);
277 
278     EXPECT_THAT(setProperty(sut->getPath(), "Reports", newPropertyVal),
279                 Eq(boost::system::errc::invalid_argument));
280 }
281 
TEST_F(TestTrigger,settingPropertyReportNamesThrowsExceptionWhenReportWithBadPath)282 TEST_F(TestTrigger,
283        settingPropertyReportNamesThrowsExceptionWhenReportWithBadPath)
284 {
285     std::vector<object_path> newPropertyVal{
286         object_path("/xyz/openbmc_project/Telemetry/NotReports/MyReport")};
287 
288     EXPECT_CALL(triggerPresenceChanged, Call(_)).Times(0);
289 
290     EXPECT_THAT(setProperty(sut->getPath(), "Reports", newPropertyVal),
291                 Eq(boost::system::errc::invalid_argument));
292 }
293 
TEST_F(TestTrigger,setPropertySensors)294 TEST_F(TestTrigger, setPropertySensors)
295 {
296     EXPECT_CALL(*triggerFactoryMockPtr, updateSensors(_, _));
297     for (const auto& threshold : thresholdMocks)
298     {
299         auto thresholdMockPtr =
300             std::dynamic_pointer_cast<NiceMock<ThresholdMock>>(threshold);
301         EXPECT_CALL(*thresholdMockPtr, updateSensors(_));
302     }
303     SensorsInfo newSensors(
304         {std::make_pair(object_path("/abc/def"), "metadata")});
305     EXPECT_THAT(setProperty(sut->getPath(), "Sensors", newSensors),
306                 Eq(boost::system::errc::success));
307 }
308 
TEST_F(TestTrigger,setPropertyThresholds)309 TEST_F(TestTrigger, setPropertyThresholds)
310 {
311     EXPECT_CALL(*triggerFactoryMockPtr, updateThresholds(_, _, _, _, _, _));
312     TriggerThresholdParams newThresholds =
313         std::vector<discrete::ThresholdParam>({std::make_tuple(
314             "discrete threshold", utils::enumToString(discrete::Severity::ok),
315             10, "12.3")});
316     EXPECT_THAT(setProperty(sut->getPath(), "Thresholds", newThresholds),
317                 Eq(boost::system::errc::success));
318 }
319 
TEST_F(TestTrigger,setThresholdParamsWithTooLongDiscreteName)320 TEST_F(TestTrigger, setThresholdParamsWithTooLongDiscreteName)
321 {
322     const TriggerThresholdParams currentValue =
323         std::visit(utils::FromLabeledThresholdParamConversion(),
324                    triggerParams.thresholdParams());
325 
326     TriggerThresholdParams newThresholds =
327         std::vector<discrete::ThresholdParam>({std::make_tuple(
328             utils::string_utils::getTooLongName(),
329             utils::enumToString(discrete::Severity::ok), 10, "12.3")});
330 
331     changeProperty<TriggerThresholdParams>(
332         sut->getPath(), "Thresholds",
333         {.valueBefore = Eq(currentValue),
334          .newValue = newThresholds,
335          .ec = Eq(boost::system::errc::invalid_argument),
336          .valueAfter = Eq(currentValue)});
337 }
338 
TEST_F(TestTrigger,setNameTooLong)339 TEST_F(TestTrigger, setNameTooLong)
340 {
341     std::string currentValue = TriggerParams().name();
342 
343     changeProperty<std::string>(
344         sut->getPath(), "Name",
345         {.valueBefore = Eq(currentValue),
346          .newValue = utils::string_utils::getTooLongName(),
347          .ec = Eq(boost::system::errc::invalid_argument),
348          .valueAfter = Eq(currentValue)});
349 }
350 
TEST_F(TestTrigger,checkIfNumericCoversionsAreGood)351 TEST_F(TestTrigger, checkIfNumericCoversionsAreGood)
352 {
353     const auto& labeledParamsBase =
354         std::get<std::vector<numeric::LabeledThresholdParam>>(
355             triggerParams.thresholdParams());
356     const auto paramsToCheck =
357         std::visit(utils::FromLabeledThresholdParamConversion(),
358                    triggerParams.thresholdParams());
359     const auto labeledParamsToCheck =
360         std::get<std::vector<numeric::LabeledThresholdParam>>(std::visit(
361             utils::ToLabeledThresholdParamConversion(), paramsToCheck));
362 
363     for (const auto& [tocheck, base] :
364          boost::combine(labeledParamsToCheck, labeledParamsBase))
365     {
366         EXPECT_THAT(tocheck.at_label<utils::tstring::Type>(),
367                     Eq(base.at_label<utils::tstring::Type>()));
368         EXPECT_THAT(tocheck.at_label<utils::tstring::Direction>(),
369                     Eq(base.at_label<utils::tstring::Direction>()));
370         EXPECT_THAT(tocheck.at_label<utils::tstring::DwellTime>(),
371                     Eq(base.at_label<utils::tstring::DwellTime>()));
372         EXPECT_THAT(tocheck.at_label<utils::tstring::ThresholdValue>(),
373                     Eq(base.at_label<utils::tstring::ThresholdValue>()));
374     }
375 }
376 
TEST_F(TestTrigger,checkIfDiscreteCoversionsAreGood)377 TEST_F(TestTrigger, checkIfDiscreteCoversionsAreGood)
378 {
379     const auto& labeledParamsBase =
380         std::get<std::vector<discrete::LabeledThresholdParam>>(
381             triggerDiscreteParams.thresholdParams());
382     const auto paramsToCheck =
383         std::visit(utils::FromLabeledThresholdParamConversion(),
384                    triggerDiscreteParams.thresholdParams());
385     const auto labeledParamsToCheck =
386         std::get<std::vector<discrete::LabeledThresholdParam>>(std::visit(
387             utils::ToLabeledThresholdParamConversion(), paramsToCheck));
388 
389     for (const auto& [tocheck, base] :
390          boost::combine(labeledParamsToCheck, labeledParamsBase))
391     {
392         EXPECT_THAT(tocheck.at_label<utils::tstring::UserId>(),
393                     Eq(base.at_label<utils::tstring::UserId>()));
394         EXPECT_THAT(tocheck.at_label<utils::tstring::Severity>(),
395                     Eq(base.at_label<utils::tstring::Severity>()));
396         EXPECT_THAT(tocheck.at_label<utils::tstring::DwellTime>(),
397                     Eq(base.at_label<utils::tstring::DwellTime>()));
398         EXPECT_THAT(tocheck.at_label<utils::tstring::ThresholdValue>(),
399                     Eq(base.at_label<utils::tstring::ThresholdValue>()));
400     }
401 }
402 
TEST_F(TestTrigger,deleteTrigger)403 TEST_F(TestTrigger, deleteTrigger)
404 {
405     EXPECT_CALL(storageMock, remove(to_file_path(sut->getId())));
406     EXPECT_CALL(*triggerManagerMockPtr, removeTrigger(sut.get()));
407 
408     auto ec = deleteTrigger(sut->getPath());
409     EXPECT_THAT(ec, Eq(boost::system::errc::success));
410 }
411 
TEST_F(TestTrigger,sendUpdateWhenTriggerIsDeleted)412 TEST_F(TestTrigger, sendUpdateWhenTriggerIsDeleted)
413 {
414     EXPECT_CALL(triggerPresenceChanged,
415                 Call(FieldsAre(messages::Presence::Removed, triggerParams.id(),
416                                UnorderedElementsAre())));
417 
418     auto ec = deleteTrigger(sut->getPath());
419     EXPECT_THAT(ec, Eq(boost::system::errc::success));
420 }
421 
TEST_F(TestTrigger,deletingNonExistingTriggerReturnInvalidRequestDescriptor)422 TEST_F(TestTrigger, deletingNonExistingTriggerReturnInvalidRequestDescriptor)
423 {
424     auto ec =
425         deleteTrigger(utils::constants::triggerDirPath.str + "NonExisting"s);
426     EXPECT_THAT(ec.value(), Eq(EBADR));
427 }
428 
TEST_F(TestTrigger,settingPersistencyToFalseRemovesTriggerFromStorage)429 TEST_F(TestTrigger, settingPersistencyToFalseRemovesTriggerFromStorage)
430 {
431     EXPECT_CALL(storageMock, remove(to_file_path(sut->getId())));
432 
433     bool persistent = false;
434     EXPECT_THAT(setProperty(sut->getPath(), "Persistent", persistent),
435                 Eq(boost::system::errc::success));
436     EXPECT_THAT(getProperty<bool>(sut->getPath(), "Persistent"),
437                 Eq(persistent));
438 }
439 
440 class TestTriggerInitialization : public TestTrigger
441 {
442   public:
SetUp()443     void SetUp() override {}
444 
445     nlohmann::json storedConfiguration;
446 };
447 
TEST_F(TestTriggerInitialization,exceptionDuringTriggerStoreDisablesPersistency)448 TEST_F(TestTriggerInitialization,
449        exceptionDuringTriggerStoreDisablesPersistency)
450 {
451     EXPECT_CALL(storageMock, store(_, _))
452         .WillOnce(Throw(std::runtime_error("Generic error!")));
453 
454     sut = makeTrigger(triggerParams);
455 
456     EXPECT_THAT(getProperty<bool>(sut->getPath(), "Persistent"), Eq(false));
457 }
458 
TEST_F(TestTriggerInitialization,creatingTriggerThrowsExceptionWhenIdIsInvalid)459 TEST_F(TestTriggerInitialization, creatingTriggerThrowsExceptionWhenIdIsInvalid)
460 {
461     EXPECT_CALL(storageMock, store(_, _)).Times(0);
462 
463     EXPECT_THROW(makeTrigger(triggerParams.id("inv?lidId")),
464                  sdbusplus::exception::SdBusError);
465 }
466 
TEST_F(TestTriggerInitialization,creatingTriggerUpdatesTriggersIdsInReports)467 TEST_F(TestTriggerInitialization, creatingTriggerUpdatesTriggersIdsInReports)
468 {
469     EXPECT_CALL(
470         triggerPresenceChanged,
471         Call(FieldsAre(messages::Presence::Exist, triggerParams.id(),
472                        UnorderedElementsAreArray(triggerParams.reportIds()))));
473 
474     sut = makeTrigger(triggerParams);
475 }
476 
477 class TestTriggerStore : public TestTrigger
478 {
479   public:
480     nlohmann::json storedConfiguration;
481     nlohmann::json storedDiscreteConfiguration;
482     std::unique_ptr<Trigger> sutDiscrete;
483 
SetUp()484     void SetUp() override
485     {
486         ON_CALL(storageMock, store(_, _))
487             .WillByDefault(SaveArg<1>(&storedConfiguration));
488         sut = makeTrigger(triggerParams);
489 
490         ON_CALL(storageMock, store(_, _))
491             .WillByDefault(SaveArg<1>(&storedDiscreteConfiguration));
492         sutDiscrete = makeTrigger(triggerDiscreteParams);
493     }
494 };
495 
TEST_F(TestTriggerStore,settingPersistencyToTrueStoresTriggerVersion)496 TEST_F(TestTriggerStore, settingPersistencyToTrueStoresTriggerVersion)
497 {
498     ASSERT_THAT(storedConfiguration.at("Version"), Eq(expectedTriggerVersion));
499 }
500 
TEST_F(TestTriggerStore,settingPersistencyToTrueStoresTriggerId)501 TEST_F(TestTriggerStore, settingPersistencyToTrueStoresTriggerId)
502 {
503     ASSERT_THAT(storedConfiguration.at("Id"), Eq(triggerParams.id()));
504 }
505 
TEST_F(TestTriggerStore,settingPersistencyToTrueStoresTriggerName)506 TEST_F(TestTriggerStore, settingPersistencyToTrueStoresTriggerName)
507 {
508     ASSERT_THAT(storedConfiguration.at("Name"), Eq(triggerParams.name()));
509 }
510 
TEST_F(TestTriggerStore,settingPersistencyToTrueStoresTriggerTriggerActions)511 TEST_F(TestTriggerStore, settingPersistencyToTrueStoresTriggerTriggerActions)
512 {
513     ASSERT_THAT(storedConfiguration.at("TriggerActions"),
514                 Eq(utils::transform(triggerParams.triggerActions(),
515                                     [](const auto& action) {
516         return actionToString(action);
517     })));
518 }
519 
TEST_F(TestTriggerStore,settingPersistencyToTrueStoresTriggerReportIds)520 TEST_F(TestTriggerStore, settingPersistencyToTrueStoresTriggerReportIds)
521 {
522     ASSERT_THAT(storedConfiguration.at("ReportIds"),
523                 Eq(triggerParams.reportIds()));
524 }
525 
TEST_F(TestTriggerStore,settingPersistencyToTrueStoresTriggerSensors)526 TEST_F(TestTriggerStore, settingPersistencyToTrueStoresTriggerSensors)
527 {
528     nlohmann::json expectedItem;
529     expectedItem["service"] = "service1";
530     expectedItem["path"] = "/xyz/openbmc_project/sensors/temperature/BMC_Temp";
531     expectedItem["metadata"] = "metadata1";
532 
533     ASSERT_THAT(storedConfiguration.at("Sensors"), ElementsAre(expectedItem));
534 }
535 
TEST_F(TestTriggerStore,settingPersistencyToTrueStoresTriggerThresholdParams)536 TEST_F(TestTriggerStore, settingPersistencyToTrueStoresTriggerThresholdParams)
537 {
538     nlohmann::json expectedItem0;
539     expectedItem0["type"] = 0;
540     expectedItem0["dwellTime"] = 10;
541     expectedItem0["direction"] = 1;
542     expectedItem0["thresholdValue"] = 0.5;
543 
544     nlohmann::json expectedItem1;
545     expectedItem1["type"] = 3;
546     expectedItem1["dwellTime"] = 10;
547     expectedItem1["direction"] = 2;
548     expectedItem1["thresholdValue"] = 90.2;
549 
550     ASSERT_THAT(storedConfiguration.at("ThresholdParamsDiscriminator"), Eq(0));
551     ASSERT_THAT(storedConfiguration.at("ThresholdParams"),
552                 ElementsAre(expectedItem0, expectedItem1));
553 }
554 
TEST_F(TestTriggerStore,settingPersistencyToTrueStoresDiscreteTriggerThresholdParams)555 TEST_F(TestTriggerStore,
556        settingPersistencyToTrueStoresDiscreteTriggerThresholdParams)
557 {
558     nlohmann::json expectedItem0;
559     expectedItem0["userId"] = "userId";
560     expectedItem0["severity"] = discrete::Severity::warning;
561     expectedItem0["dwellTime"] = 10;
562     expectedItem0["thresholdValue"] = "15.2";
563 
564     nlohmann::json expectedItem1;
565     expectedItem1["userId"] = "userId_2";
566     expectedItem1["severity"] = discrete::Severity::critical;
567     expectedItem1["dwellTime"] = 5;
568     expectedItem1["thresholdValue"] = "32.7";
569 
570     ASSERT_THAT(storedDiscreteConfiguration.at("ThresholdParamsDiscriminator"),
571                 Eq(1));
572     ASSERT_THAT(storedDiscreteConfiguration.at("ThresholdParams"),
573                 ElementsAre(expectedItem0, expectedItem1));
574 }
575