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