1 #pragma once
2 
3 #include "interfaces/trigger_factory.hpp"
4 #include "mocks/trigger_mock.hpp"
5 #include "params/trigger_params.hpp"
6 #include "trigger.hpp"
7 #include "utils/conversion_trigger.hpp"
8 #include "utils/transform.hpp"
9 
10 #include <gmock/gmock.h>
11 
12 class TriggerFactoryMock : public interfaces::TriggerFactory
13 {
14   public:
15     TriggerFactoryMock()
16     {
17         using namespace testing;
18 
19         ON_CALL(*this, make(A<const std::string&>(), _, _, _, _, _, _, _))
20             .WillByDefault(WithArgs<0>(Invoke([](const std::string& id) {
21                 return std::make_unique<NiceMock<TriggerMock>>(id);
22             })));
23     }
24 
25     MOCK_METHOD(std::unique_ptr<interfaces::Trigger>, make,
26                 (const std::string& id, const std::string& name,
27                  const std::vector<std::string>& triggerActions,
28                  const std::vector<std::string>& reportIds,
29                  interfaces::TriggerManager& triggerManager,
30                  interfaces::JsonStorage& triggerStorage,
31                  const LabeledTriggerThresholdParams& labeledThresholdParams,
32                  const std::vector<LabeledSensorInfo>& labeledSensorsInfo),
33                 (const, override));
34 
35     MOCK_METHOD(std::vector<LabeledSensorInfo>, getLabeledSensorsInfo,
36                 (boost::asio::yield_context&, const SensorsInfo&),
37                 (const, override));
38 
39     MOCK_METHOD(std::vector<LabeledSensorInfo>, getLabeledSensorsInfo,
40                 (const SensorsInfo&), (const, override));
41 
42     MOCK_METHOD(void, updateThresholds,
43                 (std::vector<std::shared_ptr<interfaces::Threshold>> &
44                      currentThresholds,
45                  const std::string& triggerId,
46                  const std::vector<TriggerAction>& triggerActions,
47                  const std::shared_ptr<std::vector<std::string>>& reportIds,
48                  const Sensors& sensors,
49                  const LabeledTriggerThresholdParams& newParams),
50                 (const, override));
51 
52     MOCK_METHOD(void, updateSensors,
53                 (Sensors & currentSensors,
54                  const std::vector<LabeledSensorInfo>& senorParams),
55                 (const, override));
56 
57     auto& expectMake(
58         std::optional<TriggerParams> paramsOpt,
59         const testing::Matcher<interfaces::TriggerManager&>& tm,
60         const testing::Matcher<interfaces::JsonStorage&>& triggerStorage)
61     {
62         using namespace testing;
63 
64         if (paramsOpt)
65         {
66             const TriggerParams& params = *paramsOpt;
67 
68             const auto sensorInfos =
69                 utils::fromLabeledSensorsInfo(params.sensors());
70 
71             ON_CALL(*this, getLabeledSensorsInfo(_, sensorInfos))
72                 .WillByDefault(Return(params.sensors()));
73 
74             return EXPECT_CALL(
75                 *this, make(params.id(), params.name(),
76                             utils::transform(params.triggerActions(),
77                                              [](const auto& action) {
78                                                  return actionToString(action);
79                                              }),
80                             params.reportIds(), tm, triggerStorage,
81                             params.thresholdParams(), params.sensors()));
82         }
83         else
84         {
85             const std::vector<LabeledSensorInfo> dummy = {
86                 {"service99",
87                  "/xyz/openbmc_project/sensors/temperature/BMC_Temp99",
88                  "metadata99"}};
89 
90             ON_CALL(*this, getLabeledSensorsInfo(_, _))
91                 .WillByDefault(Return(dummy));
92 
93             return EXPECT_CALL(*this,
94                                make(_, _, _, _, tm, triggerStorage, _, dummy));
95         }
96     }
97 };
98