1 #include "dbus_environment.hpp"
2 #include "messages/update_report_ind.hpp"
3 #include "trigger_actions.hpp"
4 #include "utils/messanger.hpp"
5 
6 #include <stdexcept>
7 
8 #include <gmock/gmock.h>
9 
10 using namespace testing;
11 
12 namespace action
13 {
14 namespace numeric
15 {
16 using LogParam = std::tuple<::numeric::Type, double, double>;
17 
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 }
25 
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 
34 template <typename ActionType>
35 class TestActionNumeric : public Test, public WithParamInterface<LogParam>
36 {
37   public:
38     void SetUp() override
39     {
40         auto [type, threshold, value] = GetParam();
41         sut = std::make_unique<ActionType>(type, threshold);
42         commmitValue = value;
43     }
44 
45     void commit()
46     {
47         sut->commit("MyTrigger", std::nullopt, "MySensor",
48                     Milliseconds{100'000}, commmitValue);
49     }
50 
51     std::unique_ptr<ActionType> sut;
52     double commmitValue;
53 };
54 
55 class TestLogToJournalNumeric : public TestActionNumeric<LogToJournal>
56 {};
57 
58 INSTANTIATE_TEST_SUITE_P(LogToJournalNumericParams, TestLogToJournalNumeric,
59                          getCorrectParams());
60 
61 TEST_P(TestLogToJournalNumeric, commitAnActionDoesNotThrow)
62 {
63     EXPECT_NO_THROW(commit());
64 }
65 
66 class TestLogToJournalNumericThrow : public TestLogToJournalNumeric
67 {};
68 
69 INSTANTIATE_TEST_SUITE_P(_, TestLogToJournalNumericThrow, getIncorrectParams());
70 
71 TEST_P(TestLogToJournalNumericThrow, commitAnActionExpectThrow)
72 {
73     EXPECT_ANY_THROW(commit());
74 }
75 
76 class TestLogToRedfishEventLogNumeric :
77     public TestActionNumeric<LogToRedfishEventLog>
78 {};
79 
80 INSTANTIATE_TEST_SUITE_P(LogToRedfishEventLogNumericParams,
81                          TestLogToRedfishEventLogNumeric, getCorrectParams());
82 
83 TEST_P(TestLogToRedfishEventLogNumeric, commitExpectNoThrow)
84 {
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());
98 }
99 
100 } // namespace numeric
101 
102 namespace discrete
103 {
104 using LogParam = std::tuple<::discrete::Severity, TriggerValue>;
105 
106 static auto getCorrectParams()
107 {
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")));
113 }
114 
115 static auto getIncorrectParams()
116 {
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)));
125 }
126 
127 template <typename ActionType>
128 class TestActionDiscrete : public Test, public WithParamInterface<LogParam>
129 {
130   public:
131     void SetUp() override
132     {
133         auto [severity, value] = GetParam();
134         sut = std::make_unique<ActionType>(severity);
135         commitValue = value;
136     }
137 
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;
147 };
148 
149 class TestLogToJournalDiscrete : public TestActionDiscrete<LogToJournal>
150 {};
151 
152 INSTANTIATE_TEST_SUITE_P(LogToJournalDiscreteParams, TestLogToJournalDiscrete,
153                          getCorrectParams());
154 
155 TEST_P(TestLogToJournalDiscrete, commitAnActionWIthDiscreteValueDoesNotThrow)
156 {
157     EXPECT_NO_THROW(commit());
158 }
159 
160 class TestLogToJournalDiscreteThrow : public TestLogToJournalDiscrete
161 {};
162 
163 INSTANTIATE_TEST_SUITE_P(_, TestLogToJournalDiscreteThrow,
164                          getIncorrectParams());
165 
166 TEST_P(TestLogToJournalDiscreteThrow, commitAnActionExpectThrow)
167 {
168     EXPECT_ANY_THROW(commit());
169 }
170 
171 class TestLogToRedfishEventLogDiscrete :
172     public TestActionDiscrete<LogToRedfishEventLog>
173 {};
174 
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,
188                          getIncorrectParams());
189 
190 TEST_P(TestLogToRedfishEventLogDiscreteThrow, commitExpectToThrow)
191 {
192     EXPECT_ANY_THROW(commit());
193 }
194 
195 namespace onChange
196 {
197 
198 template <typename ActionType>
199 class TestActionOnChange : public Test
200 {
201   public:
202     void SetUp() override
203     {
204         sut = std::make_unique<ActionType>();
205     }
206 
207     void commit(TriggerValue value)
208     {
209         sut->commit("MyTrigger", std::nullopt, "MySensor",
210                     Milliseconds{100'000}, value);
211     }
212 
213     std::unique_ptr<ActionType> sut;
214 };
215 
216 class TestLogToJournalDiscreteOnChange : public TestActionOnChange<LogToJournal>
217 {};
218 
219 TEST_F(TestLogToJournalDiscreteOnChange, commitNumericValueExpectNoThrow)
220 {
221     EXPECT_NO_THROW(commit(90.0));
222 }
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 
245 } // namespace onChange
246 } // namespace discrete
247 
248 class TestUpdateReport : public Test
249 {
250   public:
251     TestUpdateReport() : messanger(DbusEnvironment::getIoc())
252     {}
253 
254     void make(std::vector<std::string> names)
255     {
256         messanger.on_receive<messages::UpdateReportInd>(
257             [this](const auto& msg) { updateReport.Call(msg); });
258 
259         sut = std::make_unique<UpdateReport>(
260             DbusEnvironment::getIoc(),
261             std::make_shared<std::vector<std::string>>(std::move(names)));
262     }
263 
264     void commit(TriggerValue value)
265     {
266         sut->commit(triggerId, std::nullopt, "MySensor", Milliseconds{100'000},
267                     value);
268     }
269 
270     utils::Messanger messanger;
271     NiceMock<MockFunction<void(const messages::UpdateReportInd&)>> updateReport;
272     std::unique_ptr<UpdateReport> sut;
273     std::string triggerId = "MyTrigger";
274 };
275 
276 TEST_F(TestUpdateReport, commitWhenReportNameIsEmptyExpectNoReportUpdate)
277 {
278     EXPECT_CALL(updateReport, Call(_)).Times(0);
279 
280     make({});
281     commit(90.0);
282 }
283 
284 TEST_F(TestUpdateReport, commitExpectReportUpdate)
285 {
286     std::vector<std::string> names = {"Report1", "Report2", "Report3"};
287     EXPECT_CALL(updateReport,
288                 Call(FieldsAre(UnorderedElementsAreArray(names))));
289 
290     make(names);
291     commit(90.0);
292 }
293 
294 } // namespace action
295