blob: f6c4ee74b1e0e75171e6bd1121fac442ad02badd [file] [log] [blame]
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +02001#include "dbus_environment.hpp"
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +00002#include "helpers.hpp"
Wludzik, Jozefe2362792020-10-27 17:23:55 +01003#include "mocks/json_storage_mock.hpp"
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +02004#include "mocks/metric_mock.hpp"
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +02005#include "mocks/report_manager_mock.hpp"
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +02006#include "params/report_params.hpp"
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +02007#include "report.hpp"
8#include "report_manager.hpp"
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +02009#include "utils/conv_container.hpp"
Krzysztof Grobelnyf32f6fe2020-10-30 13:51:58 +010010#include "utils/set_exception.hpp"
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020011
12#include <sdbusplus/exception.hpp>
13
14using namespace testing;
15using namespace std::literals::string_literals;
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020016using namespace std::chrono_literals;
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020017
18class TestReport : public Test
19{
20 public:
Wludzik, Jozefe2362792020-10-27 17:23:55 +010021 ReportParams defaultParams;
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020022
23 std::unique_ptr<ReportManagerMock> reportManagerMock =
Wludzik, Jozefe2362792020-10-27 17:23:55 +010024 std::make_unique<NiceMock<ReportManagerMock>>();
25 testing::NiceMock<StorageMock> storageMock;
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020026 std::vector<std::shared_ptr<MetricMock>> metricMocks = {
27 std::make_shared<NiceMock<MetricMock>>(),
28 std::make_shared<NiceMock<MetricMock>>(),
29 std::make_shared<NiceMock<MetricMock>>()};
30 std::unique_ptr<Report> sut;
31
Wludzik, Jozefe2362792020-10-27 17:23:55 +010032 MockFunction<void()> checkPoint;
33
34 TestReport()
35 {
36 ON_CALL(*metricMocks[0], getReadings())
37 .WillByDefault(ReturnRefOfCopy(std::vector<MetricValue>(
38 {MetricValue{"a", "b", 17.1, 114},
39 MetricValue{"aaa", "bbb", 21.7, 100}})));
40 ON_CALL(*metricMocks[1], getReadings())
41 .WillByDefault(ReturnRefOfCopy(
42 std::vector<MetricValue>({MetricValue{"aa", "bb", 42.0, 74}})));
43
44 for (size_t i = 0; i < metricMocks.size(); ++i)
45 {
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000046 using namespace std::string_literals;
47
48 auto id = std::to_string(i);
49
50 auto sensorParameters = std::vector<LabeledSensorParameters>(
51 {LabeledSensorParameters("service"s + id, "path"s + id)});
52 auto metricParameters =
53 LabeledMetricParameters(std::move(sensorParameters), "op"s + id,
54 "id"s + id, "metadata"s + id);
55
56 ON_CALL(*metricMocks[i], dumpConfiguration())
57 .WillByDefault(Return(std::move(metricParameters)));
Wludzik, Jozefe2362792020-10-27 17:23:55 +010058 }
59 }
60
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020061 void SetUp() override
62 {
63 sut = makeReport(ReportParams());
64 }
65
Wludzik, Jozefe2362792020-10-27 17:23:55 +010066 static interfaces::JsonStorage::FilePath to_file_path(std::string name)
67 {
68 return interfaces::JsonStorage::FilePath(
69 std::to_string(std::hash<std::string>{}(name)));
70 }
71
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020072 std::unique_ptr<Report> makeReport(const ReportParams& params)
73 {
74 return std::make_unique<Report>(
75 DbusEnvironment::getIoc(), DbusEnvironment::getObjServer(),
76 params.reportName(), params.reportingType(),
Wludzik, Jozefe2362792020-10-27 17:23:55 +010077 params.emitReadingUpdate(), params.logToMetricReportCollection(),
78 params.interval(), params.readingParameters(), *reportManagerMock,
79 storageMock,
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020080 utils::convContainer<std::shared_ptr<interfaces::Metric>>(
81 metricMocks));
82 }
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020083
84 template <class T>
85 static T getProperty(const std::string& path, const std::string& property)
86 {
87 std::promise<T> propertyPromise;
88 sdbusplus::asio::getProperty<T>(
89 *DbusEnvironment::getBus(), DbusEnvironment::serviceName(), path,
90 Report::reportIfaceName, property,
Krzysztof Grobelnyf32f6fe2020-10-30 13:51:58 +010091 [&propertyPromise](boost::system::error_code) {
92 utils::setException(propertyPromise, "GetProperty failed");
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020093 },
94 [&propertyPromise](T t) { propertyPromise.set_value(t); });
Krzysztof Grobelnyf32f6fe2020-10-30 13:51:58 +010095 return DbusEnvironment::waitForFuture(propertyPromise.get_future());
96 }
97
98 boost::system::error_code call(const std::string& path,
99 const std::string& interface,
100 const std::string& method)
101 {
102 std::promise<boost::system::error_code> methodPromise;
103 DbusEnvironment::getBus()->async_method_call(
104 [&methodPromise](boost::system::error_code ec) {
105 methodPromise.set_value(ec);
106 },
107 DbusEnvironment::serviceName(), path, interface, method);
108 return DbusEnvironment::waitForFuture(methodPromise.get_future());
109 }
110
111 boost::system::error_code update(const std::string& path)
112 {
113 return call(path, Report::reportIfaceName, "Update");
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200114 }
115
116 template <class T>
117 static boost::system::error_code setProperty(const std::string& path,
118 const std::string& property,
119 const T& newValue)
120 {
121 std::promise<boost::system::error_code> setPromise;
122 sdbusplus::asio::setProperty(
123 *DbusEnvironment::getBus(), DbusEnvironment::serviceName(), path,
124 Report::reportIfaceName, property, std::move(newValue),
125 [&setPromise](boost::system::error_code ec) {
126 setPromise.set_value(ec);
127 },
128 [&setPromise]() {
129 setPromise.set_value(boost::system::error_code{});
130 });
Krzysztof Grobelnyf32f6fe2020-10-30 13:51:58 +0100131 return DbusEnvironment::waitForFuture(setPromise.get_future());
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200132 }
133
134 boost::system::error_code deleteReport(const std::string& path)
135 {
Krzysztof Grobelnyf32f6fe2020-10-30 13:51:58 +0100136 return call(path, Report::deleteIfaceName, "Delete");
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200137 }
138};
139
140TEST_F(TestReport, verifyIfPropertiesHaveValidValue)
141{
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200142 EXPECT_THAT(getProperty<uint64_t>(sut->getPath(), "Interval"),
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100143 Eq(defaultParams.interval().count()));
144 EXPECT_THAT(getProperty<bool>(sut->getPath(), "Persistency"), Eq(true));
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200145 EXPECT_THAT(getProperty<bool>(sut->getPath(), "EmitsReadingsUpdate"),
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100146 Eq(defaultParams.emitReadingUpdate()));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200147 EXPECT_THAT(
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200148 getProperty<bool>(sut->getPath(), "LogToMetricReportsCollection"),
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100149 Eq(defaultParams.logToMetricReportCollection()));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200150 EXPECT_THAT(
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200151 getProperty<ReadingParameters>(sut->getPath(), "ReadingParameters"),
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100152 Eq(defaultParams.readingParameters()));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200153}
154
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200155TEST_F(TestReport, readingsAreInitialyEmpty)
156{
157 EXPECT_THAT(getProperty<Readings>(sut->getPath(), "Readings"),
158 Eq(Readings{}));
159}
160
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200161TEST_F(TestReport, setIntervalWithValidValue)
162{
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100163 uint64_t newValue = defaultParams.interval().count() + 1;
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200164 EXPECT_THAT(setProperty(sut->getPath(), "Interval", newValue).value(),
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200165 Eq(boost::system::errc::success));
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200166 EXPECT_THAT(getProperty<uint64_t>(sut->getPath(), "Interval"),
167 Eq(newValue));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200168}
169
170TEST_F(TestReport, settingIntervalWithInvalidValueDoesNotChangeProperty)
171{
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100172 uint64_t newValue = defaultParams.interval().count() - 1;
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200173 EXPECT_THAT(setProperty(sut->getPath(), "Interval", newValue).value(),
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200174 Eq(boost::system::errc::success));
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200175 EXPECT_THAT(getProperty<uint64_t>(sut->getPath(), "Interval"),
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100176 Eq(defaultParams.interval().count()));
177}
178
Wludzik, Jozefb1ff1f62020-10-23 13:20:52 +0200179TEST_F(TestReport, settingEmitsReadingsUpdateHaveNoEffect)
180{
181 EXPECT_THAT(setProperty(sut->getPath(), "EmitsReadingsUpdate",
182 !defaultParams.emitReadingUpdate())
183 .value(),
184 Eq(boost::system::errc::read_only_file_system));
185 EXPECT_THAT(getProperty<bool>(sut->getPath(), "EmitsReadingsUpdate"),
186 Eq(defaultParams.emitReadingUpdate()));
187}
188
189TEST_F(TestReport, settingLogToMetricReportCollectionHaveNoEffect)
190{
191 EXPECT_THAT(setProperty(sut->getPath(), "LogToMetricReportsCollection",
192 !defaultParams.logToMetricReportCollection())
193 .value(),
194 Eq(boost::system::errc::read_only_file_system));
195 EXPECT_THAT(
196 getProperty<bool>(sut->getPath(), "LogToMetricReportsCollection"),
197 Eq(defaultParams.logToMetricReportCollection()));
198}
199
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100200TEST_F(TestReport, settingPersistencyToFalseRemovesReportFromStorage)
201{
202 EXPECT_CALL(storageMock, remove(to_file_path(sut->getName())));
203
204 bool persistency = false;
205 EXPECT_THAT(setProperty(sut->getPath(), "Persistency", persistency).value(),
206 Eq(boost::system::errc::success));
207 EXPECT_THAT(getProperty<bool>(sut->getPath(), "Persistency"),
208 Eq(persistency));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200209}
210
211TEST_F(TestReport, deleteReport)
212{
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200213 EXPECT_CALL(*reportManagerMock, removeReport(sut.get()));
214 auto ec = deleteReport(sut->getPath());
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200215 EXPECT_THAT(ec, Eq(boost::system::errc::success));
216}
217
218TEST_F(TestReport, deletingNonExistingReportReturnInvalidRequestDescriptor)
219{
220 auto ec = deleteReport(Report::reportDir + "NonExisting"s);
221 EXPECT_THAT(ec.value(), Eq(EBADR));
222}
223
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100224TEST_F(TestReport, deleteReportExpectThatFileIsRemoveFromStorage)
225{
226 EXPECT_CALL(storageMock, remove(to_file_path(sut->getName())));
227 auto ec = deleteReport(sut->getPath());
228 EXPECT_THAT(ec, Eq(boost::system::errc::success));
229}
230
231class TestReportStore :
232 public TestReport,
233 public WithParamInterface<std::pair<std::string, nlohmann::json>>
234{
235 public:
236 void SetUp() override
237 {}
238
239 nlohmann::json storedConfiguration;
240};
241
242INSTANTIATE_TEST_SUITE_P(
243 _, TestReportStore,
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000244 Values(
245 std::make_pair("Version"s, nlohmann::json(2)),
246 std::make_pair("Name"s, nlohmann::json(ReportParams().reportName())),
247 std::make_pair("ReportingType",
248 nlohmann::json(ReportParams().reportingType())),
249 std::make_pair("EmitsReadingsUpdate",
250 nlohmann::json(ReportParams().emitReadingUpdate())),
251 std::make_pair(
252 "LogToMetricReportsCollection",
253 nlohmann::json(ReportParams().logToMetricReportCollection())),
254 std::make_pair("Interval",
255 nlohmann::json(ReportParams().interval().count())),
256 std::make_pair(
257 "ReadingParameters",
258 nlohmann::json({{{"sensorPaths",
259 {{{"service", "service0"}, {"path", "path0"}}}},
260 {"operationType", "op0"},
261 {"id", "id0"},
262 {"metricMetadata", "metadata0"}},
263 {{"sensorPaths",
264 {{{"service", "service1"}, {"path", "path1"}}}},
265 {"operationType", "op1"},
266 {"id", "id1"},
267 {"metricMetadata", "metadata1"}},
268 {{"sensorPaths",
269 {{{"service", "service2"}, {"path", "path2"}}}},
270 {"operationType", "op2"},
271 {"id", "id2"},
272 {"metricMetadata", "metadata2"}}}))));
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100273
274TEST_P(TestReportStore, settingPersistencyToTrueStoresReport)
275{
276 sut = makeReport(ReportParams());
277
278 {
279 InSequence seq;
280 EXPECT_CALL(storageMock, remove(to_file_path(sut->getName())));
281 EXPECT_CALL(checkPoint, Call());
282 EXPECT_CALL(storageMock, store(to_file_path(sut->getName()), _))
283 .WillOnce(SaveArg<1>(&storedConfiguration));
284 }
285
286 setProperty(sut->getPath(), "Persistency", false);
287 checkPoint.Call();
288 setProperty(sut->getPath(), "Persistency", true);
289
290 const auto& [key, value] = GetParam();
291
292 ASSERT_THAT(storedConfiguration.at(key), Eq(value));
293}
294
295TEST_P(TestReportStore, reportIsSavedToStorageAfterCreated)
296{
297 EXPECT_CALL(storageMock,
298 store(to_file_path(ReportParams().reportName()), _))
299 .WillOnce(SaveArg<1>(&storedConfiguration));
300
301 sut = makeReport(ReportParams());
302
303 const auto& [key, value] = GetParam();
304
305 ASSERT_THAT(storedConfiguration.at(key), Eq(value));
306}
307
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200308class TestReportValidNames :
309 public TestReport,
310 public WithParamInterface<ReportParams>
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200311{
312 public:
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200313 void SetUp() override
314 {}
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200315};
316
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200317INSTANTIATE_TEST_SUITE_P(
318 ValidNames, TestReportValidNames,
319 Values(ReportParams().reportName("Valid_1"),
320 ReportParams().reportName("Valid_1/Valid_2"),
321 ReportParams().reportName("Valid_1/Valid_2/Valid_3")));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200322
323TEST_P(TestReportValidNames, reportCtorDoesNotThrowOnValidName)
324{
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200325 EXPECT_NO_THROW(makeReport(GetParam()));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200326}
327
328class TestReportInvalidNames :
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200329 public TestReport,
330 public WithParamInterface<ReportParams>
331{
332 public:
333 void SetUp() override
334 {}
335};
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200336
337INSTANTIATE_TEST_SUITE_P(InvalidNames, TestReportInvalidNames,
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200338 Values(ReportParams().reportName("/"),
339 ReportParams().reportName("/Invalid"),
340 ReportParams().reportName("Invalid/"),
341 ReportParams().reportName("Invalid/Invalid/"),
342 ReportParams().reportName("Invalid?")));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200343
344TEST_P(TestReportInvalidNames, reportCtorThrowOnInvalidName)
345{
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200346 EXPECT_THROW(makeReport(GetParam()), sdbusplus::exception::SdBusError);
347}
348
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100349TEST_F(TestReportInvalidNames, reportCtorThrowOnInvalidNameAndNoStoreIsCalled)
350{
351 EXPECT_CALL(storageMock, store).Times(0);
352 EXPECT_THROW(makeReport(ReportParams().reportName("/Invalid")),
353 sdbusplus::exception::SdBusError);
354}
355
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200356class TestReportAllReportTypes :
357 public TestReport,
358 public WithParamInterface<ReportParams>
359{
Wludzik, Jozefb1ff1f62020-10-23 13:20:52 +0200360 public:
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200361 void SetUp() override
362 {
363 sut = makeReport(GetParam());
364 }
365};
366
367INSTANTIATE_TEST_SUITE_P(_, TestReportAllReportTypes,
368 Values(ReportParams().reportingType("OnRequest"),
369 ReportParams().reportingType("OnChange"),
370 ReportParams().reportingType("Periodic")));
371
372TEST_P(TestReportAllReportTypes, returnPropertValueOfReportType)
373{
374 EXPECT_THAT(getProperty<std::string>(sut->getPath(), "ReportingType"),
375 Eq(GetParam().reportingType()));
376}
377
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100378TEST_P(TestReportAllReportTypes, updateReadingsCallUpdateReadingsProperty)
379{
380 const uint64_t expectedTime = std::time(0);
381
382 sut->updateReadings();
383
384 const auto [timestamp, readings] =
385 getProperty<Readings>(sut->getPath(), "Readings");
386
387 EXPECT_THAT(timestamp, Ge(expectedTime));
388}
389
Krzysztof Grobelnyf32f6fe2020-10-30 13:51:58 +0100390class TestReportOnRequestType : public TestReport
391{
392 void SetUp() override
393 {
394 sut = makeReport(ReportParams().reportingType("OnRequest"));
395 }
396};
397
398TEST_F(TestReportOnRequestType, updatesReadingTimestamp)
399{
400 const uint64_t expectedTime = std::time(0);
401
402 ASSERT_THAT(update(sut->getPath()), Eq(boost::system::errc::success));
403
404 const auto [timestamp, readings] =
405 getProperty<Readings>(sut->getPath(), "Readings");
406
407 EXPECT_THAT(timestamp, Ge(expectedTime));
408}
409
410TEST_F(TestReportOnRequestType, updatesReadingWhenUpdateIsCalled)
411{
412 ASSERT_THAT(update(sut->getPath()), Eq(boost::system::errc::success));
413
414 const auto [timestamp, readings] =
415 getProperty<Readings>(sut->getPath(), "Readings");
416
417 EXPECT_THAT(readings,
418 ElementsAre(std::make_tuple("a"s, "b"s, 17.1, 114u),
419 std::make_tuple("aaa"s, "bbb"s, 21.7, 100u),
420 std::make_tuple("aa"s, "bb"s, 42.0, 74u)));
421}
422
423class TestReportNonOnRequestType :
424 public TestReport,
425 public WithParamInterface<ReportParams>
426{
427 void SetUp() override
428 {
429 sut = makeReport(GetParam());
430 }
431};
432
433INSTANTIATE_TEST_SUITE_P(_, TestReportNonOnRequestType,
434 Values(ReportParams().reportingType("Periodic"),
435 ReportParams().reportingType("OnChange")));
436
437TEST_P(TestReportNonOnRequestType, readingsAreNotUpdateOnUpdateCall)
438{
439 ASSERT_THAT(update(sut->getPath()), Eq(boost::system::errc::success));
440
441 EXPECT_THAT(getProperty<Readings>(sut->getPath(), "Readings"),
442 Eq(Readings{}));
443}
444
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200445class TestReportNonPeriodicReport :
446 public TestReport,
447 public WithParamInterface<ReportParams>
448{
Wludzik, Jozefb1ff1f62020-10-23 13:20:52 +0200449 public:
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200450 void SetUp() override
451 {
452 sut = makeReport(GetParam());
453 }
454};
455
456INSTANTIATE_TEST_SUITE_P(_, TestReportNonPeriodicReport,
457 Values(ReportParams().reportingType("OnRequest"),
458 ReportParams().reportingType("OnChange")));
459
460TEST_P(TestReportNonPeriodicReport, readingsAreNotUpdatedAfterIntervalExpires)
461{
462 DbusEnvironment::sleepFor(ReportManager::minInterval + 1ms);
463
464 EXPECT_THAT(getProperty<Readings>(sut->getPath(), "Readings"),
465 Eq(Readings{}));
466}
467
468class TestReportPeriodicReport : public TestReport
469{
470 void SetUp() override
471 {
472 sut = makeReport(ReportParams().reportingType("Periodic"));
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200473 }
474};
475
476TEST_F(TestReportPeriodicReport, readingTimestampIsUpdatedAfterIntervalExpires)
477{
478 const uint64_t expectedTime = std::time(0);
479 DbusEnvironment::sleepFor(ReportManager::minInterval + 1ms);
480
481 const auto [timestamp, readings] =
482 getProperty<Readings>(sut->getPath(), "Readings");
483
484 EXPECT_THAT(timestamp, Ge(expectedTime));
485}
486
487TEST_F(TestReportPeriodicReport, readingsAreUpdatedAfterIntervalExpires)
488{
489 DbusEnvironment::sleepFor(ReportManager::minInterval + 1ms);
490
491 const auto [timestamp, readings] =
492 getProperty<Readings>(sut->getPath(), "Readings");
493
494 EXPECT_THAT(readings,
495 ElementsAre(std::make_tuple("a"s, "b"s, 17.1, 114u),
496 std::make_tuple("aaa"s, "bbb"s, 21.7, 100u),
497 std::make_tuple("aa"s, "bb"s, 42.0, 74u)));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200498}
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100499
500class TestReportInitialization : public TestReport
501{
502 public:
503 void SetUp() override
504 {}
505
506 void monitorProc(sdbusplus::message::message& msg)
507 {
508 std::string iface;
509 std::vector<std::pair<std::string, std::variant<Readings>>>
510 changed_properties;
511 std::vector<std::string> invalidated_properties;
512
513 msg.read(iface, changed_properties, invalidated_properties);
514
515 if (iface == Report::reportIfaceName)
516 {
517 for (const auto& [name, value] : changed_properties)
518 {
519 if (name == "Readings")
520 {
521 readingsUpdated.Call();
522 }
523 }
524 }
525 }
526
527 void makeMonitor()
528 {
529 monitor = std::make_unique<sdbusplus::bus::match::match>(
530 *DbusEnvironment::getBus(),
531 sdbusplus::bus::match::rules::propertiesChanged(
532 sut->getPath(), Report::reportIfaceName),
533 [this](auto& msg) { monitorProc(msg); });
534 }
535
536 std::unique_ptr<sdbusplus::bus::match::match> monitor;
537 MockFunction<void()> readingsUpdated;
538};
539
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100540TEST_F(TestReportInitialization, metricsAreInitializedWhenConstructed)
541{
542 for (auto& metric : metricMocks)
543 {
544 EXPECT_CALL(*metric, initialize());
545 }
546
547 sut = makeReport(ReportParams());
548}
549
Wludzik, Jozefb1ff1f62020-10-23 13:20:52 +0200550TEST_F(TestReportInitialization,
551 emitReadingsUpdateIsTrueReadingsPropertiesChangedSingalEmits)
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100552{
Wludzik, Jozefb1ff1f62020-10-23 13:20:52 +0200553 EXPECT_CALL(readingsUpdated, Call())
554 .WillOnce(
555 InvokeWithoutArgs(DbusEnvironment::setPromise("readingsUpdated")));
556
557 const auto elapsed = DbusEnvironment::measureTime([this] {
558 sut = makeReport(
559 defaultParams.reportingType("Periodic").emitReadingUpdate(true));
560 makeMonitor();
561 EXPECT_TRUE(DbusEnvironment::waitForFuture("readingsUpdated"));
562 });
563
564 EXPECT_THAT(elapsed, AllOf(Ge(defaultParams.interval()),
565 Lt(defaultParams.interval() * 2)));
566}
567
568TEST_F(TestReportInitialization,
569 emitReadingsUpdateIsFalseReadingsPropertiesChangesSigalDoesNotEmits)
570{
571 EXPECT_CALL(readingsUpdated, Call()).Times(0);
572
573 sut = makeReport(
574 defaultParams.reportingType("Periodic").emitReadingUpdate(false));
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100575 makeMonitor();
Wludzik, Jozefb1ff1f62020-10-23 13:20:52 +0200576 DbusEnvironment::sleepFor(defaultParams.interval() * 2);
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100577}