Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 1 | #include "helpers.hpp" |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 2 | #include "metric.hpp" |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 3 | #include "mocks/sensor_mock.hpp" |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 4 | #include "utils/conv_container.hpp" |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 5 | #include "utils/conversion.hpp" |
| 6 | #include "utils/tstring.hpp" |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 7 | |
| 8 | #include <gmock/gmock.h> |
| 9 | |
| 10 | using namespace testing; |
Krzysztof Grobelny | 753e4b3 | 2021-02-11 12:58:58 +0000 | [diff] [blame^] | 11 | using namespace std::chrono_literals; |
| 12 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 13 | namespace tstring = utils::tstring; |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 14 | |
| 15 | using Timestamp = uint64_t; |
| 16 | |
| 17 | class TestMetric : public Test |
| 18 | { |
| 19 | public: |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 20 | std::shared_ptr<SensorMock> sensorMock = |
| 21 | std::make_shared<NiceMock<SensorMock>>(); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 22 | |
| 23 | std::shared_ptr<Metric> sut = std::make_shared<Metric>( |
Krzysztof Grobelny | 753e4b3 | 2021-02-11 12:58:58 +0000 | [diff] [blame^] | 24 | sensorMock, OperationType::avg, "id", "metadata", |
| 25 | CollectionTimeScope::point, CollectionDuration(0ms)); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 26 | }; |
| 27 | |
| 28 | TEST_F(TestMetric, subscribesForSensorDuringInitialization) |
| 29 | { |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 30 | 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 | |
| 38 | TEST_F(TestMetric, containsEmptyReadingAfterCreated) |
| 39 | { |
| 40 | ASSERT_THAT(sut->getReading(), MetricValue({"id", "metadata", 0., 0u})); |
| 41 | } |
| 42 | |
| 43 | class TestMetricAfterInitialization : public TestMetric |
| 44 | { |
| 45 | public: |
| 46 | TestMetricAfterInitialization() |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 47 | { |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 48 | sut->initialize(); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 49 | } |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 50 | }; |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 51 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 52 | TEST_F(TestMetricAfterInitialization, containsEmptyReading) |
| 53 | { |
| 54 | ASSERT_THAT(sut->getReading(), MetricValue({"id", "metadata", 0., 0u})); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 55 | } |
| 56 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 57 | TEST_F(TestMetricAfterInitialization, updatesMetricValuesOnSensorUpdate) |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 58 | { |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 59 | sut->sensorUpdated(*sensorMock, Timestamp{18}, 31.2); |
| 60 | |
| 61 | ASSERT_THAT(sut->getReading(), |
| 62 | Eq(MetricValue{"id", "metadata", 31.2, 18u})); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 63 | } |
| 64 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 65 | TEST_F(TestMetricAfterInitialization, |
| 66 | throwsWhenUpdateIsPerformedOnUnknownSensor) |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 67 | { |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 68 | 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 Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 74 | TEST_F(TestMetricAfterInitialization, containsIdInConfigurationDump) |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 75 | { |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 76 | const auto conf = sut->dumpConfiguration(); |
| 77 | |
| 78 | EXPECT_THAT(conf.at_label<utils::tstring::Id>(), Eq("id")); |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 79 | EXPECT_THAT(conf.to_json().at(tstring::Id::str()), |
| 80 | Eq(nlohmann::json("id"))); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 81 | } |
| 82 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 83 | TEST_F(TestMetricAfterInitialization, containsOpInJsonDump) |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 84 | { |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 85 | const auto conf = sut->dumpConfiguration(); |
| 86 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 87 | 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 Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 91 | } |
| 92 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 93 | TEST_F(TestMetricAfterInitialization, containsMetadataInJsonDump) |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 94 | { |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 95 | const auto conf = sut->dumpConfiguration(); |
| 96 | |
| 97 | EXPECT_THAT(conf.at_label<utils::tstring::MetricMetadata>(), |
| 98 | Eq("metadata")); |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 99 | EXPECT_THAT(conf.to_json().at(tstring::MetricMetadata::str()), |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 100 | Eq(nlohmann::json("metadata"))); |
| 101 | } |
| 102 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 103 | TEST_F(TestMetricAfterInitialization, containsSensorPathInJsonDump) |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 104 | { |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 105 | ON_CALL(*sensorMock, id()) |
| 106 | .WillByDefault(Return(SensorMock::makeId("service1", "path1"))); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 107 | |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 108 | const auto conf = sut->dumpConfiguration(); |
| 109 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 110 | EXPECT_THAT(conf.at_label<utils::tstring::SensorPath>(), |
| 111 | Eq(LabeledSensorParameters("service1", "path1"))); |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 112 | EXPECT_THAT( |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 113 | conf.to_json().at(tstring::SensorPath::str()), |
| 114 | Eq(nlohmann::json({{"service", "service1"}, {"path", "path1"}}))); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 115 | } |