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