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()))
59 virtual void TearDown()
73 class NotificationProviderServiceTest : public TestWithMock
76 NotificationProviderServiceTest() = default;
77 ~NotificationProviderServiceTest() = default;
79 static void ConsumerSubscribedCallbackEmpty(OIC::Service::NSConsumer *)
81 std::cout << __func__ << std::endl;
84 static void MessageSynchronizedCallbackEmpty(OIC::Service::NSSyncInfo *)
86 std::cout << __func__ << std::endl;
89 static void MessageCallbackFromConsumerEmpty(
90 const int &, const std::string &, const std::string &, const std::string &)
92 std::cout << __func__ << std::endl;
95 static void SyncCallbackFromConsumerEmpty(int, int)
97 std::cout << __func__ << std::endl;
104 TestWithMock::SetUp();
106 if (g_isStartedStack == false)
108 OC::PlatformConfig cfg
110 OC::ServiceType::InProc,
114 OC::QualityOfService::HighQos
116 OC::OCPlatform::Configure(cfg);
120 OC::OCPlatform::stopPresence();
127 g_isStartedStack = true;
134 TestWithMock::TearDown();
139 TEST_F(NotificationProviderServiceTest, StartProviderPositiveWithPolicyTrue)
141 OIC::Service::NSProviderService::ProviderConfig config;
142 config.m_subscribeRequestCb = ConsumerSubscribedCallbackEmpty;
143 config.m_syncInfoCb = MessageSynchronizedCallbackEmpty;
144 config.subControllability = true;
146 OIC::Service::NSResult ret = OIC::Service::NSProviderService::getInstance()->start(config);
148 EXPECT_EQ(ret, OIC::Service::NSResult::OK);
151 TEST_F(NotificationProviderServiceTest, StopProviderPositive)
153 OIC::Service::NSResult ret = OIC::Service::NSProviderService::getInstance()->stop();
155 EXPECT_EQ(ret, OIC::Service::NSResult::OK);
158 TEST_F(NotificationProviderServiceTest, StartProviderPositiveWithPolicyFalse)
160 OIC::Service::NSProviderService::ProviderConfig config;
161 config.m_subscribeRequestCb = ConsumerSubscribedCallbackEmpty;
162 config.m_syncInfoCb = MessageSynchronizedCallbackEmpty;
163 config.subControllability = false;
165 OIC::Service::NSResult ret = OIC::Service::NSProviderService::getInstance()->start(config);
167 EXPECT_EQ(ret, OIC::Service::NSResult::OK);
168 OIC::Service::NSProviderService::getInstance()->stop();
171 TEST_F(NotificationProviderServiceTest,
172 ExpectCallbackWhenReceiveSubscribeRequestWithAccepterProvider)
175 mocks.ExpectCallFunc(ConsumerSubscribedCallbackEmpty).Do(
176 []( OIC::Service::NSConsumer * consumer)
178 std::cout << "ConsumerSubscribedCallbackEmpty" << std::endl;
179 g_consumer = consumer;
180 responseCon.notify_all();
183 OIC::Service::NSProviderService::ProviderConfig config;
184 config.m_subscribeRequestCb = ConsumerSubscribedCallbackEmpty;
185 config.m_syncInfoCb = MessageSynchronizedCallbackEmpty;
186 config.subControllability = true;
188 OIC::Service::NSProviderService::getInstance()->start(config);
191 std::unique_lock< std::mutex > lock { mutexForCondition };
192 responseCon.wait_for(lock, g_waitForResponse);
195 g_consumerSimul.setCallback(MessageCallbackFromConsumerEmpty,
196 SyncCallbackFromConsumerEmpty);
197 g_consumerSimul.findProvider();
199 std::unique_lock< std::mutex > lock { mutexForCondition };
200 responseCon.wait_for(lock, g_waitForResponse);
202 ASSERT_NE(nullptr, g_consumer) << "error: discovery failure";
205 TEST_F(NotificationProviderServiceTest, NeverCallNotifyOnConsumerByAcceptIsFalse)
207 bool expectTrue = true;
210 mocks.OnCallFunc(MessageCallbackFromConsumerEmpty).Do(
211 [& expectTrue, &msgID](const int &id, const std::string &, const std::string &,
216 std::cout << "This function never call" << std::endl;
219 responseCon.notify_all();
222 ASSERT_NE(nullptr, g_consumer) << "error: discovery failure";
224 g_consumer->acceptSubscription(false);
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"));
232 OIC::Service::NSProviderService::getInstance()->sendMessage(msg);
234 std::unique_lock< std::mutex > lock { mutexForCondition };
235 responseCon.wait_for(lock, g_waitForResponse);
238 std::unique_lock< std::mutex > lock { mutexForCondition };
239 responseCon.wait_for(lock, g_waitForResponse);
243 EXPECT_EQ(expectTrue, true);
246 TEST_F(NotificationProviderServiceTest, ExpectCallNotifyOnConsumerByAcceptIsTrue)
250 ASSERT_NE(nullptr, g_consumer) << "error: discovery failure";
252 mocks.ExpectCallFunc(MessageCallbackFromConsumerEmpty).Do(
253 [&msgID](const int &id, const std::string &, const std::string &, const std::string &)
257 std::cout << "ExpectCallNotifyOnConsumerByAcceptIsTrue" << std::endl;
258 responseCon.notify_all();
262 g_consumer->acceptSubscription(true);
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"));
270 OIC::Service::NSProviderService::getInstance()->sendMessage(msg);
271 std::unique_lock< std::mutex > lock { mutexForCondition };
272 responseCon.wait_for(lock, g_waitForResponse);
277 TEST_F(NotificationProviderServiceTest, ExpectCallbackSyncOnReadToConsumer)
281 mocks.ExpectCallFunc(SyncCallbackFromConsumerEmpty).Do(
282 [& id](int &type, int &syncId)
284 std::cout << "MessageSynchronizedCallbackEmpty" << std::endl;
286 type == (int)OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ)
288 std::cout << "ExpectCallbackSyncOnReadFromConsumer" << std::endl;
289 responseCon.notify_all();
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"));
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);
307 TEST_F(NotificationProviderServiceTest, ExpectCallbackSyncOnReadFromConsumer)
309 int type = (int)OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ;
311 mocks.ExpectCallFunc(MessageSynchronizedCallbackEmpty).Do(
312 [& id](OIC::Service::NSSyncInfo * sync)
314 std::cout << "MessageSynchronizedCallbackEmpty" << std::endl;
315 if ((int)sync->getMessageId() == id
316 && sync->getState() == OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ)
318 std::cout << "ExpectCallbackSyncOnReadFromConsumer" << std::endl;
319 responseCon.notify_all();
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);
335 TEST_F(NotificationProviderServiceTest, ExpectEqualAddedTopicsAndRegisteredTopics)
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);
344 OIC::Service::NSTopicsList *topicList =
345 OIC::Service::NSProviderService::getInstance()->getRegisteredTopicList();
348 printf("topic is NULL\n");
353 std::string compString[10];
355 for (auto itr : topicList->getTopicsList())
357 compString[i] = itr->getTopicName(); i++;
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)
366 EXPECT_EQ(isSame, true);
368 OIC::Service::NSProviderService::getInstance()->unregisterTopic(str1);
369 OIC::Service::NSProviderService::getInstance()->unregisterTopic(str2);
370 if (topicList != nullptr)
374 responseCon.wait_for(lock, g_waitForResponse);
377 TEST_F(NotificationProviderServiceTest, ExpectEqualUnregisteredTopicsAndRegisteredTopics)
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);
387 OIC::Service::NSTopicsList *topicList =
388 OIC::Service::NSProviderService::getInstance()->getRegisteredTopicList();
391 printf("topic is NULL\n");
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 )
404 EXPECT_EQ(isSame, true);
406 OIC::Service::NSProviderService::getInstance()->unregisterTopic(str1);
407 if (topicList != nullptr)
411 responseCon.wait_for(lock, g_waitForResponse);
414 TEST_F(NotificationProviderServiceTest, ExpectEqualSetConsumerTopicsAndGetConsumerTopics)
416 std::string str1("TEST1");
417 std::string str2("TEST2");
418 OIC::Service::NSProviderService::getInstance()->registerTopic(str1);
419 OIC::Service::NSProviderService::getInstance()->registerTopic(str2);
421 ASSERT_NE(nullptr, g_consumer) << "error: discovery failure";
423 g_consumer->setTopic(str1);
425 std::unique_lock< std::mutex > lock { mutexForCondition };
426 responseCon.wait_for(lock, g_waitForResponse);
429 OIC::Service::NSTopicsList *topicList = g_consumer->getConsumerTopicList();
433 printf("topic is NULL\n");
438 std::string compString[10];
439 int i = 0, state[10] = {0};
440 for (auto itr : topicList->getTopicsList())
442 compString[i] = itr->getTopicName();
443 state[i++] = (int) itr->getState();
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)
454 EXPECT_EQ(isSame, true);
456 OIC::Service::NSProviderService::getInstance()->unregisterTopic(str1);
457 OIC::Service::NSProviderService::getInstance()->unregisterTopic(str2);
458 if (topicList != nullptr)
462 responseCon.wait_for(lock, g_waitForResponse);
465 TEST_F(NotificationProviderServiceTest, ExpectEqualUnSetConsumerTopicsAndGetConsumerTopics)
467 std::string str1("TEST1");
468 std::string str2("TEST2");
469 OIC::Service::NSProviderService::getInstance()->registerTopic(str1);
470 OIC::Service::NSProviderService::getInstance()->registerTopic(str2);
472 ASSERT_NE(nullptr, g_consumer) << "error: discovery failure";
474 g_consumer->setTopic(str1);
475 g_consumer->setTopic(str2);
476 g_consumer->unsetTopic(str1);
478 std::unique_lock< std::mutex > lock { mutexForCondition };
479 responseCon.wait_for(lock, g_waitForResponse);
482 OIC::Service::NSTopicsList *topicList = g_consumer->getConsumerTopicList();
486 printf("topic is NULL\n");
491 std::string compString[10];
492 int i = 0, state[10] = {0};
493 for (auto itr : topicList->getTopicsList())
495 compString[i] = itr->getTopicName();
496 state[i++] = (int) itr->getState();
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)
507 EXPECT_EQ(isSame, true);
509 OIC::Service::NSProviderService::getInstance()->unregisterTopic(str1);
510 OIC::Service::NSProviderService::getInstance()->unregisterTopic(str2);
512 if (topicList != nullptr)
516 responseCon.wait_for(lock, g_waitForResponse);
519 TEST_F(NotificationProviderServiceTest, ExpectFailSendMessage)
521 OIC::Service::NSResult result = OIC::Service::NSResult::OK;
522 result = OIC::Service::NSProviderService::getInstance()->sendMessage(nullptr);
524 EXPECT_EQ(result, OIC::Service::NSResult::ERROR);
527 TEST_F(NotificationProviderServiceTest, CancelObserves)
529 bool ret = g_consumerSimul.cancelObserves();
531 std::unique_lock< std::mutex > lock { mutexForCondition };
532 responseCon.wait_for(lock, g_waitForResponse);
534 EXPECT_EQ(ret, true);