1 //******************************************************************
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include <gtest/gtest.h>
22 #include <HippoMocks/hippomocks.h>
25 #include <condition_variable>
29 #include "NSProviderInterface.h"
30 #include "NSConsumerSimulator.h"
35 std::atomic_bool g_isStartedStack(false);
37 std::chrono::milliseconds g_waitForResponse(500);
39 std::condition_variable responseCon;
40 std::mutex mutexForCondition;
42 NSConsumerSimulator g_consumerSimul;
46 char g_sourceName[100];
49 class TestWithMock: public testing::Test
55 virtual ~TestWithMock() noexcept(noexcept(std::declval<Test>().~Test())) {}
57 virtual void TearDown() {
70 class NotificationProviderTest : public TestWithMock
73 NotificationProviderTest() = default;
74 ~NotificationProviderTest() = default;
76 static void NSRequestedSubscribeCallbackEmpty(NSConsumer *)
78 std::cout << __func__ << std::endl;
81 static void NSSyncCallbackEmpty(NSSyncInfo *)
83 std::cout << __func__ << std::endl;
86 static void NSMessageCallbackFromConsumerEmpty(
87 const int &, const std::string &, const std::string &, const std::string &)
89 std::cout << __func__ << std::endl;
92 static void NSSyncCallbackFromConsumerEmpty(int, int)
94 std::cout << __func__ << std::endl;
101 TestWithMock::SetUp();
103 if (g_isStartedStack == false)
105 OC::PlatformConfig cfg
107 OC::ServiceType::InProc,
111 OC::QualityOfService::HighQos
113 OC::OCPlatform::Configure(cfg);
117 OC::OCPlatform::stopPresence();
124 g_isStartedStack = true;
126 strncpy(g_title, "Title", strlen("Title"));
127 strncpy(g_body, "ContentText", strlen("ContentText"));
128 strncpy(g_sourceName, "OIC", strlen("OIC"));
135 TestWithMock::TearDown();
140 TEST_F(NotificationProviderTest, StartProviderPositiveWithNSPolicyTrue)
142 NSProviderConfig config;
143 config.subRequestCallback = NSRequestedSubscribeCallbackEmpty;
144 config.syncInfoCallback = NSSyncCallbackEmpty;
145 config.subControllability = true;
146 config.userInfo = NULL;
148 NSResult ret = NSStartProvider(config);
150 std::unique_lock< std::mutex > lock{ mutexForCondition };
151 responseCon.wait_for(lock, g_waitForResponse);
153 EXPECT_EQ(ret, NS_OK);
156 TEST_F(NotificationProviderTest, StopProviderPositive)
158 NSResult ret = NSStopProvider();
160 std::unique_lock< std::mutex > lock{ mutexForCondition };
161 responseCon.wait_for(lock, g_waitForResponse);
163 EXPECT_EQ(ret, NS_OK);
166 TEST_F(NotificationProviderTest, StartProviderPositiveWithNSPolicyFalse)
168 NSProviderConfig config;
169 config.subRequestCallback = NSRequestedSubscribeCallbackEmpty;
170 config.syncInfoCallback = NSSyncCallbackEmpty;
171 config.subControllability = false;
172 config.userInfo = NULL;
174 NSResult ret = NSStartProvider(config);
176 std::unique_lock< std::mutex > lock{ mutexForCondition };
177 responseCon.wait_for(lock, std::chrono::milliseconds(3000));
178 g_consumerSimul.findProvider();
180 responseCon.wait_for(lock, std::chrono::milliseconds(3000));
182 EXPECT_EQ(ret, NS_OK);
185 TEST_F(NotificationProviderTest, ExpectCallbackWhenReceiveSubscribeRequestWithAccepterProvider)
188 mocks.OnCallFunc(NSRequestedSubscribeCallbackEmpty).Do(
189 [](NSConsumer * consumer)
191 std::cout << "NSRequestedSubscribeCallback" << std::endl;
192 g_consumerID = strdup(consumer->consumerId);
193 responseCon.notify_all();
196 NSProviderConfig config;
197 config.subRequestCallback = NSRequestedSubscribeCallbackEmpty;
198 config.syncInfoCallback = NSSyncCallbackEmpty;
199 config.subControllability = true;
200 config.userInfo = NULL;
202 NSStartProvider(config);
205 std::unique_lock< std::mutex > lock{ mutexForCondition };
206 responseCon.wait_for(lock, g_waitForResponse);
209 g_consumerSimul.setCallback(NSMessageCallbackFromConsumerEmpty,
210 NSSyncCallbackFromConsumerEmpty);
211 g_consumerSimul.findProvider();
213 std::unique_lock< std::mutex > lock{ mutexForCondition };
214 responseCon.wait_for(lock, std::chrono::milliseconds(1000));
216 EXPECT_NE((void*)g_consumerID, (void*)NULL);
219 TEST_F(NotificationProviderTest, NeverCallNotifyOnConsumerByAcceptIsFalse)
221 bool expectTrue = true;
224 mocks.OnCallFunc(NSMessageCallbackFromConsumerEmpty).Do(
225 [& expectTrue, &msgID](const int &id, const std::string&, const std::string&, const std::string&)
229 std::cout << "This function never call" << std::endl;
234 NSAcceptSubscription(g_consumerID, false);
236 NSMessage * msg = NSCreateMessage();
237 msgID = (int)msg->messageId;
238 msg->title = g_title;
239 msg->contentText = g_body;
240 msg->sourceName = g_sourceName;
244 std::unique_lock< std::mutex > lock{ mutexForCondition };
245 responseCon.wait_for(lock, g_waitForResponse);
248 std::unique_lock< std::mutex > lock{ mutexForCondition };
249 responseCon.wait_for(lock, std::chrono::milliseconds(1000));
251 EXPECT_EQ(expectTrue, true);
253 NSAcceptSubscription(g_consumerID, true);
256 TEST_F(NotificationProviderTest, ExpectCallNotifyOnConsumerByAcceptIsTrue)
260 mocks.ExpectCallFunc(NSMessageCallbackFromConsumerEmpty).Do(
261 [&msgID](const int &id, const std::string&, const std::string&, const std::string&)
263 std::cout << "id : " << id << std::endl;
266 std::cout << "ExpectCallNotifyOnConsumerByAcceptIsTrue" << std::endl;
267 responseCon.notify_all();
271 NSAcceptSubscription(g_consumerID, true);
273 NSMessage * msg = NSCreateMessage();
274 msgID = (int)msg->messageId;
275 msg->title = g_title;
276 msg->contentText = g_body;
277 msg->sourceName = g_sourceName;
280 std::unique_lock< std::mutex > lock{ mutexForCondition };
281 responseCon.wait(lock);
284 TEST_F(NotificationProviderTest, ExpectCallbackSyncOnReadToConsumer)
288 mocks.ExpectCallFunc(NSSyncCallbackFromConsumerEmpty).Do(
289 [& id](int & type, int &syncId)
291 std::cout << "NSSyncCallbackEmpty" << std::endl;
293 type == NS_SYNC_READ)
295 std::cout << "ExpectCallbackSyncOnReadFromConsumer" << std::endl;
296 responseCon.notify_all();
300 NSMessage * msg = NSCreateMessage();
301 id = (int)msg->messageId;
302 msg->title = g_title;
303 msg->contentText = g_body;
304 msg->sourceName = g_sourceName;
306 NSProviderSendSyncInfo(msg->messageId, NS_SYNC_READ);
307 std::unique_lock< std::mutex > lock{ mutexForCondition };
308 responseCon.wait_for(lock, std::chrono::milliseconds(5000));
311 TEST_F(NotificationProviderTest, ExpectCallbackSyncOnReadFromConsumer)
313 int type = NS_SYNC_READ;
315 mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
316 [& id](NSSyncInfo * sync)
318 std::cout << "NSSyncCallbackEmpty" << std::endl;
319 if ((int)sync->messageId == id && sync->state == NS_SYNC_READ)
321 std::cout << "ExpectCallbackSyncOnReadFromConsumer" << std::endl;
322 responseCon.notify_all();
326 NSMessage * msg = NSCreateMessage();
327 id = (int)msg->messageId;
328 msg->title = g_title;
329 msg->contentText = g_body;
330 msg->sourceName = g_sourceName;
332 g_consumerSimul.syncToProvider(type, id, msg->providerId);
333 std::unique_lock< std::mutex > lock{ mutexForCondition };
334 responseCon.wait_for(lock, std::chrono::milliseconds(5000));
337 TEST_F(NotificationProviderTest, ExpectEqualAddedTopicsAndRegisteredTopics)
339 std::string str("TEST1");
340 std::string str2("TEST2");
341 NSProviderRegisterTopic(str.c_str());
342 NSProviderRegisterTopic(str2.c_str());
345 NSTopicLL * topics = NSProviderGetTopics();
349 printf("topic is NULL\n");
354 NSTopicLL * iter = topics;
355 std::string compStr(iter->topicName);
356 std::string compStr2(iter->next->topicName);
358 printf("str = %s, compStr = %s\n", str.c_str(), iter->topicName);
359 printf("str2 = %s, compStr2 = %s\n", str2.c_str(), iter->next->topicName);
361 if(str.compare(compStr) == 0 && str2.compare(compStr2) == 0)
367 EXPECT_EQ(isSame, true);
370 TEST_F(NotificationProviderTest, ExpectEqualUnregisteredTopicsAndRegisteredTopics)
372 std::string str("TEST1");
373 std::string str2("TEST2");
374 NSProviderRegisterTopic(str.c_str());
375 NSProviderRegisterTopic(str2.c_str());
376 NSProviderUnregisterTopic(str2.c_str());
379 NSTopicLL * topics = NSProviderGetTopics();
383 printf("topic is NULL\n");
388 NSTopicLL * iter = topics;
389 std::string compStr(iter->topicName);
391 printf("str = %s, compStr = %s\n", str.c_str(), iter->topicName);
393 if(str.compare(compStr) == 0)
399 EXPECT_EQ(isSame, true);
402 TEST_F(NotificationProviderTest, CancelObserves)
404 bool ret = g_consumerSimul.cancelObserves();
406 std::unique_lock< std::mutex > lock{ mutexForCondition };
407 responseCon.wait_for(lock, std::chrono::milliseconds(5000));
409 EXPECT_EQ(ret, true);