Update and fix of UnitTest for cpp-wrapper
authornikhil.a <nikhil.a7@samsung.com>
Fri, 7 Oct 2016 10:45:22 +0000 (16:15 +0530)
committerUze Choi <uzchoi@samsung.com>
Wed, 12 Oct 2016 12:08:23 +0000 (12:08 +0000)
1)Updated new unittest for consumer and provider
2)Fix for removing segmentation fault

Change-Id: If06e9606c405a3fa2dace726a5c9320399b06e66
Signed-off-by: nikhil.a <nikhil.a7@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/12943
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
service/notification/cpp-wrapper/SConscript
service/notification/cpp-wrapper/unittest/NSConsumerServiceSimulator.h
service/notification/cpp-wrapper/unittest/NSConsumerServiceTest.cpp
service/notification/cpp-wrapper/unittest/NSProviderServiceSimulator.h
service/notification/cpp-wrapper/unittest/NSProviderServiceTest.cpp

index fa553bd..7dc5d41 100755 (executable)
@@ -31,3 +31,6 @@ SConscript('consumer/SConscript')
 if target_os == 'linux':
     SConscript('examples/linux/SConscript')
 
+# Go to build c++ wrapper unittest
+if target_os == 'linux':
+    SConscript('unittest/SConscript')
index cec3045..64bdac9 100644 (file)
@@ -35,6 +35,7 @@ private:
 
     std::shared_ptr<OC::OCResource> m_syncResource;
     std::shared_ptr<OC::OCResource> m_msgResource;
+    std::shared_ptr<OC::OCResource> m_topicResource;
 
 public:
     NSConsumerSimulator()
@@ -60,7 +61,6 @@ public:
     {
         if (m_syncResource == nullptr)
         {
-            std::cout << "m_syncResource is null" << std::endl;
             return;
         }
 
@@ -90,39 +90,32 @@ public:
 
 private:
     static void onPost(const OC::HeaderOptions &/*headerOption*/,
-                const OC::OCRepresentation & /*rep*/ , const int eCode)
+                const OC::OCRepresentation & /*rep*/ , const int /*eCode*/)
     {
-        std::cout << __func__ << " result : " << eCode << std::endl;
     }
     void findResultCallback(std::shared_ptr<OC::OCResource> resource)
     {
 
-        std::cout << __func__ << " " << resource->host() << std::endl;
 
         if(resource->uri() == "/notification")
         {
             resource->get(OC::QueryParamsMap(),
                     std::bind(&NSConsumerSimulator::onGet, this,
-                            std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, resource),
-                    OC::QualityOfService::LowQos);
+                            std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
+                            resource), OC::QualityOfService::LowQos);
         }
     }
     void onGet(const OC::HeaderOptions &/*headerOption*/,
-            const OC::OCRepresentation & rep , const int eCode,
+            const OC::OCRepresentation & /*rep*/ , const int /*eCode*/,
             std::shared_ptr<OC::OCResource> resource)
     {
-        std::cout << __func__ << " " << rep.getHost() << " result : " << eCode << std::endl;
 
         OC::QueryParamsMap map;
-        map.insert(std::pair<std::string,std::string>(std::string("consumerId"), std::string("123456789012345678901234567890123456")));
+        map.insert(std::pair<std::string,std::string>(std::string("consumerId"),
+                std::string("123456789012345678901234567890123456")));
 
         try
         {
-            std::cout << "resourc : host " << resource->host() << std::endl;
-            std::cout << "resourc : uri " << resource->uri() << std::endl;
-            std::cout << " resource->connectivityType() " <<  resource->connectivityType() << std::endl;
-            std::cout << "resourc : getResourceInterfaces " << resource->getResourceInterfaces()[0] << std::endl;
-            std::cout << "resourc : getResourceTypes " << resource->getResourceTypes()[0] << std::endl;
 
             std::vector<std::string> rts{"oic.wk.notification"};
 
@@ -153,31 +146,41 @@ private:
                         std::placeholders::_3, std::placeholders::_4, resource),
                 OC::QualityOfService::LowQos);
 
