Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 1 | #include "dbus_environment.hpp" |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 2 | #include "helpers.hpp" |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 3 | #include "mocks/json_storage_mock.hpp" |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 4 | #include "mocks/report_factory_mock.hpp" |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 5 | #include "params/report_params.hpp" |
| 6 | #include "report.hpp" |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 7 | #include "report_manager.hpp" |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 8 | #include "utils/conversion.hpp" |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 9 | #include "utils/transform.hpp" |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 10 | |
| 11 | using namespace testing; |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 12 | using namespace std::string_literals; |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 13 | using namespace std::chrono_literals; |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 14 | |
| 15 | class TestReportManager : public Test |
| 16 | { |
| 17 | public: |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 18 | ReportParams reportParams; |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 19 | |
| 20 | std::unique_ptr<ReportFactoryMock> reportFactoryMockPtr = |
| 21 | std::make_unique<StrictMock<ReportFactoryMock>>(); |
| 22 | ReportFactoryMock& reportFactoryMock = *reportFactoryMockPtr; |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 23 | |
| 24 | std::unique_ptr<StorageMock> storageMockPtr = |
| 25 | std::make_unique<NiceMock<StorageMock>>(); |
| 26 | StorageMock& storageMock = *storageMockPtr; |
| 27 | |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 28 | std::unique_ptr<ReportMock> reportMockPtr = |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 29 | std::make_unique<NiceMock<ReportMock>>(reportParams.reportId()); |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 30 | ReportMock& reportMock = *reportMockPtr; |
| 31 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 32 | std::unique_ptr<ReportManager> sut; |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 33 | |
| 34 | MockFunction<void(std::string)> checkPoint; |
| 35 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 36 | void SetUp() override |
| 37 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 38 | EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _)) |
| 39 | .Times(AnyNumber()); |
| 40 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 41 | sut = std::make_unique<ReportManager>(std::move(reportFactoryMockPtr), |
| 42 | std::move(storageMockPtr), |
| 43 | DbusEnvironment::getObjServer()); |
| 44 | } |
| 45 | |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 46 | void TearDown() override |
| 47 | { |
| 48 | DbusEnvironment::synchronizeIoc(); |
| 49 | } |
| 50 | |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 51 | template <class... Args> |
| 52 | requires(sizeof...(Args) > 1) |
| 53 | std::pair<boost::system::error_code, std::string> addReport( |
| 54 | Args&&... args) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 55 | { |
| 56 | std::promise<std::pair<boost::system::error_code, std::string>> |
| 57 | addReportPromise; |
| 58 | DbusEnvironment::getBus()->async_method_call( |
| 59 | [&addReportPromise](boost::system::error_code ec, |
| 60 | const std::string& path) { |
| 61 | addReportPromise.set_value({ec, path}); |
| 62 | }, |
| 63 | DbusEnvironment::serviceName(), ReportManager::reportManagerPath, |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 64 | ReportManager::reportManagerIfaceName, "AddReportFutureVersion", |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 65 | std::forward<Args>(args)...); |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 66 | return DbusEnvironment::waitForFuture(addReportPromise.get_future()); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 67 | } |
| 68 | |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 69 | auto addReport(const ReportParams& params) |
| 70 | { |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 71 | return addReport(params.reportId(), params.reportName(), |
| 72 | utils::enumToString(params.reportingType()), |
| 73 | utils::enumToString(params.reportUpdates()), |
| 74 | params.appendLimit(), |
| 75 | utils::transform(params.reportActions(), |
| 76 | [](const auto v) { |
| 77 | return utils::enumToString(v); |
| 78 | }), |
| 79 | params.interval().count(), |
| 80 | toReadingParameters(params.metricParameters())); |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 81 | } |
| 82 | |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 83 | template <class T> |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 84 | static T getProperty(const std::string& property) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 85 | { |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 86 | return DbusEnvironment::getProperty<T>( |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 87 | ReportManager::reportManagerPath, |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 88 | ReportManager::reportManagerIfaceName, property); |
Karol Niczyj | 32859b6 | 2021-05-19 10:20:46 +0200 | [diff] [blame] | 89 | } |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | TEST_F(TestReportManager, minInterval) |
| 93 | { |
| 94 | EXPECT_THAT(getProperty<uint64_t>("MinInterval"), |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 95 | Eq(ReportManager::minInterval.count())); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | TEST_F(TestReportManager, maxReports) |
| 99 | { |
Wludzik, Jozef | 503c158 | 2020-12-11 14:48:01 +0100 | [diff] [blame] | 100 | EXPECT_THAT(getProperty<size_t>("MaxReports"), |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 101 | Eq(ReportManager::maxReports)); |
| 102 | } |
| 103 | |
| 104 | TEST_F(TestReportManager, addReport) |
| 105 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 106 | EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _)); |
| 107 | reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock)) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 108 | .WillOnce(Return(ByMove(std::move(reportMockPtr)))); |
| 109 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 110 | auto [ec, path] = addReport(reportParams); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 111 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::success)); |
| 112 | EXPECT_THAT(path, Eq(reportMock.getPath())); |
| 113 | } |
| 114 | |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 115 | TEST_F(TestReportManager, nameIsUsedToGenerateIdWhenIdIsEmptyInAddReport) |
Karol Niczyj | 32859b6 | 2021-05-19 10:20:46 +0200 | [diff] [blame] | 116 | { |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 117 | reportParams.reportId("ReportName"); |
| 118 | reportParams.reportName("ReportName"); |
| 119 | |
| 120 | reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock)); |
| 121 | |
| 122 | auto [ec, path] = addReport(reportParams.reportId("")); |
| 123 | |
| 124 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::success)); |
| 125 | EXPECT_THAT(path, Eq("/ReportName")); |
| 126 | } |
| 127 | |
| 128 | TEST_F(TestReportManager, nameIsUsedToGenerateIdWhenIdIsNamespace) |
| 129 | { |
| 130 | reportParams.reportId("Prefix/ReportName"); |
| 131 | reportParams.reportName("ReportName"); |
| 132 | |
| 133 | reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock)); |
| 134 | |
| 135 | auto [ec, path] = addReport(reportParams.reportId("Prefix/")); |
| 136 | |
| 137 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::success)); |
| 138 | EXPECT_THAT(path, Eq("/Prefix/ReportName")); |
| 139 | } |
| 140 | |
| 141 | TEST_F(TestReportManager, addReportWithMaxLengthId) |
| 142 | { |
| 143 | std::string reportId(ReportManager::maxReportIdLength, 'z'); |
| 144 | reportParams.reportId(reportId); |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 145 | reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock)); |
Karol Niczyj | 32859b6 | 2021-05-19 10:20:46 +0200 | [diff] [blame] | 146 | |
| 147 | auto [ec, path] = addReport(reportParams); |
| 148 | |
| 149 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::success)); |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 150 | EXPECT_THAT(path, Eq("/"s + reportId)); |
Karol Niczyj | 32859b6 | 2021-05-19 10:20:46 +0200 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | TEST_F(TestReportManager, DISABLED_failToAddReportWithTooLongName) |
| 154 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 155 | reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock)) |
Karol Niczyj | 32859b6 | 2021-05-19 10:20:46 +0200 | [diff] [blame] | 156 | .Times(0); |
| 157 | |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 158 | reportParams.reportId( |
| 159 | std::string(ReportManager::maxReportIdLength + 1, 'z')); |
Karol Niczyj | 32859b6 | 2021-05-19 10:20:46 +0200 | [diff] [blame] | 160 | |
| 161 | auto [ec, path] = addReport(reportParams); |
| 162 | |
| 163 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument)); |
| 164 | EXPECT_THAT(path, Eq(std::string())); |
| 165 | } |
| 166 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 167 | TEST_F(TestReportManager, DISABLED_failToAddReportTwice) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 168 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 169 | reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock)) |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 170 | .WillOnce(Return(ByMove(std::move(reportMockPtr)))); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 171 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 172 | addReport(reportParams); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 173 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 174 | auto [ec, path] = addReport(reportParams); |
Karol Niczyj | 32859b6 | 2021-05-19 10:20:46 +0200 | [diff] [blame] | 175 | |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 176 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::file_exists)); |
| 177 | EXPECT_THAT(path, Eq(std::string())); |
| 178 | } |
| 179 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 180 | TEST_F(TestReportManager, DISABLED_failToAddReportWithInvalidInterval) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 181 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 182 | reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock)) |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 183 | .Times(0); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 184 | |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 185 | reportParams.reportingType(ReportingType::periodic); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 186 | reportParams.interval(reportParams.interval() - 1ms); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 187 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 188 | auto [ec, path] = addReport(reportParams); |
Karol Niczyj | 32859b6 | 2021-05-19 10:20:46 +0200 | [diff] [blame] | 189 | |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 190 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument)); |
| 191 | EXPECT_THAT(path, Eq(std::string())); |
| 192 | } |
| 193 | |
Wludzik, Jozef | bc766b4 | 2020-12-08 16:06:22 +0100 | [diff] [blame] | 194 | TEST_F(TestReportManager, DISABLED_failToAddReportWithInvalidReportingType) |
| 195 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 196 | reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock)) |
Wludzik, Jozef | bc766b4 | 2020-12-08 16:06:22 +0100 | [diff] [blame] | 197 | .Times(0); |
| 198 | |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 199 | auto [ec, path] = addReport( |
| 200 | reportParams.reportName(), "InvalidReportingType", |
| 201 | utils::transform(reportParams.reportActions(), |
| 202 | [](const auto v) { return utils::enumToString(v); }), |
| 203 | reportParams.interval().count(), |
| 204 | toReadingParameters(reportParams.metricParameters())); |
Karol Niczyj | 32859b6 | 2021-05-19 10:20:46 +0200 | [diff] [blame] | 205 | |
Wludzik, Jozef | bc766b4 | 2020-12-08 16:06:22 +0100 | [diff] [blame] | 206 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument)); |
| 207 | EXPECT_THAT(path, Eq(std::string())); |
| 208 | } |
| 209 | |
| 210 | TEST_F(TestReportManager, DISABLED_failToAddReportWithMoreSensorsThanExpected) |
| 211 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 212 | reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock)) |
Wludzik, Jozef | bc766b4 | 2020-12-08 16:06:22 +0100 | [diff] [blame] | 213 | .Times(0); |
| 214 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 215 | auto metricParams = reportParams.metricParameters(); |
Wludzik, Jozef | bc766b4 | 2020-12-08 16:06:22 +0100 | [diff] [blame] | 216 | for (size_t i = 0; i < ReportManager::maxReadingParams + 1; i++) |
| 217 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 218 | metricParams.push_back(metricParams.front()); |
Wludzik, Jozef | bc766b4 | 2020-12-08 16:06:22 +0100 | [diff] [blame] | 219 | } |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 220 | reportParams.metricParameters(std::move(metricParams)); |
Wludzik, Jozef | bc766b4 | 2020-12-08 16:06:22 +0100 | [diff] [blame] | 221 | |
| 222 | auto [ec, path] = addReport(reportParams); |
Karol Niczyj | 32859b6 | 2021-05-19 10:20:46 +0200 | [diff] [blame] | 223 | |
Wludzik, Jozef | bc766b4 | 2020-12-08 16:06:22 +0100 | [diff] [blame] | 224 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::argument_list_too_long)); |
| 225 | EXPECT_THAT(path, Eq(std::string())); |
| 226 | } |
| 227 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 228 | TEST_F(TestReportManager, DISABLED_failToAddReportWhenMaxReportIsReached) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 229 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 230 | reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock)) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 231 | .Times(ReportManager::maxReports); |
| 232 | |
| 233 | for (size_t i = 0; i < ReportManager::maxReports; i++) |
| 234 | { |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 235 | reportParams.reportId(reportParams.reportName() + std::to_string(i)); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 236 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 237 | auto [ec, path] = addReport(reportParams); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 238 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::success)); |
| 239 | } |
| 240 | |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 241 | reportParams.reportId(reportParams.reportName() + |
| 242 | std::to_string(ReportManager::maxReports)); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 243 | auto [ec, path] = addReport(reportParams); |
Karol Niczyj | 32859b6 | 2021-05-19 10:20:46 +0200 | [diff] [blame] | 244 | |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 245 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::too_many_files_open)); |
| 246 | EXPECT_THAT(path, Eq(std::string())); |
| 247 | } |
| 248 | |
| 249 | TEST_F(TestReportManager, removeReport) |
| 250 | { |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 251 | { |
| 252 | InSequence seq; |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 253 | EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _)); |
| 254 | reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock)) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 255 | .WillOnce(Return(ByMove(std::move(reportMockPtr)))); |
| 256 | EXPECT_CALL(reportMock, Die()); |
| 257 | EXPECT_CALL(checkPoint, Call("end")); |
| 258 | } |
| 259 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 260 | addReport(reportParams); |
| 261 | sut->removeReport(&reportMock); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 262 | checkPoint.Call("end"); |
| 263 | } |
| 264 | |
| 265 | TEST_F(TestReportManager, removingReportThatIsNotInContainerHasNoEffect) |
| 266 | { |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 267 | { |
| 268 | InSequence seq; |
| 269 | EXPECT_CALL(checkPoint, Call("end")); |
| 270 | EXPECT_CALL(reportMock, Die()); |
| 271 | } |
| 272 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 273 | sut->removeReport(&reportMock); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 274 | checkPoint.Call("end"); |
| 275 | } |
| 276 | |
| 277 | TEST_F(TestReportManager, removingSameReportTwiceHasNoSideEffect) |
| 278 | { |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 279 | { |
| 280 | InSequence seq; |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 281 | EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _)); |
| 282 | reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock)) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 283 | .WillOnce(Return(ByMove(std::move(reportMockPtr)))); |
| 284 | EXPECT_CALL(reportMock, Die()); |
| 285 | EXPECT_CALL(checkPoint, Call("end")); |
| 286 | } |
| 287 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 288 | addReport(reportParams); |
| 289 | sut->removeReport(&reportMock); |
| 290 | sut->removeReport(&reportMock); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 291 | checkPoint.Call("end"); |
| 292 | } |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 293 | |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 294 | TEST_F(TestReportManager, updateReportCallsUpdateReadingsForExistReport) |
| 295 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 296 | reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock)) |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 297 | .WillOnce(Return(ByMove(std::move(reportMockPtr)))); |
| 298 | EXPECT_CALL(reportMock, updateReadings()); |
| 299 | |
| 300 | addReport(reportParams); |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 301 | sut->updateReport(reportParams.reportId()); |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | TEST_F(TestReportManager, updateReportDoNothingIfReportDoesNotExist) |
| 305 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 306 | reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock)) |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 307 | .WillOnce(Return(ByMove(std::move(reportMockPtr)))); |
| 308 | EXPECT_CALL(reportMock, updateReadings()).Times(0); |
| 309 | |
| 310 | addReport(reportParams); |
| 311 | sut->updateReport("NotAReport"); |
| 312 | } |
| 313 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 314 | class TestReportManagerWithAggregationOperationType : |
| 315 | public TestReportManager, |
| 316 | public WithParamInterface<OperationType> |
| 317 | { |
| 318 | public: |
| 319 | OperationType operationType = GetParam(); |
| 320 | }; |
| 321 | |
| 322 | INSTANTIATE_TEST_SUITE_P(_, TestReportManagerWithAggregationOperationType, |
| 323 | Values(OperationType::single, OperationType::max, |
| 324 | OperationType::min, OperationType::avg, |
| 325 | OperationType::sum)); |
| 326 | |
| 327 | TEST_P(TestReportManagerWithAggregationOperationType, |
| 328 | addReportWithDifferentOperationTypes) |
| 329 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 330 | reportParams.metricParameters( |
| 331 | std::vector<LabeledMetricParameters>{{LabeledMetricParameters{ |
| 332 | {LabeledSensorParameters{"Service", |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 333 | "/xyz/openbmc_project/sensors/power/p1", |
| 334 | "Metadata1"}}, |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 335 | operationType, |
| 336 | "MetricId1", |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 337 | CollectionTimeScope::point, |
| 338 | CollectionDuration(Milliseconds(0u))}}}); |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 339 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 340 | reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock)) |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 341 | .WillOnce(Return(ByMove(std::move(reportMockPtr)))); |
| 342 | |
| 343 | auto [ec, path] = addReport(reportParams); |
Karol Niczyj | 32859b6 | 2021-05-19 10:20:46 +0200 | [diff] [blame] | 344 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 345 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::success)); |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 346 | EXPECT_THAT(path, Eq("/"s + reportParams.reportId())); |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 349 | class TestReportManagerStorage : public TestReportManager |
| 350 | { |
| 351 | public: |
| 352 | using FilePath = interfaces::JsonStorage::FilePath; |
| 353 | using DirectoryPath = interfaces::JsonStorage::DirectoryPath; |
| 354 | |
| 355 | void SetUp() override |
| 356 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 357 | EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _)).Times(0); |
| 358 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 359 | ON_CALL(storageMock, list()) |
| 360 | .WillByDefault(Return(std::vector<FilePath>{FilePath("report1")})); |
| 361 | ON_CALL(storageMock, load(FilePath("report1"))) |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 362 | .WillByDefault(InvokeWithoutArgs([this] { return data; })); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | void makeReportManager() |
| 366 | { |
| 367 | sut = std::make_unique<ReportManager>(std::move(reportFactoryMockPtr), |
| 368 | std::move(storageMockPtr), |
| 369 | DbusEnvironment::getObjServer()); |
| 370 | } |
| 371 | |
| 372 | nlohmann::json data = nlohmann::json{ |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 373 | {"Enabled", reportParams.enabled()}, |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 374 | {"Version", Report::reportVersion}, |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 375 | {"Id", reportParams.reportId()}, |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 376 | {"Name", reportParams.reportName()}, |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 377 | {"ReportingType", utils::toUnderlying(reportParams.reportingType())}, |
| 378 | {"ReportActions", reportParams.reportActions()}, |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 379 | {"Interval", reportParams.interval().count()}, |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 380 | {"ReportUpdates", utils::toUnderlying(reportParams.reportUpdates())}, |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 381 | {"AppendLimit", reportParams.appendLimit()}, |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 382 | {"ReadingParameters", reportParams.metricParameters()}}; |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 383 | }; |
| 384 | |
| 385 | TEST_F(TestReportManagerStorage, reportManagerCtorAddReportFromStorage) |
| 386 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 387 | reportFactoryMock.expectMake(reportParams, _, Ref(storageMock)); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 388 | |
| 389 | makeReportManager(); |
| 390 | } |
| 391 | |
| 392 | TEST_F(TestReportManagerStorage, |
| 393 | reportManagerCtorRemoveFileIfVersionDoesNotMatch) |
| 394 | { |
| 395 | data["Version"] = Report::reportVersion - 1; |
| 396 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 397 | EXPECT_CALL(storageMock, remove(FilePath("report1"))); |
| 398 | |
| 399 | makeReportManager(); |
| 400 | } |
| 401 | |
| 402 | TEST_F(TestReportManagerStorage, |
| 403 | reportManagerCtorRemoveFileIfIntervalHasWrongType) |
| 404 | { |
| 405 | data["Interval"] = "1000"; |
| 406 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 407 | EXPECT_CALL(storageMock, remove(FilePath("report1"))); |
| 408 | |
| 409 | makeReportManager(); |
| 410 | } |