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
|
/* Copyright (C) 2014 BMW Group
* 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/. */
/**
* @file Communication
*/
#include <functional>
#include <condition_variable>
#include <mutex>
#include <thread>
#include <fstream>
#include <gtest/gtest.h>
#include "CommonAPI/CommonAPI.hpp"
#include "v1_0/commonapi/communication/TestInterfaceProxy.hpp"
#include "stub/CMAttributesStub.h"
const std::string serviceId = "service-sample";
const std::string clientId = "client-sample";
const std::string domain = "local";
const std::string testAddress = "commonapi.communication.TestInterface";
const int tasync = 100000;
using namespace v1_0::commonapi::communication;
class Environment: public ::testing::Environment {
public:
virtual ~Environment() {
}
virtual void SetUp() {
}
virtual void TearDown() {
}
};
class CMAttributes: public ::testing::Test {
public:
void recvValue(const CommonAPI::CallStatus& callStatus, uint8_t y) {
EXPECT_EQ(callStatus, CommonAPI::CallStatus::SUCCESS);
value_ = y;
}
void recvSubscribedValue(uint8_t y) {
value_ = y;
}
protected:
void SetUp() {
runtime_ = CommonAPI::Runtime::get();
ASSERT_TRUE((bool)runtime_);
std::mutex availabilityMutex;
std::unique_lock<std::mutex> lock(availabilityMutex);
std::condition_variable cv;
bool proxyAvailable = false;
std::thread t1([this, &proxyAvailable, &cv, &availabilityMutex]() {
testProxy_ = runtime_->buildProxy<TestInterfaceProxy>(domain, testAddress, clientId);
testProxy_->isAvailableBlocking();
std::lock_guard<std::mutex> lock(availabilityMutex);
ASSERT_TRUE((bool)testProxy_);
proxyAvailable = true;
cv.notify_one();
});
testStub_ = std::make_shared<CMAttributesStub>();
bool serviceRegistered = runtime_->registerService(domain, testAddress, testStub_, serviceId);
ASSERT_TRUE(serviceRegistered);
while(!proxyAvailable) {
cv.wait(lock);
}
t1.join();
ASSERT_TRUE(testProxy_->isAvailable());
}
void TearDown() {
bool serviceUnregistered =
runtime_->unregisterService(domain, CMAttributesStub::StubInterface::getInterface(),
testAddress);
ASSERT_TRUE(serviceUnregistered);
}
uint8_t value_;
std::shared_ptr<CommonAPI::Runtime> runtime_;
std::shared_ptr<CMAttributesStub> testStub_;
std::shared_ptr<TestInterfaceProxy<>> testProxy_;
};
/**
* @test Test synchronous getValue API function for attributes with combinations of
* additional properties readonly and noSubscriptions (testAttribute,
* testA readonly, testB noSubscriptions, testC readonly noSubscriptions).
* - Set attribute to certain value on stub side.
* - Call getValue.
* - Check if returned call status is CommonAPI::CallStatus::SUCCESS.
* - Check if value of is equal to expected value.
*/
TEST_F(CMAttributes, AttributeGetSynchronous) {
CommonAPI::CallStatus callStatus;
uint8_t x = 5;
uint8_t y = 0;
testStub_->setTestValues(x);
testProxy_->getTestAttributeAttribute().getValue(callStatus, y);
EXPECT_EQ(callStatus, CommonAPI::CallStatus::SUCCESS);
EXPECT_EQ(x, y);
x = 6;
y = 0;
testStub_->setTestValues(x);
testProxy_->getTestAAttribute().getValue(callStatus, y);
EXPECT_EQ(callStatus, CommonAPI::CallStatus::SUCCESS);
EXPECT_EQ(x, y);
x = 7;
y = 0;
testStub_->setTestValues(x);
testProxy_->getTestBAttribute().getValue(callStatus, y);
EXPECT_EQ(callStatus, CommonAPI::CallStatus::SUCCESS);
EXPECT_EQ(x, y);
x = 8;
y = 0;
testStub_->setTestValues(x);
testProxy_->getTestCAttribute().getValue(callStatus, y);
EXPECT_EQ(callStatus, CommonAPI::CallStatus::SUCCESS);
EXPECT_EQ(x, y);
}
/**
* @test Test asynchronous getValue API function for attributes with combinations of
* additional properties readonly and noSubscriptions (testAttribute,
* testA readonly, testB noSubscriptions, testC readonly noSubscriptions).
* - Set attribute to certain value on stub side.
* - Call getValue.
* - Check if returned call status is CommonAPI::CallStatus::SUCCESS.
* - Check if value of is equal to expected value.
*/
TEST_F(CMAttributes, AttributeGetAsynchronous) {
CommonAPI::CallStatus callStatus;
std::function<void (const CommonAPI::CallStatus&, uint8_t)> myCallback =
std::bind(&CMAttributes::recvValue, this, std::placeholders::_1, std::placeholders::_2);
uint8_t x = 5;
testStub_->setTestValues(x);
testProxy_->getTestAttributeAttribute().getValueAsync(myCallback);
usleep(100000);
EXPECT_EQ(x, value_);
x = 6;
value_ = x;
testStub_->setTestValues(x);
testProxy_->getTestAttributeAttribute().getValueAsync(myCallback);
usleep(100000);
EXPECT_EQ(x, value_);
x = 7;
value_ = x;
testStub_->setTestValues(x);
testProxy_->getTestAttributeAttribute().getValueAsync(myCallback);
usleep(100000);
EXPECT_EQ(x, value_);
x = 8;
value_ = x;
testStub_->setTestValues(x);
testProxy_->getTestAttributeAttribute().getValueAsync(myCallback);
usleep(100000);
EXPECT_EQ(x, value_);
}
/**
* @test Test synchronous setValue API function for attributes with combinations of
* additional properties readonly and noSubscriptions (testAttribute, testB noSubscriptions)
* - Set attribute to certain value on proxy side.
* - Check if returned call status is CommonAPI::CallStatus::SUCCESS.
* - Check if returned value of setValue is equal to expected value.
*/
TEST_F(CMAttributes, AttributeSetSynchronous) {
CommonAPI::CallStatus callStatus;
uint8_t x = 5;
uint8_t y = 0;
testProxy_->getTestAttributeAttribute().setValue(x, callStatus, y);
EXPECT_EQ(callStatus, CommonAPI::CallStatus::SUCCESS);
EXPECT_EQ(x, y);
x = 6;
y = 0;
testProxy_->getTestBAttribute().setValue(x, callStatus, y);
EXPECT_EQ(callStatus, CommonAPI::CallStatus::SUCCESS);
EXPECT_EQ(x, y);
}
/**
* @test Test asynchronous setValue API function for attributes with combinations of
* additional properties readonly and noSubscriptions (testAttribute, testB noSubscriptions).
* - Set attribute to certain value on proxy side.
* - Check if returned call status is CommonAPI::CallStatus::SUCCESS.
* - Check if returned value of setValue is equal to expected value.
*/
TEST_F(CMAttributes, AttributeSetAsynchronous) {
CommonAPI::CallStatus callStatus;
std::function<void (const CommonAPI::CallStatus&, uint8_t)> myCallback =
std::bind(&CMAttributes::recvValue, this, std::placeholders::_1, std::placeholders::_2);
testStub_->setTestValues(0);
uint8_t x = 5;
testProxy_->getTestAttributeAttribute().setValueAsync(x, myCallback);
usleep(100000);
EXPECT_EQ(x, value_);
x = 6;
testProxy_->getTestBAttribute().setValueAsync(x, myCallback);
usleep(100000);
EXPECT_EQ(x, value_);
}
/**
* @test Test subscription API function for attributes
*
* - Subscribe on testAttribute.
* - Set attribute to certain value on stub side.
* - Do checks of call status (CommonAPI::CallStatus::SUCCESS) and returned value in callback function.
* - Checks if returned value of setValue is equal to expected value.
* - Set attribute to certain value with synchronous call from proxy.
* - Check again.
*/
TEST_F(CMAttributes, AttributeSubscription) {
CommonAPI::CallStatus callStatus;
std::function<void (uint8_t)> myCallback =
std::bind(&CMAttributes::recvSubscribedValue, this, std::placeholders::_1);
testStub_->setTestValues(0);
uint8_t y = 0;
uint8_t x = 5;
value_ = 0;
testProxy_->getTestAttributeAttribute().getChangedEvent().subscribe(myCallback);
testStub_->setTestValues(x);
usleep(100000);
EXPECT_EQ(x, value_);
x = 6;
testProxy_->getTestAttributeAttribute().setValue(x, callStatus, y);
usleep(100000);
EXPECT_EQ(x, value_);
}
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
::testing::AddGlobalTestEnvironment(new Environment());
return RUN_ALL_TESTS();
}
|