replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / notification / cpp-wrapper / unittest / NSConsumerServiceTest.cpp
old mode 100644 (file)
new mode 100755 (executable)
index 3f1deaf..caa1502
@@ -39,7 +39,7 @@
 namespace
 {
     NSProviderSimulator g_providerSimul;
-    OIC::Service::NSProvider *g_provider;
+    std::shared_ptr<OIC::Service::NSProvider> g_provider;
 
     std::atomic_bool g_isStartedStack(false);
 
@@ -87,17 +87,17 @@ class NotificationServiceConsumerTest : public TestWithMock
         NotificationServiceConsumerTest() = default;
         ~NotificationServiceConsumerTest() = default;
 
-        static void ProviderDiscoveredCallbackEmpty( OIC::Service::NSProvider *)
+        static void ProviderDiscoveredCallbackEmpty( std::shared_ptr<OIC::Service::NSProvider> )
         {
             std::cout << __func__ << std::endl;
         }
 
-        static void NotificationReceivedCallbackEmpty( OIC::Service::NSMessage *)
+        static void NotificationReceivedCallbackEmpty( OIC::Service::NSMessage )
         {
             std::cout << __func__ << std::endl;
         }
 
-        static void SyncCallbackEmpty(OIC::Service::NSSyncInfo *)
+        static void SyncCallbackEmpty(OIC::Service::NSSyncInfo)
         {
             std::cout << __func__ << std::endl;
         }
@@ -164,7 +164,7 @@ TEST_F(NotificationServiceConsumerTest, StopConsumerPositive)
 TEST_F(NotificationServiceConsumerTest, DiscoverProviderWithNonAccepterWhenStartedConsumerFirst)
 {
     mocks.ExpectCallFunc(ProviderDiscoveredCallbackEmpty).Do(
-        [this]( OIC::Service::NSProvider * provider)
+        [this]( std::shared_ptr<OIC::Service::NSProvider> provider)
     {
         std::cout << "Call Discovered" << std::endl;
         std::cout << provider->getProviderId() << std::endl;
@@ -193,7 +193,7 @@ TEST_F(NotificationServiceConsumerTest, DiscoverProviderWithNonAccepterWhenStart
     }
 
     mocks.ExpectCallFunc(ProviderDiscoveredCallbackEmpty).Do(
-        [this]( OIC::Service::NSProvider * provider)
+        [this]( std::shared_ptr<OIC::Service::NSProvider> provider)
     {
         std::cout << "Call Discovered" << std::endl;
         g_provider = provider;
@@ -206,13 +206,15 @@ TEST_F(NotificationServiceConsumerTest, DiscoverProviderWithNonAccepterWhenStart
     std::unique_lock< std::mutex > lock { mutexForCondition };
     responseCon.wait_for(lock, g_waitForResponse);
 
+    ASSERT_NE(nullptr, g_provider) << "error: discovery failure";
+
 }
 
 TEST_F(NotificationServiceConsumerTest, DiscoverProviderWithNonAccepterWhenRescan)
 {
     g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_CONSUMER);
     mocks.OnCallFunc(ProviderDiscoveredCallbackEmpty).Do(
-        [this]( OIC::Service::NSProvider * provider)
+        [this]( std::shared_ptr<OIC::Service::NSProvider> provider)
     {
         std::cout << "Call Discovered" << std::endl;
         g_provider = provider;
@@ -237,12 +239,16 @@ TEST_F(NotificationServiceConsumerTest, ExpectSubscribeSuccess)
         responseCon.notify_all();
     });
 
+    ASSERT_NE(nullptr, g_provider) << "error: discovery failure";
+
     g_provider->setListener( (OIC::Service::NSProvider::ProviderStateCallback)
                              ProviderChangedCallbackEmpty,
                              (OIC::Service::NSProvider::MessageReceivedCallback)NotificationReceivedCallbackEmpty,
                              (OIC::Service::NSProvider::SyncInfoReceivedCallback)SyncCallbackEmpty);
     if (!g_provider->isSubscribed())
+    {
         g_provider->subscribe();
+    }
     std::unique_lock< std::mutex > lock { mutexForCondition };
     responseCon.wait_for(lock, g_waitForResponse);
     EXPECT_EQ(OIC::Service::NSProviderState::ALLOW, revState);
@@ -255,9 +261,9 @@ TEST_F(NotificationServiceConsumerTest, ExpectReceiveNotification)
     std::string msg = "msg";
 
     mocks.ExpectCallFunc(NotificationReceivedCallbackEmpty).Do(
-        [this]( OIC::Service::NSMessage message)
+        [this]( OIC::Service::NSMessage message)
     {
-        std::cout << "Income Notification : " << message->getMessageId() << std::endl;
+        std::cout << "Income Notification : " << message.getMessageId() << std::endl;
         responseCon.notify_all();
     });
 
@@ -274,7 +280,7 @@ TEST_F(NotificationServiceConsumerTest, DiscoverProviderWithAccepterisProvider)
     g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_PROVIDER);
 
     mocks.ExpectCallFunc(ProviderDiscoveredCallbackEmpty).Do(
-        [this]( OIC::Service::NSProvider * provider)
+        [this]( std::shared_ptr<OIC::Service::NSProvider> provider)
     {
         std::cout << "Call Discovered" << std::endl;
         g_provider = provider;
@@ -283,7 +289,9 @@ TEST_F(NotificationServiceConsumerTest, DiscoverProviderWithAccepterisProvider)
                                  (OIC::Service::NSProvider::MessageReceivedCallback)NotificationReceivedCallbackEmpty,
                                  (OIC::Service::NSProvider::SyncInfoReceivedCallback)SyncCallbackEmpty);
         if (!g_provider->isSubscribed())
+        {
             g_provider->subscribe();
+        }
         std::cout << g_provider->getProviderId() << std::endl;
         responseCon.notify_all();
     });
@@ -302,10 +310,10 @@ TEST_F(NotificationServiceConsumerTest, ExpectReceiveNotificationWithAccepterisP
     uint64_t revId = 1;
 
     mocks.OnCallFunc(NotificationReceivedCallbackEmpty).Do(
-        [this, & id, & revId](OIC::Service::NSMessage message)
+        [this, & id, & revId](OIC::Service::NSMessage message)
     {
-        std::cout << "Income Notification : " << message->getMessageId() << std::endl;
-        revId =  message->getMessageId();
+        std::cout << "Income Notification : " << message.getMessageId() << std::endl;
+        revId =  message.getMessageId();
         responseCon.notify_all();
     });
 
@@ -325,17 +333,17 @@ TEST_F(NotificationServiceConsumerTest, ExpectCallbackReadCheckWhenProviderNotif
     OIC::Service::NSSyncInfo::NSSyncType type = OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED;
 
     mocks.OnCallFunc(NotificationReceivedCallbackEmpty).Do(
-        [this]( OIC::Service::NSMessage message)
+        [this]( OIC::Service::NSMessage message)
     {
-        std::cout << "Income Notification : " << message->getMessageId() << std::endl;
+        std::cout << "Income Notification : " << message.getMessageId() << std::endl;
     });
 
     mocks.OnCallFunc(SyncCallbackEmpty).Do(
-        [& type, this](OIC::Service::NSSyncInfo sync)
+        [& type, this](OIC::Service::NSSyncInfo sync)
     {
-        std::cout << "Income SyncInfo : " << sync->getMessageId()
-                  << ", State : " << (int) sync->getState() << std::endl;
-        type = sync->getState();
+        std::cout << "Income SyncInfo : " << sync.getMessageId()
+                  << ", State : " << (int) sync.getState() << std::endl;
+        type = sync.getState();
         responseCon.notify_all();
     });
 
@@ -362,17 +370,17 @@ TEST_F(NotificationServiceConsumerTest, ExpectCallbackDismissCheckWhenProviderNo
     OIC::Service::NSSyncInfo::NSSyncType type = OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ;
 
     mocks.OnCallFunc(NotificationReceivedCallbackEmpty).Do(
-        [this]( OIC::Service::NSMessage message)
+        [this]( OIC::Service::NSMessage message)
     {
-        std::cout << "Income Notification : " << message->getMessageId() << std::endl;
+        std::cout << "Income Notification : " << message.getMessageId() << std::endl;
     });
 
     mocks.OnCallFunc(SyncCallbackEmpty).Do(
-        [& type, this](OIC::Service::NSSyncInfo sync)
+        [& type, this](OIC::Service::NSSyncInfo sync)
     {
-        std::cout << "Income Notification : " << sync->getMessageId()
-                  << ", State : " << (int) sync->getState() << std::endl;
-        type = sync->getState();
+        std::cout << "Income Notification : " << sync.getMessageId()
+                  << ", State : " << (int) sync.getState() << std::endl;
+        type = sync.getState();
         responseCon.notify_all();
     });
 
@@ -398,22 +406,24 @@ TEST_F(NotificationServiceConsumerTest, ExpectCallbackReadCheckWhenConsumerPostS
     std::string msg = "msg";
     OIC::Service::NSSyncInfo::NSSyncType type = OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED;
 
+    ASSERT_NE(nullptr, g_provider) << "error: discovery failure";
+
     mocks.OnCallFunc(NotificationReceivedCallbackEmpty).Do(
-        [this]( OIC::Service::NSMessage message)
+        [this]( OIC::Service::NSMessage message)
     {
-        std::cout << "Income Notification : " << message->getMessageId() << std::endl;
-        g_provider->sendSyncInfo(message->getMessageId(),
+        std::cout << "Income Notification : " << message.getMessageId() << std::endl;
+        g_provider->sendSyncInfo(message.getMessageId(),
                                  OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ);
         std::unique_lock< std::mutex > lock { mutexForCondition };
         responseCon.wait_for(lock, g_waitForResponse);
     });
 
     mocks.OnCallFunc(SyncCallbackEmpty).Do(
-        [& type, this](OIC::Service::NSSyncInfo sync)
+        [& type, this](OIC::Service::NSSyncInfo sync)
     {
-        std::cout << "Income Notification : " << sync->getMessageId()
-                  << ", State : " << (int) sync->getState() << std::endl;
-        type = sync->getState();
+        std::cout << "Income Notification : " << sync.getMessageId()
+                  << ", State : " << (int) sync.getState() << std::endl;
+        type = sync.getState();
         responseCon.notify_all();
     });
 
@@ -433,22 +443,24 @@ TEST_F(NotificationServiceConsumerTest, ExpectCallbackDismissCheckWhenConsumerPo
     std::string msg = "msg";
     OIC::Service::NSSyncInfo::NSSyncType type = OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ;
 
+    ASSERT_NE(nullptr, g_provider) << "error: discovery failure";
+
     mocks.OnCallFunc(NotificationReceivedCallbackEmpty).Do(
-        [this]( OIC::Service::NSMessage message)
+        [this]( OIC::Service::NSMessage message)
     {
-        std::cout << "Income Notification : " << message->getMessageId() << std::endl;
-        g_provider->sendSyncInfo(message->getMessageId(),
+        std::cout << "Income Notification : " << message.getMessageId() << std::endl;
+        g_provider->sendSyncInfo(message.getMessageId(),
                                  OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED);
         std::unique_lock< std::mutex > lock { mutexForCondition };
         responseCon.wait_for(lock, g_waitForResponse);
     });
 
     mocks.OnCallFunc(SyncCallbackEmpty).Do(
-        [& type, this](OIC::Service::NSSyncInfo sync)
+        [& type, this](OIC::Service::NSSyncInfo sync)
     {
-        std::cout << "Income Notification : " << sync->getMessageId()
-                  << ", State : " << (int) sync->getState() << std::endl;
-        type = sync->getState();
+        std::cout << "Income Notification : " << sync.getMessageId()
+                  << ", State : " << (int) sync.getState() << std::endl;
+        type = sync.getState();
         responseCon.notify_all();
     });
 
@@ -463,17 +475,22 @@ TEST_F(NotificationServiceConsumerTest, ExpectCallbackDismissCheckWhenConsumerPo
 
 TEST_F(NotificationServiceConsumerTest, ExpectGetProviderSuccessWithValidProviderId)
 {
-    OIC::Service::NSProvider * provider =
-                        OIC::Service::NSConsumerService::getInstance()->getProvider(g_provider->getProviderId());
+    ASSERT_NE(nullptr, g_provider) << "error: discovery failure";
+
+    std::shared_ptr<OIC::Service::NSProvider> provider =
+        OIC::Service::NSConsumerService::getInstance()->getProvider(
+            g_provider->getProviderId());
     int ret = strcmp(provider->getProviderId().c_str(), g_provider->getProviderId().c_str());
     EXPECT_EQ(0, ret);
 }
 
 TEST_F(NotificationServiceConsumerTest, ExpectGetProviderSuccessWithInvalidProviderId)
 {
-    OIC::Service::NSProvider * provider =
-                        OIC::Service::NSConsumerService::getInstance()->getProvider("123456789012345678901234567890123457");
-    EXPECT_EQ(provider, (void*)NULL);
+    std::shared_ptr<OIC::Service::NSProvider> provider =
+        OIC::Service::NSConsumerService::getInstance()->getProvider(
+            "123456789012345678901234567890123457");
+    bool res = (provider == nullptr);
+    EXPECT_EQ(res, 1);
 }
 
 TEST_F(NotificationServiceConsumerTest, ExpectCallbackTopicUpdated)
@@ -481,11 +498,11 @@ TEST_F(NotificationServiceConsumerTest, ExpectCallbackTopicUpdated)
     OIC::Service::NSProviderState revState = OIC::Service::NSProviderState::STOPPED;
     mocks.OnCallFunc(ProviderChangedCallbackEmpty).Do(
         [this, & revState](OIC::Service::NSProviderState state)
-        {
-            std::cout << "Income Changed Callback : " << (int)state << std::endl;
-            revState = state;
-            responseCon.notify_all();
-        });
+    {
+        std::cout << "Income Changed Callback : " << (int)state << std::endl;
+        revState = state;
+        responseCon.notify_all();
+    });
 
     NSProviderSimulator::NS_TopicList topics;
     topics.push_back("1");
@@ -509,17 +526,19 @@ TEST_F(NotificationServiceConsumerTest, ExpectEQTopicList)
     topics.push_back("2");
     topics.push_back("3");
 
-    std::list<OIC::Service::NSTopic *>  retTopic = g_provider->getTopicList()->getTopicsList();
-    auto it1=retTopic.begin();
-    auto it2=topics.begin();
-    while( it1 != retTopic.end() || it2 != topics.end() )
+    ASSERT_NE(nullptr, g_provider) << "error: discovery failure";
+
+    auto  retTopic = g_provider->getTopicList()->getTopicsList();
+    auto it1 = retTopic.begin();
+    auto it2 = topics.begin();
+    while ( it1 != retTopic.end() || it2 != topics.end() )
     {
-        if((*it1)->getTopicName() !=  *it2)
+        if ((*it1).getTopicName() !=  *it2)
         {
             isSame = false; break;
         }
-            it1++;it2++;
-     }
+        it1++; it2++;
+    }
 
 
     EXPECT_EQ(true, isSame);
@@ -527,12 +546,17 @@ TEST_F(NotificationServiceConsumerTest, ExpectEQTopicList)
 
 TEST_F(NotificationServiceConsumerTest, ExpectFailUpdateTopicOnConsumer)
 {
-    OIC::Service::NSTopicsList * retTopic = g_provider->getTopicList();
+    ASSERT_NE(nullptr, g_provider) << "error: discovery failure";
+
+    auto retTopic = g_provider->getTopicList();
     for (auto it : retTopic->getTopicsList())
     {
-        it->setState(OIC::Service::NSTopic::NSTopicState::SUBSCRIBED);
+        std::cout << "Topic Name: " << it.getTopicName() << std::endl;
+        std::cout << "state : " << (int) it.getState() << std::endl;
+        it.setState(OIC::Service::NSTopic::NSTopicState::SUBSCRIBED);
     }
     OIC::Service::NSResult ret = g_provider->updateTopicList(retTopic);
+    std::cout << "ret : " << (int) ret << std::endl;
 
     EXPECT_EQ(OIC::Service::NSResult::ERROR, ret);
 }