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(2000);
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::NSProvider * , OIC::Service::NSResponse )
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::ConsumerConfig cfg;
157 cfg.m_discoverCb = ProviderDiscoveredCallbackEmpty;
158 cfg.m_changedCb = ProviderChangedCallbackEmpty;
159 OIC::Service::NSConsumerService::getInstance()->Start(cfg);
162 TEST_F(NotificationServiceConsumerTest, StopConsumerPositive)
164 OIC::Service::NSConsumerService::getInstance()->Stop();
167 TEST_F(NotificationServiceConsumerTest, DiscoverProviderWithNonAccepterWhenStartedConsumerFirst)
169 mocks.ExpectCallFunc(ProviderDiscoveredCallbackEmpty).Do(
170 [this]( OIC::Service::NSProvider * provider)
172 std::cout << "Call Discovered" << std::endl;
173 g_provider = provider;
174 g_provider->subscribe();
175 g_provider->setListener((OIC::Service::NSProvider::MessageReceivedCallback)NotificationReceivedCallbackEmpty,
176 (OIC::Service::NSProvider::SyncInfoReceivedCallback)SyncCallbackEmpty);
177 responseCon.notify_all();
180 OIC::Service::NSConsumerService::ConsumerConfig cfg;
181 cfg.m_discoverCb = ProviderDiscoveredCallbackEmpty;
182 cfg.m_changedCb = ProviderChangedCallbackEmpty;
183 OIC::Service::NSConsumerService::getInstance()->Start(cfg);
185 g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_CONSUMER);
186 g_providerSimul.createNotificationResource();
188 std::unique_lock< std::mutex > lock{ mutexForCondition };
189 responseCon.wait_for(lock, g_waitForResponse);
191 OIC::Service::NSConsumerService::getInstance()->Stop();
192 g_providerSimul.deleteNotificationResource();
195 TEST_F(NotificationServiceConsumerTest, DiscoverProviderWithNonAccepterWhenStartedConsumerAfter)
197 g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_CONSUMER);
198 g_providerSimul.createNotificationResource();
200 std::unique_lock< std::mutex > lock{ mutexForCondition };
201 responseCon.wait_for(lock, g_waitForResponse);
204 mocks.ExpectCallFunc(ProviderDiscoveredCallbackEmpty).Do(
205 [this]( OIC::Service::NSProvider * provider)
207 std::cout << "Call Discovered" << std::endl;
208 g_provider->subscribe();
209 g_provider->setListener((OIC::Service::NSProvider::MessageReceivedCallback)NotificationReceivedCallbackEmpty,
210 (OIC::Service::NSProvider::SyncInfoReceivedCallback)SyncCallbackEmpty);
211 responseCon.notify_all();
214 OIC::Service::NSConsumerService::ConsumerConfig cfg;
215 cfg.m_discoverCb = ProviderDiscoveredCallbackEmpty;
216 cfg.m_changedCb = ProviderChangedCallbackEmpty;
217 OIC::Service::NSConsumerService::getInstance()->Start(cfg);
219 std::unique_lock< std::mutex > lock{ mutexForCondition };
220 responseCon.wait_for(lock, g_waitForResponse);
224 TEST_F(NotificationServiceConsumerTest, DiscoverProviderWithNonAccepterWhenRescan)
226 g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_CONSUMER);
227 mocks.ExpectCallFunc(ProviderDiscoveredCallbackEmpty).Do(
228 [this]( OIC::Service::NSProvider * provider)
230 std::cout << "Call Discovered" << std::endl;
231 g_provider = provider;
232 g_provider->subscribe();
233 g_provider->setListener((OIC::Service::NSProvider::MessageReceivedCallback)NotificationReceivedCallbackEmpty,
234 (OIC::Service::NSProvider::SyncInfoReceivedCallback)SyncCallbackEmpty);
235 std::cout << g_provider->getProviderId() << std::endl;
236 responseCon.notify_all();
239 OIC::Service::NSConsumerService::getInstance()->RescanProvider();
241 std::unique_lock< std::mutex > lock{ mutexForCondition };
242 responseCon.wait_for(lock, g_waitForResponse);
244 // OIC::Service::NSConsumerService::getInstance()->Stop();
247 TEST_F(NotificationServiceConsumerTest, ExpectSubscribeSuccess)
249 // mocks.ExpectCallFunc(ProviderChangedCallbackEmpty).Do(
250 // []( OIC::Service::NSProvider * , OIC::Service::NSResponse)
252 // std::cout << "Income Accepted subscription : " << std::endl;
255 g_provider->subscribe();
256 std::unique_lock< std::mutex > lock{ mutexForCondition };
257 responseCon.wait_for(lock, g_waitForResponse);
260 TEST_F(NotificationServiceConsumerTest, ExpectReceiveNotification)
263 std::string title = "title";
264 std::string msg = "msg";
266 mocks.ExpectCallFunc(NotificationReceivedCallbackEmpty).Do(
267 []( OIC::Service::NSMessage * message)
269 std::cout << "Income Notification : " << message->getMessageId() << std::endl;
272 g_providerSimul.notifyMessage(id, title, msg);
274 std::unique_lock< std::mutex > lock{ mutexForCondition };
275 responseCon.wait_for(lock, g_waitForResponse);
277 OIC::Service::NSConsumerService::getInstance()->Stop();
280 TEST_F(NotificationServiceConsumerTest, ExpectReceiveNotificationWithAccepterisProvider)
283 std::string title = "title";
284 std::string msg = "msg";
286 g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_PROVIDER);
288 OIC::Service::NSConsumerService::ConsumerConfig cfg;
289 cfg.m_discoverCb = ProviderDiscoveredCallbackEmpty;
290 cfg.m_changedCb = ProviderChangedCallbackEmpty;
291 OIC::Service::NSConsumerService::getInstance()->Start(cfg);
293 std::unique_lock< std::mutex > lock{ mutexForCondition };
294 responseCon.wait_for(lock, g_waitForResponse);
297 mocks.ExpectCallFunc(NotificationReceivedCallbackEmpty).Do(
298 []( OIC::Service::NSMessage * message)
300 std::cout << "Income Notification : " << message->getMessageId() << std::endl;
303 g_providerSimul.notifyMessage(id, title, msg);
305 std::unique_lock< std::mutex > lock{ mutexForCondition };
306 responseCon.wait_for(lock, g_waitForResponse);
308 // g_providerSimul.deleteNotificationResource();
309 // OIC::Service::NSConsumerService::getInstance()->Stop();
312 TEST_F(NotificationServiceConsumerTest, ExpectCallbackReadCheckWhenProviderNotifySync)
315 std::string title = "title";
316 std::string msg = "msg";
318 OIC::Service::NSSyncInfo::NSSyncType type = OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED;
320 mocks.OnCallFunc(NotificationReceivedCallbackEmpty).Do(
321 []( OIC::Service::NSMessage * message)
323 std::cout << "Income Notification : " << message->getMessageId() << std::endl;
326 mocks.ExpectCallFunc(SyncCallbackEmpty).Do(
327 [& type](OIC::Service::NSSyncInfo * sync)
329 std::cout << "Income SyncInfo : " << sync->getMessageId()
330 << ", State : " << (int) sync->getState() << std::endl;
331 type = sync->getState();
335 g_providerSimul.notifyMessage(id, title, msg);
337 std::unique_lock< std::mutex > lock{ mutexForCondition };
338 responseCon.wait_for(lock, g_waitForResponse);
341 g_providerSimul.sendRead(id);
343 std::unique_lock< std::mutex > lock{ mutexForCondition };
344 responseCon.wait_for(lock, g_waitForResponse);
347 // g_providerSimul.deleteNotificationResource();
348 // OIC::Service::NSConsumerService::getInstance()->Stop();
350 EXPECT_EQ(OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ, type);
353 TEST_F(NotificationServiceConsumerTest, ExpectCallbackDismissCheckWhenProviderNotifySync)
356 std::string title = "title";
357 std::string msg = "msg";
359 OIC::Service::NSSyncInfo::NSSyncType type = OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ;
361 mocks.OnCallFunc(NotificationReceivedCallbackEmpty).Do(
362 []( OIC::Service::NSMessage * message)
364 std::cout << "Income Notification : " << message->getMessageId() << std::endl;
367 mocks.ExpectCallFunc(SyncCallbackEmpty).Do(
368 [& type](OIC::Service::NSSyncInfo * sync)
370 std::cout << "Income Notification : " << sync->getMessageId()
371 << ", State : " << (int) sync->getState() << std::endl;
372 type = sync->getState();
376 g_providerSimul.notifyMessage(id, title, msg);
378 std::unique_lock< std::mutex > lock{ mutexForCondition };
379 responseCon.wait_for(lock, g_waitForResponse);
382 g_providerSimul.sendDismiss(id);
384 std::unique_lock< std::mutex > lock{ mutexForCondition };
385 responseCon.wait_for(lock, g_waitForResponse);
388 // g_providerSimul.deleteNotificationResource();
389 // OIC::Service::NSConsumerService::getInstance()->Stop();
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";
400 OIC::Service::NSSyncInfo::NSSyncType type = OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED;
402 mocks.OnCallFunc(NotificationReceivedCallbackEmpty).Do(
403 []( OIC::Service::NSMessage * message)
405 std::cout << "Income Notification : " << message->getMessageId() << std::endl;
406 g_provider->SendSyncInfo(message->getMessageId(), OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ);
407 std::unique_lock< std::mutex > lock{ mutexForCondition };
408 responseCon.wait_for(lock, g_waitForResponse);
411 mocks.ExpectCallFunc(SyncCallbackEmpty).Do(
412 [& type](OIC::Service::NSSyncInfo * sync)
414 std::cout << "Income Notification : " << sync->getMessageId()
415 << ", State : " << (int) sync->getState() << std::endl;
416 type = sync->getState();
420 g_providerSimul.notifyMessage(id, title, msg);
422 std::unique_lock< std::mutex > lock{ mutexForCondition };
423 responseCon.wait_for(lock, g_waitForResponse);
426 // g_providerSimul.deleteNotificationResource();
427 // OIC::Service::NSConsumerService::getInstance()->Stop();
429 EXPECT_EQ(OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ, type);
432 TEST_F(NotificationServiceConsumerTest, ExpectCallbackDismissCheckWhenConsumerPostSync)
435 std::string title = "title";
436 std::string msg = "msg";
438 OIC::Service::NSSyncInfo::NSSyncType type = OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ;
440 mocks.OnCallFunc(NotificationReceivedCallbackEmpty).Do(
441 []( OIC::Service::NSMessage * message)
443 std::cout << "Income Notification : " << message->getMessageId() << std::endl;
444 g_provider->SendSyncInfo(message->getMessageId(), OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED);
445 std::unique_lock< std::mutex > lock{ mutexForCondition };
446 responseCon.wait_for(lock, g_waitForResponse);
449 mocks.ExpectCallFunc(SyncCallbackEmpty).Do(
450 [& type](OIC::Service::NSSyncInfo * sync)
452 std::cout << "Income Notification : " << sync->getMessageId()
453 << ", State : " << (int) sync->getState() << std::endl;
454 type = sync->getState();
458 g_providerSimul.notifyMessage(id, title, msg);
460 std::unique_lock< std::mutex > lock{ mutexForCondition };
461 responseCon.wait_for(lock, g_waitForResponse);
464 EXPECT_EQ(OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED, type);
467 TEST_F(NotificationServiceConsumerTest, ExpectUnsubscribeSuccess)
469 g_provider->unSubscribe();
470 std::unique_lock< std::mutex > lock{ mutexForCondition };
471 responseCon.wait_for(lock, g_waitForResponse);
473 g_providerSimul.deleteNotificationResource();
474 OIC::Service::NSConsumerService::getInstance()->Stop();