1 //******************************************************************
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include <gtest/gtest.h>
22 #include <HippoMocks/hippomocks.h>
25 #include <condition_variable>
29 #include "NSProviderService.h"
30 #include "NSConsumerServiceSimulator.h"
32 #include "NSSyncInfo.h"
33 #include "NSMessage.h"
34 #include "NSMediaContents.h"
38 std::atomic_bool g_isStartedStack(false);
40 std::chrono::milliseconds g_waitForResponse(500);
42 std::condition_variable responseCon;
43 std::mutex mutexForCondition;
45 NSConsumerSimulator g_consumerSimul;
46 OIC::Service::NSConsumer *g_consumer;
49 class TestWithMock: public testing::Test
55 virtual ~TestWithMock() noexcept(noexcept(std::declval<Test>().~Test())) {}
57 virtual void TearDown()
71 class NotificationProviderServiceTest : public TestWithMock
74 NotificationProviderServiceTest() = default;
75 ~NotificationProviderServiceTest() = default;
77 static void ConsumerSubscribedCallbackEmpty(OIC::Service::NSConsumer *)
79 std::cout << __func__ << std::endl;
82 static void MessageSynchronizedCallbackEmpty(OIC::Service::NSSyncInfo *)
84 std::cout << __func__ << std::endl;
87 static void MessageCallbackFromConsumerEmpty(
88 const int &, const std::string &, const std::string &, const std::string &)
90 std::cout << __func__ << std::endl;
93 static void SyncCallbackFromConsumerEmpty(int, int)
95 std::cout << __func__ << std::endl;
102 TestWithMock::SetUp();
104 if (g_isStartedStack == false)
106 OC::PlatformConfig cfg
108 OC::ServiceType::InProc,
112 OC::QualityOfService::HighQos
114 OC::OCPlatform::Configure(cfg);
118 OC::OCPlatform::stopPresence();
125 g_isStartedStack = true;
132 TestWithMock::TearDown();
137 TEST_F(NotificationProviderServiceTest, StartProviderPositiveWithPolicyTrue)
139 OIC::Service::NSProviderService::ProviderConfig config;
140 config.m_subscribeRequestCb = ConsumerSubscribedCallbackEmpty;
141 config.m_syncInfoCb = MessageSynchronizedCallbackEmpty;
142 config.subControllability = true;
144 OIC::Service::NSResult ret = OIC::Service::NSProviderService::getInstance()->start(config);
146 EXPECT_EQ(ret, OIC::Service::NSResult::OK);
149 TEST_F(NotificationProviderServiceTest, StopProviderPositive)
151 OIC::Service::NSResult ret = OIC::Service::NSProviderService::getInstance()->stop();
153 EXPECT_EQ(ret, OIC::Service::NSResult::OK);
156 TEST_F(NotificationProviderServiceTest, StartProviderPositiveWithPolicyFalse)
158 OIC::Service::NSProviderService::ProviderConfig config;
159 config.m_subscribeRequestCb = ConsumerSubscribedCallbackEmpty;
160 config.m_syncInfoCb = MessageSynchronizedCallbackEmpty;
161 config.subControllability = false;
163 OIC::Service::NSResult ret = OIC::Service::NSProviderService::getInstance()->start(config);
165 EXPECT_EQ(ret, OIC::Service::NSResult::OK);
166 OIC::Service::NSProviderService::getInstance()->stop();
169 TEST_F(NotificationProviderServiceTest,
170 ExpectCallbackWhenReceiveSubscribeRequestWithAccepterProvider)
173 mocks.ExpectCallFunc(ConsumerSubscribedCallbackEmpty).Do(
174 []( OIC::Service::NSConsumer * consumer)
176 std::cout << "ConsumerSubscribedCallbackEmpty" << std::endl;
177 g_consumer = consumer;
178 responseCon.notify_all();
181 OIC::Service::NSProviderService::ProviderConfig config;
182 config.m_subscribeRequestCb = ConsumerSubscribedCallbackEmpty;
183 config.m_syncInfoCb = MessageSynchronizedCallbackEmpty;
184 config.subControllability = true;
186 OIC::Service::NSProviderService::getInstance()->start(config);
189 std::unique_lock< std::mutex > lock { mutexForCondition };
190 responseCon.wait_for(lock, g_waitForResponse);
193 g_consumerSimul.setCallback(MessageCallbackFromConsumerEmpty,
194 SyncCallbackFromConsumerEmpty);
195 g_consumerSimul.findProvider();
197 std::unique_lock< std::mutex > lock { mutexForCondition };
198 responseCon.wait_for(lock, g_waitForResponse);
200 EXPECT_NE((void*)g_consumer, (void*)NULL);
203 TEST_F(NotificationProviderServiceTest, NeverCallNotifyOnConsumerByAcceptIsFalse)
205 bool expectTrue = true;
208 mocks.OnCallFunc(MessageCallbackFromConsumerEmpty).Do(
209 [& expectTrue, &msgID](const int & id, const std::string &, const std::string &,
214 std::cout << "This function never call" << std::endl;
217 responseCon.notify_all();
220 g_consumer->acceptSubscription(false);
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"));
228 OIC::Service::NSProviderService::getInstance()->sendMessage(msg);
230 std::unique_lock< std::mutex > lock { mutexForCondition };
231 responseCon.wait_for(lock, g_waitForResponse);
234 std::unique_lock< std::mutex > lock { mutexForCondition };
235 responseCon.wait_for(lock, g_waitForResponse);
237 EXPECT_EQ(expectTrue, true);
240 TEST_F(NotificationProviderServiceTest, ExpectCallNotifyOnConsumerByAcceptIsTrue)
244 mocks.ExpectCallFunc(MessageCallbackFromConsumerEmpty).Do(
245 [&msgID](const int & id, const std::string &, const std::string &, const std::string &)
249 std::cout << "ExpectCallNotifyOnConsumerByAcceptIsTrue" << std::endl;
250 responseCon.notify_all();
254 g_consumer->acceptSubscription(true);
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"));
262 OIC::Service::NSProviderService::getInstance()->sendMessage(msg);
263 std::unique_lock< std::mutex > lock { mutexForCondition };
264 responseCon.wait_for(lock, g_waitForResponse);
267 TEST_F(NotificationProviderServiceTest, ExpectCallbackSyncOnReadToConsumer)
271 mocks.ExpectCallFunc(SyncCallbackFromConsumerEmpty).Do(
272 [& id](int & type, int & syncId)
274 std::cout << "MessageSynchronizedCallbackEmpty" << std::endl;
276 type == (int)OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ)
278 std::cout << "ExpectCallbackSyncOnReadFromConsumer" << std::endl;
279 responseCon.notify_all();
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"));
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);
295 TEST_F(NotificationProviderServiceTest, ExpectCallbackSyncOnReadFromConsumer)
297 int type = (int)OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ;
299 mocks.ExpectCallFunc(MessageSynchronizedCallbackEmpty).Do(
300 [& id](OIC::Service::NSSyncInfo * sync)
302 std::cout << "MessageSynchronizedCallbackEmpty" << std::endl;
303 if ((int)sync->getMessageId() == id
304 && sync->getState() == OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ)
306 std::cout << "ExpectCallbackSyncOnReadFromConsumer" << std::endl;
307 responseCon.notify_all();
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);
321 TEST_F(NotificationProviderServiceTest, ExpectEqualAddedTopicsAndRegisteredTopics)
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);
330 OIC::Service::NSTopicsList *topicList =
331 OIC::Service::NSProviderService::getInstance()->getRegisteredTopicList();
334 printf("topic is NULL\n");
339 std::string compString[10];
341 for (auto itr : topicList->getTopicsList())
343 compString[i] = itr->getTopicName(); i++;
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)
352 EXPECT_EQ(isSame, true);
354 OIC::Service::NSProviderService::getInstance()->unregisterTopic(str1);
355 OIC::Service::NSProviderService::getInstance()->unregisterTopic(str2);
356 responseCon.wait_for(lock, g_waitForResponse);
359 TEST_F(NotificationProviderServiceTest, ExpectEqualUnregisteredTopicsAndRegisteredTopics)
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);
369 OIC::Service::NSTopicsList *topicList =
370 OIC::Service::NSProviderService::getInstance()->getRegisteredTopicList();
373 printf("topic is NULL\n");
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 )
386 EXPECT_EQ(isSame, true);
388 OIC::Service::NSProviderService::getInstance()->unregisterTopic(str1);
389 responseCon.wait_for(lock, g_waitForResponse);
392 TEST_F(NotificationProviderServiceTest, ExpectEqualSetConsumerTopicsAndGetConsumerTopics)
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);
400 std::unique_lock< std::mutex > lock { mutexForCondition };
401 responseCon.wait_for(lock, g_waitForResponse);
404 OIC::Service::NSTopicsList *topicList = g_consumer->getConsumerTopicList();
408 printf("topic is NULL\n");
413 std::string compString[10];
414 int i = 0, state[10];
415 for (auto itr : topicList->getTopicsList())
417 compString[i] = itr->getTopicName();
418 state[i++] = (int) itr->getState();
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)
429 EXPECT_EQ(isSame, true);
431 OIC::Service::NSProviderService::getInstance()->unregisterTopic(str1);
432 OIC::Service::NSProviderService::getInstance()->unregisterTopic(str2);
433 responseCon.wait_for(lock, g_waitForResponse);
436 TEST_F(NotificationProviderServiceTest, ExpectEqualUnSetConsumerTopicsAndGetConsumerTopics)
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);
446 std::unique_lock< std::mutex > lock { mutexForCondition };
447 responseCon.wait_for(lock, g_waitForResponse);
450 OIC::Service::NSTopicsList *topicList = g_consumer->getConsumerTopicList();
454 printf("topic is NULL\n");
459 std::string compString[10];
460 int i = 0, state[10];
461 for (auto itr : topicList->getTopicsList())
463 compString[i] = itr->getTopicName();
464 state[i++] = (int) itr->getState();
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)
475 EXPECT_EQ(isSame, true);
477 OIC::Service::NSProviderService::getInstance()->unregisterTopic(str1);
478 OIC::Service::NSProviderService::getInstance()->unregisterTopic(str2);
479 responseCon.wait_for(lock, g_waitForResponse);
482 TEST_F(NotificationProviderServiceTest, ExpectFailSendMessage)
484 OIC::Service::NSResult result = OIC::Service::NSResult::OK;
485 result = OIC::Service::NSProviderService::getInstance()->sendMessage(nullptr);
487 EXPECT_EQ(result, OIC::Service::NSResult::ERROR);
490 TEST_F(NotificationProviderServiceTest, CancelObserves)
492 bool ret = g_consumerSimul.cancelObserves();
494 std::unique_lock< std::mutex > lock { mutexForCondition };
495 responseCon.wait_for(lock, g_waitForResponse);
497 EXPECT_EQ(ret, true);