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 { 57 using UpdateParams = std::tuple<size_t, Milliseconds, double>; 58 using ExpectedParams = std::tuple<size_t, Milliseconds, double>; 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 { 77 *os << "{ SensorIndex: " << index 78 << ", Timestamp: " << timestamp.count() << ", Value: " << value 79 << " }, "; 80 } 81 *os << "Expected: "; 82 for (const auto& [index, timestamp, value] : o.expected) 83 { 84 *os << "{ SensorIndex: " << index 85 << ", Timestamp: " << timestamp.count() << ", Value: " << value 86 << " }, "; 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( 103 OnChangeParams().Updates({{0, 1ms, 80.0}}).Expected({{0, 1ms, 80.0}}), 104 OnChangeParams() 105 .Updates({{0, 1ms, 80.0}, {1, 2ms, 81.0}}) 106 .Expected({{0, 1ms, 80.0}, {1, 2ms, 81.0}}), 107 OnChangeParams() 108 .Updates({{0, 1ms, 80.0}, {0, 2ms, 90.0}}) 109 .Expected({{0, 1ms, 80.0}, {0, 2ms, 90.0}}), 110 OnChangeParams() 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}}), 113 OnChangeParams() 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}}))); 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 } 137