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, uint64_t, double>; 58 using ExpectedParams = std::tuple<size_t, uint64_t, 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 << ", Timestamp: " << timestamp 78 << ", Value: " << value << " }, "; 79 } 80 *os << "Expected: "; 81 for (const auto& [index, timestamp, value] : o.expected) 82 { 83 *os << "{ SensorIndex: " << index << ", Timestamp: " << timestamp 84 << ", Value: " << value << " }, "; 85 } 86 *os << " }"; 87 } 88 89 std::vector<UpdateParams> updates; 90 std::vector<ExpectedParams> expected; 91 }; 92 93 class TestOnChangeThresholdUpdates : 94 public TestOnChangeThreshold, 95 public WithParamInterface<OnChangeParams> 96 {}; 97 98 INSTANTIATE_TEST_SUITE_P( 99 _, TestOnChangeThresholdUpdates, 100 Values( 101 OnChangeParams().Updates({{0, 1, 80.0}}).Expected({{0, 1, 80.0}}), 102 OnChangeParams() 103 .Updates({{0, 1, 80.0}, {1, 2, 81.0}}) 104 .Expected({{0, 1, 80.0}, {1, 2, 81.0}}), 105 OnChangeParams() 106 .Updates({{0, 1, 80.0}, {0, 2, 90.0}}) 107 .Expected({{0, 1, 80.0}, {0, 2, 90.0}}), 108 OnChangeParams() 109 .Updates({{0, 1, 80.0}, {1, 2, 90.0}, {0, 3, 90.0}}) 110 .Expected({{0, 1, 80.0}, {1, 2, 90.0}, {0, 3, 90.0}}), 111 OnChangeParams() 112 .Updates({{0, 1, 80.0}, {1, 2, 80.0}, {1, 3, 90.0}, {0, 4, 90.0}}) 113 .Expected( 114 {{0, 1, 80.0}, {1, 2, 80.0}, {1, 3, 90.0}, {0, 4, 90.0}}))); 115 116 TEST_P(TestOnChangeThresholdUpdates, senorsIsUpdatedMultipleTimes) 117 { 118 InSequence seq; 119 for (const auto& [index, timestamp, value] : GetParam().expected) 120 { 121 EXPECT_CALL(actionMock, commit(sensorNames[index], timestamp, value)); 122 } 123 124 sut->initialize(); 125 for (const auto& [index, timestamp, value] : GetParam().updates) 126 { 127 sut->sensorUpdated(*sensorMocks[index], timestamp, value); 128 } 129 } 130