Added SVACE Fixes for C++ wrapper Unit TCs
[platform/upstream/iotivity.git] / service / notification / cpp-wrapper / unittest / NSProviderServiceTest.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 "NSProviderService.h"
30 #include "NSConsumerServiceSimulator.h"
31 #include "NSUtils.h"
32 #include "NSSyncInfo.h"
33 #include "NSMessage.h"
34 #include "NSMediaContents.h"
35
36 namespace
37 {
38     std::atomic_bool g_isStartedStack(false);
39
40     std::chrono::milliseconds g_waitForResponse(500);
41
42     std::condition_variable responseCon;
43     std::mutex mutexForCondition;
44
45     NSConsumerSimulator g_consumerSimul;
46     OIC::Service::NSConsumer *g_consumer;
47 }
48
49 class TestWithMock: public testing::Test
50 {
51     public:
52         MockRepository mocks;
53
54     protected:
55         virtual ~TestWithMock() noexcept(noexcept(std::declval<Test>().~Test()))
56         {
57         }
58
59         virtual void TearDown()
60         {
61             try
62             {
63                 mocks.VerifyAll();
64             }
65             catch (...)
66             {
67                 mocks.reset();
68                 throw;
69             }
70         }
71 };
72
73 class NotificationProviderServiceTest : public TestWithMock
74 {
75     public:
76         NotificationProviderServiceTest() = default;
77         ~NotificationProviderServiceTest() = default;
78
79         static void ConsumerSubscribedCallbackEmpty(OIC::Service::NSConsumer *)
80         {
81             std::cout << __func__ << std::endl;
82         }
83
84         static void MessageSynchronizedCallbackEmpty(OIC::Service::NSSyncInfo *)
85         {
86             std::cout << __func__ << std::endl;
87         }
88
89         static void MessageCallbackFromConsumerEmpty(
90             const int &, const std::string &, const std::string &, const std::string &)
91         {
92             std::cout << __func__ << std::endl;
93         }
94
95         static void SyncCallbackFromConsumerEmpty(int, int)
96         {
97             std::cout << __func__ << std::endl;
98         }
99
100     protected:
101
102         void SetUp()
103         {
104             TestWithMock::SetUp();
105
106             if (g_isStartedStack == false)
107             {
108                 OC::PlatformConfig cfg
109                 {
110                     OC::ServiceType::InProc,
111                     OC::ModeType::Both,
112                     "0.0.0.0",
113                     0,
114                     OC::QualityOfService::HighQos
115                 };
116                 OC::OCPlatform::Configure(cfg);
117
118                 try
119                 {
120                     OC::OCPlatform::stopPresence();
121                 }
122                 catch (...)
123                 {
124
125                 }
126
127                 g_isStartedStack = true;
128             }
129
130         }
131
132         void TearDown()
133         {
134             TestWithMock::TearDown();
135         }
136
137 };
138
139 TEST_F(NotificationProviderServiceTest, StartProviderPositiveWithPolicyTrue)
140 {
141     OIC::Service::NSProviderService::ProviderConfig config;
142     config.m_subscribeRequestCb = ConsumerSubscribedCallbackEmpty;
143     config.m_syncInfoCb = MessageSynchronizedCallbackEmpty;
144     config.subControllability = true;
145
146     OIC::Service::NSResult ret =  OIC::Service::NSProviderService::getInstance()->start(config);
147
148     EXPECT_EQ(ret,  OIC::Service::NSResult::OK);
149 }
150
151 TEST_F(NotificationProviderServiceTest, StopProviderPositive)
152 {
153     OIC::Service::NSResult ret =  OIC::Service::NSProviderService::getInstance()->stop();
154
155     EXPECT_EQ(ret,  OIC::Service::NSResult::OK);
156 }
157
158 TEST_F(NotificationProviderServiceTest, StartProviderPositiveWithPolicyFalse)
159 {
160     OIC::Service::NSProviderService::ProviderConfig  config;
161     config.m_subscribeRequestCb = ConsumerSubscribedCallbackEmpty;
162     config.m_syncInfoCb = MessageSynchronizedCallbackEmpty;
163     config.subControllability = false;
164
165     OIC::Service::NSResult ret =  OIC::Service::NSProviderService::getInstance()->start(config);
166
167     EXPECT_EQ(ret,  OIC::Service::NSResult::OK);
168     OIC::Service::NSProviderService::getInstance()->stop();
169 }
170
171 TEST_F(NotificationProviderServiceTest,
172        ExpectCallbackWhenReceiveSubscribeRequestWithAccepterProvider)
173 {
174     g_consumer = NULL;
175     mocks.ExpectCallFunc(ConsumerSubscribedCallbackEmpty).Do(
176         []( OIC::Service::NSConsumer * consumer)
177     {
178         std::cout << "ConsumerSubscribedCallbackEmpty" << std::endl;
179         g_consumer = consumer;
180         responseCon.notify_all();
181     });
182
183     OIC::Service::NSProviderService::ProviderConfig  config;
184     config.m_subscribeRequestCb = ConsumerSubscribedCallbackEmpty;
185     config.m_syncInfoCb = MessageSynchronizedCallbackEmpty;
186     config.subControllability = true;
187
188     OIC::Service::NSProviderService::getInstance()->start(config);
189
190     {
191         std::unique_lock< std::mutex > lock { mutexForCondition };
192         responseCon.wait_for(lock, g_waitForResponse);
193     }
194
195     g_consumerSimul.setCallback(MessageCallbackFromConsumerEmpty,
196                                 SyncCallbackFromConsumerEmpty);
197     g_consumerSimul.findProvider();
198
199     std::unique_lock< std::mutex > lock { mutexForCondition };
200     responseCon.wait_for(lock, g_waitForResponse);
201
202     EXPECT_NE((void*)g_consumer, (void*)NULL);
203 }
204
205 TEST_F(NotificationProviderServiceTest, NeverCallNotifyOnConsumerByAcceptIsFalse)
206 {
207     bool expectTrue = true;
208     int msgID = 0;
209
210     mocks.OnCallFunc(MessageCallbackFromConsumerEmpty).Do(
211         [& expectTrue, &msgID](const int & id, const std::string &, const std::string &,
212                                const std::string &)
213     {
214         if (id == msgID)
215         {
216             std::cout << "This function never call" << std::endl;
217             expectTrue = false;
218         }
219         responseCon.notify_all();
220     });
221
222     g_consumer->acceptSubscription(false);
223
224     OIC::Service::NSMessage *msg =  OIC::Service::NSProviderService::getInstance()->createMessage();
225     msgID = (int)msg->getMessageId();
226     msg->setTitle(std::string("Title"));
227     msg->setContentText(std::string("ContentText"));
228     msg->setSourceName(std::string("OCF"));
229
230     OIC::Service::NSProviderService::getInstance()->sendMessage(msg);
231     {
232         std::unique_lock< std::mutex > lock { mutexForCondition };
233         responseCon.wait_for(lock, g_waitForResponse);
234     }
235
236     std::unique_lock< std::mutex > lock { mutexForCondition };
237     responseCon.wait_for(lock, g_waitForResponse);
238
239     EXPECT_EQ(expectTrue, true);
240 }
241
242 TEST_F(NotificationProviderServiceTest, ExpectCallNotifyOnConsumerByAcceptIsTrue)
243 {
244     int msgID = 0;
245
246     mocks.ExpectCallFunc(MessageCallbackFromConsumerEmpty).Do(
247         [&msgID](const int & id, const std::string &, const std::string &, const std::string &)
248     {
249         if (id == msgID)
250         {
251             std::cout << "ExpectCallNotifyOnConsumerByAcceptIsTrue" << std::endl;
252             responseCon.notify_all();
253         }
254     });
255
256     g_consumer->acceptSubscription(true);
257
258     OIC::Service::NSMessage *msg =  OIC::Service::NSProviderService::getInstance()->createMessage();
259     msgID = (int)msg->getMessageId();
260     msg->setTitle(std::string("Title"));
261     msg->setContentText(std::string("ContentText"));
262     msg->setSourceName(std::string("OCF"));
263
264     OIC::Service::NSProviderService::getInstance()->sendMessage(msg);
265     std::unique_lock< std::mutex > lock { mutexForCondition };
266     responseCon.wait_for(lock, g_waitForResponse);
267 }
268
269 TEST_F(NotificationProviderServiceTest, ExpectCallbackSyncOnReadToConsumer)
270 {
271     int id = 0;
272
273     mocks.ExpectCallFunc(SyncCallbackFromConsumerEmpty).Do(
274         [& id](int & type, int & syncId)
275     {
276         std::cout << "MessageSynchronizedCallbackEmpty" << std::endl;
277         if (syncId == id &&
278             type == (int)OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ)
279         {
280             std::cout << "ExpectCallbackSyncOnReadFromConsumer" << std::endl;
281             responseCon.notify_all();
282         }
283     });
284
285     OIC::Service::NSMessage *msg =  OIC::Service::NSProviderService::getInstance()->createMessage();
286     id = (int)msg->getMessageId();
287     msg->setTitle(std::string("Title"));
288     msg->setContentText(std::string("ContentText"));
289     msg->setSourceName(std::string("OCF"));
290
291     OIC::Service::NSProviderService::getInstance()->sendSyncInfo(msg->getMessageId(),
292             OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ);
293     std::unique_lock< std::mutex > lock { mutexForCondition };
294     responseCon.wait_for(lock, g_waitForResponse);
295 }
296
297 TEST_F(NotificationProviderServiceTest, ExpectCallbackSyncOnReadFromConsumer)
298 {
299     int type = (int)OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ;
300     int id = 0;
301     mocks.ExpectCallFunc(MessageSynchronizedCallbackEmpty).Do(
302         [& id](OIC::Service::NSSyncInfo * sync)
303     {
304         std::cout << "MessageSynchronizedCallbackEmpty" << std::endl;
305         if ((int)sync->getMessageId() == id
306             && sync->getState() == OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ)
307         {
308             std::cout << "ExpectCallbackSyncOnReadFromConsumer" << std::endl;
309             responseCon.notify_all();
310         }
311     });
312
313     OIC::Service::NSMessage *msg =  OIC::Service::NSProviderService::getInstance()->createMessage();
314     id = (int)msg->getMessageId();
315     msg->setTitle(std::string("Title"));
316     msg->setContentText(std::string("ContentText"));
317     msg->setSourceName(std::string("OCF"));
318     g_consumerSimul.syncToProvider(type, id, msg->getProviderId());
319     std::unique_lock< std::mutex > lock { mutexForCondition };
320     responseCon.wait(lock);
321 }
322
323 TEST_F(NotificationProviderServiceTest, ExpectEqualAddedTopicsAndRegisteredTopics)
324 {
325     std::string str1("TEST1");
326     std::string str2("TEST2");
327     OIC::Service::NSProviderService::getInstance()->registerTopic(str1);
328     OIC::Service::NSProviderService::getInstance()->registerTopic(str2);
329     std::unique_lock< std::mutex > lock { mutexForCondition };
330     responseCon.wait_for(lock, g_waitForResponse);
331     bool isSame = false;
332     OIC::Service::NSTopicsList *topicList =
333         OIC::Service::NSProviderService::getInstance()->getRegisteredTopicList();
334     if (!topicList)
335     {
336         printf("topic is NULL\n");
337         isSame = false;
338     }
339     else
340     {
341         std::string compString[10];
342         int i = 0;
343         for (auto itr : topicList->getTopicsList())
344         {
345             compString[i] = itr->getTopicName(); i++;
346         }
347         std::cout << compString[0] << std::endl;
348         std::cout << compString[1] << std::endl;
349         if (str1.compare(compString[0]) == 0 && str2.compare(compString[1]) == 0)
350         {
351             isSame = true;
352         }
353     }
354     EXPECT_EQ(isSame, true);
355
356     OIC::Service::NSProviderService::getInstance()->unregisterTopic(str1);
357     OIC::Service::NSProviderService::getInstance()->unregisterTopic(str2);
358     if(topicList != nullptr)
359     {
360         delete topicList;
361     }
362     responseCon.wait_for(lock, g_waitForResponse);
363 }
364
365 TEST_F(NotificationProviderServiceTest, ExpectEqualUnregisteredTopicsAndRegisteredTopics)
366 {
367     std::string str1("TEST1");
368     std::string str2("TEST2");
369     OIC::Service::NSProviderService::getInstance()->registerTopic(str1);
370     OIC::Service::NSProviderService::getInstance()->registerTopic(str2);
371     OIC::Service::NSProviderService::getInstance()->unregisterTopic(str2);
372     std::unique_lock< std::mutex > lock { mutexForCondition };
373     responseCon.wait_for(lock, g_waitForResponse);
374     bool isSame = false;
375     OIC::Service::NSTopicsList *topicList =
376         OIC::Service::NSProviderService::getInstance()->getRegisteredTopicList();
377     if (!topicList)
378     {
379         printf("topic is NULL\n");
380         isSame = false;
381     }
382     else
383     {
384         std::list<OIC::Service::NSTopic *>::iterator it = topicList->getTopicsList().begin();
385         std::string compStr = (*it)->getTopicName() ;
386         std::cout << compStr << std::endl;
387         if (str1.compare(compStr) == 0 )
388         {
389             isSame = true;
390         }
391     }
392     EXPECT_EQ(isSame, true);
393
394     OIC::Service::NSProviderService::getInstance()->unregisterTopic(str1);
395     if(topicList != nullptr)
396     {
397         delete topicList;
398     }
399     responseCon.wait_for(lock, g_waitForResponse);
400 }
401
402 TEST_F(NotificationProviderServiceTest, ExpectEqualSetConsumerTopicsAndGetConsumerTopics)
403 {
404     std::string str1("TEST1");
405     std::string str2("TEST2");
406     OIC::Service::NSProviderService::getInstance()->registerTopic(str1);
407     OIC::Service::NSProviderService::getInstance()->registerTopic(str2);
408     g_consumer->setTopic(str1);
409
410     std::unique_lock< std::mutex > lock { mutexForCondition };
411     responseCon.wait_for(lock, g_waitForResponse);
412
413     bool isSame = false;
414     OIC::Service::NSTopicsList *topicList =  g_consumer->getConsumerTopicList();
415
416     if (!topicList)
417     {
418         printf("topic is NULL\n");
419         isSame = false;
420     }
421     else
422     {
423         std::string compString[10];
424         int i = 0, state[10] = {0};
425         for (auto itr : topicList->getTopicsList())
426         {
427             compString[i] = itr->getTopicName();
428             state[i++] = (int) itr->getState();
429         }
430         std::cout << compString[0] << std::endl;
431         std::cout << compString[1] << std::endl;
432         if (str1.compare(compString[0]) == 0 && str2.compare(compString[1]) == 0
433             && state[0] == 1 &&  state[1] == 0)
434         {
435             isSame = true;
436         }
437     }
438
439     EXPECT_EQ(isSame, true);
440
441     OIC::Service::NSProviderService::getInstance()->unregisterTopic(str1);
442     OIC::Service::NSProviderService::getInstance()->unregisterTopic(str2);
443     if(topicList != nullptr)
444     {
445         delete topicList;
446     }
447     responseCon.wait_for(lock, g_waitForResponse);
448 }
449
450 TEST_F(NotificationProviderServiceTest, ExpectEqualUnSetConsumerTopicsAndGetConsumerTopics)
451 {
452     std::string str1("TEST1");
453     std::string str2("TEST2");
454     OIC::Service::NSProviderService::getInstance()->registerTopic(str1);
455     OIC::Service::NSProviderService::getInstance()->registerTopic(str2);
456     g_consumer->setTopic(str1);
457     g_consumer->setTopic(str2);
458     g_consumer->unsetTopic(str1);
459
460     std::unique_lock< std::mutex > lock { mutexForCondition };
461     responseCon.wait_for(lock, g_waitForResponse);
462
463     bool isSame = false;
464     OIC::Service::NSTopicsList *topicList =  g_consumer->getConsumerTopicList();
465
466     if (!topicList)
467     {
468         printf("topic is NULL\n");
469         isSame = false;
470     }
471     else
472     {
473         std::string compString[10];
474         int i = 0, state[10] = {0};
475         for (auto itr : topicList->getTopicsList())
476         {
477             compString[i] = itr->getTopicName();
478             state[i++] = (int) itr->getState();
479         }
480         std::cout << compString[0] << std::endl;
481         std::cout << compString[1] << std::endl;
482         if (str1.compare(compString[0]) == 0 && str2.compare(compString[1]) == 0
483             && state[0] == 0 &&  state[1] == 1)
484         {
485             isSame = true;
486         }
487     }
488
489     EXPECT_EQ(isSame, true);
490
491     OIC::Service::NSProviderService::getInstance()->unregisterTopic(str1);
492     OIC::Service::NSProviderService::getInstance()->unregisterTopic(str2);
493     
494     if(topicList != nullptr)
495     {
496         delete topicList;
497     }
498     responseCon.wait_for(lock, g_waitForResponse);
499 }
500
501 TEST_F(NotificationProviderServiceTest, ExpectFailSendMessage)
502 {
503     OIC::Service::NSResult result = OIC::Service::NSResult::OK;
504     result = OIC::Service::NSProviderService::getInstance()->sendMessage(nullptr);
505
506     EXPECT_EQ(result, OIC::Service::NSResult::ERROR);
507 }
508
509 TEST_F(NotificationProviderServiceTest, CancelObserves)
510 {
511     bool ret = g_consumerSimul.cancelObserves();
512
513     std::unique_lock< std::mutex > lock { mutexForCondition };
514     responseCon.wait_for(lock, g_waitForResponse);
515
516     EXPECT_EQ(ret, true);
517 }