Modify resourceCache Unittest code
authorYounghyunJoo <yh_.joo@samsung.com>
Mon, 3 Aug 2015 02:20:39 +0000 (11:20 +0900)
committerMadan Lanka <lanka.madan@samsung.com>
Mon, 3 Aug 2015 04:18:41 +0000 (04:18 +0000)
- modify DataCache handler type which is low pointer to shared_ptr on the dataCache for updated DataCache code
- add unittest execute option(TEST) into the SConscript file
- remove useless comments
- set code format

Change-Id: I69d22561d0d028b624c5f836e9b8bdd1cdaa44e6
Signed-off-by: YounghyunJoo <yh_.joo@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/2043
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Madan Lanka <lanka.madan@samsung.com>
service/resource-encapsulation/src/resourceCache/src/DataCache.cpp
service/resource-encapsulation/src/resourceCache/src/ResourceCacheManager.cpp
service/resource-encapsulation/src/resourceCache/unittests/DataCacheTest.cpp
service/resource-encapsulation/src/resourceCache/unittests/ResourceCacheTest.cpp
service/resource-encapsulation/src/resourceCache/unittests/SConscript

index ac2f1e2..b0bc45f 100644 (file)
@@ -43,7 +43,7 @@ namespace OIC
                 int _result, int _seq, std::weak_ptr<DataCache> rpPtr)
             {
                 std::shared_ptr<DataCache> Ptr = rpPtr.lock();
-                if(Ptr)
+                if (Ptr)
                 {
                     Ptr->onObserve(_hos, _rep, _result, _seq);
                 }
@@ -52,16 +52,16 @@ namespace OIC
             ObserveCB verifiedObserveCB(std::weak_ptr<DataCache> rpPtr)
             {
                 return std::bind(verifyObserveCB,
-                        std::placeholders::_1, std::placeholders::_2,
-                        std::placeholders::_3, std::placeholders::_4, rpPtr);
+                                 std::placeholders::_1, std::placeholders::_2,
+                                 std::placeholders::_3, std::placeholders::_4, rpPtr);
             }
 
             void verifyGetCB(
-                    const HeaderOptions &_hos, const ResponseStatement &_rep,
-                    int _result, std::weak_ptr<DataCache> rpPtr)
+                const HeaderOptions &_hos, const ResponseStatement &_rep,
+                int _result, std::weak_ptr<DataCache> rpPtr)
             {
                 std::shared_ptr<DataCache> Ptr = rpPtr.lock();
-                if(Ptr)
+                if (Ptr)
                 {
                     Ptr->onGet(_hos, _rep, _result);
                 }
@@ -70,8 +70,8 @@ namespace OIC
             GetCB verifiedGetCB(std::weak_ptr<DataCache> rpPtr)
             {
                 return std::bind(verifyGetCB,
-                        std::placeholders::_1, std::placeholders::_2,
-                        std::placeholders::_3, rpPtr);
+                                 std::placeholders::_1, std::placeholders::_2,
+                                 std::placeholders::_3, rpPtr);
             }
         }
 
@@ -100,12 +100,13 @@ namespace OIC
                 subscriberList.release();
             }
 
-            if(mode == CACHE_MODE::OBSERVE)
+            if (mode == CACHE_MODE::OBSERVE)
             {
                 try
                 {
                     sResource->cancelObserve();
-                } catch (...)
+                }
+                catch (...)
                 {
                     // ignore the exception because data cache was released.
                 }
@@ -202,7 +203,7 @@ namespace OIC
         }
 
         void DataCache::onObserve(const HeaderOptions & /*_hos*/,
-                const ResponseStatement & _rep, int _result, int _seq)
+                                  const ResponseStatement &_rep, int _result, int _seq)
         {
 
             if (_result != OC_STACK_OK || _rep.getAttributes().empty() || lastSequenceNum > _seq)
@@ -285,14 +286,14 @@ namespace OIC
 
         void DataCache::onTimeOut(unsigned int /*timerID*/)
         {
-            if(mode == CACHE_MODE::OBSERVE)
+            if (mode == CACHE_MODE::OBSERVE)
             {
                 sResource->cancelObserve();
                 mode = CACHE_MODE::FREQUENCY;
 
                 networkTimer.cancelTimer(networkTimeOutHandle);
                 networkTimeOutHandle = networkTimer.postTimer(
-                        CACHE_DEFAULT_EXPIRED_MILLITIME, pTimerCB);
+                                           CACHE_DEFAULT_EXPIRED_MILLITIME, pTimerCB);
 
                 pollingHandle = pollingTimer.postTimer(CACHE_DEFAULT_REPORT_MILLITIME, pPollingCB);
                 return;
index 8018c3e..e53608b 100644 (file)
@@ -161,7 +161,7 @@ namespace OIC
                 throw InvalidParameterException {"[getCachedData] Primitive Resource is invaild"};
             }
 
-            if(handler->isCachedData() == false)
+            if (handler->isCachedData() == false)
             {
                 throw HasNoCachedDataException {"[getCachedData] Cached Data is not stored"};
             }
@@ -182,7 +182,7 @@ namespace OIC
                 throw InvalidParameterException {"[getCachedData] CacheID is invaild"};
             }
 
