blob: 953ddd5462cc0d7dcf12bb3d00a374a27d07a287 [file] [log] [blame]
Szymon Dompkef763c9e2021-03-12 09:19:22 +01001#include "dbus_environment.hpp"
2#include "discrete_threshold.hpp"
3#include "helpers.hpp"
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +02004#include "mocks/clock_mock.hpp"
Szymon Dompkef763c9e2021-03-12 09:19:22 +01005#include "mocks/sensor_mock.hpp"
6#include "mocks/trigger_action_mock.hpp"
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +01007#include "types/duration_types.hpp"
Szymon Dompkef763c9e2021-03-12 09:19:22 +01008#include "utils/conv_container.hpp"
9
10#include <gmock/gmock.h>
11
12using namespace testing;
13using namespace std::chrono_literals;
14
15class TestDiscreteThreshold : public Test
16{
17 public:
18 std::vector<std::shared_ptr<SensorMock>> sensorMocks = {
19 std::make_shared<NiceMock<SensorMock>>(),
20 std::make_shared<NiceMock<SensorMock>>()};
21 std::vector<std::string> sensorNames = {"Sensor1", "Sensor2"};
22 std::unique_ptr<TriggerActionMock> actionMockPtr =
23 std::make_unique<StrictMock<TriggerActionMock>>();
24 TriggerActionMock& actionMock = *actionMockPtr;
25 std::shared_ptr<DiscreteThreshold> sut;
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020026 std::string triggerId = "MyTrigger";
27 std::unique_ptr<NiceMock<ClockMock>> clockMockPtr =
28 std::make_unique<NiceMock<ClockMock>>();
Szymon Dompkef763c9e2021-03-12 09:19:22 +010029
Szymon Dompke94f71c52021-12-10 07:16:33 +010030 std::shared_ptr<DiscreteThreshold>
Szymon Dompkeaa572362022-03-23 16:31:24 +010031 makeThreshold(Milliseconds dwellTime, std::string thresholdValue,
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020032 discrete::Severity severity = discrete::Severity::ok,
33 std::string thresholdName = "treshold name")
Szymon Dompkef763c9e2021-03-12 09:19:22 +010034 {
35 std::vector<std::unique_ptr<interfaces::TriggerAction>> actions;
36 actions.push_back(std::move(actionMockPtr));
37
38 return std::make_shared<DiscreteThreshold>(
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020039 DbusEnvironment::getIoc(), triggerId,
Szymon Dompkef763c9e2021-03-12 09:19:22 +010040 utils::convContainer<std::shared_ptr<interfaces::Sensor>>(
41 sensorMocks),
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020042 std::move(actions), dwellTime, thresholdValue, thresholdName,
43 severity, std::move(clockMockPtr));
Szymon Dompkef763c9e2021-03-12 09:19:22 +010044 }
45
46 void SetUp() override
47 {
Szymon Dompke94f71c52021-12-10 07:16:33 +010048 for (size_t idx = 0; idx < sensorMocks.size(); idx++)
49 {
50 ON_CALL(*sensorMocks.at(idx), getName())
51 .WillByDefault(Return(sensorNames[idx]));
52 }
53
Szymon Dompkeaa572362022-03-23 16:31:24 +010054 sut = makeThreshold(0ms, "90.0", discrete::Severity::critical);
Szymon Dompkef763c9e2021-03-12 09:19:22 +010055 }
56};
57
58TEST_F(TestDiscreteThreshold, initializeThresholdExpectAllSensorsAreRegistered)
59{
60 for (auto& sensor : sensorMocks)
61 {
62 EXPECT_CALL(*sensor,
63 registerForUpdates(Truly([sut = sut.get()](const auto& x) {
64 return x.lock().get() == sut;
65 })));
66 }
67
68 sut->initialize();
69}
70
71TEST_F(TestDiscreteThreshold, thresholdIsNotInitializeExpectNoActionCommit)
72{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020073 EXPECT_CALL(actionMock, commit(_, _, _, _, _)).Times(0);
Szymon Dompkef763c9e2021-03-12 09:19:22 +010074}
75
Szymon Dompkeaa572362022-03-23 16:31:24 +010076class TestDiscreteThresholdValues :
77 public TestDiscreteThreshold,
78 public WithParamInterface<std::string>
79{};
80
81INSTANTIATE_TEST_SUITE_P(_, TestDiscreteThresholdValues,
82 Values("90", ".90", "90.123", "0.0"));
83
84TEST_P(TestDiscreteThresholdValues, thresholdValueIsNumericAndStoredCorrectly)
Szymon Dompke94f71c52021-12-10 07:16:33 +010085{
Szymon Dompkeaa572362022-03-23 16:31:24 +010086 sut = makeThreshold(0ms, GetParam(), discrete::Severity::critical);
Szymon Dompke94f71c52021-12-10 07:16:33 +010087 LabeledThresholdParam expected = discrete::LabeledThresholdParam(
Szymon Dompkeaa572362022-03-23 16:31:24 +010088 "treshold name", discrete::Severity::critical, 0, GetParam());
Szymon Dompke94f71c52021-12-10 07:16:33 +010089 EXPECT_EQ(sut->getThresholdParam(), expected);
90}
91
Szymon Dompkeaa572362022-03-23 16:31:24 +010092class TestBadDiscreteThresholdValues :
93 public TestDiscreteThreshold,
94 public WithParamInterface<std::string>
95{};
96
97INSTANTIATE_TEST_SUITE_P(_, TestBadDiscreteThresholdValues,
98 Values("90ad", "ab.90", "x90", "On", "Off", ""));
99
100TEST_P(TestBadDiscreteThresholdValues, throwsWhenNotNumericValues)
101{
102 EXPECT_THROW(makeThreshold(0ms, GetParam()), std::invalid_argument);
103}
104
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200105class TestDiscreteThresholdInit : public TestDiscreteThreshold
106{
107 void SetUp() override
108 {}
109};
110
111TEST_F(TestDiscreteThresholdInit, nonEmptyNameIsNotChanged)
112{
113 auto sut = makeThreshold(0ms, "12.3", discrete::Severity::ok, "non-empty");
114 EXPECT_THAT(
115 std::get<discrete::LabeledThresholdParam>(sut->getThresholdParam())
116 .at_label<utils::tstring::UserId>(),
117 Eq("non-empty"));
118}
119
120TEST_F(TestDiscreteThresholdInit, emptyNameIsChanged)
121{
122 auto sut = makeThreshold(0ms, "12.3", discrete::Severity::ok, "");
123 EXPECT_THAT(
124 std::get<discrete::LabeledThresholdParam>(sut->getThresholdParam())
125 .at_label<utils::tstring::UserId>(),
126 Not(Eq("")));
127}
128
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100129struct DiscreteParams
130{
131 struct UpdateParams
132 {
133 size_t sensor;
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100134 double value;
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000135 Milliseconds sleepAfter;
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100136
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200137 UpdateParams(size_t sensor, double value,
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000138 Milliseconds sleepAfter = 0ms) :
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100139 sensor(sensor),
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200140 value(value), sleepAfter(sleepAfter)
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100141 {}
142 };
143
144 struct ExpectedParams
145 {
146 size_t sensor;
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100147 double value;
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000148 Milliseconds waitMin;
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100149
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200150 ExpectedParams(size_t sensor, double value,
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000151 Milliseconds waitMin = 0ms) :
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100152 sensor(sensor),
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200153 value(value), waitMin(waitMin)
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100154 {}
155 };
156
157 DiscreteParams& Updates(std::vector<UpdateParams> val)
158 {
159 updates = std::move(val);
160 return *this;
161 }
162
163 DiscreteParams& Expected(std::vector<ExpectedParams> val)
164 {
165 expected = std::move(val);
166 return *this;
167 }
168
Szymon Dompkeaa572362022-03-23 16:31:24 +0100169 DiscreteParams& ThresholdValue(std::string val)
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100170 {
Szymon Dompkeaa572362022-03-23 16:31:24 +0100171 thresholdValue = std::move(val);
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100172 return *this;
173 }
174
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000175 DiscreteParams& DwellTime(Milliseconds val)
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100176 {
177 dwellTime = std::move(val);
178 return *this;
179 }
180
181 friend void PrintTo(const DiscreteParams& o, std::ostream* os)
182 {
183 *os << "{ DwellTime: " << o.dwellTime.count() << "ms ";
184 *os << ", ThresholdValue: " << o.thresholdValue;
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200185 *os << ", Updates: [ ";
186 for (const auto& [index, value, sleepAfter] : o.updates)
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100187 {
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200188 *os << "{ SensorIndex: " << index << ", Value: " << value
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100189 << ", SleepAfter: " << sleepAfter.count() << "ms }, ";
190 }
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200191 *os << " ] Expected: [ ";
192 for (const auto& [index, value, waitMin] : o.expected)
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100193 {
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200194 *os << "{ SensorIndex: " << index << ", Value: " << value
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +0100195 << ", waitMin: " << waitMin.count() << "ms }, ";
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100196 }
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200197 *os << " ] }";
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100198 }
199
200 std::vector<UpdateParams> updates;
201 std::vector<ExpectedParams> expected;
Szymon Dompkeaa572362022-03-23 16:31:24 +0100202 std::string thresholdValue = "0.0";
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000203 Milliseconds dwellTime = 0ms;
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100204};
205
206class TestDiscreteThresholdCommon :
207 public TestDiscreteThreshold,
208 public WithParamInterface<DiscreteParams>
209{
210 public:
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000211 void sleep(Milliseconds duration)
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100212 {
213 if (duration != 0ms)
214 {
215 DbusEnvironment::sleepFor(duration);
216 }
217 }
218
219 void testBodySensorIsUpdatedMultipleTimes()
220 {
221 std::vector<std::chrono::time_point<std::chrono::high_resolution_clock>>
222 timestamps(sensorMocks.size());
223
224 sut->initialize();
225
226 InSequence seq;
227
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200228 for (const auto& [index, value, waitMin] : GetParam().expected)
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100229 {
230 EXPECT_CALL(actionMock,
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200231 commit(triggerId, Optional(StrEq("treshold name")),
232 sensorNames[index], _,
233 TriggerValue(GetParam().thresholdValue)))
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100234 .WillOnce(DoAll(
235 InvokeWithoutArgs([idx = index, &timestamps] {
236 timestamps[idx] =
237 std::chrono::high_resolution_clock::now();
238 }),
239 InvokeWithoutArgs(DbusEnvironment::setPromise("commit"))));
240 }
241
242 auto start = std::chrono::high_resolution_clock::now();
243
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200244 for (const auto& [index, value, sleepAfter] : GetParam().updates)
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100245 {
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200246 sut->sensorUpdated(*sensorMocks[index], 42ms, value);
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100247 sleep(sleepAfter);
248 }
249
250 EXPECT_THAT(DbusEnvironment::waitForFutures("commit"), true);
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200251 for (const auto& [index, value, waitMin] : GetParam().expected)
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100252 {
253 EXPECT_THAT(timestamps[index] - start, Ge(waitMin));
254 }
255 }
256};
257
258class TestDiscreteThresholdNoDwellTime : public TestDiscreteThresholdCommon
259{
260 public:
261 void SetUp() override
262 {
Szymon Dompke94f71c52021-12-10 07:16:33 +0100263 for (size_t idx = 0; idx < sensorMocks.size(); idx++)
264 {
265 ON_CALL(*sensorMocks.at(idx), getName())
266 .WillByDefault(Return(sensorNames[idx]));
267 }
268
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100269 sut = makeThreshold(0ms, GetParam().thresholdValue);
270 }
271};
272
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200273INSTANTIATE_TEST_SUITE_P(
274 _, TestDiscreteThresholdNoDwellTime,
275 Values(DiscreteParams()
276 .ThresholdValue("90.0")
277 .Updates({{0, 80.0}, {0, 89.0}})
278 .Expected({}),
279 DiscreteParams()
280 .ThresholdValue("90.0")
281 .Updates({{0, 80.0}, {0, 90.0}, {0, 80.0}, {0, 90.0}})
282 .Expected({{0, 90.0}, {0, 90.0}}),
283 DiscreteParams()
284 .ThresholdValue("90.0")
285 .Updates({{0, 90.0}, {0, 99.0}, {1, 100.0}, {1, 90.0}})
286 .Expected({{0, 90.0}, {1, 90.0}})));
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100287
288TEST_P(TestDiscreteThresholdNoDwellTime, senorsIsUpdatedMultipleTimes)
289{
290 testBodySensorIsUpdatedMultipleTimes();
291}
292
293class TestDiscreteThresholdWithDwellTime : public TestDiscreteThresholdCommon
294{
295 public:
296 void SetUp() override
297 {
Szymon Dompke94f71c52021-12-10 07:16:33 +0100298 for (size_t idx = 0; idx < sensorMocks.size(); idx++)
299 {
300 ON_CALL(*sensorMocks.at(idx), getName())
301 .WillByDefault(Return(sensorNames[idx]));
302 }
303
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100304 sut = makeThreshold(GetParam().dwellTime, GetParam().thresholdValue);
305 }
306};
307
308INSTANTIATE_TEST_SUITE_P(
309 _, TestDiscreteThresholdWithDwellTime,
310 Values(DiscreteParams()
311 .DwellTime(200ms)
Szymon Dompkeaa572362022-03-23 16:31:24 +0100312 .ThresholdValue("90.0")
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200313 .Updates({{0, 90.0, 100ms}, {0, 91.0}, {0, 90.0}})
314 .Expected({{0, 90.0, 300ms}}),
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100315 DiscreteParams()
316 .DwellTime(100ms)
Szymon Dompkeaa572362022-03-23 16:31:24 +0100317 .ThresholdValue("90.0")
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200318 .Updates({{0, 90.0, 100ms}})
319 .Expected({{0, 90.0, 100ms}}),
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100320 DiscreteParams()
321 .DwellTime(1000ms)
Szymon Dompkeaa572362022-03-23 16:31:24 +0100322 .ThresholdValue("90.0")
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200323 .Updates({{0, 90.0, 700ms},
324 {0, 91.0, 100ms},
325 {0, 90.0, 300ms},
326 {0, 91.0, 100ms}})
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100327 .Expected({}),
328 DiscreteParams()
329 .DwellTime(200ms)
Szymon Dompkeaa572362022-03-23 16:31:24 +0100330 .ThresholdValue("90.0")
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200331 .Updates({{0, 90.0},
332 {1, 89.0, 100ms},
333 {1, 90.0, 100ms},
334 {1, 89.0, 100ms},
335 {1, 90.0, 300ms},
336 {1, 89.0, 100ms}})
337 .Expected({{0, 90, 200ms}, {1, 90, 500ms}})));
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100338
339TEST_P(TestDiscreteThresholdWithDwellTime, senorsIsUpdatedMultipleTimes)
340{
341 testBodySensorIsUpdatedMultipleTimes();
342}