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