Notification cpp-wrapper UnitTest
[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(2000);
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::NSProvider * , OIC::Service::NSResponse )
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::ConsumerConfig cfg;
157     cfg.m_discoverCb = ProviderDiscoveredCallbackEmpty;
158     cfg.m_changedCb = ProviderChangedCallbackEmpty;
159     OIC::Service::NSConsumerService::getInstance()->Start(cfg);
160 }
161
162 TEST_F(NotificationServiceConsumerTest, StopConsumerPositive)
163 {
164     OIC::Service::NSConsumerService::getInstance()->Stop();
165 }
166
167 TEST_F(NotificationServiceConsumerTest, DiscoverProviderWithNonAccepterWhenStartedConsumerFirst)
168 {
169     mocks.ExpectCallFunc(ProviderDiscoveredCallbackEmpty).Do(
170             [this]( OIC::Service::NSProvider * provider)
171             {
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();
178             });
179
180     OIC::Service::NSConsumerService::ConsumerConfig cfg;
181     cfg.m_discoverCb = ProviderDiscoveredCallbackEmpty;
182     cfg.m_changedCb = ProviderChangedCallbackEmpty;
183     OIC::Service::NSConsumerService::getInstance()->Start(cfg);
184
185     g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_CONSUMER);
186     g_providerSimul.createNotificationResource();
187
188     std::unique_lock< std::mutex > lock{ mutexForCondition };
189     responseCon.wait_for(lock, g_waitForResponse);
190
191      OIC::Service::NSConsumerService::getInstance()->Stop();
192     g_providerSimul.deleteNotificationResource();
193 }
194
195 TEST_F(NotificationServiceConsumerTest, DiscoverProviderWithNonAccepterWhenStartedConsumerAfter)
196 {
197     g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_CONSUMER);
198     g_providerSimul.createNotificationResource();
199     {
200         std::unique_lock< std::mutex > lock{ mutexForCondition };
201         responseCon.wait_for(lock, g_waitForResponse);
202     }
203
204     mocks.ExpectCallFunc(ProviderDiscoveredCallbackEmpty).Do(
205             [this]( OIC::Service::NSProvider * provider)
206             {
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();
212             });
213
214     OIC::Service::NSConsumerService::ConsumerConfig cfg;
215     cfg.m_discoverCb = ProviderDiscoveredCallbackEmpty;
216     cfg.m_changedCb = ProviderChangedCallbackEmpty;
217     OIC::Service::NSConsumerService::getInstance()->Start(cfg);
218
219     std::unique_lock< std::mutex > lock{ mutexForCondition };
220     responseCon.wait_for(lock, g_waitForResponse);
221
222 }
223
224 TEST_F(NotificationServiceConsumerTest, DiscoverProviderWithNonAccepterWhenRescan)
225 {
226     g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_CONSUMER);
227     mocks.ExpectCallFunc(ProviderDiscoveredCallbackEmpty).Do(
228             [this]( OIC::Service::NSProvider * provider)
229             {
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();
237             });
238
239      OIC::Service::NSConsumerService::getInstance()->RescanProvider();
240
241     std::unique_lock< std::mutex > lock{ mutexForCondition };
242     responseCon.wait_for(lock, g_waitForResponse);
243
244 //     OIC::Service::NSConsumerService::getInstance()->Stop();
245 }
246
247 TEST_F(NotificationServiceConsumerTest, ExpectSubscribeSuccess)
248 {
249 //    mocks.ExpectCallFunc(ProviderChangedCallbackEmpty).Do(
250 //            []( OIC::Service::NSProvider * , OIC::Service::NSResponse)
251 //            {
252 //                std::cout << "Income Accepted subscription : " << std::endl;
253 //            });
254
255     g_provider->subscribe();
256     std::unique_lock< std::mutex > lock{ mutexForCondition };
257     responseCon.wait_for(lock, g_waitForResponse);
258 }
259
260 TEST_F(NotificationServiceConsumerTest, ExpectReceiveNotification)
261 {
262     uint64_t id = 10;
263     std::string title = "title";
264     std::string msg = "msg";
265
266     mocks.ExpectCallFunc(NotificationReceivedCallbackEmpty).Do(
267             []( OIC::Service::NSMessage * message)
268             {
269                 std::cout << "Income Notification : " << message->getMessageId() << std::endl;
270             });
271
272     g_providerSimul.notifyMessage(id, title, msg);
273
274     std::unique_lock< std::mutex > lock{ mutexForCondition };
275     responseCon.wait_for(lock, g_waitForResponse);
276
277      OIC::Service::NSConsumerService::getInstance()->Stop();
278 }
279
280 TEST_F(NotificationServiceConsumerTest, ExpectReceiveNotificationWithAccepterisProvider)
281 {
282     uint64_t id = 11;
283     std::string title = "title";
284     std::string msg = "msg";
285
286     g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_PROVIDER);
287
288     OIC::Service::NSConsumerService::ConsumerConfig cfg;
289     cfg.m_discoverCb = ProviderDiscoveredCallbackEmpty;
290     cfg.m_changedCb = ProviderChangedCallbackEmpty;
291     OIC::Service::NSConsumerService::getInstance()->Start(cfg);
292     {
293         std::unique_lock< std::mutex > lock{ mutexForCondition };
294         responseCon.wait_for(lock, g_waitForResponse);
295     }
296
297     mocks.ExpectCallFunc(NotificationReceivedCallbackEmpty).Do(
298             []( OIC::Service::NSMessage * message)
299             {
300                 std::cout << "Income Notification : " << message->getMessageId() << std::endl;
301             });
302
303     g_providerSimul.notifyMessage(id, title, msg);
304
305     std::unique_lock< std::mutex > lock{ mutexForCondition };
306     responseCon.wait_for(lock, g_waitForResponse);
307
308 //    g_providerSimul.deleteNotificationResource();
309 //     OIC::Service::NSConsumerService::getInstance()->Stop();
310 }
311
312 TEST_F(NotificationServiceConsumerTest, ExpectCallbackReadCheckWhenProviderNotifySync)
313 {
314     uint64_t id = 12;
315     std::string title = "title";
316     std::string msg = "msg";
317
318     OIC::Service::NSSyncInfo::NSSyncType type = OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED;
319
320     mocks.OnCallFunc(NotificationReceivedCallbackEmpty).Do(
321             []( OIC::Service::NSMessage * message)
322             {
323                 std::cout << "Income Notification : " << message->getMessageId() << std::endl;
324             });
325
326     mocks.ExpectCallFunc(SyncCallbackEmpty).Do(
327             [& type](OIC::Service::NSSyncInfo * sync)
328             {
329                 std::cout << "Income SyncInfo : " << sync->getMessageId()
330                         << ", State : " << (int) sync->getState() << std::endl;
331                 type = sync->getState();
332
333             });
334
335     g_providerSimul.notifyMessage(id, title, msg);
336     {
337         std::unique_lock< std::mutex > lock{ mutexForCondition };
338         responseCon.wait_for(lock, g_waitForResponse);
339     }
340
341     g_providerSimul.sendRead(id);
342     {
343         std::unique_lock< std::mutex > lock{ mutexForCondition };
344         responseCon.wait_for(lock, g_waitForResponse);
345     }
346
347 //    g_providerSimul.deleteNotificationResource();
348 //     OIC::Service::NSConsumerService::getInstance()->Stop();
349
350     EXPECT_EQ(OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ, type);
351 }
352
353 TEST_F(NotificationServiceConsumerTest, ExpectCallbackDismissCheckWhenProviderNotifySync)
354 {
355     uint64_t id = 13;
356     std::string title = "title";
357     std::string msg = "msg";
358
359     OIC::Service::NSSyncInfo::NSSyncType type = OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ;
360
361     mocks.OnCallFunc(NotificationReceivedCallbackEmpty).Do(
362             []( OIC::Service::NSMessage * message)
363             {
364                 std::cout << "Income Notification : " << message->getMessageId() << std::endl;
365             });
366
367     mocks.ExpectCallFunc(SyncCallbackEmpty).Do(
368             [& type](OIC::Service::NSSyncInfo * sync)
369             {
370                 std::cout << "Income Notification : " << sync->getMessageId()
371                         << ", State : " << (int) sync->getState() << std::endl;
372                 type = sync->getState();
373
374             });
375
376     g_providerSimul.notifyMessage(id, title, msg);
377     {
378         std::unique_lock< std::mutex > lock{ mutexForCondition };
379         responseCon.wait_for(lock, g_waitForResponse);
380     }
381
382     g_providerSimul.sendDismiss(id);
383     {
384         std::unique_lock< std::mutex > lock{ mutexForCondition };
385         responseCon.wait_for(lock, g_waitForResponse);
386     }
387
388 //    g_providerSimul.deleteNotificationResource();
389 //     OIC::Service::NSConsumerService::getInstance()->Stop();
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
400     OIC::Service::NSSyncInfo::NSSyncType type = OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED;
401
402     mocks.OnCallFunc(NotificationReceivedCallbackEmpty).Do(
403             []( OIC::Service::NSMessage * message)
404             {
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);
409             });
410
411     mocks.ExpectCallFunc(SyncCallbackEmpty).Do(
412             [& type](OIC::Service::NSSyncInfo * sync)
413             {
414                 std::cout << "Income Notification : " << sync->getMessageId()
415                         << ", State : " << (int) sync->getState() << std::endl;
416                 type = sync->getState();
417
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 //    g_providerSimul.deleteNotificationResource();
427 //     OIC::Service::NSConsumerService::getInstance()->Stop();
428
429     EXPECT_EQ(OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ, type);
430 }
431
432 TEST_F(NotificationServiceConsumerTest, ExpectCallbackDismissCheckWhenConsumerPostSync)
433 {
434     uint64_t id = 15;
435     std::string title = "title";
436     std::string msg = "msg";
437
438     OIC::Service::NSSyncInfo::NSSyncType type = OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ;
439
440     mocks.OnCallFunc(NotificationReceivedCallbackEmpty).Do(
441             []( OIC::Service::NSMessage * message)
442             {
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);
447             });
448
449     mocks.ExpectCallFunc(SyncCallbackEmpty).Do(
450             [& type](OIC::Service::NSSyncInfo * sync)
451             {
452                 std::cout << "Income Notification : " << sync->getMessageId()
453                         << ", State : " << (int) sync->getState() << std::endl;
454                 type = sync->getState();
455
456             });
457
458     g_providerSimul.notifyMessage(id, title, msg);
459     {
460         std::unique_lock< std::mutex > lock{ mutexForCondition };
461         responseCon.wait_for(lock, g_waitForResponse);
462     }
463
464     EXPECT_EQ(OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED, type);
465 }
466
467 TEST_F(NotificationServiceConsumerTest, ExpectUnsubscribeSuccess)
468 {
469     g_provider->unSubscribe();
470     std::unique_lock< std::mutex > lock{ mutexForCondition };
471     responseCon.wait_for(lock, g_waitForResponse);
472
473     g_providerSimul.deleteNotificationResource();
474      OIC::Service::NSConsumerService::getInstance()->Stop();
475
476 }