Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 1 | #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 | |
| 10 | using namespace testing; |
| 11 | using namespace std::chrono_literals; |
| 12 | |
| 13 | class 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 | |
| 37 | TEST_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 | |
| 50 | TEST_F(TestOnChangeThreshold, thresholdIsNotInitializeExpectNoActionCommit) |
| 51 | { |
| 52 | EXPECT_CALL(actionMock, commit(_, _, _)).Times(0); |
| 53 | } |
| 54 | |
| 55 | struct OnChangeParams |
| 56 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 57 | using UpdateParams = std::tuple<size_t, Milliseconds, double>; |
| 58 | using ExpectedParams = std::tuple<size_t, Milliseconds, double>; |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 59 | |
| 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 Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 77 | *os << "{ SensorIndex: " << index |
| 78 | << ", Timestamp: " << timestamp.count() << ", Value: " << value |
| 79 | << " }, "; |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 80 | } |
| 81 | *os << "Expected: "; |
| 82 | for (const auto& [index, timestamp, value] : o.expected) |
| 83 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 84 | *os << "{ SensorIndex: " << index |
| 85 | << ", Timestamp: " << timestamp.count() << ", Value: " << value |
| 86 | << " }, "; |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 87 | } |
| 88 | *os << " }"; |
| 89 | } |
| 90 | |
| 91 | std::vector<UpdateParams> updates; |
| 92 | std::vector<ExpectedParams> expected; |
| 93 | }; |
| 94 | |
| 95 | class TestOnChangeThresholdUpdates : |
| 96 | public TestOnChangeThreshold, |
| 97 | public WithParamInterface<OnChangeParams> |
| 98 | {}; |
| 99 | |
| 100 | INSTANTIATE_TEST_SUITE_P( |
| 101 | _, TestOnChangeThresholdUpdates, |
| 102 | Values( |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 103 | OnChangeParams().Updates({{0, 1ms, 80.0}}).Expected({{0, 1ms, 80.0}}), |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 104 | OnChangeParams() |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 105 | .Updates({{0, 1ms, 80.0}, {1, 2ms, 81.0}}) |
| 106 | .Expected({{0, 1ms, 80.0}, {1, 2ms, 81.0}}), |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 107 | OnChangeParams() |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 108 | .Updates({{0, 1ms, 80.0}, {0, 2ms, 90.0}}) |
| 109 | .Expected({{0, 1ms, 80.0}, {0, 2ms, 90.0}}), |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 110 | OnChangeParams() |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 111 | .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 Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 113 | OnChangeParams() |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 114 | .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 Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 122 | |
| 123 | TEST_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 | } |