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 | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 11 | namespace tstring = utils::tstring; |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 12 | |
| 13 | using Timestamp = uint64_t; |
| 14 | |
| 15 | class TestMetric : public Test |
| 16 | { |
| 17 | public: |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 18 | std::shared_ptr<SensorMock> sensorMock = |
| 19 | std::make_shared<NiceMock<SensorMock>>(); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 20 | |
| 21 | std::shared_ptr<Metric> sut = std::make_shared<Metric>( |
Krzysztof Grobelny | 9f9ff90 | 2021-03-05 14:57:53 +0000 | [diff] [blame^] | 22 | sensorMock, OperationType::avg, "id", "metadata"); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 23 | }; |
| 24 | |
| 25 | TEST_F(TestMetric, subscribesForSensorDuringInitialization) |
| 26 | { |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 27 | 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 | |
| 35 | TEST_F(TestMetric, containsEmptyReadingAfterCreated) |
| 36 | { |
| 37 | ASSERT_THAT(sut->getReading(), MetricValue({"id", "metadata", 0., 0u})); |
| 38 | } |
| 39 | |
| 40 | class TestMetricAfterInitialization : public TestMetric |
| 41 | { |
| 42 | public: |
| 43 | TestMetricAfterInitialization() |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 44 | { |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 45 | sut->initialize(); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 46 | } |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 47 | }; |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 48 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 49 | TEST_F(TestMetricAfterInitialization, containsEmptyReading) |
| 50 | { |
| 51 | ASSERT_THAT(sut->getReading(), MetricValue({"id", "metadata", 0., 0u})); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 52 | } |
| 53 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 54 | TEST_F(TestMetricAfterInitialization, updatesMetricValuesOnSensorUpdate) |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 55 | { |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 56 | sut->sensorUpdated(*sensorMock, Timestamp{18}, 31.2); |
| 57 | |
| 58 | ASSERT_THAT(sut->getReading(), |
| 59 | Eq(MetricValue{"id", "metadata", 31.2, 18u})); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 60 | } |
| 61 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 62 | TEST_F(TestMetricAfterInitialization, |
| 63 | throwsWhenUpdateIsPerformedOnUnknownSensor) |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 64 | { |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 65 | 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 Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 71 | TEST_F(TestMetricAfterInitialization, containsIdInConfigurationDump) |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 72 | { |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 73 | const auto conf = sut->dumpConfiguration(); |
| 74 | |
| 75 | EXPECT_THAT(conf.at_label<utils::tstring::Id>(), Eq("id")); |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 76 | EXPECT_THAT(conf.to_json().at(tstring::Id::str()), |
| 77 | Eq(nlohmann::json("id"))); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 78 | } |
| 79 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 80 | TEST_F(TestMetricAfterInitialization, containsOpInJsonDump) |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 81 | { |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 82 | const auto conf = sut->dumpConfiguration(); |
| 83 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 84 | 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 Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 88 | } |
| 89 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 90 | TEST_F(TestMetricAfterInitialization, containsMetadataInJsonDump) |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 91 | { |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 92 | const auto conf = sut->dumpConfiguration(); |
| 93 | |
| 94 | EXPECT_THAT(conf.at_label<utils::tstring::MetricMetadata>(), |
| 95 | Eq("metadata")); |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 96 | EXPECT_THAT(conf.to_json().at(tstring::MetricMetadata::str()), |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 97 | Eq(nlohmann::json("metadata"))); |
| 98 | } |
| 99 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 100 | TEST_F(TestMetricAfterInitialization, containsSensorPathInJsonDump) |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 101 | { |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 102 | ON_CALL(*sensorMock, id()) |
| 103 | .WillByDefault(Return(SensorMock::makeId("service1", "path1"))); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 104 | |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 105 | const auto conf = sut->dumpConfiguration(); |
| 106 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 107 | EXPECT_THAT(conf.at_label<utils::tstring::SensorPath>(), |
| 108 | Eq(LabeledSensorParameters("service1", "path1"))); |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 109 | EXPECT_THAT( |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 110 | conf.to_json().at(tstring::SensorPath::str()), |
| 111 | Eq(nlohmann::json({{"service", "service1"}, {"path", "path1"}}))); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 112 | } |