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