blob: 786f0251d9101f38cf8e739347e24067567aee0a [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 Grobelny753e4b32021-02-11 12:58:58 +000011using namespace std::chrono_literals;
12
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000013namespace tstring = utils::tstring;
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010014
15using Timestamp = uint64_t;
16
17class TestMetric : public Test
18{
19 public:
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000020 std::shared_ptr<SensorMock> sensorMock =
21 std::make_shared<NiceMock<SensorMock>>();
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010022
23 std::shared_ptr<Metric> sut = std::make_shared<Metric>(
Krzysztof Grobelny753e4b32021-02-11 12:58:58 +000024 sensorMock, OperationType::avg, "id", "metadata",
25 CollectionTimeScope::point, CollectionDuration(0ms));
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010026};
27
28TEST_F(TestMetric, subscribesForSensorDuringInitialization)
29{
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000030 EXPECT_CALL(*sensorMock,
31 registerForUpdates(Truly([sut = sut.get()](const auto& a0) {
32 return a0.lock().get() == sut;
33 })));
34
35 sut->initialize();
36}
37
38TEST_F(TestMetric, containsEmptyReadingAfterCreated)
39{
40 ASSERT_THAT(sut->getReading(), MetricValue({"id", "metadata", 0., 0u}));
41}
42
43class TestMetricAfterInitialization : public TestMetric
44{
45 public:
46 TestMetricAfterInitialization()
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010047 {
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000048 sut->initialize();
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010049 }
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000050};
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010051
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000052TEST_F(TestMetricAfterInitialization, containsEmptyReading)
53{
54 ASSERT_THAT(sut->getReading(), MetricValue({"id", "metadata", 0., 0u}));
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010055}
56
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000057TEST_F(TestMetricAfterInitialization, updatesMetricValuesOnSensorUpdate)
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010058{
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000059 sut->sensorUpdated(*sensorMock, Timestamp{18}, 31.2);
60
61 ASSERT_THAT(sut->getReading(),
62 Eq(MetricValue{"id", "metadata", 31.2, 18u}));
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010063}
64
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000065TEST_F(TestMetricAfterInitialization,
66 throwsWhenUpdateIsPerformedOnUnknownSensor)
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010067{
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010068 auto sensor = std::make_shared<StrictMock<SensorMock>>();
69 EXPECT_THROW(sut->sensorUpdated(*sensor, Timestamp{10}), std::out_of_range);
70 EXPECT_THROW(sut->sensorUpdated(*sensor, Timestamp{10}, 20.0),
71 std::out_of_range);
72}
73
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000074TEST_F(TestMetricAfterInitialization, containsIdInConfigurationDump)
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010075{
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000076 const auto conf = sut->dumpConfiguration();
77
78 EXPECT_THAT(conf.at_label<utils::tstring::Id>(), Eq("id"));
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000079 EXPECT_THAT(conf.to_json().at(tstring::Id::str()),
80 Eq(nlohmann::json("id")));
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010081}
82
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000083TEST_F(TestMetricAfterInitialization, containsOpInJsonDump)
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010084{
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000085 const auto conf = sut->dumpConfiguration();
86
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000087 EXPECT_THAT(conf.at_label<utils::tstring::OperationType>(),
88 Eq(OperationType::avg));
89 EXPECT_THAT(conf.to_json().at(tstring::OperationType::str()),
90 Eq(nlohmann::json(utils::toUnderlying(OperationType::avg))));
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010091}
92
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000093TEST_F(TestMetricAfterInitialization, containsMetadataInJsonDump)
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010094{
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000095 const auto conf = sut->dumpConfiguration();
96
97 EXPECT_THAT(conf.at_label<utils::tstring::MetricMetadata>(),
98 Eq("metadata"));
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000099 EXPECT_THAT(conf.to_json().at(tstring::MetricMetadata::str()),
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100100 Eq(nlohmann::json("metadata")));
101}
102
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000103TEST_F(TestMetricAfterInitialization, containsSensorPathInJsonDump)
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100104{
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000105 ON_CALL(*sensorMock, id())
106 .WillByDefault(Return(SensorMock::makeId("service1", "path1")));
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100107
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000108 const auto conf = sut->dumpConfiguration();
109
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000110 EXPECT_THAT(conf.at_label<utils::tstring::SensorPath>(),
111 Eq(LabeledSensorParameters("service1", "path1")));
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000112 EXPECT_THAT(
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000113 conf.to_json().at(tstring::SensorPath::str()),
114 Eq(nlohmann::json({{"service", "service1"}, {"path", "path1"}})));
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100115}