[CONPRO-1430] BlockWiseTransfer Error
[platform/upstream/iotivity.git] / service / notification / unittest / NSConsumerTest.cpp
index 388957d..4a57bde 100644 (file)
@@ -52,6 +52,8 @@ namespace
     };
 
     NSConsumerConfig cfg;
+
+    NSProviderSimulator::NS_TopicStateList g_topicStateList;
 }
 
 class TestWithMock: public testing::Test
@@ -148,8 +150,45 @@ protected:
 
 };
 
+TEST_F(NotificationConsumerTest, StartConsumerNegativeNonSetChangedCB)
+{
+    cfg.changedCb = NULL;
+    cfg.messageCb = NSNotificationReceivedCallbackEmpty;
+    cfg.syncInfoCb = NSSyncCallbackEmpty;
+
+    EXPECT_EQ(NS_ERROR, NSStartConsumer(cfg));
+    std::unique_lock< std::mutex > lock{ mutexForCondition };
+    responseCon.wait_for(lock, g_waitForResponse);
+}
+
+TEST_F(NotificationConsumerTest, StartConsumerNegativeNonSetNotiReceiveCB)
+{
+    cfg.changedCb = NSProviderChangedCallback;
+    cfg.messageCb = NULL;
+    cfg.syncInfoCb = NSSyncCallbackEmpty;
+
+    EXPECT_EQ(NS_ERROR, NSStartConsumer(cfg));
+    std::unique_lock< std::mutex > lock{ mutexForCondition };
+    responseCon.wait_for(lock, g_waitForResponse);
+}
+
+TEST_F(NotificationConsumerTest, StartConsumerNegativeNonSetSyncCB)
+{
+    cfg.changedCb = NSProviderChangedCallback;
+    cfg.messageCb = NSNotificationReceivedCallbackEmpty;
+    cfg.syncInfoCb = NULL;
+
+    EXPECT_EQ(NS_ERROR, NSStartConsumer(cfg));
+    std::unique_lock< std::mutex > lock{ mutexForCondition };
+    responseCon.wait_for(lock, g_waitForResponse);
+}
+
 TEST_F(NotificationConsumerTest, StartConsumerPositive)
 {
+    cfg.changedCb = NSProviderChangedCallback;
+    cfg.messageCb = NSNotificationReceivedCallbackEmpty;
+    cfg.syncInfoCb = NSSyncCallbackEmpty;
+
     EXPECT_EQ(NS_OK, NSStartConsumer(cfg));
     std::unique_lock< std::mutex > lock{ mutexForCondition };
     responseCon.wait_for(lock, g_waitForResponse);
@@ -160,12 +199,19 @@ TEST_F(NotificationConsumerTest, StopConsumerPositive)
     EXPECT_EQ(NSStopConsumer(), NS_OK);
 }
 
