1 #pragma once 2 3 #include "interfaces/report_factory.hpp" 4 #include "mocks/report_mock.hpp" 5 #include "params/report_params.hpp" 6 #include "utils/transform.hpp" 7 8 #include <gmock/gmock.h> 9 10 class ReportFactoryMock : public interfaces::ReportFactory 11 { 12 static std::vector<LabeledMetricParameters> 13 convertToLabeled(const ReadingParameters& readingParams) 14 { 15 return utils::transform(readingParams, [](const auto& params) { 16 return LabeledMetricParameters( 17 utils::transform(std::get<0>(params), 18 [](const auto& sensorPath) { 19 return LabeledSensorParameters("Service", 20 sensorPath); 21 }), 22 utils::stringToOperationType(std::get<1>(params)), 23 std::get<2>(params), std::get<3>(params), 24 utils::stringToCollectionTimeScope(std::get<4>(params)), 25 CollectionDuration(Milliseconds(std::get<5>(params)))); 26 }); 27 } 28 29 public: 30 ReportFactoryMock() 31 { 32 using namespace testing; 33 34 ON_CALL(*this, convertMetricParams(_, _)) 35 .WillByDefault( 36 WithArgs<1>(Invoke(&ReportFactoryMock::convertToLabeled))); 37 38 ON_CALL(*this, 39 make(A<const std::string&>(), _, _, _, _, _, _, _, _, _, _)) 40 .WillByDefault(WithArgs<0>(Invoke([](const std::string& name) { 41 return std::make_unique<NiceMock<ReportMock>>(name); 42 }))); 43 } 44 45 MOCK_METHOD(std::vector<LabeledMetricParameters>, convertMetricParams, 46 (boost::asio::yield_context&, const ReadingParameters&), 47 (const, override)); 48 49 MOCK_METHOD(std::unique_ptr<interfaces::Report>, make, 50 (const std::string&, const std::string&, bool, bool, 51 Milliseconds, uint64_t, const std::string&, 52 interfaces::ReportManager&, interfaces::JsonStorage&, 53 std::vector<LabeledMetricParameters>, bool), 54 (const, override)); 55 56 auto& expectMake( 57 std::optional<std::reference_wrapper<const ReportParams>> paramsRef, 58 const testing::Matcher<interfaces::ReportManager&>& rm, 59 const testing::Matcher<interfaces::JsonStorage&>& js) 60 { 61 if (paramsRef) 62 { 63 const ReportParams& params = *paramsRef; 64 return EXPECT_CALL( 65 *this, 66 make(params.reportName(), params.reportingType(), 67 params.emitReadingUpdate(), 68 params.logToMetricReportCollection(), params.interval(), 69 params.appendLimit(), params.reportUpdates(), rm, js, 70 params.metricParameters(), params.enabled())); 71 } 72 else 73 { 74 using testing::_; 75 return EXPECT_CALL(*this, make(_, _, _, _, _, _, _, rm, js, _, _)); 76 } 77 } 78 }; 79