Merge "Merge remote-tracking branch 'origin/notification-service' Updated with static...
[platform/upstream/iotivity.git] / service / notification / cpp-wrapper / unittest / NSConsumerServiceTest.cpp
1 //******************************************************************
2 //
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include <gtest/gtest.h>
22 #include <HippoMocks/hippomocks.h>
23 #include <atomic>
24 #include <functional>
25 #include <condition_variable>
26 #include <mutex>
27 #include <chrono>
28
29 #include "ocstack.h"
30
31 #include "NSUtils.h"
32 #include "NSSyncInfo.h"
33 #include "NSMessage.h"
34 #include "NSMediaContents.h"
35 #include "NSConsumerService.h"
36
37 #include "NSProviderServiceSimulator.h"
38
39 namespace
40 {
41     NSProviderSimulator g_providerSimul;
42     OIC::Service::NSProvider *g_provider;
43
44     std::atomic_bool g_isStartedStack(false);
45
46     std::chrono::milliseconds g_waitForResponse(1000);
47
48     std::condition_variable responseCon;
49     std::mutex mutexForCondition;
50
51     enum class NSSelector
52     {
53         NS_SELECTION_CONSUMER = 0,
54         NS_SELECTION_PROVIDER = 1
55     };
56
57 }
58
59 class TestWithMock: public testing::Test
60 {
61     public:
62         MockRepository mocks;
63
64     protected:
65         virtual ~TestWithMock() noexcept(noexcept(std::declval<Test>().~Test()))
66         {
67
68         }
69
70         virtual void TearDown()
71         {
72             try
73             {
74                 mocks.VerifyAll();
75             }
76             catch (...)
77             {
78                 mocks.reset();
79                 throw;
80             }
81         }
82 };
83
84 class NotificationServiceConsumerTest : public TestWithMock
85 {
86     public:
87         NotificationServiceConsumerTest() = default;
88         ~NotificationServiceConsumerTest() = default;
89
90         static void ProviderDiscoveredCallbackEmpty( OIC::Service::NSProvider *)
91         {
92             std::cout << __func__ << std::endl;
93         }
94
95         static void NotificationReceivedCallbackEmpty( OIC::Service::NSMessage *)
96         {
97             std::cout << __func__ << std::endl;
98         }
99
100         static void SyncCallbackEmpty(OIC::Service::NSSyncInfo *)
101         {
102             std::cout << __func__ << std::endl;
103         }
104
105         static void FoundResourceEmpty(std::shared_ptr< OC::OCResource >)
106         {
107             std::cout << __func__ << std::endl;
108         }
109
110         static void ProviderChangedCallbackEmpty( OIC::Service::NSProviderState )
111         {
112             std::cout << __func__ << std::endl;
113         }
114
115     protected:
116
117         void SetUp()
118         {
119             TestWithMock::SetUp();
120
121             if (g_isStartedStack == false)
122             {
123                 OC::PlatformConfig cfg
124                 {
125                     OC::ServiceType::InProc,
126                     OC::ModeType::Both,
127                     "0.0.0.0",
128                     0,
129                     OC::QualityOfService::LowQos
130                 };
131                 OC::OCPlatform::Configure(cfg);
132
133                 try
134                 {
135                     OC::OCPlatform::stopPresence();
136                 }
137                 catch (...)
138                 {
139
140                 }
141
142                 g_isStartedStack = true;
143             }
144
145         }
146
147         void TearDown()
148         {
149             TestWithMock::TearDown();
150         }
151
152 };
153
154 TEST_F(NotificationServiceConsumerTest, StartConsumerPositive)
155 {
156     OIC::Service::NSConsumerService::getInstance()->start(ProviderDiscoveredCallbackEmpty);
157 }
158
159 TEST_F(NotificationServiceConsumerTest, StopConsumerPositive)
160 {
161     OIC::Service::NSConsumerService::getInstance()->stop();
162 }
163
164 TEST_F(NotificationServiceConsumerTest, DiscoverProviderWithNonAccepterWhenStartedConsumerFirst)
165 {
166     mocks.ExpectCallFunc(ProviderDiscoveredCallbackEmpty).Do(
167         [this]( OIC::Service::NSProvider * provider)
168     {
169         std::cout << "Call Discovered" << std::endl;
170         std::cout << provider->getProviderId() << std::endl;
171         responseCon.notify_all();
172     });
173
174     OIC::Service::NSConsumerService::getInstance()->start(ProviderDiscoveredCallbackEmpty);
175
176     g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_CONSUMER);
177     g_providerSimul.createNotificationResource();
178
179     std::unique_lock< std::mutex > lock { mutexForCondition };
180     responseCon.wait_for(lock, g_waitForResponse);
181
182     OIC::Service::NSConsumerService::getInstance()->stop();
183     g_providerSimul.deleteNotificationResource();
184 }
185
186 TEST_F(NotificationServiceConsumerTest, DiscoverProviderWithNonAccepterWhenStartedConsumerAfter)
187 {
188     g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_CONSUMER);
189     g_providerSimul.createNotificationResource();
190     {
191         std::unique_lock< std::mutex > lock { mutexForCondition };
192         responseCon.wait_for(lock, g_waitForResponse);
193     }
194
195     mocks.ExpectCallFunc(ProviderDiscoveredCallbackEmpty).Do(
196         [this]( OIC::Service::NSProvider * provider)
197     {
198         std::cout << "Call Discovered" << std::endl;
199         g_provider = provider;
200         std::cout << g_provider->getProviderId() << std::endl;
201         responseCon.notify_all();
202     });
203
204     OIC::Service::NSConsumerService::getInstance()->start(ProviderDiscoveredCallbackEmpty);
205
206     std::unique_lock< std::mutex > lock { mutexForCondition };
207     responseCon.wait_for(lock, g_waitForResponse);
208
209 }
210
211 TEST_F(NotificationServiceConsumerTest, DiscoverProviderWithNonAccepterWhenRescan)
212 {
213     g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_CONSUMER);
214     mocks.OnCallFunc(ProviderDiscoveredCallbackEmpty).Do(
215         [this]( OIC::Service::NSProvider * provider)
216     {
217         std::cout << "Call Discovered" << std::endl;
218         g_provider = provider;
219         std::cout << g_provider->getProviderId() << std::endl;
220         responseCon.notify_all();
221     });
222
223     OIC::Service::NSConsumerService::getInstance()->rescanProvider();
224
225     std::unique_lock< std::mutex > lock { mutexForCondition };
226     responseCon.wait_for(lock, g_waitForResponse);
227 }
228
229 TEST_F(NotificationServiceConsumerTest, ExpectSubscribeSuccess)
230 {
231     OIC::Service::NSProviderState revState = OIC::Service::NSProviderState::DENY;
232     mocks.OnCallFunc(ProviderChangedCallbackEmpty).Do(
233         [this, & revState](OIC::Service::NSProviderState state)
234     {
235         std::cout << "Income Accepted subscription : " << std::endl;
236         revState = state;
237         responseCon.notify_all();
238     });
239
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);
249 }
250
251 TEST_F(NotificationServiceConsumerTest, ExpectReceiveNotification)
252 {
253     uint64_t id = 10;
254     std::string title = "title";
255     std::string msg = "msg";
256
257     mocks.ExpectCallFunc(NotificationReceivedCallbackEmpty).Do(
258         [this]( OIC::Service::NSMessage * message)
259     {
260         std::cout << "Income Notification : " << message->getMessageId() << std::endl;
261         responseCon.notify_all();
262     });
263
264     g_providerSimul.notifyMessage(id, title, msg);
265
266     std::unique_lock< std::mutex > lock { mutexForCondition };
267     responseCon.wait_for(lock, g_waitForResponse);
268
269     OIC::Service::NSConsumerService::getInstance()->stop();
270 }
271
272 TEST_F(NotificationServiceConsumerTest, DiscoverProviderWithAccepterisProvider)
273 {
274     g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_PROVIDER);
275
276     mocks.ExpectCallFunc(ProviderDiscoveredCallbackEmpty).Do(
277         [this]( OIC::Service::NSProvider * provider)
278     {
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();
289     });
290
291
292     OIC::Service::NSConsumerService::getInstance()->start(ProviderDiscoveredCallbackEmpty);
293     std::unique_lock< std::mutex > lock { mutexForCondition };
294     responseCon.wait_for(lock, g_waitForResponse);
295 }
296
297 TEST_F(NotificationServiceConsumerTest, ExpectReceiveNotificationWithAccepterisProvider)
298 {
299     uint64_t id = 11;
300     std::string title = "title";
301     std::string msg = "msg";
302     uint64_t revId = 1;
303
304     mocks.OnCallFunc(NotificationReceivedCallbackEmpty).Do(
305         [this, & id, & revId](OIC::Service::NSMessage * message)
306     {
307         std::cout << "Income Notification : " << message->getMessageId() << std::endl;
308         revId =  message->getMessageId();
309         responseCon.notify_all();
310     });
311
312     g_providerSimul.notifyMessage(id, title, msg);
313
314     std::unique_lock< std::mutex > lock { mutexForCondition };
315     responseCon.wait_for(lock, g_waitForResponse);
316
317     EXPECT_EQ(id, revId);
318 }
319
320 TEST_F(NotificationServiceConsumerTest, ExpectCallbackReadCheckWhenProviderNotifySync)
321 {
322     uint64_t id = 12;
323     std::string title = "title";
324     std::string msg = "msg";
325     OIC::Service::NSSyncInfo::NSSyncType type = OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED;
326
327     mocks.OnCallFunc(NotificationReceivedCallbackEmpty).Do(
328         [this]( OIC::Service::NSMessage * message)
329     {
330         std::cout << "Income Notification : " << message->getMessageId() << std::endl;
331     });
332
333     mocks.OnCallFunc(SyncCallbackEmpty).Do(
334         [& type, this](OIC::Service::NSSyncInfo * sync)
335     {
336         std::cout << "Income SyncInfo : " << sync->getMessageId()
337                   << ", State : " << (int) sync->getState() << std::endl;
338         type = sync->getState();
339         responseCon.notify_all();
340     });
341
342     g_providerSimul.notifyMessage(id, title, msg);
343     {
344         std::unique_lock< std::mutex > lock { mutexForCondition };
345         responseCon.wait_for(lock, g_waitForResponse);
346     }
347
348     g_providerSimul.sendRead(id);
349     {
350         std::unique_lock< std::mutex > lock { mutexForCondition };
351         responseCon.wait_for(lock, g_waitForResponse);
352     }
353
354     EXPECT_EQ(OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ, type);
355 }
356
357 TEST_F(NotificationServiceConsumerTest, ExpectCallbackDismissCheckWhenProviderNotifySync)
358 {
359     uint64_t id = 13;
360     std::string title = "title";
361     std::string msg = "msg";
362     OIC::Service::NSSyncInfo::NSSyncType type = OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ;
363
364     mocks.OnCallFunc(NotificationReceivedCallbackEmpty).Do(
365         [this]( OIC::Service::NSMessage * message)
366     {
367         std::cout << "Income Notification : " << message->getMessageId() << std::endl;
368     });
369
370     mocks.OnCallFunc(SyncCallbackEmpty).Do(
371         [& type, this](OIC::Service::NSSyncInfo * sync)
372     {
373         std::cout << "Income Notification : " << sync->getMessageId()
374                   << ", State : " << (int) sync->getState() << std::endl;
375         type = sync->getState();
376         responseCon.notify_all();
377     });
378
379     g_providerSimul.notifyMessage(id, title, msg);
380     {
381         std::unique_lock< std::mutex > lock { mutexForCondition };
382         responseCon.wait_for(lock, g_waitForResponse);
383     }
384
385     g_providerSimul.sendDismiss(id);
386     {
387         std::unique_lock< std::mutex > lock { mutexForCondition };
388         responseCon.wait_for(lock, g_waitForResponse);
389     }
390
391     EXPECT_EQ(OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED, type);
392 }
393
394 TEST_F(NotificationServiceConsumerTest, ExpectCallbackReadCheckWhenConsumerPostSync)
395 {
396     uint64_t id = 14;
397     std::string title = "title";
398     std::string msg = "msg";
399     OIC::Service::NSSyncInfo::NSSyncType type = OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED;
400
401     mocks.OnCallFunc(NotificationReceivedCallbackEmpty).Do(
402         [this]( OIC::Service::NSMessage * message)
403     {
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);
409     });
410
411     mocks.OnCallFunc(SyncCallbackEmpty).Do(
412         [& type, this](OIC::Service::NSSyncInfo * sync)
413     {
414         std::cout << "Income Notification : " << sync->getMessageId()
415                   << ", State : " << (int) sync->getState() << std::endl;
416         type = sync->getState();
417         responseCon.notify_all();
418     });
419
420     g_providerSimul.notifyMessage(id, title, msg);
421     {
422         std::unique_lock< std::mutex > lock { mutexForCondition };
423         responseCon.wait_for(lock, g_waitForResponse);
424     }
425
426     EXPECT_EQ(OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ, type);
427 }
428
429 TEST_F(NotificationServiceConsumerTest, ExpectCallbackDismissCheckWhenConsumerPostSync)
430 {
431     uint64_t id = 15;
432     std::string title = "title";
433     std::string msg = "msg";
434     OIC::Service::NSSyncInfo::NSSyncType type = OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ;
435
436     mocks.OnCallFunc(NotificationReceivedCallbackEmpty).Do(
437         [this]( OIC::Service::NSMessage * message)
438     {
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);
444     });
445
446     mocks.OnCallFunc(SyncCallbackEmpty).Do(
447         [& type, this](OIC::Service::NSSyncInfo * sync)
448     {
449         std::cout << "Income Notification : " << sync->getMessageId()
450                   << ", State : " << (int) sync->getState() << std::endl;
451         type = sync->getState();
452         responseCon.notify_all();
453     });
454
455     g_providerSimul.notifyMessage(id, title, msg);
456     {
457         std::unique_lock< std::mutex > lock { mutexForCondition };
458         responseCon.wait_for(lock, g_waitForResponse);
459     }
460
461     EXPECT_EQ(OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED, type);
462 }
463
464 TEST_F(NotificationServiceConsumerTest, ExpectCallbackDeletedProvider)
465 {
466     OIC::Service::NSProviderState type = OIC::Service::NSProviderState::ALLOW;
467     mocks.OnCallFunc(ProviderChangedCallbackEmpty).Do(
468         [& type, this](OIC::Service::NSProviderState state)
469     {
470         std::cout << "Income Changed Callback : " << std::endl;
471         type = state;
472         responseCon.notify_all();
473     });
474
475     g_providerSimul.deleteNotificationResource();
476
477     std::unique_lock< std::mutex > lock { mutexForCondition };
478     responseCon.wait_for(lock, g_waitForResponse);
479
480     EXPECT_EQ(type, OIC::Service::NSProviderState::STOPPED);
481     OIC::Service::NSConsumerService::getInstance()->stop();
482 }
483
484 //TO add when unsubscibe API is added back
485 //TEST_F(NotificationServiceConsumerTest, ExpectUnsubscribeSuccess)
486 //{
487 //    g_provider->unsubscribe();
488 //   std::unique_lock< std::mutex > lock{ mutexForCondition };
489 //    responseCon.wait_for(lock, g_waitForResponse);
490 //
491 //    g_providerSimul.deleteNotificationResource();
492 //     OIC::Service::NSConsumerService::getInstance()->stop();
493 //
494 //}