Update unittest of notification-manager.
authorjyong2.kim <jyong2.kim@samsung.com>
Tue, 22 Sep 2015 06:58:28 +0000 (15:58 +0900)
committerMadan Lanka <lanka.madan@samsung.com>
Tue, 22 Sep 2015 11:09:48 +0000 (11:09 +0000)
Adding unittest for HostingObject Class,
and fixing release of smart pointer.

Change-Id: Ia27070b5908c5b42348a430dc602e136b70a9217
Signed-off-by: jyong2.kim <jyong2.kim@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/2911
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Hun-je Yeon <hunje.yeon@samsung.com>
Reviewed-by: Madan Lanka <lanka.madan@samsung.com>
service/notification-manager/NotificationManager/src/unittest/HostingObjectUnitTest.cpp [new file with mode: 0644]
service/notification-manager/NotificationManager/src/unittest/RequestObjectUnitTest.cpp
service/notification-manager/NotificationManager/src/unittest/ResourceEncapsulationTestSimulator.h
service/notification-manager/NotificationManager/src/unittest/ResourceHostingTest.cpp [deleted file]
service/notification-manager/NotificationManager/src/unittest/ResourceHostingUnitTest.cpp [new file with mode: 0644]

diff --git a/service/notification-manager/NotificationManager/src/unittest/HostingObjectUnitTest.cpp b/service/notification-manager/NotificationManager/src/unittest/HostingObjectUnitTest.cpp
new file mode 100644 (file)
index 0000000..4f28cec
--- /dev/null
@@ -0,0 +1,199 @@
+//******************************************************************
+//
+// Copyright 2015 Samsung Electronics All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+#include <memory>
+
+#include "ResourceEncapsulationTestSimulator.h"
+#include "HostingObject.h"
+
+#include "RCSDiscoveryManager.h"
+
+using namespace testing;
+using namespace OIC::Service;
+
+namespace
+{
+    bool isDeleted = false;
+    void onDestroy(std::weak_ptr<HostingObject> rPtr)
+    {
+        HostingObject::Ptr ptr = rPtr.lock();
+        if(ptr) ptr.reset();
+        isDeleted = true;
+    }
+    void onDiscoveryResource(RCSRemoteResourceObject::Ptr){ }
+
+    void onUpdatedCache(const RCSResourceAttributes &) { }
+}
+
+class HostingObjectTest : public TestWithMock
+{
+public:
+    ResourceEncapsulationTestSimulator::Ptr testObject;
+    RCSRemoteResourceObject::Ptr remoteObject;
+
+    std::mutex mutexForCondition;
+    std::condition_variable responseCon;
+
+protected:
+
+    void SetUp()
+    {
+        TestWithMock::SetUp();
+
+        testObject = std::make_shared<ResourceEncapsulationTestSimulator>();
+        testObject->defaultRunSimulator();
+        remoteObject = testObject->getRemoteResource();
+
+        isDeleted = false;
+    }
+
+    void TearDown()
+    {
+        TestWithMock::TearDown();
+
+        if(remoteObject.use_count() > 0)
+        {
+            if(remoteObject->isCaching())
+            {
+                remoteObject->stopCaching();
+            }
+            if(remoteObject->isMonitoring())
+            {
+                remoteObject->stopMonitoring();
+            }
+        }
+        testObject->destroy();
+    }
+
+public:
+    void waitForCondition(int waitingTime = 1000)
+    {
+        std::unique_lock< std::mutex > lock{ mutexForCondition };
+        responseCon.wait_for(lock, std::chrono::milliseconds{ waitingTime });
+    }
+
+    void notifyCondition()
+    {
+        responseCon.notify_all();
+    }
+
+};
+
+TEST_F(HostingObjectTest, startCachingAtInitialize)
+{
+    HostingObject::Ptr instance = std::make_shared<HostingObject>();
+    instance->initializeHostingObject(
+            remoteObject, std::bind(onDestroy, std::weak_ptr<HostingObject>(instance)));
+
+    EXPECT_TRUE(remoteObject->isCaching());
+}
+
+TEST_F(HostingObjectTest, startMonitoringAtInitialize)
+{
+    HostingObject::Ptr instance = std::make_shared<HostingObject>();
+    instance->initializeHostingObject(
+            remoteObject, std::bind(onDestroy, std::weak_ptr<HostingObject>(instance)));
+
+    ASSERT_TRUE(remoteObject->isMonitoring());
+}
+
+TEST_F(HostingObjectTest, getRemoteResourceisValid)
+{
+    HostingObject::Ptr instance = std::make_shared<HostingObject>();
+    instance->initializeHostingObject(
+            remoteObject, std::bind(onDestroy, std::weak_ptr<HostingObject>(instance)));
+
+    ASSERT_EQ(remoteObject->getUri(), instance->getRemoteResource()->getUri());
+}
+
+TEST_F(HostingObjectTest, createMirroredServer)
+{
+    int waitForResponse = 1000;
+    std::string uri = "";
+
+    HostingObject::Ptr instance = std::make_shared<HostingObject>();
+    instance->initializeHostingObject(
+            remoteObject, std::bind(onDestroy, std::weak_ptr<HostingObject>(instance)));
+    std::this_thread::sleep_for(std::chrono::milliseconds {waitForResponse});
+
+    std::unique_ptr<RCSDiscoveryManager::DiscoveryTask> discoveryTask = { };
+
+    mocks.OnCallFunc(onDiscoveryResource).Do(
+            [this, &uri, &discoveryTask](RCSRemoteResourceObject::Ptr ptr)
+            {
+                if(ptr->getUri() == testObject->getHostedServerUri())
+                {
+                    uri = ptr->getUri();
+                    discoveryTask->cancel();
+                    notifyCondition();
+                }
+            });
+
+    discoveryTask = RCSDiscoveryManager::getInstance()->discoverResourceByType(
+            RCSAddress::multicast(), "resource.hosting", onDiscoveryResource);
+    waitForCondition(waitForResponse);
+
+    EXPECT_EQ(testObject->getHostedServerUri(), uri);
+}
+
+TEST_F(HostingObjectTest, UpdateCachedDataWhenChangedOriginResource)
+{
+    int waitForResponse = 1000;
+    HostingObject::Ptr instance = std::make_shared<HostingObject>();
+    instance->initializeHostingObject(
+            remoteObject, std::bind(onDestroy, std::weak_ptr<HostingObject>(instance)));
+    std::this_thread::sleep_for(std::chrono::milliseconds {waitForResponse});
+
+    std::unique_ptr<RCSDiscoveryManager::DiscoveryTask> discoveryTask = { };
+    RCSRemoteResourceObject::Ptr discoveredResource = { };
+
+    mocks.OnCallFunc(onDiscoveryResource).Do(
+            [this, &discoveredResource, &discoveryTask](RCSRemoteResourceObject::Ptr ptr)
+            {
+                if(ptr->getUri() == testObject->getHostedServerUri())
+                {
+                    discoveredResource = ptr;
+                    discoveryTask->cancel();
+                    notifyCondition();
+                }
+            });
+
+    discoveryTask =  RCSDiscoveryManager::getInstance()->discoverResourceByType(
+            RCSAddress::multicast(), "resource.hosting", onDiscoveryResource);
+    waitForCondition(waitForResponse);
+
+    RCSResourceAttributes::Value result = { };
+    mocks.OnCallFunc(onUpdatedCache).Do(
+            [this, &result](const RCSResourceAttributes & att)
+            {
+                result = att.at("Temperature");
+                notifyCondition();
+            });
+
+    discoveredResource->startCaching(onUpdatedCache);
+    std::this_thread::sleep_for(std::chrono::milliseconds {waitForResponse});
+
+    RCSResourceAttributes::Value settingValue = 10;
+    testObject->getResourceServer()->setAttribute("Temperature", settingValue);
+    waitForCondition(waitForResponse);
+
+    EXPECT_EQ(result.toString(), settingValue.toString());
+
+}
index 03fe833c5e62a74dfa30f8eecbabafae51c53fd6..94df26bc9d26e782d68e3202ce2cc61bc58bfd2e 100644 (file)
@@ -51,24 +51,24 @@ protected:
 
         testObject = std::make_shared<ResourceEncapsulationTestSimulator>();
         testObject->defaultRunSimulator();
