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>
32 #include "NSConsumerInterface.h"
34 #include "NSProviderSimulator.h"
38 NSProviderSimulator g_providerSimul;
39 NSProvider * g_provider;
41 std::atomic_bool g_isStartedStack(false);
43 std::chrono::milliseconds g_waitForResponse(500);
45 std::condition_variable responseCon;
46 std::mutex mutexForCondition;
50 NS_SELECTION_CONSUMER = 0,
51 NS_SELECTION_PROVIDER = 1
56 NSProviderSimulator::NS_TopicStateList g_topicStateList;
59 class TestWithMock: public testing::Test
65 virtual ~TestWithMock() noexcept(noexcept(std::declval<Test>().~Test()))
70 virtual void TearDown()
84 class NotificationConsumerTest : public TestWithMock
87 NotificationConsumerTest() = default;
88 ~NotificationConsumerTest() = default;
90 static void NSNotificationReceivedCallbackEmpty(NSMessage *)
92 std::cout << __func__ << std::endl;
95 static void NSSyncCallbackEmpty(NSSyncInfo *)
97 std::cout << __func__ << std::endl;
100 static void NSFoundResourceEmpty(std::shared_ptr< OC::OCResource >)
102 std::cout << __func__ << std::endl;
105 static void NSProviderChangedCallback(NSProvider *, NSProviderState)
107 std::cout << __func__ << std::endl;
114 TestWithMock::SetUp();
116 if (g_isStartedStack == false)
118 OC::PlatformConfig occfg
120 OC::ServiceType::InProc,
124 OC::QualityOfService::LowQos
126 OC::OCPlatform::Configure(occfg);
130 OC::OCPlatform::stopPresence();
137 g_isStartedStack = true;
139 cfg.changedCb = NSProviderChangedCallback;
140 cfg.messageCb = NSNotificationReceivedCallbackEmpty;
141 cfg.syncInfoCb = NSSyncCallbackEmpty;
148 TestWithMock::TearDown();
153 TEST_F(NotificationConsumerTest, StartConsumerNegativeNonSetChangedCB)
155 cfg.changedCb = NULL;
156 cfg.messageCb = NSNotificationReceivedCallbackEmpty;
157 cfg.syncInfoCb = NSSyncCallbackEmpty;
159 EXPECT_EQ(NS_ERROR, NSStartConsumer(cfg));
160 std::unique_lock< std::mutex > lock{ mutexForCondition };
161 responseCon.wait_for(lock, g_waitForResponse);
164 TEST_F(NotificationConsumerTest, StartConsumerNegativeNonSetNotiReceiveCB)
166 cfg.changedCb = NSProviderChangedCallback;
167 cfg.messageCb = NULL;
168 cfg.syncInfoCb = NSSyncCallbackEmpty;
170 EXPECT_EQ(NS_ERROR, NSStartConsumer(cfg));
171 std::unique_lock< std::mutex > lock{ mutexForCondition };
172 responseCon.wait_for(lock, g_waitForResponse);
175 TEST_F(NotificationConsumerTest, StartConsumerNegativeNonSetSyncCB)
177 cfg.changedCb = NSProviderChangedCallback;
178 cfg.messageCb = NSNotificationReceivedCallbackEmpty;
179 cfg.syncInfoCb = NULL;
181 EXPECT_EQ(NS_ERROR, NSStartConsumer(cfg));
182 std::unique_lock< std::mutex > lock{ mutexForCondition };
183 responseCon.wait_for(lock, g_waitForResponse);
186 TEST_F(NotificationConsumerTest, StartConsumerPositive)
188 cfg.changedCb = NSProviderChangedCallback;
189 cfg.messageCb = NSNotificationReceivedCallbackEmpty;
190 cfg.syncInfoCb = NSSyncCallbackEmpty;
192 EXPECT_EQ(NS_OK, NSStartConsumer(cfg));
193 std::unique_lock< std::mutex > lock{ mutexForCondition };
194 responseCon.wait_for(lock, g_waitForResponse);
197 TEST_F(NotificationConsumerTest, StopConsumerPositive)
199 EXPECT_EQ(NSStopConsumer(), NS_OK);
202 TEST_F(NotificationConsumerTest, StopConsumerNegative)
204 EXPECT_EQ(NSStopConsumer(), NS_ERROR);
207 TEST_F(NotificationConsumerTest, DiscoverProviderWithNonAccepterWhenStartedConsumerFirst)
209 NSProviderState revState = NS_STOPPED;
210 mocks.OnCallFunc(NSProviderChangedCallback).Do(
211 [this, & revState](NSProvider *, NSProviderState state)
213 std::cout << "Call Discovered" << std::endl;
215 responseCon.notify_all();
218 NSStartConsumer(cfg);
220 g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_CONSUMER);
221 g_providerSimul.createNotificationResource();
223 std::unique_lock< std::mutex > lock{ mutexForCondition };
224 responseCon.wait_for(lock, g_waitForResponse);
227 g_providerSimul.deleteNotificationResource();
229 EXPECT_EQ(NS_DISCOVERED, revState);
232 TEST_F(NotificationConsumerTest, DiscoverProviderWithNonAccepterWhenStartedConsumerAfter)
234 g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_CONSUMER);
235 g_providerSimul.createNotificationResource();
237 std::unique_lock< std::mutex > lock{ mutexForCondition };
238 responseCon.wait_for(lock, g_waitForResponse);
241 NSProviderState revState = NS_STOPPED;
242 mocks.OnCallFunc(NSProviderChangedCallback).Do(
243 [this, & revState](NSProvider * provider, NSProviderState state)
245 std::cout << "Call Discovered" << std::endl;
247 g_provider = provider;
249 responseCon.notify_all();
252 NSStartConsumer(cfg);
254 std::unique_lock< std::mutex > lock{ mutexForCondition };
255 responseCon.wait_for(lock, g_waitForResponse);
257 EXPECT_EQ(NS_DISCOVERED, revState);
260 TEST_F(NotificationConsumerTest, DiscoverProviderWithNonAccepterWhenRescan)
262 g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_CONSUMER);
263 NSProviderState revState = NS_STOPPED;
264 mocks.OnCallFunc(NSProviderChangedCallback).Do(
265 [this, &revState](NSProvider * provider, NSProviderState state)
267 std::cout << "Call Discovered" << std::endl;
269 g_provider = provider;
270 std::cout << g_provider->providerId << std::endl;
271 responseCon.notify_all();
276 std::unique_lock< std::mutex > lock{ mutexForCondition };
277 responseCon.wait_for(lock, g_waitForResponse);
279 EXPECT_EQ(NS_DISCOVERED, revState);
282 TEST_F(NotificationConsumerTest, ExpectSubscribeSuccess)
284 NSProviderState revState = NS_DENY;
285 mocks.OnCallFunc(NSProviderChangedCallback).Do(
286 [this, & revState](NSProvider * , NSProviderState state)
288 std::cout << "Income Changed Callback : " << state << std::endl;
290 responseCon.notify_all();
293 NSResult ret = NSSubscribe(g_provider->providerId);
294 std::unique_lock< std::mutex > lock{ mutexForCondition };
295 responseCon.wait_for(lock, g_waitForResponse);
297 EXPECT_EQ(NS_ALLOW, revState);
298 EXPECT_EQ(NS_OK, ret);
301 TEST_F(NotificationConsumerTest, ExpectReceiveNotification)
304 std::string title = "title";
305 std::string msg = "msg";
309 mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
310 [this, & revId](NSMessage * message)
312 std::cout << "Income Notification : " << message->messageId << std::endl;
313 revId = message->messageId;
314 responseCon.notify_all();
317 g_providerSimul.notifyMessage(id, title, msg);
319 std::unique_lock< std::mutex > lock{ mutexForCondition };
320 responseCon.wait_for(lock, g_waitForResponse);
322 EXPECT_EQ(id, revId);
324 g_providerSimul.deleteNotificationResource();
327 TEST_F(NotificationConsumerTest, ExpectReceiveSubAllowWithAccepterisProvider)
329 g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_PROVIDER);
330 NSProviderState revState = NS_DENY;
331 g_providerSimul.createNotificationResource();
333 std::unique_lock< std::mutex > lock{ mutexForCondition };
334 responseCon.wait_for(lock, g_waitForResponse);
337 mocks.OnCallFunc(NSProviderChangedCallback).Do(
338 [this, & revState](NSProvider * provider, NSProviderState state)
340 std::cout << "Income Changed Callback : " << state << std::endl;
342 g_provider = provider;
343 responseCon.notify_all();
345 mocks.OnCallFunc(NSProviderChangedCallback).Do(
346 [this, & revState](NSProvider *, NSProviderState state)
348 std::cout << "Income Changed Callback : " << state << std::endl;
350 responseCon.notify_all();
353 NSStartConsumer(cfg);
354 std::unique_lock< std::mutex > lock{ mutexForCondition };
355 responseCon.wait_for(lock, g_waitForResponse);
357 EXPECT_EQ(NS_ALLOW, revState);
360 TEST_F(NotificationConsumerTest, ExpectReceiveNotificationWithAccepterisProvider)
363 std::string title = "title";
364 std::string msg = "msg";
367 mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
368 [this, & id, & revId](NSMessage * message)
370 std::cout << "Income Notification : " << message->messageId << std::endl;
371 revId = message->messageId;
372 responseCon.notify_all();
375 g_providerSimul.notifyMessage(id, title, msg);
377 std::unique_lock< std::mutex > lock{ mutexForCondition };
378 responseCon.wait_for(lock, g_waitForResponse);
380 EXPECT_EQ(id, revId);
383 TEST_F(NotificationConsumerTest, ExpectCallbackReadCheckWhenProviderNotifySync)
386 std::string title = "title";
387 std::string msg = "msg";
388 NSSyncType type = NS_SYNC_DELETED;
390 mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
391 [this](NSMessage * message)
393 std::cout << "Income Notification : " << message->messageId << std::endl;
394 responseCon.notify_all();
397 mocks.OnCallFunc(NSSyncCallbackEmpty).Do(
398 [& type, this](NSSyncInfo * sync)
400 std::cout << "Income SyncInfo : " << sync->messageId
401 << ", State : " << sync->state << std::endl;
403 responseCon.notify_all();
406 g_providerSimul.notifyMessage(id, title, msg);
408 std::unique_lock< std::mutex > lock{ mutexForCondition };
409 responseCon.wait_for(lock, g_waitForResponse);
412 g_providerSimul.sendRead(id);
414 std::unique_lock< std::mutex > lock{ mutexForCondition };
415 responseCon.wait_for(lock, g_waitForResponse);
418 EXPECT_EQ(NS_SYNC_READ, type);
421 TEST_F(NotificationConsumerTest, ExpectCallbackDismissCheckWhenProviderNotifySync)
424 std::string title = "title";
425 std::string msg = "msg";
426 NSSyncType type = NS_SYNC_READ;
428 mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
429 [this](NSMessage * message)
431 std::cout << "Income Notification : " << message->messageId << std::endl;
432 responseCon.notify_all();
435 mocks.OnCallFunc(NSSyncCallbackEmpty).Do(
436 [& type, this](NSSyncInfo * sync)
438 std::cout << "Income Notification : " << sync->messageId
439 << ", State : " << sync->state << std::endl;
441 responseCon.notify_all();
444 g_providerSimul.notifyMessage(id, title, msg);
446 std::unique_lock< std::mutex > lock{ mutexForCondition };
447 responseCon.wait_for(lock, g_waitForResponse);
450 g_providerSimul.sendDismiss(id);
452 std::unique_lock< std::mutex > lock{ mutexForCondition };
453 responseCon.wait_for(lock, g_waitForResponse);
456 EXPECT_EQ(NS_SYNC_DELETED, type);
459 TEST_F(NotificationConsumerTest, ExpectCallbackReadCheckWhenConsumerPostSync)
462 std::string title = "title";
463 std::string msg = "msg";
464 NSSyncType type = NS_SYNC_DELETED;
466 mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
467 [this](NSMessage * message)
469 std::cout << "Income Notification : " << message->messageId << std::endl;
470 NSConsumerSendSyncInfo(message->providerId, message->messageId, NS_SYNC_READ);
471 std::unique_lock< std::mutex > lock{ mutexForCondition };
472 responseCon.wait_for(lock, g_waitForResponse);
475 mocks.OnCallFunc(NSSyncCallbackEmpty).Do(
476 [& type, this](NSSyncInfo * sync)
478 std::cout << "Income Notification : " << sync->messageId
479 << ", State : " << sync->state << std::endl;
481 responseCon.notify_all();
484 g_providerSimul.notifyMessage(id, title, msg);
486 std::unique_lock< std::mutex > lock{ mutexForCondition };
487 responseCon.wait_for(lock, g_waitForResponse);
490 EXPECT_EQ(NS_SYNC_READ, type);
493 TEST_F(NotificationConsumerTest, ExpectCallbackDismissCheckWhenConsumerPostSync)
496 std::string title = "title";
497 std::string msg = "msg";
498 NSSyncType state = NS_SYNC_READ;
500 mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
501 [this](NSMessage * message)
503 std::cout << "Income Notification : " << message->messageId << std::endl;
504 NSConsumerSendSyncInfo(message->providerId, message->messageId, NS_SYNC_DELETED);
505 std::unique_lock< std::mutex > lock{ mutexForCondition };
506 responseCon.wait_for(lock, g_waitForResponse);
509 mocks.OnCallFunc(NSSyncCallbackEmpty).Do(
510 [& state, this](NSSyncInfo * sync)
512 std::cout << "Income Notification : " << sync->messageId
513 << ", State : " << sync->state << std::endl;
515 responseCon.notify_all();
518 g_providerSimul.notifyMessage(id, title, msg);
520 std::unique_lock< std::mutex > lock{ mutexForCondition };
521 responseCon.wait_for(lock, g_waitForResponse);
524 EXPECT_EQ(NS_SYNC_DELETED, state);
527 TEST_F(NotificationConsumerTest, ExpectGetProviderSuccessWithValidProviderId)
529 NSProvider * provider = NSConsumerGetProvider(g_provider->providerId);
530 int ret = strcmp(provider->providerId, g_provider->providerId);
535 TEST_F(NotificationConsumerTest, ExpectGetProviderSuccessWithInvalidProviderId)
537 NSProvider * provider = NSConsumerGetProvider("123456789012345678901234567890123457");
538 EXPECT_EQ(provider, (void*)NULL);
541 TEST_F(NotificationConsumerTest, ExpectGetProviderSuccessWithNULL)
543 NSProvider * provider = NSConsumerGetProvider(NULL);
544 EXPECT_EQ(provider, (void*)NULL);
547 TEST_F(NotificationConsumerTest, ExpectGetTopicListIsNULL)
549 NSTopicLL * currentTopics = NSConsumerGetTopicList(g_provider->providerId);
550 EXPECT_EQ(NULL, currentTopics);
553 TEST_F(NotificationConsumerTest, ExpectCallbackTopicUpdated)
555 NSProviderState revState = NS_STOPPED;
556 mocks.OnCallFunc(NSProviderChangedCallback).Do(
557 [this, & revState](NSProvider * , NSProviderState state)
559 std::cout << "Income Changed Callback : " << state << std::endl;
561 responseCon.notify_all();
564 NSProviderSimulator::NS_TopicList topics;
565 topics.push_back("1");
566 topics.push_back("2");
567 topics.push_back("3");
569 g_providerSimul.setTopics(topics);
571 std::unique_lock< std::mutex > lock{ mutexForCondition };
572 responseCon.wait_for(lock, g_waitForResponse);
574 EXPECT_EQ(NS_TOPIC, revState);
577 TEST_F(NotificationConsumerTest, ExpectEQTopicList)
581 NSProviderSimulator::NS_TopicList topics;
582 topics.push_back("1");
583 topics.push_back("2");
584 topics.push_back("3");
586 NSTopicLL * retTopic = NSConsumerGetTopicList(g_provider->providerId);
587 std::for_each (topics.begin(), topics.end(),
588 [this, & retTopic, & isSame](const std::string & str)
590 isSame = (str == std::string(retTopic->topicName));
591 retTopic = retTopic->next;
594 EXPECT_EQ(true, isSame);
597 TEST_F(NotificationConsumerTest, ExpectFailUpdateTopicOnConsumer)
599 NSTopicLL * retTopic = NSConsumerGetTopicList(g_provider->providerId);
600 for (; retTopic; retTopic = retTopic->next)
602 retTopic->state = NS_TOPIC_SUBSCRIBED;
604 NSResult ret = NSConsumerUpdateTopicList(g_provider->providerId, retTopic);
606 EXPECT_EQ(NS_ERROR, ret);
609 TEST_F(NotificationConsumerTest, ExpectCallbackDeletedProvider)
611 NSProviderState type = NS_ALLOW;
612 mocks.OnCallFunc(NSProviderChangedCallback).Do(
613 [& type, this](NSProvider * , NSProviderState state)
615 std::cout << "Income Changed Callback : " << state << std::endl;
617 responseCon.notify_all();
620 g_providerSimul.deleteNotificationResource();
622 std::unique_lock< std::mutex > lock{ mutexForCondition };
623 responseCon.wait_for(lock, std::chrono::milliseconds(2000));
625 EXPECT_EQ(type, NS_STOPPED);