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