+TEST_F(NotificationConsumerTest, StopConsumerNegative)
+{
+    EXPECT_EQ(NSStopConsumer(), NS_ERROR);
+}
+
 TEST_F(NotificationConsumerTest, DiscoverProviderWithNonAccepterWhenStartedConsumerFirst)
 {
-    mocks.ExpectCallFunc(NSProviderChangedCallback).Do(
-            [this](NSProvider *, NSProviderState)
+    NSProviderState revState = NS_STOPPED;
+    mocks.OnCallFunc(NSProviderChangedCallback).Do(
+            [this, & revState](NSProvider *, NSProviderState state)
             {
                 std::cout << "Call Discovered" << std::endl;
+                revState = state;
                 responseCon.notify_all();
             });
 
@@ -179,6 +225,8 @@ TEST_F(NotificationConsumerTest, DiscoverProviderWithNonAccepterWhenStartedConsu
 
     NSStopConsumer();
     g_providerSimul.deleteNotificationResource();
+
+    EXPECT_EQ(NS_DISCOVERED, revState);
 }
 
 TEST_F(NotificationConsumerTest, DiscoverProviderWithNonAccepterWhenStartedConsumerAfter)
@@ -190,11 +238,14 @@ TEST_F(NotificationConsumerTest, DiscoverProviderWithNonAccepterWhenStartedConsu
         responseCon.wait_for(lock, g_waitForResponse);
     }
 
-    mocks.ExpectCallFunc(NSProviderChangedCallback).Do(
-            [this](NSProvider * provider, NSProviderState)
+    NSProviderState revState = NS_STOPPED;
+    mocks.OnCallFunc(NSProviderChangedCallback).Do(
+            [this, & revState](NSProvider * provider, NSProviderState state)
             {
                 std::cout << "Call Discovered" << std::endl;
+
                 g_provider = provider;
+                revState = state;
                 responseCon.notify_all();
             });
 
@@ -203,15 +254,20 @@ TEST_F(NotificationConsumerTest, DiscoverProviderWithNonAccepterWhenStartedConsu
     std::unique_lock< std::mutex > lock{ mutexForCondition };
     responseCon.wait_for(lock, g_waitForResponse);
 
+    EXPECT_EQ(NS_DISCOVERED, revState);
+
+    ASSERT_NE(nullptr, g_provider) << "error: discovery failure";
 }
 
 TEST_F(NotificationConsumerTest, DiscoverProviderWithNonAccepterWhenRescan)
 {
     g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_CONSUMER);
-    mocks.ExpectCallFunc(NSProviderChangedCallback).Do(
-            [this](NSProvider * provider, NSProviderState)
+    NSProviderState revState = NS_STOPPED;
+    mocks.OnCallFunc(NSProviderChangedCallback).Do(
+            [this, &revState](NSProvider * provider, NSProviderState state)
             {
                 std::cout << "Call Discovered" << std::endl;
+                revState = state;
                 g_provider = provider;
                 std::cout << g_provider->providerId << std::endl;
                 responseCon.notify_all();
@@ -222,21 +278,28 @@ TEST_F(NotificationConsumerTest, DiscoverProviderWithNonAccepterWhenRescan)
     std::unique_lock< std::mutex > lock{ mutexForCondition };
     responseCon.wait_for(lock, g_waitForResponse);
 
-//    NSStopConsumer();
+    EXPECT_EQ(NS_DISCOVERED, revState);
 }
 
 TEST_F(NotificationConsumerTest, ExpectSubscribeSuccess)
 {
-//    mocks.ExpectCallFunc(NSSubscriptionAcceptedCallback).Do(
-//            [](NSProvider * )
-//            {
-//                std::cout << "Income Accepted subscription : " << std::endl;
-//            });
+    NSProviderState revState = NS_DENY;
+
+    ASSERT_NE(nullptr, g_provider) << "error: discovery failure";
+
+    mocks.OnCallFunc(NSProviderChangedCallback).Do(
+            [this, & revState](NSProvider * , NSProviderState state)
+            {
+                std::cout << "Income Changed Callback : " << state << std::endl;
+                revState = state;
+                responseCon.notify_all();
+            });
 
     NSResult ret = NSSubscribe(g_provider->providerId);
     std::unique_lock< std::mutex > lock{ mutexForCondition };
     responseCon.wait_for(lock, g_waitForResponse);
 
+    EXPECT_EQ(NS_ALLOW, revState);
     EXPECT_EQ(NS_OK, ret);
 }
 
@@ -245,11 +308,14 @@ TEST_F(NotificationConsumerTest, ExpectReceiveNotification)
     uint64_t id = 10;
     std::string title = "title";
     std::string msg = "msg";
+    uint64_t revId = 0;
 
-    mocks.ExpectCallFunc(NSNotificationReceivedCallbackEmpty).Do(
-            [](NSMessage * message)
+    mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
+            [this, & revId](NSMessage * message)
             {
                 std::cout << "Income Notification : " << message->messageId << std::endl;
+                revId = message->messageId;
+                responseCon.notify_all();
             });
 
     g_providerSimul.notifyMessage(id, title, msg);
@@ -257,27 +323,57 @@ TEST_F(NotificationConsumerTest, ExpectReceiveNotification)
     std::unique_lock< std::mutex > lock{ mutexForCondition };
     responseCon.wait_for(lock, g_waitForResponse);
 
+    EXPECT_EQ(id, revId);
     NSStopConsumer();
+    g_providerSimul.deleteNotificationResource();
 }
 
-TEST_F(NotificationConsumerTest, ExpectReceiveNotificationWithAccepterisProvider)
+TEST_F(NotificationConsumerTest, ExpectReceiveSubAllowWithAccepterisProvider)
 {
-    uint64_t id = 11;
-    std::string title = "title";
-    std::string msg = "msg";
-
     g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_PROVIDER);
-
-    NSStartConsumer(cfg);
+    NSProviderState revState = NS_DENY;
+    g_providerSimul.createNotificationResource();
     {
         std::unique_lock< std::mutex > lock{ mutexForCondition };
         responseCon.wait_for(lock, g_waitForResponse);
     }
 
-    mocks.ExpectCallFunc(NSNotificationReceivedCallbackEmpty).Do(
-            [](NSMessage * message)
+    mocks.OnCallFunc(NSProviderChangedCallback).Do(
+            [this, & revState](NSProvider * provider, NSProviderState state)
+            {
+                std::cout << "Income Changed Callback : " << state << std::endl;
+                revState = state;
+                g_provider = provider;
+                responseCon.notify_all();
+            });
+    mocks.OnCallFunc(NSProviderChangedCallback).Do(
+            [this, & revState](NSProvider *, NSProviderState state)
+            {
+                std::cout << "Income Changed Callback : " << state << std::endl;
+                revState = state;
+                responseCon.notify_all();
+            });
+
+    NSStartConsumer(cfg);
+    std::unique_lock< std::mutex > lock{ mutexForCondition };
+    responseCon.wait_for(lock, g_waitForResponse);
+
+    EXPECT_EQ(NS_ALLOW, revState);
+}
+
+TEST_F(NotificationConsumerTest, ExpectReceiveNotificationWithAccepterisProvider)
+{
+    uint64_t id = 11;
+    std::string title = "title";
+    std::string msg = "msg";
+    uint64_t revId = 1;
+
+    mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
+            [this, & id, & revId](NSMessage * message)
             {
                 std::cout << "Income Notification : " << message->messageId << std::endl;
+                revId = message->messageId;
+                responseCon.notify_all();
             });
 
     g_providerSimul.notifyMessage(id, title, msg);
@@ -285,8 +381,7 @@ TEST_F(NotificationConsumerTest, ExpectReceiveNotificationWithAccepterisProvider
     std::unique_lock< std::mutex > lock{ mutexForCondition };
     responseCon.wait_for(lock, g_waitForResponse);
 
-//    g_providerSimul.deleteNotificationResource();
-//    NSStopConsumer();
+    EXPECT_EQ(id, revId);
 }
 
 TEST_F(NotificationConsumerTest, ExpectCallbackReadCheckWhenProviderNotifySync)
@@ -294,22 +389,22 @@ TEST_F(NotificationConsumerTest, ExpectCallbackReadCheckWhenProviderNotifySync)
     uint64_t id = 12;
     std::string title = "title";
     std::string msg = "msg";
-
     NSSyncType type = NS_SYNC_DELETED;
 
     mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
-            [](NSMessage * message)
+            [this](NSMessage * message)
             {
                 std::cout << "Income Notification : " << message->messageId << std::endl;
+                responseCon.notify_all();
             });
 
-    mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
-            [& type](NSSyncInfo * sync)
+    mocks.OnCallFunc(NSSyncCallbackEmpty).Do(
+            [& type, this](NSSyncInfo * sync)
             {
                 std::cout << "Income SyncInfo : " << sync->messageId
                         << ", State : " << sync->state << std::endl;
                 type = sync->state;
-
+                responseCon.notify_all();
             });
 
     g_providerSimul.notifyMessage(id, title, msg);
@@ -324,9 +419,6 @@ TEST_F(NotificationConsumerTest, ExpectCallbackReadCheckWhenProviderNotifySync)
         responseCon.wait_for(lock, g_waitForResponse);
     }
 
-//    g_providerSimul.deleteNotificationResource();
-//    NSStopConsumer();
-
     EXPECT_EQ(NS_SYNC_READ, type);
 }
 
@@ -335,22 +427,22 @@ TEST_F(NotificationConsumerTest, ExpectCallbackDismissCheckWhenProviderNotifySyn
     uint64_t id = 13;
     std::string title = "title";
     std::string msg = "msg";
-
     NSSyncType type = NS_SYNC_READ;
 
     mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
-            [](NSMessage * message)
+            [this](NSMessage * message)
             {
                 std::cout << "Income Notification : " << message->messageId << std::endl;
+                responseCon.notify_all();
             });
 
-    mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
-            [& type](NSSyncInfo * sync)
+    mocks.OnCallFunc(NSSyncCallbackEmpty).Do(
+            [& type, this](NSSyncInfo * sync)
             {
                 std::cout << "Income Notification : " << sync->messageId
                         << ", State : " << sync->state << std::endl;
                 type = sync->state;
-
+                responseCon.notify_all();
             });
 
     g_providerSimul.notifyMessage(id, title, msg);
@@ -365,9 +457,6 @@ TEST_F(NotificationConsumerTest, ExpectCallbackDismissCheckWhenProviderNotifySyn
         responseCon.wait_for(lock, g_waitForResponse);
     }
 
-//    g_providerSimul.deleteNotificationResource();
-//    NSStopConsumer();
-
     EXPECT_EQ(NS_SYNC_DELETED, type);
 }
 
