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 "NSSyncInfo.h"
33 #include "NSMessage.h"
34 #include "NSMediaContents.h"
35 #include "NSConsumerService.h"
37 #include "NSProviderServiceSimulator.h"
41 NSProviderSimulator g_providerSimul;
42 std::shared_ptr<OIC::Service::NSProvider> g_provider;
44 std::atomic_bool g_isStartedStack(false);
46 std::chrono::milliseconds g_waitForResponse(1000);
48 std::condition_variable responseCon;
49 std::mutex mutexForCondition;
53 NS_SELECTION_CONSUMER = 0,
54 NS_SELECTION_PROVIDER = 1
59 class TestWithMock: public testing::Test
65 virtual ~TestWithMock() noexcept(noexcept(std::declval<Test>().~Test()))
70 virtual void TearDown()
84 class NotificationServiceConsumerTest : public TestWithMock
87 NotificationServiceConsumerTest() = default;
88 ~NotificationServiceConsumerTest() = default;
90 static void ProviderDiscoveredCallbackEmpty( std::shared_ptr<OIC::Service::NSProvider> )
92 std::cout << __func__ << std::endl;
95 static void NotificationReceivedCallbackEmpty( OIC::Service::NSMessage )
97 std::cout << __func__ << std::endl;
100 static void SyncCallbackEmpty(OIC::Service::NSSyncInfo)
102 std::cout << __func__ << std::endl;
105 static void FoundResourceEmpty(std::shared_ptr< OC::OCResource >)
107 std::cout << __func__ << std::endl;
110 static void ProviderChangedCallbackEmpty( OIC::Service::NSProviderState )
112 std::cout << __func__ << std::endl;
119 TestWithMock::SetUp();
121 if (g_isStartedStack == false)
123 OC::PlatformConfig cfg
125 OC::ServiceType::InProc,
129 OC::QualityOfService::LowQos
131 OC::OCPlatform::Configure(cfg);
135 OC::OCPlatform::stopPresence();
142 g_isStartedStack = true;
149 TestWithMock::TearDown();
154 TEST_F(NotificationServiceConsumerTest, StartConsumerPositive)
156 OIC::Service::NSConsumerService::getInstance()->start(ProviderDiscoveredCallbackEmpty);
159 TEST_F(NotificationServiceConsumerTest, StopConsumerPositive)
161 OIC::Service::NSConsumerService::getInstance()->stop();
164 TEST_F(NotificationServiceConsumerTest, DiscoverProviderWithNonAccepterWhenStartedConsumerFirst)
166 mocks.ExpectCallFunc(ProviderDiscoveredCallbackEmpty).Do(
167 [this]( std::shared_ptr<OIC::Service::NSProvider> provider)
169 std::cout << "Call Discovered" << std::endl;
170 std::cout << provider->getProviderId() << std::endl;
171 responseCon.notify_all();
174 OIC::Service::NSConsumerService::getInstance()->start(ProviderDiscoveredCallbackEmpty);
176 g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_CONSUMER);
177 g_providerSimul.createNotificationResource();
179 std::unique_lock< std::mutex > lock { mutexForCondition };
180 responseCon.wait_for(lock, g_waitForResponse);
182 OIC::Service::NSConsumerService::getInstance()->stop();
183 g_providerSimul.deleteNotificationResource();
186 TEST_F(NotificationServiceConsumerTest, DiscoverProviderWithNonAccepterWhenStartedConsumerAfter)
188 g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_CONSUMER);
189 g_providerSimul.createNotificationResource();
191 std::unique_lock< std::mutex > lock { mutexForCondition };
192 responseCon.wait_for(lock, g_waitForResponse);
195 mocks.ExpectCallFunc(ProviderDiscoveredCallbackEmpty).Do(
196 [this]( std::shared_ptr<OIC::Service::NSProvider> provider)
198 std::cout << "Call Discovered" << std::endl;
199 g_provider = provider;
200 std::cout << g_provider->getProviderId() << std::endl;
201 responseCon.notify_all();
204 OIC::Service::NSConsumerService::getInstance()->start(ProviderDiscoveredCallbackEmpty);
206 std::unique_lock< std::mutex > lock { mutexForCondition };
207 responseCon.wait_for(lock, g_waitForResponse);
209 ASSERT_NE(nullptr, g_provider) << "error: discovery failure";
213 TEST_F(NotificationServiceConsumerTest, DiscoverProviderWithNonAccepterWhenRescan)
215 g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_CONSUMER);
216 mocks.OnCallFunc(ProviderDiscoveredCallbackEmpty).Do(
217 [this]( std::shared_ptr<OIC::Service::NSProvider> provider)
219 std::cout << "Call Discovered" << std::endl;
220 g_provider = provider;
221 std::cout << g_provider->getProviderId() << std::endl;
222 responseCon.notify_all();
225 OIC::Service::NSConsumerService::getInstance()->rescanProvider();
227 std::unique_lock< std::mutex > lock { mutexForCondition };
228 responseCon.wait_for(lock, g_waitForResponse);
231 TEST_F(NotificationServiceConsumerTest, ExpectSubscribeSuccess)
233 OIC::Service::NSProviderState revState = OIC::Service::NSProviderState::DENY;
234 mocks.OnCallFunc(ProviderChangedCallbackEmpty).Do(
235 [this, & revState](OIC::Service::NSProviderState state)
237 std::cout << "Income Accepted subscription : " << std::endl;
239 responseCon.notify_all();
242 ASSERT_NE(nullptr, g_provider) << "error: discovery failure";
244 g_provider->setListener( (OIC::Service::NSProvider::ProviderStateCallback)
245 ProviderChangedCallbackEmpty,
246 (OIC::Service::NSProvider::MessageReceivedCallback)NotificationReceivedCallbackEmpty,
247 (OIC::Service::NSProvider::SyncInfoReceivedCallback)SyncCallbackEmpty);
248 if (!g_provider->isSubscribed())
250 g_provider->subscribe();
252 std::unique_lock< std::mutex > lock { mutexForCondition };
253 responseCon.wait_for(lock, g_waitForResponse);
254 EXPECT_EQ(OIC::Service::NSProviderState::ALLOW, revState);
257 TEST_F(NotificationServiceConsumerTest, ExpectReceiveNotification)
260 std::string title = "title";
261 std::string msg = "msg";
263 mocks.ExpectCallFunc(NotificationReceivedCallbackEmpty).Do(
264 [this]( OIC::Service::NSMessage message)
266 std::cout << "Income Notification : " << message.getMessageId() << std::endl;
267 responseCon.notify_all();
270 g_providerSimul.notifyMessage(id, title, msg);
272 std::unique_lock< std::mutex > lock { mutexForCondition };
273 responseCon.wait_for(lock, g_waitForResponse);
275 OIC::Service::NSConsumerService::getInstance()->stop();
278 TEST_F(NotificationServiceConsumerTest, DiscoverProviderWithAccepterisProvider)
280 g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_PROVIDER);
282 mocks.ExpectCallFunc(ProviderDiscoveredCallbackEmpty).Do(
283 [this]( std::shared_ptr<OIC::Service::NSProvider> provider)
285 std::cout << "Call Discovered" << std::endl;
286 g_provider = provider;
287 g_provider->setListener( (OIC::Service::NSProvider::ProviderStateCallback)
288 ProviderChangedCallbackEmpty,
289 (OIC::Service::NSProvider::MessageReceivedCallback)NotificationReceivedCallbackEmpty,
290 (OIC::Service::NSProvider::SyncInfoReceivedCallback)SyncCallbackEmpty);
291 if (!g_provider->isSubscribed())
293 g_provider->subscribe();
295 std::cout << g_provider->getProviderId() << std::endl;
296 responseCon.notify_all();
300 OIC::Service::NSConsumerService::getInstance()->start(ProviderDiscoveredCallbackEmpty);
301 std::unique_lock< std::mutex > lock { mutexForCondition };
302 responseCon.wait_for(lock, g_waitForResponse);
305 TEST_F(NotificationServiceConsumerTest, ExpectReceiveNotificationWithAccepterisProvider)
308 std::string title = "title";
309 std::string msg = "msg";
312 mocks.OnCallFunc(NotificationReceivedCallbackEmpty).Do(
313 [this, & id, & revId](OIC::Service::NSMessage message)
315 std::cout << "Income Notification : " << message.getMessageId() << std::endl;
316 revId = message.getMessageId();
317 responseCon.notify_all();
320 g_providerSimul.notifyMessage(id, title, msg);
322 std::unique_lock< std::mutex > lock { mutexForCondition };
323 responseCon.wait_for(lock, g_waitForResponse);
325 EXPECT_EQ(id, revId);
328 TEST_F(NotificationServiceConsumerTest, ExpectCallbackReadCheckWhenProviderNotifySync)
331 std::string title = "title";
332 std::string msg = "msg";
333 OIC::Service::NSSyncInfo::NSSyncType type = OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED;
335 mocks.OnCallFunc(NotificationReceivedCallbackEmpty).Do(
336 [this]( OIC::Service::NSMessage message)
338 std::cout << "Income Notification : " << message.getMessageId() << std::endl;
341 mocks.OnCallFunc(SyncCallbackEmpty).Do(
342 [& type, this](OIC::Service::NSSyncInfo sync)
344 std::cout << "Income SyncInfo : " << sync.getMessageId()
345 << ", State : " << (int) sync.getState() << std::endl;
346 type = sync.getState();
347 responseCon.notify_all();
350 g_providerSimul.notifyMessage(id, title, msg);
352 std::unique_lock< std::mutex > lock { mutexForCondition };
353 responseCon.wait_for(lock, g_waitForResponse);
356 g_providerSimul.sendRead(id);
358 std::unique_lock< std::mutex > lock { mutexForCondition };
359 responseCon.wait_for(lock, g_waitForResponse);
362 EXPECT_EQ(OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ, type);
365 TEST_F(NotificationServiceConsumerTest, ExpectCallbackDismissCheckWhenProviderNotifySync)
368 std::string title = "title";
369 std::string msg = "msg";
370 OIC::Service::NSSyncInfo::NSSyncType type = OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ;
372 mocks.OnCallFunc(NotificationReceivedCallbackEmpty).Do(
373 [this]( OIC::Service::NSMessage message)
375 std::cout << "Income Notification : " << message.getMessageId() << std::endl;
378 mocks.OnCallFunc(SyncCallbackEmpty).Do(
379 [& type, this](OIC::Service::NSSyncInfo sync)
381 std::cout << "Income Notification : " << sync.getMessageId()
382 << ", State : " << (int) sync.getState() << std::endl;
383 type = sync.getState();
384 responseCon.notify_all();
387 g_providerSimul.notifyMessage(id, title, msg);
389 std::unique_lock< std::mutex > lock { mutexForCondition };
390 responseCon.wait_for(lock, g_waitForResponse);
393 g_providerSimul.sendDismiss(id);
395 std::unique_lock< std::mutex > lock { mutexForCondition };
396 responseCon.wait_for(lock, g_waitForResponse);
399 EXPECT_EQ(OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED, type);
402 TEST_F(NotificationServiceConsumerTest, ExpectCallbackReadCheckWhenConsumerPostSync)
405 std::string title = "title";
406 std::string msg = "msg";
407 OIC::Service::NSSyncInfo::NSSyncType type = OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED;
409 ASSERT_NE(nullptr, g_provider) << "error: discovery failure";
411 mocks.OnCallFunc(NotificationReceivedCallbackEmpty).Do(
412 [this]( OIC::Service::NSMessage message)
414 std::cout << "Income Notification : " << message.getMessageId() << std::endl;
415 g_provider->sendSyncInfo(message.getMessageId(),
416 OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ);
417 std::unique_lock< std::mutex > lock { mutexForCondition };
418 responseCon.wait_for(lock, g_waitForResponse);
421 mocks.OnCallFunc(SyncCallbackEmpty).Do(
422 [& type, this](OIC::Service::NSSyncInfo sync)
424 std::cout << "Income Notification : " << sync.getMessageId()
425 << ", State : " << (int) sync.getState() << std::endl;
426 type = sync.getState();
427 responseCon.notify_all();
430 g_providerSimul.notifyMessage(id, title, msg);
432 std::unique_lock< std::mutex > lock { mutexForCondition };
433 responseCon.wait_for(lock, g_waitForResponse);
436 EXPECT_EQ(OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ, type);
439 TEST_F(NotificationServiceConsumerTest, ExpectCallbackDismissCheckWhenConsumerPostSync)
442 std::string title = "title";
443 std::string msg = "msg";
444 OIC::Service::NSSyncInfo::NSSyncType type = OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ;
446 ASSERT_NE(nullptr, g_provider) << "error: discovery failure";
448 mocks.OnCallFunc(NotificationReceivedCallbackEmpty).Do(
449 [this]( OIC::Service::NSMessage message)
451 std::cout << "Income Notification : " << message.getMessageId() << std::endl;
452 g_provider->sendSyncInfo(message.getMessageId(),
453 OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED);
454 std::unique_lock< std::mutex > lock { mutexForCondition };
455 responseCon.wait_for(lock, g_waitForResponse);
458 mocks.OnCallFunc(SyncCallbackEmpty).Do(
459 [& type, this](OIC::Service::NSSyncInfo sync)
461 std::cout << "Income Notification : " << sync.getMessageId()
462 << ", State : " << (int) sync.getState() << std::endl;
463 type = sync.getState();
464 responseCon.notify_all();
467 g_providerSimul.notifyMessage(id, title, msg);
469 std::unique_lock< std::mutex > lock { mutexForCondition };
470 responseCon.wait_for(lock, g_waitForResponse);
473 EXPECT_EQ(OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED, type);
476 TEST_F(NotificationServiceConsumerTest, ExpectGetProviderSuccessWithValidProviderId)
478 ASSERT_NE(nullptr, g_provider) << "error: discovery failure";
480 std::shared_ptr<OIC::Service::NSProvider> provider =
481 OIC::Service::NSConsumerService::getInstance()->getProvider(
482 g_provider->getProviderId());
483 int ret = strcmp(provider->getProviderId().c_str(), g_provider->getProviderId().c_str());
487 TEST_F(NotificationServiceConsumerTest, ExpectGetProviderSuccessWithInvalidProviderId)
489 std::shared_ptr<OIC::Service::NSProvider> provider =
490 OIC::Service::NSConsumerService::getInstance()->getProvider(
491 "123456789012345678901234567890123457");
492 bool res = (provider == nullptr);
496 TEST_F(NotificationServiceConsumerTest, ExpectCallbackTopicUpdated)
498 OIC::Service::NSProviderState revState = OIC::Service::NSProviderState::STOPPED;
499 mocks.OnCallFunc(ProviderChangedCallbackEmpty).Do(
500 [this, & revState](OIC::Service::NSProviderState state)
502 std::cout << "Income Changed Callback : " << (int)state << std::endl;
504 responseCon.notify_all();
507 NSProviderSimulator::NS_TopicList topics;
508 topics.push_back("1");
509 topics.push_back("2");
510 topics.push_back("3");
512 g_providerSimul.setTopics(topics);
514 std::unique_lock< std::mutex > lock{ mutexForCondition };
515 responseCon.wait_for(lock, g_waitForResponse);
517 EXPECT_EQ(OIC::Service::NSProviderState::TOPIC, revState);
520 TEST_F(NotificationServiceConsumerTest, ExpectEQTopicList)
524 NSProviderSimulator::NS_TopicList topics;
525 topics.push_back("1");
526 topics.push_back("2");
527 topics.push_back("3");
529 ASSERT_NE(nullptr, g_provider) << "error: discovery failure";
531 auto retTopic = g_provider->getTopicList()->getTopicsList();
532 auto it1 = retTopic.begin();
533 auto it2 = topics.begin();
534 while ( it1 != retTopic.end() || it2 != topics.end() )
536 if ((*it1).getTopicName() != *it2)
538 isSame = false; break;
544 EXPECT_EQ(true, isSame);
547 TEST_F(NotificationServiceConsumerTest, ExpectFailUpdateTopicOnConsumer)
549 ASSERT_NE(nullptr, g_provider) << "error: discovery failure";
551 auto retTopic = g_provider->getTopicList();
552 for (auto it : retTopic->getTopicsList())
554 std::cout << "Topic Name: " << it.getTopicName() << std::endl;
555 std::cout << "state : " << (int) it.getState() << std::endl;
556 it.setState(OIC::Service::NSTopic::NSTopicState::SUBSCRIBED);
558 OIC::Service::NSResult ret = g_provider->updateTopicList(retTopic);
559 std::cout << "ret : " << (int) ret << std::endl;
561 EXPECT_EQ(OIC::Service::NSResult::ERROR, ret);
565 TEST_F(NotificationServiceConsumerTest, ExpectCallbackDeletedProvider)
567 OIC::Service::NSProviderState type = OIC::Service::NSProviderState::ALLOW;
568 mocks.OnCallFunc(ProviderChangedCallbackEmpty).Do(
569 [& type, this](OIC::Service::NSProviderState state)
571 std::cout << "Income Changed Callback : " << std::endl;
575 g_providerSimul.deleteNotificationResource();
577 std::unique_lock< std::mutex > lock { mutexForCondition };
578 responseCon.wait_for(lock, g_waitForResponse);
580 EXPECT_EQ(type, OIC::Service::NSProviderState::STOPPED);
581 OIC::Service::NSConsumerService::getInstance()->stop();