-            if(handler->isCachedData() == false)
+            if (handler->isCachedData() == false)
             {
                 throw HasNoCachedDataException {"[getCachedData] Cached Data is not stored"};
             }
index 31610ba..41c3e6a 100644 (file)
@@ -41,7 +41,7 @@ class DataCacheTest : public TestWithMock
         void(const OIC::Service::HeaderOptions &, const OIC::Service::ResponseStatement &, int,
              int) > ObserveCallback;
     public:
-        DataCache *cacheHandler;
+        std::shared_ptr<DataCache> cacheHandler;
         PrimitiveResource::Ptr pResource;
         CacheCB cb;
         CacheID id;
@@ -51,13 +51,14 @@ class DataCacheTest : public TestWithMock
         {
             TestWithMock::SetUp();
             pResource = PrimitiveResource::Ptr(mocks.Mock< PrimitiveResource >(), [](PrimitiveResource *) {});
-            cacheHandler = new DataCache();
+            cacheHandler.reset(new DataCache());
             cb = ([](std::shared_ptr<PrimitiveResource >, const RCSResourceAttributes &)->OCStackResult {return OC_STACK_OK;});
         }
 
         virtual void TearDown()
         {
-            delete cacheHandler;
+            cacheHandler.reset();
+            pResource.reset();
             TestWithMock::TearDown();
         }
 };
@@ -247,19 +248,12 @@ TEST_F(DataCacheTest, requestGet_normalCasetest)
     mocks.OnCall(pResource.get(), PrimitiveResource::requestGet).Do(
         [](GetCallback callback)
     {
-        std::cout << "HelloWorld" << std::endl;
         OIC::Service::HeaderOptions hos;
-
         OIC::Service::RCSResourceAttributes attr;
-        //attr = mocks.Mcok< OIC::Service::RCSResourceAttributes >();
         OIC::Service::ResponseStatement rep(attr);
-        //rep = mocks.Mock< OIC::Service::ResponseStatement >(attr);
-        //fakeResource = mocks.Mock< FakeOCResource >();
-        //mocks.OnCallFunc(RCSResourceAttributes::empty()).Return(false);
         callback(hos, rep, OC_STACK_OK);
         return;
-    }
-    );
+    });
     mocks.OnCall(pResource.get(), PrimitiveResource::cancelObserve);
 
     cacheHandler->initializeDataCache(pResource);
index fec80f8..14f3f40 100644 (file)
@@ -46,6 +46,7 @@ class ResourceCacheManagerTest : public TestWithMock
 
         virtual void TearDown()
         {
+            pResource.reset();
             TestWithMock::TearDown();
         }
 };
@@ -279,6 +280,7 @@ TEST_F(ResourceCacheManagerTest, getResourceCacheStateCacheID_cacheIDIsZero)
 TEST_F(ResourceCacheManagerTest, getResourceCacheStateCacheID_handlerIsNULL)
 {
 
+    id = 1;
     ASSERT_EQ(cacheInstance->getResourceCacheState(id), CACHE_STATE::NONE);
 }
 
index 15fc270..1561068 100644 (file)
@@ -75,3 +75,11 @@ cache_test_src = env.Glob('./*.cpp')
 cache_test = cache_test_env.Program('cache_test', cache_test_src)
 Alias("cache_test", cache_test)
 env.AppendTarget('cache_test')
+
+if env.get('TEST') == '1':
+        target_os = env.get('TARGET_OS')
+        if target_os == 'linux':
+                from tools.scons.RunTest import *
+                run_test(cache_test_env,
+                         'cache_test.memcheck',
+                         'service/resource-encapsulation/src/resourceCache/unittests/cache_test')
\ No newline at end of file