@@ -376,11 +465,10 @@ TEST_F(NotificationConsumerTest, ExpectCallbackReadCheckWhenConsumerPostSync)
     uint64_t id = 14;
     std::string title = "title";
     std::string msg = "msg";
-
     NSSyncType type = NS_SYNC_DELETED;
 
     mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
-            [](NSMessage * message)
+            [this](NSMessage * message)
             {
                 std::cout << "Income Notification : " << message->messageId << std::endl;
                 NSConsumerSendSyncInfo(message->providerId, message->messageId, NS_SYNC_READ);
@@ -388,13 +476,13 @@ TEST_F(NotificationConsumerTest, ExpectCallbackReadCheckWhenConsumerPostSync)
                 responseCon.wait_for(lock, g_waitForResponse);
             });
 
-    mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
-            [& type](NSSyncInfo * sync)
+    mocks.OnCallFunc(NSSyncCallbackEmpty).Do(
+            [& type, this](NSSyncInfo * sync)
             {
                 std::cout << "Income Notification : " << sync->messageId
                         << ", State : " << sync->state << std::endl;
                 type = sync->state;
-
+                responseCon.notify_all();
             });
 
     g_providerSimul.notifyMessage(id, title, msg);
@@ -403,9 +491,6 @@ TEST_F(NotificationConsumerTest, ExpectCallbackReadCheckWhenConsumerPostSync)
         responseCon.wait_for(lock, g_waitForResponse);
     }
 
