Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 1 | #include "dbus_environment.hpp" |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 2 | #include "fakes/clock_fake.hpp" |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 3 | #include "helpers.hpp" |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 4 | #include "messages/collect_trigger_id.hpp" |
| 5 | #include "messages/trigger_presence_changed_ind.hpp" |
| 6 | #include "messages/update_report_ind.hpp" |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 7 | #include "mocks/json_storage_mock.hpp" |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 8 | #include "mocks/metric_mock.hpp" |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 9 | #include "mocks/report_factory_mock.hpp" |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 10 | #include "mocks/report_manager_mock.hpp" |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 11 | #include "params/report_params.hpp" |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 12 | #include "report.hpp" |
| 13 | #include "report_manager.hpp" |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 14 | #include "utils/clock.hpp" |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 15 | #include "utils/contains.hpp" |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 16 | #include "utils/conv_container.hpp" |
Szymon Dompke | 1cdd7e4 | 2022-06-08 14:43:13 +0200 | [diff] [blame] | 17 | #include "utils/dbus_path_utils.hpp" |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 18 | #include "utils/messanger.hpp" |
Szymon Dompke | 32305f1 | 2022-07-05 15:37:21 +0200 | [diff] [blame] | 19 | #include "utils/string_utils.hpp" |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 20 | #include "utils/transform.hpp" |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 21 | #include "utils/tstring.hpp" |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 22 | |
| 23 | #include <sdbusplus/exception.hpp> |
| 24 | |
Krzysztof Grobelny | a8182be | 2022-07-04 11:26:20 +0200 | [diff] [blame] | 25 | #include <ranges> |
| 26 | |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 27 | using namespace testing; |
| 28 | using namespace std::literals::string_literals; |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 29 | using namespace std::chrono_literals; |
Szymon Dompke | 1cdd7e4 | 2022-06-08 14:43:13 +0200 | [diff] [blame] | 30 | using sdbusplus::message::object_path; |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 31 | namespace tstring = utils::tstring; |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 32 | |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 33 | using ErrorMessageDbusType = std::tuple<std::string, std::string>; |
| 34 | using ErrorMessagesDbusType = std::vector<ErrorMessageDbusType>; |
| 35 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 36 | constexpr Milliseconds systemTimestamp = 55ms; |
| 37 | |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 38 | namespace |
| 39 | { |
| 40 | |
| 41 | ReportParams defaultParams() |
| 42 | { |
| 43 | return ReportParams(); |
| 44 | } |
| 45 | |
| 46 | ReportParams defaultOnChangeParams() |
| 47 | { |
| 48 | return defaultParams().reportingType(ReportingType::onChange); |
| 49 | } |
| 50 | |
| 51 | } // namespace |
| 52 | |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 53 | class TestReport : public Test |
| 54 | { |
| 55 | public: |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 56 | std::unique_ptr<ReportManagerMock> reportManagerMock = |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 57 | std::make_unique<NiceMock<ReportManagerMock>>(); |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 58 | std::unique_ptr<ReportFactoryMock> reportFactoryMock = |
| 59 | std::make_unique<NiceMock<ReportFactoryMock>>(); |
Krzysztof Grobelny | 493e62e | 2022-02-14 10:55:50 +0100 | [diff] [blame] | 60 | nlohmann::json storedConfiguration; |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 61 | NiceMock<StorageMock> storageMock; |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 62 | std::vector<std::shared_ptr<MetricMock>> metricMocks; |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 63 | std::unique_ptr<ClockFake> clockFakePtr = std::make_unique<ClockFake>(); |
| 64 | ClockFake& clockFake = *clockFakePtr; |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 65 | std::unique_ptr<Report> sut; |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 66 | utils::Messanger messanger; |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 67 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 68 | MockFunction<void()> checkPoint; |
| 69 | |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 70 | TestReport() : messanger(DbusEnvironment::getIoc()) |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 71 | { |
| 72 | clockFake.system.set(systemTimestamp); |
Krzysztof Grobelny | 493e62e | 2022-02-14 10:55:50 +0100 | [diff] [blame] | 73 | ON_CALL(storageMock, store(to_file_path(ReportParams().reportId()), _)) |
| 74 | .WillByDefault(SaveArg<1>(&storedConfiguration)); |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 75 | } |
| 76 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 77 | void initMetricMocks( |
| 78 | const std::vector<LabeledMetricParameters>& metricParameters) |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 79 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 80 | for (auto i = metricMocks.size(); i < metricParameters.size(); ++i) |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 81 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 82 | metricMocks.emplace_back(std::make_shared<NiceMock<MetricMock>>()); |
| 83 | } |
| 84 | metricMocks.resize(metricParameters.size()); |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 85 | |
Krzysztof Grobelny | cff70c1 | 2022-10-27 07:16:08 +0000 | [diff] [blame] | 86 | std::vector<MetricValue> readings{ |
| 87 | {MetricValue{"b", 17.1, 114}, MetricValue{"bb", 42.0, 74}}}; |
Szymon Dompke | bcf045a | 2022-09-16 15:23:30 +0200 | [diff] [blame] | 88 | |
| 89 | ASSERT_THAT(readings.size(), Ge(metricParameters.size())); |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 90 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 91 | for (size_t i = 0; i < metricParameters.size(); ++i) |
| 92 | { |
Krzysztof Grobelny | 9e8da54 | 2022-02-17 10:40:16 +0100 | [diff] [blame] | 93 | ON_CALL(*metricMocks[i], getUpdatedReadings()) |
| 94 | .WillByDefault(ReturnRefOfCopy(std::vector({readings[i]}))); |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 95 | ON_CALL(*metricMocks[i], dumpConfiguration()) |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 96 | .WillByDefault(Return(metricParameters[i])); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 100 | std::vector<std::shared_ptr<interfaces::Metric>> |
| 101 | getMetricsFromReadingParams(const ReadingParameters& params) |
| 102 | { |
| 103 | const auto metricParameters = |
| 104 | reportFactoryMock->convertMetricParams(params); |
| 105 | std::vector<std::shared_ptr<MetricMock>> metricMocks; |
| 106 | |
| 107 | for (size_t i = 0; i < metricParameters.size(); ++i) |
| 108 | { |
| 109 | metricMocks.emplace_back(std::make_shared<NiceMock<MetricMock>>()); |
| 110 | ON_CALL(*metricMocks[i], dumpConfiguration()) |
| 111 | .WillByDefault(Return(metricParameters[i])); |
Krzysztof Grobelny | 18e7101 | 2022-11-02 13:17:01 +0000 | [diff] [blame] | 112 | ON_CALL(*metricMocks[i], metricCount()) |
Szymon Dompke | 892f7c8 | 2022-10-12 09:54:22 +0200 | [diff] [blame] | 113 | .WillByDefault(Return(metricParameters[i] |
| 114 | .at_label<tstring::SensorPath>() |
| 115 | .size())); |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | return utils::convContainer<std::shared_ptr<interfaces::Metric>>( |
| 119 | metricMocks); |
| 120 | } |
| 121 | |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 122 | void SetUp() override |
| 123 | { |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 124 | sut = makeReport(defaultParams()); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 125 | } |
| 126 | |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 127 | static interfaces::JsonStorage::FilePath to_file_path(std::string id) |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 128 | { |
| 129 | return interfaces::JsonStorage::FilePath( |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 130 | std::to_string(std::hash<std::string>{}(id))); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 131 | } |
| 132 | |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 133 | std::unique_ptr<Report> makeReport(const ReportParams& params) |
| 134 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 135 | initMetricMocks(params.metricParameters()); |
| 136 | |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 137 | return std::make_unique<Report>( |
| 138 | DbusEnvironment::getIoc(), DbusEnvironment::getObjServer(), |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 139 | params.reportId(), params.reportName(), params.reportingType(), |
| 140 | params.reportActions(), params.interval(), params.appendLimit(), |
| 141 | params.reportUpdates(), *reportManagerMock, storageMock, |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 142 | utils::convContainer<std::shared_ptr<interfaces::Metric>>( |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 143 | metricMocks), |
Krzysztof Grobelny | 493e62e | 2022-02-14 10:55:50 +0100 | [diff] [blame] | 144 | *reportFactoryMock, params.enabled(), std::move(clockFakePtr), |
| 145 | params.readings()); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 146 | } |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 147 | |
| 148 | template <class T> |
| 149 | static T getProperty(const std::string& path, const std::string& property) |
| 150 | { |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 151 | return DbusEnvironment::getProperty<T>(path, Report::reportIfaceName, |
| 152 | property); |
| 153 | } |
| 154 | |
| 155 | template <class T> |
| 156 | static boost::system::error_code setProperty(const std::string& path, |
| 157 | const std::string& property, |
| 158 | const T& newValue) |
| 159 | { |
| 160 | return DbusEnvironment::setProperty<T>(path, Report::reportIfaceName, |
| 161 | property, newValue); |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 162 | } |
| 163 | |
Krzysztof Grobelny | cff70c1 | 2022-10-27 07:16:08 +0000 | [diff] [blame] | 164 | template <class... Args> |
| 165 | static boost::system::error_code callMethod(const std::string& path, |
| 166 | const std::string& method, |
| 167 | Args&&... args) |
| 168 | { |
| 169 | return DbusEnvironment::callMethod(path, Report::reportIfaceName, |
| 170 | "SetReportingProperties", |
| 171 | std::forward<Args>(args)...); |
| 172 | } |
| 173 | |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 174 | template <class T> |
| 175 | struct ChangePropertyParams |
| 176 | { |
| 177 | Matcher<T> valueBefore = _; |
| 178 | T newValue; |
| 179 | Matcher<boost::system::error_code> ec = |
| 180 | Eq(boost::system::errc::success); |
| 181 | Matcher<T> valueAfter = Eq(newValue); |
| 182 | }; |
| 183 | |
| 184 | template <class T> |
| 185 | static void changeProperty(const std::string& path, |
| 186 | const std::string& property, |
| 187 | ChangePropertyParams<T> p) |
| 188 | { |
| 189 | ASSERT_THAT(getProperty<T>(path, property), p.valueBefore); |
| 190 | ASSERT_THAT(setProperty<T>(path, property, p.newValue), p.ec); |
| 191 | EXPECT_THAT(getProperty<T>(path, property), p.valueAfter); |
| 192 | } |
| 193 | |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 194 | boost::system::error_code call(const std::string& path, |
| 195 | const std::string& interface, |
| 196 | const std::string& method) |
| 197 | { |
| 198 | std::promise<boost::system::error_code> methodPromise; |
| 199 | DbusEnvironment::getBus()->async_method_call( |
| 200 | [&methodPromise](boost::system::error_code ec) { |
Patrick Williams | 3a1c297 | 2023-05-10 07:51:04 -0500 | [diff] [blame] | 201 | methodPromise.set_value(ec); |
Patrick Williams | c7935fa | 2023-10-20 11:19:30 -0500 | [diff] [blame] | 202 | }, |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 203 | DbusEnvironment::serviceName(), path, interface, method); |
| 204 | return DbusEnvironment::waitForFuture(methodPromise.get_future()); |
| 205 | } |
| 206 | |
| 207 | boost::system::error_code update(const std::string& path) |
| 208 | { |
| 209 | return call(path, Report::reportIfaceName, "Update"); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 210 | } |
| 211 | |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 212 | boost::system::error_code deleteReport(const std::string& path) |
| 213 | { |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 214 | return call(path, Report::deleteIfaceName, "Delete"); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 215 | } |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 216 | |
| 217 | static std::pair<std::string, std::vector<std::string>> |
| 218 | makeStateDetail(const std::string& detailType, |
| 219 | std::vector<std::string> detailArgs) |
| 220 | { |
| 221 | return make_pair(detailType, detailArgs); |
| 222 | } |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 223 | }; |
| 224 | |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 225 | TEST_F(TestReport, returnsId) |
| 226 | { |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 227 | EXPECT_THAT(sut->getId(), Eq(defaultParams().reportId())); |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 228 | } |
| 229 | |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 230 | TEST_F(TestReport, verifyIfPropertiesHaveValidValue) |
| 231 | { |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 232 | EXPECT_THAT(getProperty<bool>(sut->getPath(), "Enabled"), |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 233 | Eq(defaultParams().enabled())); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 234 | EXPECT_THAT(getProperty<uint64_t>(sut->getPath(), "Interval"), |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 235 | Eq(defaultParams().interval().count())); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 236 | EXPECT_THAT(getProperty<bool>(sut->getPath(), "Persistency"), Eq(true)); |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 237 | EXPECT_THAT( |
| 238 | getProperty<std::vector<std::string>>(sut->getPath(), "ReportActions"), |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 239 | Eq(utils::transform(defaultParams().reportActions(), [](const auto v) { |
Patrick Williams | c7935fa | 2023-10-20 11:19:30 -0500 | [diff] [blame] | 240 | return utils::enumToString(v); |
| 241 | }))); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 242 | EXPECT_THAT(getProperty<bool>(sut->getPath(), "EmitsReadingsUpdate"), |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 243 | Eq(utils::contains(defaultParams().reportActions(), |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 244 | ReportAction::emitsReadingsUpdate))); |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 245 | EXPECT_THAT(getProperty<uint64_t>(sut->getPath(), "AppendLimit"), |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 246 | Eq(defaultParams().appendLimit())); |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 247 | EXPECT_THAT(getProperty<std::string>(sut->getPath(), "ReportingType"), |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 248 | Eq(utils::enumToString(defaultParams().reportingType()))); |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 249 | EXPECT_THAT(getProperty<std::string>(sut->getPath(), "ReportUpdates"), |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 250 | Eq(utils::enumToString(defaultParams().reportUpdates()))); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 251 | EXPECT_THAT( |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 252 | getProperty<bool>(sut->getPath(), "LogToMetricReportsCollection"), |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 253 | Eq(utils::contains(defaultParams().reportActions(), |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 254 | ReportAction::logToMetricReportsCollection))); |
Krzysztof Grobelny | cff70c1 | 2022-10-27 07:16:08 +0000 | [diff] [blame] | 255 | EXPECT_THAT( |
| 256 | getProperty<ReadingParameters>(sut->getPath(), "ReadingParameters"), |
| 257 | Eq(toReadingParameters(defaultParams().metricParameters()))); |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 258 | EXPECT_THAT(getProperty<std::string>(sut->getPath(), "Name"), |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 259 | Eq(defaultParams().reportName())); |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 260 | EXPECT_THAT( |
Szymon Dompke | 1cdd7e4 | 2022-06-08 14:43:13 +0200 | [diff] [blame] | 261 | getProperty<std::vector<object_path>>(sut->getPath(), "Triggers"), |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 262 | IsEmpty()); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 263 | } |
| 264 | |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 265 | TEST_F(TestReport, readingsAreInitialyEmpty) |
| 266 | { |
| 267 | EXPECT_THAT(getProperty<Readings>(sut->getPath(), "Readings"), |
| 268 | Eq(Readings{})); |
| 269 | } |
| 270 | |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 271 | TEST_F(TestReport, setReadingParametersWithNewParams) |
| 272 | { |
| 273 | ReadingParameters newParams = toReadingParameters( |
| 274 | std::vector<LabeledMetricParameters>{{LabeledMetricParameters{ |
| 275 | {LabeledSensorInfo{"Service", |
| 276 | "/xyz/openbmc_project/sensors/power/psu", |
| 277 | "NewMetadata123"}}, |
| 278 | OperationType::avg, |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 279 | CollectionTimeScope::startup, |
| 280 | CollectionDuration(250ms)}}}); |
| 281 | auto metrics = getMetricsFromReadingParams(newParams); |
| 282 | |
| 283 | EXPECT_CALL(*reportFactoryMock, updateMetrics(_, _, _)) |
| 284 | .WillOnce(SetArgReferee<0>(metrics)); |
| 285 | EXPECT_THAT( |
Krzysztof Grobelny | cff70c1 | 2022-10-27 07:16:08 +0000 | [diff] [blame] | 286 | setProperty(sut->getPath(), "ReadingParameters", newParams).value(), |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 287 | Eq(boost::system::errc::success)); |
Krzysztof Grobelny | cff70c1 | 2022-10-27 07:16:08 +0000 | [diff] [blame] | 288 | EXPECT_THAT( |
| 289 | getProperty<ReadingParameters>(sut->getPath(), "ReadingParameters"), |
| 290 | Eq(newParams)); |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | TEST_F(TestReport, setReportActionsWithValidNewActions) |
| 294 | { |
Krzysztof Grobelny | cff70c1 | 2022-10-27 07:16:08 +0000 | [diff] [blame] | 295 | std::vector<std::string> newActions = { |
| 296 | utils::enumToString(ReportAction::emitsReadingsUpdate)}; |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 297 | std::vector<std::string> currActions = |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 298 | utils::transform(defaultParams().reportActions(), |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 299 | [](const auto v) { return utils::enumToString(v); }); |
| 300 | |
| 301 | EXPECT_THAT(newActions, Ne(currActions)); |
| 302 | EXPECT_THAT( |
| 303 | setProperty(sut->getPath(), "ReportActions", newActions).value(), |
| 304 | Eq(boost::system::errc::success)); |
| 305 | EXPECT_THAT( |
| 306 | getProperty<std::vector<std::string>>(sut->getPath(), "ReportActions"), |
Krzysztof Grobelny | cff70c1 | 2022-10-27 07:16:08 +0000 | [diff] [blame] | 307 | UnorderedElementsAre( |
| 308 | utils::enumToString(ReportAction::emitsReadingsUpdate), |
| 309 | utils::enumToString(ReportAction::logToMetricReportsCollection))); |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | TEST_F(TestReport, setReportActionsWithValidUnsortedActions) |
| 313 | { |
Krzysztof Grobelny | cff70c1 | 2022-10-27 07:16:08 +0000 | [diff] [blame] | 314 | std::vector<std::string> newActions = { |
| 315 | utils::enumToString(ReportAction::logToMetricReportsCollection), |
| 316 | utils::enumToString(ReportAction::emitsReadingsUpdate)}; |
| 317 | std::vector<std::string> expectedActions = { |
| 318 | utils::enumToString(ReportAction::emitsReadingsUpdate), |
| 319 | utils::enumToString(ReportAction::logToMetricReportsCollection)}; |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 320 | std::vector<std::string> currActions = |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 321 | utils::transform(defaultParams().reportActions(), |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 322 | [](const auto v) { return utils::enumToString(v); }); |
| 323 | |
| 324 | EXPECT_THAT(newActions, Ne(currActions)); |
| 325 | EXPECT_THAT( |
| 326 | setProperty(sut->getPath(), "ReportActions", newActions).value(), |
| 327 | Eq(boost::system::errc::success)); |
| 328 | EXPECT_THAT( |
| 329 | getProperty<std::vector<std::string>>(sut->getPath(), "ReportActions"), |
| 330 | Eq(expectedActions)); |
| 331 | } |
| 332 | |
| 333 | TEST_F(TestReport, setReportActionsWithEmptyActions) |
| 334 | { |
| 335 | std::vector<std::string> newActions = {}; |
Krzysztof Grobelny | cff70c1 | 2022-10-27 07:16:08 +0000 | [diff] [blame] | 336 | std::vector<std::string> expectedActions = { |
| 337 | utils::enumToString(ReportAction::logToMetricReportsCollection)}; |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 338 | std::vector<std::string> currActions = |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 339 | utils::transform(defaultParams().reportActions(), |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 340 | [](const auto v) { return utils::enumToString(v); }); |
| 341 | |
| 342 | EXPECT_THAT(newActions, Ne(currActions)); |
| 343 | EXPECT_THAT( |
| 344 | setProperty(sut->getPath(), "ReportActions", newActions).value(), |
| 345 | Eq(boost::system::errc::success)); |
| 346 | EXPECT_THAT( |
| 347 | getProperty<std::vector<std::string>>(sut->getPath(), "ReportActions"), |
| 348 | Eq(expectedActions)); |
| 349 | } |
| 350 | |
| 351 | TEST_F(TestReport, setReportActionsWithInvalidActions) |
| 352 | { |
| 353 | std::vector<std::string> invalidActions = {"EmitsReadingsUpdate_1"}; |
| 354 | EXPECT_THAT( |
| 355 | setProperty(sut->getPath(), "ReportActions", invalidActions).value(), |
| 356 | Eq(boost::system::errc::invalid_argument)); |
| 357 | EXPECT_THAT( |
| 358 | getProperty<std::vector<std::string>>(sut->getPath(), "ReportActions"), |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 359 | Eq(utils::transform(defaultParams().reportActions(), [](const auto v) { |
Patrick Williams | c7935fa | 2023-10-20 11:19:30 -0500 | [diff] [blame] | 360 | return utils::enumToString(v); |
| 361 | }))); |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | TEST_F(TestReport, createReportWithEmptyActions) |
| 365 | { |
Krzysztof Grobelny | cff70c1 | 2022-10-27 07:16:08 +0000 | [diff] [blame] | 366 | std::vector<std::string> expectedActions = { |
| 367 | utils::enumToString(ReportAction::logToMetricReportsCollection)}; |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 368 | |
| 369 | sut = makeReport(ReportParams().reportId("TestId_1").reportActions({})); |
| 370 | EXPECT_THAT( |
| 371 | getProperty<std::vector<std::string>>(sut->getPath(), "ReportActions"), |
| 372 | Eq(expectedActions)); |
| 373 | } |
| 374 | |
| 375 | TEST_F(TestReport, createReportWithValidUnsortedActions) |
| 376 | { |
Krzysztof Grobelny | cff70c1 | 2022-10-27 07:16:08 +0000 | [diff] [blame] | 377 | std::vector<std::string> newActions = { |
| 378 | utils::enumToString(ReportAction::logToMetricReportsCollection), |
| 379 | utils::enumToString(ReportAction::emitsReadingsUpdate)}; |
| 380 | std::vector<std::string> expectedActions = { |
| 381 | utils::enumToString(ReportAction::emitsReadingsUpdate), |
| 382 | utils::enumToString(ReportAction::logToMetricReportsCollection)}; |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 383 | |
| 384 | sut = makeReport( |
| 385 | ReportParams() |
| 386 | .reportId("TestId_1") |
| 387 | .reportActions(utils::transform(newActions, [](const auto& action) { |
Patrick Williams | c7935fa | 2023-10-20 11:19:30 -0500 | [diff] [blame] | 388 | return utils::toReportAction(action); |
| 389 | }))); |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 390 | EXPECT_THAT( |
| 391 | getProperty<std::vector<std::string>>(sut->getPath(), "ReportActions"), |
| 392 | Eq(expectedActions)); |
| 393 | } |
| 394 | |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 395 | TEST_F(TestReport, setEnabledWithNewValue) |
| 396 | { |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 397 | bool newValue = !defaultParams().enabled(); |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 398 | EXPECT_THAT(setProperty(sut->getPath(), "Enabled", newValue).value(), |
| 399 | Eq(boost::system::errc::success)); |
| 400 | EXPECT_THAT(getProperty<bool>(sut->getPath(), "Enabled"), Eq(newValue)); |
| 401 | } |
| 402 | |
Krzysztof Grobelny | cff70c1 | 2022-10-27 07:16:08 +0000 | [diff] [blame] | 403 | TEST_F(TestReport, setReportingPropertiesWithValidValues) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 404 | { |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 405 | uint64_t newValue = ReportManager::minInterval.count() * 42; |
Krzysztof Grobelny | cff70c1 | 2022-10-27 07:16:08 +0000 | [diff] [blame] | 406 | EXPECT_THAT(callMethod(sut->getPath(), "SetReportingProperties", |
| 407 | utils::enumToString(ReportingType::periodic), |
| 408 | newValue), |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 409 | Eq(boost::system::errc::success)); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 410 | EXPECT_THAT(getProperty<uint64_t>(sut->getPath(), "Interval"), |
| 411 | Eq(newValue)); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 412 | } |
| 413 | |
Krzysztof Grobelny | cff70c1 | 2022-10-27 07:16:08 +0000 | [diff] [blame] | 414 | TEST_F(TestReport, failsToSetInvalidInterval) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 415 | { |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 416 | uint64_t newValue = ReportManager::minInterval.count() - 1; |
Krzysztof Grobelny | cff70c1 | 2022-10-27 07:16:08 +0000 | [diff] [blame] | 417 | |
| 418 | EXPECT_THAT( |
| 419 | callMethod(sut->getPath(), "SetReportingProperties", "", newValue), |
| 420 | Eq(boost::system::errc::invalid_argument)); |
| 421 | |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 422 | EXPECT_THAT(getProperty<uint64_t>(sut->getPath(), "Interval"), |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 423 | Eq(defaultParams().interval().count())); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 424 | } |
| 425 | |
Krzysztof Grobelny | cff70c1 | 2022-10-27 07:16:08 +0000 | [diff] [blame] | 426 | TEST_F(TestReport, failsToSetIncompatibleInterval) |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 427 | { |
| 428 | auto report = makeReport(defaultParams() |
| 429 | .reportId("report2") |
| 430 | .reportingType(ReportingType::onRequest) |
| 431 | .interval(Milliseconds{0})); |
| 432 | |
Krzysztof Grobelny | cff70c1 | 2022-10-27 07:16:08 +0000 | [diff] [blame] | 433 | uint64_t newValue = ReportManager::minInterval.count(); |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 434 | |
| 435 | EXPECT_THAT( |
Krzysztof Grobelny | cff70c1 | 2022-10-27 07:16:08 +0000 | [diff] [blame] | 436 | callMethod(report->getPath(), "SetReportingProperties", "", newValue), |
| 437 | Eq(boost::system::errc::invalid_argument)); |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 438 | |
| 439 | EXPECT_THAT(getProperty<uint64_t>(report->getPath(), "Interval"), |
Krzysztof Grobelny | cff70c1 | 2022-10-27 07:16:08 +0000 | [diff] [blame] | 440 | Eq(defaultParams().interval().count())); |
| 441 | } |
| 442 | |
| 443 | TEST_F(TestReport, failsToSetInvalidReportingType) |
| 444 | { |
| 445 | auto report = makeReport(defaultParams() |
| 446 | .reportId("report2") |
| 447 | .reportingType(ReportingType::onRequest) |
| 448 | .interval(Milliseconds{0})); |
| 449 | |
| 450 | EXPECT_THAT(callMethod(sut->getPath(), "SetReportingProperties", "XYZ", |
| 451 | std::numeric_limits<uint64_t>::max()), |
| 452 | Eq(boost::system::errc::invalid_argument)); |
| 453 | |
| 454 | EXPECT_THAT(getProperty<std::string>(report->getPath(), "ReportingType"), |
| 455 | Eq(utils::enumToString(ReportingType::onRequest))); |
| 456 | } |
| 457 | |
| 458 | TEST_F(TestReport, failsToSetIncompatibleReportingType) |
| 459 | { |
| 460 | auto report = makeReport(defaultParams() |
| 461 | .reportId("report2") |
| 462 | .reportingType(ReportingType::onRequest) |
| 463 | .interval(Milliseconds{0})); |
| 464 | |
| 465 | EXPECT_THAT(callMethod(sut->getPath(), "SetReportingProperties", |
| 466 | utils::enumToString(ReportingType::periodic), |
| 467 | std::numeric_limits<uint64_t>::max()), |
| 468 | Eq(boost::system::errc::invalid_argument)); |
| 469 | |
| 470 | EXPECT_THAT(getProperty<std::string>(report->getPath(), "ReportingType"), |
| 471 | Eq(utils::enumToString(ReportingType::onRequest))); |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 472 | } |
| 473 | |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 474 | TEST_F(TestReport, settingEmitsReadingsUpdateHaveNoEffect) |
| 475 | { |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 476 | EXPECT_THAT( |
| 477 | setProperty(sut->getPath(), "EmitsReadingsUpdate", true).value(), |
| 478 | Eq(boost::system::errc::read_only_file_system)); |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 479 | EXPECT_THAT(getProperty<bool>(sut->getPath(), "EmitsReadingsUpdate"), |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 480 | Eq(utils::contains(defaultParams().reportActions(), |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 481 | ReportAction::emitsReadingsUpdate))); |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | TEST_F(TestReport, settingLogToMetricReportCollectionHaveNoEffect) |
| 485 | { |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 486 | EXPECT_THAT( |
| 487 | setProperty(sut->getPath(), "LogToMetricReportsCollection", true) |
| 488 | .value(), |
| 489 | Eq(boost::system::errc::read_only_file_system)); |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 490 | EXPECT_THAT( |
| 491 | getProperty<bool>(sut->getPath(), "LogToMetricReportsCollection"), |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 492 | Eq(utils::contains(defaultParams().reportActions(), |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 493 | ReportAction::logToMetricReportsCollection))); |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 494 | } |
| 495 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 496 | TEST_F(TestReport, settingPersistencyToFalseRemovesReportFromStorage) |
| 497 | { |
Krzysztof Grobelny | 493e62e | 2022-02-14 10:55:50 +0100 | [diff] [blame] | 498 | EXPECT_CALL(storageMock, store(_, _)).Times(0); |
| 499 | EXPECT_CALL(storageMock, remove(to_file_path(sut->getId()))) |
| 500 | .Times(AtLeast(1)); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 501 | |
| 502 | bool persistency = false; |
| 503 | EXPECT_THAT(setProperty(sut->getPath(), "Persistency", persistency).value(), |
| 504 | Eq(boost::system::errc::success)); |
| 505 | EXPECT_THAT(getProperty<bool>(sut->getPath(), "Persistency"), |
| 506 | Eq(persistency)); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | TEST_F(TestReport, deleteReport) |
| 510 | { |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 511 | EXPECT_CALL(*reportManagerMock, removeReport(sut.get())); |
| 512 | auto ec = deleteReport(sut->getPath()); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 513 | EXPECT_THAT(ec, Eq(boost::system::errc::success)); |
| 514 | } |
| 515 | |
| 516 | TEST_F(TestReport, deletingNonExistingReportReturnInvalidRequestDescriptor) |
| 517 | { |
Szymon Dompke | 1cdd7e4 | 2022-06-08 14:43:13 +0200 | [diff] [blame] | 518 | auto ec = |
| 519 | deleteReport(utils::constants::reportDirPath.str + "NonExisting"s); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 520 | EXPECT_THAT(ec.value(), Eq(EBADR)); |
| 521 | } |
| 522 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 523 | TEST_F(TestReport, deleteReportExpectThatFileIsRemoveFromStorage) |
| 524 | { |
Krzysztof Grobelny | 493e62e | 2022-02-14 10:55:50 +0100 | [diff] [blame] | 525 | EXPECT_CALL(storageMock, store(_, _)).Times(0); |
| 526 | EXPECT_CALL(storageMock, remove(to_file_path(sut->getId()))) |
| 527 | .Times(AtLeast(1)); |
| 528 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 529 | auto ec = deleteReport(sut->getPath()); |
| 530 | EXPECT_THAT(ec, Eq(boost::system::errc::success)); |
| 531 | } |
| 532 | |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 533 | TEST_F(TestReport, updatesTriggerIdWhenTriggerIsAdded) |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 534 | { |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 535 | utils::Messanger messanger(DbusEnvironment::getIoc()); |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 536 | |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 537 | messanger.send(messages::TriggerPresenceChangedInd{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 538 | messages::Presence::Exist, "trigger1", {defaultParams().reportId()}}); |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 539 | messanger.send(messages::TriggerPresenceChangedInd{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 540 | messages::Presence::Exist, "trigger1", {defaultParams().reportId()}}); |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 541 | messanger.send(messages::TriggerPresenceChangedInd{ |
| 542 | messages::Presence::Exist, "trigger2", {"someOtherReport"}}); |
| 543 | messanger.send(messages::TriggerPresenceChangedInd{ |
| 544 | messages::Presence::Exist, |
| 545 | "trigger3", |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 546 | {"someOtherReport", defaultParams().reportId()}}); |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 547 | |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 548 | EXPECT_THAT( |
Szymon Dompke | 1cdd7e4 | 2022-06-08 14:43:13 +0200 | [diff] [blame] | 549 | getProperty<std::vector<object_path>>(sut->getPath(), "Triggers"), |
| 550 | UnorderedElementsAre(utils::constants::triggerDirPath / "trigger1", |
| 551 | utils::constants::triggerDirPath / "trigger3")); |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 552 | } |
| 553 | |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 554 | TEST_F(TestReport, updatesTriggerIdWhenTriggerIsRemoved) |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 555 | { |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 556 | utils::Messanger messanger(DbusEnvironment::getIoc()); |
| 557 | |
| 558 | messanger.send(messages::TriggerPresenceChangedInd{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 559 | messages::Presence::Exist, "trigger1", {defaultParams().reportId()}}); |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 560 | messanger.send(messages::TriggerPresenceChangedInd{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 561 | messages::Presence::Exist, "trigger2", {defaultParams().reportId()}}); |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 562 | messanger.send(messages::TriggerPresenceChangedInd{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 563 | messages::Presence::Exist, "trigger3", {defaultParams().reportId()}}); |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 564 | |
| 565 | messanger.send(messages::TriggerPresenceChangedInd{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 566 | messages::Presence::Removed, "trigger1", {defaultParams().reportId()}}); |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 567 | messanger.send(messages::TriggerPresenceChangedInd{ |
| 568 | messages::Presence::Removed, "trigger2", {}}); |
| 569 | messanger.send(messages::TriggerPresenceChangedInd{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 570 | messages::Presence::Removed, "trigger1", {defaultParams().reportId()}}); |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 571 | |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 572 | EXPECT_THAT( |
Szymon Dompke | 1cdd7e4 | 2022-06-08 14:43:13 +0200 | [diff] [blame] | 573 | getProperty<std::vector<object_path>>(sut->getPath(), "Triggers"), |
| 574 | UnorderedElementsAre(utils::constants::triggerDirPath / "trigger3")); |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 575 | } |
| 576 | |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 577 | TEST_F(TestReport, updatesTriggerIdWhenTriggerIsModified) |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 578 | { |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 579 | utils::Messanger messanger(DbusEnvironment::getIoc()); |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 580 | |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 581 | messanger.send(messages::TriggerPresenceChangedInd{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 582 | messages::Presence::Exist, "trigger1", {defaultParams().reportId()}}); |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 583 | messanger.send(messages::TriggerPresenceChangedInd{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 584 | messages::Presence::Exist, "trigger2", {defaultParams().reportId()}}); |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 585 | messanger.send(messages::TriggerPresenceChangedInd{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 586 | messages::Presence::Exist, "trigger3", {defaultParams().reportId()}}); |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 587 | |
| 588 | messanger.send(messages::TriggerPresenceChangedInd{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 589 | messages::Presence::Exist, "trigger1", {defaultParams().reportId()}}); |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 590 | messanger.send(messages::TriggerPresenceChangedInd{ |
| 591 | messages::Presence::Exist, "trigger2", {}}); |
| 592 | messanger.send(messages::TriggerPresenceChangedInd{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 593 | messages::Presence::Exist, "trigger3", {defaultParams().reportId()}}); |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 594 | |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 595 | EXPECT_THAT( |
Szymon Dompke | 1cdd7e4 | 2022-06-08 14:43:13 +0200 | [diff] [blame] | 596 | getProperty<std::vector<object_path>>(sut->getPath(), "Triggers"), |
| 597 | UnorderedElementsAre(utils::constants::triggerDirPath / "trigger1", |
| 598 | utils::constants::triggerDirPath / "trigger3")); |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 599 | } |
| 600 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 601 | class TestReportStore : |
| 602 | public TestReport, |
| 603 | public WithParamInterface<std::pair<std::string, nlohmann::json>> |
| 604 | { |
Patrick Williams | 3a1c297 | 2023-05-10 07:51:04 -0500 | [diff] [blame] | 605 | void SetUp() override {} |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 606 | }; |
| 607 | |
| 608 | INSTANTIATE_TEST_SUITE_P( |
| 609 | _, TestReportStore, |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 610 | Values( |
| 611 | std::make_pair("Enabled"s, nlohmann::json(defaultParams().enabled())), |
Krzysztof Grobelny | 18e7101 | 2022-11-02 13:17:01 +0000 | [diff] [blame] | 612 | std::make_pair("Version"s, nlohmann::json(7)), |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 613 | std::make_pair("Id"s, nlohmann::json(defaultParams().reportId())), |
| 614 | std::make_pair("Name"s, nlohmann::json(defaultParams().reportName())), |
| 615 | std::make_pair("ReportingType", |
| 616 | nlohmann::json(defaultParams().reportingType())), |
| 617 | std::make_pair("ReportActions", nlohmann::json(utils::transform( |
| 618 | defaultParams().reportActions(), |
| 619 | [](const auto v) { |
Patrick Williams | 3a1c297 | 2023-05-10 07:51:04 -0500 | [diff] [blame] | 620 | return utils::toUnderlying(v); |
Patrick Williams | c7935fa | 2023-10-20 11:19:30 -0500 | [diff] [blame] | 621 | }))), |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 622 | std::make_pair("Interval", |
| 623 | nlohmann::json(defaultParams().interval().count())), |
| 624 | std::make_pair("AppendLimit", |
| 625 | nlohmann::json(ReportParams().appendLimit())), |
| 626 | std::make_pair( |
| 627 | "ReadingParameters", |
| 628 | nlohmann::json( |
| 629 | {{{tstring::SensorPath::str(), |
| 630 | {{{tstring::Service::str(), "Service"}, |
| 631 | {tstring::Path::str(), |
| 632 | "/xyz/openbmc_project/sensors/power/p1"}, |
| 633 | {tstring::Metadata::str(), "metadata1"}}}}, |
| 634 | {tstring::OperationType::str(), OperationType::avg}, |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 635 | {tstring::CollectionTimeScope::str(), |
| 636 | CollectionTimeScope::point}, |
| 637 | {tstring::CollectionDuration::str(), 0}}, |
| 638 | {{tstring::SensorPath::str(), |
| 639 | {{{tstring::Service::str(), "Service"}, |
| 640 | {tstring::Path::str(), |
| 641 | "/xyz/openbmc_project/sensors/power/p2"}, |
| 642 | {tstring::Metadata::str(), "metadata2"}}}}, |
| 643 | {tstring::OperationType::str(), OperationType::avg}, |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 644 | {tstring::CollectionTimeScope::str(), |
| 645 | CollectionTimeScope::point}, |
| 646 | {tstring::CollectionDuration::str(), 0}}})))); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 647 | |
| 648 | TEST_P(TestReportStore, settingPersistencyToTrueStoresReport) |
| 649 | { |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 650 | sut = makeReport(defaultParams()); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 651 | |
| 652 | { |
| 653 | InSequence seq; |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 654 | EXPECT_CALL(storageMock, remove(to_file_path(sut->getId()))); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 655 | EXPECT_CALL(checkPoint, Call()); |
Krzysztof Grobelny | 493e62e | 2022-02-14 10:55:50 +0100 | [diff] [blame] | 656 | EXPECT_CALL(storageMock, store(to_file_path(sut->getId()), _)); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | setProperty(sut->getPath(), "Persistency", false); |
| 660 | checkPoint.Call(); |
| 661 | setProperty(sut->getPath(), "Persistency", true); |
| 662 | |
| 663 | const auto& [key, value] = GetParam(); |
| 664 | |
| 665 | ASSERT_THAT(storedConfiguration.at(key), Eq(value)); |
| 666 | } |
| 667 | |
| 668 | TEST_P(TestReportStore, reportIsSavedToStorageAfterCreated) |
| 669 | { |
Krzysztof Grobelny | 493e62e | 2022-02-14 10:55:50 +0100 | [diff] [blame] | 670 | EXPECT_CALL(storageMock, |
| 671 | store(to_file_path(defaultParams().reportId()), _)); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 672 | |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 673 | sut = makeReport(defaultParams()); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 674 | |
| 675 | const auto& [key, value] = GetParam(); |
| 676 | |
| 677 | ASSERT_THAT(storedConfiguration.at(key), Eq(value)); |
| 678 | } |
| 679 | |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 680 | class TestReportValidNames : |
| 681 | public TestReport, |
| 682 | public WithParamInterface<ReportParams> |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 683 | { |
| 684 | public: |
Patrick Williams | 3a1c297 | 2023-05-10 07:51:04 -0500 | [diff] [blame] | 685 | void SetUp() override {} |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 686 | }; |
| 687 | |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 688 | INSTANTIATE_TEST_SUITE_P( |
| 689 | ValidNames, TestReportValidNames, |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 690 | Values(defaultParams().reportName("Valid_1"), |
| 691 | defaultParams().reportName("Valid_1/Valid_2"), |
| 692 | defaultParams().reportName("Valid_1/Valid_2/Valid_3"))); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 693 | |
| 694 | TEST_P(TestReportValidNames, reportCtorDoesNotThrowOnValidName) |
| 695 | { |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 696 | EXPECT_NO_THROW(makeReport(GetParam())); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 697 | } |
| 698 | |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 699 | class TestReportInvalidIds : |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 700 | public TestReport, |
| 701 | public WithParamInterface<ReportParams> |
| 702 | { |
| 703 | public: |
Patrick Williams | 3a1c297 | 2023-05-10 07:51:04 -0500 | [diff] [blame] | 704 | void SetUp() override {} |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 705 | }; |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 706 | |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 707 | INSTANTIATE_TEST_SUITE_P(InvalidNames, TestReportInvalidIds, |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 708 | Values(defaultParams().reportId("/"), |
| 709 | defaultParams().reportId("/Invalid"), |
| 710 | defaultParams().reportId("Invalid/"), |
| 711 | defaultParams().reportId("Invalid/Invalid/"), |
| 712 | defaultParams().reportId("Invalid?"))); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 713 | |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 714 | TEST_P(TestReportInvalidIds, failsToCreateReportWithInvalidName) |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 715 | { |
| 716 | EXPECT_CALL(storageMock, store).Times(0); |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 717 | |
| 718 | EXPECT_THROW(makeReport(GetParam()), sdbusplus::exception::SdBusError); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 719 | } |
| 720 | |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 721 | class TestReportAllReportTypes : |
| 722 | public TestReport, |
| 723 | public WithParamInterface<ReportParams> |
| 724 | { |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 725 | public: |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 726 | void SetUp() override |
| 727 | { |
| 728 | sut = makeReport(GetParam()); |
| 729 | } |
| 730 | }; |
| 731 | |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 732 | INSTANTIATE_TEST_SUITE_P( |
| 733 | _, TestReportAllReportTypes, |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 734 | Values(defaultParams().reportingType(ReportingType::onRequest), |
| 735 | defaultParams().reportingType(ReportingType::onChange), |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 736 | defaultParams() |
| 737 | .reportingType(ReportingType::periodic) |
| 738 | .interval(ReportManager::minInterval))); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 739 | |
| 740 | TEST_P(TestReportAllReportTypes, returnPropertValueOfReportType) |
| 741 | { |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 742 | EXPECT_THAT(utils::toReportingType( |
| 743 | getProperty<std::string>(sut->getPath(), "ReportingType")), |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 744 | Eq(GetParam().reportingType())); |
| 745 | } |
| 746 | |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 747 | TEST_P(TestReportAllReportTypes, readingsAreUpdated) |
| 748 | { |
| 749 | clockFake.system.advance(10ms); |
| 750 | |
| 751 | messanger.send(messages::UpdateReportInd{{sut->getId()}}); |
Patrick Williams | 3a1c297 | 2023-05-10 07:51:04 -0500 | [diff] [blame] | 752 | const auto [timestamp, readings] = getProperty<Readings>(sut->getPath(), |
| 753 | "Readings"); |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 754 | |
| 755 | EXPECT_THAT(Milliseconds{timestamp}, Eq(systemTimestamp + 10ms)); |
| 756 | } |
| 757 | |
| 758 | TEST_P(TestReportAllReportTypes, readingsAreNotUpdatedWhenReportIsDisabled) |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 759 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 760 | clockFake.system.advance(10ms); |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 761 | |
| 762 | setProperty(sut->getPath(), "Enabled", false); |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 763 | messanger.send(messages::UpdateReportInd{{sut->getId()}}); |
Patrick Williams | 3a1c297 | 2023-05-10 07:51:04 -0500 | [diff] [blame] | 764 | const auto [timestamp, readings] = getProperty<Readings>(sut->getPath(), |
| 765 | "Readings"); |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 766 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 767 | EXPECT_THAT(Milliseconds{timestamp}, Eq(0ms)); |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 768 | } |
| 769 | |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 770 | TEST_P(TestReportAllReportTypes, readingsAreNotUpdatedWhenReportIdDiffers) |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 771 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 772 | clockFake.system.advance(10ms); |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 773 | |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 774 | messanger.send(messages::UpdateReportInd{{sut->getId() + "x"s}}); |
Patrick Williams | 3a1c297 | 2023-05-10 07:51:04 -0500 | [diff] [blame] | 775 | const auto [timestamp, readings] = getProperty<Readings>(sut->getPath(), |
| 776 | "Readings"); |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 777 | |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 778 | EXPECT_THAT(Milliseconds{timestamp}, Eq(0ms)); |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 779 | } |
| 780 | |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 781 | class TestReportOnRequestType : public TestReport |
| 782 | { |
| 783 | void SetUp() override |
| 784 | { |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 785 | sut = |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 786 | makeReport(defaultParams().reportingType(ReportingType::onRequest)); |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 787 | } |
| 788 | }; |
| 789 | |
| 790 | TEST_F(TestReportOnRequestType, updatesReadingTimestamp) |
| 791 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 792 | clockFake.system.advance(10ms); |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 793 | |
| 794 | ASSERT_THAT(update(sut->getPath()), Eq(boost::system::errc::success)); |
| 795 | |
Patrick Williams | 3a1c297 | 2023-05-10 07:51:04 -0500 | [diff] [blame] | 796 | const auto [timestamp, readings] = getProperty<Readings>(sut->getPath(), |
| 797 | "Readings"); |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 798 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 799 | EXPECT_THAT(Milliseconds{timestamp}, Eq(systemTimestamp + 10ms)); |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | TEST_F(TestReportOnRequestType, updatesReadingWhenUpdateIsCalled) |
| 803 | { |
| 804 | ASSERT_THAT(update(sut->getPath()), Eq(boost::system::errc::success)); |
| 805 | |
Patrick Williams | 3a1c297 | 2023-05-10 07:51:04 -0500 | [diff] [blame] | 806 | const auto [timestamp, readings] = getProperty<Readings>(sut->getPath(), |
| 807 | "Readings"); |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 808 | |
Krzysztof Grobelny | cff70c1 | 2022-10-27 07:16:08 +0000 | [diff] [blame] | 809 | EXPECT_THAT(readings, ElementsAre(std::make_tuple("b"s, 17.1, 114u), |
| 810 | std::make_tuple("bb"s, 42.0, 74u))); |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | class TestReportNonOnRequestType : |
| 814 | public TestReport, |
| 815 | public WithParamInterface<ReportParams> |
| 816 | { |
| 817 | void SetUp() override |
| 818 | { |
| 819 | sut = makeReport(GetParam()); |
| 820 | } |
| 821 | }; |
| 822 | |
Krzysztof Grobelny | cff70c1 | 2022-10-27 07:16:08 +0000 | [diff] [blame] | 823 | INSTANTIATE_TEST_SUITE_P(_, TestReportNonOnRequestType, |
| 824 | Values(defaultParams() |
| 825 | .reportingType(ReportingType::periodic) |
| 826 | .interval(ReportManager::minInterval * 10), |
| 827 | defaultParams() |
| 828 | .reportingType(ReportingType::onChange) |
| 829 | .interval(Milliseconds(0)))); |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 830 | |
| 831 | TEST_P(TestReportNonOnRequestType, readingsAreNotUpdateOnUpdateCall) |
| 832 | { |
| 833 | ASSERT_THAT(update(sut->getPath()), Eq(boost::system::errc::success)); |
| 834 | |
| 835 | EXPECT_THAT(getProperty<Readings>(sut->getPath(), "Readings"), |
| 836 | Eq(Readings{})); |
| 837 | } |
| 838 | |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 839 | class TestReportNonPeriodicReport : |
| 840 | public TestReport, |
| 841 | public WithParamInterface<ReportParams> |
| 842 | { |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 843 | public: |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 844 | void SetUp() override |
| 845 | { |
| 846 | sut = makeReport(GetParam()); |
| 847 | } |
| 848 | }; |
| 849 | |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 850 | INSTANTIATE_TEST_SUITE_P( |
| 851 | _, TestReportNonPeriodicReport, |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 852 | Values(defaultParams().reportingType(ReportingType::onRequest), |
| 853 | defaultParams().reportingType(ReportingType::onChange))); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 854 | |
| 855 | TEST_P(TestReportNonPeriodicReport, readingsAreNotUpdatedAfterIntervalExpires) |
| 856 | { |
| 857 | DbusEnvironment::sleepFor(ReportManager::minInterval + 1ms); |
| 858 | |
| 859 | EXPECT_THAT(getProperty<Readings>(sut->getPath(), "Readings"), |
| 860 | Eq(Readings{})); |
| 861 | } |
| 862 | |
| 863 | class TestReportPeriodicReport : public TestReport |
| 864 | { |
| 865 | void SetUp() override |
| 866 | { |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 867 | sut = makeReport(defaultParams() |
Krzysztof Grobelny | 18e7101 | 2022-11-02 13:17:01 +0000 | [diff] [blame] | 868 | .appendLimit(2u) |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 869 | .reportingType(ReportingType::periodic) |
| 870 | .interval(ReportManager::minInterval)); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 871 | } |
| 872 | }; |
| 873 | |
| 874 | TEST_F(TestReportPeriodicReport, readingTimestampIsUpdatedAfterIntervalExpires) |
| 875 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 876 | clockFake.system.advance(10ms); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 877 | DbusEnvironment::sleepFor(ReportManager::minInterval + 1ms); |
| 878 | |
Patrick Williams | 3a1c297 | 2023-05-10 07:51:04 -0500 | [diff] [blame] | 879 | const auto [timestamp, readings] = getProperty<Readings>(sut->getPath(), |
| 880 | "Readings"); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 881 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 882 | EXPECT_THAT(Milliseconds{timestamp}, Eq(systemTimestamp + 10ms)); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | TEST_F(TestReportPeriodicReport, readingsAreUpdatedAfterIntervalExpires) |
| 886 | { |
| 887 | DbusEnvironment::sleepFor(ReportManager::minInterval + 1ms); |
| 888 | |
Patrick Williams | 3a1c297 | 2023-05-10 07:51:04 -0500 | [diff] [blame] | 889 | const auto [timestamp, readings] = getProperty<Readings>(sut->getPath(), |
| 890 | "Readings"); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 891 | |
Krzysztof Grobelny | cff70c1 | 2022-10-27 07:16:08 +0000 | [diff] [blame] | 892 | EXPECT_THAT(readings, ElementsAre(std::make_tuple("b"s, 17.1, 114u), |
| 893 | std::make_tuple("bb"s, 42.0, 74u))); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 894 | } |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 895 | |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 896 | struct ReportUpdatesReportParams |
| 897 | { |
| 898 | ReportParams reportParams; |
| 899 | std::vector<ReadingData> expectedReadings; |
| 900 | bool expectedEnabled; |
Michal Orzel | b3e03d2 | 2024-06-28 13:55:47 +0200 | [diff] [blame^] | 901 | |
| 902 | friend void PrintTo(const ReportUpdatesReportParams& params, |
| 903 | std::ostream* os) |
| 904 | { |
| 905 | *os << "{ ReportParams: "; |
| 906 | PrintTo(params.reportParams, os); |
| 907 | *os << ", ExpectedReadings: "; |
| 908 | PrintTo(params.expectedReadings, os); |
| 909 | *os << ", ExpectedEnabled: " << params.expectedEnabled << " }"; |
| 910 | } |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 911 | }; |
| 912 | |
| 913 | class TestReportWithReportUpdatesAndLimit : |
| 914 | public TestReport, |
| 915 | public WithParamInterface<ReportUpdatesReportParams> |
| 916 | { |
Krzysztof Grobelny | a8182be | 2022-07-04 11:26:20 +0200 | [diff] [blame] | 917 | public: |
Patrick Williams | 3a1c297 | 2023-05-10 07:51:04 -0500 | [diff] [blame] | 918 | void SetUp() override {} |
Krzysztof Grobelny | a8182be | 2022-07-04 11:26:20 +0200 | [diff] [blame] | 919 | |
| 920 | void changeReport(ReportingType rt, Milliseconds interval) |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 921 | { |
Krzysztof Grobelny | cff70c1 | 2022-10-27 07:16:08 +0000 | [diff] [blame] | 922 | callMethod(sut->getPath(), "SetReportingProperties", |
| 923 | utils::enumToString(rt), interval.count()); |
Krzysztof Grobelny | a8182be | 2022-07-04 11:26:20 +0200 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | auto readings() |
| 927 | { |
Patrick Williams | 3a1c297 | 2023-05-10 07:51:04 -0500 | [diff] [blame] | 928 | auto [timestamp, readings] = getProperty<Readings>(sut->getPath(), |
| 929 | "Readings"); |
Krzysztof Grobelny | a8182be | 2022-07-04 11:26:20 +0200 | [diff] [blame] | 930 | return readings; |
| 931 | } |
| 932 | |
| 933 | void updateReportFourTimes() |
| 934 | { |
| 935 | for (int i = 0; i < 4; i++) |
| 936 | { |
| 937 | messanger.send(messages::UpdateReportInd{{sut->getId()}}); |
| 938 | } |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 939 | } |
| 940 | }; |
| 941 | |
| 942 | INSTANTIATE_TEST_SUITE_P( |
| 943 | _, TestReportWithReportUpdatesAndLimit, |
| 944 | Values( |
| 945 | ReportUpdatesReportParams{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 946 | defaultParams() |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 947 | .reportUpdates(ReportUpdates::appendWrapsWhenFull) |
| 948 | .appendLimit(5), |
Krzysztof Grobelny | cff70c1 | 2022-10-27 07:16:08 +0000 | [diff] [blame] | 949 | std::vector<ReadingData>{{std::make_tuple("bb"s, 42.0, 74u), |
| 950 | std::make_tuple("b"s, 17.1, 114u), |
| 951 | std::make_tuple("bb"s, 42.0, 74u), |
| 952 | std::make_tuple("bb"s, 42.0, 74u), |
| 953 | std::make_tuple("b"s, 17.1, 114u)}}, |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 954 | true}, |
| 955 | ReportUpdatesReportParams{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 956 | defaultParams() |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 957 | .reportUpdates(ReportUpdates::appendWrapsWhenFull) |
| 958 | .appendLimit(4), |
Krzysztof Grobelny | cff70c1 | 2022-10-27 07:16:08 +0000 | [diff] [blame] | 959 | std::vector<ReadingData>{{std::make_tuple("b"s, 17.1, 114u), |
| 960 | std::make_tuple("bb"s, 42.0, 74u), |
| 961 | std::make_tuple("b"s, 17.1, 114u), |
| 962 | std::make_tuple("bb"s, 42.0, 74u)}}, |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 963 | true}, |
| 964 | ReportUpdatesReportParams{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 965 | defaultParams() |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 966 | .reportUpdates(ReportUpdates::appendWrapsWhenFull) |
| 967 | .appendLimit(0), |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 968 | std::vector<ReadingData>{}, true}, |
| 969 | ReportUpdatesReportParams{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 970 | defaultParams() |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 971 | .reportUpdates(ReportUpdates::appendStopsWhenFull) |
| 972 | .appendLimit(10), |
Krzysztof Grobelny | cff70c1 | 2022-10-27 07:16:08 +0000 | [diff] [blame] | 973 | std::vector<ReadingData>{{std::make_tuple("b"s, 17.1, 114u), |
| 974 | std::make_tuple("bb"s, 42.0, 74u), |
| 975 | std::make_tuple("b"s, 17.1, 114u), |
| 976 | std::make_tuple("bb"s, 42.0, 74u), |
| 977 | std::make_tuple("b"s, 17.1, 114u), |
| 978 | std::make_tuple("bb"s, 42.0, 74u), |
| 979 | std::make_tuple("b"s, 17.1, 114u), |
| 980 | std::make_tuple("bb"s, 42.0, 74u)}}, |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 981 | true}, |
| 982 | ReportUpdatesReportParams{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 983 | defaultParams() |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 984 | .reportUpdates(ReportUpdates::appendStopsWhenFull) |
| 985 | .appendLimit(5), |
Krzysztof Grobelny | cff70c1 | 2022-10-27 07:16:08 +0000 | [diff] [blame] | 986 | std::vector<ReadingData>{{std::make_tuple("b"s, 17.1, 114u), |
| 987 | std::make_tuple("bb"s, 42.0, 74u), |
| 988 | std::make_tuple("b"s, 17.1, 114u), |
| 989 | std::make_tuple("bb"s, 42.0, 74u), |
| 990 | std::make_tuple("b"s, 17.1, 114u)}}, |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 991 | false}, |
| 992 | ReportUpdatesReportParams{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 993 | defaultParams() |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 994 | .reportUpdates(ReportUpdates::appendStopsWhenFull) |
| 995 | .appendLimit(4), |
Krzysztof Grobelny | cff70c1 | 2022-10-27 07:16:08 +0000 | [diff] [blame] | 996 | std::vector<ReadingData>{{std::make_tuple("b"s, 17.1, 114u), |
| 997 | std::make_tuple("bb"s, 42.0, 74u), |
| 998 | std::make_tuple("b"s, 17.1, 114u), |
| 999 | std::make_tuple("bb"s, 42.0, 74u)}}, |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 1000 | false}, |
| 1001 | ReportUpdatesReportParams{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 1002 | defaultParams() |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 1003 | .reportUpdates(ReportUpdates::appendStopsWhenFull) |
| 1004 | .appendLimit(0), |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 1005 | std::vector<ReadingData>{}, false}, |
| 1006 | ReportUpdatesReportParams{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 1007 | defaultParams() |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 1008 | .reportUpdates(ReportUpdates::overwrite) |
| 1009 | .appendLimit(500), |
Krzysztof Grobelny | cff70c1 | 2022-10-27 07:16:08 +0000 | [diff] [blame] | 1010 | std::vector<ReadingData>{{std::make_tuple("b"s, 17.1, 114u), |
| 1011 | std::make_tuple("bb"s, 42.0, 74u)}}, |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 1012 | true}, |
| 1013 | ReportUpdatesReportParams{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 1014 | defaultParams() |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 1015 | .reportUpdates(ReportUpdates::overwrite) |
| 1016 | .appendLimit(1), |
Krzysztof Grobelny | cff70c1 | 2022-10-27 07:16:08 +0000 | [diff] [blame] | 1017 | std::vector<ReadingData>{{std::make_tuple("b"s, 17.1, 114u), |
| 1018 | std::make_tuple("bb"s, 42.0, 74u)}}, |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 1019 | true}, |
| 1020 | ReportUpdatesReportParams{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 1021 | defaultParams() |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 1022 | .reportUpdates(ReportUpdates::overwrite) |
| 1023 | .appendLimit(0), |
Krzysztof Grobelny | cff70c1 | 2022-10-27 07:16:08 +0000 | [diff] [blame] | 1024 | std::vector<ReadingData>{{std::make_tuple("b"s, 17.1, 114u), |
| 1025 | std::make_tuple("bb"s, 42.0, 74u)}}, |
Krzysztof Grobelny | a8182be | 2022-07-04 11:26:20 +0200 | [diff] [blame] | 1026 | true}, |
| 1027 | ReportUpdatesReportParams{ |
| 1028 | defaultParams() |
| 1029 | .reportUpdates(ReportUpdates::appendStopsWhenFull) |
Krzysztof Grobelny | 18e7101 | 2022-11-02 13:17:01 +0000 | [diff] [blame] | 1030 | .appendLimit(2u), |
Krzysztof Grobelny | cff70c1 | 2022-10-27 07:16:08 +0000 | [diff] [blame] | 1031 | std::vector<ReadingData>{{std::make_tuple("b"s, 17.1, 114u), |
| 1032 | std::make_tuple("bb"s, 42.0, 74u)}}, |
Krzysztof Grobelny | a8182be | 2022-07-04 11:26:20 +0200 | [diff] [blame] | 1033 | false})); |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 1034 | |
| 1035 | TEST_P(TestReportWithReportUpdatesAndLimit, |
| 1036 | readingsAreUpdatedAfterIntervalExpires) |
| 1037 | { |
Krzysztof Grobelny | a8182be | 2022-07-04 11:26:20 +0200 | [diff] [blame] | 1038 | sut = makeReport(ReportParams(GetParam().reportParams) |
| 1039 | .reportingType(ReportingType::periodic) |
| 1040 | .interval(std::chrono::hours(1000))); |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 1041 | |
Krzysztof Grobelny | a8182be | 2022-07-04 11:26:20 +0200 | [diff] [blame] | 1042 | updateReportFourTimes(); |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 1043 | |
Krzysztof Grobelny | a8182be | 2022-07-04 11:26:20 +0200 | [diff] [blame] | 1044 | EXPECT_THAT(readings(), ElementsAreArray(GetParam().expectedReadings)); |
| 1045 | EXPECT_THAT(getProperty<bool>(sut->getPath(), "Enabled"), |
| 1046 | Eq(GetParam().expectedEnabled)); |
| 1047 | } |
| 1048 | |
| 1049 | TEST_P(TestReportWithReportUpdatesAndLimit, |
| 1050 | appendLimitIsRespectedAfterChangingToPeriodic) |
| 1051 | { |
| 1052 | sut = makeReport(ReportParams(GetParam().reportParams) |
Krzysztof Grobelny | 18e7101 | 2022-11-02 13:17:01 +0000 | [diff] [blame] | 1053 | .appendLimit(GetParam().expectedReadings.size()) |
Krzysztof Grobelny | a8182be | 2022-07-04 11:26:20 +0200 | [diff] [blame] | 1054 | .reportingType(ReportingType::onRequest) |
| 1055 | .interval(std::chrono::hours(0))); |
| 1056 | |
| 1057 | changeReport(ReportingType::periodic, std::chrono::hours(1000)); |
| 1058 | updateReportFourTimes(); |
| 1059 | |
| 1060 | EXPECT_THAT(readings(), ElementsAreArray(GetParam().expectedReadings)); |
| 1061 | EXPECT_THAT(getProperty<bool>(sut->getPath(), "Enabled"), |
| 1062 | Eq(GetParam().expectedEnabled)); |
| 1063 | } |
| 1064 | |
| 1065 | TEST_P(TestReportWithReportUpdatesAndLimit, |
| 1066 | appendLimitIsIgnoredAfterChangingToOnRequest) |
| 1067 | { |
| 1068 | sut = makeReport(ReportParams(GetParam().reportParams) |
| 1069 | .reportingType(ReportingType::periodic) |
| 1070 | .interval(std::chrono::hours(1000))); |
| 1071 | |
| 1072 | changeReport(ReportingType::onRequest, Milliseconds{0}); |
| 1073 | updateReportFourTimes(); |
| 1074 | |
| 1075 | EXPECT_THAT(readings(), SizeIs(2u)); |
| 1076 | EXPECT_THAT(getProperty<bool>(sut->getPath(), "Enabled"), Eq(true)); |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 1077 | } |
| 1078 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 1079 | class TestReportInitialization : public TestReport |
| 1080 | { |
| 1081 | public: |
| 1082 | void SetUp() override |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 1083 | { |
| 1084 | initMetricMocks(defaultParams().metricParameters()); |
| 1085 | } |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 1086 | |
Patrick Williams | 39cc6ac | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 1087 | void monitorProc(sdbusplus::message_t& msg) |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 1088 | { |
| 1089 | std::string iface; |
| 1090 | std::vector<std::pair<std::string, std::variant<Readings>>> |
| 1091 | changed_properties; |
| 1092 | std::vector<std::string> invalidated_properties; |
| 1093 | |
| 1094 | msg.read(iface, changed_properties, invalidated_properties); |
| 1095 | |
| 1096 | if (iface == Report::reportIfaceName) |
| 1097 | { |
| 1098 | for (const auto& [name, value] : changed_properties) |
| 1099 | { |
| 1100 | if (name == "Readings") |
| 1101 | { |
| 1102 | readingsUpdated.Call(); |
| 1103 | } |
| 1104 | } |
| 1105 | } |
| 1106 | } |
| 1107 | |
| 1108 | void makeMonitor() |
| 1109 | { |
Patrick Williams | 3a62ee1 | 2021-12-03 10:13:25 -0600 | [diff] [blame] | 1110 | monitor = std::make_unique<sdbusplus::bus::match_t>( |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 1111 | *DbusEnvironment::getBus(), |
| 1112 | sdbusplus::bus::match::rules::propertiesChanged( |
| 1113 | sut->getPath(), Report::reportIfaceName), |
| 1114 | [this](auto& msg) { monitorProc(msg); }); |
| 1115 | } |
| 1116 | |
Patrick Williams | 3a62ee1 | 2021-12-03 10:13:25 -0600 | [diff] [blame] | 1117 | std::unique_ptr<sdbusplus::bus::match_t> monitor; |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 1118 | MockFunction<void()> readingsUpdated; |
| 1119 | }; |
| 1120 | |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 1121 | TEST_F(TestReportInitialization, |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 1122 | registersForMetricUpdatesWhenOnChangeReportCreated) |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 1123 | { |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 1124 | std::vector<const interfaces::MetricListener*> args; |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 1125 | for (auto& metric : metricMocks) |
| 1126 | { |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 1127 | EXPECT_CALL(*metric, registerForUpdates(_)) |
| 1128 | .WillOnce(Invoke([&args](const interfaces::MetricListener& report) { |
Patrick Williams | c7935fa | 2023-10-20 11:19:30 -0500 | [diff] [blame] | 1129 | args.emplace_back(&report); |
| 1130 | })); |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 1131 | ; |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 1132 | } |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 1133 | |
| 1134 | sut = makeReport(defaultParams().reportingType(ReportingType::onChange)); |
| 1135 | |
| 1136 | EXPECT_THAT(args, SizeIs(metricMocks.size())); |
| 1137 | for (const auto* reportPtr : args) |
| 1138 | { |
| 1139 | EXPECT_THAT(reportPtr, Eq(sut.get())); |
| 1140 | } |
| 1141 | } |
| 1142 | |
| 1143 | TEST_F(TestReportInitialization, |
| 1144 | deregistersForMetricUpdatesWhenOnChangeReportDestroyed) |
| 1145 | { |
| 1146 | sut = makeReport(defaultParams().reportingType(ReportingType::onChange)); |
| 1147 | |
| 1148 | for (auto& metric : metricMocks) |
| 1149 | { |
| 1150 | EXPECT_CALL(*metric, |
| 1151 | unregisterFromUpdates(Ref( |
| 1152 | static_cast<interfaces::MetricListener&>(*sut.get())))); |
| 1153 | } |
| 1154 | |
| 1155 | sut = nullptr; |
| 1156 | } |
| 1157 | |
| 1158 | TEST_F(TestReportInitialization, |
| 1159 | metricsAreInitializedWhenEnabledReportConstructed) |
| 1160 | { |
| 1161 | for (auto& metric : metricMocks) |
| 1162 | { |
| 1163 | EXPECT_CALL(*metric, initialize()); |
| 1164 | } |
| 1165 | sut = makeReport(defaultParams().enabled(true)); |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 1166 | } |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 1167 | |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 1168 | TEST_F(TestReportInitialization, |
| 1169 | metricsAreNotInitializedWhenDisabledReportConstructed) |
| 1170 | { |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 1171 | for (auto& metric : metricMocks) |
| 1172 | { |
| 1173 | EXPECT_CALL(*metric, initialize()).Times(0); |
| 1174 | } |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 1175 | sut = makeReport(defaultParams().enabled(false)); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 1176 | } |
| 1177 | |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 1178 | TEST_F(TestReportInitialization, |
| 1179 | emitReadingsUpdateIsTrueReadingsPropertiesChangedSingalEmits) |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 1180 | { |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 1181 | EXPECT_CALL(readingsUpdated, Call()) |
| 1182 | .WillOnce( |
| 1183 | InvokeWithoutArgs(DbusEnvironment::setPromise("readingsUpdated"))); |
| 1184 | |
| 1185 | const auto elapsed = DbusEnvironment::measureTime([this] { |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 1186 | sut = makeReport(defaultParams() |
| 1187 | .reportingType(ReportingType::periodic) |
| 1188 | .reportActions({ReportAction::emitsReadingsUpdate}) |
| 1189 | .interval(ReportManager::minInterval)); |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 1190 | makeMonitor(); |
| 1191 | EXPECT_TRUE(DbusEnvironment::waitForFuture("readingsUpdated")); |
| 1192 | }); |
| 1193 | |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 1194 | EXPECT_THAT(elapsed, AllOf(Ge(ReportManager::minInterval), |
| 1195 | Lt(ReportManager::minInterval * 2))); |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | TEST_F(TestReportInitialization, |
| 1199 | emitReadingsUpdateIsFalseReadingsPropertiesChangesSigalDoesNotEmits) |
| 1200 | { |
| 1201 | EXPECT_CALL(readingsUpdated, Call()).Times(0); |
| 1202 | |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 1203 | sut = makeReport(defaultParams() |
| 1204 | .reportingType(ReportingType::periodic) |
Krzysztof Grobelny | cff70c1 | 2022-10-27 07:16:08 +0000 | [diff] [blame] | 1205 | .reportActions({}) |
| 1206 | .interval(Milliseconds(1000))); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 1207 | makeMonitor(); |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 1208 | DbusEnvironment::sleepFor(defaultParams().interval() * 2); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 1209 | } |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 1210 | |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 1211 | TEST_F(TestReportInitialization, triggerIdsPropertyIsInitialzed) |
| 1212 | { |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 1213 | for (const auto& triggerId : {"trigger1", "trigger2"}) |
| 1214 | { |
| 1215 | messanger.on_receive<messages::CollectTriggerIdReq>( |
Szymon Dompke | e0ed508 | 2022-05-20 10:54:25 +0200 | [diff] [blame] | 1216 | [this, triggerId](const auto& msg) { |
Patrick Williams | 3a1c297 | 2023-05-10 07:51:04 -0500 | [diff] [blame] | 1217 | messanger.send(messages::CollectTriggerIdResp{triggerId}); |
| 1218 | }); |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | sut = makeReport(ReportParams()); |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 1222 | |
| 1223 | EXPECT_THAT( |
Szymon Dompke | 1cdd7e4 | 2022-06-08 14:43:13 +0200 | [diff] [blame] | 1224 | getProperty<std::vector<object_path>>(sut->getPath(), "Triggers"), |
| 1225 | UnorderedElementsAre(utils::constants::triggerDirPath / "trigger1", |
| 1226 | utils::constants::triggerDirPath / "trigger2")); |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 1227 | } |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 1228 | |
Krzysztof Grobelny | 493e62e | 2022-02-14 10:55:50 +0100 | [diff] [blame] | 1229 | TEST_F(TestReportInitialization, |
| 1230 | metricValuesAreNotStoredForReportUpdatesDifferentThanAppendStopsWhenFull) |
| 1231 | { |
| 1232 | sut = makeReport(ReportParams() |
| 1233 | .reportingType(ReportingType::periodic) |
| 1234 | .interval(1h) |
| 1235 | .reportUpdates(ReportUpdates::appendWrapsWhenFull) |
| 1236 | .readings(Readings{{}, {{}}})); |
| 1237 | |
| 1238 | ASSERT_THAT(storedConfiguration.find("MetricValues"), |
| 1239 | Eq(storedConfiguration.end())); |
| 1240 | } |
| 1241 | |
| 1242 | TEST_F(TestReportInitialization, metricValuesAreNotStoredForOnRequestReport) |
| 1243 | { |
| 1244 | sut = makeReport(ReportParams() |
| 1245 | .reportingType(ReportingType::onRequest) |
| 1246 | .reportUpdates(ReportUpdates::appendStopsWhenFull) |
| 1247 | .readings(Readings{{}, {{}}})); |
| 1248 | |
| 1249 | ASSERT_THAT(storedConfiguration.find("MetricValues"), |
| 1250 | Eq(storedConfiguration.end())); |
| 1251 | } |
| 1252 | |
| 1253 | TEST_F(TestReportInitialization, |
| 1254 | metricValuesAreStoredForNonOnRequestReportWithAppendStopsWhenFull) |
| 1255 | { |
| 1256 | const auto readings = Readings{{}, {{}}}; |
| 1257 | |
| 1258 | sut = makeReport(ReportParams() |
| 1259 | .reportingType(ReportingType::periodic) |
| 1260 | .interval(1h) |
| 1261 | .reportUpdates(ReportUpdates::appendStopsWhenFull) |
| 1262 | .readings(readings)); |
| 1263 | |
| 1264 | ASSERT_THAT(storedConfiguration.at("MetricValues").get<LabeledReadings>(), |
| 1265 | Eq(utils::toLabeledReadings(readings))); |
| 1266 | } |
| 1267 | |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 1268 | class TestReportInitializationOnChangeReport : public TestReportInitialization |
| 1269 | { |
| 1270 | public: |
| 1271 | void SetUp() override |
| 1272 | { |
| 1273 | initMetricMocks(params.metricParameters()); |
| 1274 | } |
| 1275 | |
| 1276 | ReportParams params = defaultOnChangeParams(); |
| 1277 | }; |
| 1278 | |
| 1279 | TEST_F(TestReportInitializationOnChangeReport, |
| 1280 | doesntUpdateReadingsWhenNotRequired) |
| 1281 | { |
| 1282 | EXPECT_CALL(*metricMocks[0], updateReadings(_)).Times(0); |
| 1283 | |
| 1284 | ON_CALL(*metricMocks[0], isTimerRequired()).WillByDefault(Return(false)); |
| 1285 | |
| 1286 | sut = makeReport(params); |
| 1287 | |
| 1288 | DbusEnvironment::sleepFor(500ms); |
| 1289 | } |
| 1290 | |
| 1291 | TEST_F(TestReportInitializationOnChangeReport, updatesReadingsWhenRequired) |
| 1292 | { |
| 1293 | EXPECT_CALL(*metricMocks[0], updateReadings(_)) |
| 1294 | .WillOnce(Return()) |
| 1295 | .WillOnce( |
| 1296 | InvokeWithoutArgs(DbusEnvironment::setPromise("readingsUpdated"))) |
| 1297 | .WillRepeatedly(Return()); |
| 1298 | |
| 1299 | ON_CALL(*metricMocks[0], isTimerRequired()).WillByDefault(Return(true)); |
| 1300 | |
| 1301 | sut = makeReport(params); |
| 1302 | |
| 1303 | DbusEnvironment::waitForFuture("readingsUpdated"); |
| 1304 | } |