replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / notification / cpp-wrapper / unittest / NSConsumerServiceTest.cpp
old mode 100644 (file)
new mode 100755 (executable)
index 87cb5b2..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();
     });
 
@@ -461,6 +473,95 @@ TEST_F(NotificationServiceConsumerTest, ExpectCallbackDismissCheckWhenConsumerPo
     EXPECT_EQ(OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED, type);
 }
 
+TEST_F(NotificationServiceConsumerTest, ExpectGetProviderSuccessWithValidProviderId)
+{
+    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)
+{
+    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)
+{
+    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();
+    });
+
+    NSProviderSimulator::NS_TopicList topics;
+    topics.push_back("1");
+    topics.push_back("2");
+    topics.push_back("3");
+
+    g_providerSimul.setTopics(topics);
+
+    std::unique_lock< std::mutex > lock{ mutexForCondition };
+    responseCon.wait_for(lock, g_waitForResponse);
+
+    EXPECT_EQ(OIC::Service::NSProviderState::TOPIC, revState);
+}
+
+TEST_F(NotificationServiceConsumerTest, ExpectEQTopicList)
+{
+    bool isSame = true;
+
+    NSProviderSimulator::NS_TopicList topics;
+    topics.push_back("1");
+    topics.push_back("2");
+    topics.push_back("3");
+
+    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)
+        {
+            isSame = false; break;
+        }
+        it1++; it2++;
+    }
+
+
+    EXPECT_EQ(true, isSame);
+}
+
+TEST_F(NotificationServiceConsumerTest, ExpectFailUpdateTopicOnConsumer)
+{
+    ASSERT_NE(nullptr, g_provider) << "error: discovery failure";
+
+    auto retTopic = g_provider->getTopicList();
+    for (auto it : retTopic->getTopicsList())
+    {
+        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);
+}
+
+
 TEST_F(NotificationServiceConsumerTest, ExpectCallbackDeletedProvider)
 {
     OIC::Service::NSProviderState type = OIC::Service::NSProviderState::ALLOW;
@@ -469,7 +570,6 @@ TEST_F(NotificationServiceConsumerTest, ExpectCallbackDeletedProvider)
     {
         std::cout << "Income Changed Callback : " << std::endl;
         type = state;
-        responseCon.notify_all();
     });
 
     g_providerSimul.deleteNotificationResource();
@@ -480,15 +580,3 @@ TEST_F(NotificationServiceConsumerTest, ExpectCallbackDeletedProvider)
     EXPECT_EQ(type, OIC::Service::NSProviderState::STOPPED);
     OIC::Service::NSConsumerService::getInstance()->stop();
 }
-
-//TO add when unsubscibe API is added back
-//TEST_F(NotificationServiceConsumerTest, ExpectUnsubscribeSuccess)
-//{
-//    g_provider->unsubscribe();
-//   std::unique_lock< std::mutex > lock{ mutexForCondition };
-//    responseCon.wait_for(lock, g_waitForResponse);
-//
-//    g_providerSimul.deleteNotificationResource();
-//     OIC::Service::NSConsumerService::getInstance()->stop();
-//
-//}