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