Updated unit test of consumer.
authorKIM JungYong <jyong2.kim@samsung.com>
Wed, 20 Jul 2016 05:01:58 +0000 (14:01 +0900)
committerUze Choi <uzchoi@samsung.com>
Thu, 21 Jul 2016 01:00:57 +0000 (01:00 +0000)
Test flow sync with latest consumer code.

Change-Id: Iec53ec7256a1dc91f1e24eb421ba972fe1c81cab
Signed-off-by: KIM JungYong <jyong2.kim@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/9489
Reviewed-by: Uze Choi <uzchoi@samsung.com>
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
service/notification/unittest/NSConsumerTest.cpp
service/notification/unittest/NSProviderSimulator.h

index 8f38d43..ba3c426 100644 (file)
@@ -53,9 +53,13 @@ public:
     MockRepository mocks;
 
 protected:
-    virtual ~TestWithMock() noexcept(noexcept(std::declval<Test>().~Test())) {}
+    virtual ~TestWithMock() noexcept(noexcept(std::declval<Test>().~Test()))
+    {
+
+    }
 
-    virtual void TearDown() {
+    virtual void TearDown()
+    {
         try
         {
             mocks.VerifyAll();
@@ -79,11 +83,25 @@ public:
         std::cout << __func__ << std::endl;
     }
 
-    static void NSNotificationReceivedCallbackEmpty(NSProvider *, NSMessage *) { }
+    static void NSNotificationReceivedCallbackEmpty(NSMessage *)
+    {
+        std::cout << __func__ << std::endl;
+    }
+
+    static void NSSyncCallbackEmpty(NSSyncInfo *)
+    {
+        std::cout << __func__ << std::endl;
+    }
 
-    static void NSSyncCallbackEmpty(NSProvider *, NSSync *) { }
+    static void foundResourceEmpty(std::shared_ptr< OC::OCResource >)
+    {
+        std::cout << __func__ << std::endl;
+    }
 
-    static void foundResourceEmpty(std::shared_ptr< OC::OCResource >) { }
+    static void SubscriptionAcceptedCallback(NSProvider *)
+    {
+        std::cout << __func__ << std::endl;
+    }
 
 protected:
 
@@ -126,11 +144,12 @@ protected:
 
 TEST_F(NotificationConsumerTest, StartConsumerPositive)
 {
-    EXPECT_EQ(NS_OK,
-              NSStartConsumer(
-                    NSProviderDiscoveredCallbackEmpty,
-                    NSNotificationReceivedCallbackEmpty,
-                    NSSyncCallbackEmpty));
+    NSConsumerConfig cfg;
+    cfg.discoverCb = NSProviderDiscoveredCallbackEmpty;
+    cfg.acceptedCb = SubscriptionAcceptedCallback;
+    cfg.messageCb = NSNotificationReceivedCallbackEmpty;
+    cfg.syncInfoCb = NSSyncCallbackEmpty;
+    EXPECT_EQ(NS_OK, NSStartConsumer(cfg));
 }
 
 TEST_F(NotificationConsumerTest, StopConsumerPositive)
@@ -141,16 +160,18 @@ TEST_F(NotificationConsumerTest, StopConsumerPositive)
 TEST_F(NotificationConsumerTest, DiscoverProviderWithNonAccepterWhenStartedConsumerFirst)
 {
     mocks.ExpectCallFunc(NSProviderDiscoveredCallbackEmpty).Do(
-            [this, & responseCon](NSProvider *)
+            [this](NSProvider *)
             {
                 std::cout << "Call Discovered" << std::endl;
                 responseCon.notify_all();
             });
 
-    NSStartConsumer(
-            NSProviderDiscoveredCallbackEmpty,
-            NSNotificationReceivedCallbackEmpty,
-            NSSyncCallbackEmpty);
+    NSConsumerConfig cfg;
+    cfg.discoverCb = NSProviderDiscoveredCallbackEmpty;
+    cfg.acceptedCb = SubscriptionAcceptedCallback;
+    cfg.messageCb = NSNotificationReceivedCallbackEmpty;
+    cfg.syncInfoCb = NSSyncCallbackEmpty;
+    NSStartConsumer(cfg);
 
     g_providerSimul.setAccepter(1);
     g_providerSimul.createNotificationResource();
@@ -172,16 +193,19 @@ TEST_F(NotificationConsumerTest, DiscoverProviderWithNonAccepterWhenStartedConsu
     }
 
     mocks.ExpectCallFunc(NSProviderDiscoveredCallbackEmpty).Do(
-            [this, & responseCon](NSProvider *)
+            [this](NSProvider * provider)
             {
                 std::cout << "Call Discovered" << std::endl;
+                g_provider = provider;
                 responseCon.notify_all();
             });
 
-    NSStartConsumer(
-            NSProviderDiscoveredCallbackEmpty,
-            NSNotificationReceivedCallbackEmpty,
-            NSSyncCallbackEmpty);
+    NSConsumerConfig cfg;
+    cfg.discoverCb = NSProviderDiscoveredCallbackEmpty;
+    cfg.acceptedCb = SubscriptionAcceptedCallback;
+    cfg.messageCb = NSNotificationReceivedCallbackEmpty;
+    cfg.syncInfoCb = NSSyncCallbackEmpty;
+    NSStartConsumer(cfg);
 
     std::unique_lock< std::mutex > lock{ mutexForCondition };
     responseCon.wait_for(lock, g_waitForResponse);
@@ -190,12 +214,13 @@ TEST_F(NotificationConsumerTest, DiscoverProviderWithNonAccepterWhenStartedConsu
 
 TEST_F(NotificationConsumerTest, DiscoverProviderWithNonAccepterWhenRescan)
 {
-    mocks.ExpectCallFunc(NSProviderDiscoveredCallbackEmpty)
-            .Do(
-            [this, & responseCon](NSProvider * provider)
+    g_providerSimul.setAccepter(1);
+    mocks.ExpectCallFunc(NSProviderDiscoveredCallbackEmpty).Do(
+            [this](NSProvider * provider)
             {
                 std::cout << "Call Discovered" << std::endl;
                 g_provider = provider;
+                std::cout << g_provider->providerId << std::endl;
                 responseCon.notify_all();
             });
 
@@ -209,6 +234,12 @@ TEST_F(NotificationConsumerTest, DiscoverProviderWithNonAccepterWhenRescan)
 
 TEST_F(NotificationConsumerTest, ExpectSubscribeSuccess)
 {
+    mocks.ExpectCallFunc(SubscriptionAcceptedCallback).Do(
+            [](NSProvider * )
+            {
+                std::cout << "Income Accepted subscription : " << std::endl;
+            });
+
     NSResult ret = NSSubscribe(g_provider);
     std::unique_lock< std::mutex > lock{ mutexForCondition };
     responseCon.wait_for(lock, g_waitForResponse);
@@ -218,15 +249,14 @@ TEST_F(NotificationConsumerTest, ExpectSubscribeSuccess)
 
 TEST_F(NotificationConsumerTest, ExpectReceiveNotification)
 {
-    std::string id = "id";
+    uint64_t id = 10;
     std::string title = "title";
     std::string msg = "msg";
 
     mocks.ExpectCallFunc(NSNotificationReceivedCallbackEmpty).Do(
-            [](NSProvider *, NSMessage * message)
+            [](NSMessage * message)
             {
-                std::cout << "Income Notification : " << message->mId << std::endl;
-                NSDropNSObject(message);
+                std::cout << "Income Notification : " << message->messageId << std::endl;
             });
 
     g_providerSimul.notifyMessage(id, title, msg);
@@ -234,36 +264,32 @@ TEST_F(NotificationConsumerTest, ExpectReceiveNotification)
     std::unique_lock< std::mutex > lock{ mutexForCondition };
     responseCon.wait_for(lock, g_waitForResponse);
 
-}
-
-TEST_F(NotificationConsumerTest, ExpectUnsubscribeSuccess)
-{
-    NSResult ret = NSUnsubscribe(g_provider);
-    std::unique_lock< std::mutex > lock{ mutexForCondition };
-    responseCon.wait_for(lock, g_waitForResponse);
-
-    EXPECT_EQ(NS_OK, ret);
+    NSStopConsumer();
 }
 
 TEST_F(NotificationConsumerTest, ExpectReceiveNotificationWithAccepterisProvider)
 {
-    std::string id = "ExpectReceiveNotificationWithAccepterisProvider";
+    uint64_t id = 11;
     std::string title = "title";
     std::string msg = "msg";
 
     g_providerSimul.setAccepter(0);
 
-    NSRescanProvider();
+    NSConsumerConfig cfg;
+    cfg.discoverCb = NSProviderDiscoveredCallbackEmpty;
+    cfg.acceptedCb = SubscriptionAcceptedCallback;
+    cfg.messageCb = NSNotificationReceivedCallbackEmpty;
+    cfg.syncInfoCb = NSSyncCallbackEmpty;
+    NSStartConsumer(cfg);
     {
         std::unique_lock< std::mutex > lock{ mutexForCondition };
         responseCon.wait_for(lock, g_waitForResponse);
     }
 
     mocks.ExpectCallFunc(NSNotificationReceivedCallbackEmpty).Do(
-            [](NSProvider *, NSMessage * message)
+            [](NSMessage * message)
             {
-                std::cout << "Income Notification : " << message->mId << std::endl;
-                NSDropNSObject(message);
+                std::cout << "Income Notification : " << message->messageId << std::endl;
             });
 
     g_providerSimul.notifyMessage(id, title, msg);
@@ -277,25 +303,24 @@ TEST_F(NotificationConsumerTest, ExpectReceiveNotificationWithAccepterisProvider
 
 TEST_F(NotificationConsumerTest, ExpectCallbackReadCheckWhenProviderNotifySync)
 {
-    std::string id = "ExpectCallbackReadCheckWhenProviderNotifySync";
+    uint64_t id = 12;
     std::string title = "title";
     std::string msg = "msg";
 
-    NSSyncTypes type = Notification_Dismiss;
+    NSSyncType type = NS_SYNC_DELETED;
 
     mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
-            [](NSProvider *, NSMessage * message)
+            [](NSMessage * message)
             {
-                std::cout << "Income Notification : " << message->mId << std::endl;
-                NSDropNSObject(message);
+                std::cout << "Income Notification : " << message->messageId << std::endl;
             });
 
     mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
-            [& type](NSProvider *, NSSync * sync)
+            [& type](NSSyncInfo * sync)
             {
-                std::cout << "Income Notification : " << sync->mMessageId
-                        << ", State : " << sync->mState << std::endl;
-                type = sync->mState;
+                std::cout << "Income SyncInfo : " << sync->messageId
+                        << ", State : " << sync->state << std::endl;
+                type = sync->state;
 
             });
 
@@ -314,30 +339,29 @@ TEST_F(NotificationConsumerTest, ExpectCallbackReadCheckWhenProviderNotifySync)
 //    g_providerSimul.deleteNotificationResource();
 //    NSStopConsumer();
 
-    EXPECT_EQ(Notification_Read, type);
+    EXPECT_EQ(NS_SYNC_READ, type);
 }
 
 TEST_F(NotificationConsumerTest, ExpectCallbackDismissCheckWhenProviderNotifySync)
 {
-    std::string id = "ExpectCallbackDismissCheckWhenProviderNotifySync";
+    uint64_t id = 13;
     std::string title = "title";
     std::string msg = "msg";
 
-    NSSyncTypes type = Notification_Read;
+    NSSyncType type = NS_SYNC_READ;
 
     mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
-            [](NSProvider *, NSMessage * message)
+            [](NSMessage * message)
             {
-                std::cout << "Income Notification : " << message->mId << std::endl;
-                NSDropNSObject(message);
+                std::cout << "Income Notification : " << message->messageId << std::endl;
             });
 
     mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
-            [& type](NSProvider *, NSSync * sync)
+            [& type](NSSyncInfo * sync)
             {
-                std::cout << "Income Notification : " << sync->mMessageId
-                        << ", State : " << sync->mState << std::endl;
-                type = sync->mState;
+                std::cout << "Income Notification : " << sync->messageId
+                        << ", State : " << sync->state << std::endl;
+                type = sync->state;
 
             });
 
@@ -356,32 +380,32 @@ TEST_F(NotificationConsumerTest, ExpectCallbackDismissCheckWhenProviderNotifySyn
 //    g_providerSimul.deleteNotificationResource();
 //    NSStopConsumer();
 
-    EXPECT_EQ(Notification_Dismiss, type);
+    EXPECT_EQ(NS_SYNC_DELETED, type);
 }
 
 TEST_F(NotificationConsumerTest, ExpectCallbackReadCheckWhenConsumerPostSync)
 {
-    std::string id = "ExpectCallbackReadCheckWhenConsumerPostSync";
+    uint64_t id = 14;
     std::string title = "title";
     std::string msg = "msg";
 
-    NSSyncTypes type = Notification_Dismiss;
+    NSSyncType type = NS_SYNC_DELETED;
 
     mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
-            [](NSProvider *, NSMessage * message)
+            [](NSMessage * message)
             {
-                std::cout << "Income Notification : " << message->mId << std::endl;
-                NSConsumerReadCheck(message);
+                std::cout << "Income Notification : " << message->messageId << std::endl;
+                NSConsumerSendSyncInfo(message->providerId, message->messageId, NS_SYNC_READ);
                 std::unique_lock< std::mutex > lock{ mutexForCondition };
                 responseCon.wait_for(lock, g_waitForResponse);
             });
 
     mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
-            [& type](NSProvider *, NSSync * sync)
+            [& type](NSSyncInfo * sync)
             {
-                std::cout << "Income Notification : " << sync->mMessageId
-                        << ", State : " << sync->mState << std::endl;
-                type = sync->mState;
+                std::cout << "Income Notification : " << sync->messageId
+                        << ", State : " << sync->state << std::endl;
+                type = sync->state;
 
             });
 
@@ -394,32 +418,32 @@ TEST_F(NotificationConsumerTest, ExpectCallbackReadCheckWhenConsumerPostSync)
 //    g_providerSimul.deleteNotificationResource();
 //    NSStopConsumer();
 
-    EXPECT_EQ(Notification_Read, type);
+    EXPECT_EQ(NS_SYNC_READ, type);
 }
 
 TEST_F(NotificationConsumerTest, ExpectCallbackDismissCheckWhenConsumerPostSync)
 {
-    std::string id = "ExpectCallbackDismissCheckWhenConsumerPostSync";
+    uint64_t id = 15;
     std::string title = "title";
     std::string msg = "msg";
 
-    NSSyncTypes type = Notification_Read;
+    NSSyncType type = NS_SYNC_READ;
 
     mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
-            [](NSProvider *, NSMessage * message)
+            [](NSMessage * message)
             {
-                std::cout << "Income Notification : " << message->mId << std::endl;
-                NSConsumerDismissCheck(message);
+                std::cout << "Income Notification : " << message->messageId << std::endl;
+                NSConsumerSendSyncInfo(message->providerId, message->messageId, NS_SYNC_DELETED);
                 std::unique_lock< std::mutex > lock{ mutexForCondition };
                 responseCon.wait_for(lock, g_waitForResponse);
             });
 
     mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
-            [& type](NSProvider *, NSSync * sync)
+            [& type](NSSyncInfo * sync)
             {
-                std::cout << "Income Notification : " << sync->mMessageId
-                        << ", State : " << sync->mState << std::endl;
-                type = sync->mState;
+                std::cout << "Income Notification : " << sync->messageId
+                        << ", State : " << sync->state << std::endl;
+                type = sync->state;
 
             });
 
@@ -429,8 +453,17 @@ TEST_F(NotificationConsumerTest, ExpectCallbackDismissCheckWhenConsumerPostSync)
         responseCon.wait_for(lock, g_waitForResponse);
     }
 
+    EXPECT_EQ(NS_SYNC_DELETED, type);
+}
+
+TEST_F(NotificationConsumerTest, ExpectUnsubscribeSuccess)
+{
+    NSResult ret = NSUnsubscribe(g_provider);
+    std::unique_lock< std::mutex > lock{ mutexForCondition };
+    responseCon.wait_for(lock, g_waitForResponse);
+
     g_providerSimul.deleteNotificationResource();
     NSStopConsumer();
 
-    EXPECT_EQ(Notification_Dismiss, type);
+    EXPECT_EQ(NS_OK, ret);
 }
index 36a8f54..97c2d15 100644 (file)
@@ -60,7 +60,10 @@ public:
       m_notificationUri(std::string("/notification")),
       m_messageUri(std::string("/message")),
       m_syncUri(std::string("/sync")),
-      m_syncObservers() { };
+      m_syncObservers()
+    {
+
+    };
 
     ~NSProviderSimulator() = default;
 
@@ -94,9 +97,11 @@ private:
                 {
                     std::string msgUri = m_notificationUri + m_messageUri;
                     std::string syncUri = m_notificationUri + m_syncUri;
+                    std::string providerId = "123456789012345678901234567890123456";
                     rep.setValue("ACCEPTER", m_accepter);
                     rep.setValue("MESSAGE_URI", msgUri);
                     rep.setValue("SYNC_URI", syncUri);
+                    rep.setValue("PROVIDER_ID", providerId);
                 }
                 else if (type == requestType::NS_SYNC)
                 {
@@ -150,6 +155,25 @@ private:
                         observationInfo.obsId), m_syncObservers.end());
             }
         }
