215359ab77b98e1bc68c83988d2eec571c93569a
[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     ASSERT_NE(nullptr, g_consumer) << "error: discovery failure";
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     ASSERT_NE(nullptr, g_consumer) << "error: discovery failure";
223
224     g_consumer->acceptSubscription(false);
225
226     OIC::Service::NSMessage *msg =  OIC::Service::NSProviderService::getInstance()->createMessage();
227     msgID = (int)msg->getMessageId();
228     msg->setTitle(std::string("Title"));
229     msg->setContentText(std::string("ContentText"));
230     msg->setSourceName(std::string("OCF"));
231
232     OIC::Service::NSProviderService::getInstance()->sendMessage(msg);
233     {
234         std::unique_lock< std::mutex > lock { mutexForCondition };
235         responseCon.wait_for(lock, g_waitForResponse);
236     }
237
238     std::unique_lock< std::mutex > lock { mutexForCondition };
239     responseCon.wait_for(lock, g_waitForResponse);
240
241     delete msg;
242
243     EXPECT_EQ(expectTrue, true);
244 }
245
246 TEST_F(NotificationProviderServiceTest, ExpectCallNotifyOnConsumerByAcceptIsTrue)
247 {
248     int msgID = 0;
249
250     ASSERT_NE(nullptr, g_consumer) << "error: discovery failure";
251
252     mocks.ExpectCallFunc(MessageCallbackFromConsumerEmpty).Do(
253         [&msgID](const int &id, const std::string &, const std::string &, const std::string &)
254     {
255         if (id == msgID)
256         {
257             std::cout << "ExpectCallNotifyOnConsumerByAcceptIsTrue" << std::endl;
258             responseCon.notify_all();
259         }
260     });
261
262     g_consumer->acceptSubscription(true);
263
264     OIC::Service::NSMessage *msg =  OIC::Service::NSProviderService::getInstance()->createMessage();
265     msgID = (int)msg->getMessageId();
266     msg->setTitle(std::string("Title"));
267     msg->setContentText(std::string("ContentText"));
268     msg->setSourceName(std::string("OCF"));
269
270     OIC::Service::NSProviderService::getInstance()->sendMessage(msg);
271     std::unique_lock< std::mutex > lock { mutexForCondition };
272     responseCon.wait_for(lock, g_waitForResponse);
273
274     delete msg;
275 }
276
277 TEST_F(NotificationProviderServiceTest, ExpectCallbackSyncOnReadToConsumer)
278 {
279     int id = 0;
280
281     mocks.ExpectCallFunc(SyncCallbackFromConsumerEmpty).Do(
282         [& id](int &type, int &syncId)
283     {
284         std::cout << "MessageSynchronizedCallbackEmpty" << std::endl;
285         if (syncId == id &&
286             type == (int)OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ)
287         {
288             std::cout << "ExpectCallbackSyncOnReadFromConsumer" << std::endl;
289             responseCon.notify_all();
290         }
291     });
292
293     OIC::Service::NSMessage *msg =  OIC::Service::NSProviderService::getInstance()->createMessage();
294     id = (int)msg->getMessageId();
295     msg->setTitle(std::string("Title"));
296     msg->setContentText(std::string("ContentText"));
297     msg->setSourceName(std::string("OCF"));
298
299     OIC::Service::NSProviderService::getInstance()->sendSyncInfo(msg->getMessageId(),
300             OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ);
301     std::unique_lock< std::mutex > lock { mutexForCondition };
302     responseCon.wait_for(lock, g_waitForResponse);
303
304     delete msg;
305 }
306
307 TEST_F(NotificationProviderServiceTest, ExpectCallbackSyncOnReadFromConsumer)
308 {
309     int type = (int)OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ;
310     int id = 0;
311     mocks.ExpectCallFunc(MessageSynchronizedCallbackEmpty).Do(
312         [& id](OIC::Service::NSSyncInfo * sync)
313     {
314         std::cout << "MessageSynchronizedCallbackEmpty" << std::endl;
315         if ((int)sync->getMessageId() == id
316             && sync->getState() == OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ)
317         {
318             std::cout << "ExpectCallbackSyncOnReadFromConsumer" << std::endl;
319             responseCon.notify_all();
320         }
321     });
322
323     OIC::Service::NSMessage *msg =  OIC::Service::NSProviderService::getInstance()->createMessage();
324     id = (int)msg->getMessageId();
325     msg->setTitle(std::string("Title"));
326     msg->setContentText(std::string("ContentText"));
327     msg->setSourceName(std::string("OCF"));
328     g_consumerSimul.syncToProvider(type, id, msg->getProviderId());
329     std::unique_lock< std::mutex > lock { mutexForCondition };
330     responseCon.wait(lock);
331
332     delete msg;
333 }
334
335 TEST_F(NotificationProviderServiceTest, ExpectEqualAddedTopicsAndRegisteredTopics)
336 {
337     std::string str1("TEST1");
338     std::string str2("TEST2");
339     OIC::Service::NSProviderService::getInstance()->registerTopic(str1);
340     OIC::Service::NSProviderService::getInstance()->registerTopic(str2);
341     std::unique_lock< std::mutex > lock { mutexForCondition };
342     responseCon.wait_for(lock, g_waitForResponse);
343     bool isSame = false;
344     OIC::Service::NSTopicsList *topicList =
345         OIC::Service::NSProviderService::getInstance()->getRegisteredTopicList();
346     if (!topicList)
347     {
348         printf("topic is NULL\n");
349         isSame = false;
350     }
351     else
352     {
353         std::string compString[10];
354         int i = 0;
355         for (auto itr : topicList->getTopicsList())
356         {
357             compString[i] = itr->getTopicName(); i++;
358         }
359         std::cout << compString[0] << std::endl;
360         std::cout << compString[1] << std::endl;
361         if (str1.compare(compString[0]) == 0 && str2.compare(compString[1]) == 0)
362         {
363             isSame = true;
364         }
365     }
366     EXPECT_EQ(isSame, true);
367
368     OIC::Service::NSProviderService::getInstance()->unregisterTopic(str1);
369     OIC::Service::NSProviderService::getInstance()->unregisterTopic(str2);
370     if (topicList != nullptr)
371     {
372         delete topicList;
373     }
374     responseCon.wait_for(lock, g_waitForResponse);
375 }
376
377 TEST_F(NotificationProviderServiceTest, ExpectEqualUnregisteredTopicsAndRegisteredTopics)
378 {
379     std::string str1("TEST1");
380     std::string str2("TEST2");
381     OIC::Service::NSProviderService::getInstance()->registerTopic(str1);
382     OIC::Service::NSProviderService::getInstance()->registerTopic(str2);
383     OIC::Service::NSProviderService::getInstance()->unregisterTopic(str2);
384     std::unique_lock< std::mutex > lock { mutexForCondition };
385     responseCon.wait_for(lock, g_waitForResponse);
386     bool isSame = false;
387     OIC::Service::NSTopicsList *topicList =
388         OIC::Service::NSProviderService::getInstance()->getRegisteredTopicList();
389     if (!topicList)
390     {
391         printf("topic is NULL\n");
392         isSame = false;
393     }
394     else
395     {
396         std::list<OIC::Service::NSTopic *>::iterator it = topicList->getTopicsList().begin();
397         std::string compStr = (*it)->getTopicName() ;
398         std::cout << compStr << std::endl;
399         if (str1.compare(compStr) == 0 )
400         {
401             isSame = true;
402         }
403     }
404     EXPECT_EQ(isSame, true);
405
406     OIC::Service::NSProviderService::getInstance()->unregisterTopic(str1);
407     if (topicList != nullptr)
408     {
409         delete topicList;
410     }
411     responseCon.wait_for(lock, g_waitForResponse);
412 }
413
414 TEST_F(NotificationProviderServiceTest, ExpectEqualSetConsumerTopicsAndGetConsumerTopics)
415 {
416     std::string str1("TEST1");
417     std::string str2("TEST2");
418     OIC::Service::NSProviderService::getInstance()->registerTopic(str1);
419     OIC::Service::NSProviderService::getInstance()->registerTopic(str2);
420
421     ASSERT_NE(nullptr, g_consumer) << "error: discovery failure";
422
423     g_consumer->setTopic(str1);
424
425     std::unique_lock< std::mutex > lock { mutexForCondition };
426     responseCon.wait_for(lock, g_waitForResponse);
427
428     bool isSame = false;
429     OIC::Service::NSTopicsList *topicList =  g_consumer->getConsumerTopicList();
430
431     if (!topicList)
432     {
433         printf("topic is NULL\n");
434         isSame = false;
435     }
436     else
437     {
438         std::string compString[10];
439         int i = 0, state[10] = {0};
440         for (auto itr : topicList->getTopicsList())
441         {
442             compString[i] = itr->getTopicName();
443             state[i++] = (int) itr->getState();
444         }
445         std::cout << compString[0] << std::endl;
446         std::cout << compString[1] << std::endl;
447         if (str1.compare(compString[0]) == 0 && str2.compare(compString[1]) == 0
448             && state[0] == 1 &&  state[1] == 0)
449         {
450             isSame = true;
451         }
452     }
453
454     EXPECT_EQ(isSame, true);
455
456     OIC::Service::NSProviderService::getInstance()->unregisterTopic(str1);
457     OIC::Service::NSProviderService::getInstance()->unregisterTopic(str2);
458     if (topicList != nullptr)
459     {
460         delete topicList;
461     }
462     responseCon.wait_for(lock, g_waitForResponse);
463 }
464
465 TEST_F(NotificationProviderServiceTest, ExpectEqualUnSetConsumerTopicsAndGetConsumerTopics)
466 {
467     std::string str1("TEST1");
468     std::string str2("TEST2");
469     OIC::Service::NSProviderService::getInstance()->registerTopic(str1);
470     OIC::Service::NSProviderService::getInstance()->registerTopic(str2);
471
472     ASSERT_NE(nullptr, g_consumer) << "error: discovery failure";
473
474     g_consumer->setTopic(str1);
475     g_consumer->setTopic(str2);
476     g_consumer->unsetTopic(str1);
477
478     std::unique_lock< std::mutex > lock { mutexForCondition };
479     responseCon.wait_for(lock, g_waitForResponse);
480
481     bool isSame = false;
482     OIC::Service::NSTopicsList *topicList =  g_consumer->getConsumerTopicList();
483
484     if (!topicList)
485     {
486         printf("topic is NULL\n");
487         isSame = false;
488     }
489     else
490     {
491         std::string compString[10];
492         int i = 0, state[10] = {0};
493         for (auto itr : topicList->getTopicsList())
494         {
495             compString[i] = itr->getTopicName();
496             state[i++] = (int) itr->getState();
497         }
498         std::cout << compString[0] << std::endl;
499         std::cout << compString[1] << std::endl;
500         if (str1.compare(compString[0]) == 0 && str2.compare(compString[1]) == 0
501             && state[0] == 0 &&  state[1] == 1)
502         {
503             isSame = true;
504         }
505     }
506
507     EXPECT_EQ(isSame, true);
508
509     OIC::Service::NSProviderService::getInstance()->unregisterTopic(str1);
510     OIC::Service::NSProviderService::getInstance()->unregisterTopic(str2);
511
512     if (topicList != nullptr)
513     {
514         delete topicList;
515     }
516     responseCon.wait_for(lock, g_waitForResponse);
517 }
518
519 TEST_F(NotificationProviderServiceTest, ExpectFailSendMessage)
520 {
521     OIC::Service::NSResult result = OIC::Service::NSResult::OK;
522     result = OIC::Service::NSProviderService::getInstance()->sendMessage(nullptr);
523
524     EXPECT_EQ(result, OIC::Service::NSResult::ERROR);
525 }
526
527 TEST_F(NotificationProviderServiceTest, CancelObserves)
528 {
529     bool ret = g_consumerSimul.cancelObserves();
530
531     std::unique_lock< std::mutex > lock { mutexForCondition };
532     responseCon.wait_for(lock, g_waitForResponse);
533
534     EXPECT_EQ(ret, true);
535 }