Merge "Merge remote-tracking branch 'origin/notification-service' Updated with static...
[platform/upstream/iotivity.git] / service / notification / unittest / NSProviderTest.cpp
old mode 100644 (file)
new mode 100755 (executable)
index 41c7fab..55505be
 #include <mutex>
 #include <chrono>
 
+#include "NSProviderInterface.h"
 #include "NSConsumerSimulator.h"
-
 #include "NSCommon.h"
-#include "NSProviderInterface.h"
 
 namespace
 {
@@ -41,8 +40,10 @@ namespace
     std::mutex mutexForCondition;
 
     NSConsumerSimulator g_consumerSimul;
-    NSConsumer * g_consumer;
-
+    char * g_consumerID;
+    char g_title[100];
+    char g_body[100];
+    char g_sourceName[100];
 }
 
 class TestWithMock: public testing::Test
@@ -77,18 +78,18 @@ public:
         std::cout << __func__ << std::endl;
     }
 
-    static void NSSyncCallbackEmpty(NSSync *)
+    static void NSSyncCallbackEmpty(NSSyncInfo *)
     {
         std::cout << __func__ << std::endl;
     }
 
     static void NSMessageCallbackFromConsumerEmpty(
-            const std::string &, const std::string &, const std::string &)
+            const int &, const std::string &, const std::string &, const std::string &)
     {
         std::cout << __func__ << std::endl;
     }
 
-    static void NSSyncCallbackFromConsumerEmpty(int, const std::string &)
+    static void NSSyncCallbackFromConsumerEmpty(int, int)
     {
         std::cout << __func__ << std::endl;
     }
@@ -107,7 +108,7 @@ protected:
                 OC::ModeType::Both,
                 "0.0.0.0",
                 0,
-                OC::QualityOfService::LowQos
+                OC::QualityOfService::HighQos
             };
             OC::OCPlatform::Configure(cfg);
 
@@ -121,6 +122,10 @@ protected:
             }
 
             g_isStartedStack = true;
+
+            strncpy(g_title, "Title", strlen("Title"));
+            strncpy(g_body, "ContentText", strlen("ContentText"));
+            strncpy(g_sourceName, "OIC", strlen("OIC"));
         }
 
     }
@@ -132,11 +137,15 @@ protected:
 
 };
 
-TEST_F(NotificationProviderTest, StartProviderPositive)
+TEST_F(NotificationProviderTest, StartProviderPositiveWithNSPolicyTrue)
 {
-    NSResult ret = NSStartProvider(NS_ACCEPTER_PROVIDER,
-            NSRequestedSubscribeCallbackEmpty,
-            NSSyncCallbackEmpty);
+    NSProviderConfig config;
+    config.subRequestCallback = NSRequestedSubscribeCallbackEmpty;
+    config.syncInfoCallback = NSSyncCallbackEmpty;
+    config.subControllability = true;
+    config.userInfo = NULL;
+
+    NSResult ret = NSStartProvider(config);
 
     std::unique_lock< std::mutex > lock{ mutexForCondition };
     responseCon.wait_for(lock, g_waitForResponse);
@@ -154,18 +163,43 @@ TEST_F(NotificationProviderTest, StopProviderPositive)
     EXPECT_EQ(ret, NS_OK);
 }
 
