blob: 6c685138056c2f9ef4fe46005bf98fe38cf0d6c7 [file] [log] [blame]
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +00001#include "helpers.hpp"
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +02002#include "metric.hpp"
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +01003#include "mocks/sensor_mock.hpp"
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +00004#include "params/metric_params.hpp"
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +01005#include "utils/conv_container.hpp"
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +00006#include "utils/conversion.hpp"
7#include "utils/tstring.hpp"
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +01008
9#include <gmock/gmock.h>
10
11using namespace testing;
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000012using namespace std::chrono_literals;
13
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000014namespace tstring = utils::tstring;
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010015
16using Timestamp = uint64_t;
17
18class TestMetric : public Test
19{
20 public:
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000021 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 Grobelny6ccfcbf2020-11-04 09:31:36 +010031
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000032 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 Grobelny6ccfcbf2020-11-04 09:31:36 +010050};
51
52TEST_F(TestMetric, subscribesForSensorDuringInitialization)
53{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000054 sut = makeSut(params);
55
56 EXPECT_CALL(*sensorMocks.front(),
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000057 registerForUpdates(Truly([sut = sut.get()](const auto& a0) {
58 return a0.lock().get() == sut;
59 })));
60
61 sut->initialize();
62}
63
64TEST_F(TestMetric, containsEmptyReadingAfterCreated)
65{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000066 sut = makeSut(params);
67
68 ASSERT_THAT(sut->getReadings(),
69 ElementsAre(MetricValue({"id", "metadata", 0., 0u})));
70}
71
72TEST_F(TestMetric, parsesSensorMetadata)
73{
Szymon Dompke3a617022021-07-19 18:23:02 +020074 using ReadingMetadata =
75 utils::LabeledTuple<std::tuple<std::string, std::string>,
76 utils::tstring::SensorDbusPath,
77 utils::tstring::SensorRedfishUri>;
78
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000079 nlohmann::json metadata;
80 metadata["MetricProperties"] = {"sensor1", "sensor2"};
81
82 sensorMocks = makeSensorMocks(2);
83 sut = makeSut(params.metadata(metadata.dump()));
84
Szymon Dompke3a617022021-07-19 18:23:02 +020085 EXPECT_THAT(
86 sut->getReadings(),
87 ElementsAre(
88 MetricValue{"id", ReadingMetadata("", "sensor1").dump(), 0., 0u},
89 MetricValue{"id", ReadingMetadata("", "sensor2").dump(), 0., 0u}));
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000090}
91
92TEST_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
104TEST_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 Grobelnye8fc5752021-02-05 14:30:45 +0000115}
116
117class TestMetricAfterInitialization : public TestMetric
118{
119 public:
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000120 void SetUp() override
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100121 {
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000122 sut = makeSut(params);
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000123 sut->initialize();
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100124 }
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000125};
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100126
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000127TEST_F(TestMetricAfterInitialization, containsEmptyReading)
128{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000129 ASSERT_THAT(sut->getReadings(),
130 ElementsAre(MetricValue({"id", "metadata", 0., 0u})));
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100131}
132
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000133TEST_F(TestMetricAfterInitialization, updatesMetricValuesOnSensorUpdate)
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100134{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000135 sut->sensorUpdated(*sensorMocks.front(), Timestamp{18}, 31.2);
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000136
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000137 ASSERT_THAT(sut->getReadings(),
138 ElementsAre(MetricValue{"id", "metadata", 31.2, 18u}));
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100139}
140
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000141TEST_F(TestMetricAfterInitialization,
142 throwsWhenUpdateIsPerformedOnUnknownSensor)
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100143{
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100144 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 Grobelnydcc4e192021-03-08 09:09:34 +0000150TEST_F(TestMetricAfterInitialization, dumpsConfiguration)
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100151{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000152 namespace ts = utils::tstring;
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000153
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000154 ON_CALL(*sensorMocks.front(), id())
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000155 .WillByDefault(Return(SensorMock::makeId("service1", "path1")));
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100156
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000157 const auto conf = sut->dumpConfiguration();
158
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000159 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 Grobelny6ccfcbf2020-11-04 09:31:36 +0100170}