blob: 9003d49c535bc8f01b7480848464748721862514 [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
Patrick Williams583ba442025-02-03 14:28:19 -050030 std::shared_ptr<DiscreteThreshold> makeThreshold(
31 Milliseconds dwellTime, std::string thresholdValue,
32 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) {
Patrick Williams583ba442025-02-03 14:28:19 -050064 return x.lock().get() == sut;
65 })));
Szymon Dompkef763c9e2021-03-12 09:19:22 +010066 }
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{
Patrick Williams3a1c2972023-05-10 07:51:04 -0500107 void SetUp() override {}
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200108};
109
110TEST_F(TestDiscreteThresholdInit, nonEmptyNameIsNotChanged)
111{
112 auto sut = makeThreshold(0ms, "12.3", discrete::Severity::ok, "non-empty");
113 EXPECT_THAT(
114 std::get<discrete::LabeledThresholdParam>(sut->getThresholdParam())
115 .at_label<utils::tstring::UserId>(),
116 Eq("non-empty"));
117}
118
119TEST_F(TestDiscreteThresholdInit, emptyNameIsChanged)
120{
121 auto sut = makeThreshold(0ms, "12.3", discrete::Severity::ok, "");
122 EXPECT_THAT(
123 std::get<discrete::LabeledThresholdParam>(sut->getThresholdParam())
124 .at_label<utils::tstring::UserId>(),
125 Not(Eq("")));
126}
127
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100128struct DiscreteParams
129{
130 struct UpdateParams
131 {
132 size_t sensor;
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100133 double value;
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000134 Milliseconds sleepAfter;
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100135
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200136 UpdateParams(size_t sensor, double value,
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000137 Milliseconds sleepAfter = 0ms) :
Patrick Williamsf535cad2024-08-16 15:21:20 -0400138 sensor(sensor), value(value), sleepAfter(sleepAfter)
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100139 {}
140 };
141
142 struct ExpectedParams
143 {
144 size_t sensor;
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100145 double value;
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000146 Milliseconds waitMin;
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100147
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200148 ExpectedParams(size_t sensor, double value,
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000149 Milliseconds waitMin = 0ms) :
Patrick Williamsf535cad2024-08-16 15:21:20 -0400150 sensor(sensor), value(value), waitMin(waitMin)
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100151 {}
152 };
153
154 DiscreteParams& Updates(std::vector<UpdateParams> val)
155 {
156 updates = std::move(val);
157 return *this;
158 }
159
160 DiscreteParams& Expected(std::vector<ExpectedParams> val)
161 {
162 expected = std::move(val);
163 return *this;
164 }
165
Szymon Dompkeaa572362022-03-23 16:31:24 +0100166 DiscreteParams& ThresholdValue(std::string val)
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100167 {
Szymon Dompkeaa572362022-03-23 16:31:24 +0100168 thresholdValue = std::move(val);
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100169 return *this;
170 }
171
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000172 DiscreteParams& DwellTime(Milliseconds val)
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100173 {
174 dwellTime = std::move(val);
175 return *this;
176 }
177
178 friend void PrintTo(const DiscreteParams& o, std::ostream* os)
179 {
180 *os << "{ DwellTime: " << o.dwellTime.count() << "ms ";
181 *os << ", ThresholdValue: " << o.thresholdValue;
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200182 *os << ", Updates: [ ";
183 for (const auto& [index, value, sleepAfter] : o.updates)
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100184 {
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200185 *os << "{ SensorIndex: " << index << ", Value: " << value
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100186 << ", SleepAfter: " << sleepAfter.count() << "ms }, ";
187 }
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200188 *os << " ] Expected: [ ";
189 for (const auto& [index, value, waitMin] : o.expected)
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100190 {
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200191 *os << "{ SensorIndex: " << index << ", Value: " << value
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +0100192 << ", waitMin: " << waitMin.count() << "ms }, ";
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100193 }
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200194 *os << " ] }";
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100195 }
196
197 std::vector<UpdateParams> updates;
198 std::vector<ExpectedParams> expected;
Szymon Dompkeaa572362022-03-23 16:31:24 +0100199 std::string thresholdValue = "0.0";
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000200 Milliseconds dwellTime = 0ms;
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100201};
202
203class TestDiscreteThresholdCommon :
204 public TestDiscreteThreshold,
205 public WithParamInterface<DiscreteParams>
206{
207 public:
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000208 void sleep(Milliseconds duration)
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100209 {
210 if (duration != 0ms)
211 {
212 DbusEnvironment::sleepFor(duration);
213 }
214 }
215
216 void testBodySensorIsUpdatedMultipleTimes()
217 {
218 std::vector<std::chrono::time_point<std::chrono::high_resolution_clock>>
219 timestamps(sensorMocks.size());
220
221 sut->initialize();
222
223 InSequence seq;
224
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200225 for (const auto& [index, value, waitMin] : GetParam().expected)
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100226 {
227 EXPECT_CALL(actionMock,
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200228 commit(triggerId, Optional(StrEq("treshold name")),
229 sensorNames[index], _,
230 TriggerValue(GetParam().thresholdValue)))
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100231 .WillOnce(DoAll(
232 InvokeWithoutArgs([idx = index, &timestamps] {
Patrick Williams583ba442025-02-03 14:28:19 -0500233 timestamps[idx] =
234 std::chrono::high_resolution_clock::now();
235 }),
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100236 InvokeWithoutArgs(DbusEnvironment::setPromise("commit"))));
237 }
238
239 auto start = std::chrono::high_resolution_clock::now();
240
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200241 for (const auto& [index, value, sleepAfter] : GetParam().updates)
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100242 {
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200243 sut->sensorUpdated(*sensorMocks[index], 42ms, value);
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100244 sleep(sleepAfter);
245 }
246
247 EXPECT_THAT(DbusEnvironment::waitForFutures("commit"), true);
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200248 for (const auto& [index, value, waitMin] : GetParam().expected)
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100249 {
250 EXPECT_THAT(timestamps[index] - start, Ge(waitMin));
251 }
252 }
253};
254
255class TestDiscreteThresholdNoDwellTime : public TestDiscreteThresholdCommon
256{
257 public:
258 void SetUp() override
259 {
Szymon Dompke94f71c52021-12-10 07:16:33 +0100260 for (size_t idx = 0; idx < sensorMocks.size(); idx++)
261 {
262 ON_CALL(*sensorMocks.at(idx), getName())
263 .WillByDefault(Return(sensorNames[idx]));
264 }
265
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100266 sut = makeThreshold(0ms, GetParam().thresholdValue);
267 }
268};
269
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200270INSTANTIATE_TEST_SUITE_P(
271 _, TestDiscreteThresholdNoDwellTime,
272 Values(DiscreteParams()
273 .ThresholdValue("90.0")
274 .Updates({{0, 80.0}, {0, 89.0}})
275 .Expected({}),
276 DiscreteParams()
277 .ThresholdValue("90.0")
278 .Updates({{0, 80.0}, {0, 90.0}, {0, 80.0}, {0, 90.0}})
279 .Expected({{0, 90.0}, {0, 90.0}}),
280 DiscreteParams()
281 .ThresholdValue("90.0")
282 .Updates({{0, 90.0}, {0, 99.0}, {1, 100.0}, {1, 90.0}})
283 .Expected({{0, 90.0}, {1, 90.0}})));
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100284
285TEST_P(TestDiscreteThresholdNoDwellTime, senorsIsUpdatedMultipleTimes)
286{
287 testBodySensorIsUpdatedMultipleTimes();
288}
289
290class TestDiscreteThresholdWithDwellTime : public TestDiscreteThresholdCommon
291{
292 public:
293 void SetUp() override
294 {
Szymon Dompke94f71c52021-12-10 07:16:33 +0100295 for (size_t idx = 0; idx < sensorMocks.size(); idx++)
296 {
297 ON_CALL(*sensorMocks.at(idx), getName())
298 .WillByDefault(Return(sensorNames[idx]));
299 }
300
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100301 sut = makeThreshold(GetParam().dwellTime, GetParam().thresholdValue);
302 }
303};
304
305INSTANTIATE_TEST_SUITE_P(
306 _, TestDiscreteThresholdWithDwellTime,
307 Values(DiscreteParams()
308 .DwellTime(200ms)
Szymon Dompkeaa572362022-03-23 16:31:24 +0100309 .ThresholdValue("90.0")
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200310 .Updates({{0, 90.0, 100ms}, {0, 91.0}, {0, 90.0}})
311 .Expected({{0, 90.0, 300ms}}),
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100312 DiscreteParams()
313 .DwellTime(100ms)
Szymon Dompkeaa572362022-03-23 16:31:24 +0100314 .ThresholdValue("90.0")
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200315 .Updates({{0, 90.0, 100ms}})
316 .Expected({{0, 90.0, 100ms}}),
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100317 DiscreteParams()
318 .DwellTime(1000ms)
Szymon Dompkeaa572362022-03-23 16:31:24 +0100319 .ThresholdValue("90.0")
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200320 .Updates({{0, 90.0, 700ms},
321 {0, 91.0, 100ms},
322 {0, 90.0, 300ms},
323 {0, 91.0, 100ms}})
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100324 .Expected({}),
325 DiscreteParams()
326 .DwellTime(200ms)
Szymon Dompkeaa572362022-03-23 16:31:24 +0100327 .ThresholdValue("90.0")
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200328 .Updates({{0, 90.0},
329 {1, 89.0, 100ms},
330 {1, 90.0, 100ms},
331 {1, 89.0, 100ms},
332 {1, 90.0, 300ms},
333 {1, 89.0, 100ms}})
334 .Expected({{0, 90, 200ms}, {1, 90, 500ms}})));
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100335
336TEST_P(TestDiscreteThresholdWithDwellTime, senorsIsUpdatedMultipleTimes)
337{
338 testBodySensorIsUpdatedMultipleTimes();
339}