blob: 4e3ce8dc07066901829886127d342e2345ad026d [file] [log] [blame]
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +02001#include "dbus_environment.hpp"
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +01002#include "fakes/clock_fake.hpp"
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +00003#include "helpers.hpp"
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +01004#include "messages/collect_trigger_id.hpp"
5#include "messages/trigger_presence_changed_ind.hpp"
6#include "messages/update_report_ind.hpp"
Wludzik, Jozefe2362792020-10-27 17:23:55 +01007#include "mocks/json_storage_mock.hpp"
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +02008#include "mocks/metric_mock.hpp"
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +02009#include "mocks/report_manager_mock.hpp"
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020010#include "params/report_params.hpp"
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020011#include "report.hpp"
12#include "report_manager.hpp"
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010013#include "utils/clock.hpp"
Krzysztof Grobelny51497a02021-11-09 14:56:22 +010014#include "utils/contains.hpp"
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020015#include "utils/conv_container.hpp"
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +010016#include "utils/messanger.hpp"
Krzysztof Grobelny51497a02021-11-09 14:56:22 +010017#include "utils/transform.hpp"
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000018#include "utils/tstring.hpp"
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020019
20#include <sdbusplus/exception.hpp>
21
22using namespace testing;
23using namespace std::literals::string_literals;
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020024using namespace std::chrono_literals;
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000025namespace tstring = utils::tstring;
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020026
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +010027constexpr Milliseconds systemTimestamp = 55ms;
28
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020029class TestReport : public Test
30{
31 public:
Wludzik, Jozefe2362792020-10-27 17:23:55 +010032 ReportParams defaultParams;
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020033
34 std::unique_ptr<ReportManagerMock> reportManagerMock =
Wludzik, Jozefe2362792020-10-27 17:23:55 +010035 std::make_unique<NiceMock<ReportManagerMock>>();
36 testing::NiceMock<StorageMock> storageMock;
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000037 std::vector<std::shared_ptr<MetricMock>> metricMocks;
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +010038 std::unique_ptr<ClockFake> clockFakePtr = std::make_unique<ClockFake>();
39 ClockFake& clockFake = *clockFakePtr;
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020040 std::unique_ptr<Report> sut;
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +010041 utils::Messanger messanger;
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020042
Wludzik, Jozefe2362792020-10-27 17:23:55 +010043 MockFunction<void()> checkPoint;
44
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +010045 TestReport() : messanger(DbusEnvironment::getIoc())
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +010046 {
47 clockFake.system.set(systemTimestamp);
48 }
49
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000050 void initMetricMocks(
51 const std::vector<LabeledMetricParameters>& metricParameters)
Wludzik, Jozefe2362792020-10-27 17:23:55 +010052 {
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000053 for (auto i = metricMocks.size(); i < metricParameters.size(); ++i)
Wludzik, Jozefe2362792020-10-27 17:23:55 +010054 {
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000055 metricMocks.emplace_back(std::make_shared<NiceMock<MetricMock>>());
56 }
57 metricMocks.resize(metricParameters.size());
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000058
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000059 std::vector<MetricValue> readings{{MetricValue{"a", "b", 17.1, 114},
60 MetricValue{"aa", "bb", 42.0, 74}}};
61 readings.resize(metricParameters.size());
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000062
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000063 for (size_t i = 0; i < metricParameters.size(); ++i)
64 {
65 ON_CALL(*metricMocks[i], getReadings())
Krzysztof Grobelny80697712021-03-04 09:49:27 +000066 .WillByDefault(Return(std::vector({readings[i]})));
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000067 ON_CALL(*metricMocks[i], dumpConfiguration())
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000068 .WillByDefault(Return(metricParameters[i]));
Wludzik, Jozefe2362792020-10-27 17:23:55 +010069 }
70 }
71
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020072 void SetUp() override
73 {
74 sut = makeReport(ReportParams());
75 }
76
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010077 static interfaces::JsonStorage::FilePath to_file_path(std::string id)
Wludzik, Jozefe2362792020-10-27 17:23:55 +010078 {
79 return interfaces::JsonStorage::FilePath(
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010080 std::to_string(std::hash<std::string>{}(id)));
Wludzik, Jozefe2362792020-10-27 17:23:55 +010081 }
82
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020083 std::unique_ptr<Report> makeReport(const ReportParams& params)
84 {
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000085 initMetricMocks(params.metricParameters());
86
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020087 return std::make_unique<Report>(
88 DbusEnvironment::getIoc(), DbusEnvironment::getObjServer(),
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010089 params.reportId(), params.reportName(), params.reportingType(),
90 params.reportActions(), params.interval(), params.appendLimit(),
91 params.reportUpdates(), *reportManagerMock, storageMock,
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020092 utils::convContainer<std::shared_ptr<interfaces::Metric>>(
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +020093 metricMocks),
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +010094 params.enabled(), std::move(clockFakePtr));
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020095 }
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020096
97 template <class T>
98 static T getProperty(const std::string& path, const std::string& property)
99 {
Szymon Dompkee28aa532021-10-27 12:33:12 +0200100 return DbusEnvironment::getProperty<T>(path, Report::reportIfaceName,
101 property);
102 }
103
104 template <class T>
105 static boost::system::error_code setProperty(const std::string& path,
106 const std::string& property,
107 const T& newValue)
108 {
109 return DbusEnvironment::setProperty<T>(path, Report::reportIfaceName,
110 property, newValue);
Krzysztof Grobelnyf32f6fe2020-10-30 13:51:58 +0100111 }
112
113 boost::system::error_code call(const std::string& path,
114 const std::string& interface,
115 const std::string& method)
116 {
117 std::promise<boost::system::error_code> methodPromise;
118 DbusEnvironment::getBus()->async_method_call(
119 [&methodPromise](boost::system::error_code ec) {
120 methodPromise.set_value(ec);
121 },
122 DbusEnvironment::serviceName(), path, interface, method);
123 return DbusEnvironment::waitForFuture(methodPromise.get_future());
124 }
125
126 boost::system::error_code update(const std::string& path)
127 {
128 return call(path, Report::reportIfaceName, "Update");
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200129 }
130
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200131 boost::system::error_code deleteReport(const std::string& path)
132 {
Krzysztof Grobelnyf32f6fe2020-10-30 13:51:58 +0100133 return call(path, Report::deleteIfaceName, "Delete");
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200134 }
135};
136
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100137TEST_F(TestReport, returnsId)
138{
139 EXPECT_THAT(sut->getId(), Eq(defaultParams.reportId()));
140}
141
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200142TEST_F(TestReport, verifyIfPropertiesHaveValidValue)
143{
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +0200144 EXPECT_THAT(getProperty<bool>(sut->getPath(), "Enabled"),
145 Eq(defaultParams.enabled()));
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200146 EXPECT_THAT(getProperty<uint64_t>(sut->getPath(), "Interval"),
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100147 Eq(defaultParams.interval().count()));
148 EXPECT_THAT(getProperty<bool>(sut->getPath(), "Persistency"), Eq(true));
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100149 EXPECT_THAT(
150 getProperty<std::vector<std::string>>(sut->getPath(), "ReportActions"),
151 Eq(utils::transform(defaultParams.reportActions(), [](const auto v) {
152 return utils::enumToString(v);
153 })));
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200154 EXPECT_THAT(getProperty<bool>(sut->getPath(), "EmitsReadingsUpdate"),
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100155 Eq(utils::contains(defaultParams.reportActions(),
156 ReportAction::emitsReadingsUpdate)));
Szymon Dompke3eb56862021-09-20 15:32:04 +0200157 EXPECT_THAT(getProperty<uint64_t>(sut->getPath(), "AppendLimit"),
158 Eq(defaultParams.appendLimit()));
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100159 EXPECT_THAT(getProperty<std::string>(sut->getPath(), "ReportingType"),
160 Eq(utils::enumToString(defaultParams.reportingType())));
Szymon Dompke3eb56862021-09-20 15:32:04 +0200161 EXPECT_THAT(getProperty<std::string>(sut->getPath(), "ReportUpdates"),
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100162 Eq(utils::enumToString(defaultParams.reportUpdates())));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200163 EXPECT_THAT(
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200164 getProperty<bool>(sut->getPath(), "LogToMetricReportsCollection"),
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100165 Eq(utils::contains(defaultParams.reportActions(),
166 ReportAction::logToMetricReportsCollection)));
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000167 EXPECT_THAT(getProperty<ReadingParameters>(
168 sut->getPath(), "ReadingParametersFutureVersion"),
169 Eq(toReadingParameters(defaultParams.metricParameters())));
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100170 EXPECT_THAT(getProperty<std::string>(sut->getPath(), "Name"),
171 Eq(defaultParams.reportName()));
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100172 EXPECT_THAT(
173 getProperty<std::vector<std::string>>(sut->getPath(), "TriggerIds"),
174 Eq(std::vector<std::string>()));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200175}
176
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200177TEST_F(TestReport, readingsAreInitialyEmpty)
178{
179 EXPECT_THAT(getProperty<Readings>(sut->getPath(), "Readings"),
180 Eq(Readings{}));
181}
182
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +0200183TEST_F(TestReport, setEnabledWithNewValue)
184{
185 bool newValue = !defaultParams.enabled();
186 EXPECT_THAT(setProperty(sut->getPath(), "Enabled", newValue).value(),
187 Eq(boost::system::errc::success));
188 EXPECT_THAT(getProperty<bool>(sut->getPath(), "Enabled"), Eq(newValue));
189}
190
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200191TEST_F(TestReport, setIntervalWithValidValue)
192{
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100193 uint64_t newValue = defaultParams.interval().count() + 1;
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200194 EXPECT_THAT(setProperty(sut->getPath(), "Interval", newValue).value(),
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200195 Eq(boost::system::errc::success));
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200196 EXPECT_THAT(getProperty<uint64_t>(sut->getPath(), "Interval"),
197 Eq(newValue));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200198}
199
Szymon Dompke3e2cc9d2021-12-14 12:00:52 +0100200TEST_F(
201 TestReport,
202 settingIntervalWithInvalidValueDoesNotChangePropertyAndReturnsInvalidArgument)
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200203{
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100204 uint64_t newValue = defaultParams.interval().count() - 1;
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200205 EXPECT_THAT(setProperty(sut->getPath(), "Interval", newValue).value(),
Szymon Dompke3e2cc9d2021-12-14 12:00:52 +0100206 Eq(boost::system::errc::invalid_argument));
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200207 EXPECT_THAT(getProperty<uint64_t>(sut->getPath(), "Interval"),
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100208 Eq(defaultParams.interval().count()));
209}
210
Wludzik, Jozefb1ff1f62020-10-23 13:20:52 +0200211TEST_F(TestReport, settingEmitsReadingsUpdateHaveNoEffect)
212{
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100213 EXPECT_THAT(
214 setProperty(sut->getPath(), "EmitsReadingsUpdate", true).value(),
215 Eq(boost::system::errc::read_only_file_system));
Wludzik, Jozefb1ff1f62020-10-23 13:20:52 +0200216 EXPECT_THAT(getProperty<bool>(sut->getPath(), "EmitsReadingsUpdate"),
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100217 Eq(utils::contains(defaultParams.reportActions(),
218 ReportAction::emitsReadingsUpdate)));
Wludzik, Jozefb1ff1f62020-10-23 13:20:52 +0200219}
220
221TEST_F(TestReport, settingLogToMetricReportCollectionHaveNoEffect)
222{
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100223 EXPECT_THAT(
224 setProperty(sut->getPath(), "LogToMetricReportsCollection", true)
225 .value(),
226 Eq(boost::system::errc::read_only_file_system));
Wludzik, Jozefb1ff1f62020-10-23 13:20:52 +0200227 EXPECT_THAT(
228 getProperty<bool>(sut->getPath(), "LogToMetricReportsCollection"),
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100229 Eq(utils::contains(defaultParams.reportActions(),
230 ReportAction::logToMetricReportsCollection)));
Wludzik, Jozefb1ff1f62020-10-23 13:20:52 +0200231}
232
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100233TEST_F(TestReport, settingPersistencyToFalseRemovesReportFromStorage)
234{
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100235 EXPECT_CALL(storageMock, remove(to_file_path(sut->getId())));
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100236
237 bool persistency = false;
238 EXPECT_THAT(setProperty(sut->getPath(), "Persistency", persistency).value(),
239 Eq(boost::system::errc::success));
240 EXPECT_THAT(getProperty<bool>(sut->getPath(), "Persistency"),
241 Eq(persistency));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200242}
243
244TEST_F(TestReport, deleteReport)
245{
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200246 EXPECT_CALL(*reportManagerMock, removeReport(sut.get()));
247 auto ec = deleteReport(sut->getPath());
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200248 EXPECT_THAT(ec, Eq(boost::system::errc::success));
249}
250
251TEST_F(TestReport, deletingNonExistingReportReturnInvalidRequestDescriptor)
252{
253 auto ec = deleteReport(Report::reportDir + "NonExisting"s);
254 EXPECT_THAT(ec.value(), Eq(EBADR));
255}
256
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100257TEST_F(TestReport, deleteReportExpectThatFileIsRemoveFromStorage)
258{
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100259 EXPECT_CALL(storageMock, remove(to_file_path(sut->getId())));
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100260 auto ec = deleteReport(sut->getPath());
261 EXPECT_THAT(ec, Eq(boost::system::errc::success));
262}
263
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100264TEST_F(TestReport, updatesTriggerIdWhenTriggerIsAdded)
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100265{
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100266 utils::Messanger messanger(DbusEnvironment::getIoc());
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100267
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100268 messanger.send(messages::TriggerPresenceChangedInd{
269 messages::Presence::Exist, "trigger1", {defaultParams.reportId()}});
270 messanger.send(messages::TriggerPresenceChangedInd{
271 messages::Presence::Exist, "trigger1", {defaultParams.reportId()}});
272 messanger.send(messages::TriggerPresenceChangedInd{
273 messages::Presence::Exist, "trigger2", {"someOtherReport"}});
274 messanger.send(messages::TriggerPresenceChangedInd{
275 messages::Presence::Exist,
276 "trigger3",
277 {"someOtherReport", defaultParams.reportId()}});
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100278
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100279 EXPECT_THAT(
280 getProperty<std::vector<std::string>>(sut->getPath(), "TriggerIds"),
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100281 UnorderedElementsAre("trigger1", "trigger3"));
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100282}
283
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100284TEST_F(TestReport, updatesTriggerIdWhenTriggerIsRemoved)
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100285{
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100286 utils::Messanger messanger(DbusEnvironment::getIoc());
287
288 messanger.send(messages::TriggerPresenceChangedInd{
289 messages::Presence::Exist, "trigger1", {defaultParams.reportId()}});
290 messanger.send(messages::TriggerPresenceChangedInd{
291 messages::Presence::Exist, "trigger2", {defaultParams.reportId()}});
292 messanger.send(messages::TriggerPresenceChangedInd{
293 messages::Presence::Exist, "trigger3", {defaultParams.reportId()}});
294
295 messanger.send(messages::TriggerPresenceChangedInd{
296 messages::Presence::Removed, "trigger1", {defaultParams.reportId()}});
297 messanger.send(messages::TriggerPresenceChangedInd{
298 messages::Presence::Removed, "trigger2", {}});
299 messanger.send(messages::TriggerPresenceChangedInd{
300 messages::Presence::Removed, "trigger1", {defaultParams.reportId()}});
301
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100302 EXPECT_THAT(
303 getProperty<std::vector<std::string>>(sut->getPath(), "TriggerIds"),
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100304 UnorderedElementsAre("trigger3"));
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100305}
306
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100307TEST_F(TestReport, updatesTriggerIdWhenTriggerIsModified)
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100308{
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100309 utils::Messanger messanger(DbusEnvironment::getIoc());
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100310
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100311 messanger.send(messages::TriggerPresenceChangedInd{
312 messages::Presence::Exist, "trigger1", {defaultParams.reportId()}});
313 messanger.send(messages::TriggerPresenceChangedInd{
314 messages::Presence::Exist, "trigger2", {defaultParams.reportId()}});
315 messanger.send(messages::TriggerPresenceChangedInd{
316 messages::Presence::Exist, "trigger3", {defaultParams.reportId()}});
317
318 messanger.send(messages::TriggerPresenceChangedInd{
319 messages::Presence::Exist, "trigger1", {defaultParams.reportId()}});
320 messanger.send(messages::TriggerPresenceChangedInd{
321 messages::Presence::Exist, "trigger2", {}});
322 messanger.send(messages::TriggerPresenceChangedInd{
323 messages::Presence::Exist, "trigger3", {defaultParams.reportId()}});
324
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100325 EXPECT_THAT(
326 getProperty<std::vector<std::string>>(sut->getPath(), "TriggerIds"),
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100327 UnorderedElementsAre("trigger1", "trigger3"));
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100328}
329
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100330class TestReportStore :
331 public TestReport,
332 public WithParamInterface<std::pair<std::string, nlohmann::json>>
333{
334 public:
335 void SetUp() override
336 {}
337
338 nlohmann::json storedConfiguration;
339};
340
341INSTANTIATE_TEST_SUITE_P(
342 _, TestReportStore,
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +0200343 Values(std::make_pair("Enabled"s, nlohmann::json(ReportParams().enabled())),
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100344 std::make_pair("Version"s, nlohmann::json(6)),
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100345 std::make_pair("Id"s, nlohmann::json(ReportParams().reportId())),
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000346 std::make_pair("Name"s, nlohmann::json(ReportParams().reportName())),
347 std::make_pair("ReportingType",
348 nlohmann::json(ReportParams().reportingType())),
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100349 std::make_pair("ReportActions", nlohmann::json(utils::transform(
350 ReportParams().reportActions(),
351 [](const auto v) {
352 return utils::toUnderlying(
353 v);
354 }))),
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000355 std::make_pair("Interval",
356 nlohmann::json(ReportParams().interval().count())),
Krzysztof Grobelnye6c417c2022-02-02 17:25:53 +0100357 std::make_pair("AppendLimit",
358 nlohmann::json(ReportParams().appendLimit())),
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000359 std::make_pair(
360 "ReadingParameters",
361 nlohmann::json(
362 {{{tstring::SensorPath::str(),
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000363 {{{tstring::Service::str(), "Service"},
364 {tstring::Path::str(),
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100365 "/xyz/openbmc_project/sensors/power/p1"},
366 {tstring::Metadata::str(), "metadata1"}}}},
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000367 {tstring::OperationType::str(), OperationType::single},
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000368 {tstring::Id::str(), "MetricId1"},
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000369 {tstring::CollectionTimeScope::str(),
370 CollectionTimeScope::point},
371 {tstring::CollectionDuration::str(), 0}},
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000372 {{tstring::SensorPath::str(),
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000373 {{{tstring::Service::str(), "Service"},
374 {tstring::Path::str(),
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100375 "/xyz/openbmc_project/sensors/power/p2"},
376 {tstring::Metadata::str(), "metadata2"}}}},
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000377 {tstring::OperationType::str(), OperationType::single},
378 {tstring::Id::str(), "MetricId2"},
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000379 {tstring::CollectionTimeScope::str(),
380 CollectionTimeScope::point},
381 {tstring::CollectionDuration::str(), 0}}}))));
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100382
383TEST_P(TestReportStore, settingPersistencyToTrueStoresReport)
384{
385 sut = makeReport(ReportParams());
386
387 {
388 InSequence seq;
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100389 EXPECT_CALL(storageMock, remove(to_file_path(sut->getId())));
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100390 EXPECT_CALL(checkPoint, Call());
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100391 EXPECT_CALL(storageMock, store(to_file_path(sut->getId()), _))
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100392 .WillOnce(SaveArg<1>(&storedConfiguration));
393 }
394
395 setProperty(sut->getPath(), "Persistency", false);
396 checkPoint.Call();
397 setProperty(sut->getPath(), "Persistency", true);
398
399 const auto& [key, value] = GetParam();
400
401 ASSERT_THAT(storedConfiguration.at(key), Eq(value));
402}
403
404TEST_P(TestReportStore, reportIsSavedToStorageAfterCreated)
405{
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100406 EXPECT_CALL(storageMock, store(to_file_path(ReportParams().reportId()), _))
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100407 .WillOnce(SaveArg<1>(&storedConfiguration));
408
409 sut = makeReport(ReportParams());
410
411 const auto& [key, value] = GetParam();
412
413 ASSERT_THAT(storedConfiguration.at(key), Eq(value));
414}
415
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200416class TestReportValidNames :
417 public TestReport,
418 public WithParamInterface<ReportParams>
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200419{
420 public:
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200421 void SetUp() override
422 {}
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200423};
424
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200425INSTANTIATE_TEST_SUITE_P(
426 ValidNames, TestReportValidNames,
427 Values(ReportParams().reportName("Valid_1"),
428 ReportParams().reportName("Valid_1/Valid_2"),
429 ReportParams().reportName("Valid_1/Valid_2/Valid_3")));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200430
431TEST_P(TestReportValidNames, reportCtorDoesNotThrowOnValidName)
432{
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200433 EXPECT_NO_THROW(makeReport(GetParam()));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200434}
435
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100436class TestReportInvalidIds :
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200437 public TestReport,
438 public WithParamInterface<ReportParams>
439{
440 public:
441 void SetUp() override
442 {}
443};
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200444
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100445INSTANTIATE_TEST_SUITE_P(InvalidNames, TestReportInvalidIds,
446 Values(ReportParams().reportId("/"),
447 ReportParams().reportId("/Invalid"),
448 ReportParams().reportId("Invalid/"),
449 ReportParams().reportId("Invalid/Invalid/"),
450 ReportParams().reportId("Invalid?")));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200451
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100452TEST_P(TestReportInvalidIds, failsToCreateReportWithInvalidName)
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100453{
454 EXPECT_CALL(storageMock, store).Times(0);
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100455
456 EXPECT_THROW(makeReport(GetParam()), sdbusplus::exception::SdBusError);
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100457}
458
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200459class TestReportAllReportTypes :
460 public TestReport,
461 public WithParamInterface<ReportParams>
462{
Wludzik, Jozefb1ff1f62020-10-23 13:20:52 +0200463 public:
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200464 void SetUp() override
465 {
466 sut = makeReport(GetParam());
467 }
468};
469
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100470INSTANTIATE_TEST_SUITE_P(
471 _, TestReportAllReportTypes,
472 Values(ReportParams().reportingType(ReportingType::onRequest),
473 ReportParams().reportingType(ReportingType::onChange),
474 ReportParams().reportingType(ReportingType::periodic)));
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200475
476TEST_P(TestReportAllReportTypes, returnPropertValueOfReportType)
477{
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100478 EXPECT_THAT(utils::toReportingType(
479 getProperty<std::string>(sut->getPath(), "ReportingType")),
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200480 Eq(GetParam().reportingType()));
481}
482
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100483TEST_P(TestReportAllReportTypes, readingsAreUpdated)
484{
485 clockFake.system.advance(10ms);
486
487 messanger.send(messages::UpdateReportInd{{sut->getId()}});
488 const auto [timestamp, readings] =
489 getProperty<Readings>(sut->getPath(), "Readings");
490
491 EXPECT_THAT(Milliseconds{timestamp}, Eq(systemTimestamp + 10ms));
492}
493
494TEST_P(TestReportAllReportTypes, readingsAreNotUpdatedWhenReportIsDisabled)
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +0200495{
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +0100496 clockFake.system.advance(10ms);
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +0200497
498 setProperty(sut->getPath(), "Enabled", false);
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100499 messanger.send(messages::UpdateReportInd{{sut->getId()}});
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +0200500 const auto [timestamp, readings] =
501 getProperty<Readings>(sut->getPath(), "Readings");
502
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +0100503 EXPECT_THAT(Milliseconds{timestamp}, Eq(0ms));
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +0200504}
505
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100506TEST_P(TestReportAllReportTypes, readingsAreNotUpdatedWhenReportIdDiffers)
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100507{
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +0100508 clockFake.system.advance(10ms);
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100509
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100510 messanger.send(messages::UpdateReportInd{{sut->getId() + "x"s}});
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100511 const auto [timestamp, readings] =
512 getProperty<Readings>(sut->getPath(), "Readings");
513
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100514 EXPECT_THAT(Milliseconds{timestamp}, Eq(0ms));
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100515}
516
Krzysztof Grobelnyf32f6fe2020-10-30 13:51:58 +0100517class TestReportOnRequestType : public TestReport
518{
519 void SetUp() override
520 {
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100521 sut =
522 makeReport(ReportParams().reportingType(ReportingType::onRequest));
Krzysztof Grobelnyf32f6fe2020-10-30 13:51:58 +0100523 }
524};
525
526TEST_F(TestReportOnRequestType, updatesReadingTimestamp)
527{
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +0100528 clockFake.system.advance(10ms);
Krzysztof Grobelnyf32f6fe2020-10-30 13:51:58 +0100529
530 ASSERT_THAT(update(sut->getPath()), Eq(boost::system::errc::success));
531
532 const auto [timestamp, readings] =
533 getProperty<Readings>(sut->getPath(), "Readings");
534
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +0100535 EXPECT_THAT(Milliseconds{timestamp}, Eq(systemTimestamp + 10ms));
Krzysztof Grobelnyf32f6fe2020-10-30 13:51:58 +0100536}
537
538TEST_F(TestReportOnRequestType, updatesReadingWhenUpdateIsCalled)
539{
540 ASSERT_THAT(update(sut->getPath()), Eq(boost::system::errc::success));
541
542 const auto [timestamp, readings] =
543 getProperty<Readings>(sut->getPath(), "Readings");
544
545 EXPECT_THAT(readings,
546 ElementsAre(std::make_tuple("a"s, "b"s, 17.1, 114u),
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000547 std::make_tuple("aa"s, "bb"s, 42.0, 74u)));
Krzysztof Grobelnyf32f6fe2020-10-30 13:51:58 +0100548}
549
550class TestReportNonOnRequestType :
551 public TestReport,
552 public WithParamInterface<ReportParams>
553{
554 void SetUp() override
555 {
556 sut = makeReport(GetParam());
557 }
558};
559
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100560INSTANTIATE_TEST_SUITE_P(
561 _, TestReportNonOnRequestType,
562 Values(ReportParams().reportingType(ReportingType::periodic),
563 ReportParams().reportingType(ReportingType::onChange)));
Krzysztof Grobelnyf32f6fe2020-10-30 13:51:58 +0100564
565TEST_P(TestReportNonOnRequestType, readingsAreNotUpdateOnUpdateCall)
566{
567 ASSERT_THAT(update(sut->getPath()), Eq(boost::system::errc::success));
568
569 EXPECT_THAT(getProperty<Readings>(sut->getPath(), "Readings"),
570 Eq(Readings{}));
571}
572
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200573class TestReportNonPeriodicReport :
574 public TestReport,
575 public WithParamInterface<ReportParams>
576{
Wludzik, Jozefb1ff1f62020-10-23 13:20:52 +0200577 public:
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200578 void SetUp() override
579 {
580 sut = makeReport(GetParam());
581 }
582};
583
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100584INSTANTIATE_TEST_SUITE_P(
585 _, TestReportNonPeriodicReport,
586 Values(ReportParams().reportingType(ReportingType::onRequest),
587 ReportParams().reportingType(ReportingType::onChange)));
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200588
589TEST_P(TestReportNonPeriodicReport, readingsAreNotUpdatedAfterIntervalExpires)
590{
591 DbusEnvironment::sleepFor(ReportManager::minInterval + 1ms);
592
593 EXPECT_THAT(getProperty<Readings>(sut->getPath(), "Readings"),
594 Eq(Readings{}));
595}
596
597class TestReportPeriodicReport : public TestReport
598{
599 void SetUp() override
600 {
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100601 sut = makeReport(ReportParams().reportingType(ReportingType::periodic));
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200602 }
603};
604
605TEST_F(TestReportPeriodicReport, readingTimestampIsUpdatedAfterIntervalExpires)
606{
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +0100607 clockFake.system.advance(10ms);
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200608 DbusEnvironment::sleepFor(ReportManager::minInterval + 1ms);
609
610 const auto [timestamp, readings] =
611 getProperty<Readings>(sut->getPath(), "Readings");
612
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +0100613 EXPECT_THAT(Milliseconds{timestamp}, Eq(systemTimestamp + 10ms));
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200614}
615
616TEST_F(TestReportPeriodicReport, readingsAreUpdatedAfterIntervalExpires)
617{
618 DbusEnvironment::sleepFor(ReportManager::minInterval + 1ms);
619
620 const auto [timestamp, readings] =
621 getProperty<Readings>(sut->getPath(), "Readings");
622
623 EXPECT_THAT(readings,
624 ElementsAre(std::make_tuple("a"s, "b"s, 17.1, 114u),
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000625 std::make_tuple("aa"s, "bb"s, 42.0, 74u)));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200626}
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100627
Szymon Dompke3eb56862021-09-20 15:32:04 +0200628struct ReportUpdatesReportParams
629{
630 ReportParams reportParams;
631 std::vector<ReadingData> expectedReadings;
632 bool expectedEnabled;
633};
634
635class TestReportWithReportUpdatesAndLimit :
636 public TestReport,
637 public WithParamInterface<ReportUpdatesReportParams>
638{
639 void SetUp() override
640 {
641 sut = makeReport(ReportParams(GetParam().reportParams)
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100642 .reportingType(ReportingType::periodic)
Szymon Dompke3eb56862021-09-20 15:32:04 +0200643 .interval(std::chrono::hours(1000)));
644 }
645};
646
647INSTANTIATE_TEST_SUITE_P(
648 _, TestReportWithReportUpdatesAndLimit,
649 Values(
650 ReportUpdatesReportParams{
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100651 ReportParams()
652 .reportUpdates(ReportUpdates::appendWrapsWhenFull)
653 .appendLimit(5),
Szymon Dompke3eb56862021-09-20 15:32:04 +0200654 std::vector<ReadingData>{{std::make_tuple("aa"s, "bb"s, 42.0, 74u),
655 std::make_tuple("a"s, "b"s, 17.1, 114u),
656 std::make_tuple("aa"s, "bb"s, 42.0, 74u),
657 std::make_tuple("aa"s, "bb"s, 42.0, 74u),
658 std::make_tuple("a"s, "b"s, 17.1, 114u)}},
659 true},
660 ReportUpdatesReportParams{
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100661 ReportParams()
662 .reportUpdates(ReportUpdates::appendWrapsWhenFull)
663 .appendLimit(4),
Szymon Dompke3eb56862021-09-20 15:32:04 +0200664 std::vector<ReadingData>{
665 {std::make_tuple("a"s, "b"s, 17.1, 114u),
666 std::make_tuple("aa"s, "bb"s, 42.0, 74u),
667 std::make_tuple("a"s, "b"s, 17.1, 114u),
668 std::make_tuple("aa"s, "bb"s, 42.0, 74u)}},
669 true},
670 ReportUpdatesReportParams{
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100671 ReportParams()
672 .reportUpdates(ReportUpdates::appendWrapsWhenFull)
673 .appendLimit(0),
Szymon Dompke3eb56862021-09-20 15:32:04 +0200674 std::vector<ReadingData>{}, true},
675 ReportUpdatesReportParams{
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100676 ReportParams()
677 .reportUpdates(ReportUpdates::appendStopsWhenFull)
678 .appendLimit(10),
Szymon Dompke3eb56862021-09-20 15:32:04 +0200679 std::vector<ReadingData>{
680 {std::make_tuple("a"s, "b"s, 17.1, 114u),
681 std::make_tuple("aa"s, "bb"s, 42.0, 74u),
682 std::make_tuple("a"s, "b"s, 17.1, 114u),
683 std::make_tuple("aa"s, "bb"s, 42.0, 74u),
684 std::make_tuple("a"s, "b"s, 17.1, 114u),
685 std::make_tuple("aa"s, "bb"s, 42.0, 74u),
686 std::make_tuple("a"s, "b"s, 17.1, 114u),
687 std::make_tuple("aa"s, "bb"s, 42.0, 74u)}},
688 true},
689 ReportUpdatesReportParams{
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100690 ReportParams()
691 .reportUpdates(ReportUpdates::appendStopsWhenFull)
692 .appendLimit(5),
Szymon Dompke3eb56862021-09-20 15:32:04 +0200693 std::vector<ReadingData>{{std::make_tuple("a"s, "b"s, 17.1, 114u),
694 std::make_tuple("aa"s, "bb"s, 42.0, 74u),
695 std::make_tuple("a"s, "b"s, 17.1, 114u),
696 std::make_tuple("aa"s, "bb"s, 42.0, 74u),
697 std::make_tuple("a"s, "b"s, 17.1, 114u)}},
698 false},
699 ReportUpdatesReportParams{
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100700 ReportParams()
701 .reportUpdates(ReportUpdates::appendStopsWhenFull)
702 .appendLimit(4),
Szymon Dompke3eb56862021-09-20 15:32:04 +0200703 std::vector<ReadingData>{
704 {std::make_tuple("a"s, "b"s, 17.1, 114u),
705 std::make_tuple("aa"s, "bb"s, 42.0, 74u),
706 std::make_tuple("a"s, "b"s, 17.1, 114u),
707 std::make_tuple("aa"s, "bb"s, 42.0, 74u)}},
708 false},
709 ReportUpdatesReportParams{
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100710 ReportParams()
711 .reportUpdates(ReportUpdates::appendStopsWhenFull)
712 .appendLimit(0),
Szymon Dompke3eb56862021-09-20 15:32:04 +0200713 std::vector<ReadingData>{}, false},
714 ReportUpdatesReportParams{
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100715 ReportParams()
716 .reportUpdates(ReportUpdates::overwrite)
717 .appendLimit(500),
Szymon Dompke3eb56862021-09-20 15:32:04 +0200718 std::vector<ReadingData>{
719 {std::make_tuple("a"s, "b"s, 17.1, 114u),
720 std::make_tuple("aa"s, "bb"s, 42.0, 74u)}},
721 true},
722 ReportUpdatesReportParams{
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100723 ReportParams()
724 .reportUpdates(ReportUpdates::overwrite)
725 .appendLimit(1),
Szymon Dompke3eb56862021-09-20 15:32:04 +0200726 std::vector<ReadingData>{
727 {std::make_tuple("a"s, "b"s, 17.1, 114u),
728 std::make_tuple("aa"s, "bb"s, 42.0, 74u)}},
729 true},
730 ReportUpdatesReportParams{
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100731 ReportParams()
732 .reportUpdates(ReportUpdates::overwrite)
733 .appendLimit(0),
Szymon Dompke3eb56862021-09-20 15:32:04 +0200734 std::vector<ReadingData>{
735 {std::make_tuple("a"s, "b"s, 17.1, 114u),
736 std::make_tuple("aa"s, "bb"s, 42.0, 74u)}},
737 true}));
738
739TEST_P(TestReportWithReportUpdatesAndLimit,
740 readingsAreUpdatedAfterIntervalExpires)
741{
742 for (int i = 0; i < 4; i++)
743 {
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100744 messanger.send(messages::UpdateReportInd{{sut->getId()}});
Szymon Dompke3eb56862021-09-20 15:32:04 +0200745 }
746
747 const auto [timestamp, readings] =
748 getProperty<Readings>(sut->getPath(), "Readings");
749 const auto enabled = getProperty<bool>(sut->getPath(), "Enabled");
750
751 EXPECT_THAT(readings, ElementsAreArray(GetParam().expectedReadings));
752 EXPECT_EQ(enabled, GetParam().expectedEnabled);
753}
754
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100755class TestReportInitialization : public TestReport
756{
757 public:
758 void SetUp() override
759 {}
760
761 void monitorProc(sdbusplus::message::message& msg)
762 {
763 std::string iface;
764 std::vector<std::pair<std::string, std::variant<Readings>>>
765 changed_properties;
766 std::vector<std::string> invalidated_properties;
767
768 msg.read(iface, changed_properties, invalidated_properties);
769
770 if (iface == Report::reportIfaceName)
771 {
772 for (const auto& [name, value] : changed_properties)
773 {
774 if (name == "Readings")
775 {
776 readingsUpdated.Call();
777 }
778 }
779 }
780 }
781
782 void makeMonitor()
783 {
Patrick Williams3a62ee12021-12-03 10:13:25 -0600784 monitor = std::make_unique<sdbusplus::bus::match_t>(
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100785 *DbusEnvironment::getBus(),
786 sdbusplus::bus::match::rules::propertiesChanged(
787 sut->getPath(), Report::reportIfaceName),
788 [this](auto& msg) { monitorProc(msg); });
789 }
790
Patrick Williams3a62ee12021-12-03 10:13:25 -0600791 std::unique_ptr<sdbusplus::bus::match_t> monitor;
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100792 MockFunction<void()> readingsUpdated;
793};
794
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +0200795TEST_F(TestReportInitialization,
796 metricsAreInitializedWhenEnabledReportConstructed)
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100797{
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +0200798 initMetricMocks(defaultParams.metricParameters());
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100799 for (auto& metric : metricMocks)
800 {
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +0200801 EXPECT_CALL(*metric, initialize()).Times(1);
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100802 }
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +0200803 sut = makeReport(defaultParams.enabled(true));
804}
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100805
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +0200806TEST_F(TestReportInitialization,
807 metricsAreNotInitializedWhenDisabledReportConstructed)
808{
809 initMetricMocks(defaultParams.metricParameters());
810 for (auto& metric : metricMocks)
811 {
812 EXPECT_CALL(*metric, initialize()).Times(0);
813 }
814 sut = makeReport(defaultParams.enabled(false));
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100815}
816
Wludzik, Jozefb1ff1f62020-10-23 13:20:52 +0200817TEST_F(TestReportInitialization,
818 emitReadingsUpdateIsTrueReadingsPropertiesChangedSingalEmits)
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100819{
Wludzik, Jozefb1ff1f62020-10-23 13:20:52 +0200820 EXPECT_CALL(readingsUpdated, Call())
821 .WillOnce(
822 InvokeWithoutArgs(DbusEnvironment::setPromise("readingsUpdated")));
823
824 const auto elapsed = DbusEnvironment::measureTime([this] {
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100825 sut =
826 makeReport(defaultParams.reportingType(ReportingType::periodic)
827 .reportActions({ReportAction::emitsReadingsUpdate}));
Wludzik, Jozefb1ff1f62020-10-23 13:20:52 +0200828 makeMonitor();
829 EXPECT_TRUE(DbusEnvironment::waitForFuture("readingsUpdated"));
830 });
831
832 EXPECT_THAT(elapsed, AllOf(Ge(defaultParams.interval()),
833 Lt(defaultParams.interval() * 2)));
834}
835
836TEST_F(TestReportInitialization,
837 emitReadingsUpdateIsFalseReadingsPropertiesChangesSigalDoesNotEmits)
838{
839 EXPECT_CALL(readingsUpdated, Call()).Times(0);
840
841 sut = makeReport(
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100842 defaultParams.reportingType(ReportingType::periodic).reportActions({}));
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100843 makeMonitor();
Wludzik, Jozefb1ff1f62020-10-23 13:20:52 +0200844 DbusEnvironment::sleepFor(defaultParams.interval() * 2);
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100845}
Szymon Dompke3eb56862021-09-20 15:32:04 +0200846
847TEST_F(TestReportInitialization, appendLimitDeducedProperly)
848{
849 sut = makeReport(
850 ReportParams().appendLimit(std::numeric_limits<uint64_t>::max()));
851 auto appendLimit = getProperty<uint64_t>(sut->getPath(), "AppendLimit");
852 EXPECT_EQ(appendLimit, 2ull);
853}
Krzysztof Grobelnye6c417c2022-02-02 17:25:53 +0100854
855TEST_F(TestReportInitialization, appendLimitSetToUintMaxIsStoredCorrectly)
856{
857 nlohmann::json storedConfiguration;
858
859 EXPECT_CALL(storageMock, store(to_file_path(ReportParams().reportId()), _))
860 .WillOnce(SaveArg<1>(&storedConfiguration));
861
862 sut = makeReport(
863 ReportParams().appendLimit(std::numeric_limits<uint64_t>::max()));
864
865 ASSERT_THAT(storedConfiguration.at("AppendLimit"),
866 Eq(std::numeric_limits<uint64_t>::max()));
867}
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100868
869TEST_F(TestReportInitialization, triggerIdsPropertyIsInitialzed)
870{
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100871 for (const auto& triggerId : {"trigger1", "trigger2"})
872 {
873 messanger.on_receive<messages::CollectTriggerIdReq>(
874 [&](const auto& msg) {
875 messanger.send(messages::CollectTriggerIdResp{triggerId});
876 });
877 }
878
879 sut = makeReport(ReportParams());
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100880
881 EXPECT_THAT(
882 getProperty<std::vector<std::string>>(sut->getPath(), "TriggerIds"),
883 UnorderedElementsAre("trigger1", "trigger2"));
884}