+        m_topicResource
+            = OC::OCPlatform::constructResourceObject(resource->host(), resource->uri() + "/topic",
+                    resource->connectivityType(), true, resource->getResourceTypes(),
+                    resource->getResourceInterfaces());
     }
     void onObserve(const OC::HeaderOptions &/*headerOption*/,
-            const OC::OCRepresentation &rep , const int &eCode, const int &,
+            const OC::OCRepresentation &rep , const int & /*eCode*/, const int &,
             std::shared_ptr<OC::OCResource> )
     {
-        std::cout << __func__ << " " << rep.getHost() << " result : " << eCode;
-        std::cout << " uri : " << rep.getUri() << std::endl;
 
         if (rep.getUri() == "/notification/message" && rep.hasAttribute("messageId")
                 && rep.getValue<int>("messageId") != 1)
         {
-            std::cout << "ID : " << rep.getValue<int>("messageId") << std::endl;
-            std::cout << "TITLE : " << rep.getValueToString("title") << std::endl;
-            std::cout << "CONTENT : " << rep.getValueToString("contentText") << std::endl;
             m_messageFunc(int(rep.getValue<int>("messageId")),
                           std::string(rep.getValueToString("title")),
                           std::string(rep.getValueToString("contentText")),
                           std::string(rep.getValueToString("source")));
+            if(rep.getValue<int>("messageId") == 3)
+            {
+                m_topicResource->get(OC::QueryParamsMap(),
+                        std::bind(&NSConsumerSimulator::onTopicGet, this, std::placeholders::_1,
+                                std::placeholders::_2, std::placeholders::_3, m_topicResource),
+                                OC::QualityOfService::LowQos);
+            }
         }
         else if (rep.getUri() == "/notification/sync")
         {
-            std::cout << "else if (rep.getUri() == sync) " << std::endl;
             m_syncFunc(int(rep.getValue<int>("state")), int(rep.getValue<int>("messageId")));
         }
     }
+    void onTopicGet(const OC::HeaderOptions &/*headerOption*/,
+            const OC::OCRepresentation & /*rep*/ , const int /*eCode*/,
+            std::shared_ptr<OC::OCResource> /*resource*/)
+    {
+    }
 
     OCStackResult msgResourceCancelObserve(OC::QualityOfService qos)
     {
index 87cb5b2..3f1deaf 100644 (file)
@@ -461,6 +461,83 @@ TEST_F(NotificationServiceConsumerTest, ExpectCallbackDismissCheckWhenConsumerPo
     EXPECT_EQ(OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED, type);
 }
 
+TEST_F(NotificationServiceConsumerTest, ExpectGetProviderSuccessWithValidProviderId)
+{
+    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);
+}
+
+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");
+
+    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() )
+    {
+        if((*it1)->getTopicName() !=  *it2)
+        {
+            isSame = false; break;
+        }
+            it1++;it2++;
+     }
+
+
+    EXPECT_EQ(true, isSame);
+}
+
+TEST_F(NotificationServiceConsumerTest, ExpectFailUpdateTopicOnConsumer)
+{
+    OIC::Service::NSTopicsList * retTopic = g_provider->getTopicList();
+    for (auto it : retTopic->getTopicsList())
+    {
+        it->setState(OIC::Service::NSTopic::NSTopicState::SUBSCRIBED);
+    }
+    OIC::Service::NSResult ret = g_provider->updateTopicList(retTopic);
+
+    EXPECT_EQ(OIC::Service::NSResult::ERROR, ret);
+}
+
+
 TEST_F(NotificationServiceConsumerTest, ExpectCallbackDeletedProvider)
 {
     OIC::Service::NSProviderState type = OIC::Service::NSProviderState::ALLOW;
@@ -469,7 +546,6 @@ TEST_F(NotificationServiceConsumerTest, ExpectCallbackDeletedProvider)
     {
         std::cout << "Income Changed Callback : " << std::endl;
         type = state;
-        responseCon.notify_all();
     });
 
     g_providerSimul.deleteNotificationResource();
@@ -480,15 +556,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();
-//
-//}
index d87303b..2245567 100644 (file)
@@ -22,6 +22,7 @@
 #define _NS_PROVIDER_SERVICE_SIMULATOR_H_
 
 #include <iostream>
+#include <memory>
 
 #include "OCPlatform.h"
 #include "OCApi.h"
@@ -34,15 +35,33 @@ namespace
         NS_NOTIFICATION,
         NS_MESSAGE,
         NS_SYNC,