-        server = testObject->getResourceServer();
         remoteObject = testObject->getRemoteResource();
     }
 
     void TearDown()
     {
         TestWithMock::TearDown();
-        if(remoteObject){
-          if(remoteObject->isCaching())
-          {
-              remoteObject->stopCaching();
-          }
-          if(remoteObject->isMonitoring())
-          {
-              remoteObject->stopMonitoring();
-          }
+        if(remoteObject)
+        {
+            if(remoteObject->isCaching())
+            {
+                remoteObject->stopCaching();
+            }
+            if(remoteObject->isMonitoring())
+            {
+                remoteObject->stopMonitoring();
+            }
         }
-         testObject->destroy();
+        testObject->destroy();
     }
 
 public:
index 69fbf09586b89f6e32f2c697cb55964baca648ec..ac3c61dddcf6d89e284e96def859f49bec1da6e5 100644 (file)
@@ -69,9 +69,7 @@ public:
      ATTR_VALUE(0)
     { }
     ~ResourceEncapsulationTestSimulator()
-    {
-        std::cout << "~ResourceEncapsulationTestSimulator()" << std::endl;
-    }
+    { }
 
 private:
     void onDiscoveryResource_Impl(RCSRemoteResourceObject::Ptr resourceObject)
@@ -98,10 +96,6 @@ private:
         {
             ptr->onDiscoveryResource_Impl(resourceObject);
         }
-        else
-        {
-            std::cout << "Aleady delete simulator\n";
-        }
     }
     void waitForDiscovery()
     {
@@ -127,9 +121,8 @@ private:
 public:
     void destroy()
     {
-        server.reset();
-        remoteResource.reset();
-        std::cout << "waitforptrbeingunique" << std::endl;
+        if(server.use_count()) server.reset();
+        if(remoteResource.use_count()) remoteResource.reset();
         WaitForPtrBeingUnique();
     }
     void defaultRunSimulator()
diff --git a/service/notification-manager/NotificationManager/src/unittest/ResourceHostingTest.cpp b/service/notification-manager/NotificationManager/src/unittest/ResourceHostingTest.cpp
deleted file mode 100644 (file)
index d0f630c..0000000
+++ /dev/null
@@ -1,140 +0,0 @@
-//******************************************************************
-//
-// Copyright 2015 Samsung Electronics All Rights Reserved.
-//
-//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//      http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-
-#include <memory>
-
-#include "ResourceEncapsulationTestSimulator.h"
-
-#include "ResourceHosting.h"
-
-using namespace testing;
-using namespace OIC::Service;
-
-namespace
-{
-    void onDiscoveryResource(RCSRemoteResourceObject::Ptr) { }
-}
-
-class ResourceHostingTest : public TestWithMock
-{
-public:
-    std::mutex mutexForCondition;
-    std::condition_variable responseCon;
-    std::unique_ptr<RCSDiscoveryManager::DiscoveryTask> discoveryTask;
-
-protected:
-
-    void SetUp()
-    {
-        TestWithMock::SetUp();
-    }
-
-    void TearDown()
-    {
-        TestWithMock::TearDown();
-
-        if (ResourceHosting::getInstance())
-        {
-            ResourceHosting::getInstance()->stopHosting();
-        }
-
-    }
-
-public:
-    void waitForCondition(int waitingTime = 1000)
-    {
-        std::unique_lock< std::mutex > lock{ mutexForCondition };
-        responseCon.wait_for(lock, std::chrono::milliseconds{ waitingTime });
-        std::cout << "condition return back" << std::endl;
-    }
-
-    void notifyCondition()
-    {
-        std::cout << "notify condition to all" << std::endl;
-        responseCon.notify_all();
-    }
-
-};
-
-TEST(ResourceHostingSTATICMethodTest, getInstanceAllwaysSameReturnInstance)
-{
-    EXPECT_EQ(ResourceHosting::getInstance(), ResourceHosting::getInstance());
-}
-
-TEST_F(ResourceHostingTest, HostingFoundBeforeMakeOriginServer)
-{
-    ResourceEncapsulationTestSimulator::Ptr testObject
-        = std::make_shared<ResourceEncapsulationTestSimulator>();
-    testObject->createResource();
-
-    ResourceHosting::getInstance()->startHosting();
-    std::this_thread::sleep_for(std::chrono::milliseconds{1000});
-
-    std::string uri = "";
-    mocks.OnCallFunc(onDiscoveryResource).Do(
-            [this, &uri, &testObject](RCSRemoteResourceObject::Ptr ptr)
-            {
-                if(testObject.use_count() <= 0)
-                {
-                    return;
-                }
-                if(ptr->getUri() == testObject->getServerUri())
-                {
-                    uri = ptr->getUri();
-                    notifyCondition();
-                }
-            });
-
-    discoveryTask = RCSDiscoveryManager::getInstance()->discoverResourceByType(
-            RCSAddress::multicast(), "resource.hosting", onDiscoveryResource);
-    waitForCondition(2000);
-
-    std::string mirroredUri = { testObject->getServerUri() };
-
-    testObject->destroy();
-
-    ASSERT_EQ(uri, mirroredUri);
-}
-
-TEST_F(ResourceHostingTest, startHosting)
-{
-    ResourceEncapsulationTestSimulator::Ptr testObject
-        = std::make_shared<ResourceEncapsulationTestSimulator>();
-    testObject->createResource();
-
-    ResourceHosting::getInstance()->startHosting();
-    std::this_thread::sleep_for(std::chrono::milliseconds{1000});
-
-    testObject->destroy();
-}
-
-TEST_F(ResourceHostingTest, stopHosting)
-{
-    ResourceEncapsulationTestSimulator::Ptr testObject
-        = std::make_shared<ResourceEncapsulationTestSimulator>();
-    testObject->createResource();
-
-    ResourceHosting::getInstance()->startHosting();
-    std::this_thread::sleep_for(std::chrono::milliseconds{1000});
-    
-    testObject->destroy();
-
-    ResourceHosting::getInstance()->stopHosting();
-}
\ No newline at end of file
diff --git a/service/notification-manager/NotificationManager/src/unittest/ResourceHostingUnitTest.cpp b/service/notification-manager/NotificationManager/src/unittest/ResourceHostingUnitTest.cpp
new file mode 100644 (file)
index 0000000..404c953
--- /dev/null
@@ -0,0 +1,135 @@
+//******************************************************************
+//
+// Copyright 2015 Samsung Electronics All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+#include <memory>
+
+#include "ResourceEncapsulationTestSimulator.h"
+
+#include "ResourceHosting.h"
+
+using namespace testing;
+using namespace OIC::Service;
+
+namespace
+{
+    void onDiscoveryResource(RCSRemoteResourceObject::Ptr) { }
+}
+
+class ResourceHostingTest : public TestWithMock
+{
+public:
+    std::mutex mutexForCondition;
+    std::condition_variable responseCon;
+    std::unique_ptr<RCSDiscoveryManager::DiscoveryTask> discoveryTask;
+
+protected:
+
+    void SetUp()
+    {
+        TestWithMock::SetUp();
+    }
+
+    void TearDown()
+    {
+        TestWithMock::TearDown();
+
+        if (ResourceHosting::getInstance())
+        {
+            ResourceHosting::getInstance()->stopHosting();
+        }
+
+    }
+
+public:
+    void waitForCondition(int waitingTime = 1000)
+    {
+        std::unique_lock< std::mutex > lock{ mutexForCondition };
+        responseCon.wait_for(lock, std::chrono::milliseconds{ waitingTime });
+    }
+
+    void notifyCondition()
+    {
+        responseCon.notify_all();
+    }
+
+};
+
+TEST(ResourceHostingSTATICMethodTest, getInstanceAllwaysSameReturnInstance)
+{
+    EXPECT_EQ(ResourceHosting::getInstance(), ResourceHosting::getInstance());
+}
+
+TEST_F(ResourceHostingTest, HostingFoundBeforeMakeOriginServer)
+{
+    ResourceEncapsulationTestSimulator::Ptr testObject
+        = std::make_shared<ResourceEncapsulationTestSimulator>();
+    testObject->createResource();
+
+    ResourceHosting::getInstance()->startHosting();
+    std::this_thread::sleep_for(std::chrono::milliseconds{1000});
+
+    std::string uri = "";
+    mocks.OnCallFunc(onDiscoveryResource).Do(
+            [this, &uri, &testObject, &discoveryTask](RCSRemoteResourceObject::Ptr ptr)
+            {
+                if(ptr->getUri() == testObject->getHostedServerUri())
+                {
+                    uri = ptr->getUri();
+                    discoveryTask->cancel();
+                    notifyCondition();
+                }
+            });
+
+    discoveryTask = RCSDiscoveryManager::getInstance()->discoverResourceByType(
+            RCSAddress::multicast(), "resource.hosting", onDiscoveryResource);
+    waitForCondition(2000);
+
+    std::string mirroredUri = { testObject->getHostedServerUri() };
+
+    testObject->destroy();
+
+    ASSERT_EQ(uri, mirroredUri);
+}
+
+TEST_F(ResourceHostingTest, startHosting)
+{
+    ResourceEncapsulationTestSimulator::Ptr testObject
+        = std::make_shared<ResourceEncapsulationTestSimulator>();
+    testObject->createResource();
+
+    ResourceHosting::getInstance()->startHosting();
+    std::this_thread::sleep_for(std::chrono::milliseconds{1000});
+
+    testObject->destroy();
+}
+
+TEST_F(ResourceHostingTest, stopHosting)
+{
+    ResourceEncapsulationTestSimulator::Ptr testObject
+        = std::make_shared<ResourceEncapsulationTestSimulator>();
+    testObject->createResource();
+
+    ResourceHosting::getInstance()->startHosting();
+    std::this_thread::sleep_for(std::chrono::milliseconds{1000});
+
+    testObject->destroy();
+
+    ResourceHosting::getInstance()->stopHosting();
+}