Added SVACE Fixes for C++ wrapper Unit TCs
[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     {
246         g_provider->subscribe();
247     }
248     std::unique_lock< std::mutex > lock { mutexForCondition };
249     responseCon.wait_for(lock, g_waitForResponse);
250     EXPECT_EQ(OIC::Service::NSProviderState::ALLOW, revState);
251 }
252
253 TEST_F(NotificationServiceConsumerTest, ExpectReceiveNotification)
254 {
255     uint64_t id = 10;
256     std::string title = "title";
257     std::string msg = "msg";
258
259     mocks.ExpectCallFunc(NotificationReceivedCallbackEmpty).Do(
260         [this]( OIC::Service::NSMessage * message)
261     {
262         std::cout << "Income Notification : " << message->getMessageId() << std::endl;
263         responseCon.notify_all();
264     });
265
266     g_providerSimul.notifyMessage(id, title, msg);
267
268     std::unique_lock< std::mutex > lock { mutexForCondition };
269     responseCon.wait_for(lock, g_waitForResponse);
270
271     OIC::Service::NSConsumerService::getInstance()->stop();
272 }
273
274 TEST_F(NotificationServiceConsumerTest, DiscoverProviderWithAccepterisProvider)
275 {
276     g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_PROVIDER);
277
278     mocks.ExpectCallFunc(ProviderDiscoveredCallbackEmpty).Do(
279         [this]( OIC::Service::NSProvider * provider)
280     {
281         std::cout << "Call Discovered" << std::endl;
282         g_provider = provider;
283         g_provider->setListener( (OIC::Service::NSProvider::ProviderStateCallback)
284                                  ProviderChangedCallbackEmpty,
285                                  (OIC::Service::NSProvider::MessageReceivedCallback)NotificationReceivedCallbackEmpty,
286                                  (OIC::Service::NSProvider::SyncInfoReceivedCallback)SyncCallbackEmpty);
287         if (!g_provider->isSubscribed())
288         {
289             g_provider->subscribe();
290         }
291         std::cout << g_provider->getProviderId() << std::endl;
292         responseCon.notify_all();
293     });
294
295
296     OIC::Service::NSConsumerService::getInstance()->start(ProviderDiscoveredCallbackEmpty);
297     std::unique_lock< std::mutex > lock { mutexForCondition };
298     responseCon.wait_for(lock, g_waitForResponse);
299 }
300
301 TEST_F(NotificationServiceConsumerTest, ExpectReceiveNotificationWithAccepterisProvider)
302 {
303     uint64_t id = 11;
304     std::string title = "title";
305     std::string msg = "msg";
306     uint64_t revId = 1;
307
308     mocks.OnCallFunc(NotificationReceivedCallbackEmpty).Do(
309         [this, & id, & revId](OIC::Service::NSMessage * message)
310     {
311         std::cout << "Income Notification : " << message->getMessageId() << std::endl;
312         revId =  message->getMessageId();
313         responseCon.notify_all();
314     });
315
316     g_providerSimul.notifyMessage(id, title, msg);
317
318     std::unique_lock< std::mutex > lock { mutexForCondition };
319     responseCon.wait_for(lock, g_waitForResponse);
320
321     EXPECT_EQ(id, revId);
322 }
323
324 TEST_F(NotificationServiceConsumerTest, ExpectCallbackReadCheckWhenProviderNotifySync)
325 {
326     uint64_t id = 12;
327     std::string title = "title";
328     std::string msg = "msg";
329     OIC::Service::NSSyncInfo::NSSyncType type = OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED;
330
331     mocks.OnCallFunc(NotificationReceivedCallbackEmpty).Do(
332         [this]( OIC::Service::NSMessage * message)
333     {
334         std::cout << "Income Notification : " << message->getMessageId() << std::endl;
335     });
336
337     mocks.OnCallFunc(SyncCallbackEmpty).Do(
338         [& type, this](OIC::Service::NSSyncInfo * sync)
339     {
340         std::cout << "Income SyncInfo : " << sync->getMessageId()
341                   << ", State : " << (int) sync->getState() << std::endl;
342         type = sync->getState();
343         responseCon.notify_all();
344     });
345
346     g_providerSimul.notifyMessage(id, title, msg);
347     {
348         std::unique_lock< std::mutex > lock { mutexForCondition };
349         responseCon.wait_for(lock, g_waitForResponse);
350     }
351
352     g_providerSimul.sendRead(id);
353     {
354         std::unique_lock< std::mutex > lock { mutexForCondition };
355         responseCon.wait_for(lock, g_waitForResponse);
356     }
357
358     EXPECT_EQ(OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ, type);
359 }
360
361 TEST_F(NotificationServiceConsumerTest, ExpectCallbackDismissCheckWhenProviderNotifySync)
362 {
363     uint64_t id = 13;
364     std::string title = "title";
365     std::string msg = "msg";
366     OIC::Service::NSSyncInfo::NSSyncType type = OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ;
367
368     mocks.OnCallFunc(NotificationReceivedCallbackEmpty).Do(
369         [this]( OIC::Service::NSMessage * message)
370     {
371         std::cout << "Income Notification : " << message->getMessageId() << std::endl;
372     });
373
374     mocks.OnCallFunc(SyncCallbackEmpty).Do(
375         [& type, this](OIC::Service::NSSyncInfo * sync)
376     {
377         std::cout << "Income Notification : " << sync->getMessageId()
378                   << ", State : " << (int) sync->getState() << std::endl;
379         type = sync->getState();
380         responseCon.notify_all();
381     });
382
383     g_providerSimul.notifyMessage(id, title, msg);
384     {
385         std::unique_lock< std::mutex > lock { mutexForCondition };
386         responseCon.wait_for(lock, g_waitForResponse);
387     }
388
389     g_providerSimul.sendDismiss(id);
390     {
391         std::unique_lock< std::mutex > lock { mutexForCondition };
392         responseCon.wait_for(lock, g_waitForResponse);
393     }
394
395     EXPECT_EQ(OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED, type);
396 }
397
398 TEST_F(NotificationServiceConsumerTest, ExpectCallbackReadCheckWhenConsumerPostSync)
399 {
400     uint64_t id = 14;
401     std::string title = "title";
402     std::string msg = "msg";
403     OIC::Service::NSSyncInfo::NSSyncType type = OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED;
404
405     mocks.OnCallFunc(NotificationReceivedCallbackEmpty).Do(
406         [this]( OIC::Service::NSMessage * message)
407     {
408         std::cout << "Income Notification : " << message->getMessageId() << std::endl;
409         g_provider->sendSyncInfo(message->getMessageId(),
410                                  OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ);
411         std::unique_lock< std::mutex > lock { mutexForCondition };
412         responseCon.wait_for(lock, g_waitForResponse);
413     });
414
415     mocks.OnCallFunc(SyncCallbackEmpty).Do(
416         [& type, this](OIC::Service::NSSyncInfo * sync)
417     {
418         std::cout << "Income Notification : " << sync->getMessageId()
419                   << ", State : " << (int) sync->getState() << std::endl;
420         type = sync->getState();
421         responseCon.notify_all();
422     });
423
424     g_providerSimul.notifyMessage(id, title, msg);
425     {
426         std::unique_lock< std::mutex > lock { mutexForCondition };
427         responseCon.wait_for(lock, g_waitForResponse);
428     }
429
430     EXPECT_EQ(OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ, type);
431 }
432
433 TEST_F(NotificationServiceConsumerTest, ExpectCallbackDismissCheckWhenConsumerPostSync)
434 {
435     uint64_t id = 15;
436     std::string title = "title";
437     std::string msg = "msg";
438     OIC::Service::NSSyncInfo::NSSyncType type = OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ;
439
440     mocks.OnCallFunc(NotificationReceivedCallbackEmpty).Do(
441         [this]( OIC::Service::NSMessage * message)
442     {
443         std::cout << "Income Notification : " << message->getMessageId() << std::endl;
444         g_provider->sendSyncInfo(message->getMessageId(),
445                                  OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED);
446         std::unique_lock< std::mutex > lock { mutexForCondition };
447         responseCon.wait_for(lock, g_waitForResponse);
448     });
449
450     mocks.OnCallFunc(SyncCallbackEmpty).Do(
451         [& type, this](OIC::Service::NSSyncInfo * sync)
452     {
453         std::cout << "Income Notification : " << sync->getMessageId()
454                   << ", State : " << (int) sync->getState() << std::endl;
455         type = sync->getState();
456         responseCon.notify_all();
457     });
458
459     g_providerSimul.notifyMessage(id, title, msg);
460     {
461         std::unique_lock< std::mutex > lock { mutexForCondition };
462         responseCon.wait_for(lock, g_waitForResponse);
463     }
464
465     EXPECT_EQ(OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED, type);
466 }
467
468 TEST_F(NotificationServiceConsumerTest, ExpectGetProviderSuccessWithValidProviderId)
469 {
470     OIC::Service::NSProvider * provider =
471                         OIC::Service::NSConsumerService::getInstance()->getProvider(g_provider->getProviderId());
472     int ret = strcmp(provider->getProviderId().c_str(), g_provider->getProviderId().c_str());
473     EXPECT_EQ(0, ret);
474 }
475
476 TEST_F(NotificationServiceConsumerTest, ExpectGetProviderSuccessWithInvalidProviderId)
477 {
478     OIC::Service::NSProvider * provider =
479                         OIC::Service::NSConsumerService::getInstance()->getProvider("123456789012345678901234567890123457");
480     EXPECT_EQ(provider, (void*)NULL);
481 }
482
483 TEST_F(NotificationServiceConsumerTest, ExpectCallbackTopicUpdated)
484 {
485     OIC::Service::NSProviderState revState = OIC::Service::NSProviderState::STOPPED;
486     mocks.OnCallFunc(ProviderChangedCallbackEmpty).Do(
487         [this, & revState](OIC::Service::NSProviderState state)
488         {
489             std::cout << "Income Changed Callback : " << (int)state << std::endl;
490             revState = state;
491             responseCon.notify_all();
492         });
493
494     NSProviderSimulator::NS_TopicList topics;
495     topics.push_back("1");
496     topics.push_back("2");
497     topics.push_back("3");
498
499     g_providerSimul.setTopics(topics);
500
501     std::unique_lock< std::mutex > lock{ mutexForCondition };
502     responseCon.wait_for(lock, g_waitForResponse);
503
504     EXPECT_EQ(OIC::Service::NSProviderState::TOPIC, revState);
505 }
506
507 TEST_F(NotificationServiceConsumerTest, ExpectEQTopicList)
508 {
509     bool isSame = true;
510
511     NSProviderSimulator::NS_TopicList topics;
512     topics.push_back("1");
513     topics.push_back("2");
514     topics.push_back("3");
515
516     std::list<OIC::Service::NSTopic *>  retTopic = g_provider->getTopicList()->getTopicsList();
517     auto it1=retTopic.begin();
518     auto it2=topics.begin();
519     while( it1 != retTopic.end() || it2 != topics.end() )
520     {
521         if((*it1)->getTopicName() !=  *it2)
522         {
523             isSame = false; break;
524         }
525         it1++;it2++;
526     }
527
528
529     EXPECT_EQ(true, isSame);
530 }
531
532 TEST_F(NotificationServiceConsumerTest, ExpectFailUpdateTopicOnConsumer)
533 {
534     OIC::Service::NSTopicsList * retTopic = g_provider->getTopicList();
535     for (auto it : retTopic->getTopicsList())
536     {
537         it->setState(OIC::Service::NSTopic::NSTopicState::SUBSCRIBED);
538     }
539     OIC::Service::NSResult ret = g_provider->updateTopicList(retTopic);
540
541     EXPECT_EQ(OIC::Service::NSResult::ERROR, ret);
542 }
543
544
545 TEST_F(NotificationServiceConsumerTest, ExpectCallbackDeletedProvider)
546 {
547     OIC::Service::NSProviderState type = OIC::Service::NSProviderState::ALLOW;
548     mocks.OnCallFunc(ProviderChangedCallbackEmpty).Do(
549         [& type, this](OIC::Service::NSProviderState state)
550     {
551         std::cout << "Income Changed Callback : " << std::endl;
552         type = state;
553     });
554
555     g_providerSimul.deleteNotificationResource();
556
557     std::unique_lock< std::mutex > lock { mutexForCondition };
558     responseCon.wait_for(lock, g_waitForResponse);
559
560     EXPECT_EQ(type, OIC::Service::NSProviderState::STOPPED);
561     OIC::Service::NSConsumerService::getInstance()->stop();
562 }