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 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( 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]( 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]( 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);
211 TEST_F(NotificationServiceConsumerTest, DiscoverProviderWithNonAccepterWhenRescan)
213 g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_CONSUMER);
214 mocks.OnCallFunc(ProviderDiscoveredCallbackEmpty).Do(
215 [this]( OIC::Service::NSProvider * provider)
217 std::cout << "Call Discovered" << std::endl;
218 g_provider = provider;
219 std::cout << g_provider->getProviderId() << std::endl;
220 responseCon.notify_all();
223 OIC::Service::NSConsumerService::getInstance()->rescanProvider();
225 std::unique_lock< std::mutex > lock { mutexForCondition };
226 responseCon.wait_for(lock, g_waitForResponse);
229 TEST_F(NotificationServiceConsumerTest, ExpectSubscribeSuccess)
231 OIC::Service::NSProviderState revState = OIC::Service::NSProviderState::DENY;
232 mocks.OnCallFunc(ProviderChangedCallbackEmpty).Do(
233 [this, & revState](OIC::Service::NSProviderState state)
235 std::cout << "Income Accepted subscription : " << std::endl;
237 responseCon.notify_all();
240 g_provider->setListener( (OIC::Service::NSProvider::ProviderStateCallback)
241 ProviderChangedCallbackEmpty,
242 (OIC::Service::NSProvider::MessageReceivedCallback)NotificationReceivedCallbackEmpty,
243 (OIC::Service::NSProvider::SyncInfoReceivedCallback)SyncCallbackEmpty);
244 if (!g_provider->isSubscribed())
245 g_provider->subscribe();
246 std::unique_lock< std::mutex > lock { mutexForCondition };
247 responseCon.wait_for(lock, g_waitForResponse);
248 EXPECT_EQ(OIC::Service::NSProviderState::ALLOW, revState);
251 TEST_F(NotificationServiceConsumerTest, ExpectReceiveNotification)
254 std::string title = "title";
255 std::string msg = "msg";
257 mocks.ExpectCallFunc(NotificationReceivedCallbackEmpty).Do(
258 [this]( OIC::Service::NSMessage * message)
260 std::cout << "Income Notification : " << message->getMessageId() << std::endl;
261 responseCon.notify_all();
264 g_providerSimul.notifyMessage(id, title, msg);
266 std::unique_lock< std::mutex > lock { mutexForCondition };
267 responseCon.wait_for(lock, g_waitForResponse);
269 OIC::Service::NSConsumerService::getInstance()->stop();
272 TEST_F(NotificationServiceConsumerTest, DiscoverProviderWithAccepterisProvider)
274 g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_PROVIDER);
276 mocks.ExpectCallFunc(ProviderDiscoveredCallbackEmpty).Do(
277 [this]( OIC::Service::NSProvider * provider)
279 std::cout << "Call Discovered" << std::endl;
280 g_provider = provider;
281 g_provider->setListener( (OIC::Service::NSProvider::ProviderStateCallback)
282 ProviderChangedCallbackEmpty,
283 (OIC::Service::NSProvider::MessageReceivedCallback)NotificationReceivedCallbackEmpty,
284 (OIC::Service::NSProvider::SyncInfoReceivedCallback)SyncCallbackEmpty);
285 if (!g_provider->isSubscribed())
286 g_provider->subscribe();
287 std::cout << g_provider->getProviderId() << std::endl;
288 responseCon.notify_all();
292 OIC::Service::NSConsumerService::getInstance()->start(ProviderDiscoveredCallbackEmpty);
293 std::unique_lock< std::mutex > lock { mutexForCondition };
294 responseCon.wait_for(lock, g_waitForResponse);
297 TEST_F(NotificationServiceConsumerTest, ExpectReceiveNotificationWithAccepterisProvider)
300 std::string title = "title";
301 std::string msg = "msg";
304 mocks.OnCallFunc(NotificationReceivedCallbackEmpty).Do(
305 [this, & id, & revId](OIC::Service::NSMessage * message)
307 std::cout << "Income Notification : " << message->getMessageId() << std::endl;
308 revId = message->getMessageId();
309 responseCon.notify_all();
312 g_providerSimul.notifyMessage(id, title, msg);
314 std::unique_lock< std::mutex > lock { mutexForCondition };
315 responseCon.wait_for(lock, g_waitForResponse);
317 EXPECT_EQ(id, revId);
320 TEST_F(NotificationServiceConsumerTest, ExpectCallbackReadCheckWhenProviderNotifySync)
323 std::string title = "title";
324 std::string msg = "msg";
325 OIC::Service::NSSyncInfo::NSSyncType type = OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED;
327 mocks.OnCallFunc(NotificationReceivedCallbackEmpty).Do(
328 [this]( OIC::Service::NSMessage * message)
330 std::cout << "Income Notification : " << message->getMessageId() << std::endl;
333 mocks.OnCallFunc(SyncCallbackEmpty).Do(
334 [& type, this](OIC::Service::NSSyncInfo * sync)
336 std::cout << "Income SyncInfo : " << sync->getMessageId()
337 << ", State : " << (int) sync->getState() << std::endl;
338 type = sync->getState();
339 responseCon.notify_all();
342 g_providerSimul.notifyMessage(id, title, msg);
344 std::unique_lock< std::mutex > lock { mutexForCondition };
345 responseCon.wait_for(lock, g_waitForResponse);
348 g_providerSimul.sendRead(id);
350 std::unique_lock< std::mutex > lock { mutexForCondition };
351 responseCon.wait_for(lock, g_waitForResponse);
354 EXPECT_EQ(OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ, type);
357 TEST_F(NotificationServiceConsumerTest, ExpectCallbackDismissCheckWhenProviderNotifySync)
360 std::string title = "title";
361 std::string msg = "msg";
362 OIC::Service::NSSyncInfo::NSSyncType type = OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ;
364 mocks.OnCallFunc(NotificationReceivedCallbackEmpty).Do(
365 [this]( OIC::Service::NSMessage * message)
367 std::cout << "Income Notification : " << message->getMessageId() << std::endl;
370 mocks.OnCallFunc(SyncCallbackEmpty).Do(
371 [& type, this](OIC::Service::NSSyncInfo * sync)
373 std::cout << "Income Notification : " << sync->getMessageId()
374 << ", State : " << (int) sync->getState() << std::endl;
375 type = sync->getState();
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);
385 g_providerSimul.sendDismiss(id);
387 std::unique_lock< std::mutex > lock { mutexForCondition };
388 responseCon.wait_for(lock, g_waitForResponse);
391 EXPECT_EQ(OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED, type);
394 TEST_F(NotificationServiceConsumerTest, ExpectCallbackReadCheckWhenConsumerPostSync)
397 std::string title = "title";
398 std::string msg = "msg";
399 OIC::Service::NSSyncInfo::NSSyncType type = OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED;
401 mocks.OnCallFunc(NotificationReceivedCallbackEmpty).Do(
402 [this]( OIC::Service::NSMessage * message)
404 std::cout << "Income Notification : " << message->getMessageId() << std::endl;
405 g_provider->sendSyncInfo(message->getMessageId(),
406 OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ);
407 std::unique_lock< std::mutex > lock { mutexForCondition };
408 responseCon.wait_for(lock, g_waitForResponse);
411 mocks.OnCallFunc(SyncCallbackEmpty).Do(
412 [& type, this](OIC::Service::NSSyncInfo * sync)
414 std::cout << "Income Notification : " << sync->getMessageId()
415 << ", State : " << (int) sync->getState() << std::endl;
416 type = sync->getState();
417 responseCon.notify_all();
420 g_providerSimul.notifyMessage(id, title, msg);
422 std::unique_lock< std::mutex > lock { mutexForCondition };
423 responseCon.wait_for(lock, g_waitForResponse);
426 EXPECT_EQ(OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ, type);
429 TEST_F(NotificationServiceConsumerTest, ExpectCallbackDismissCheckWhenConsumerPostSync)
432 std::string title = "title";
433 std::string msg = "msg";
434 OIC::Service::NSSyncInfo::NSSyncType type = OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ;
436 mocks.OnCallFunc(NotificationReceivedCallbackEmpty).Do(
437 [this]( OIC::Service::NSMessage * message)
439 std::cout << "Income Notification : " << message->getMessageId() << std::endl;
440 g_provider->sendSyncInfo(message->getMessageId(),
441 OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED);
442 std::unique_lock< std::mutex > lock { mutexForCondition };
443 responseCon.wait_for(lock, g_waitForResponse);
446 mocks.OnCallFunc(SyncCallbackEmpty).Do(
447 [& type, this](OIC::Service::NSSyncInfo * sync)
449 std::cout << "Income Notification : " << sync->getMessageId()
450 << ", State : " << (int) sync->getState() << std::endl;
451 type = sync->getState();
452 responseCon.notify_all();
455 g_providerSimul.notifyMessage(id, title, msg);
457 std::unique_lock< std::mutex > lock { mutexForCondition };
458 responseCon.wait_for(lock, g_waitForResponse);
461 EXPECT_EQ(OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED, type);
464 TEST_F(NotificationServiceConsumerTest, ExpectCallbackDeletedProvider)
466 OIC::Service::NSProviderState type = OIC::Service::NSProviderState::ALLOW;
467 mocks.OnCallFunc(ProviderChangedCallbackEmpty).Do(
468 [& type, this](OIC::Service::NSProviderState state)
470 std::cout << "Income Changed Callback : " << std::endl;
472 responseCon.notify_all();
475 g_providerSimul.deleteNotificationResource();
477 std::unique_lock< std::mutex > lock { mutexForCondition };
478 responseCon.wait_for(lock, g_waitForResponse);
480 EXPECT_EQ(type, OIC::Service::NSProviderState::STOPPED);
481 OIC::Service::NSConsumerService::getInstance()->stop();
484 //TO add when unsubscibe API is added back
485 //TEST_F(NotificationServiceConsumerTest, ExpectUnsubscribeSuccess)
487 // g_provider->unsubscribe();
488 // std::unique_lock< std::mutex > lock{ mutexForCondition };
489 // responseCon.wait_for(lock, g_waitForResponse);
491 // g_providerSimul.deleteNotificationResource();
492 // OIC::Service::NSConsumerService::getInstance()->stop();