summaryrefslogtreecommitdiff
path: root/src/test/DBusPolymorphicTest.cpp
blob: 65930cbe34c3b1ca6906a94d3cdcd89574de62cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/* Copyright (C) 2013 BMW Group
 * Author: Manfred Bathelt (manfred.bathelt@bmw.de)
 * Author: Juergen Gehring (juergen.gehring@bmw.de)
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <gtest/gtest.h>
#include <commonapi/tests/DerivedTypeCollection.h>
#include <commonapi/tests/TestInterfaceDBusProxy.h>
#include <commonapi/tests/TestInterfaceDBusStubAdapter.h>
#include <commonapi/tests/TestInterfaceStubDefault.h>

#ifndef COMMONAPI_INTERNAL_COMPILATION
#define COMMONAPI_INTERNAL_COMPILATION
#endif
#include <CommonAPI/DBus/DBusRuntime.h>

#ifndef COMMONAPI_INTERNAL_COMPILATION
#define COMMONAPI_INTERNAL_COMPILATION
#endif

static const std::string commonApiAddress =
                "local:CommonAPI.DBus.tests.DBusProxyTestInterface:CommonAPI.DBus.tests.DBusProxyTestService";
static const std::string interfaceName = "CommonAPI.DBus.tests.DBusProxyTestInterface";
static const std::string busName = "CommonAPI.DBus.tests.DBusProxyTestService";
static const std::string objectPath = "/CommonAPI/DBus/tests/DBusProxyTestService";

class PolymorphicTestStub: public commonapi::tests::TestInterfaceStubDefault {
public:
    void TestArrayOfPolymorphicStructMethod(
                    const std::shared_ptr<CommonAPI::ClientId> clientId,
                    std::vector<std::shared_ptr<commonapi::tests::DerivedTypeCollection::TestPolymorphicStruct>> inArray) {
        numberOfContainedElements_ = inArray.size();

        if (numberOfContainedElements_ > 0) {
            std::shared_ptr<commonapi::tests::DerivedTypeCollection::TestExtendedPolymorphicStruct> extended =
                            std::dynamic_pointer_cast<
                                            commonapi::tests::DerivedTypeCollection::TestExtendedPolymorphicStruct>(
                                            inArray[0]);
            firstElementIsExtended_ = (extended != NULL);
        }

        if (numberOfContainedElements_ > 1) {
            std::shared_ptr<commonapi::tests::DerivedTypeCollection::TestExtendedPolymorphicStruct> extended =
                            std::dynamic_pointer_cast<
                                            commonapi::tests::DerivedTypeCollection::TestExtendedPolymorphicStruct>(
                                            inArray[1]);
            secondElementIsExtended_ = (extended != NULL);
        }
    }

    void TestMapOfPolymorphicStructMethod(
                    const std::shared_ptr<CommonAPI::ClientId> clientId,
                    std::unordered_map<uint8_t, std::shared_ptr<commonapi::tests::DerivedTypeCollection::TestPolymorphicStruct>> inMap) {
        numberOfContainedElements_ = inMap.size();

        auto iteratorFirst = inMap.find(5);
        auto iteratorSecond = inMap.find(10);

        if (iteratorFirst != inMap.end()) {
            std::shared_ptr<commonapi::tests::DerivedTypeCollection::TestExtendedPolymorphicStruct> extended =
                            std::dynamic_pointer_cast<
                                            commonapi::tests::DerivedTypeCollection::TestExtendedPolymorphicStruct>(
                                            iteratorFirst->second);
            firstElementIsExtended_ = (extended != NULL);
        }

        if (iteratorSecond != inMap.end()) {
            std::shared_ptr<commonapi::tests::DerivedTypeCollection::TestExtendedPolymorphicStruct> extended =
                            std::dynamic_pointer_cast<
                                            commonapi::tests::DerivedTypeCollection::TestExtendedPolymorphicStruct>(
                                            iteratorSecond->second);
            secondElementIsExtended_ = (extended != NULL);
        }
    }

    void TestMapWithPolymorphicStructKeyMethod(const std::shared_ptr<CommonAPI::ClientId> clientId,
                                               commonapi::tests::DerivedTypeCollection::MapPolymorphicToInt inMap) {
        numberOfContainedElements_ = inMap.size();

        auto mapIterator = inMap.begin();

        if (mapIterator != inMap.end()) {
            std::shared_ptr<commonapi::tests::DerivedTypeCollection::TestExtendedPolymorphicStruct> extended =
                            std::dynamic_pointer_cast<
                                            commonapi::tests::DerivedTypeCollection::TestExtendedPolymorphicStruct>(
                                            mapIterator->first);
            firstElementIsExtended_ = (extended != NULL);
            mapIterator++;
        }

        if (mapIterator != inMap.end()) {
            std::shared_ptr<commonapi::tests::DerivedTypeCollection::TestExtendedPolymorphicStruct> extended =
                            std::dynamic_pointer_cast<
                                            commonapi::tests::DerivedTypeCollection::TestExtendedPolymorphicStruct>(
                                            mapIterator->first);
            firstElementIsExtended_ = (extended != NULL);
        }

    }

    void TestStructWithPolymorphicMemberMethod(const std::shared_ptr<CommonAPI::ClientId> clientId,
                                               commonapi::tests::DerivedTypeCollection::StructWithPolymorphicMember inStruct) {
        if (inStruct.polymorphicMember != NULL) {
            numberOfContainedElements_ = 1;

            std::shared_ptr<commonapi::tests::DerivedTypeCollection::TestExtendedPolymorphicStruct> extended =
                            std::dynamic_pointer_cast<
                                            commonapi::tests::DerivedTypeCollection::TestExtendedPolymorphicStruct>(
                                            inStruct.polymorphicMember);
            firstElementIsExtended_ = (extended != NULL);
        }
    }

    int numberOfContainedElements_;
    bool firstElementIsExtended_;
    bool secondElementIsExtended_;
};

class PolymorphicTest: public ::testing::Test {
protected:
    void SetUp() {
        auto runtime = std::dynamic_pointer_cast<CommonAPI::DBus::DBusRuntime>(CommonAPI::Runtime::load());

        serviceFactory = std::dynamic_pointer_cast<CommonAPI::DBus::DBusFactory>(runtime->createFactory());

        proxyDBusConnection_ = CommonAPI::DBus::DBusConnection::getSessionBus();
        ASSERT_TRUE(proxyDBusConnection_->connect());

        proxy_ = std::make_shared<commonapi::tests::TestInterfaceDBusProxy>(
                        serviceFactory,
                        commonApiAddress,
                        interfaceName,
                        busName,
                        objectPath,
                        proxyDBusConnection_);
        proxy_->init();

        registerTestStub();

        for (unsigned int i = 0; !proxy_->isAvailable() && i < 100; ++i) {
            usleep(10000);
        }
        ASSERT_TRUE(proxy_->isAvailable());

        baseInstance1_ = std::make_shared<commonapi::tests::DerivedTypeCollection::TestPolymorphicStruct>();
        baseInstance1_->testString = "abc";

        extendedInstance1_ = std::make_shared<commonapi::tests::DerivedTypeCollection::TestExtendedPolymorphicStruct>();
        extendedInstance1_->additionalValue = 7;
    }

    std::shared_ptr<CommonAPI::DBus::DBusFactory> serviceFactory;

    virtual void TearDown() {
        deregisterTestStub();
        usleep(30000);
    }

    void registerTestStub() {
        stubDBusConnection_ = CommonAPI::DBus::DBusConnection::getSessionBus();
        ASSERT_TRUE(stubDBusConnection_->connect());

        testStub = std::make_shared<PolymorphicTestStub>();
        stubAdapter_ = std::make_shared<commonapi::tests::TestInterfaceDBusStubAdapter>(
                        serviceFactory,
                        commonApiAddress,
                        interfaceName,
                        busName,
                        objectPath,
                        stubDBusConnection_,
                        testStub);
        stubAdapter_->init();

        const bool isStubAdapterRegistered = CommonAPI::DBus::DBusServicePublisher::getInstance()->registerService(
                        stubAdapter_);
        ASSERT_TRUE(isStubAdapterRegistered);
    }

    void deregisterTestStub() {
        const bool isStubAdapterUnregistered = CommonAPI::DBus::DBusServicePublisher::getInstance()->unregisterService(
                        stubAdapter_->getAddress());
        ASSERT_TRUE(isStubAdapterUnregistered);
        stubAdapter_.reset();

        if (stubDBusConnection_->isConnected()) {
            stubDBusConnection_->disconnect();
        }
        stubDBusConnection_.reset();
    }

    std::shared_ptr<CommonAPI::DBus::DBusConnection> proxyDBusConnection_;
    std::shared_ptr<commonapi::tests::TestInterfaceDBusProxy> proxy_;

    std::shared_ptr<CommonAPI::DBus::DBusConnection> stubDBusConnection_;
    std::shared_ptr<commonapi::tests::TestInterfaceDBusStubAdapter> stubAdapter_;

    std::shared_ptr<commonapi::tests::DerivedTypeCollection::TestPolymorphicStruct> baseInstance1_;
    std::shared_ptr<commonapi::tests::DerivedTypeCollection::TestExtendedPolymorphicStruct> extendedInstance1_;

    std::shared_ptr<PolymorphicTestStub> testStub;
};

TEST_F(PolymorphicTest, SendVectorOfBaseType) {
    CommonAPI::CallStatus stat;
    std::vector<std::shared_ptr<commonapi::tests::DerivedTypeCollection::TestPolymorphicStruct> > inputArray;
    inputArray.push_back(baseInstance1_);

    proxy_->TestArrayOfPolymorphicStructMethod(inputArray, stat);

    ASSERT_EQ(stat, CommonAPI::CallStatus::SUCCESS);
    ASSERT_EQ(testStub->numberOfContainedElements_, 1);
    ASSERT_FALSE(testStub->firstElementIsExtended_);
}

TEST_F(PolymorphicTest, SendVectorOfExtendedType) {
    CommonAPI::CallStatus stat;
    std::vector<std::shared_ptr<commonapi::tests::DerivedTypeCollection::TestPolymorphicStruct> > inputArray;
    inputArray.push_back(extendedInstance1_);

    proxy_->TestArrayOfPolymorphicStructMethod(inputArray, stat);

    ASSERT_EQ(stat, CommonAPI::CallStatus::SUCCESS);
    ASSERT_EQ(testStub->numberOfContainedElements_, 1);
    ASSERT_TRUE(testStub->firstElementIsExtended_);
}

TEST_F(PolymorphicTest, SendVectorOfBaseAndExtendedType) {
    CommonAPI::CallStatus stat;
    std::vector<std::shared_ptr<commonapi::tests::DerivedTypeCollection::TestPolymorphicStruct> > inputArray;
    inputArray.push_back(baseInstance1_);
    inputArray.push_back(extendedInstance1_);

    proxy_->TestArrayOfPolymorphicStructMethod(inputArray, stat);

    ASSERT_EQ(stat, CommonAPI::CallStatus::SUCCESS);
    ASSERT_EQ(testStub->numberOfContainedElements_, 2);
    ASSERT_FALSE(testStub->firstElementIsExtended_);
    ASSERT_TRUE(testStub->secondElementIsExtended_);
}

TEST_F(PolymorphicTest, SendMapOfBaseAndExtendedType) {
    CommonAPI::CallStatus stat;
    std::unordered_map<uint8_t, std::shared_ptr<commonapi::tests::DerivedTypeCollection::TestPolymorphicStruct> > inputMap;
    inputMap.insert( {5, baseInstance1_});
    inputMap.insert( {10, extendedInstance1_});

    proxy_->TestMapOfPolymorphicStructMethod(inputMap, stat);

    ASSERT_EQ(stat, CommonAPI::CallStatus::SUCCESS);
    ASSERT_EQ(testStub->numberOfContainedElements_, 2);
    ASSERT_FALSE(testStub->firstElementIsExtended_);
    ASSERT_TRUE(testStub->secondElementIsExtended_);
}

// disabled as it leads to a crash in libdbus which does not allow container types as map keys in its signature validation
TEST_F(PolymorphicTest, DISABLED_SendMapOfBaseAndExtendedTypeAsKey) {
    CommonAPI::CallStatus stat;
    std::unordered_map<std::shared_ptr<commonapi::tests::DerivedTypeCollection::TestPolymorphicStruct>, uint8_t> inputMap;
    inputMap.insert( {baseInstance1_, 5});
    inputMap.insert( {extendedInstance1_, 10});

    proxy_->TestMapWithPolymorphicStructKeyMethod(inputMap, stat);

    ASSERT_EQ(stat, CommonAPI::CallStatus::SUCCESS);
    ASSERT_EQ(testStub->numberOfContainedElements_, 2);
    ASSERT_FALSE(testStub->firstElementIsExtended_);
    ASSERT_TRUE(testStub->secondElementIsExtended_);
}

TEST_F(PolymorphicTest, SendStructWithPolymorphicMember) {
    CommonAPI::CallStatus stat;
    commonapi::tests::DerivedTypeCollection::StructWithPolymorphicMember inputStruct;
    inputStruct.polymorphicMember = extendedInstance1_;

    proxy_->TestStructWithPolymorphicMemberMethod(inputStruct, stat);

    ASSERT_EQ(stat, CommonAPI::CallStatus::SUCCESS);
    ASSERT_EQ(testStub->numberOfContainedElements_, 1);
    ASSERT_TRUE(testStub->firstElementIsExtended_);
}

TEST_F(PolymorphicTest, SendStructWithMapWithEnumKeyMember) {
    CommonAPI::CallStatus stat;
    commonapi::tests::DerivedTypeCollection::StructWithEnumKeyMap inputStruct;
    inputStruct.testMap.insert( { commonapi::tests::DerivedTypeCollection::TestEnum::E_OK, "test" } );

    proxy_->TestStructWithEnumKeyMapMember(inputStruct, stat);

    ASSERT_EQ(stat, CommonAPI::CallStatus::SUCCESS);
}

int main(int argc, char** argv) {
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}