+TEST_F(NotificationProviderTest, StartProviderPositiveWithNSPolicyFalse)
+{
+    NSProviderConfig config;
+    config.subRequestCallback = NSRequestedSubscribeCallbackEmpty;
+    config.syncInfoCallback = NSSyncCallbackEmpty;
+    config.subControllability = false;
+    config.userInfo = NULL;
+
+    NSResult ret = NSStartProvider(config);
+
+    std::unique_lock< std::mutex > lock{ mutexForCondition };
+    responseCon.wait_for(lock, std::chrono::milliseconds(3000));
+    g_consumerSimul.findProvider();
+
+    responseCon.wait_for(lock, std::chrono::milliseconds(3000));
+    NSStopProvider();
+    EXPECT_EQ(ret, NS_OK);
+}
+
 TEST_F(NotificationProviderTest, ExpectCallbackWhenReceiveSubscribeRequestWithAccepterProvider)
 {
-    mocks.ExpectCallFunc(NSRequestedSubscribeCallbackEmpty).Do(
+    g_consumerID = NULL;
+    mocks.OnCallFunc(NSRequestedSubscribeCallbackEmpty).Do(
             [](NSConsumer * consumer)
             {
                 std::cout << "NSRequestedSubscribeCallback" << std::endl;
-                g_consumer = consumer;
+                g_consumerID = strdup(consumer->consumerId);
                 responseCon.notify_all();
             });
 
-    NSStartProvider(NS_ACCEPTER_PROVIDER,
-            NSRequestedSubscribeCallbackEmpty, NSSyncCallbackEmpty);
+    NSProviderConfig config;
+    config.subRequestCallback = NSRequestedSubscribeCallbackEmpty;
+    config.syncInfoCallback = NSSyncCallbackEmpty;
+    config.subControllability = true;
+    config.userInfo = NULL;
+
+    NSStartProvider(config);
 
     {
         std::unique_lock< std::mutex > lock{ mutexForCondition };
@@ -178,28 +212,33 @@ TEST_F(NotificationProviderTest, ExpectCallbackWhenReceiveSubscribeRequestWithAc
 
     std::unique_lock< std::mutex > lock{ mutexForCondition };
     responseCon.wait_for(lock, std::chrono::milliseconds(1000));
+
+    EXPECT_NE((void*)g_consumerID, (void*)NULL);
 }
 
 TEST_F(NotificationProviderTest, NeverCallNotifyOnConsumerByAcceptIsFalse)
 {
     bool expectTrue = true;
+    int msgID;
 
     mocks.OnCallFunc(NSMessageCallbackFromConsumerEmpty).Do(
-            [& expectTrue](const std::string &id, const std::string&, const std::string&)
+            [& expectTrue, &msgID](const int &id, const std::string&, const std::string&, const std::string&)
             {
-                if (id == "NeverCallNotifyOnConsumerByAcceptIsFalse")
+                if (id == msgID)
                 {
                     std::cout << "This function never call" << std::endl;
                     expectTrue = false;
                 }
             });
 
-    NSAccept(g_consumer, false);
+    NSAcceptSubscription(g_consumerID, false);
+
+    NSMessage * msg = NSCreateMessage();
+    msgID = (int)msg->messageId;
+    msg->title = g_title;
+    msg->contentText = g_body;
+    msg->sourceName = g_sourceName;
 
-    NSMessage * msg = new NSMessage();
-    msg->mId = strdup(std::string("NeverCallNotifyOnConsumerByAcceptIsFalse").c_str());
-    msg->mTitle = strdup(std::string("Title").c_str());
-    msg->mContentText = strdup(std::string("ContentText").c_str());
     NSSendMessage(msg);
     {
         std::unique_lock< std::mutex > lock{ mutexForCondition };
@@ -210,77 +249,271 @@ TEST_F(NotificationProviderTest, NeverCallNotifyOnConsumerByAcceptIsFalse)
     responseCon.wait_for(lock, std::chrono::milliseconds(1000));
 
     EXPECT_EQ(expectTrue, true);
+
+    NSAcceptSubscription(g_consumerID, true);
 }
 
 TEST_F(NotificationProviderTest, ExpectCallNotifyOnConsumerByAcceptIsTrue)
 {
+    int msgID;
+
     mocks.ExpectCallFunc(NSMessageCallbackFromConsumerEmpty).Do(
-            [](const std::string &id, const std::string&, const std::string&)
+            [&msgID](const int &id, const std::string&, const std::string&, const std::string&)
             {
-                if (id == "ExpectCallNotifyOnConsumerByAcceptIsTrue")
+                std::cout << "id : " << id << std::endl;
+                if (id == msgID)
                 {
                     std::cout << "ExpectCallNotifyOnConsumerByAcceptIsTrue" << std::endl;
                     responseCon.notify_all();
                 }
             });
 
-    NSAccept(g_consumer, true);
+    NSAcceptSubscription(g_consumerID, true);
 
-    NSMessage * msg = new NSMessage();
-    msg->mId = strdup(std::string("ExpectCallNotifyOnConsumerByAcceptIsTrue").c_str());
-    msg->mTitle = strdup(std::string("Title").c_str());
-    msg->mContentText = strdup(std::string("ContentText").c_str());
+    NSMessage * msg = NSCreateMessage();
+    msgID = (int)msg->messageId;
+    msg->title = g_title;
+    msg->contentText = g_body;
+    msg->sourceName = g_sourceName;
     NSSendMessage(msg);
+
+    std::unique_lock< std::mutex > lock{ mutexForCondition };
+    responseCon.wait(lock);
+}
+
+TEST_F(NotificationProviderTest, ExpectCallbackSyncOnReadToConsumer)
+{
+    int id;
+
+    mocks.ExpectCallFunc(NSSyncCallbackFromConsumerEmpty).Do(
+            [& id](int & type, int &syncId)
+            {
+        std::cout << "NSSyncCallbackEmpty" << std::endl;
+                if (syncId == id &&
+                        type == NS_SYNC_READ)
+                {
+                    std::cout << "ExpectCallbackSyncOnReadFromConsumer" << std::endl;
+                    responseCon.notify_all();
+                }
+            });
+
+    NSMessage * msg = NSCreateMessage();
+    id = (int)msg->messageId;
+    msg->title = g_title;
+    msg->contentText = g_body;
+    msg->sourceName = g_sourceName;
+
+    NSProviderSendSyncInfo(msg->messageId, NS_SYNC_READ);
+    std::unique_lock< std::mutex > lock{ mutexForCondition };
+    responseCon.wait_for(lock, std::chrono::milliseconds(5000));
+}
+
+TEST_F(NotificationProviderTest, ExpectCallbackSyncOnReadFromConsumer)
+{
+    int type = NS_SYNC_READ;
+    int id;
+    mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
+            [& id](NSSyncInfo * sync)
+            {
+                std::cout << "NSSyncCallbackEmpty" << std::endl;
+                if ((int)sync->messageId == id && sync->state == NS_SYNC_READ)
+                {
+                    std::cout << "ExpectCallbackSyncOnReadFromConsumer" << std::endl;
+                    responseCon.notify_all();
+                }
+            });
+
+    NSMessage * msg = NSCreateMessage();
+    id = (int)msg->messageId;
+    msg->title = g_title;
+    msg->contentText = g_body;
+    msg->sourceName = g_sourceName;
+
+    g_consumerSimul.syncToProvider(type, id, msg->providerId);
+    std::unique_lock< std::mutex > lock{ mutexForCondition };
+
+    responseCon.wait(lock);
+    //responseCon.wait_for(lock, std::chrono::milliseconds(3000));
+}
+
+TEST_F(NotificationProviderTest, ExpectEqualAddedTopicsAndRegisteredTopics)
+{
+    std::string str("TEST1");
+    std::string str2("TEST2");
+    NSProviderRegisterTopic(str.c_str());
+    NSProviderRegisterTopic(str2.c_str());
+
+    std::unique_lock< std::mutex > lock{ mutexForCondition };
+    responseCon.wait_for(lock, std::chrono::milliseconds(500));
+
+    bool isSame = true;
+    NSTopicLL * topics = NSProviderGetTopics();
+
+    if(!topics)
     {
-        std::unique_lock< std::mutex > lock{ mutexForCondition };
-        responseCon.wait_for(lock, g_waitForResponse);
+        printf("topic is NULL\n");
+        isSame = false;
+    }
+    else
+    {
+        NSTopicLL * iter = topics;
+        std::string compStr(iter->topicName);
+        std::string compStr2(iter->next->topicName);
+
+        printf("str = %s, compStr = %s\n", str.c_str(), iter->topicName);
+        printf("str2 = %s, compStr2 = %s\n", str2.c_str(), iter->next->topicName);
+
+        if(str.compare(compStr) == 0 && str2.compare(compStr2) == 0)
+        {
+            isSame = true;
+        }
+    }
+
+    EXPECT_EQ(isSame, true);
+
+    NSProviderUnregisterTopic(str.c_str());
+    NSProviderUnregisterTopic(str2.c_str());
+
+    responseCon.wait_for(lock, std::chrono::milliseconds(500));
+}
+
+TEST_F(NotificationProviderTest, ExpectEqualUnregisteredTopicsAndRegisteredTopics)
+{
+    std::string str("TEST1");
+    std::string str2("TEST2");
+    NSProviderRegisterTopic(str.c_str());
+    NSProviderRegisterTopic(str2.c_str());
+    NSProviderUnregisterTopic(str2.c_str());
+
+    std::unique_lock< std::mutex > lock{ mutexForCondition };
+    responseCon.wait_for(lock, std::chrono::milliseconds(500));
+
+    bool isSame = true;
+    NSTopicLL * topics = NSProviderGetTopics();
+
+    if(!topics)
+    {
+        printf("topic is NULL\n");
+        isSame = false;
+    }
+    else
+    {
+        NSTopicLL * iter = topics;
+        std::string compStr(iter->topicName);
+
+        printf("str = %s, compStr = %s\n", str.c_str(), iter->topicName);
+
+        if(str.compare(compStr) == 0)
+        {
+            isSame = true;
+        }
     }
 
+    EXPECT_EQ(isSame, true);
+
+    NSProviderUnregisterTopic(str.c_str());
+
+    responseCon.wait_for(lock, std::chrono::milliseconds(500));
+}
+
+TEST_F(NotificationProviderTest, ExpectEqualSetConsumerTopicsAndGetConsumerTopics)
+{
+    std::string str("TEST1");
+    std::string str2("TEST2");
+    NSProviderRegisterTopic(str.c_str());
+    NSProviderRegisterTopic(str2.c_str());
+    NSProviderSetConsumerTopic(g_consumerID, str.c_str());
+
     std::unique_lock< std::mutex > lock{ mutexForCondition };
-    responseCon.wait_for(lock, g_waitForResponse);
+    responseCon.wait_for(lock, std::chrono::milliseconds(500));
+
+    bool isSame = false;
+    NSTopicLL * topics = NSProviderGetConsumerTopics(g_consumerID);
+
+    if(!topics)
+    {
+        printf("topic is NULL\n");
+        isSame = false;
+    }
+    else
+    {
+        NSTopicLL * firstData = topics;
+        NSTopicLL * secondData = firstData->next;
+
+        printf("str = %s, compStr = %s, state = %d\n", str.c_str(), firstData->topicName,
+                (int)firstData->state);
+
+        printf("str2 = %s, compStr = %s, state = %d\n", str2.c_str(), secondData->topicName,
+                (int)secondData->state);
+
+        if(str.compare(firstData->topicName) == 0 && str2.compare(secondData->topicName) == 0
+                && ((int)firstData->state) == 1 && ((int)secondData->state) == 0)
+        {
+            isSame = true;
+        }
+    }
+
+    EXPECT_EQ(isSame, true);
+
+    NSProviderUnregisterTopic(str.c_str());
+    NSProviderUnregisterTopic(str2.c_str());
+
+    responseCon.wait_for(lock, std::chrono::milliseconds(500));
 }
 
-//TEST_F(NotificationProviderTest, ExpectCallbackSyncOnReadToConsumer)
-//{
-//    int type = 0;
-//    std::string id = "ExpectCallNotifyOnConsumerByAcceptIsTrue";
-//    mocks.ExpectCallFunc(NSSyncCallbackFromConsumerEmpty).Do(
-//            [& id](int type, const std::string & syncId)
-//            {
-//        std::cout << "NSSyncCallbackEmpty" << std::endl;
-//                if (syncId == id &&
-//                        type == Notification_Read)
-//                {
-//                    std::cout << "ExpectCallbackSyncOnReadFromConsumer" << std::endl;
-//                    responseCon.notify_all();
-//                }
-//            });
-//
-//    NSMessage * msg = new NSMessage();
-//    msg->mId = strdup(std::string("ExpectCallNotifyOnConsumerByAcceptIsTrue").c_str());
-//    msg->mTitle = strdup(std::string("Title").c_str());
-//    msg->mContentText = strdup(std::string("ContentText").c_str());
-//    NSProviderReadCheck(msg);
-//    std::unique_lock< std::mutex > lock{ mutexForCondition };
-//    responseCon.wait_for(lock, std::chrono::milliseconds(5000));
-//}
-
-//TEST_F(NotificationProviderTest, ExpectCallbackSyncOnReadFromConsumer)
-//{
-//    int type = 0;
-//    std::string id("ExpectCallNotifyOnConsumerByAcceptIsTrue");
-//    mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
-//            [& id](NSSync * sync)
-//            {
-//                std::cout << "NSSyncCallbackEmpty" << std::endl;
-//                if (sync->mMessageId == id && sync->mState == Notification_Read)
-//                {
-//                    std::cout << "ExpectCallbackSyncOnReadFromConsumer" << std::endl;
-//                    responseCon.notify_all();
-//                }
-//            });
-//
-//    g_consumerSimul.syncToProvider(type, std::string(id));
-//    std::unique_lock< std::mutex > lock{ mutexForCondition };
-//    responseCon.wait_for(lock, std::chrono::milliseconds(5000));
-//}
+TEST_F(NotificationProviderTest, ExpectEqualUnSetConsumerTopicsAndGetConsumerTopics)
+{
+    std::string str("TEST1");
+    std::string str2("TEST2");
+    NSProviderRegisterTopic(str.c_str());
+    NSProviderRegisterTopic(str2.c_str());
+    NSProviderSetConsumerTopic(g_consumerID, str.c_str());
+    NSProviderSetConsumerTopic(g_consumerID, str2.c_str());
+    NSProviderUnsetConsumerTopic(g_consumerID, str.c_str());
+
+    std::unique_lock< std::mutex > lock{ mutexForCondition };
+    responseCon.wait_for(lock, std::chrono::milliseconds(500));
+
+    bool isSame = false;
+    NSTopicLL * topics = NSProviderGetConsumerTopics(g_consumerID);
+
+    if(!topics)
+    {
+        printf("topic is NULL\n");
+        isSame = false;
+    }
+    else
+    {
+        NSTopicLL * firstData = topics;
+        NSTopicLL * secondData = firstData->next;
+
+        printf("str = %s, compStr = %s, state = %d\n", str.c_str(), firstData->topicName,
+                (int)firstData->state);
+
+        printf("str2 = %s, compStr = %s, state = %d\n", str2.c_str(), secondData->topicName,
+                (int)secondData->state);
+
+        if(str.compare(firstData->topicName) == 0 && str2.compare(secondData->topicName) == 0
+                && ((int)firstData->state) == 0 && ((int)secondData->state) == 1)
+        {
+            isSame = true;
+        }
+    }
+
+    EXPECT_EQ(isSame, true);
+
+    NSProviderUnregisterTopic(str.c_str());
+    NSProviderUnregisterTopic(str2.c_str());
+
+    responseCon.wait_for(lock, std::chrono::milliseconds(500));
+}
+
+TEST_F(NotificationProviderTest, CancelObserves)
+{
+    bool ret = g_consumerSimul.cancelObserves();
+
+    std::unique_lock< std::mutex > lock{ mutexForCondition };
+    responseCon.wait_for(lock, std::chrono::milliseconds(5000));
+
+    EXPECT_EQ(ret, true);
+}