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, make(A<const std::string&>(), _, _, _, _, _, _, _, _))
39             .WillByDefault(WithArgs<0>(Invoke([](const std::string& name) {
40                 return std::make_unique<NiceMock<ReportMock>>(name);
41             })));
42     }
43 
44     MOCK_METHOD(std::vector<LabeledMetricParameters>, convertMetricParams,
45                 (boost::asio::yield_context&, const ReadingParameters&),
46                 (const, override));
47 
48     MOCK_METHOD(std::unique_ptr<interfaces::Report>, make,
49                 (const std::string&, const std::string&, bool, bool,
50                  Milliseconds, interfaces::ReportManager&,
51                  interfaces::JsonStorage&, std::vector<LabeledMetricParameters>,
52                  bool),
53                 (const, override));
54 
55     auto& expectMake(
56         std::optional<std::reference_wrapper<const ReportParams>> paramsRef,
57         const testing::Matcher<interfaces::ReportManager&>& rm,
58         const testing::Matcher<interfaces::JsonStorage&>& js)
59     {
60         if (paramsRef)
61         {
62             const ReportParams& params = *paramsRef;
63             return EXPECT_CALL(
64                 *this,
65                 make(params.reportName(), params.reportingType(),
66                      params.emitReadingUpdate(),
67                      params.logToMetricReportCollection(), params.interval(),
68                      rm, js, params.metricParameters(), params.enabled()));
69         }
70         else
71         {
72             using testing::_;
73             return EXPECT_CALL(*this, make(_, _, _, _, _, rm, js, _, _));
74         }
75     }
76 };
77