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