blob: a57e1234d4d72000e4231156042702e504e6a348 [file] [log] [blame]
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +00001#include "helpers.hpp"
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +01002#include "interfaces/sensor.hpp"
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +02003#include "metric.hpp"
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +01004#include "mocks/sensor_mock.hpp"
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +01005#include "utils/conv_container.hpp"
6
7#include <gmock/gmock.h>
8
9using namespace testing;
10
11using Timestamp = uint64_t;
12
13class TestMetric : 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::make_shared<NiceMock<SensorMock>>()};
20
21 std::shared_ptr<Metric> sut = std::make_shared<Metric>(
22 utils::convContainer<std::shared_ptr<interfaces::Sensor>>(sensorMocks),
23 "op", "id", "metadata");
24};
25
26TEST_F(TestMetric, subscribesForSensorDuringInitialization)
27{
28 for (auto& sensor : sensorMocks)
29 {
30 EXPECT_CALL(*sensor,
31 registerForUpdates(Truly([sut = sut.get()](const auto& a0) {
32 return a0.lock().get() == sut;
33 })));
34 }
35
36 sut->initialize();
37}
38
39TEST_F(TestMetric, containsNoReadingsWhenNotInitialized)
40{
41 ASSERT_THAT(sut->getReadings(), ElementsAre());
42}
43
44TEST_F(TestMetric, containsEmptyReadingsAfterInitialize)
45{
46 sut->initialize();
47
48 ASSERT_THAT(sut->getReadings(),
49 ElementsAre(MetricValue{"id", "metadata", 0., 0u},
50 MetricValue{"id", "metadata", 0., 0u},
51 MetricValue{"id", "metadata", 0., 0u}));
52}
53
54TEST_F(TestMetric, throwsWhenUpdateIsPerformedWhenNotInitialized)
55{
56 EXPECT_THROW(sut->sensorUpdated(*sensorMocks[0], Timestamp{10}),
57 std::out_of_range);
58 EXPECT_THROW(sut->sensorUpdated(*sensorMocks[1], Timestamp{10}, 20.0),
59 std::out_of_range);
60}
61
62TEST_F(TestMetric, updatesMetricValuesOnSensorUpdate)
63{
64 sut->initialize();
65
66 sut->sensorUpdated(*sensorMocks[2], Timestamp{18}, 31.0);
67 sut->sensorUpdated(*sensorMocks[0], Timestamp{21});
68
69 ASSERT_THAT(sut->getReadings(),
70 ElementsAre(MetricValue{"id", "metadata", 0., 21u},
71 MetricValue{"id", "metadata", 0., 0u},
72 MetricValue{"id", "metadata", 31., 18u}));
73}
74
75TEST_F(TestMetric, throwsWhenUpdateIsPerformedOnUnknownSensor)
76{
77 sut->initialize();
78
79 auto sensor = std::make_shared<StrictMock<SensorMock>>();
80 EXPECT_THROW(sut->sensorUpdated(*sensor, Timestamp{10}), std::out_of_range);
81 EXPECT_THROW(sut->sensorUpdated(*sensor, Timestamp{10}, 20.0),
82 std::out_of_range);
83}
84
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000085TEST_F(TestMetric, containsIdInConfigurationDump)
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010086{
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000087 const auto conf = sut->dumpConfiguration();
88
89 EXPECT_THAT(conf.at_label<utils::tstring::Id>(), Eq("id"));
90 EXPECT_THAT(conf.to_json().at("id"), Eq(nlohmann::json("id")));
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010091}
92
93TEST_F(TestMetric, containsOpInJsonDump)
94{
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000095 const auto conf = sut->dumpConfiguration();
96
97 EXPECT_THAT(conf.at_label<utils::tstring::OperationType>(), Eq("op"));
98 EXPECT_THAT(conf.to_json().at("operationType"), Eq(nlohmann::json("op")));
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010099}
100
101TEST_F(TestMetric, containsMetadataInJsonDump)
102{
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000103 const auto conf = sut->dumpConfiguration();
104
105 EXPECT_THAT(conf.at_label<utils::tstring::MetricMetadata>(),
106 Eq("metadata"));
107 EXPECT_THAT(conf.to_json().at("metricMetadata"),
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100108 Eq(nlohmann::json("metadata")));
109}
110
111TEST_F(TestMetric, containsSensorPathsInJsonDump)
112{
113 for (size_t i = 0; i < sensorMocks.size(); ++i)
114 {
115 const auto no = std::to_string(i);
116 ON_CALL(*sensorMocks[i], id())
117 .WillByDefault(
118 Return(SensorMock::makeId("service" + no, "path" + no)));
119 }
120
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000121 const auto conf = sut->dumpConfiguration();
122
123 EXPECT_THAT(conf.at_label<utils::tstring::SensorPaths>(),
124 ElementsAre(LabeledSensorParameters("service0", "path0"),
125 LabeledSensorParameters("service1", "path1"),
126 LabeledSensorParameters("service2", "path2")));
127 EXPECT_THAT(
128 conf.to_json().at("sensorPaths"),
129 Eq(nlohmann::json({{{"service", "service0"}, {"path", "path0"}},
130 {{"service", "service1"}, {"path", "path1"}},
131 {{"service", "service2"}, {"path", "path2"}}})));
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100132}