Modify DiscoveryManager and DiscoveryManagerTest
authorChaJiwon <jw_wonny.cha@samsung.com>
Mon, 1 Feb 2016 08:18:39 +0000 (17:18 +0900)
committerMadan Lanka <lanka.madan@samsung.com>
Thu, 4 Feb 2016 05:20:11 +0000 (05:20 +0000)
- Discovery Test using multiple resource types
- Check the size of vector that contains resource types

Change-Id: Ia5c8db2413d787ead4490cc2b128fcc2c2a5bbe6
Signed-off-by: ChaJiWon <jw_wonny.cha@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/4903
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Madan Lanka <lanka.madan@samsung.com>
service/resource-encapsulation/src/resourceClient/RCSDiscoveryManager.cpp
service/resource-encapsulation/src/resourceClient/RCSDiscoveryManagerImpl.cpp
service/resource-encapsulation/src/resourceClient/RCSDiscoveryManagerImpl.h [changed mode: 0644->0755]
service/resource-encapsulation/unittests/DiscoveryManagerTest.cpp [changed mode: 0644->0755]

index b658a7d..6fbeb73 100755 (executable)
 
 #include "RCSDiscoveryManagerImpl.h"
 
-namespace
-{
-    constexpr char ALL_RESOURCE_TYPES[] = "";
-}
-
 namespace OIC
 {
     namespace Service
     {
         RCSDiscoveryManager::DiscoveryTask::DiscoveryTask(unsigned int id) :
-                m_id{ id }
-        {
-        }
+                m_id { id } {}
 
         bool RCSDiscoveryManager::DiscoveryTask::isCanceled()
         {
@@ -42,7 +35,8 @@ namespace OIC
 
         void RCSDiscoveryManager::DiscoveryTask::cancel()
         {
-            if (isCanceled()) return;
+            if (isCanceled())
+                return;
 
             RCSDiscoveryManagerImpl::getInstance()->cancel(m_id);
             m_id = RCSDiscoveryManagerImpl::INVALID_ID;
@@ -57,15 +51,16 @@ namespace OIC
         RCSDiscoveryManager::DiscoveryTask::Ptr RCSDiscoveryManager::discoverResource(
                 const RCSAddress& address, ResourceDiscoveredCallback cb)
         {
-            return discoverResourceByType(address, OC_RSRVD_WELL_KNOWN_URI, ALL_RESOURCE_TYPES,
-                    std::move(cb));
+            return discoverResourceByType(address, OC_RSRVD_WELL_KNOWN_URI,
+                    RCSDiscoveryManagerImpl::ALL_RESOURCE_TYPE, std::move(cb));
         }
 
         RCSDiscoveryManager::DiscoveryTask::Ptr RCSDiscoveryManager::discoverResource(
                 const RCSAddress& address, const std::string& relativeUri,
                 ResourceDiscoveredCallback cb)
         {
-            return discoverResourceByType(address, relativeUri, ALL_RESOURCE_TYPES, std::move(cb));
+            return discoverResourceByType(address, relativeUri,
+                    RCSDiscoveryManagerImpl::ALL_RESOURCE_TYPE, std::move(cb));
         }
 
         RCSDiscoveryManager::DiscoveryTask::Ptr RCSDiscoveryManager::discoverResourceByType(
index a44399f..cc9db98 100755 (executable)
@@ -28,7 +28,6 @@
 
 namespace
 {
-    constexpr char ALL_RESOURCE_TYPE[] = "";
     constexpr unsigned int POLLING_INTERVAL_TIME = 60000;
 
     std::string makeResourceId(const std::shared_ptr< OIC::Service::PrimitiveResource >& resource)
@@ -42,6 +41,7 @@ namespace OIC
     namespace Service
     {
         constexpr RCSDiscoveryManagerImpl::ID RCSDiscoveryManagerImpl::INVALID_ID;
+        constexpr char const* RCSDiscoveryManagerImpl::ALL_RESOURCE_TYPE;
 
         RCSDiscoveryManagerImpl::RCSDiscoveryManagerImpl()
         {
@@ -83,11 +83,11 @@ namespace OIC
                 throw RCSInvalidParameterException{ "Callback is empty" };
             }
 
-            for(const auto& it : resourceTypes)
+            for(auto it = resourceTypes.begin()+1; it < resourceTypes.end(); it++)
             {
-                if (it.compare(ALL_RESOURCE_TYPE) == 0)
+                if ((*it).compare(ALL_RESOURCE_TYPE) == 0)
                 {
-                    throw RCSInvalidParameterException{ "ResourceType is duplicated!" };
+                   throw RCSInvalidParameterException{ "ResourceType is duplicated!" };
                 }
             }
 
@@ -182,7 +182,7 @@ namespace OIC
         {
             if (m_resourceTypes.empty())
             {
-                m_resourceTypes.push_back(ALL_RESOURCE_TYPE);
+                m_resourceTypes.push_back(RCSDiscoveryManagerImpl::ALL_RESOURCE_TYPE);
             }
         }
 
old mode 100644 (file)
new mode 100755 (executable)
index b9f3d9d..b1d75f0
@@ -82,6 +82,7 @@ namespace OIC
                  * @note This is generated for each discovery request
                  */
                 typedef unsigned int ID;
+                constexpr static char const* ALL_RESOURCE_TYPE = "";
 
             public:
 
old mode 100644 (file)
new mode 100755 (executable)
index 4eedc82..1029a36
@@ -40,12 +40,7 @@ constexpr char SECOND_RESOURCETYPE[]{ "resource.type.second" };
 constexpr char RESOURCEINTERFACE[]{ "oic.if.baseline" };
 constexpr int DEFAULT_DISCOVERYTASK_DELAYTIME = 3000;
 
-int callTimes = 0;
-
-void resourceDiscoveredForTwoTimesCall(RCSRemoteResourceObject::Ptr)
-{
-    callTimes++;
-}
+void resourceDiscoveredForTwoTimesCall(RCSRemoteResourceObject::Ptr) {}
 void resourceDiscoveredForCall(RCSRemoteResourceObject::Ptr) {}
 void resourceDiscoveredForNeverCall(RCSRemoteResourceObject::Ptr) {}
 
@@ -95,15 +90,6 @@ public:
         cond.notify_all();
     }
 
-    void proceed_on_condition()
-    {
-        cond.notify_all();
-        if(callTimes != 2)
-        {
-            waitForDiscoveryTask();
-        }
-    }
-
     void waitForDiscoveryTask(int waitingTime = DEFAULT_DISCOVERYTASK_DELAYTIME)
     {
         std::unique_lock< std::mutex > lock{ mutex };
@@ -166,38 +152,44 @@ TEST_F(DiscoveryManagerTest, resourceIsSupportedPresenceAndAfterDiscovering)
     stopPresence();
 }
 
-TEST_F(DiscoveryManagerTest, discoveryRequestByMultipleResourceTypesAndFindResourcesOfTwoResourceTypes)
+TEST_F(DiscoveryManagerTest, discoveryRequestByMultipleResourceTypesAndFindResourceOfOneResourceType)
 {
     std::vector<std::string> resourceTypes{RESOURCETYPE, SECOND_RESOURCETYPE};
     const std::string uri  = "/oic/res";
-    callTimes = 0;
 
-    createResource();
     createSecondResource();
 
-    mocks.ExpectCallFunc(resourceDiscoveredForTwoTimesCall).Do(
-        [this](RCSRemoteResourceObject::Ptr){ proceed_on_condition(); });
+    mocks.ExpectCallFunc(resourceDiscoveredForCall).Do(
+        [this](RCSRemoteResourceObject::Ptr){ proceed();});
 
     RCSDiscoveryManager::getInstance()->discoverResourceByTypes(RCSAddress::multicast(),
-            uri, resourceTypes, resourceDiscoveredForTwoTimesCall);
+            uri, resourceTypes, resourceDiscoveredForCall);
 
     waitForDiscoveryTask();
 }
 
-TEST_F(DiscoveryManagerTest, discoveryRequestByMultipleResourceTypesAndFindResourceOfOneResourceType)
+TEST_F(DiscoveryManagerTest, discoveryRequestByMultipleResourceTypesAndFindResourcesOfTwoResourceTypes)
 {
     std::vector<std::string> resourceTypes{RESOURCETYPE, SECOND_RESOURCETYPE};
     const std::string uri  = "/oic/res";
+    unsigned int callTimes = 0;
 
+    createResource();
     createSecondResource();
 
-    mocks.ExpectCallFunc(resourceDiscoveredForCall).Do(
-        [this](RCSRemoteResourceObject::Ptr){ proceed();});
+    mocks.OnCallFunc(resourceDiscoveredForTwoTimesCall).Do(
+        [this, &callTimes](RCSRemoteResourceObject::Ptr){ 
+            callTimes++;
+            if (callTimes == 2) proceed();
+        }
+    );
 
     RCSDiscoveryManager::getInstance()->discoverResourceByTypes(RCSAddress::multicast(),
-            uri, resourceTypes, resourceDiscoveredForCall);
+            uri, resourceTypes, resourceDiscoveredForTwoTimesCall);
 
     waitForDiscoveryTask();
+
+    EXPECT_EQ(resourceTypes.size(), callTimes);
 }
 
 TEST_F(DiscoveryManagerTest, cancelDiscoveryTaskAfterDiscoveryResource)