1 #include "mocks/report_manager_mock.hpp"
2 #include "trigger_actions.hpp"
3 
4 #include <stdexcept>
5 
6 using namespace testing;
7 
8 namespace action
9 {
10 namespace numeric
11 {
12 using LogParam = std::tuple<::numeric::Type, double, double>;
13 
14 static auto getCorrectParams()
15 {
16     return Values(std::make_tuple(::numeric::Type::upperCritical, 91.1, 90),
17                   std::make_tuple(::numeric::Type::lowerCritical, 91.2, 90),
18                   std::make_tuple(::numeric::Type::upperWarning, 88.5, 90),
19                   std::make_tuple(::numeric::Type::lowerWarning, 88.6, 90));
20 }
21 
22 static auto getIncorrectParams()
23 {
24     return Values(
25         std::make_tuple(::numeric::Type::upperCritical, 90.0, 90),
26         std::make_tuple(static_cast<::numeric::Type>(-1), 88.0, 90),
27         std::make_tuple(static_cast<::numeric::Type>(123), 123.0, 90));
28 }
29 
30 class TestLogToJournalNumeric : public Test, public WithParamInterface<LogParam>
31 {
32   public:
33     void SetUp() override
34     {
35         auto [type, threshold, value] = GetParam();
36         sut = std::make_unique<numeric::LogToJournal>(type, threshold);
37         commmitValue = value;
38     }
39 
40     std::unique_ptr<numeric::LogToJournal> sut;
41     double commmitValue;
42 };
43 
44 INSTANTIATE_TEST_SUITE_P(LogToJournalNumericParams, TestLogToJournalNumeric,
45                          getCorrectParams());
46 
47 TEST_P(TestLogToJournalNumeric, commitAnActionDoesNotThrow)
48 {
49     EXPECT_NO_THROW(sut->commit("Test", Milliseconds{100'000}, commmitValue));
50 }
51 
52 class TestLogToJournalNumericThrow : public TestLogToJournalNumeric
53 {};
54 
55 INSTANTIATE_TEST_SUITE_P(_, TestLogToJournalNumericThrow, getIncorrectParams());
56 
57 TEST_P(TestLogToJournalNumericThrow, commitAnActionExpectThrow)
58 {
59     EXPECT_THROW(sut->commit("Test", Milliseconds{100'000}, commmitValue),
60                  std::runtime_error);
61 }
62 
63 class TestLogToRedfishNumeric : public Test, public WithParamInterface<LogParam>
64 {
65   public:
66     void SetUp() override
67     {
68         auto [type, threshold, value] = GetParam();
69         sut = std::make_unique<LogToRedfish>(type, threshold);
70         commmitValue = value;
71     }
72 
73     std::unique_ptr<LogToRedfish> sut;
74     double commmitValue;
75 };
76 
77 INSTANTIATE_TEST_SUITE_P(LogToRedfishNumericParams, TestLogToRedfishNumeric,
78                          getCorrectParams());
79 
80 TEST_P(TestLogToRedfishNumeric, commitExpectNoThrow)
81 {
82     EXPECT_NO_THROW(sut->commit("Test", Milliseconds{100'000}, commmitValue));
83 }
84 
85 class TestLogToRedfishNumericThrow : public TestLogToRedfishNumeric
86 {};
87 
88 INSTANTIATE_TEST_SUITE_P(_, TestLogToRedfishNumericThrow, getIncorrectParams());
89 
90 TEST_P(TestLogToRedfishNumericThrow, commitExpectToThrow)
91 {
92     EXPECT_THROW(sut->commit("Test", Milliseconds{100'000}, commmitValue),
93                  std::runtime_error);
94 }
95 
96 } // namespace numeric
97 
98 namespace discrete
99 {
100 using LogParam = ::discrete::Severity;
101 
102 static auto getCorrectParams()
103 {
104     return Values(::discrete::Severity::critical, ::discrete::Severity::warning,
105                   ::discrete::Severity::ok);
106 }
107 
108 static auto getIncorrectParams()
109 {
110     return Values(static_cast<::discrete::Severity>(-1),
111                   static_cast<::discrete::Severity>(42));
112 }
113 
114 class TestLogToJournalDiscrete :
115     public Test,
116     public WithParamInterface<LogParam>
117 {
118   public:
119     void SetUp() override
120     {
121         auto severity = GetParam();
122         sut = std::make_unique<LogToJournal>(severity);
123     }
124 
125     std::unique_ptr<LogToJournal> sut;
126 };
127 
128 INSTANTIATE_TEST_SUITE_P(LogToJournalDiscreteParams, TestLogToJournalDiscrete,
129                          getCorrectParams());
130 
131 TEST_P(TestLogToJournalDiscrete, commitAnActionDoesNotThrow)
132 {
133     EXPECT_NO_THROW(sut->commit("Test", Milliseconds{100'000}, 90.0));
134 }
135 
136 class TestLogToJournalDiscreteThrow : public TestLogToJournalDiscrete
137 {};
138 
139 INSTANTIATE_TEST_SUITE_P(_, TestLogToJournalDiscreteThrow,
140                          getIncorrectParams());
141 
142 TEST_P(TestLogToJournalDiscreteThrow, commitAnActionExpectThrow)
143 {
144     EXPECT_THROW(sut->commit("Test", Milliseconds{100'000}, 90.0),
145                  std::runtime_error);
146 }
147 
148 class TestLogToRedfishDiscrete :
149     public Test,
150     public WithParamInterface<LogParam>
151 {
152   public:
153     void SetUp() override
154     {
155         auto severity = GetParam();
156         sut = std::make_unique<LogToRedfish>(severity);
157     }
158 
159     std::unique_ptr<LogToRedfish> sut;
160 };
161 
162 INSTANTIATE_TEST_SUITE_P(LogToRedfishDiscreteParams, TestLogToRedfishDiscrete,
163                          getCorrectParams());
164 
165 TEST_P(TestLogToRedfishDiscrete, commitExpectNoThrow)
166 {
167     EXPECT_NO_THROW(sut->commit("Test", Milliseconds{100'000}, 90.0));
168 }
169 
170 class TestLogToRedfishDiscreteThrow : public TestLogToRedfishDiscrete
171 {};
172 
173 INSTANTIATE_TEST_SUITE_P(_, TestLogToRedfishDiscreteThrow,
174                          getIncorrectParams());
175 
176 TEST_P(TestLogToRedfishDiscreteThrow, commitExpectToThrow)
177 {
178     EXPECT_THROW(sut->commit("Test", Milliseconds{100'000}, 90.0),
179                  std::runtime_error);
180 }
181 
182 namespace onChange
183 {
184 class TestLogToJournalDiscreteOnChange : public Test
185 {
186   public:
187     void SetUp() override
188     {
189         sut = std::make_unique<LogToJournal>();
190     }
191 
192     std::unique_ptr<LogToJournal> sut;
193 };
194 
195 TEST_F(TestLogToJournalDiscreteOnChange, commitExpectNoThrow)
196 {
197     EXPECT_NO_THROW(sut->commit("Test", Milliseconds{100'000}, 90.0));
198 }
199 
200 class TestLogToRedfishDiscreteOnChange : public Test
201 {
202   public:
203     void SetUp() override
204     {
205         sut = std::make_unique<LogToRedfish>();
206     }
207 
208     std::unique_ptr<LogToRedfish> sut;
209 };
210 
211 TEST_F(TestLogToRedfishDiscreteOnChange, commitExpectNoThrow)
212 {
213     EXPECT_NO_THROW(sut->commit("Test", Milliseconds{100'000}, 90.0));
214 }
215 } // namespace onChange
216 } // namespace discrete
217 
218 class TestUpdateReport : public Test
219 {
220   public:
221     void make(std::vector<std::string> names)
222     {
223         sut = std::make_unique<UpdateReport>(
224             reportManager,
225             std::make_shared<std::vector<std::string>>(std::move(names)));
226     }
227 
228     NiceMock<ReportManagerMock> reportManager;
229     std::unique_ptr<UpdateReport> sut;
230 };
231 
232 TEST_F(TestUpdateReport, commitWhenReportNameIsEmptyExpectNoReportUpdate)
233 {
234     EXPECT_CALL(reportManager, updateReport(_)).Times(0);
235 
236     make({});
237     sut->commit("Test", Milliseconds{100'000}, 90.0);
238 }
239 
240 TEST_F(TestUpdateReport, commitExpectReportUpdate)
241 {
242     std::vector<std::string> names = {"Report1", "Report2", "Report3"};
243     for (const auto& name : names)
244     {
245         EXPECT_CALL(reportManager, updateReport(name));
246     }
247 
248     make(names);
249     sut->commit("Test", Milliseconds{100'000}, 90.0);
250 }
251 
252 } // namespace action
253