summaryrefslogtreecommitdiff
path: root/src/test/dbusDynamicLoadingTests/DBusDynamicLoadingMultipleDefinitionTest.cpp
blob: 2e402c3d7b7bf41e86220f18a6a8c8a2e0a645c2 (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
/* 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 <fstream>

#include "DBusDynamicLoadingDefinitions.h"


class Environment: public ::testing::Environment {
public:
    virtual ~Environment() {
    }

    virtual void SetUp() {
        environmentIdentifier_ = COMMONAPI_ENVIRONMENT_BINDING_PATH + "=" ;
        environmentString_ = environmentIdentifier_ + currentWorkingDirectory;
        char* environment = (char*) (environmentString_.c_str());
        putenv(environment);

        configFileName_ = CommonAPI::getCurrentBinaryFileFQN();
        configFileName_ += COMMONAPI_CONFIG_SUFFIX;
        std::ofstream configFile(configFileName_);
        ASSERT_TRUE(configFile.is_open());
        configFile << validForMultiplyDefinedDBusBinding;
        configFile.close();
    }

    virtual void TearDown() {
        std::remove(configFileName_.c_str());

        char* environment = (char*) (environmentIdentifier_.c_str());
        putenv(environment);
    }

    std::string configFileName_;
    std::string environmentIdentifier_;
    std::string environmentString_;
};


class DBusDynamicLoadingMultipleDefinitionTest: public ::testing::Test {
 protected:
    virtual void SetUp() {
    }

    virtual void TearDown() {
    }
};


TEST_F(DBusDynamicLoadingMultipleDefinitionTest, LoadsUnconfiguredDefaultDynamicallyLinkedLibrary) {
    std::shared_ptr<CommonAPI::Runtime> runtime = CommonAPI::Runtime::load();
    EXPECT_TRUE((bool)runtime);
}

TEST_F(DBusDynamicLoadingMultipleDefinitionTest, LoadsSpecificDynamicallyLinkedDBusLibrary) {
    std::shared_ptr<CommonAPI::Runtime> defaultRuntime = CommonAPI::Runtime::load();
    std::shared_ptr<CommonAPI::Runtime> fakeRuntime = CommonAPI::Runtime::load("Fake");
    EXPECT_TRUE((bool)defaultRuntime);
    EXPECT_TRUE((bool)fakeRuntime);
    //The DBus binding is alphabetically before the Fake binding, so the DBusRuntime will be loaded as default
    ASSERT_NE(fakeRuntime, defaultRuntime);
}

TEST_F(DBusDynamicLoadingMultipleDefinitionTest, LoadsAliasedDynamicallyLinkedDBusLibrary) {
    std::shared_ptr<CommonAPI::Runtime> runtime = CommonAPI::Runtime::load("MyFirstAlias");
    EXPECT_TRUE((bool)runtime);
    std::shared_ptr<CommonAPI::Runtime> runtime2 = CommonAPI::Runtime::load("MySecondAlias");
    EXPECT_TRUE((bool)runtime2);
}

TEST_F(DBusDynamicLoadingMultipleDefinitionTest, ReturnsEmptyPointerOnRequestForUnknownMiddleware) {
    std::shared_ptr<CommonAPI::Runtime> runtime = CommonAPI::Runtime::load("NonExisting");
    EXPECT_FALSE((bool)runtime);
}

TEST_F(DBusDynamicLoadingMultipleDefinitionTest, LoadsDBusLibraryAsSingleton) {
    std::shared_ptr<CommonAPI::Runtime> runtime1 = CommonAPI::Runtime::load("DBus");
    std::shared_ptr<CommonAPI::Runtime> runtime2 = CommonAPI::Runtime::load("MyFirstAlias");
    std::shared_ptr<CommonAPI::Runtime> runtime3 = CommonAPI::Runtime::load("MySecondAlias");
    std::shared_ptr<CommonAPI::Runtime> runtime4 = CommonAPI::Runtime::load("DBus");
    EXPECT_TRUE((bool)runtime1);
    EXPECT_TRUE((bool)runtime2);
    EXPECT_TRUE((bool)runtime3);
    EXPECT_TRUE((bool)runtime4);

    EXPECT_EQ(runtime1, runtime2);
    EXPECT_EQ(runtime1, runtime3);
    EXPECT_EQ(runtime2, runtime3);
    EXPECT_EQ(runtime1, runtime4);
    EXPECT_EQ(runtime2, runtime4);
    EXPECT_EQ(runtime3, runtime4);
}

TEST_F(DBusDynamicLoadingMultipleDefinitionTest, RuntimeLoadsFactory) {
    std::shared_ptr<CommonAPI::Runtime> runtime = CommonAPI::Runtime::load("DBus");
    ASSERT_TRUE((bool)runtime);

    std::shared_ptr<CommonAPI::Factory> proxyFactory = runtime->createFactory();
    EXPECT_TRUE((bool)proxyFactory);
}

TEST_F(DBusDynamicLoadingMultipleDefinitionTest, RuntimeLoadsServicePublisher) {
    std::shared_ptr<CommonAPI::Runtime> runtime = CommonAPI::Runtime::load("DBus");
    ASSERT_TRUE((bool)runtime);

    std::shared_ptr<CommonAPI::ServicePublisher> servicePublisher = runtime->getServicePublisher();
    EXPECT_TRUE((bool)servicePublisher);
}

TEST_F(DBusDynamicLoadingMultipleDefinitionTest, FactoryCanCreateProxies) {
    std::shared_ptr<CommonAPI::Runtime> runtime = CommonAPI::Runtime::load("DBus");
    ASSERT_TRUE((bool)runtime);

    std::shared_ptr<CommonAPI::Factory> proxyFactory = runtime->createFactory();
    EXPECT_TRUE((bool)proxyFactory);

    auto defaultTestProxy = proxyFactory->buildProxy<commonapi::tests::TestInterfaceProxy>(testServiceAddress);
    ASSERT_TRUE((bool)defaultTestProxy);
}

TEST_F(DBusDynamicLoadingMultipleDefinitionTest, ServicePublisherCanRegisterStubs) {
    std::shared_ptr<CommonAPI::Runtime> runtime = CommonAPI::Runtime::load("DBus");
    ASSERT_TRUE((bool)runtime);

    std::shared_ptr<CommonAPI::Factory> serviceFactory = runtime->createFactory();
    EXPECT_TRUE((bool)serviceFactory);

    std::shared_ptr<CommonAPI::ServicePublisher> servicePublisher = runtime->getServicePublisher();
    EXPECT_TRUE((bool)servicePublisher);

    auto myStub = std::make_shared<commonapi::tests::TestInterfaceStubDefault>();

    servicePublisher->registerService(myStub, testServiceAddress, serviceFactory);
    servicePublisher->unregisterService(testServiceAddress);
}

TEST_F(DBusDynamicLoadingMultipleDefinitionTest, CreatedProxiesAndServicesCanCommunicate) {
    std::shared_ptr<CommonAPI::Runtime> runtime = CommonAPI::Runtime::load("DBus");
    ASSERT_TRUE((bool)runtime);

    std::shared_ptr<CommonAPI::Factory> proxyFactory = runtime->createFactory();
    EXPECT_TRUE((bool)proxyFactory);

    auto defaultTestProxy = proxyFactory->buildProxy<commonapi::tests::TestInterfaceProxy>(testServiceAddress);
    ASSERT_TRUE((bool)defaultTestProxy);

    std::shared_ptr<CommonAPI::Factory> serviceFactory = runtime->createFactory();
    EXPECT_TRUE((bool)serviceFactory);

    std::shared_ptr<CommonAPI::ServicePublisher> servicePublisher = runtime->getServicePublisher();
    EXPECT_TRUE((bool)servicePublisher);

    auto myStub = std::make_shared<commonapi::tests::TestInterfaceStubDefault>();

    servicePublisher->registerService(myStub, testServiceAddress, serviceFactory);

    for (uint32_t i = 0; i < 300 && !defaultTestProxy->isAvailable(); ++i) {
        usleep(1000);
    }
    EXPECT_TRUE(defaultTestProxy->isAvailable());

    CommonAPI::CallStatus status;
    defaultTestProxy->testEmptyMethod(status);
    ASSERT_EQ(CommonAPI::CallStatus::SUCCESS, status);

    servicePublisher->unregisterService(testServiceAddress);
}

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