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);
259 ASSERT_NE(nullptr, g_provider) << "error: discovery failure";
262 TEST_F(NotificationConsumerTest, DiscoverProviderWithNonAccepterWhenRescan)
264 g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_CONSUMER);
265 NSProviderState revState = NS_STOPPED;
266 mocks.OnCallFunc(NSProviderChangedCallback).Do(
267 [this, &revState](NSProvider * provider, NSProviderState state)
269 std::cout << "Call Discovered" << std::endl;
271 g_provider = provider;
272 std::cout << g_provider->providerId << std::endl;
273 responseCon.notify_all();
278 std::unique_lock< std::mutex > lock{ mutexForCondition };
279 responseCon.wait_for(lock, g_waitForResponse);
281 EXPECT_EQ(NS_DISCOVERED, revState);
284 TEST_F(NotificationConsumerTest, ExpectSubscribeSuccess)
286 NSProviderState revState = NS_DENY;
288 ASSERT_NE(nullptr, g_provider) << "error: discovery failure";
290 mocks.OnCallFunc(NSProviderChangedCallback).Do(
291 [this, & revState](NSProvider * , NSProviderState state)
293 std::cout << "Income Changed Callback : " << state << std::endl;
295 responseCon.notify_all();
298 NSResult ret = NSSubscribe(g_provider->providerId);
299 std::unique_lock< std::mutex > lock{ mutexForCondition };
300 responseCon.wait_for(lock, g_waitForResponse);
302 EXPECT_EQ(NS_ALLOW, revState);
303 EXPECT_EQ(NS_OK, ret);
306 TEST_F(NotificationConsumerTest, ExpectReceiveNotification)
309 std::string title = "title";
310 std::string msg = "msg";
313 mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
314 [this, & revId](NSMessage * message)
316 std::cout << "Income Notification : " << message->messageId << std::endl;
317 revId = message->messageId;
318 responseCon.notify_all();
321 g_providerSimul.notifyMessage(id, title, msg);
323 std::unique_lock< std::mutex > lock{ mutexForCondition };
324 responseCon.wait_for(lock, g_waitForResponse);
326 EXPECT_EQ(id, revId);
328 g_providerSimul.deleteNotificationResource();
331 TEST_F(NotificationConsumerTest, ExpectReceiveSubAllowWithAccepterisProvider)
333 g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_PROVIDER);
334 NSProviderState revState = NS_DENY;
335 g_providerSimul.createNotificationResource();
337 std::unique_lock< std::mutex > lock{ mutexForCondition };
338 responseCon.wait_for(lock, g_waitForResponse);
341 mocks.OnCallFunc(NSProviderChangedCallback).Do(
342 [this, & revState](NSProvider * provider, NSProviderState state)
344 std::cout << "Income Changed Callback : " << state << std::endl;
346 g_provider = provider;
347 responseCon.notify_all();
349 mocks.OnCallFunc(NSProviderChangedCallback).Do(
350 [this, & revState](NSProvider *, NSProviderState state)
352 std::cout << "Income Changed Callback : " << state << std::endl;
354 responseCon.notify_all();
357 NSStartConsumer(cfg);
358 std::unique_lock< std::mutex > lock{ mutexForCondition };
359 responseCon.wait_for(lock, g_waitForResponse);
361 EXPECT_EQ(NS_ALLOW, revState);
364 TEST_F(NotificationConsumerTest, ExpectReceiveNotificationWithAccepterisProvider)
367 std::string title = "title";
368 std::string msg = "msg";
371 mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
372 [this, & id, & revId](NSMessage * message)
374 std::cout << "Income Notification : " << message->messageId << std::endl;
375 revId = message->messageId;
376 responseCon.notify_all();
379 g_providerSimul.notifyMessage(id, title, msg);
381 std::unique_lock< std::mutex > lock{ mutexForCondition };
382 responseCon.wait_for(lock, g_waitForResponse);
384 EXPECT_EQ(id, revId);
387 TEST_F(NotificationConsumerTest, ExpectCallbackReadCheckWhenProviderNotifySync)
390 std::string title = "title";
391 std::string msg = "msg";
392 NSSyncType type = NS_SYNC_DELETED;
394 mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
395 [this](NSMessage * message)
397 std::cout << "Income Notification : " << message->messageId << std::endl;
398 responseCon.notify_all();
401 mocks.OnCallFunc(NSSyncCallbackEmpty).Do(
402 [& type, this](NSSyncInfo * sync)
404 std::cout << "Income SyncInfo : " << sync->messageId
405 << ", State : " << sync->state << std::endl;
407 responseCon.notify_all();
410 g_providerSimul.notifyMessage(id, title, msg);
412 std::unique_lock< std::mutex > lock{ mutexForCondition };
413 responseCon.wait_for(lock, g_waitForResponse);
416 g_providerSimul.sendRead(id);
418 std::unique_lock< std::mutex > lock{ mutexForCondition };
419 responseCon.wait_for(lock, g_waitForResponse);
422 EXPECT_EQ(NS_SYNC_READ, type);
425 TEST_F(NotificationConsumerTest, ExpectCallbackDismissCheckWhenProviderNotifySync)
428 std::string title = "title";
429 std::string msg = "msg";
430 NSSyncType type = NS_SYNC_READ;
432 mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
433 [this](NSMessage * message)
435 std::cout << "Income Notification : " << message->messageId << std::endl;
436 responseCon.notify_all();
439 mocks.OnCallFunc(NSSyncCallbackEmpty).Do(
440 [& type, this](NSSyncInfo * sync)
442 std::cout << "Income Notification : " << sync->messageId
443 << ", State : " << sync->state << std::endl;
445 responseCon.notify_all();
448 g_providerSimul.notifyMessage(id, title, msg);
450 std::unique_lock< std::mutex > lock{ mutexForCondition };
451 responseCon.wait_for(lock, g_waitForResponse);
454 g_providerSimul.sendDismiss(id);
456 std::unique_lock< std::mutex > lock{ mutexForCondition };
457 responseCon.wait_for(lock, g_waitForResponse);
460 EXPECT_EQ(NS_SYNC_DELETED, type);
463 TEST_F(NotificationConsumerTest, ExpectCallbackReadCheckWhenConsumerPostSync)
466 std::string title = "title";
467 std::string msg = "msg";
468 NSSyncType type = NS_SYNC_DELETED;
470 mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
471 [this](NSMessage * message)
473 std::cout << "Income Notification : " << message->messageId << std::endl;
474 NSConsumerSendSyncInfo(message->providerId, message->messageId, NS_SYNC_READ);
475 std::unique_lock< std::mutex > lock{ mutexForCondition };
476 responseCon.wait_for(lock, g_waitForResponse);
479 mocks.OnCallFunc(NSSyncCallbackEmpty).Do(
480 [& type, this](NSSyncInfo * sync)
482 std::cout << "Income Notification : " << sync->messageId
483 << ", State : " << sync->state << std::endl;
485 responseCon.notify_all();
488 g_providerSimul.notifyMessage(id, title, msg);
490 std::unique_lock< std::mutex > lock{ mutexForCondition };
491 responseCon.wait_for(lock, g_waitForResponse);
494 EXPECT_EQ(NS_SYNC_READ, type);
497 TEST_F(NotificationConsumerTest, ExpectCallbackDismissCheckWhenConsumerPostSync)
500 std::string title = "title";
501 std::string msg = "msg";
502 NSSyncType state = NS_SYNC_READ;
504 mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
505 [this](NSMessage * message)
507 std::cout << "Income Notification : " << message->messageId << std::endl;
508 NSConsumerSendSyncInfo(message->providerId, message->messageId, NS_SYNC_DELETED);
509 std::unique_lock< std::mutex > lock{ mutexForCondition };
510 responseCon.wait_for(lock, g_waitForResponse);
513 mocks.OnCallFunc(NSSyncCallbackEmpty).Do(
514 [& state, this](NSSyncInfo * sync)
516 std::cout << "Income Notification : " << sync->messageId
517 << ", State : " << sync->state << std::endl;
519 responseCon.notify_all();
522 g_providerSimul.notifyMessage(id, title, msg);
524 std::unique_lock< std::mutex > lock{ mutexForCondition };
525 responseCon.wait_for(lock, g_waitForResponse);
528 EXPECT_EQ(NS_SYNC_DELETED, state);
531 TEST_F(NotificationConsumerTest, ExpectGetProviderSuccessWithValidProviderId)
533 ASSERT_NE(nullptr, g_provider) << "error: discovery failure";
535 NSProvider * provider = NSConsumerGetProvider(g_provider->providerId);
536 int ret = strcmp(provider->providerId, g_provider->providerId);
541 TEST_F(NotificationConsumerTest, ExpectGetProviderSuccessWithInvalidProviderId)
543 NSProvider * provider = NSConsumerGetProvider("123456789012345678901234567890123457");
544 EXPECT_EQ(provider, (void*)NULL);
547 TEST_F(NotificationConsumerTest, ExpectGetProviderSuccessWithNULL)
549 NSProvider * provider = NSConsumerGetProvider(NULL);
550 EXPECT_EQ(provider, (void*)NULL);
553 TEST_F(NotificationConsumerTest, ExpectGetTopicListIsNULL)
555 ASSERT_NE(nullptr, g_provider) << "error: discovery failure";
557 NSTopicLL * currentTopics = NSConsumerGetTopicList(g_provider->providerId);
558 EXPECT_EQ(NULL, currentTopics);
561 TEST_F(NotificationConsumerTest, ExpectCallbackTopicUpdated)
563 NSProviderState revState = NS_STOPPED;
564 mocks.OnCallFunc(NSProviderChangedCallback).Do(
565 [this, & revState](NSProvider * , NSProviderState state)
567 std::cout << "Income Changed Callback : " << state << std::endl;
569 responseCon.notify_all();
572 NSProviderSimulator::NS_TopicList topics;
573 topics.push_back("1");
574 topics.push_back("2");
575 topics.push_back("3");
577 g_providerSimul.setTopics(topics);
579 std::unique_lock< std::mutex > lock{ mutexForCondition };
580 responseCon.wait_for(lock, g_waitForResponse);
582 EXPECT_EQ(NS_TOPIC, revState);
585 TEST_F(NotificationConsumerTest, ExpectEQTopicList)
589 NSProviderSimulator::NS_TopicList topics;
590 topics.push_back("1");
591 topics.push_back("2");
592 topics.push_back("3");
594 ASSERT_NE(nullptr, g_provider) << "error: discovery failure";
596 NSTopicLL * retTopic = NSConsumerGetTopicList(g_provider->providerId);
597 std::for_each (topics.begin(), topics.end(),
598 [this, & retTopic, & isSame](const std::string & str)
600 isSame = (str == std::string(retTopic->topicName));
601 retTopic = retTopic->next;
604 EXPECT_EQ(true, isSame);
607 TEST_F(NotificationConsumerTest, ExpectFailUpdateTopicOnConsumer)
609 ASSERT_NE(nullptr, g_provider) << "error: discovery failure";
611 NSTopicLL * retTopic = NSConsumerGetTopicList(g_provider->providerId);
612 for (; retTopic; retTopic = retTopic->next)
614 retTopic->state = NS_TOPIC_SUBSCRIBED;
616 NSResult ret = NSConsumerUpdateTopicList(g_provider->providerId, retTopic);
618 EXPECT_EQ(NS_ERROR, ret);
621 TEST_F(NotificationConsumerTest, ExpectCallbackDeletedProvider)
623 NSProviderState type = NS_ALLOW;
624 mocks.OnCallFunc(NSProviderChangedCallback).Do(
625 [& type, this](NSProvider * , NSProviderState state)
627 std::cout << "Income Changed Callback : " << state << std::endl;
629 responseCon.notify_all();
632 g_providerSimul.deleteNotificationResource();
634 std::unique_lock< std::mutex > lock{ mutexForCondition };
635 responseCon.wait_for(lock, std::chrono::milliseconds(2000));
637 EXPECT_EQ(type, NS_STOPPED);