Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 1 | #include "helpers.hpp" |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 2 | #include "metric.hpp" |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 3 | #include "mocks/sensor_mock.hpp" |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 4 | #include "params/metric_params.hpp" |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 5 | #include "utils/conv_container.hpp" |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 6 | #include "utils/conversion.hpp" |
| 7 | #include "utils/tstring.hpp" |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 8 | |
| 9 | #include <gmock/gmock.h> |
| 10 | |
| 11 | using namespace testing; |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 12 | using namespace std::chrono_literals; |
| 13 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 14 | namespace tstring = utils::tstring; |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 15 | |
| 16 | using Timestamp = uint64_t; |
| 17 | |
| 18 | class TestMetric : public Test |
| 19 | { |
| 20 | public: |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 21 | static std::vector<std::shared_ptr<SensorMock>> |
| 22 | makeSensorMocks(size_t amount) |
| 23 | { |
| 24 | std::vector<std::shared_ptr<SensorMock>> result; |
| 25 | for (size_t i = 0; i < amount; ++i) |
| 26 | { |
| 27 | result.emplace_back(std::make_shared<NiceMock<SensorMock>>()); |
| 28 | } |
| 29 | return result; |
| 30 | } |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 31 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 32 | std::shared_ptr<Metric> makeSut(const MetricParams& p) |
| 33 | { |
| 34 | return std::make_shared<Metric>( |
| 35 | utils::convContainer<std::shared_ptr<interfaces::Sensor>>( |
| 36 | sensorMocks), |
| 37 | p.operationType(), p.id(), p.metadata(), p.collectionTimeScope(), |
| 38 | p.collectionDuration()); |
| 39 | } |
| 40 | |
| 41 | MetricParams params = |
| 42 | MetricParams() |
| 43 | .id("id") |
| 44 | .metadata("metadata") |
| 45 | .operationType(OperationType::avg) |
| 46 | .collectionTimeScope(CollectionTimeScope::interval) |
| 47 | .collectionDuration(CollectionDuration(42ms)); |
| 48 | std::vector<std::shared_ptr<SensorMock>> sensorMocks = makeSensorMocks(1u); |
| 49 | std::shared_ptr<Metric> sut; |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | TEST_F(TestMetric, subscribesForSensorDuringInitialization) |
| 53 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 54 | sut = makeSut(params); |
| 55 | |
| 56 | EXPECT_CALL(*sensorMocks.front(), |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 57 | registerForUpdates(Truly([sut = sut.get()](const auto& a0) { |
| 58 | return a0.lock().get() == sut; |
| 59 | }))); |
| 60 | |
| 61 | sut->initialize(); |
| 62 | } |
| 63 | |
| 64 | TEST_F(TestMetric, containsEmptyReadingAfterCreated) |
| 65 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 66 | sut = makeSut(params); |
| 67 | |
| 68 | ASSERT_THAT(sut->getReadings(), |
| 69 | ElementsAre(MetricValue({"id", "metadata", 0., 0u}))); |
| 70 | } |
| 71 | |
| 72 | TEST_F(TestMetric, parsesSensorMetadata) |
| 73 | { |
Szymon Dompke | 3a61702 | 2021-07-19 18:23:02 +0200 | [diff] [blame] | 74 | using ReadingMetadata = |
| 75 | utils::LabeledTuple<std::tuple<std::string, std::string>, |
| 76 | utils::tstring::SensorDbusPath, |
| 77 | utils::tstring::SensorRedfishUri>; |
| 78 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 79 | nlohmann::json metadata; |
| 80 | metadata["MetricProperties"] = {"sensor1", "sensor2"}; |
| 81 | |
| 82 | sensorMocks = makeSensorMocks(2); |
| 83 | sut = makeSut(params.metadata(metadata.dump())); |
| 84 | |
Szymon Dompke | 3a61702 | 2021-07-19 18:23:02 +0200 | [diff] [blame] | 85 | EXPECT_THAT( |
| 86 | sut->getReadings(), |
| 87 | ElementsAre( |
| 88 | MetricValue{"id", ReadingMetadata("", "sensor1").dump(), 0., 0u}, |
| 89 | MetricValue{"id", ReadingMetadata("", "sensor2").dump(), 0., 0u})); |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | TEST_F(TestMetric, parsesSensorMetadataWhenMoreMetadataThanSensors) |
| 93 | { |
| 94 | nlohmann::json metadata; |
| 95 | metadata["MetricProperties"] = {"sensor1", "sensor2"}; |
| 96 | |
| 97 | sensorMocks = makeSensorMocks(1); |
| 98 | sut = makeSut(params.metadata(metadata.dump())); |
| 99 | |
| 100 | EXPECT_THAT(sut->getReadings(), |
| 101 | ElementsAre(MetricValue{"id", metadata.dump(), 0., 0u})); |
| 102 | } |
| 103 | |
| 104 | TEST_F(TestMetric, parsesSensorMetadataWhenMoreSensorsThanMetadata) |
| 105 | { |
| 106 | nlohmann::json metadata; |
| 107 | metadata["MetricProperties"] = {"sensor1"}; |
| 108 | |
| 109 | sensorMocks = makeSensorMocks(2); |
| 110 | sut = makeSut(params.metadata(metadata.dump())); |
| 111 | |
| 112 | EXPECT_THAT(sut->getReadings(), |
| 113 | ElementsAre(MetricValue{"id", metadata.dump(), 0., 0u}, |
| 114 | MetricValue{"id", metadata.dump(), 0., 0u})); |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | class TestMetricAfterInitialization : public TestMetric |
| 118 | { |
| 119 | public: |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 120 | void SetUp() override |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 121 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 122 | sut = makeSut(params); |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 123 | sut->initialize(); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 124 | } |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 125 | }; |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 126 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 127 | TEST_F(TestMetricAfterInitialization, containsEmptyReading) |
| 128 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 129 | ASSERT_THAT(sut->getReadings(), |
| 130 | ElementsAre(MetricValue({"id", "metadata", 0., 0u}))); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 131 | } |
| 132 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 133 | TEST_F(TestMetricAfterInitialization, updatesMetricValuesOnSensorUpdate) |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 134 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 135 | sut->sensorUpdated(*sensorMocks.front(), Timestamp{18}, 31.2); |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 136 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 137 | ASSERT_THAT(sut->getReadings(), |
| 138 | ElementsAre(MetricValue{"id", "metadata", 31.2, 18u})); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 139 | } |
| 140 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 141 | TEST_F(TestMetricAfterInitialization, |
| 142 | throwsWhenUpdateIsPerformedOnUnknownSensor) |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 143 | { |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 144 | auto sensor = std::make_shared<StrictMock<SensorMock>>(); |
| 145 | EXPECT_THROW(sut->sensorUpdated(*sensor, Timestamp{10}), std::out_of_range); |
| 146 | EXPECT_THROW(sut->sensorUpdated(*sensor, Timestamp{10}, 20.0), |
| 147 | std::out_of_range); |
| 148 | } |
| 149 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 150 | TEST_F(TestMetricAfterInitialization, dumpsConfiguration) |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 151 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 152 | namespace ts = utils::tstring; |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 153 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 154 | ON_CALL(*sensorMocks.front(), id()) |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 155 | .WillByDefault(Return(SensorMock::makeId("service1", "path1"))); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 156 | |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 157 | const auto conf = sut->dumpConfiguration(); |
| 158 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 159 | LabeledMetricParameters expected = {}; |
| 160 | expected.at_label<ts::Id>() = "id"; |
| 161 | expected.at_label<ts::MetricMetadata>() = "metadata"; |
| 162 | expected.at_label<ts::OperationType>() = OperationType::avg; |
| 163 | expected.at_label<ts::CollectionTimeScope>() = |
| 164 | CollectionTimeScope::interval; |
| 165 | expected.at_label<ts::CollectionDuration>() = CollectionDuration(42ms); |
| 166 | expected.at_label<ts::SensorPath>() = { |
| 167 | LabeledSensorParameters("service1", "path1")}; |
| 168 | |
| 169 | EXPECT_THAT(conf, Eq(expected)); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 170 | } |