Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 1 | #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 | |
| 8 | using namespace testing; |
| 9 | using namespace std::literals::string_literals; |
| 10 | |
| 11 | class 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, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 27 | triggerParams.reportNames(), triggerParams.thresholdParams(), |
| 28 | std::vector<std::shared_ptr<interfaces::Threshold>>{}, |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 29 | *triggerManagerMockPtr); |
| 30 | } |
| 31 | |
| 32 | template <class T> |
| 33 | static T getProperty(const std::string& path, const std::string& property) |
| 34 | { |
Ed Tanous | 0e7ae5d | 2021-02-23 14:06:49 -0800 | [diff] [blame^] | 35 | auto propertyPromise = std::promise<T>(); |
| 36 | auto propertyFuture = propertyPromise.get_future(); |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 37 | sdbusplus::asio::getProperty<T>( |
| 38 | *DbusEnvironment::getBus(), DbusEnvironment::serviceName(), path, |
| 39 | Trigger::triggerIfaceName, property, |
Ed Tanous | 0e7ae5d | 2021-02-23 14:06:49 -0800 | [diff] [blame^] | 40 | [&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, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 49 | } |
| 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 | |
| 64 | TEST_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, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 83 | Eq(triggerParams.thresholdParams())); |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | TEST_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 | |
| 93 | TEST_F(TestTrigger, deletingNonExistingTriggerReturnInvalidRequestDescriptor) |
| 94 | { |
| 95 | auto ec = deleteTrigger(Trigger::triggerDir + "NonExisting"s); |
| 96 | EXPECT_THAT(ec.value(), Eq(EBADR)); |
| 97 | } |