[Resource-Encapsulation] improving unittest
authorRami Jung <rami.jung@samsung.com>
Sun, 2 Aug 2015 07:10:58 +0000 (16:10 +0900)
committerMadan Lanka <lanka.madan@samsung.com>
Mon, 3 Aug 2015 01:28:56 +0000 (01:28 +0000)
- adding more functions
- modification for existing functions

Change-Id: I898221305bac3c2e2779ecca9f836a8fac57f627
Signed-off-by: Rami Jung <rami.jung@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/2041
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Madan Lanka <lanka.madan@samsung.com>
service/resource-encapsulation/src/serverBuilder/unittests/RCSResourceObjectTest.cpp

index 64b0fd7..33cfaa6 100755 (executable)
@@ -587,50 +587,47 @@ public:
     }
 };
 
-TEST_F(AttributeUpdatedListenerTest, AddListenerReturnsTrueIfListenerIsCalled)
+class FunctionsForAttributeUpdatedListener
 {
-    bool called=false;
+public:
+    virtual void fCalled(const OIC::Service::RCSResourceAttributes::Value&,
+        const OIC::Service::RCSResourceAttributes::Value&)=0;
+    virtual void fNotCalled(const OIC::Service::RCSResourceAttributes::Value&,
+        const OIC::Service::RCSResourceAttributes::Value&)=0;
+};
+
+TEST_F(AttributeUpdatedListenerTest, AddListenerRunsAddedFunction)
+{
+    FunctionsForAttributeUpdatedListener *ptrMock =
+        mocks.Mock<FunctionsForAttributeUpdatedListener>();
 
     server->setAttribute(KEY, 0);
-    OCRepresentation ocRep = createOCRepresentation();
 
-    server->addAttributeUpdatedListener(KEY,
-        [&called](const OIC::Service::RCSResourceAttributes::Value&,
-        const OIC::Service::RCSResourceAttributes::Value& )
-        {
-            called=true;
-        } );
+    mocks.ExpectCall(ptrMock, FunctionsForAttributeUpdatedListener::fCalled);
 
-    handler(ResourceObjectHandlingRequestTest::createRequest(OC_REST_PUT, ocRep));
+    server->addAttributeUpdatedListener(KEY,
+        (std::bind(&FunctionsForAttributeUpdatedListener::fCalled, ptrMock, _1, _2)));
 
-    ASSERT_TRUE(called);
+    handler(createRequest(OC_REST_PUT, createOCRepresentation()));
 }
 
-TEST_F(AttributeUpdatedListenerTest, AddListenerisChangedAccordingToLastAddedFunction)
+TEST_F(AttributeUpdatedListenerTest, AddListenerRunsAccordingToLastAddedFunction)
 {
-    int called=0, expected=100;
-    string myKey("key");  // 'myKey' and 'constexpr char KEY[]' should have same value
+    FunctionsForAttributeUpdatedListener *ptrMock =
+        mocks.Mock<FunctionsForAttributeUpdatedListener>();
 
-    server->setAttribute(KEY,0);
-    OCRepresentation ocRep = createOCRepresentation();
+    string duplicateKEY(KEY);
+    server->setAttribute(KEY, 0);
 
-    server->addAttributeUpdatedListener(myKey,
-        [&called](const OIC::Service::RCSResourceAttributes::Value&,
-        const OIC::Service::RCSResourceAttributes::Value&)
-        {
-            called=10;
-        } );
+    mocks.ExpectCall(ptrMock, FunctionsForAttributeUpdatedListener::fCalled);
+    mocks.NeverCall(ptrMock, FunctionsForAttributeUpdatedListener::fNotCalled);
 
+    server->addAttributeUpdatedListener(duplicateKEY,
+        (std::bind(&FunctionsForAttributeUpdatedListener::fNotCalled, ptrMock, _1, _2)));
     server->addAttributeUpdatedListener(KEY,
-        [&called](const OIC::Service::RCSResourceAttributes::Value&,
-        const OIC::Service::RCSResourceAttributes::Value&)
-        {
-            called=100;
-        } );
+        (std::bind(&FunctionsForAttributeUpdatedListener::fCalled, ptrMock, _1, _2)));
 
-    handler(ResourceObjectHandlingRequestTest::createRequest(OC_REST_PUT, ocRep));
-
-    ASSERT_EQ(expected, called);
+    handler(createRequest(OC_REST_PUT, createOCRepresentation()));
 }
 
 TEST_F(AttributeUpdatedListenerTest, RemoveListenerReturnsTrueIfListenerIsNotAdded)
@@ -640,13 +637,29 @@ TEST_F(AttributeUpdatedListenerTest, RemoveListenerReturnsTrueIfListenerIsNotAdd
 
 TEST_F(AttributeUpdatedListenerTest, RemoveListenerReturnsTrueIfListenerIsAdded)
 {
+    FunctionsForAttributeUpdatedListener *ptrMock =
+        mocks.Mock<FunctionsForAttributeUpdatedListener>();
+
     server->addAttributeUpdatedListener(KEY,
-        [](const OIC::Service::RCSResourceAttributes::Value&,
-        const OIC::Service::RCSResourceAttributes::Value&)
-        {
-        } );
+        (std::bind(&FunctionsForAttributeUpdatedListener::fNotCalled, ptrMock, _1, _2)));
 
     ASSERT_TRUE(server->removeAttributeUpdatedListener(KEY));
 }
 
+TEST_F(AttributeUpdatedListenerTest, RemoveListenerNeverRunsRemovedFunc)
+{
+    FunctionsForAttributeUpdatedListener *ptrMock =
+        mocks.Mock<FunctionsForAttributeUpdatedListener>();
+
+    mocks.NeverCall(ptrMock, FunctionsForAttributeUpdatedListener::fNotCalled);
+
+    server->setAttribute(KEY, 0);
+    server->addAttributeUpdatedListener(KEY,
+        (std::bind(&FunctionsForAttributeUpdatedListener::fNotCalled, ptrMock, _1, _2)));
+    server->removeAttributeUpdatedListener(KEY);
+
+    handler(createRequest(OC_REST_PUT, createOCRepresentation()));
+}
+
+