Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 1 | #include "dbus_environment.hpp" |
| 2 | #include "messages/update_report_ind.hpp" |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 3 | #include "trigger_actions.hpp" |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 4 | #include "utils/messanger.hpp" |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 5 | |
| 6 | #include <stdexcept> |
| 7 | |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 8 | #include <gmock/gmock.h> |
| 9 | |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 10 | using namespace testing; |
| 11 | |
| 12 | namespace action |
| 13 | { |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 14 | namespace numeric |
| 15 | { |
| 16 | using LogParam = std::tuple<::numeric::Type, double, double>; |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 17 | |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 18 | static auto getCorrectParams() |
| 19 | { |
| 20 | return Values(std::make_tuple(::numeric::Type::upperCritical, 91.1, 90), |
| 21 | std::make_tuple(::numeric::Type::lowerCritical, 91.2, 90), |
| 22 | std::make_tuple(::numeric::Type::upperWarning, 88.5, 90), |
| 23 | std::make_tuple(::numeric::Type::lowerWarning, 88.6, 90)); |
| 24 | } |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 25 | |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 26 | static auto getIncorrectParams() |
| 27 | { |
| 28 | return Values( |
| 29 | std::make_tuple(::numeric::Type::upperCritical, 90.0, 90), |
| 30 | std::make_tuple(static_cast<::numeric::Type>(-1), 88.0, 90), |
| 31 | std::make_tuple(static_cast<::numeric::Type>(123), 123.0, 90)); |
| 32 | } |
| 33 | |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame^] | 34 | template <typename ActionType> |
| 35 | class TestActionNumeric : public Test, public WithParamInterface<LogParam> |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 36 | { |
| 37 | public: |
| 38 | void SetUp() override |
| 39 | { |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 40 | auto [type, threshold, value] = GetParam(); |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame^] | 41 | sut = std::make_unique<ActionType>(type, threshold); |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 42 | commmitValue = value; |
| 43 | } |
| 44 | |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame^] | 45 | void commit() |
| 46 | { |
| 47 | sut->commit("MyTrigger", std::nullopt, "MySensor", |
| 48 | Milliseconds{100'000}, commmitValue); |
| 49 | } |
| 50 | |
| 51 | std::unique_ptr<ActionType> sut; |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 52 | double commmitValue; |
| 53 | }; |
| 54 | |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame^] | 55 | class TestLogToJournalNumeric : public TestActionNumeric<LogToJournal> |
| 56 | {}; |
| 57 | |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 58 | INSTANTIATE_TEST_SUITE_P(LogToJournalNumericParams, TestLogToJournalNumeric, |
| 59 | getCorrectParams()); |
| 60 | |
| 61 | TEST_P(TestLogToJournalNumeric, commitAnActionDoesNotThrow) |
| 62 | { |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame^] | 63 | EXPECT_NO_THROW(commit()); |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | class TestLogToJournalNumericThrow : public TestLogToJournalNumeric |
| 67 | {}; |
| 68 | |
| 69 | INSTANTIATE_TEST_SUITE_P(_, TestLogToJournalNumericThrow, getIncorrectParams()); |
| 70 | |
| 71 | TEST_P(TestLogToJournalNumericThrow, commitAnActionExpectThrow) |
| 72 | { |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame^] | 73 | EXPECT_ANY_THROW(commit()); |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 74 | } |
| 75 | |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame^] | 76 | class TestLogToRedfishEventLogNumeric : |
| 77 | public TestActionNumeric<LogToRedfishEventLog> |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 78 | {}; |
| 79 | |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame^] | 80 | INSTANTIATE_TEST_SUITE_P(LogToRedfishEventLogNumericParams, |
| 81 | TestLogToRedfishEventLogNumeric, getCorrectParams()); |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 82 | |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame^] | 83 | TEST_P(TestLogToRedfishEventLogNumeric, commitExpectNoThrow) |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 84 | { |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame^] | 85 | EXPECT_NO_THROW(commit()); |
| 86 | } |
| 87 | |
| 88 | class TestLogToRedfishEventLogNumericThrow : |
| 89 | public TestLogToRedfishEventLogNumeric |
| 90 | {}; |
| 91 | |
| 92 | INSTANTIATE_TEST_SUITE_P(_, TestLogToRedfishEventLogNumericThrow, |
| 93 | getIncorrectParams()); |
| 94 | |
| 95 | TEST_P(TestLogToRedfishEventLogNumericThrow, commitExpectToThrow) |
| 96 | { |
| 97 | EXPECT_ANY_THROW(commit()); |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | } // namespace numeric |
| 101 | |
| 102 | namespace discrete |
| 103 | { |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame^] | 104 | using LogParam = std::tuple<::discrete::Severity, TriggerValue>; |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 105 | |
| 106 | static auto getCorrectParams() |
| 107 | { |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame^] | 108 | return Values( |
| 109 | std::make_tuple(::discrete::Severity::critical, |
| 110 | TriggerValue("DiscreteVal")), |
| 111 | std::make_tuple(::discrete::Severity::warning, TriggerValue("On")), |
| 112 | std::make_tuple(::discrete::Severity::ok, TriggerValue("Off"))); |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | static auto getIncorrectParams() |
| 116 | { |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame^] | 117 | return Values( |
| 118 | std::make_tuple(static_cast<::discrete::Severity>(-1), |
| 119 | TriggerValue("DiscreteVal42")), |
| 120 | std::make_tuple(static_cast<::discrete::Severity>(42), |
| 121 | TriggerValue("On")), |
| 122 | std::make_tuple(::discrete::Severity::critical, TriggerValue(42.0)), |
| 123 | std::make_tuple(::discrete::Severity::warning, TriggerValue(0.0)), |
| 124 | std::make_tuple(::discrete::Severity::ok, TriggerValue(0.1))); |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 125 | } |
| 126 | |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame^] | 127 | template <typename ActionType> |
| 128 | class TestActionDiscrete : public Test, public WithParamInterface<LogParam> |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 129 | { |
| 130 | public: |
| 131 | void SetUp() override |
| 132 | { |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame^] | 133 | auto [severity, value] = GetParam(); |
| 134 | sut = std::make_unique<ActionType>(severity); |
| 135 | commitValue = value; |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 136 | } |
| 137 | |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame^] | 138 | void commit() |
| 139 | { |
| 140 | std::string thresholdName = "MyThreshold"; |
| 141 | sut->commit("MyTrigger", std::cref(thresholdName), "MySensor", |
| 142 | Milliseconds{100'000}, commitValue); |
| 143 | } |
| 144 | |
| 145 | TriggerValue commitValue; |
| 146 | std::unique_ptr<ActionType> sut; |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 147 | }; |
| 148 | |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame^] | 149 | class TestLogToJournalDiscrete : public TestActionDiscrete<LogToJournal> |
| 150 | {}; |
| 151 | |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 152 | INSTANTIATE_TEST_SUITE_P(LogToJournalDiscreteParams, TestLogToJournalDiscrete, |
| 153 | getCorrectParams()); |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 154 | |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame^] | 155 | TEST_P(TestLogToJournalDiscrete, commitAnActionWIthDiscreteValueDoesNotThrow) |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 156 | { |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame^] | 157 | EXPECT_NO_THROW(commit()); |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 158 | } |
| 159 | |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 160 | class TestLogToJournalDiscreteThrow : public TestLogToJournalDiscrete |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 161 | {}; |
| 162 | |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 163 | INSTANTIATE_TEST_SUITE_P(_, TestLogToJournalDiscreteThrow, |
| 164 | getIncorrectParams()); |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 165 | |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 166 | TEST_P(TestLogToJournalDiscreteThrow, commitAnActionExpectThrow) |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 167 | { |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame^] | 168 | EXPECT_ANY_THROW(commit()); |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 169 | } |
| 170 | |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame^] | 171 | class TestLogToRedfishEventLogDiscrete : |
| 172 | public TestActionDiscrete<LogToRedfishEventLog> |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 173 | {}; |
| 174 | |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame^] | 175 | INSTANTIATE_TEST_SUITE_P(LogToRedfishEventLogDiscreteParams, |
| 176 | TestLogToRedfishEventLogDiscrete, getCorrectParams()); |
| 177 | |
| 178 | TEST_P(TestLogToRedfishEventLogDiscrete, commitExpectNoThrow) |
| 179 | { |
| 180 | EXPECT_NO_THROW(commit()); |
| 181 | } |
| 182 | |
| 183 | class TestLogToRedfishEventLogDiscreteThrow : |
| 184 | public TestLogToRedfishEventLogDiscrete |
| 185 | {}; |
| 186 | |
| 187 | INSTANTIATE_TEST_SUITE_P(_, TestLogToRedfishEventLogDiscreteThrow, |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 188 | getIncorrectParams()); |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 189 | |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame^] | 190 | TEST_P(TestLogToRedfishEventLogDiscreteThrow, commitExpectToThrow) |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 191 | { |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame^] | 192 | EXPECT_ANY_THROW(commit()); |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 193 | } |
| 194 | |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 195 | namespace onChange |
| 196 | { |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame^] | 197 | |
| 198 | template <typename ActionType> |
| 199 | class TestActionOnChange : public Test |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 200 | { |
| 201 | public: |
| 202 | void SetUp() override |
| 203 | { |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame^] | 204 | sut = std::make_unique<ActionType>(); |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 205 | } |
| 206 | |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame^] | 207 | void commit(TriggerValue value) |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 208 | { |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame^] | 209 | sut->commit("MyTrigger", std::nullopt, "MySensor", |
| 210 | Milliseconds{100'000}, value); |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 211 | } |
| 212 | |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame^] | 213 | std::unique_ptr<ActionType> sut; |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 214 | }; |
| 215 | |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame^] | 216 | class TestLogToJournalDiscreteOnChange : public TestActionOnChange<LogToJournal> |
| 217 | {}; |
| 218 | |
| 219 | TEST_F(TestLogToJournalDiscreteOnChange, commitNumericValueExpectNoThrow) |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 220 | { |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame^] | 221 | EXPECT_NO_THROW(commit(90.0)); |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 222 | } |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame^] | 223 | |
| 224 | TEST_F(TestLogToJournalDiscreteOnChange, commitDiscreteValueExpectNoThrow) |
| 225 | { |
| 226 | EXPECT_NO_THROW(commit("Off")); |
| 227 | } |
| 228 | |
| 229 | class TestLogToRedfishEventLogDiscreteOnChange : |
| 230 | public TestActionOnChange<LogToRedfishEventLog> |
| 231 | {}; |
| 232 | |
| 233 | TEST_F(TestLogToRedfishEventLogDiscreteOnChange, |
| 234 | commitNumericValueExpectNoThrow) |
| 235 | { |
| 236 | EXPECT_NO_THROW(commit(90.0)); |
| 237 | } |
| 238 | |
| 239 | TEST_F(TestLogToRedfishEventLogDiscreteOnChange, |
| 240 | commitDiscreteValueExpectNoThrow) |
| 241 | { |
| 242 | EXPECT_NO_THROW(commit("Off")); |
| 243 | } |
| 244 | |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 245 | } // namespace onChange |
| 246 | } // namespace discrete |
| 247 | |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 248 | class TestUpdateReport : public Test |
| 249 | { |
| 250 | public: |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 251 | TestUpdateReport() : messanger(DbusEnvironment::getIoc()) |
| 252 | {} |
| 253 | |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 254 | void make(std::vector<std::string> names) |
| 255 | { |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 256 | messanger.on_receive<messages::UpdateReportInd>( |
| 257 | [this](const auto& msg) { updateReport.Call(msg); }); |
| 258 | |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 259 | sut = std::make_unique<UpdateReport>( |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 260 | DbusEnvironment::getIoc(), |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 261 | std::make_shared<std::vector<std::string>>(std::move(names))); |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 262 | } |
| 263 | |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame^] | 264 | void commit(TriggerValue value) |
| 265 | { |
| 266 | sut->commit(triggerId, std::nullopt, "MySensor", Milliseconds{100'000}, |
| 267 | value); |
| 268 | } |
| 269 | |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 270 | utils::Messanger messanger; |
| 271 | NiceMock<MockFunction<void(const messages::UpdateReportInd&)>> updateReport; |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 272 | std::unique_ptr<UpdateReport> sut; |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame^] | 273 | std::string triggerId = "MyTrigger"; |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 274 | }; |
| 275 | |
| 276 | TEST_F(TestUpdateReport, commitWhenReportNameIsEmptyExpectNoReportUpdate) |
| 277 | { |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 278 | EXPECT_CALL(updateReport, Call(_)).Times(0); |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 279 | |
| 280 | make({}); |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame^] | 281 | commit(90.0); |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | TEST_F(TestUpdateReport, commitExpectReportUpdate) |
| 285 | { |
| 286 | std::vector<std::string> names = {"Report1", "Report2", "Report3"}; |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 287 | EXPECT_CALL(updateReport, |
| 288 | Call(FieldsAre(UnorderedElementsAreArray(names)))); |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 289 | |
| 290 | make(names); |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame^] | 291 | commit(90.0); |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | } // namespace action |