+        else if (type == requestType::NS_MESSAGE)
+        {
+            OC::OCRepresentation rep;
+            std::string providerId = "123456789012345678901234567890123456";
+            rep.setValue<int>("MESSAGE_ID", (int)1);
+            rep.setValue("PROVIDER_ID", providerId);
+
+            auto response = std::make_shared<OC::OCResourceResponse>();
+            response->setRequestHandle(requests->getRequestHandle());
+            response->setResourceHandle(requests->getResourceHandle());
+            response->setErrorCode(200);
+            response->setResponseResult(OC_EH_OK);
+            response->setResourceRepresentation(rep);
+
+            OC::ObservationIds ids;
+            ids.push_back(requests->getObservationInfo().obsId);
+
+            OC::OCPlatform::notifyListOfObservers(m_messageHandle, ids, response);
+        }
     }
 
     OCEntityHandlerResult entityHandler(
@@ -199,30 +223,36 @@ public:
         OC::OCPlatform::notifyAllObservers(m_messageHandle);
     }
 
-    void notifyMessage(const std::string & id, const std::string & title, const std::string & content)
+    void notifyMessage(const uint64_t & id, const std::string & title, const std::string & content)
     {
         setMessage(id, title, content);
         notifyMessage();
     }
 
-    void sendRead(const std::string & id)
+    void sendRead(const uint64_t & id)
     {
-        m_syncRep.setValue("ID", id);
-        m_syncRep.setValue("STATE", (int)0);
+        std::string providerId = "123456789012345678901234567890123456";
+        m_syncRep.setValue<int>("MESSAGE_ID", id);
+        m_syncRep.setValue("STATE", (int)1);
+        m_syncRep.setValue("PROVIDER_ID", providerId);
         OC::OCPlatform::notifyAllObservers(m_syncHandle);
     }
