RTC Toolkit 5.1.0
Loading...
Searching...
No Matches
helpers.hpp
Go to the documentation of this file.
1
13#pragma once
14
15#include <gmock/gmock.h>
16#include <gtest/gtest.h>
17
24
25#include <iostream>
26#include <memory>
27#include <mutex>
28
29using ::testing::_; // NOLINT(google-global-names-in-headers)
30using ::testing::InSequence; // NOLINT(google-global-names-in-headers)
31using ::testing::NiceMock; // NOLINT(google-global-names-in-headers)
32using ::testing::Return; // NOLINT(google-global-names-in-headers)
33using ::testing::Sequence; // NOLINT(google-global-names-in-headers)
34
35using namespace rtctk::componentFramework; // NOLINT(google-global-names-in-headers)
36using namespace rtctk::componentFramework::test; // NOLINT(google-global-names-in-headers)
37
38extern void* rec_ptr;
39
43
45public:
46 friend MockEventService;
47
48 void Publish(const JsonPayload& sample) override {
49 }
50
51private:
52 explicit MockEventPublisher(const std::string& topic) {
53 }
54};
55
57public:
58 friend MockEventService;
59
60 void Subscribe(std::function<void(const JsonPayload&)> cb) override {
61 auto lock = std::scoped_lock(callback_lock);
62 callback = cb;
64 }
65 MOCK_METHOD(void, SubscribeMock, (std::function<void(const JsonPayload&)>), ());
66 MOCK_METHOD(void, Unsubscribe, (), (override));
67
68 std::function<void(const JsonPayload&)> GetCallback() {
69 auto lock = std::scoped_lock(callback_lock);
70 auto copy = callback;
71 return copy;
72 };
73
74private:
75 std::function<void(const JsonPayload&)> callback;
76 std::mutex callback_lock;
77
78 explicit MockEventSubscriber(const std::string& topic) {
79 }
80};
81
82class MockEventService : public EventServiceIf {
83public:
86
87 std::unique_ptr<EventPublisherIf> MakePublisher(const std::string& topic) override {
88 pub[topic] = new MockEventPublisher(topic);
89 return std::unique_ptr<MockEventPublisher>(pub.at(topic));
90 }
91
92 std::unique_ptr<EventSubscriberIf> MakeSubscriber(const std::string& topic) override {
93 sub[topic] = new MockEventSubscriber(topic);
94 return std::unique_ptr<MockEventSubscriber>(sub.at(topic));
95 }
96
97 std::map<std::string, MockEventPublisher*> pub;
98 std::map<std::string, MockEventSubscriber*> sub;
99};
100
101struct FakeServices {
103 services.Add<RuntimeRepoIf>(std::make_shared<test::FakeRuntimeRepoIf>());
104 services.Add<OldbIf>(std::make_shared<test::FakeOldbIf>());
105 services.Add<ComponentMetricsIf>(std::make_shared<FakeComponentMetrics>());
106 // cppcheck-suppress useInitializationList
107 es = std::make_shared<MockEventService>();
109 }
110
112 return services;
113 }
114
116 return *es;
117 }
118
120 std::shared_ptr<MockEventService> es;
121};
122
124 CII_THROW(RtctkException, "Some Exception");
125}
Definition helpers.hpp:76
void Publish(const JsonPayload &sample) override
Publishes a JSON object.
Definition helpers.hpp:48
friend MockEventService
Definition helpers.hpp:78
Definition helpers.hpp:114
std::unique_ptr< EventPublisherIf > MakePublisher(const std::string &topic) override
Creates a new publisher for a specified topic.
Definition helpers.hpp:87
std::map< std::string, MockEventPublisher * > pub
Definition helpers.hpp:129
MockEventService()
Definition helpers.hpp:84
std::map< std::string, MockEventSubscriber * > sub
Definition helpers.hpp:130
std::unique_ptr< EventSubscriberIf > MakeSubscriber(const std::string &topic) override
Creates a new subscriber for a specified topic.
Definition helpers.hpp:92
Definition helpers.hpp:88
void Subscribe(std::function< void(const JsonPayload &)> cb) override
Subscribes to a specified topic.
Definition helpers.hpp:60
MOCK_METHOD(void, SubscribeMock,(std::function< void(const JsonPayload &)>),())
MOCK_METHOD(void, Unsubscribe,(),(override))
std::function< void(const JsonPayload &)> GetCallback()
Definition helpers.hpp:68
friend MockEventService
Definition helpers.hpp:90
Component metrics interface.
Definition componentMetricsIf.hpp:85
Interface class for publishing JSON events.
Definition eventServiceIf.hpp:52
Interface class for providing pub/sub facilities for JSON events.
Definition eventServiceIf.hpp:29
Interface class for subscribing to JSON events.
Definition eventServiceIf.hpp:68
virtual void Unsubscribe()=0
Unsubscribes from a specified topic.
Base interface for all OLDB adapters.
Definition oldbIf.hpp:25
The RtctkException class is the base class for all Rtctk exceptions.
Definition exceptions.hpp:211
Base interface for all Runtime Configuration Repository adapters.
Definition runtimeRepoIf.hpp:27
Container class that holds services of any type.
Definition serviceContainer.hpp:39
void Add(Service &&service, const std::string &name="")
Insert new service into the container.
Definition serviceContainer.hpp:62
Header file for ComponentMetricsIf.
Low-level interface of the event service.
Declaration of helper mock class for ComponentMetricsIf.
Header file providing a FakeOldbIf.
Header file providing a FakeRuntimeRepoIf.
Definition fakeClock.cpp:15
Definition commandReplier.cpp:22
nlohmann::json JsonPayload
Type requirements:
Definition jsonPayload.hpp:25
elt::mal::future< std::string > InjectReqRepEvent(StateMachineEngine &engine)
Definition malEventInjector.hpp:23
void * rec_ptr
ACTION(Throw)
Definition helpers.hpp:123
A container that can hold any type of service.
Definition helpers.hpp:133
std::shared_ptr< MockEventService > es
Definition helpers.hpp:152
ServiceContainer & operator*()
Definition helpers.hpp:111
FakeServices()
Definition helpers.hpp:102
MockEventService & Es()
Definition helpers.hpp:115
ServiceContainer services
Definition helpers.hpp:151