blob: 5c41d97e6c06d5adcf119e6f3a9e482c65b2e9f6 [file] [log] [blame]
Wludzik, Jozef76833cb2020-12-21 14:42:41 +01001#include "dbus_environment.hpp"
2#include "helpers.hpp"
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +01003#include "messages/collect_trigger_id.hpp"
4#include "messages/trigger_presence_changed_ind.hpp"
Cezary Zwolaka4e67612021-02-18 13:16:16 +01005#include "mocks/json_storage_mock.hpp"
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +01006#include "mocks/report_manager_mock.hpp"
Szymon Dompke94f71c52021-12-10 07:16:33 +01007#include "mocks/sensor_mock.hpp"
8#include "mocks/threshold_mock.hpp"
9#include "mocks/trigger_factory_mock.hpp"
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010010#include "mocks/trigger_manager_mock.hpp"
11#include "params/trigger_params.hpp"
12#include "trigger.hpp"
Szymon Dompkee28aa532021-10-27 12:33:12 +020013#include "trigger_manager.hpp"
Cezary Zwolak4416fce2021-03-17 03:21:06 +010014#include "utils/conversion_trigger.hpp"
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +010015#include "utils/messanger.hpp"
Cezary Zwolak4416fce2021-03-17 03:21:06 +010016#include "utils/transform.hpp"
17#include "utils/tstring.hpp"
18
19#include <boost/range/combine.hpp>
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010020
21using namespace testing;
22using namespace std::literals::string_literals;
Szymon Dompke1cdd7e42022-06-08 14:43:13 +020023using sdbusplus::message::object_path;
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010024
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020025static constexpr size_t expectedTriggerVersion = 2;
Cezary Zwolaka4e67612021-02-18 13:16:16 +010026
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010027class TestTrigger : public Test
28{
29 public:
30 TriggerParams triggerParams;
Cezary Zwolak4416fce2021-03-17 03:21:06 +010031 TriggerParams triggerDiscreteParams =
32 TriggerParams()
Szymon Dompkee28aa532021-10-27 12:33:12 +020033 .id("DiscreteTrigger")
34 .name("My Discrete Trigger")
Cezary Zwolak4416fce2021-03-17 03:21:06 +010035 .thresholdParams(std::vector<discrete::LabeledThresholdParam>{
36 discrete::LabeledThresholdParam{
37 "userId", discrete::Severity::warning,
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000038 Milliseconds(10).count(), "15.2"},
Cezary Zwolak4416fce2021-03-17 03:21:06 +010039 discrete::LabeledThresholdParam{
40 "userId_2", discrete::Severity::critical,
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000041 Milliseconds(5).count(), "32.7"},
Cezary Zwolak4416fce2021-03-17 03:21:06 +010042 });
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010043
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +010044 std::unique_ptr<ReportManagerMock> reportManagerMockPtr =
45 std::make_unique<NiceMock<ReportManagerMock>>();
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010046 std::unique_ptr<TriggerManagerMock> triggerManagerMockPtr =
47 std::make_unique<NiceMock<TriggerManagerMock>>();
Szymon Dompke94f71c52021-12-10 07:16:33 +010048 std::unique_ptr<TriggerFactoryMock> triggerFactoryMockPtr =
49 std::make_unique<NiceMock<TriggerFactoryMock>>();
Cezary Zwolaka4e67612021-02-18 13:16:16 +010050 testing::NiceMock<StorageMock> storageMock;
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +010051 NiceMock<MockFunction<void(const messages::TriggerPresenceChangedInd)>>
52 triggerPresenceChanged;
Szymon Dompke94f71c52021-12-10 07:16:33 +010053 std::vector<std::shared_ptr<interfaces::Threshold>> thresholdMocks;
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +010054 utils::Messanger messanger;
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010055 std::unique_ptr<Trigger> sut;
56
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +010057 TestTrigger() : messanger(DbusEnvironment::getIoc())
58 {
59 messanger.on_receive<messages::TriggerPresenceChangedInd>(
60 [this](const auto& msg) { triggerPresenceChanged.Call(msg); });
61 }
62
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010063 void SetUp() override
64 {
Cezary Zwolaka4e67612021-02-18 13:16:16 +010065 sut = makeTrigger(triggerParams);
66 }
67
Cezary Zwolak4416fce2021-03-17 03:21:06 +010068 static std::vector<LabeledSensorInfo>
69 convertToLabeledSensor(const SensorsInfo& sensorsInfo)
70 {
71 return utils::transform(sensorsInfo, [](const auto& sensorInfo) {
72 const auto& [sensorPath, sensorMetadata] = sensorInfo;
73 return LabeledSensorInfo("service1", sensorPath, sensorMetadata);
74 });
75 }
76
Cezary Zwolaka4e67612021-02-18 13:16:16 +010077 std::unique_ptr<Trigger> makeTrigger(const TriggerParams& params)
78 {
Szymon Dompke94f71c52021-12-10 07:16:33 +010079 thresholdMocks =
80 ThresholdMock::makeThresholds(params.thresholdParams());
81
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020082 auto id = std::make_unique<const std::string>(params.id());
83
Cezary Zwolaka4e67612021-02-18 13:16:16 +010084 return std::make_unique<Trigger>(
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010085 DbusEnvironment::getIoc(), DbusEnvironment::getObjServer(),
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020086 std::move(id), params.name(), params.triggerActions(),
Szymon Dompke94f71c52021-12-10 07:16:33 +010087 std::make_shared<std::vector<std::string>>(
88 params.reportIds().begin(), params.reportIds().end()),
89 std::vector<std::shared_ptr<interfaces::Threshold>>(thresholdMocks),
90 *triggerManagerMockPtr, storageMock, *triggerFactoryMockPtr,
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +010091 SensorMock::makeSensorMocks(params.sensors()));
Cezary Zwolaka4e67612021-02-18 13:16:16 +010092 }
93
94 static interfaces::JsonStorage::FilePath to_file_path(std::string name)
95 {
96 return interfaces::JsonStorage::FilePath(
97 std::to_string(std::hash<std::string>{}(name)));
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010098 }
99
100 template <class T>
101 static T getProperty(const std::string& path, const std::string& property)
102 {
Szymon Dompkee28aa532021-10-27 12:33:12 +0200103 return DbusEnvironment::getProperty<T>(path, Trigger::triggerIfaceName,
104 property);
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100105 }
106
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100107 template <class T>
108 static boost::system::error_code setProperty(const std::string& path,
109 const std::string& property,
110 const T& newValue)
111 {
Szymon Dompkee28aa532021-10-27 12:33:12 +0200112 return DbusEnvironment::setProperty<T>(path, Trigger::triggerIfaceName,
113 property, newValue);
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100114 }
115
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100116 boost::system::error_code deleteTrigger(const std::string& path)
117 {
118 std::promise<boost::system::error_code> methodPromise;
119 DbusEnvironment::getBus()->async_method_call(
120 [&methodPromise](boost::system::error_code ec) {
121 methodPromise.set_value(ec);
122 },
123 DbusEnvironment::serviceName(), path, Trigger::deleteIfaceName,
124 "Delete");
125 return DbusEnvironment::waitForFuture(methodPromise.get_future());
126 }
127};
128
129TEST_F(TestTrigger, checkIfPropertiesAreSet)
130{
Szymon Dompkee28aa532021-10-27 12:33:12 +0200131 EXPECT_THAT(getProperty<std::string>(sut->getPath(), "Name"),
132 Eq(triggerParams.name()));
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100133 EXPECT_THAT(getProperty<bool>(sut->getPath(), "Persistent"), Eq(true));
Szymon Dompke20013012021-07-23 09:54:20 +0200134 EXPECT_THAT(
135 getProperty<std::vector<std::string>>(sut->getPath(), "TriggerActions"),
Szymon Dompke94f71c52021-12-10 07:16:33 +0100136 Eq(utils::transform(
137 triggerParams.triggerActions(),
138 [](const auto& action) { return actionToString(action); })));
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100139 EXPECT_THAT((getProperty<SensorsInfo>(sut->getPath(), "Sensors")),
140 Eq(utils::fromLabeledSensorsInfo(triggerParams.sensors())));
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100141 EXPECT_THAT(
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200142 getProperty<std::vector<object_path>>(sut->getPath(), "Reports"),
143 Eq(triggerParams.reports()));
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100144 EXPECT_THAT(
Szymon Dompke94f71c52021-12-10 07:16:33 +0100145 getProperty<bool>(sut->getPath(), "Discrete"),
146 Eq(isTriggerThresholdDiscrete(triggerParams.thresholdParams())));
147 EXPECT_THAT(
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100148 getProperty<TriggerThresholdParams>(sut->getPath(), "Thresholds"),
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100149 Eq(std::visit(utils::FromLabeledThresholdParamConversion(),
150 triggerParams.thresholdParams())));
151}
152
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100153TEST_F(TestTrigger, checkBasicGetters)
154{
155 EXPECT_THAT(sut->getId(), Eq(triggerParams.id()));
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200156 EXPECT_THAT(sut->getPath(),
157 Eq(utils::constants::triggerDirPath.str + triggerParams.id()));
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100158}
159
Szymon Dompkee28aa532021-10-27 12:33:12 +0200160TEST_F(TestTrigger, setPropertyNameToCorrectValue)
161{
162 std::string name = "custom name 1234 %^#5";
163 EXPECT_THAT(setProperty(sut->getPath(), "Name", name),
164 Eq(boost::system::errc::success));
165 EXPECT_THAT(getProperty<std::string>(sut->getPath(), "Name"), Eq(name));
166}
167
Szymon Dompke94f71c52021-12-10 07:16:33 +0100168TEST_F(TestTrigger, setPropertyReportNames)
169{
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200170 std::vector<object_path> newNames = {
171 utils::constants::reportDirPath / "abc",
172 utils::constants::reportDirPath / "one",
173 utils::constants::reportDirPath / "prefix" / "two"};
174 EXPECT_THAT(setProperty(sut->getPath(), "Reports", newNames),
Szymon Dompke94f71c52021-12-10 07:16:33 +0100175 Eq(boost::system::errc::success));
176 EXPECT_THAT(
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200177 getProperty<std::vector<object_path>>(sut->getPath(), "Reports"),
Szymon Dompke94f71c52021-12-10 07:16:33 +0100178 Eq(newNames));
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100179}
180
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100181TEST_F(TestTrigger, sendsUpdateWhenReportNamesChanges)
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100182{
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200183 std::vector<object_path> newPropertyVal = {
184 utils::constants::reportDirPath / "abc",
185 utils::constants::reportDirPath / "one",
186 utils::constants::reportDirPath / "two"};
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100187
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100188 EXPECT_CALL(triggerPresenceChanged,
189 Call(FieldsAre(messages::Presence::Exist, triggerParams.id(),
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200190 UnorderedElementsAre("abc", "one", "two"))));
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100191
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200192 EXPECT_THAT(setProperty(sut->getPath(), "Reports", newPropertyVal),
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100193 Eq(boost::system::errc::success));
194}
195
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100196TEST_F(TestTrigger, sendsUpdateWhenReportNamesChangesToSameValue)
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100197{
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200198 const std::vector<object_path> newPropertyVal = triggerParams.reports();
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100199
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100200 EXPECT_CALL(
201 triggerPresenceChanged,
202 Call(FieldsAre(messages::Presence::Exist, triggerParams.id(),
203 UnorderedElementsAreArray(triggerParams.reportIds()))));
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100204
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200205 EXPECT_THAT(setProperty(sut->getPath(), "Reports", newPropertyVal),
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100206 Eq(boost::system::errc::success));
207}
208
209TEST_F(TestTrigger,
210 DISABLED_settingPropertyReportNamesThrowsExceptionWhenDuplicateReportIds)
211{
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200212 std::vector<object_path> newPropertyVal{
213 utils::constants::reportDirPath / "report1",
214 utils::constants::reportDirPath / "report2",
215 utils::constants::reportDirPath / "report1"};
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100216
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100217 EXPECT_CALL(triggerPresenceChanged, Call(_)).Times(0);
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100218
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200219 EXPECT_THAT(setProperty(sut->getPath(), "Reports", newPropertyVal),
220 Eq(boost::system::errc::invalid_argument));
221}
222
223TEST_F(
224 TestTrigger,
225 DISABLED_settingPropertyReportNamesThrowsExceptionWhenReportWithTooManyPrefixes)
226{
227 std::vector<object_path> newPropertyVal{
228 object_path("/xyz/openbmc_project/Telemetry/Reports/P1/P2/MyReport")};
229
230 EXPECT_CALL(triggerPresenceChanged, Call(_)).Times(0);
231
232 EXPECT_THAT(setProperty(sut->getPath(), "Reports", newPropertyVal),
233 Eq(boost::system::errc::invalid_argument));
234}
235
236TEST_F(TestTrigger,
237 DISABLED_settingPropertyReportNamesThrowsExceptionWhenReportWithBadPath)
238{
239 std::vector<object_path> newPropertyVal{
240 object_path("/xyz/openbmc_project/Telemetry/NotReports/MyReport")};
241
242 EXPECT_CALL(triggerPresenceChanged, Call(_)).Times(0);
243
244 EXPECT_THAT(setProperty(sut->getPath(), "Reports", newPropertyVal),
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100245 Eq(boost::system::errc::invalid_argument));
Szymon Dompke94f71c52021-12-10 07:16:33 +0100246}
247
248TEST_F(TestTrigger, setPropertySensors)
249{
250 EXPECT_CALL(*triggerFactoryMockPtr, updateSensors(_, _));
251 for (const auto& threshold : thresholdMocks)
252 {
253 auto thresholdMockPtr =
254 std::dynamic_pointer_cast<NiceMock<ThresholdMock>>(threshold);
255 EXPECT_CALL(*thresholdMockPtr, updateSensors(_));
256 }
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200257 SensorsInfo newSensors(
258 {std::make_pair(object_path("/abc/def"), "metadata")});
Szymon Dompke94f71c52021-12-10 07:16:33 +0100259 EXPECT_THAT(setProperty(sut->getPath(), "Sensors", newSensors),
260 Eq(boost::system::errc::success));
261}
262
263TEST_F(TestTrigger, setPropertyThresholds)
264{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200265 EXPECT_CALL(*triggerFactoryMockPtr, updateThresholds(_, _, _, _, _, _));
Szymon Dompke94f71c52021-12-10 07:16:33 +0100266 TriggerThresholdParams newThresholds =
267 std::vector<discrete::ThresholdParam>(
268 {std::make_tuple("discrete threshold", "OK", 10, "12.3")});
269 EXPECT_THAT(setProperty(sut->getPath(), "Thresholds", newThresholds),
270 Eq(boost::system::errc::success));
271}
272
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100273TEST_F(TestTrigger, checkIfNumericCoversionsAreGood)
274{
275 const auto& labeledParamsBase =
276 std::get<std::vector<numeric::LabeledThresholdParam>>(
277 triggerParams.thresholdParams());
278 const auto paramsToCheck =
279 std::visit(utils::FromLabeledThresholdParamConversion(),
280 triggerParams.thresholdParams());
281 const auto labeledParamsToCheck =
282 std::get<std::vector<numeric::LabeledThresholdParam>>(std::visit(
283 utils::ToLabeledThresholdParamConversion(), paramsToCheck));
284
285 for (const auto& [tocheck, base] :
286 boost::combine(labeledParamsToCheck, labeledParamsBase))
287 {
288 EXPECT_THAT(tocheck.at_label<utils::tstring::Type>(),
289 Eq(base.at_label<utils::tstring::Type>()));
290 EXPECT_THAT(tocheck.at_label<utils::tstring::Direction>(),
291 Eq(base.at_label<utils::tstring::Direction>()));
292 EXPECT_THAT(tocheck.at_label<utils::tstring::DwellTime>(),
293 Eq(base.at_label<utils::tstring::DwellTime>()));
294 EXPECT_THAT(tocheck.at_label<utils::tstring::ThresholdValue>(),
295 Eq(base.at_label<utils::tstring::ThresholdValue>()));
296 }
297}
298
299TEST_F(TestTrigger, checkIfDiscreteCoversionsAreGood)
300{
301 const auto& labeledParamsBase =
302 std::get<std::vector<discrete::LabeledThresholdParam>>(
303 triggerDiscreteParams.thresholdParams());
304 const auto paramsToCheck =
305 std::visit(utils::FromLabeledThresholdParamConversion(),
306 triggerDiscreteParams.thresholdParams());
307 const auto labeledParamsToCheck =
308 std::get<std::vector<discrete::LabeledThresholdParam>>(std::visit(
309 utils::ToLabeledThresholdParamConversion(), paramsToCheck));
310
311 for (const auto& [tocheck, base] :
312 boost::combine(labeledParamsToCheck, labeledParamsBase))
313 {
314 EXPECT_THAT(tocheck.at_label<utils::tstring::UserId>(),
315 Eq(base.at_label<utils::tstring::UserId>()));
316 EXPECT_THAT(tocheck.at_label<utils::tstring::Severity>(),
317 Eq(base.at_label<utils::tstring::Severity>()));
318 EXPECT_THAT(tocheck.at_label<utils::tstring::DwellTime>(),
319 Eq(base.at_label<utils::tstring::DwellTime>()));
320 EXPECT_THAT(tocheck.at_label<utils::tstring::ThresholdValue>(),
321 Eq(base.at_label<utils::tstring::ThresholdValue>()));
322 }
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100323}
324
325TEST_F(TestTrigger, deleteTrigger)
326{
Szymon Dompkee28aa532021-10-27 12:33:12 +0200327 EXPECT_CALL(storageMock, remove(to_file_path(sut->getId())));
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100328 EXPECT_CALL(*triggerManagerMockPtr, removeTrigger(sut.get()));
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100329
330 auto ec = deleteTrigger(sut->getPath());
331 EXPECT_THAT(ec, Eq(boost::system::errc::success));
332}
333
334TEST_F(TestTrigger, sendUpdateWhenTriggerIsDeleted)
335{
336 EXPECT_CALL(triggerPresenceChanged,
337 Call(FieldsAre(messages::Presence::Removed, triggerParams.id(),
338 UnorderedElementsAre())));
339
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100340 auto ec = deleteTrigger(sut->getPath());
341 EXPECT_THAT(ec, Eq(boost::system::errc::success));
342}
343
344TEST_F(TestTrigger, deletingNonExistingTriggerReturnInvalidRequestDescriptor)
345{
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200346 auto ec =
347 deleteTrigger(utils::constants::triggerDirPath.str + "NonExisting"s);
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100348 EXPECT_THAT(ec.value(), Eq(EBADR));
349}
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100350
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100351TEST_F(TestTrigger, settingPersistencyToFalseRemovesTriggerFromStorage)
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100352{
Szymon Dompkee28aa532021-10-27 12:33:12 +0200353 EXPECT_CALL(storageMock, remove(to_file_path(sut->getId())));
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100354
355 bool persistent = false;
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100356 EXPECT_THAT(setProperty(sut->getPath(), "Persistent", persistent),
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100357 Eq(boost::system::errc::success));
358 EXPECT_THAT(getProperty<bool>(sut->getPath(), "Persistent"),
359 Eq(persistent));
360}
361
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100362class TestTriggerInitialization : public TestTrigger
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100363{
364 public:
365 void SetUp() override
366 {}
367
368 nlohmann::json storedConfiguration;
369};
370
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100371TEST_F(TestTriggerInitialization,
372 exceptionDuringTriggerStoreDisablesPersistency)
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100373{
374 EXPECT_CALL(storageMock, store(_, _))
375 .WillOnce(Throw(std::runtime_error("Generic error!")));
376
377 sut = makeTrigger(triggerParams);
378
379 EXPECT_THAT(getProperty<bool>(sut->getPath(), "Persistent"), Eq(false));
380}
381
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100382TEST_F(TestTriggerInitialization, creatingTriggerThrowsExceptionWhenIdIsInvalid)
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100383{
384 EXPECT_CALL(storageMock, store(_, _)).Times(0);
385
Szymon Dompkee28aa532021-10-27 12:33:12 +0200386 EXPECT_THROW(makeTrigger(triggerParams.id("inv?lidId")),
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100387 sdbusplus::exception::SdBusError);
388}
389
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100390TEST_F(TestTriggerInitialization, creatingTriggerUpdatesTriggersIdsInReports)
391{
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100392 EXPECT_CALL(
393 triggerPresenceChanged,
394 Call(FieldsAre(messages::Presence::Exist, triggerParams.id(),
395 UnorderedElementsAreArray(triggerParams.reportIds()))));
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100396
397 sut = makeTrigger(triggerParams);
398}
399
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100400class TestTriggerStore : public TestTrigger
401{
402 public:
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100403 nlohmann::json storedConfiguration;
404 nlohmann::json storedDiscreteConfiguration;
405 std::unique_ptr<Trigger> sutDiscrete;
406
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100407 void SetUp() override
408 {
409 ON_CALL(storageMock, store(_, _))
410 .WillByDefault(SaveArg<1>(&storedConfiguration));
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100411 sut = makeTrigger(triggerParams);
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100412
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100413 ON_CALL(storageMock, store(_, _))
414 .WillByDefault(SaveArg<1>(&storedDiscreteConfiguration));
415 sutDiscrete = makeTrigger(triggerDiscreteParams);
416 }
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100417};
418
419TEST_F(TestTriggerStore, settingPersistencyToTrueStoresTriggerVersion)
420{
421 ASSERT_THAT(storedConfiguration.at("Version"), Eq(expectedTriggerVersion));
422}
423
Szymon Dompkee28aa532021-10-27 12:33:12 +0200424TEST_F(TestTriggerStore, settingPersistencyToTrueStoresTriggerId)
425{
426 ASSERT_THAT(storedConfiguration.at("Id"), Eq(triggerParams.id()));
427}
428
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100429TEST_F(TestTriggerStore, settingPersistencyToTrueStoresTriggerName)
430{
431 ASSERT_THAT(storedConfiguration.at("Name"), Eq(triggerParams.name()));
432}
433
Szymon Dompke20013012021-07-23 09:54:20 +0200434TEST_F(TestTriggerStore, settingPersistencyToTrueStoresTriggerTriggerActions)
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100435{
Szymon Dompke20013012021-07-23 09:54:20 +0200436 ASSERT_THAT(storedConfiguration.at("TriggerActions"),
Szymon Dompke94f71c52021-12-10 07:16:33 +0100437 Eq(utils::transform(triggerParams.triggerActions(),
438 [](const auto& action) {
439 return actionToString(action);
440 })));
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100441}
442
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100443TEST_F(TestTriggerStore, settingPersistencyToTrueStoresTriggerReportIds)
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100444{
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100445 ASSERT_THAT(storedConfiguration.at("ReportIds"),
446 Eq(triggerParams.reportIds()));
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100447}
448
449TEST_F(TestTriggerStore, settingPersistencyToTrueStoresTriggerSensors)
450{
451 nlohmann::json expectedItem;
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100452 expectedItem["service"] = "service1";
Szymon Dompke94f71c52021-12-10 07:16:33 +0100453 expectedItem["path"] = "/xyz/openbmc_project/sensors/temperature/BMC_Temp";
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100454 expectedItem["metadata"] = "metadata1";
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100455
456 ASSERT_THAT(storedConfiguration.at("Sensors"), ElementsAre(expectedItem));
457}
458
459TEST_F(TestTriggerStore, settingPersistencyToTrueStoresTriggerThresholdParams)
460{
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100461 nlohmann::json expectedItem0;
462 expectedItem0["type"] = 0;
463 expectedItem0["dwellTime"] = 10;
464 expectedItem0["direction"] = 1;
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100465 expectedItem0["thresholdValue"] = 0.5;
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100466
467 nlohmann::json expectedItem1;
468 expectedItem1["type"] = 3;
469 expectedItem1["dwellTime"] = 10;
470 expectedItem1["direction"] = 2;
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100471 expectedItem1["thresholdValue"] = 90.2;
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100472
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100473 ASSERT_THAT(storedConfiguration.at("ThresholdParamsDiscriminator"), Eq(0));
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100474 ASSERT_THAT(storedConfiguration.at("ThresholdParams"),
475 ElementsAre(expectedItem0, expectedItem1));
476}
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100477
478TEST_F(TestTriggerStore,
479 settingPersistencyToTrueStoresDiscreteTriggerThresholdParams)
480{
481 nlohmann::json expectedItem0;
482 expectedItem0["userId"] = "userId";
483 expectedItem0["severity"] = discrete::Severity::warning;
484 expectedItem0["dwellTime"] = 10;
Szymon Dompke9f346792021-07-14 21:07:11 +0200485 expectedItem0["thresholdValue"] = "15.2";
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100486
487 nlohmann::json expectedItem1;
488 expectedItem1["userId"] = "userId_2";
489 expectedItem1["severity"] = discrete::Severity::critical;
490 expectedItem1["dwellTime"] = 5;
Szymon Dompke9f346792021-07-14 21:07:11 +0200491 expectedItem1["thresholdValue"] = "32.7";
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100492
493 ASSERT_THAT(storedDiscreteConfiguration.at("ThresholdParamsDiscriminator"),
494 Eq(1));
495 ASSERT_THAT(storedDiscreteConfiguration.at("ThresholdParams"),
496 ElementsAre(expectedItem0, expectedItem1));
497}