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 for (size_t idx = 0; idx < sensorMocks.size(); idx++) 31 { 32 ON_CALL(*sensorMocks.at(idx), getName()) 33 .WillByDefault(Return(sensorNames[idx])); 34 } 35 36 sut = std::make_shared<OnChangeThreshold>( 37 utils::convContainer<std::shared_ptr<interfaces::Sensor>>( 38 sensorMocks), 39 std::move(actions)); 40 } 41 }; 42 43 TEST_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 56 TEST_F(TestOnChangeThreshold, thresholdIsNotInitializeExpectNoActionCommit) 57 { 58 EXPECT_CALL(actionMock, commit(_, _, _)).Times(0); 59 } 60 61 TEST_F(TestOnChangeThreshold, getLabeledParamsReturnsCorrectly) 62 { 63 LabeledThresholdParam expected = std::monostate(); 64 EXPECT_EQ(sut->getThresholdParam(), expected); 65 } 66 67 struct OnChangeParams 68 { 69 using UpdateParams = std::tuple<size_t, Milliseconds, double>; 70 using ExpectedParams = std::tuple<size_t, Milliseconds, double>; 71 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 { 89 *os << "{ SensorIndex: " << index 90 << ", Timestamp: " << timestamp.count() << ", Value: " << value 91 << " }, "; 92 } 93 *os << "Expected: "; 94 for (const auto& [index, timestamp, value] : o.expected) 95 { 96 *os << "{ SensorIndex: " << index 97 << ", Timestamp: " << timestamp.count() << ", Value: " << value 98 << " }, "; 99 } 100 *os << " }"; 101 } 102 103 std::vector<UpdateParams> updates; 104 std::vector<ExpectedParams> expected; 105 }; 106 107 class TestOnChangeThresholdUpdates : 108 public TestOnChangeThreshold, 109 public WithParamInterface<OnChangeParams> 110 {}; 111 112 INSTANTIATE_TEST_SUITE_P( 113 _, TestOnChangeThresholdUpdates, 114 Values( 115 OnChangeParams().Updates({{0, 1ms, 80.0}}).Expected({{0, 1ms, 80.0}}), 116 OnChangeParams() 117 .Updates({{0, 1ms, 80.0}, {1, 2ms, 81.0}}) 118 .Expected({{0, 1ms, 80.0}, {1, 2ms, 81.0}}), 119 OnChangeParams() 120 .Updates({{0, 1ms, 80.0}, {0, 2ms, 90.0}}) 121 .Expected({{0, 1ms, 80.0}, {0, 2ms, 90.0}}), 122 OnChangeParams() 123 .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}}), 125 OnChangeParams() 126 .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}}))); 134 135 TEST_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 } 149