+        NS_TOPIC,
+    };
+    enum class messageType
+    {
+        NS_ALLOW = 1,
+        NS_DENY,
+        NS_TOPIC,
     };
 }
 
 class NSProviderSimulator
 {
+public:
+    enum class TopicAllowState
+    {
+        DENY,
+        ALLOW
+    };
+    typedef std::pair<std::string, TopicAllowState> NS_TopicState;
+    typedef std::map<std::string, TopicAllowState> NS_TopicStateList;
+
+    typedef std::list<std::string> NS_TopicList;
 private:
     OCResourceHandle m_notificationHandle;
     OCResourceHandle m_messageHandle;
     OCResourceHandle m_syncHandle;
+    OCResourceHandle m_topicHandle;
     OC::OCRepresentation m_syncRep;
     OC::OCRepresentation m_messageRep;
     int m_accepter;
@@ -50,16 +69,22 @@ private:
     std::string m_notificationUri;
     std::string m_messageUri;
     std::string m_syncUri;
+    std::string m_topicUri;
+    NS_TopicList m_topicList;
+    NS_TopicStateList m_allowedTopicList;
 
     OC::ObservationIds m_syncObservers;
 
 public:
     NSProviderSimulator()
-    : m_notificationHandle(), m_messageHandle(), m_syncHandle(),
+    : m_notificationHandle(), m_messageHandle(), m_syncHandle(), m_topicHandle(),
       m_syncRep(), m_messageRep(), m_accepter(0),
       m_notificationUri(std::string("/notification")),
       m_messageUri(std::string("/message")),
       m_syncUri(std::string("/sync")),
+      m_topicUri(std::string("/topic")),
+      m_topicList(),
+      m_allowedTopicList(),
       m_syncObservers()
     {
 
@@ -97,10 +122,12 @@ private:
                 {
                     std::string msgUri = m_notificationUri + m_messageUri;
                     std::string syncUri = m_notificationUri + m_syncUri;
+                    std::string topicUri = m_notificationUri + m_topicUri;
                     std::string providerId = "123456789012345678901234567890123456";
-                    rep.setValue("subControllability", (bool) m_accepter);
+                    rep.setValue("subControllability", m_accepter);
                     rep.setValue("messageUri", msgUri);
                     rep.setValue("syncUri", syncUri);
+                    rep.setValue("topicUri", topicUri);
                     rep.setValue("providerId", providerId);
                 }
                 else if (type == requestType::NS_SYNC)
@@ -111,6 +138,35 @@ private:
                 {
                     rep = m_messageRep;
                 }
+                else if (type == requestType::NS_TOPIC)
+                {
+                    if (m_allowedTopicList.empty())
+                    {
+                        std::for_each (m_topicList.begin(), m_topicList.end(),
+                            [this](const std::string & topic)
+                            {
+                                m_allowedTopicList.insert(
+                                        std::make_pair<std::string, TopicAllowState>(
+                                                std::string(topic), TopicAllowState::DENY));
+                            }
+                        );
+                    }
+
+                    std::vector<OC::OCRepresentation> topicArr;
+
+                    std::for_each (m_allowedTopicList.begin(), m_allowedTopicList.end(),
+                        [& topicArr](const NS_TopicState & topicState)
+                        {
+                            OC::OCRepresentation topic;
+                            topic.setValue("topicName", topicState.first);
+                            topic.setValue("topicState", (int) topicState.second);
+                            topicArr.push_back(topic);
+                        }
+                    );
+
+                    rep.setValue<std::vector<OC::OCRepresentation>>
+                        ("topicList", topicArr);
+                }
                 else
                 {
                     return NULL;
@@ -120,19 +176,39 @@ private:
                 return response;
             }
 
-            else if (request == "POST" && type == requestType::NS_SYNC)
+            else if (request == "POST")
             {
-                m_syncRep = requests->getResourceRepresentation();
+                if (type == requestType::NS_SYNC)
+                {
+                    m_syncRep = requests->getResourceRepresentation();
 
-                std::cout << "Receive POST at Sync" << std::endl;
-                std::cout << "provider Id : " << m_syncRep.getValueToString("providerId") << std::endl;
-                std::cout << "Sync State : " << m_syncRep.getValueToString("state") << std::endl;
+                    std::cout << "Receive POST for Sync" << std::endl;
+                    std::cout << "provider Id : " << m_syncRep.getValueToString("providerId") << std::endl;
+                    std::cout << "Sync State : " << m_syncRep.getValueToString("state") << std::endl;
 
-                response->setResourceRepresentation(m_syncRep);
+                    response->setResourceRepresentation(m_syncRep);
 
-                OC::OCPlatform::notifyListOfObservers(m_syncHandle, m_syncObservers, response);
+                    OC::OCPlatform::notifyAllObservers(m_syncHandle);
 
-                return response;
+                    return response;
+                }
+                else if (type == requestType::NS_TOPIC)
+                {
+                    auto receivePayload =
+                            requests->getResourceRepresentation()
+                            .getValue<std::vector<OC::OCRepresentation>>("topicList");
+
+                    std::for_each (receivePayload.begin(), receivePayload.end(),
+                          [this](const OC::OCRepresentation & rep)
+                          {
+                              auto tmp = m_allowedTopicList.find(rep.getValueToString("topicName"));
+                              if (tmp != m_allowedTopicList.end())
+                              {
+                                  tmp->second = (TopicAllowState) rep.getValue<int>("topicState");
+                              }
+                          }
+                    );
+                }
             }
         }
 
@@ -159,7 +235,7 @@ private:
         {
             OC::OCRepresentation rep;
             std::string providerId = "123456789012345678901234567890123456";
-            rep.setValue<int>("messageId", (int)1);
+            rep.setValue<int>("messageId", (int)messageType::NS_ALLOW);
             rep.setValue("providerId", providerId);
 
             auto response = std::make_shared<OC::OCResourceResponse>();
@@ -255,11 +331,58 @@ public:
         m_messageRep.setValue("providerId", providerId);
     }
 
+    void setTopics(const NS_TopicList & topics)
+    {
+        bool isChanged = false;
+        std::for_each (topics.begin(), topics.end(),
+            [this, & isChanged](const std::string & topic)
+            {
+                auto found = std::find(
+                        this->m_topicList.begin(), this->m_topicList.end(), topic);
+                if (found == this->m_topicList.end())
+                {
+                    this->m_topicList.push_back(topic);
+                    isChanged = true;
+                }
+            });
+
+        if (isChanged)
+        {
+            this->notifyMessage((uint64_t)messageType::NS_TOPIC, "", "");
+        }
+    }
+
+    NS_TopicList getTopics() const
+    {
+        return m_topicList;
+    }
+
+    void updateTopicState(const NS_TopicStateList & allowedTopics)
+    {
+        std::for_each (allowedTopics.begin(), allowedTopics.end(),
+            [this](const NS_TopicState & allowedTopic)
+            {
+                auto found = this->m_allowedTopicList.find(allowedTopic.first);
+                if (found != this->m_allowedTopicList.end())
+                {
+                    found->second = allowedTopic.second;
+                }
+            });
+    }
+
+    NS_TopicStateList getTopicAllowState() const
+    {
+        return m_allowedTopicList;
+    }
+
     void deleteNotificationResource()
     {
         OC::OCPlatform::unregisterResource(m_notificationHandle);
         OC::OCPlatform::unregisterResource(m_messageHandle);
         OC::OCPlatform::unregisterResource(m_syncHandle);
+        OC::OCPlatform::unregisterResource(m_topicHandle);
+        m_allowedTopicList.clear();
+        m_topicList.clear();
     }
 
     void createNotificationResource()
@@ -277,11 +400,28 @@ public:
         OC::OCPlatform::startPresence(30);
 
         std::string notificationUri = m_notificationUri;
-        std::string resourceTypeName = "oic.wk.notification.message";
+        std::string resourceTypeName = "oic.wk.notification.topic";
         std::string resourceInterface = OC::DEFAULT_INTERFACE;
 
         uint8_t resourceProperty = OC_OBSERVABLE;
-        std::string childUri = uri + m_messageUri;
+        std::string childUri = uri + m_topicUri;
+        try
+        {
+            OC::OCPlatform::registerResource(
+                    m_topicHandle, childUri,
+                    resourceTypeName, resourceInterface,
+                    std::bind(& NSProviderSimulator::entityHandler, this,
+                            std::placeholders::_1, requestType::NS_TOPIC),
+                            resourceProperty);
+        }
+        catch(std::exception & e)
+        {
+            std::cout << e.what() << std::endl;
+        }
+
+        //resourceProperty |= OC_OBSERVABLE;
+        resourceTypeName = "oic.wk.notification.message";
+        childUri = uri + m_messageUri;
         try
         {
             OC::OCPlatform::registerResource(
@@ -312,7 +452,7 @@ public:
             std::cout << e.what() << std::endl;
         }
 
-        resourceProperty = OC_DISCOVERABLE;
+        resourceProperty |= OC_DISCOVERABLE;
         resourceTypeName = "oic.wk.notification";
         try
         {
index b5e76af..a9acd13 100644 (file)
@@ -258,10 +258,8 @@ TEST_F(NotificationProviderServiceTest, ExpectCallNotifyOnConsumerByAcceptIsTrue
     msg->setTitle(std::string("Title"));
     msg->setContentText(std::string("ContentText"));
     msg->setSourceName(std::string("OCF"));
-    OIC::Service::NSProviderService::getInstance()->registerTopic("OCF_TOPIC1");
-    g_consumer->setTopic("OCF_TOPIC1");
+
     OIC::Service::NSProviderService::getInstance()->sendMessage(msg);
-    OIC::Service::NSProviderService::getInstance()->unregisterTopic("OCF_TOPIC1");
     std::unique_lock< std::mutex > lock { mutexForCondition };
     responseCon.wait_for(lock, g_waitForResponse);
 }
@@ -481,6 +479,14 @@ TEST_F(NotificationProviderServiceTest, ExpectEqualUnSetConsumerTopicsAndGetCons
     responseCon.wait_for(lock, g_waitForResponse);
 }
 
+TEST_F(NotificationProviderServiceTest, ExpectFailSendMessage)
+{
+    OIC::Service::NSResult result = OIC::Service::NSResult::OK;
+    result = OIC::Service::NSProviderService::getInstance()->sendMessage(nullptr);
+
+    EXPECT_EQ(result, OIC::Service::NSResult::ERROR);
+}
+
 TEST_F(NotificationProviderServiceTest, CancelObserves)
 {
     bool ret = g_consumerSimul.cancelObserves();