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 
9 #include <gmock/gmock.h>
10 
11 class TriggerFactoryMock : public interfaces::TriggerFactory
12 {
13   public:
14     TriggerFactoryMock()
15     {
16         using namespace testing;
17 
18         ON_CALL(*this, make(A<const std::string&>(), _, _, _, _, _, _, _))
19             .WillByDefault(WithArgs<0>(Invoke([](const std::string& id) {
20                 return std::make_unique<NiceMock<TriggerMock>>(id);
21             })));
22     }
23 
24     MOCK_METHOD(std::unique_ptr<interfaces::Trigger>, make,
25                 (const std::string& id, const std::string& name,
26                  const std::vector<std::string>& triggerActions,
27                  const std::vector<std::string>& reportNames,
28                  interfaces::TriggerManager& triggerManager,
29                  interfaces::JsonStorage& triggerStorage,
30                  const LabeledTriggerThresholdParams& labeledThresholdParams,
31                  const std::vector<LabeledSensorInfo>& labeledSensorsInfo),
32                 (const, override));
33 
34     MOCK_METHOD(std::vector<LabeledSensorInfo>, getLabeledSensorsInfo,
35                 (boost::asio::yield_context&, const SensorsInfo&),
36                 (const, override));
37 
38     auto& expectMake(
39         std::optional<TriggerParams> paramsOpt,
40         const testing::Matcher<interfaces::TriggerManager&>& tm,
41         const testing::Matcher<interfaces::JsonStorage&>& triggerStorage)
42     {
43         using namespace testing;
44 
45         if (paramsOpt)
46         {
47             const TriggerParams& params = *paramsOpt;
48 
49             const auto sensorInfos =
50                 utils::fromLabeledSensorsInfo(params.sensors());
51 
52             ON_CALL(*this, getLabeledSensorsInfo(_, sensorInfos))
53                 .WillByDefault(Return(params.sensors()));
54 
55             return EXPECT_CALL(
56                 *this, make(params.id(), params.name(), params.triggerActions(),
57                             params.reportNames(), tm, triggerStorage,
58                             params.thresholdParams(), params.sensors()));
59         }
60         else
61         {
62             const std::vector<LabeledSensorInfo> dummy = {
63                 {"service99",
64                  "/xyz/openbmc_project/sensors/temperature/BMC_Temp99",
65                  "metadata99"}};
66 
67             ON_CALL(*this, getLabeledSensorsInfo(_, _))
68                 .WillByDefault(Return(dummy));
69 
70             return EXPECT_CALL(*this,
71                                make(_, _, _, _, tm, triggerStorage, _, dummy));
72         }
73     }
74 };
75