replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / notification / cpp-wrapper / unittest / NSProviderServiceTest.cpp
old mode 100644 (file)
new mode 100755 (executable)
index a9acd13..7b6cd1c
@@ -43,7 +43,7 @@ namespace
     std::mutex mutexForCondition;
 
     NSConsumerSimulator g_consumerSimul;
-    OIC::Service::NSConsumer *g_consumer;
+    std::shared_ptr<OIC::Service::NSConsumer> g_consumer;
 }
 
 class TestWithMock: public testing::Test
@@ -52,7 +52,9 @@ class TestWithMock: public testing::Test
         MockRepository mocks;
 
     protected:
-        virtual ~TestWithMock() noexcept(noexcept(std::declval<Test>().~Test())) {}
+        virtual ~TestWithMock() noexcept(noexcept(std::declval<Test>().~Test()))
+        {
+        }
 
         virtual void TearDown()
         {
@@ -74,12 +76,12 @@ class NotificationProviderServiceTest : public TestWithMock
         NotificationProviderServiceTest() = default;
         ~NotificationProviderServiceTest() = default;
 
-        static void ConsumerSubscribedCallbackEmpty(OIC::Service::NSConsumer *)
+        static void ConsumerSubscribedCallbackEmpty(std::shared_ptr<OIC::Service::NSConsumer> )
         {
             std::cout << __func__ << std::endl;
         }
 
-        static void MessageSynchronizedCallbackEmpty(OIC::Service::NSSyncInfo *)
+        static void MessageSynchronizedCallbackEmpty(OIC::Service::NSSyncInfo)
         {
             std::cout << __func__ << std::endl;
         }
@@ -171,7 +173,7 @@ TEST_F(NotificationProviderServiceTest,
 {
     g_consumer = NULL;
     mocks.ExpectCallFunc(ConsumerSubscribedCallbackEmpty).Do(
-        []( OIC::Service::NSConsumer * consumer)
+        []( std::shared_ptr<OIC::Service::NSConsumer> consumer)
     {
         std::cout << "ConsumerSubscribedCallbackEmpty" << std::endl;
         g_consumer = consumer;
@@ -197,16 +199,16 @@ TEST_F(NotificationProviderServiceTest,
     std::unique_lock< std::mutex > lock { mutexForCondition };
     responseCon.wait_for(lock, g_waitForResponse);
 
-    EXPECT_NE((void*)g_consumer, (void*)NULL);
+    ASSERT_NE(nullptr, g_consumer) << "error: discovery failure";
 }
 
 TEST_F(NotificationProviderServiceTest, NeverCallNotifyOnConsumerByAcceptIsFalse)
 {
     bool expectTrue = true;
-    int msgID;
+    int msgID = 0;
 
     mocks.OnCallFunc(MessageCallbackFromConsumerEmpty).Do(
-        [& expectTrue, &msgID](const int & id, const std::string &, const std::string &,
+        [& expectTrue, &msgID](const int &id, const std::string &, const std::string &,
                                const std::string &)
     {
         if (id == msgID)
@@ -217,13 +219,14 @@ TEST_F(NotificationProviderServiceTest, NeverCallNotifyOnConsumerByAcceptIsFalse
         responseCon.notify_all();
     });
 
-    g_consumer->acceptSubscription(false);
+    ASSERT_NE(nullptr, g_consumer) << "error: discovery failure";
 
-    OIC::Service::NSMessage *msg =  OIC::Service::NSProviderService::getInstance()->createMessage();
-    msgID = (int)msg->getMessageId();
-    msg->setTitle(std::string("Title"));
-    msg->setContentText(std::string("ContentText"));
-    msg->setSourceName(std::string("OCF"));
+    g_consumer->acceptSubscription(false);
+    OIC::Service::NSMessage msg =  OIC::Service::NSProviderService::getInstance()->createMessage();
+    msgID = (int)msg.getMessageId();
+    msg.setTitle(std::string("Title"));
+    msg.setContentText(std::string("ContentText"));
+    msg.setSourceName(std::string("OCF"));
 
     OIC::Service::NSProviderService::getInstance()->sendMessage(msg);
     {
@@ -239,10 +242,12 @@ TEST_F(NotificationProviderServiceTest, NeverCallNotifyOnConsumerByAcceptIsFalse
 
 TEST_F(NotificationProviderServiceTest, ExpectCallNotifyOnConsumerByAcceptIsTrue)
 {
-    int msgID;
+    int msgID = 0;
+
+    ASSERT_NE(nullptr, g_consumer) << "error: discovery failure";
 
     mocks.ExpectCallFunc(MessageCallbackFromConsumerEmpty).Do(
-        [&msgID](const int & id, const std::string &, const std::string &, const std::string &)
+        [&msgID](const int &id, const std::string &, const std::string &, const std::string &)
     {
         if (id == msgID)
         {
@@ -253,23 +258,24 @@ TEST_F(NotificationProviderServiceTest, ExpectCallNotifyOnConsumerByAcceptIsTrue
 
     g_consumer->acceptSubscription(true);
 
-    OIC::Service::NSMessage *msg =  OIC::Service::NSProviderService::getInstance()->createMessage();
-    msgID = (int)msg->getMessageId();
-    msg->setTitle(std::string("Title"));
-    msg->setContentText(std::string("ContentText"));
-    msg->setSourceName(std::string("OCF"));
+    OIC::Service::NSMessage msg =  OIC::Service::NSProviderService::getInstance()->createMessage();
+    msgID = (int)msg.getMessageId();
+    msg.setTitle(std::string("Title"));
+    msg.setContentText(std::string("ContentText"));
+    msg.setSourceName(std::string("OCF"));
 
     OIC::Service::NSProviderService::getInstance()->sendMessage(msg);
     std::unique_lock< std::mutex > lock { mutexForCondition };
     responseCon.wait_for(lock, g_waitForResponse);
+
 }
 
 TEST_F(NotificationProviderServiceTest, ExpectCallbackSyncOnReadToConsumer)
 {
-    int id;
+    int id = 0;
 
     mocks.ExpectCallFunc(SyncCallbackFromConsumerEmpty).Do(
-        [& id](int & type, int & syncId)
+        [& id](int &type, int &syncId)
     {
         std::cout << "MessageSynchronizedCallbackEmpty" << std::endl;
         if (syncId == id &&
@@ -280,13 +286,13 @@ TEST_F(NotificationProviderServiceTest, ExpectCallbackSyncOnReadToConsumer)
         }
     });
 
-    OIC::Service::NSMessage *msg =  OIC::Service::NSProviderService::getInstance()->createMessage();
-    id = (int)msg->getMessageId();
-    msg->setTitle(std::string("Title"));
-    msg->setContentText(std::string("ContentText"));
-    msg->setSourceName(std::string("OCF"));
+    OIC::Service::NSMessage msg =  OIC::Service::NSProviderService::getInstance()->createMessage();
+    id = (int)msg.getMessageId();
+    msg.setTitle(std::string("Title"));
+    msg.setContentText(std::string("ContentText"));
+    msg.setSourceName(std::string("OCF"));
 
-    OIC::Service::NSProviderService::getInstance()->sendSyncInfo(msg->getMessageId(),
+    OIC::Service::NSProviderService::getInstance()->sendSyncInfo(msg.getMessageId(),
             OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ);
     std::unique_lock< std::mutex > lock { mutexForCondition };
     responseCon.wait_for(lock, g_waitForResponse);
@@ -295,25 +301,25 @@ TEST_F(NotificationProviderServiceTest, ExpectCallbackSyncOnReadToConsumer)
 TEST_F(NotificationProviderServiceTest, ExpectCallbackSyncOnReadFromConsumer)
 {
     int type = (int)OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ;
-    int id;
+    int id = 0;
     mocks.ExpectCallFunc(MessageSynchronizedCallbackEmpty).Do(
-        [& id](OIC::Service::NSSyncInfo sync)
+        [& id](OIC::Service::NSSyncInfo sync)
     {
         std::cout << "MessageSynchronizedCallbackEmpty" << std::endl;
-        if ((int)sync->getMessageId() == id
-            && sync->getState() == OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ)
+        if ((int)sync.getMessageId() == id
+            && sync.getState() == OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ)
         {
             std::cout << "ExpectCallbackSyncOnReadFromConsumer" << std::endl;
             responseCon.notify_all();
         }
     });
 
-    OIC::Service::NSMessage *msg =  OIC::Service::NSProviderService::getInstance()->createMessage();
-    id = (int)msg->getMessageId();
-    msg->setTitle(std::string("Title"));
-    msg->setContentText(std::string("ContentText"));
-    msg->setSourceName(std::string("OCF"));
-    g_consumerSimul.syncToProvider(type, id, msg->getProviderId());
+    OIC::Service::NSMessage msg =  OIC::Service::NSProviderService::getInstance()->createMessage();
+    id = (int)msg.getMessageId();
+    msg.setTitle(std::string("Title"));
+    msg.setContentText(std::string("ContentText"));
+    msg.setSourceName(std::string("OCF"));
+    g_consumerSimul.syncToProvider(type, id, msg.getProviderId());
     std::unique_lock< std::mutex > lock { mutexForCondition };
     responseCon.wait(lock);
 }
@@ -327,7 +333,7 @@ TEST_F(NotificationProviderServiceTest, ExpectEqualAddedTopicsAndRegisteredTopic
     std::unique_lock< std::mutex > lock { mutexForCondition };
     responseCon.wait_for(lock, g_waitForResponse);
     bool isSame = false;
-    OIC::Service::NSTopicsList *topicList =
+    auto topicList =
         OIC::Service::NSProviderService::getInstance()->getRegisteredTopicList();
     if (!topicList)
     {
@@ -340,7 +346,7 @@ TEST_F(NotificationProviderServiceTest, ExpectEqualAddedTopicsAndRegisteredTopic
         int i = 0;
         for (auto itr : topicList->getTopicsList())
         {
-            compString[i] = itr->getTopicName(); i++;
+            compString[i] = itr.getTopicName(); i++;
         }
         std::cout << compString[0] << std::endl;
         std::cout << compString[1] << std::endl;
@@ -366,7 +372,7 @@ TEST_F(NotificationProviderServiceTest, ExpectEqualUnregisteredTopicsAndRegister
     std::unique_lock< std::mutex > lock { mutexForCondition };
     responseCon.wait_for(lock, g_waitForResponse);
     bool isSame = false;
-    OIC::Service::NSTopicsList *topicList =
+    auto topicList =
         OIC::Service::NSProviderService::getInstance()->getRegisteredTopicList();
     if (!topicList)
     {
@@ -375,8 +381,9 @@ TEST_F(NotificationProviderServiceTest, ExpectEqualUnregisteredTopicsAndRegister
     }
     else
     {
-        std::list<OIC::Service::NSTopic *>::iterator it = topicList->getTopicsList().begin();
-        std::string compStr = (*it)->getTopicName() ;
+        auto topic = topicList->getTopicsList();
+        auto it = topic.begin();
+        std::string compStr = (*it).getTopicName() ;
         std::cout << compStr << std::endl;
         if (str1.compare(compStr) == 0 )
         {
@@ -395,13 +402,16 @@ TEST_F(NotificationProviderServiceTest, ExpectEqualSetConsumerTopicsAndGetConsum
     std::string str2("TEST2");
     OIC::Service::NSProviderService::getInstance()->registerTopic(str1);
     OIC::Service::NSProviderService::getInstance()->registerTopic(str2);
+
+    ASSERT_NE(nullptr, g_consumer) << "error: discovery failure";
+
     g_consumer->setTopic(str1);
 
     std::unique_lock< std::mutex > lock { mutexForCondition };
     responseCon.wait_for(lock, g_waitForResponse);
 
     bool isSame = false;
-    OIC::Service::NSTopicsList *topicList =  g_consumer->getConsumerTopicList();
+    auto topicList =  g_consumer->getConsumerTopicList();
 
     if (!topicList)
     {
@@ -411,11 +421,11 @@ TEST_F(NotificationProviderServiceTest, ExpectEqualSetConsumerTopicsAndGetConsum
     else
     {
         std::string compString[10];
-        int i = 0, state[10];
+        int i = 0, state[10] = {0};
         for (auto itr : topicList->getTopicsList())
         {
-            compString[i] = itr->getTopicName();
-            state[i++] = (int) itr->getState();
+            compString[i] = itr.getTopicName();
+            state[i++] = (int) itr.getState();
         }
         std::cout << compString[0] << std::endl;
         std::cout << compString[1] << std::endl;
@@ -439,6 +449,9 @@ TEST_F(NotificationProviderServiceTest, ExpectEqualUnSetConsumerTopicsAndGetCons
     std::string str2("TEST2");
     OIC::Service::NSProviderService::getInstance()->registerTopic(str1);
     OIC::Service::NSProviderService::getInstance()->registerTopic(str2);
+
+    ASSERT_NE(nullptr, g_consumer) << "error: discovery failure";
+
     g_consumer->setTopic(str1);
     g_consumer->setTopic(str2);
     g_consumer->unsetTopic(str1);
@@ -447,7 +460,7 @@ TEST_F(NotificationProviderServiceTest, ExpectEqualUnSetConsumerTopicsAndGetCons
     responseCon.wait_for(lock, g_waitForResponse);
 
     bool isSame = false;
-    OIC::Service::NSTopicsList *topicList =  g_consumer->getConsumerTopicList();
+    auto topicList =  g_consumer->getConsumerTopicList();
 
     if (!topicList)
     {
@@ -457,11 +470,11 @@ TEST_F(NotificationProviderServiceTest, ExpectEqualUnSetConsumerTopicsAndGetCons
     else
     {
         std::string compString[10];
-        int i = 0, state[10];
+        int i = 0, state[10] = {0};
         for (auto itr : topicList->getTopicsList())
         {
-            compString[i] = itr->getTopicName();
-            state[i++] = (int) itr->getState();
+            compString[i] = itr.getTopicName();
+            state[i++] = (int) itr.getState();
         }
         std::cout << compString[0] << std::endl;
         std::cout << compString[1] << std::endl;
@@ -479,13 +492,6 @@ TEST_F(NotificationProviderServiceTest, ExpectEqualUnSetConsumerTopicsAndGetCons
     responseCon.wait_for(lock, g_waitForResponse);
 }
 
-TEST_F(NotificationProviderServiceTest, ExpectFailSendMessage)
-{
-    OIC::Service::NSResult result = OIC::Service::NSResult::OK;
-    result = OIC::Service::NSProviderService::getInstance()->sendMessage(nullptr);
-
-    EXPECT_EQ(result, OIC::Service::NSResult::ERROR);
-}
 
 TEST_F(NotificationProviderServiceTest, CancelObserves)
 {