blob: 0c3b301b4343d08e3c334ad9bcee3cf31bccb618 [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
Szymon Dompke94f71c52021-12-10 07:16:33 +010030 for (size_t idx = 0; idx < sensorMocks.size(); idx++)
31 {
32 ON_CALL(*sensorMocks.at(idx), getName())
33 .WillByDefault(Return(sensorNames[idx]));
34 }
35
Szymon Dompkef763c9e2021-03-12 09:19:22 +010036 sut = std::make_shared<OnChangeThreshold>(
37 utils::convContainer<std::shared_ptr<interfaces::Sensor>>(
38 sensorMocks),
Szymon Dompke94f71c52021-12-10 07:16:33 +010039 std::move(actions));
Szymon Dompkef763c9e2021-03-12 09:19:22 +010040 }
41};
42
43TEST_F(TestOnChangeThreshold, initializeThresholdExpectAllSensorsAreRegistered)
44{
45 for (auto& sensor : sensorMocks)
46 {
47 EXPECT_CALL(*sensor,
48 registerForUpdates(Truly([sut = sut.get()](const auto& x) {
49 return x.lock().get() == sut;
50 })));
51 }
52
53 sut->initialize();
54}
55
56TEST_F(TestOnChangeThreshold, thresholdIsNotInitializeExpectNoActionCommit)
57{
58 EXPECT_CALL(actionMock, commit(_, _, _)).Times(0);
59}
60
Szymon Dompke94f71c52021-12-10 07:16:33 +010061TEST_F(TestOnChangeThreshold, getLabeledParamsReturnsCorrectly)
62{
63 LabeledThresholdParam expected = std::monostate();
64 EXPECT_EQ(sut->getThresholdParam(), expected);
65}
66
Szymon Dompkef763c9e2021-03-12 09:19:22 +010067struct OnChangeParams
68{
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +010069 using UpdateParams = std::tuple<size_t, Milliseconds, double>;
70 using ExpectedParams = std::tuple<size_t, Milliseconds, double>;
Szymon Dompkef763c9e2021-03-12 09:19:22 +010071
72 OnChangeParams& Updates(std::vector<UpdateParams> val)
73 {
74 updates = std::move(val);
75 return *this;
76 }
77
78 OnChangeParams& Expected(std::vector<ExpectedParams> val)
79 {
80 expected = std::move(val);
81 return *this;
82 }
83
84 friend void PrintTo(const OnChangeParams& o, std::ostream* os)
85 {
86 *os << "{ Updates: ";
87 for (const auto& [index, timestamp, value] : o.updates)
88 {
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +010089 *os << "{ SensorIndex: " << index
90 << ", Timestamp: " << timestamp.count() << ", Value: " << value
91 << " }, ";
Szymon Dompkef763c9e2021-03-12 09:19:22 +010092 }
93 *os << "Expected: ";
94 for (const auto& [index, timestamp, value] : o.expected)
95 {
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +010096 *os << "{ SensorIndex: " << index
97 << ", Timestamp: " << timestamp.count() << ", Value: " << value
98 << " }, ";
Szymon Dompkef763c9e2021-03-12 09:19:22 +010099 }
100 *os << " }";
101 }
102
103 std::vector<UpdateParams> updates;
104 std::vector<ExpectedParams> expected;
105};
106
107class TestOnChangeThresholdUpdates :
108 public TestOnChangeThreshold,
109 public WithParamInterface<OnChangeParams>
110{};
111
112INSTANTIATE_TEST_SUITE_P(
113 _, TestOnChangeThresholdUpdates,
114 Values(
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +0100115 OnChangeParams().Updates({{0, 1ms, 80.0}}).Expected({{0, 1ms, 80.0}}),
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100116 OnChangeParams()
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +0100117 .Updates({{0, 1ms, 80.0}, {1, 2ms, 81.0}})
118 .Expected({{0, 1ms, 80.0}, {1, 2ms, 81.0}}),
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100119 OnChangeParams()
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +0100120 .Updates({{0, 1ms, 80.0}, {0, 2ms, 90.0}})
121 .Expected({{0, 1ms, 80.0}, {0, 2ms, 90.0}}),
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100122 OnChangeParams()
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +0100123 .Updates({{0, 1ms, 80.0}, {1, 2ms, 90.0}, {0, 3ms, 90.0}})
124 .Expected({{0, 1ms, 80.0}, {1, 2ms, 90.0}, {0, 3ms, 90.0}}),
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100125 OnChangeParams()
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +0100126 .Updates({{0, 1ms, 80.0},
127 {1, 2ms, 80.0},
128 {1, 3ms, 90.0},
129 {0, 4ms, 90.0}})
130 .Expected({{0, 1ms, 80.0},
131 {1, 2ms, 80.0},
132 {1, 3ms, 90.0},
133 {0, 4ms, 90.0}})));
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100134
135TEST_P(TestOnChangeThresholdUpdates, senorsIsUpdatedMultipleTimes)
136{
137 InSequence seq;
138 for (const auto& [index, timestamp, value] : GetParam().expected)
139 {
140 EXPECT_CALL(actionMock, commit(sensorNames[index], timestamp, value));
141 }
142
143 sut->initialize();
144 for (const auto& [index, timestamp, value] : GetParam().updates)
145 {
146 sut->sensorUpdated(*sensorMocks[index], timestamp, value);
147 }
148}