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