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 std::shared_ptr<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(std::shared_ptr<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 []( std::shared_ptr<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);
225 OIC::Service::NSMessage msg = OIC::Service::NSProviderService::getInstance()->createMessage();
226 msgID = (int)msg.getMessageId();
227 msg.setTitle(std::string("Title"));
228 msg.setContentText(std::string("ContentText"));
229 msg.setSourceName(std::string("OCF"));
231 OIC::Service::NSProviderService::getInstance()->sendMessage(msg);
233 std::unique_lock< std::mutex > lock { mutexForCondition };
234 responseCon.wait_for(lock, g_waitForResponse);
237 std::unique_lock< std::mutex > lock { mutexForCondition };
238 responseCon.wait_for(lock, g_waitForResponse);
240 EXPECT_EQ(expectTrue, true);
243 TEST_F(NotificationProviderServiceTest, ExpectCallNotifyOnConsumerByAcceptIsTrue)
247 ASSERT_NE(nullptr, g_consumer) << "error: discovery failure";
249 mocks.ExpectCallFunc(MessageCallbackFromConsumerEmpty).Do(
250 [&msgID](const int &id, const std::string &, const std::string &, const std::string &)
254 std::cout << "ExpectCallNotifyOnConsumerByAcceptIsTrue" << std::endl;
255 responseCon.notify_all();
259 g_consumer->acceptSubscription(true);
261 OIC::Service::NSMessage msg = OIC::Service::NSProviderService::getInstance()->createMessage();
262 msgID = (int)msg.getMessageId();
263 msg.setTitle(std::string("Title"));
264 msg.setContentText(std::string("ContentText"));
265 msg.setSourceName(std::string("OCF"));
267 OIC::Service::NSProviderService::getInstance()->sendMessage(msg);
268 std::unique_lock< std::mutex > lock { mutexForCondition };
269 responseCon.wait_for(lock, g_waitForResponse);
273 TEST_F(NotificationProviderServiceTest, ExpectCallbackSyncOnReadToConsumer)
277 mocks.ExpectCallFunc(SyncCallbackFromConsumerEmpty).Do(
278 [& id](int &type, int &syncId)
280 std::cout << "MessageSynchronizedCallbackEmpty" << std::endl;
282 type == (int)OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ)
284 std::cout << "ExpectCallbackSyncOnReadFromConsumer" << std::endl;
285 responseCon.notify_all();
289 OIC::Service::NSMessage msg = OIC::Service::NSProviderService::getInstance()->createMessage();
290 id = (int)msg.getMessageId();
291 msg.setTitle(std::string("Title"));
292 msg.setContentText(std::string("ContentText"));
293 msg.setSourceName(std::string("OCF"));
295 OIC::Service::NSProviderService::getInstance()->sendSyncInfo(msg.getMessageId(),
296 OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ);
297 std::unique_lock< std::mutex > lock { mutexForCondition };
298 responseCon.wait_for(lock, g_waitForResponse);
301 TEST_F(NotificationProviderServiceTest, ExpectCallbackSyncOnReadFromConsumer)
303 int type = (int)OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ;
305 mocks.ExpectCallFunc(MessageSynchronizedCallbackEmpty).Do(
306 [& id](OIC::Service::NSSyncInfo sync)
308 std::cout << "MessageSynchronizedCallbackEmpty" << std::endl;
309 if ((int)sync.getMessageId() == id
310 && sync.getState() == OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ)
312 std::cout << "ExpectCallbackSyncOnReadFromConsumer" << std::endl;
313 responseCon.notify_all();
317 OIC::Service::NSMessage msg = OIC::Service::NSProviderService::getInstance()->createMessage();
318 id = (int)msg.getMessageId();
319 msg.setTitle(std::string("Title"));
320 msg.setContentText(std::string("ContentText"));
321 msg.setSourceName(std::string("OCF"));
322 g_consumerSimul.syncToProvider(type, id, msg.getProviderId());
323 std::unique_lock< std::mutex > lock { mutexForCondition };
324 responseCon.wait(lock);
327 TEST_F(NotificationProviderServiceTest, ExpectEqualAddedTopicsAndRegisteredTopics)
329 std::string str1("TEST1");
330 std::string str2("TEST2");
331 OIC::Service::NSProviderService::getInstance()->registerTopic(str1);
332 OIC::Service::NSProviderService::getInstance()->registerTopic(str2);
333 std::unique_lock< std::mutex > lock { mutexForCondition };
334 responseCon.wait_for(lock, g_waitForResponse);
337 OIC::Service::NSProviderService::getInstance()->getRegisteredTopicList();
340 printf("topic is NULL\n");
345 std::string compString[10];
347 for (auto itr : topicList->getTopicsList())
349 compString[i] = itr.getTopicName(); i++;
351 std::cout << compString[0] << std::endl;
352 std::cout << compString[1] << std::endl;
353 if (str1.compare(compString[0]) == 0 && str2.compare(compString[1]) == 0)
358 EXPECT_EQ(isSame, true);
360 OIC::Service::NSProviderService::getInstance()->unregisterTopic(str1);
361 OIC::Service::NSProviderService::getInstance()->unregisterTopic(str2);
362 responseCon.wait_for(lock, g_waitForResponse);
365 TEST_F(NotificationProviderServiceTest, ExpectEqualUnregisteredTopicsAndRegisteredTopics)
367 std::string str1("TEST1");
368 std::string str2("TEST2");
369 OIC::Service::NSProviderService::getInstance()->registerTopic(str1);
370 OIC::Service::NSProviderService::getInstance()->registerTopic(str2);
371 OIC::Service::NSProviderService::getInstance()->unregisterTopic(str2);
372 std::unique_lock< std::mutex > lock { mutexForCondition };
373 responseCon.wait_for(lock, g_waitForResponse);
376 OIC::Service::NSProviderService::getInstance()->getRegisteredTopicList();
379 printf("topic is NULL\n");
384 auto topic = topicList->getTopicsList();
385 auto it = topic.begin();
386 std::string compStr = (*it).getTopicName() ;
387 std::cout << compStr << std::endl;
388 if (str1.compare(compStr) == 0 )
393 EXPECT_EQ(isSame, true);
395 OIC::Service::NSProviderService::getInstance()->unregisterTopic(str1);
396 responseCon.wait_for(lock, g_waitForResponse);
399 TEST_F(NotificationProviderServiceTest, ExpectEqualSetConsumerTopicsAndGetConsumerTopics)
401 std::string str1("TEST1");
402 std::string str2("TEST2");
403 OIC::Service::NSProviderService::getInstance()->registerTopic(str1);
404 OIC::Service::NSProviderService::getInstance()->registerTopic(str2);
406 ASSERT_NE(nullptr, g_consumer) << "error: discovery failure";
408 g_consumer->setTopic(str1);
410 std::unique_lock< std::mutex > lock { mutexForCondition };
411 responseCon.wait_for(lock, g_waitForResponse);
414 auto topicList = g_consumer->getConsumerTopicList();
418 printf("topic is NULL\n");
423 std::string compString[10];
424 int i = 0, state[10] = {0};
425 for (auto itr : topicList->getTopicsList())
427 compString[i] = itr.getTopicName();
428 state[i++] = (int) itr.getState();
430 std::cout << compString[0] << std::endl;
431 std::cout << compString[1] << std::endl;
432 if (str1.compare(compString[0]) == 0 && str2.compare(compString[1]) == 0
433 && state[0] == 1 && state[1] == 0)
439 EXPECT_EQ(isSame, true);
441 OIC::Service::NSProviderService::getInstance()->unregisterTopic(str1);
442 OIC::Service::NSProviderService::getInstance()->unregisterTopic(str2);
443 responseCon.wait_for(lock, g_waitForResponse);
446 TEST_F(NotificationProviderServiceTest, ExpectEqualUnSetConsumerTopicsAndGetConsumerTopics)
448 std::string str1("TEST1");
449 std::string str2("TEST2");
450 OIC::Service::NSProviderService::getInstance()->registerTopic(str1);
451 OIC::Service::NSProviderService::getInstance()->registerTopic(str2);
453 ASSERT_NE(nullptr, g_consumer) << "error: discovery failure";
455 g_consumer->setTopic(str1);
456 g_consumer->setTopic(str2);
457 g_consumer->unsetTopic(str1);
459 std::unique_lock< std::mutex > lock { mutexForCondition };
460 responseCon.wait_for(lock, g_waitForResponse);
463 auto topicList = g_consumer->getConsumerTopicList();
467 printf("topic is NULL\n");
472 std::string compString[10];
473 int i = 0, state[10] = {0};
474 for (auto itr : topicList->getTopicsList())
476 compString[i] = itr.getTopicName();
477 state[i++] = (int) itr.getState();
479 std::cout << compString[0] << std::endl;
480 std::cout << compString[1] << std::endl;
481 if (str1.compare(compString[0]) == 0 && str2.compare(compString[1]) == 0
482 && state[0] == 0 && state[1] == 1)
488 EXPECT_EQ(isSame, true);
490 OIC::Service::NSProviderService::getInstance()->unregisterTopic(str1);
491 OIC::Service::NSProviderService::getInstance()->unregisterTopic(str2);
492 responseCon.wait_for(lock, g_waitForResponse);
496 TEST_F(NotificationProviderServiceTest, CancelObserves)
498 bool ret = g_consumerSimul.cancelObserves();
500 std::unique_lock< std::mutex > lock { mutexForCondition };
501 responseCon.wait_for(lock, g_waitForResponse);
503 EXPECT_EQ(ret, true);