-    void sendDismiss(const std::string & id)
+    void sendDismiss(const uint64_t & id)
     {
-        m_syncRep.setValue("ID", id);
-        m_syncRep.setValue("STATE", (int)1);
+        std::string providerId = "123456789012345678901234567890123456";
+        m_syncRep.setValue<int>("MESSAGE_ID", id);
+        m_syncRep.setValue("STATE", (int)2);
+        m_syncRep.setValue("PROVIDER_ID", providerId);
         OC::OCPlatform::notifyAllObservers(m_syncHandle);
     }
 
-    void setMessage(const std::string & id, const std::string & title, const std::string & content)
+    void setMessage(const uint64_t & id, const std::string & title, const std::string & content)
     {
-        m_messageRep.setValue("ID", id);
+        std::string providerId = "123456789012345678901234567890123456";
+        m_messageRep.setValue<int>("MESSAGE_ID", id);
         m_messageRep.setValue("TITLE", title);
         m_messageRep.setValue("CONTENTTEXT", content);
+        m_messageRep.setValue("PROVIDER_ID", providerId);
     }
 
     void deleteNotificationResource()
@@ -260,7 +290,8 @@ public:
                     std::bind(& NSProviderSimulator::entityHandler, this,
                             std::placeholders::_1, requestType::NS_MESSAGE),
                             resourceProperty);
-        } catch (std::exception & e)
+        }
+        catch (std::exception & e)
         {
             std::cout << e.what() << std::endl;
         }
@@ -275,7 +306,8 @@ public:
                     std::bind(& NSProviderSimulator::entityHandler, this,
                             std::placeholders::_1, requestType::NS_SYNC),
                             resourceProperty);
-        } catch (std::exception & e)
+        }
+        catch (std::exception & e)
         {
             std::cout << e.what() << std::endl;
         }
@@ -290,7 +322,8 @@ public:
                     std::bind(& NSProviderSimulator::entityHandler, this,
                             std::placeholders::_1, requestType::NS_NOTIFICATION),
                             resourceProperty);
-        } catch (std::exception & e)
+        }
+        catch (std::exception & e)
         {
             std::cout << e.what() << std::endl;
         }