1 /* Copyright (C) 2013 BMW Group
2 * Author: Manfred Bathelt (manfred.bathelt@bmw.de)
3 * Author: Juergen Gehring (juergen.gehring@bmw.de)
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
9 #include <gtest/gtest.h>
14 #include <CommonAPI/DBus/DBusAddressTranslator.h>
15 #include <CommonAPI/DBus/DBusUtils.h>
17 #include <CommonAPI/types.h>
18 #include <CommonAPI/AttributeExtension.h>
19 #include <CommonAPI/Runtime.h>
21 #include <CommonAPI/DBus/DBusConnection.h>
22 #include <CommonAPI/DBus/DBusProxy.h>
23 #include <CommonAPI/DBus/DBusRuntime.h>
25 #include "commonapi/tests/PredefinedTypeCollection.h"
26 #include "commonapi/tests/DerivedTypeCollection.h"
27 #include "commonapi/tests/TestInterfaceProxy.h"
28 #include "commonapi/tests/TestInterfaceStubDefault.h"
29 #include "commonapi/tests/TestInterfaceDBusStubAdapter.h"
31 #include "fakeLegacyService/fake/legacy/service/LegacyInterfaceProxy.h"
34 static const std::vector<std::string> commonApiAddresses = {
35 "local:no.nothing.service:no.nothing.instance",
36 "local:service:instance",
37 "local:no.interface.service:no.interface.instance",
38 "local:no.connection.service:no.connection.instance",
39 "local:no.object.service:no.object.instance",
40 "local:only.interface.service:only.interface.instance",
41 "local:only.connection.service:only.connection.instance",
42 "local:only.object.service:only.object.instance",
43 "local:fake.legacy.service.LegacyInterface:fake.legacy.service"
46 static const std::string fileString =
47 "[local:no.nothing.service:no.nothing.instance]\n"
49 "[local:service:instance]\n"
50 "dbus_connection=connection.name\n"
51 "dbus_object=/path/to/object\n"
52 "dbus_interface=service.name\n"
54 "[local:no.interface.service:no.interface.instance]\n"
55 "dbus_connection=no.interface.connection\n"
56 "dbus_object=/no/interface/path\n"
58 "[local:no.connection.service:no.connection.instance]\n"
59 "dbus_object=/no/connection/path\n"
60 "dbus_interface=no.connection.interface\n"
62 "[local:no.object.service:no.object.instance]\n"
63 "dbus_connection=no.object.connection\n"
64 "dbus_interface=no.object.interface\n"
66 "[local:only.interface.service:only.interface.instance]\n"
67 "dbus_interface=only.interface.interface\n"
69 "[local:only.connection.service:only.connection.instance]\n"
70 "dbus_connection=only.connection.connection\n"
72 "[local:only.object.service:only.object.instance]\n"
73 "dbus_object=/only/object/path\n"
75 "[local:fake.legacy.service.LegacyInterface:fake.legacy.service]\n"
76 "dbus_connection=fake.legacy.service.connection\n"
77 "dbus_object=/some/legacy/path/6259504\n"
78 "dbus_interface=fake.legacy.service.LegacyInterface\n";
80 typedef std::vector<CommonAPI::DBus::DBusServiceAddress>::value_type vt;
81 static const std::vector<CommonAPI::DBus::DBusServiceAddress> dbusAddresses = {
82 vt("no.nothing.instance", "/no/nothing/instance", "no.nothing.service", false),
83 vt("connection.name", "/path/to/object", "service.name", false),
84 vt("no.interface.connection", "/no/interface/path", "no.interface.service", false),
85 vt("no.connection.instance", "/no/connection/path", "no.connection.interface", false),
86 vt("no.object.connection", "/no/object/instance", "no.object.interface", false),
87 vt("only.interface.instance", "/only/interface/instance", "only.interface.interface", false),
88 vt("only.connection.connection", "/only/connection/instance", "only.connection.service", false),
89 vt("only.object.instance", "/only/object/path", "only.object.service", false),
90 vt("fake.legacy.service.connection", "/some/legacy/path/6259504", "fake.legacy.service.LegacyInterface", false)
94 class Environment: public ::testing::Environment {
96 virtual ~Environment() {
99 virtual void SetUp() {
100 configFileName_ = CommonAPI::DBus::getCurrentBinaryFileFQN();
101 configFileName_ += CommonAPI::DBus::DBUS_CONFIG_SUFFIX;
102 std::ofstream configFile(configFileName_);
103 ASSERT_TRUE(configFile.is_open());
104 configFile << fileString;
108 virtual void TearDown() {
109 std::remove(configFileName_.c_str());
112 std::string configFileName_;
116 class AddressTranslatorTest: public ::testing::Test {
121 virtual void TearDown() {
126 TEST_F(AddressTranslatorTest, InstanceCanBeRetrieved) {
127 CommonAPI::DBus::DBusAddressTranslator& translator = CommonAPI::DBus::DBusAddressTranslator::getInstance();
131 TEST_F(AddressTranslatorTest, ParsesDBusAddresses) {
132 CommonAPI::DBus::DBusAddressTranslator& translator = CommonAPI::DBus::DBusAddressTranslator::getInstance();
134 for(unsigned int i = 0; i < commonApiAddresses.size(); i++) {
135 std::string interfaceName, connectionName, objectPath;
136 translator.searchForDBusAddress(commonApiAddresses[i], interfaceName, connectionName, objectPath);
137 ASSERT_EQ(std::get<0>(dbusAddresses[i]), connectionName);
138 ASSERT_EQ(std::get<1>(dbusAddresses[i]), objectPath);
139 ASSERT_EQ(std::get<2>(dbusAddresses[i]), interfaceName);
144 TEST_F(AddressTranslatorTest, ParsesCommonAPIAddresses) {
145 CommonAPI::DBus::DBusAddressTranslator& translator = CommonAPI::DBus::DBusAddressTranslator::getInstance();
147 for(unsigned int i = 0; i < commonApiAddresses.size(); i++) {
148 std::string commonApiAddress;
149 translator.searchForCommonAddress(
150 std::get<2>(dbusAddresses[i]),
151 std::get<0>(dbusAddresses[i]),
152 std::get<1>(dbusAddresses[i]),
154 ASSERT_EQ(commonApiAddresses[i], commonApiAddress);
159 TEST_F(AddressTranslatorTest, ServicesUsingPredefinedAddressesCanCommunicate) {
160 std::shared_ptr<CommonAPI::Runtime> runtime;
161 std::shared_ptr<CommonAPI::Factory> proxyFactory;
162 std::shared_ptr<CommonAPI::Factory> stubFactory;
164 runtime = CommonAPI::Runtime::load();
165 ASSERT_TRUE((bool)runtime);
166 CommonAPI::DBus::DBusRuntime* dbusRuntime = dynamic_cast<CommonAPI::DBus::DBusRuntime*>(&(*runtime));
167 ASSERT_TRUE(dbusRuntime != NULL);
169 proxyFactory = runtime->createFactory();
170 ASSERT_TRUE((bool)proxyFactory);
171 stubFactory = runtime->createFactory();
172 ASSERT_TRUE((bool)stubFactory);
174 auto defaultTestProxy = proxyFactory->buildProxy<commonapi::tests::TestInterfaceProxy>(commonApiAddresses[0]);
175 ASSERT_TRUE((bool)defaultTestProxy);
177 auto stub = std::make_shared<commonapi::tests::TestInterfaceStubDefault>();
178 bool success = stubFactory->registerService(stub, commonApiAddresses[0]);
179 ASSERT_TRUE(success);
182 std::string v2 = "Hai :)";
183 CommonAPI::CallStatus stat;
184 defaultTestProxy->testVoidPredefinedTypeMethod(v1, v2, stat);
186 ASSERT_EQ(stat, CommonAPI::CallStatus::SUCCESS);
190 const std::string addressOfFakeLegacyService = commonApiAddresses[8];
192 const std::string domainOfFakeLegacyService = "local";
193 const std::string serviceIdOfFakeLegacyService = "fake.legacy.service.LegacyInterface";
194 const std::string participantIdOfFakeLegacyService = "fake.legacy.service";
196 TEST_F(AddressTranslatorTest, CreatedProxyHasCorrectCommonApiAddress) {
197 std::shared_ptr<CommonAPI::Runtime> runtime = CommonAPI::Runtime::load();
198 ASSERT_TRUE((bool)runtime);
199 CommonAPI::DBus::DBusRuntime* dbusRuntime = dynamic_cast<CommonAPI::DBus::DBusRuntime*>(&(*runtime));
200 ASSERT_TRUE(dbusRuntime != NULL);
202 std::shared_ptr<CommonAPI::Factory> proxyFactory = runtime->createFactory();
203 ASSERT_TRUE((bool)proxyFactory);
204 auto proxyForFakeLegacyService = proxyFactory->buildProxy<fake::legacy::service::LegacyInterfaceProxy>(addressOfFakeLegacyService);
205 ASSERT_TRUE((bool)proxyForFakeLegacyService);
207 ASSERT_EQ(addressOfFakeLegacyService, proxyForFakeLegacyService->getAddress());
208 ASSERT_EQ(domainOfFakeLegacyService, proxyForFakeLegacyService->getDomain());
209 ASSERT_EQ(serviceIdOfFakeLegacyService, proxyForFakeLegacyService->getServiceId());
210 ASSERT_EQ(participantIdOfFakeLegacyService, proxyForFakeLegacyService->getInstanceId());
214 void fakeLegacyServiceThread() {
215 int resultCode = system("python ./src/test/fakeLegacyService/fakeLegacyService.py");
216 EXPECT_EQ(0, resultCode);
219 TEST_F(AddressTranslatorTest, FakeLegacyServiceCanBeAddressed) {
220 std::thread fakeServiceThread = std::thread(fakeLegacyServiceThread);
223 std::shared_ptr<CommonAPI::Runtime> runtime = CommonAPI::Runtime::load();
224 ASSERT_TRUE((bool)runtime);
225 CommonAPI::DBus::DBusRuntime* dbusRuntime = dynamic_cast<CommonAPI::DBus::DBusRuntime*>(&(*runtime));
226 ASSERT_TRUE(dbusRuntime != NULL);
228 std::shared_ptr<CommonAPI::Factory> proxyFactory = runtime->createFactory();
229 ASSERT_TRUE((bool)proxyFactory);
230 auto proxyForFakeLegacyService = proxyFactory->buildProxy<fake::legacy::service::LegacyInterfaceProxy>(addressOfFakeLegacyService);
231 ASSERT_TRUE((bool)proxyForFakeLegacyService);
233 ASSERT_EQ(addressOfFakeLegacyService, proxyForFakeLegacyService->getAddress());
235 CommonAPI::CallStatus status;
237 const int32_t input = 42;
238 int32_t output1, output2;
239 proxyForFakeLegacyService->TestMethod(input, status, output1, output2);
240 EXPECT_EQ(CommonAPI::CallStatus::SUCCESS, status);
241 if(CommonAPI::CallStatus::SUCCESS == status) {
242 EXPECT_EQ(input -5, output1);
243 EXPECT_EQ(input +5, output2);
246 std::string greeting;
248 proxyForFakeLegacyService->OtherTestMethod(status, greeting, identifier);
249 EXPECT_EQ(CommonAPI::CallStatus::SUCCESS, status);
250 if(CommonAPI::CallStatus::SUCCESS == status) {
251 EXPECT_EQ(std::string("Hello"), greeting);
252 EXPECT_EQ(42, identifier);
255 //end the fake legacy service via dbus
256 int resultCode = system("python ./src/test/fakeLegacyService/sendToFakeLegacyService.py finish");
257 EXPECT_EQ(0, resultCode);
259 fakeServiceThread.join();
263 int main(int argc, char** argv) {
264 ::testing::InitGoogleTest(&argc, argv);
265 ::testing::AddGlobalTestEnvironment(new Environment());
266 return RUN_ALL_TESTS();