RTC Toolkit 6.0.0-pre2
Loading...
Searching...
No Matches
helpers.hpp
Go to the documentation of this file.
1
12
13#pragma once
14
15#include <gmock/gmock.h>
16#include <gtest/gtest.h>
17
24
25#include <memory>
26#include <mutex>
27
28using ::testing::_; // NOLINT(google-global-names-in-headers)
29using ::testing::InSequence; // NOLINT(google-global-names-in-headers)
30using ::testing::NiceMock; // NOLINT(google-global-names-in-headers)
31using ::testing::Return; // NOLINT(google-global-names-in-headers)
32using ::testing::Sequence; // NOLINT(google-global-names-in-headers)
33
34using namespace rtctk::componentFramework; // NOLINT(google-global-names-in-headers)
35using namespace rtctk::componentFramework::test; // NOLINT(google-global-names-in-headers)
36
37extern void* rec_ptr;
38
42
44public:
45 friend MockEventService;
46
47 void Publish(const JsonPayload& sample) override {
48 }
49
50private:
51 explicit MockEventPublisher(const std::string& topic) {
52 }
53};
54
56public:
57 friend MockEventService;
58
59 void Subscribe(std::function<void(const JsonPayload&)> cb) override {
60 auto lock = std::scoped_lock(callback_lock);
61 callback = cb;
62 SubscribeMock(cb);
63 }
64 MOCK_METHOD(void, SubscribeMock, (std::function<void(const JsonPayload&)>), ());
65 MOCK_METHOD(void, Unsubscribe, (), (override));
66
67 std::function<void(const JsonPayload&)> GetCallback() {
68 auto lock = std::scoped_lock(callback_lock);
69 auto copy = callback;
70 return copy;
71 };
72
73private:
74 std::function<void(const JsonPayload&)> callback;
75 std::mutex callback_lock;
76
77 explicit MockEventSubscriber(const std::string& topic) {
78 }
79};
80
81class MockEventService : public EventServiceIf {
82public:
83 MockEventService() = default;
84
85 std::unique_ptr<EventPublisherIf> MakePublisher(const std::string& topic) override {
86 pub[topic] = new MockEventPublisher(topic);
87 return std::unique_ptr<MockEventPublisher>(pub.at(topic));
88 }
89
90 std::unique_ptr<EventSubscriberIf> MakeSubscriber(const std::string& topic) override {
91 sub[topic] = new MockEventSubscriber(topic);
92 return std::unique_ptr<MockEventSubscriber>(sub.at(topic));
93 }
94
95 std::map<std::string, MockEventPublisher*> pub;
96 std::map<std::string, MockEventSubscriber*> sub;
97};
98
99struct FakeServices {
101 services.Add<RuntimeRepoIf>(std::make_shared<test::FakeRuntimeRepoIf>());
102 services.Add<OldbIf>(std::make_shared<test::FakeOldbIf>());
103 services.Add<ComponentMetricsIf>(std::make_shared<FakeComponentMetrics>());
104 // cppcheck-suppress useInitializationList
105 es = std::make_shared<MockEventService>();
107 }
108
110 return services;
111 }
112
114 return *es;
115 }
116
118 std::shared_ptr<MockEventService> es;
119};
120
121ACTION(Throw) {
122 CII_THROW(RtctkException, "Some Exception");
123}
Definition helpers.hpp:76
void Publish(const JsonPayload &sample) override
Publishes a JSON object.
Definition helpers.hpp:47
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:85
std::map< std::string, MockEventPublisher * > pub
Definition helpers.hpp:128
MockEventService()=default
std::map< std::string, MockEventSubscriber * > sub
Definition helpers.hpp:129
std::unique_ptr< EventSubscriberIf > MakeSubscriber(const std::string &topic) override
Creates a new subscriber for a specified topic.
Definition helpers.hpp:90
Definition helpers.hpp:88
void Subscribe(std::function< void(const JsonPayload &)> cb) override
Subscribes to a specified topic.
Definition helpers.hpp:59
MOCK_METHOD(void, SubscribeMock,(std::function< void(const JsonPayload &)>),())
MOCK_METHOD(void, Unsubscribe,(),(override))
std::function< void(const JsonPayload &)> GetCallback()
Definition helpers.hpp:67
friend MockEventService
Definition helpers.hpp:90
Component metrics interface.
Definition componentMetricsIf.hpp:164
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:213
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 * rec_ptr
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
ACTION(Throw)
Definition helpers.hpp:121
A container that can hold any type of service.
Definition helpers.hpp:132
std::shared_ptr< MockEventService > es
Definition helpers.hpp:151
ServiceContainer & operator*()
Definition helpers.hpp:109
FakeServices()
Definition helpers.hpp:100
MockEventService & Es()
Definition helpers.hpp:113
ServiceContainer services
Definition helpers.hpp:150