-//    g_providerSimul.deleteNotificationResource();
-//    NSStopConsumer();
-
     EXPECT_EQ(NS_SYNC_READ, type);
 }
 
@@ -414,11 +499,10 @@ TEST_F(NotificationConsumerTest, ExpectCallbackDismissCheckWhenConsumerPostSync)
     uint64_t id = 15;
     std::string title = "title";
     std::string msg = "msg";
-
-    NSSyncType type = NS_SYNC_READ;
+    NSSyncType state = NS_SYNC_READ;
 
     mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
-            [](NSMessage * message)
+            [this](NSMessage * message)
             {
                 std::cout << "Income Notification : " << message->messageId << std::endl;
                 NSConsumerSendSyncInfo(message->providerId, message->messageId, NS_SYNC_DELETED);
@@ -426,13 +510,13 @@ TEST_F(NotificationConsumerTest, ExpectCallbackDismissCheckWhenConsumerPostSync)
                 responseCon.wait_for(lock, g_waitForResponse);
             });
 
-    mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
-            [& type](NSSyncInfo * sync)
+    mocks.OnCallFunc(NSSyncCallbackEmpty).Do(
+            [& state, this](NSSyncInfo * sync)
             {
                 std::cout << "Income Notification : " << sync->messageId
                         << ", State : " << sync->state << std::endl;
-                type = sync->state;
-
+                state = sync->state;
+                responseCon.notify_all();
             });
 
     g_providerSimul.notifyMessage(id, title, msg);
@@ -441,20 +525,115 @@ TEST_F(NotificationConsumerTest, ExpectCallbackDismissCheckWhenConsumerPostSync)
         responseCon.wait_for(lock, g_waitForResponse);
     }
 
-    g_providerSimul.deleteNotificationResource();
-    NSStopConsumer();
+    EXPECT_EQ(NS_SYNC_DELETED, state);
+}
 
