blob: e91750debe255fdb45d946b7bfeb82961c5cc7d2 [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"
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +00005#include "utils/conversion.hpp"
6#include "utils/tstring.hpp"
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +01007
8#include <gmock/gmock.h>
9
10using namespace testing;
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000011namespace tstring = utils::tstring;
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010012
13using Timestamp = uint64_t;
14
15class TestMetric : public Test
16{
17 public:
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000018 std::shared_ptr<SensorMock> sensorMock =
19 std::make_shared<NiceMock<SensorMock>>();
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010020
21 std::shared_ptr<Metric> sut = std::make_shared<Metric>(
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000022 sensorMock, OperationType::avg, "id", "metadata");
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010023};
24
25TEST_F(TestMetric, subscribesForSensorDuringInitialization)
26{
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000027 EXPECT_CALL(*sensorMock,
28 registerForUpdates(Truly([sut = sut.get()](const auto& a0) {
29 return a0.lock().get() == sut;
30 })));
31
32 sut->initialize();
33}
34
35TEST_F(TestMetric, containsEmptyReadingAfterCreated)
36{
37 ASSERT_THAT(sut->getReading(), MetricValue({"id", "metadata", 0., 0u}));
38}
39
40class TestMetricAfterInitialization : public TestMetric
41{
42 public:
43 TestMetricAfterInitialization()
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010044 {
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000045 sut->initialize();
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010046 }
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000047};
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010048
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000049TEST_F(TestMetricAfterInitialization, containsEmptyReading)
50{
51 ASSERT_THAT(sut->getReading(), MetricValue({"id", "metadata", 0., 0u}));
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010052}
53
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000054TEST_F(TestMetricAfterInitialization, updatesMetricValuesOnSensorUpdate)
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010055{
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000056 sut->sensorUpdated(*sensorMock, Timestamp{18}, 31.2);
57
58 ASSERT_THAT(sut->getReading(),
59 Eq(MetricValue{"id", "metadata", 31.2, 18u}));
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010060}
61
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000062TEST_F(TestMetricAfterInitialization,
63 throwsWhenUpdateIsPerformedOnUnknownSensor)
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010064{
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010065 auto sensor = std::make_shared<StrictMock<SensorMock>>();
66 EXPECT_THROW(sut->sensorUpdated(*sensor, Timestamp{10}), std::out_of_range);
67 EXPECT_THROW(sut->sensorUpdated(*sensor, Timestamp{10}, 20.0),
68 std::out_of_range);
69}
70
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000071TEST_F(TestMetricAfterInitialization, containsIdInConfigurationDump)
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010072{
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000073 const auto conf = sut->dumpConfiguration();
74
75 EXPECT_THAT(conf.at_label<utils::tstring::Id>(), Eq("id"));
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000076 EXPECT_THAT(conf.to_json().at(tstring::Id::str()),
77 Eq(nlohmann::json("id")));
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010078}
79
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000080TEST_F(TestMetricAfterInitialization, containsOpInJsonDump)
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010081{
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000082 const auto conf = sut->dumpConfiguration();
83
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000084 EXPECT_THAT(conf.at_label<utils::tstring::OperationType>(),
85 Eq(OperationType::avg));
86 EXPECT_THAT(conf.to_json().at(tstring::OperationType::str()),
87 Eq(nlohmann::json(utils::toUnderlying(OperationType::avg))));
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010088}
89
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000090TEST_F(TestMetricAfterInitialization, containsMetadataInJsonDump)
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010091{
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000092 const auto conf = sut->dumpConfiguration();
93
94 EXPECT_THAT(conf.at_label<utils::tstring::MetricMetadata>(),
95 Eq("metadata"));
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000096 EXPECT_THAT(conf.to_json().at(tstring::MetricMetadata::str()),
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010097 Eq(nlohmann::json("metadata")));
98}
99
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000100TEST_F(TestMetricAfterInitialization, containsSensorPathInJsonDump)
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100101{
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000102 ON_CALL(*sensorMock, id())
103 .WillByDefault(Return(SensorMock::makeId("service1", "path1")));
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100104
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000105 const auto conf = sut->dumpConfiguration();
106
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000107 EXPECT_THAT(conf.at_label<utils::tstring::SensorPath>(),
108 Eq(LabeledSensorParameters("service1", "path1")));
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000109 EXPECT_THAT(
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000110 conf.to_json().at(tstring::SensorPath::str()),
111 Eq(nlohmann::json({{"service", "service1"}, {"path", "path1"}})));
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100112}