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