blob: 75a63c5378efca27a46cba3a4d461041830a5dd6 [file] [log] [blame]
Szymon Dompkef763c9e2021-03-12 09:19:22 +01001#include "dbus_environment.hpp"
2#include "helpers.hpp"
3#include "mocks/sensor_mock.hpp"
4#include "mocks/trigger_action_mock.hpp"
5#include "on_change_threshold.hpp"
6#include "utils/conv_container.hpp"
7
8#include <gmock/gmock.h>
9
10using namespace testing;
11using namespace std::chrono_literals;
12
13class TestOnChangeThreshold : public Test
14{
15 public:
16 std::vector<std::shared_ptr<SensorMock>> sensorMocks = {
17 std::make_shared<NiceMock<SensorMock>>(),
18 std::make_shared<NiceMock<SensorMock>>()};
19 std::vector<std::string> sensorNames = {"Sensor1", "Sensor2"};
20 std::unique_ptr<TriggerActionMock> actionMockPtr =
21 std::make_unique<StrictMock<TriggerActionMock>>();
22 TriggerActionMock& actionMock = *actionMockPtr;
23 std::shared_ptr<OnChangeThreshold> sut;
24
25 void SetUp() override
26 {
27 std::vector<std::unique_ptr<interfaces::TriggerAction>> actions;
28 actions.push_back(std::move(actionMockPtr));
29
30 sut = std::make_shared<OnChangeThreshold>(
31 utils::convContainer<std::shared_ptr<interfaces::Sensor>>(
32 sensorMocks),
33 sensorNames, std::move(actions));
34 }
35};
36
37TEST_F(TestOnChangeThreshold, initializeThresholdExpectAllSensorsAreRegistered)
38{
39 for (auto& sensor : sensorMocks)
40 {
41 EXPECT_CALL(*sensor,
42 registerForUpdates(Truly([sut = sut.get()](const auto& x) {
43 return x.lock().get() == sut;
44 })));
45 }
46
47 sut->initialize();
48}
49
50TEST_F(TestOnChangeThreshold, thresholdIsNotInitializeExpectNoActionCommit)
51{
52 EXPECT_CALL(actionMock, commit(_, _, _)).Times(0);
53}
54
55struct OnChangeParams
56{
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +010057 using UpdateParams = std::tuple<size_t, Milliseconds, double>;
58 using ExpectedParams = std::tuple<size_t, Milliseconds, double>;
Szymon Dompkef763c9e2021-03-12 09:19:22 +010059
60 OnChangeParams& Updates(std::vector<UpdateParams> val)
61 {
62 updates = std::move(val);
63 return *this;
64 }
65
66 OnChangeParams& Expected(std::vector<ExpectedParams> val)
67 {
68 expected = std::move(val);
69 return *this;
70 }
71
72 friend void PrintTo(const OnChangeParams& o, std::ostream* os)
73 {
74 *os << "{ Updates: ";
75 for (const auto& [index, timestamp, value] : o.updates)
76 {
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +010077 *os << "{ SensorIndex: " << index
78 << ", Timestamp: " << timestamp.count() << ", Value: " << value
79 << " }, ";
Szymon Dompkef763c9e2021-03-12 09:19:22 +010080 }
81 *os << "Expected: ";
82 for (const auto& [index, timestamp, value] : o.expected)
83 {
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +010084 *os << "{ SensorIndex: " << index
85 << ", Timestamp: " << timestamp.count() << ", Value: " << value
86 << " }, ";
Szymon Dompkef763c9e2021-03-12 09:19:22 +010087 }
88 *os << " }";
89 }
90
91 std::vector<UpdateParams> updates;
92 std::vector<ExpectedParams> expected;
93};
94
95class TestOnChangeThresholdUpdates :
96 public TestOnChangeThreshold,
97 public WithParamInterface<OnChangeParams>
98{};
99
100INSTANTIATE_TEST_SUITE_P(
101 _, TestOnChangeThresholdUpdates,
102 Values(
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +0100103 OnChangeParams().Updates({{0, 1ms, 80.0}}).Expected({{0, 1ms, 80.0}}),
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100104 OnChangeParams()
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +0100105 .Updates({{0, 1ms, 80.0}, {1, 2ms, 81.0}})
106 .Expected({{0, 1ms, 80.0}, {1, 2ms, 81.0}}),
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100107 OnChangeParams()
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +0100108 .Updates({{0, 1ms, 80.0}, {0, 2ms, 90.0}})
109 .Expected({{0, 1ms, 80.0}, {0, 2ms, 90.0}}),
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100110 OnChangeParams()
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +0100111 .Updates({{0, 1ms, 80.0}, {1, 2ms, 90.0}, {0, 3ms, 90.0}})
112 .Expected({{0, 1ms, 80.0}, {1, 2ms, 90.0}, {0, 3ms, 90.0}}),
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100113 OnChangeParams()
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +0100114 .Updates({{0, 1ms, 80.0},
115 {1, 2ms, 80.0},
116 {1, 3ms, 90.0},
117 {0, 4ms, 90.0}})
118 .Expected({{0, 1ms, 80.0},
119 {1, 2ms, 80.0},
120 {1, 3ms, 90.0},
121 {0, 4ms, 90.0}})));
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100122
123TEST_P(TestOnChangeThresholdUpdates, senorsIsUpdatedMultipleTimes)
124{
125 InSequence seq;
126 for (const auto& [index, timestamp, value] : GetParam().expected)
127 {
128 EXPECT_CALL(actionMock, commit(sensorNames[index], timestamp, value));
129 }
130
131 sut->initialize();
132 for (const auto& [index, timestamp, value] : GetParam().updates)
133 {
134 sut->sensorUpdated(*sensorMocks[index], timestamp, value);
135 }
136}