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