blob: b2b259c1756f413932736b39f722138e0d5f3417 [file] [log] [blame]
Wludzik, Jozef76833cb2020-12-21 14:42:41 +01001#include "dbus_environment.hpp"
2#include "helpers.hpp"
3#include "mocks/trigger_manager_mock.hpp"
4#include "params/trigger_params.hpp"
5#include "trigger.hpp"
6#include "utils/set_exception.hpp"
7
8using namespace testing;
9using namespace std::literals::string_literals;
10
11class TestTrigger : public Test
12{
13 public:
14 TriggerParams triggerParams;
15
16 std::unique_ptr<TriggerManagerMock> triggerManagerMockPtr =
17 std::make_unique<NiceMock<TriggerManagerMock>>();
18 std::unique_ptr<Trigger> sut;
19
20 void SetUp() override
21 {
22 sut = std::make_unique<Trigger>(
23 DbusEnvironment::getIoc(), DbusEnvironment::getObjServer(),
24 triggerParams.name(), triggerParams.isDiscrete(),
25 triggerParams.logToJournal(), triggerParams.logToRedfish(),
26 triggerParams.updateReport(), triggerParams.sensors(),
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010027 triggerParams.reportNames(), triggerParams.thresholdParams(),
28 std::vector<std::shared_ptr<interfaces::Threshold>>{},
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010029 *triggerManagerMockPtr);
30 }
31
32 template <class T>
33 static T getProperty(const std::string& path, const std::string& property)
34 {
Ed Tanous0e7ae5d2021-02-23 14:06:49 -080035 auto propertyPromise = std::promise<T>();
36 auto propertyFuture = propertyPromise.get_future();
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010037 sdbusplus::asio::getProperty<T>(
38 *DbusEnvironment::getBus(), DbusEnvironment::serviceName(), path,
39 Trigger::triggerIfaceName, property,
Ed Tanous0e7ae5d2021-02-23 14:06:49 -080040 [&propertyPromise](const boost::system::error_code& ec, T t) {
41 if (ec)
42 {
43 utils::setException(propertyPromise, "GetProperty failed");
44 return;
45 }
46 propertyPromise.set_value(t);
47 });
48 return DbusEnvironment::waitForFuture(std::move(propertyFuture));
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010049 }
50
51 boost::system::error_code deleteTrigger(const std::string& path)
52 {
53 std::promise<boost::system::error_code> methodPromise;
54 DbusEnvironment::getBus()->async_method_call(
55 [&methodPromise](boost::system::error_code ec) {
56 methodPromise.set_value(ec);
57 },
58 DbusEnvironment::serviceName(), path, Trigger::deleteIfaceName,
59 "Delete");
60 return DbusEnvironment::waitForFuture(methodPromise.get_future());
61 }
62};
63
64TEST_F(TestTrigger, checkIfPropertiesAreSet)
65{
66 EXPECT_THAT(getProperty<bool>(sut->getPath(), "Discrete"),
67 Eq(triggerParams.isDiscrete()));
68 EXPECT_THAT(getProperty<bool>(sut->getPath(), "LogToJournal"),
69 Eq(triggerParams.logToJournal()));
70 EXPECT_THAT(getProperty<bool>(sut->getPath(), "LogToRedfish"),
71 Eq(triggerParams.logToRedfish()));
72 EXPECT_THAT(getProperty<bool>(sut->getPath(), "UpdateReport"),
73 Eq(triggerParams.updateReport()));
74 EXPECT_THAT((getProperty<std::vector<
75 std::pair<sdbusplus::message::object_path, std::string>>>(
76 sut->getPath(), "Sensors")),
77 Eq(triggerParams.sensors()));
78 EXPECT_THAT(
79 getProperty<std::vector<std::string>>(sut->getPath(), "ReportNames"),
80 Eq(triggerParams.reportNames()));
81 EXPECT_THAT(
82 getProperty<TriggerThresholdParams>(sut->getPath(), "Thresholds"),
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010083 Eq(triggerParams.thresholdParams()));
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010084}
85
86TEST_F(TestTrigger, deleteTrigger)
87{
88 EXPECT_CALL(*triggerManagerMockPtr, removeTrigger(sut.get()));
89 auto ec = deleteTrigger(sut->getPath());
90 EXPECT_THAT(ec, Eq(boost::system::errc::success));
91}
92
93TEST_F(TestTrigger, deletingNonExistingTriggerReturnInvalidRequestDescriptor)
94{
95 auto ec = deleteTrigger(Trigger::triggerDir + "NonExisting"s);
96 EXPECT_THAT(ec.value(), Eq(EBADR));
97}