-    EXPECT_EQ(NS_SYNC_DELETED, type);
+TEST_F(NotificationConsumerTest, ExpectGetProviderSuccessWithValidProviderId)
+{
+    ASSERT_NE(nullptr, g_provider) << "error: discovery failure";
+
+    NSProvider * provider = NSConsumerGetProvider(g_provider->providerId);
+    int ret = strcmp(provider->providerId, g_provider->providerId);
+    EXPECT_EQ(0, ret);
+    free(provider);
 }
 
-//TEST_F(NotificationConsumerTest, ExpectUnsubscribeSuccess)
-//{
-//    NSResult ret = NSUnsubscribe(g_provider->providerId);
-//    std::unique_lock< std::mutex > lock{ mutexForCondition };
-//    responseCon.wait_for(lock, g_waitForResponse);
-//
-//    g_providerSimul.deleteNotificationResource();
-//    NSStopConsumer();
-//
-//    EXPECT_EQ(NS_OK, ret);
-//}
+TEST_F(NotificationConsumerTest, ExpectGetProviderSuccessWithInvalidProviderId)
+{
+    NSProvider * provider = NSConsumerGetProvider("123456789012345678901234567890123457");
+    EXPECT_EQ(provider, (void*)NULL);
+}
+
+TEST_F(NotificationConsumerTest, ExpectGetProviderSuccessWithNULL)
+{
+    NSProvider * provider = NSConsumerGetProvider(NULL);
+    EXPECT_EQ(provider, (void*)NULL);
+}
+
+TEST_F(NotificationConsumerTest, ExpectGetTopicListIsNULL)
+{
+    ASSERT_NE(nullptr, g_provider) << "error: discovery failure";
+
+    NSTopicLL * currentTopics = NSConsumerGetTopicList(g_provider->providerId);
+    EXPECT_EQ(NULL, currentTopics);
+}
+
+TEST_F(NotificationConsumerTest, ExpectCallbackTopicUpdated)
+{
+    NSProviderState revState = NS_STOPPED;
+    mocks.OnCallFunc(NSProviderChangedCallback).Do(
+        [this, & revState](NSProvider * , NSProviderState state)
+        {
+            std::cout << "Income Changed Callback : " << 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(NS_TOPIC, revState);
+}
+
+TEST_F(NotificationConsumerTest, ExpectEQTopicList)
+{
+    bool isSame = false;
+
+    NSProviderSimulator::NS_TopicList topics;
+    topics.push_back("1");
+    topics.push_back("2");
+    topics.push_back("3");
+
+    ASSERT_NE(nullptr, g_provider) << "error: discovery failure";
+
+    NSTopicLL * retTopic = NSConsumerGetTopicList(g_provider->providerId);
+    std::for_each (topics.begin(), topics.end(),
+            [this, & retTopic, & isSame](const std::string & str)
+            {
+                isSame = (str == std::string(retTopic->topicName));
+                retTopic = retTopic->next;
+            });
+
+    EXPECT_EQ(true, isSame);
+}
+
+TEST_F(NotificationConsumerTest, ExpectFailUpdateTopicOnConsumer)
+{
+    ASSERT_NE(nullptr, g_provider) << "error: discovery failure";
+
+    NSTopicLL * retTopic = NSConsumerGetTopicList(g_provider->providerId);
+    for (; retTopic; retTopic = retTopic->next)
+    {
+        retTopic->state = NS_TOPIC_SUBSCRIBED;
+    }
+    NSResult ret = NSConsumerUpdateTopicList(g_provider->providerId, retTopic);
+
+    EXPECT_EQ(NS_ERROR, ret);
+}
+
+TEST_F(NotificationConsumerTest, ExpectCallbackDeletedProvider)
+{
+    NSProviderState type = NS_ALLOW;
+    mocks.OnCallFunc(NSProviderChangedCallback).Do(
+            [& type, this](NSProvider * , NSProviderState state)
+            {
+                std::cout << "Income Changed Callback : " << state << std::endl;
+                type = state;
+                responseCon.notify_all();
+            });
+
+    g_providerSimul.deleteNotificationResource();
+
+    std::unique_lock< std::mutex > lock{ mutexForCondition };
+    responseCon.wait_for(lock, std::chrono::milliseconds(2000));
+
+    EXPECT_EQ(type, NS_STOPPED);
+    NSStopConsumer();
+}