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> convertToLabeled(const ReadingParameters & readingParams)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& sensorData) { 19 return LabeledSensorInfo("Service", std::get<0>(sensorData), 20 std::get<1>(sensorData)); 21 }), 22 utils::toOperationType(std::get<1>(params)), 23 utils::toCollectionTimeScope(std::get<2>(params)), 24 CollectionDuration(Milliseconds(std::get<3>(params)))); 25 }); 26 } 27 28 public: ReportFactoryMock()29 ReportFactoryMock() 30 { 31 using namespace testing; 32 33 ON_CALL(*this, convertMetricParams(_)) 34 .WillByDefault( 35 WithArgs<0>(Invoke(&ReportFactoryMock::convertToLabeled))); 36 37 ON_CALL(*this, convertMetricParams(_, _)) 38 .WillByDefault( 39 WithArgs<1>(Invoke(&ReportFactoryMock::convertToLabeled))); 40 41 ON_CALL(*this, 42 make(A<const std::string&>(), _, _, _, _, _, _, _, _, _, _, _)) 43 .WillByDefault(WithArgs<0>(Invoke([](const std::string& id) { 44 return std::make_unique<NiceMock<ReportMock>>(id); 45 }))); 46 } 47 48 MOCK_METHOD(std::vector<LabeledMetricParameters>, convertMetricParams, 49 (boost::asio::yield_context&, const ReadingParameters&), 50 (const, override)); 51 52 MOCK_METHOD(std::vector<LabeledMetricParameters>, convertMetricParams, 53 (const ReadingParameters&), (const, override)); 54 55 MOCK_METHOD(void, updateMetrics, 56 (std::vector<std::shared_ptr<interfaces::Metric>> & metrics, 57 bool enabled, const std::vector<LabeledMetricParameters>&), 58 (const, override)); 59 60 MOCK_METHOD(std::unique_ptr<interfaces::Report>, make, 61 (const std::string&, const std::string&, const ReportingType, 62 const std::vector<ReportAction>&, Milliseconds, uint64_t, 63 const ReportUpdates, interfaces::ReportManager&, 64 interfaces::JsonStorage&, std::vector<LabeledMetricParameters>, 65 bool, Readings), 66 (const, override)); 67 expectMake(std::optional<std::reference_wrapper<const ReportParams>> paramsRef,const testing::Matcher<interfaces::ReportManager &> & rm,const testing::Matcher<interfaces::JsonStorage &> & js)68 auto& expectMake( 69 std::optional<std::reference_wrapper<const ReportParams>> paramsRef, 70 const testing::Matcher<interfaces::ReportManager&>& rm, 71 const testing::Matcher<interfaces::JsonStorage&>& js) 72 { 73 using testing::_; 74 if (paramsRef) 75 { 76 const ReportParams& params = *paramsRef; 77 return EXPECT_CALL( 78 *this, 79 make(params.reportId(), params.reportName(), 80 params.reportingType(), params.reportActions(), 81 params.interval(), params.appendLimit(), 82 params.reportUpdates(), rm, js, params.metricParameters(), 83 params.enabled(), params.readings())); 84 } 85 else 86 { 87 return EXPECT_CALL(*this, 88 make(_, _, _, _, _, _, _, rm, js, _, _, _)); 89 } 90 } 91 }; 92