blob: 9006962581f4f38049e1abf45d41e1fe6ae2d765 [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{
74 nlohmann::json metadata;
75 metadata["MetricProperties"] = {"sensor1", "sensor2"};
76
77 sensorMocks = makeSensorMocks(2);
78 sut = makeSut(params.metadata(metadata.dump()));
79
80 EXPECT_THAT(sut->getReadings(),
81 ElementsAre(MetricValue{"id", "sensor1", 0., 0u},
82 MetricValue{"id", "sensor2", 0., 0u}));
83}
84
85TEST_F(TestMetric, parsesSensorMetadataWhenMoreMetadataThanSensors)
86{
87 nlohmann::json metadata;
88 metadata["MetricProperties"] = {"sensor1", "sensor2"};
89
90 sensorMocks = makeSensorMocks(1);
91 sut = makeSut(params.metadata(metadata.dump()));
92
93 EXPECT_THAT(sut->getReadings(),
94 ElementsAre(MetricValue{"id", metadata.dump(), 0., 0u}));
95}
96
97TEST_F(TestMetric, parsesSensorMetadataWhenMoreSensorsThanMetadata)
98{
99 nlohmann::json metadata;
100 metadata["MetricProperties"] = {"sensor1"};
101
102 sensorMocks = makeSensorMocks(2);
103 sut = makeSut(params.metadata(metadata.dump()));
104
105 EXPECT_THAT(sut->getReadings(),
106 ElementsAre(MetricValue{"id", metadata.dump(), 0., 0u},
107 MetricValue{"id", metadata.dump(), 0., 0u}));
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000108}
109
110class TestMetricAfterInitialization : public TestMetric
111{
112 public:
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000113 void SetUp() override
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100114 {
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000115 sut = makeSut(params);
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000116 sut->initialize();
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100117 }
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000118};
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100119
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000120TEST_F(TestMetricAfterInitialization, containsEmptyReading)
121{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000122 ASSERT_THAT(sut->getReadings(),
123 ElementsAre(MetricValue({"id", "metadata", 0., 0u})));
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100124}
125
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000126TEST_F(TestMetricAfterInitialization, updatesMetricValuesOnSensorUpdate)
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100127{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000128 sut->sensorUpdated(*sensorMocks.front(), Timestamp{18}, 31.2);
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000129
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000130 ASSERT_THAT(sut->getReadings(),
131 ElementsAre(MetricValue{"id", "metadata", 31.2, 18u}));
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100132}
133
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000134TEST_F(TestMetricAfterInitialization,
135 throwsWhenUpdateIsPerformedOnUnknownSensor)
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100136{
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100137 auto sensor = std::make_shared<StrictMock<SensorMock>>();
138 EXPECT_THROW(sut->sensorUpdated(*sensor, Timestamp{10}), std::out_of_range);
139 EXPECT_THROW(sut->sensorUpdated(*sensor, Timestamp{10}, 20.0),
140 std::out_of_range);
141}
142
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000143TEST_F(TestMetricAfterInitialization, dumpsConfiguration)
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100144{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000145 namespace ts = utils::tstring;
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000146
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000147 ON_CALL(*sensorMocks.front(), id())
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000148 .WillByDefault(Return(SensorMock::makeId("service1", "path1")));
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100149
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000150 const auto conf = sut->dumpConfiguration();
151
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000152 LabeledMetricParameters expected = {};
153 expected.at_label<ts::Id>() = "id";
154 expected.at_label<ts::MetricMetadata>() = "metadata";
155 expected.at_label<ts::OperationType>() = OperationType::avg;
156 expected.at_label<ts::CollectionTimeScope>() =
157 CollectionTimeScope::interval;
158 expected.at_label<ts::CollectionDuration>() = CollectionDuration(42ms);
159 expected.at_label<ts::SensorPath>() = {
160 LabeledSensorParameters("service1", "path1")};
161
162 EXPECT_THAT(conf, Eq(expected));
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100163}