[RE] Synced changes from master to 1.3-rel branch.
authorJay Sharma <jay.sharma@samsung.com>
Tue, 22 Aug 2017 06:22:37 +0000 (11:52 +0530)
committerUze Choi <uzchoi@samsung.com>
Wed, 23 Aug 2017 10:49:29 +0000 (10:49 +0000)
bug: https://jira.iotivity.org/browse/IOT-2267
Change-Id: If1fdd8d20e7a67b917e14bcf8090e8070eb2e5ed
Signed-off-by: Jay Sharma <jay.sharma@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/22017
Reviewed-by: Uze Choi <uzchoi@samsung.com>
Tested-by: jenkins-iotivity <jenkins@iotivity.org>
Reviewed-by: Phil Coval <philippe.coval@osg.samsung.com>
14 files changed:
service/resource-encapsulation/src/common/SConscript
service/resource-encapsulation/src/common/expiryTimer/unittests/ExpiryTimerTest.cpp
service/resource-encapsulation/src/common/primitiveResource/unittests/PresenceSubscriberTest.cpp
service/resource-encapsulation/src/common/primitiveResource/unittests/PrimitiveResourceTest.cpp
service/resource-encapsulation/src/common/primitiveResource/unittests/ResourceAttributesTest.cpp
service/resource-encapsulation/src/common/primitiveResource/unittests/SConscript
service/resource-encapsulation/src/common/utils/include/UnitTestHelperWithFakeOCPlatform.cpp [new file with mode: 0644]
service/resource-encapsulation/src/common/utils/include/UnitTestHelperWithFakeOCPlatform.h [new file with mode: 0644]
service/resource-encapsulation/src/serverBuilder/SConscript
service/resource-encapsulation/src/serverBuilder/unittests/RCSResourceObjectTest.cpp
service/resource-encapsulation/src/serverBuilder/unittests/RequestHandlerTest.cpp
service/resource-encapsulation/unittests/DiscoveryManagerTest.cpp
service/resource-encapsulation/unittests/ResourceClientTest.cpp
service/resource-encapsulation/unittests/SConscript

index 302ae07..2803a42 100644 (file)
@@ -100,4 +100,3 @@ else:
 ######################################################################
 if target_os in ['linux']:
     SConscript('primitiveResource/unittests/SConscript')
-
index 258a28a..e47aea9 100644 (file)
@@ -92,10 +92,10 @@ TEST_F(ExpiryTimerImplTest, CallbackBeInvokedWithinTolerance)
     ExpiryTimerImpl::getInstance()->post(10,
             std::bind(&FunctionObject::execute, functor, std::placeholders::_1));
 
-    Wait();
+    Wait(TOLERANCE_IN_MILLIS * 2);
 }
 
-TEST_F(ExpiryTimerImplTest, CallbackBeInvokedWithTimerId)
+TEST_F(ExpiryTimerImplTest, DISABLED_CallbackBeInvokedWithTimerId)
 {
     ExpiryTimerImpl::Id returnedId = 0;
     FunctionObject* functor = mocks.Mock< FunctionObject >();
@@ -160,7 +160,11 @@ TEST_F(ExpiryTimerImplTest, CancelReturnsFalseIfAlreadyExecuted)
 
 TEST_F(ExpiryTimerImplTest, CallbackBeInvokedWithinToleranceWithMultiplePost)
 {
+#ifdef HIPPOMOCKS_ISSUE
+    constexpr int NUM_OF_POST{ 10 };
+#else
     constexpr int NUM_OF_POST{ 10000 };
+#endif
     std::atomic_int called{ 0 };
 
     for (int i=0; i<NUM_OF_POST; ++i)
@@ -228,7 +232,7 @@ TEST_F(ExpiryTimerTest, CallbackBeInvokedWithinTolerance)
             }
     );
 
-    timer.post(10,
+    timer.post(20,
             std::bind(&FunctionObject::execute, functor, std::placeholders::_1));
 
     Wait();
@@ -251,7 +255,7 @@ TEST_F(ExpiryTimerTest, CallbackBeInvokedWithTimerId)
             }
     );
 
-    returnedId = timer.post(1, std::bind(&FunctionObject::execute, functor, std::placeholders::_1));
+    returnedId = timer.post(10, std::bind(&FunctionObject::execute, functor, std::placeholders::_1));
 
     Wait();
 }
@@ -295,8 +299,13 @@ TEST_F(ExpiryTimerTest, CancelReturnsFalseIfAlreadyExecuted)
 
 TEST_F(ExpiryTimerTest, NumOfPendingReturnsNumberOfNotExecuted)
 {
+#ifdef HIPPOMOCKS_ISSUE
+    constexpr size_t numOfFutureTask{ 10 };
+    constexpr size_t numOfShortDelayTask{ 10 };
+#else
     constexpr size_t numOfFutureTask{ 100 };
     constexpr size_t numOfShortDelayTask{ 100 };
+#endif
 
     for (size_t i=0; i<numOfFutureTask; ++i)
     {
@@ -321,7 +330,11 @@ TEST_F(ExpiryTimerTest, NumOfPendingReturnsNumberOfNotExecuted)
 
 TEST_F(ExpiryTimerTest, CancelAllCancelsAllTasks)
 {
+#ifdef HIPPOMOCKS_ISSUE
+    constexpr size_t numOfTask{ 10 };
+#else
     constexpr size_t numOfTask{ 100 };
+#endif
 
     for (size_t i=0; i<numOfTask; ++i)
     {
index 5be112a..897a487 100644 (file)
@@ -18,7 +18,7 @@
 //
 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
-#include <UnitTestHelper.h>
+#include "UnitTestHelperWithFakeOCPlatform.h"
 
 #include <PresenceSubscriber.h>
 #include <RCSException.h>
@@ -35,53 +35,55 @@ typedef OCStackResult (*SubscribePresence2)(OC::OCPlatform::OCPresenceHandle&,
 const std::string HOST{ "host" };
 const OCConnectivityType CONTYPE{ };
 
-
-class PresenceSubscriberNonMemberTest: public TestWithMock
+class PresenceSubscriberNonMemberTest: public TestWithExternMock
 {
 public:
     OCDoHandle handle;
 };
 
+MockRepository mocks;
+FakeOCPlatform * mockFakePlatform;
+
 TEST_F(PresenceSubscriberNonMemberTest, OCPlatformSubscribePresenceWillBeCalled)
 {
-    mocks.ExpectCallFuncOverload(
-            static_cast< SubscribePresence1 >(OC::OCPlatform::subscribePresence))
-                        .With(_, HOST,CONTYPE, _).Return(OC_STACK_OK);
+    mocks.ExpectCall(mockFakePlatform, FakeOCPlatform::subscribePresence)
+            .With(_, HOST,CONTYPE, _).Return(OC_STACK_OK);
 
     subscribePresence(handle, HOST, CONTYPE, SubscribeCallback());
 }
 
 TEST_F(PresenceSubscriberNonMemberTest, SubscribePresenceThrowsIfResultIsNotOK)
 {
-    mocks.ExpectCallFuncOverload(
-            static_cast< SubscribePresence1>(OC::OCPlatform::subscribePresence))
-                    .Return(OC_STACK_ERROR);
+    mocks.ExpectCall(mockFakePlatform, FakeOCPlatform::subscribePresence)
+            .Return(OC_STACK_ERROR);
 
     ASSERT_THROW(subscribePresence(handle, "", CONTYPE, SubscribeCallback()), RCSPlatformException);
 }
 
 TEST_F(PresenceSubscriberNonMemberTest, OCPlatformUnsubscribePresenceWillBeCalled)
 {
-    mocks.ExpectCallFuncOverload(OC::OCPlatform::unsubscribePresence).Return(OC_STACK_OK);
+    mocks.ExpectCall(mockFakePlatform, FakeOCPlatform::unsubscribePresence)
+            .Return(OC_STACK_OK);
 
     unsubscribePresence(handle);
 }
 
 TEST_F(PresenceSubscriberNonMemberTest, UnsubscribePresenceThrowIfResultIsNotOK)
 {
-    mocks.ExpectCallFuncOverload(OC::OCPlatform::unsubscribePresence).Return(OC_STACK_ERROR);
+    mocks.ExpectCall(mockFakePlatform, FakeOCPlatform::unsubscribePresence)
+            .Return(OC_STACK_ERROR);
 
     ASSERT_THROW(unsubscribePresence(handle), RCSPlatformException);
 }
 
-class PresenceSubscriberTest: public TestWithMock
+class PresenceSubscriberTest: public TestWithExternMock
 {
 protected:
     void SetUp()
     {
-        mocks.OnCallFuncOverload(
-                static_cast< SubscribePresence1 >(OC::OCPlatform::subscribePresence)).Do(
+        TestWithExternMock::SetUp();
 
+        mocks.OnCall(mockFakePlatform, FakeOCPlatform::subscribePresence).Do(
             [](OC::OCPlatform::OCPresenceHandle& handle, const std::string&,
                     OCConnectivityType, SubscribeCallback) -> OCStackResult
             {
@@ -90,7 +92,8 @@ protected:
             }
         );
 
-        mocks.OnCallFunc(OC::OCPlatform::unsubscribePresence).Return(OC_STACK_OK);
+        mocks.OnCall(mockFakePlatform, FakeOCPlatform::unsubscribePresence)
+                .Return(OC_STACK_OK);
     }
 
 };
@@ -103,9 +106,8 @@ TEST_F(PresenceSubscriberTest, IsNotSubscribingWhenCreatedWithDefaultConstructor
 
 TEST_F(PresenceSubscriberTest, ConstructorCallOCPlatformSubscribe)
 {
-    mocks.ExpectCallFuncOverload(
-            static_cast< SubscribePresence1 >(OC::OCPlatform::subscribePresence))
-                     .With(_, HOST, CONTYPE, _).Return(OC_STACK_OK);
+    mocks.ExpectCall(mockFakePlatform, FakeOCPlatform::subscribePresence)
+            .With(_, HOST, CONTYPE, _).Return(OC_STACK_OK);
 
     PresenceSubscriber subscriber{ HOST, CONTYPE, SubscribeCallback() };
 }
@@ -114,18 +116,16 @@ TEST_F(PresenceSubscriberTest, ConstructorWithResourceTypeCallOCPlatformSubscrib
 {
     const std::string resType { "resType" };
 
-    mocks.ExpectCallFuncOverload(
-            static_cast< SubscribePresence2 >(OC::OCPlatform::subscribePresence))
-                     .With(_, HOST, resType, CONTYPE, _).Return(OC_STACK_OK);
+    mocks.ExpectCall(mockFakePlatform, FakeOCPlatform::subscribePresence2)
+            .With(_, HOST, resType, CONTYPE, _).Return(OC_STACK_OK);
 
     PresenceSubscriber subscriber{ HOST, resType, CONTYPE, SubscribeCallback() };
 }
 
 TEST_F(PresenceSubscriberTest, ConstructorThrowsIfResultIsNotOK)
 {
-    mocks.ExpectCallFuncOverload(
-            static_cast< SubscribePresence1 >(OC::OCPlatform::subscribePresence))
-                    .Return(OC_STACK_ERROR);
+    mocks.ExpectCall(mockFakePlatform, FakeOCPlatform::subscribePresence)
+            .Return(OC_STACK_ERROR);
 
     ASSERT_THROW(PresenceSubscriber(HOST, CONTYPE, SubscribeCallback()), RCSPlatformException);
 }
index c51f2d2..5a69004 100644 (file)
@@ -206,8 +206,11 @@ TEST_F(DiscoverResourceTest, CallbackIsInvokedWhenResourceIsDiscovered)
 
     discoverResource("", "", OCConnectivityType{ }, discovered);
 }
-
+#ifdef HIPPOMOCKS_ISSUE
+TEST_F(DiscoverResourceTest, DISABLED_ThrowsdWhenOCPlatformFindResourceReturnsNotOK)
+#else
 TEST_F(DiscoverResourceTest, ThrowsdWhenOCPlatformFindResourceReturnsNotOK)
+#endif
 {
     mocks.ExpectCallFuncOverload(static_cast<FindResource>(OC::OCPlatform::findResource)).
             Return(OC_STACK_ERROR);
index 37f4bca..29a6c83 100644 (file)
@@ -384,6 +384,7 @@ TEST(ResourceAttributesConverterTest, ResourceAttributesCanBeConvertedIntoOCRepr
     {
         ASSERT_EQ(ocValue.bytes[i], rcsValue[i]);
     }
+    delete[] ocValue.bytes;
 }
 
 TEST(ResourceAttributesConverterTest, NestedOCRepresentationCanBeConvertedIntoResourceAttributes)
index 0db2d24..d8c483b 100644 (file)
@@ -47,6 +47,7 @@ rcs_common_test_env.PrependUnique(LIBS = [
 rcs_common_test_src = [
     rcs_common_test_env.Glob('*.cpp'),
     '../../expiryTimer/unittests/ExpiryTimerTest.cpp',
+    '../../utils/include/UnitTestHelperWithFakeOCPlatform.cpp'
 ]
 
 rcs_common_test = rcs_common_test_env.Program('rcs_common_test', rcs_common_test_src)
@@ -54,10 +55,9 @@ Alias("rcs_common_test", rcs_common_test)
 rcs_common_test_env.AppendTarget('rcs_common_test')
 
 if rcs_common_test_env.get('TEST') == '1':
-    rcs_common_test_env.AppendUnique(CPPDEFINES = ['HIPPOMOCKS_ISSUE'])
+    rcs_common_test_env.AppendUnique(CPPDEFINES=['HIPPOMOCKS_ISSUE'])
     from tools.scons.RunTest import run_test
     run_test(rcs_common_test_env,
-#            'service_resource-encapsulation_src_common_primitiveResource_unittests_rcs_common_test.memcheck',
-             '',  # TODO: fix leaks
+             'service_resource-encapsulation_src_common_primitiveResource_unittests_rcs_common_test.memcheck',
              'service/resource-encapsulation/src/common/primitiveResource/unittests/rcs_common_test',
              rcs_common_test)
diff --git a/service/resource-encapsulation/src/common/utils/include/UnitTestHelperWithFakeOCPlatform.cpp b/service/resource-encapsulation/src/common/utils/include/UnitTestHelperWithFakeOCPlatform.cpp
new file mode 100644 (file)
index 0000000..d3dd940
--- /dev/null
@@ -0,0 +1,95 @@
+//******************************************************************
+//
+// 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 "UnitTestHelperWithFakeOCPlatform.h"
+
+using namespace std;
+using namespace std::placeholders;
+
+extern FakeOCPlatform * mockFakePlatform;
+
+namespace OC
+{
+    namespace OCPlatform
+    {
+        OCStackResult subscribePresence(OCPresenceHandle& a, const std::string& b,
+                                        OCConnectivityType c, SubscribeCallback d)
+        {
+            return mockFakePlatform->subscribePresence(a, b, c, d);
+        }
+
+        OCStackResult subscribePresence(OCPresenceHandle& a, const std::string& b,
+                                        const std::string& c, OCConnectivityType d,
+                                        SubscribeCallback e)
+        {
+            return mockFakePlatform->subscribePresence2(a, b, c, d, e);
+        }
+
+        OCStackResult unsubscribePresence(OCPresenceHandle a)
+        {
+            return mockFakePlatform->unsubscribePresence(a);
+        }
+
+        OCStackResult findResource(const std::string& a, const std::string& b,
+                                   OCConnectivityType c, FindCallback d)
+        {
+            return mockFakePlatform->findResource(a, b, c, d);
+        }
+
+        OCStackResult registerResource(
+                OCResourceHandle &a, string &b, const string &c,
+                const string &d, EntityHandler e, uint8_t f)
+        {
+            return mockFakePlatform->registerResource(a, b, c, d, e, f);
+        }
+
+        OCStackResult unregisterResource(const OCResourceHandle &a)
+        {
+            return mockFakePlatform->unregisterResource(a);
+        }
+
+        OCStackResult bindResource(const OCResourceHandle a, const OCResourceHandle b)
+        {
+            return mockFakePlatform->bindResource(a, b);
+        }
+
+        OCStackResult sendResponse(const std::shared_ptr<OCResourceResponse> a)
+        {
+            return mockFakePlatform->sendResponse(a);
+        }
+
+        OCStackResult bindTypeToResource(const OCResourceHandle &a,
+                                const std::string &b)
+        {
+            return mockFakePlatform->bindTypeToResource(a, b);
+        }
+
+        OCStackResult bindInterfaceToResource(const OCResourceHandle &a,
+                                              const std::string &b)
+        {
+            return mockFakePlatform->bindInterfaceToResource(a, b);
+        }
+
+        OCStackResult notifyAllObservers(OCResourceHandle a)
+        {
+            return mockFakePlatform->notifyAllObservers(a);
+        }
+    }
+}
diff --git a/service/resource-encapsulation/src/common/utils/include/UnitTestHelperWithFakeOCPlatform.h b/service/resource-encapsulation/src/common/utils/include/UnitTestHelperWithFakeOCPlatform.h
new file mode 100644 (file)
index 0000000..6a2401c
--- /dev/null
@@ -0,0 +1,118 @@
+//******************************************************************
+//
+// 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.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+#ifndef COMMON_UTIL_UNITTESTHELPERWITHFAKEOCPLATFORM_H
+#define COMMON_UTIL_UNITTESTHELPERWITHFAKEOCPLATFORM_H
+
+#include <gtest/gtest.h>
+#include <HippoMocks/hippomocks.h>
+
+#include "octypes.h"
+#include "OCPlatform.h"
+
+using namespace std;
+using namespace std::placeholders;
+
+class FakeOCPlatform
+{
+public:
+    virtual OCStackResult subscribePresence(OC::OCPlatform::OCPresenceHandle& a,
+                                            const std::string& b, OCConnectivityType c,
+                                            OC::SubscribeCallback d);
+
+    virtual OCStackResult subscribePresence2(OC::OCPlatform::OCPresenceHandle& a,
+                                             const std::string& b, const std::string& c,
+                                             OCConnectivityType d, OC::SubscribeCallback e);
+
+    virtual OCStackResult unsubscribePresence(OC::OCPlatform::OCPresenceHandle a);
+
+    virtual OCStackResult findResource(const std::string& a, const std::string& b,
+                               OCConnectivityType c, OC::FindCallback d);
+    virtual OCStackResult registerResource(OCResourceHandle&, string&, const string&, const string&,
+               OC::EntityHandler, uint8_t ) = 0;
+    virtual OCStackResult unregisterResource(const OCResourceHandle &) = 0;
+
+    virtual OCStackResult sendResponse(const std::shared_ptr<OC::OCResourceResponse>) = 0;
+    virtual OCStackResult bindInterfaceToResource(
+            const OCResourceHandle &, const std::string &) = 0;
+    virtual OCStackResult bindTypeToResource(const OCResourceHandle &, const std::string &) = 0;
+    virtual OCStackResult bindResource(const OCResourceHandle, const OCResourceHandle) = 0;
+
+    virtual OCStackResult notifyAllObservers(OCResourceHandle) = 0;
+
+    virtual ~FakeOCPlatform() { }
+};
+
+extern MockRepository mocks;
+extern FakeOCPlatform * mockFakePlatform;
+
+namespace OC
+{
+    namespace OCPlatform
+    {
+        OCStackResult subscribePresence(OCPresenceHandle& a, const std::string& b,
+                                        OCConnectivityType c, SubscribeCallback d);
+
+        OCStackResult subscribePresence(OCPresenceHandle& a, const std::string& b,
+                                        const std::string& c, OCConnectivityType d,
+                                        SubscribeCallback e);
+
+        OCStackResult unsubscribePresence(OCPresenceHandle a);
+
+        OCStackResult findResource(const std::string& a, const std::string& b,
+                                   OCConnectivityType c, FindCallback d);
+
+        OCStackResult registerResource(
+                OCResourceHandle &a, string &b, const string &c,
+                const string &d, EntityHandler e, uint8_t f);
+
+        OCStackResult unregisterResource(const OCResourceHandle &a);
+
+        OCStackResult bindResource(const OCResourceHandle a, const OCResourceHandle b);
+
+        OCStackResult sendResponse(const std::shared_ptr<OCResourceResponse> a);
+
+        OCStackResult bindTypeToResource(const OCResourceHandle &a,
+                                const std::string &b);
+
+        OCStackResult bindInterfaceToResource(const OCResourceHandle &a,
+                                              const std::string &b);
+
+        OCStackResult notifyAllObservers(OCResourceHandle a);
+    }
+}
+
+class TestWithExternMock: public testing::Test
+{
+protected:
+    virtual ~TestWithExternMock() noexcept(noexcept(std::declval<Test>().~Test())) {}
+
+    virtual void SetUp()
+    {
+        mockFakePlatform = mocks.Mock< FakeOCPlatform >();
+    }
+
+    virtual void TearDown()
+    {
+        mocks.reset();
+    }
+};
+
+#endif // COMMON_UTIL_UNITTESTHELPERWITHFAKEOCPLATFORM_H
index cb73d1b..27be413 100644 (file)
@@ -99,6 +99,7 @@ else:
 ######################################################################
 if target_os in ['linux']:
     server_builder_test_env = server_builder_env.Clone()
+
     server_builder_test_env.AppendUnique(CPPPATH=[
         '#/extlibs/hippomocks/hippomocks',
         '../common/utils/include'
@@ -116,8 +117,12 @@ if target_os in ['linux']:
         'coap',
     ])
 
+    unitTestHelperWithFakeOCPlatform_obj = server_builder_test_env.SharedObject(
+        server_builder_test_env.Glob(
+            '../common/utils/include/UnitTestHelperWithFakeOCPlatform.cpp'))
     server_builder_test_src = [
         server_builder_test_env.Glob('unittests/*.cpp'),
+        unitTestHelperWithFakeOCPlatform_obj
     ]
 
     server_builder_test = server_builder_test_env.Program(
@@ -126,10 +131,11 @@ if target_os in ['linux']:
     server_builder_test_env.AppendTarget('rcs_server_test')
 
     if server_builder_test_env.get('TEST') == '1':
+        server_builder_test_env.AppendUnique(CPPDEFINES=['HIPPOMOCKS_ISSUE'])
+        # this import needs to remain here so it is protected by target check
         from tools.scons.RunTest import run_test
         run_test(
             server_builder_test_env,
-#           'service_resource-encapsulation_src_serverBuilder_rcs_server_test.memcheck',
-            '', # TODO: fix this test on linux and remove commented lines
+            'service_resource-encapsulation_src_serverBuilder_rcs_server_test.memcheck',
             'service/resource-encapsulation/src/serverBuilder/rcs_server_test',
             server_builder_test)
index 851c97c..5d85f84 100644 (file)
 //
 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
-#include "UnitTestHelper.h"
+#include "UnitTestHelperWithFakeOCPlatform.h"
 
 #include "RCSResourceObject.h"
 #include "RCSRequest.h"
 #include "RCSSeparateResponse.h"
 #include "InterfaceHandler.h"
 #include "ResourceAttributesConverter.h"
-
-#include "OCPlatform.h"
+#include "ocpayload.h"
 
 using namespace std;
 using namespace std::placeholders;
 
-using namespace OIC::Service;
 using namespace OC;
+using namespace OIC::Service;
 
 typedef OCStackResult (*registerResource)(OCResourceHandle&, string&, const string&, const string&,
                            EntityHandler, uint8_t );
@@ -45,26 +44,42 @@ constexpr char KEY[]{ "key" };
 constexpr char CUSTOM_INTERFACE[]{ "oic.if.custom" };
 constexpr int VALUE{ 100 };
 
+MockRepository mocks;
+FakeOCPlatform * mockFakePlatform;
+
 TEST(ResourceObjectBuilderCreateTest, ThrowIfUriIsInvalid)
 {
+    mockFakePlatform =
+            mocks.Mock< FakeOCPlatform >();
+
+    mocks.OnCall(
+            mockFakePlatform, FakeOCPlatform::registerResource)
+                    .Return(OC_STACK_ERROR);
     ASSERT_THROW(RCSResourceObject::Builder("", "", "").build(), RCSPlatformException);
+
+    mocks.reset();
 }
 
-class ResourceObjectBuilderTest: public TestWithMock
+class ResourceObjectBuilderTest: public TestWithExternMock
 {
 protected:
+    virtual ~ResourceObjectBuilderTest()
+    noexcept(noexcept(std::declval<Test>().~Test())) {}
+
     void SetUp()
     {
-        TestWithMock::SetUp();
+        TestWithExternMock::SetUp();
 
-        mocks.OnCallFuncOverload(static_cast< registerResource >(OCPlatform::registerResource))
-                .Return(OC_STACK_OK);
+        mocks.OnCall(
+                mockFakePlatform, FakeOCPlatform::registerResource)
+                        .Return(OC_STACK_OK);
     }
 };
 
 TEST_F(ResourceObjectBuilderTest, RegisterResourceWhenCallCreate)
 {
-    mocks.ExpectCallFuncOverload(static_cast< registerResource >(OCPlatform::registerResource))
+    mocks.ExpectCall(
+            mockFakePlatform, FakeOCPlatform::registerResource)
             .Return(OC_STACK_OK);
 
     RCSResourceObject::Builder(RESOURCE_URI, RESOURCE_TYPE, "").build();
@@ -94,7 +109,8 @@ TEST_F(ResourceObjectBuilderTest, ResourceServerHasAttrsSetByBuilder)
 TEST_F(ResourceObjectBuilderTest, TypesAddedInBuilderWillBeBound)
 {
     int count = 0;
-    mocks.OnCallFunc(OCPlatform::bindTypeToResource).Do(
+    mocks.OnCall(
+            mockFakePlatform, FakeOCPlatform::bindTypeToResource).Do(
             [&count](const OCResourceHandle&, const std::string&)
             {
                 ++count;
@@ -111,7 +127,8 @@ TEST_F(ResourceObjectBuilderTest, TypesAddedInBuilderWillBeBound)
 TEST_F(ResourceObjectBuilderTest, InterfaceAddedInBuilderWillBeBound)
 {
     int count = 0;
-    mocks.OnCallFunc(OCPlatform::bindInterfaceToResource).Do(
+    mocks.OnCall(
+            mockFakePlatform, FakeOCPlatform::bindInterfaceToResource).Do(
             [&count](const OCResourceHandle&, const std::string&)
             {
                 ++count;
@@ -125,15 +142,18 @@ TEST_F(ResourceObjectBuilderTest, InterfaceAddedInBuilderWillBeBound)
     EXPECT_EQ(2, count);
 }
 
-class ResourceObjectTest: public TestWithMock
+class ResourceObjectTest: public TestWithExternMock
 {
 public:
     RCSResourceObject::Ptr server;
 
 protected:
+    virtual ~ResourceObjectTest()
+    noexcept(noexcept(std::declval<Test>().~Test())) {}
+
     void SetUp()
     {
-        TestWithMock::SetUp();
+        TestWithExternMock::SetUp();
 
         initMocks();
 
@@ -144,10 +164,13 @@ protected:
 
     virtual void initMocks()
     {
-        mocks.OnCallFuncOverload(static_cast< registerResource >(OCPlatform::registerResource)).
-                Return(OC_STACK_OK);
+        mocks.OnCall(
+                mockFakePlatform, FakeOCPlatform::registerResource)
+                        .Return(OC_STACK_OK);
 
-        mocks.OnCallFunc(OCPlatform::unregisterResource).Return(OC_STACK_OK);
+        mocks.OnCall(
+                mockFakePlatform, FakeOCPlatform::unregisterResource)
+                        .Return(OC_STACK_OK);
     }
 
     virtual void initResourceObject() {
@@ -212,10 +235,11 @@ TEST_F(ResourceObjectTest, ThrowIfResourceToBindIsInvalid)
 
 TEST_F(ResourceObjectTest, ThrowIfBindResourceFailed)
 {
-    mocks.OnCallFunc(OCBindResource).Return(OC_STACK_ERROR);
+    mocks.OnCall(mockFakePlatform, FakeOCPlatform::bindResource)
+            .Return(OC_STACK_ERROR);
 
-    ASSERT_THROW(server->bindResource(
-            RCSResourceObject::Builder("a/temp", RESOURCE_TYPE, "").build()), RCSPlatformException);
+    ASSERT_THROW(server->bindResource(RCSResourceObject::Builder(
+            "a/temp", RESOURCE_TYPE, "").build()), RCSPlatformException);
 }
 
 TEST_F(ResourceObjectTest, ThrowIfResourceToUnbindIsInvalid)
@@ -225,7 +249,9 @@ TEST_F(ResourceObjectTest, ThrowIfResourceToUnbindIsInvalid)
 
 TEST_F(ResourceObjectTest, BoundResourceCanBeRetrieved)
 {
-    mocks.OnCallFunc(OCBindResource).Return(OC_STACK_OK);
+//    OCBindResource();
+    mocks.OnCall(mockFakePlatform, FakeOCPlatform::bindResource)
+            .Return(OC_STACK_OK);
 
     auto boundResource = RCSResourceObject::Builder("a/temp", RESOURCE_TYPE, "").build();
     server->bindResource(boundResource);
@@ -239,8 +265,17 @@ class AutoNotifyTest: public ResourceObjectTest
 protected:
     void initMocks()
     {
-        mocks.OnCallFuncOverload(static_cast< NotifyAllObservers >(
-                OCPlatform::notifyAllObservers)).Return(OC_STACK_OK);
+        mocks.OnCall(
+                        mockFakePlatform, FakeOCPlatform::registerResource)
+                                .Return(OC_STACK_OK);
+
+        mocks.OnCall(
+                mockFakePlatform, FakeOCPlatform::unregisterResource)
+                        .Return(OC_STACK_OK);
+
+        mocks.OnCall(
+                mockFakePlatform, FakeOCPlatform::notifyAllObservers)
+                        .Return(OC_STACK_OK);
     }
 
     virtual void initResourceObject() {
@@ -265,8 +300,8 @@ TEST_F(AutoNotifyTest, WithUpdatedPolicy_NeverBeNotifiedIfAttributeIsNotChanged)
     server->setAutoNotifyPolicy(RCSResourceObject::AutoNotifyPolicy::UPDATED);
     server->setAttribute(KEY, VALUE);
 
-    mocks.NeverCallFuncOverload(static_cast< NotifyAllObservers >(
-            OC::OCPlatform::notifyAllObservers));
+    mocks.NeverCall(
+            mockFakePlatform, FakeOCPlatform::notifyAllObservers);
 
     server->setAttribute(KEY, VALUE);
 }
@@ -276,8 +311,9 @@ TEST_F(AutoNotifyTest, WithUpdatedPolicy_WillBeNotifiedIfAttributeIsChanged)
     server->setAutoNotifyPolicy(RCSResourceObject::AutoNotifyPolicy::UPDATED);
     server->setAttribute(KEY, VALUE);
 
-    mocks.ExpectCallFuncOverload(static_cast< NotifyAllObservers >(
-            OC::OCPlatform::notifyAllObservers)).Return(OC_STACK_OK);
+    mocks.ExpectCall(
+            mockFakePlatform, FakeOCPlatform::notifyAllObservers)
+                    .Return(OC_STACK_OK);
 
     server->setAttribute(KEY, VALUE + 1);
 }
@@ -287,8 +323,9 @@ TEST_F(AutoNotifyTest, WithUpdatedPolicy_WillBeNotifiedIfValueIsAdded)
     constexpr char newKey[]{ "newKey" };
     server->setAutoNotifyPolicy(RCSResourceObject::AutoNotifyPolicy::UPDATED);
 
-    mocks.ExpectCallFuncOverload(static_cast< NotifyAllObservers >(
-            OC::OCPlatform::notifyAllObservers)).Return(OC_STACK_OK);
+    mocks.ExpectCall(
+            mockFakePlatform, FakeOCPlatform::notifyAllObservers)
+                    .Return(OC_STACK_OK);
 
     server->setAttribute(newKey, VALUE);
 }
@@ -297,8 +334,8 @@ TEST_F(AutoNotifyTest, WithNeverPolicy_NeverBeNotifiedEvenIfAttributeIsChanged)
 {
     server->setAutoNotifyPolicy(RCSResourceObject::AutoNotifyPolicy::NEVER);
 
-    mocks.NeverCallFuncOverload(static_cast< NotifyAllObservers >(
-            OC::OCPlatform::notifyAllObservers));
+    mocks.NeverCall(
+            mockFakePlatform, FakeOCPlatform::notifyAllObservers);
 
     RCSResourceObject::LockGuard lock{ server };
     server->setAttribute(KEY, VALUE);
@@ -309,8 +346,9 @@ TEST_F(AutoNotifyTest, WithUpdatePolicy_WillBeNotifiedIfAttributeIsDeleted)
     server->setAutoNotifyPolicy(RCSResourceObject::AutoNotifyPolicy::UPDATED);
     server->setAttribute(KEY, VALUE);
 
-    mocks.ExpectCallFuncOverload(static_cast< NotifyAllObservers >(
-            OC::OCPlatform::notifyAllObservers)).Return(OC_STACK_OK);
+    mocks.ExpectCall(
+            mockFakePlatform, FakeOCPlatform::notifyAllObservers)
+                    .Return(OC_STACK_OK);
 
     server->removeAttribute(KEY);
 }
@@ -323,8 +361,9 @@ TEST_F(AutoNotifyWithGuardTest, GuardFollowsServerPolicyByDefault)
 {
     server->setAutoNotifyPolicy(RCSResourceObject::AutoNotifyPolicy::UPDATED);
 
-    mocks.ExpectCallFuncOverload(static_cast< NotifyAllObservers >(
-            OC::OCPlatform::notifyAllObservers)).Return(OC_STACK_OK);
+    mocks.ExpectCall(
+            mockFakePlatform, FakeOCPlatform::notifyAllObservers)
+                    .Return(OC_STACK_OK);
 
     RCSResourceObject::LockGuard guard{ server };
     server->setAttribute(KEY, VALUE);
@@ -334,8 +373,8 @@ TEST_F(AutoNotifyWithGuardTest, GuardCanOverridePolicy)
 {
     server->setAutoNotifyPolicy(RCSResourceObject::AutoNotifyPolicy::ALWAYS);
 
-    mocks.NeverCallFuncOverload(static_cast< NotifyAllObservers >(
-            OC::OCPlatform::notifyAllObservers));
+    mocks.NeverCall(
+            mockFakePlatform, FakeOCPlatform::notifyAllObservers);
 
     RCSResourceObject::LockGuard guard{ server, RCSResourceObject::AutoNotifyPolicy::NEVER };
     server->getAttributes()[KEY] = VALUE;
@@ -345,16 +384,18 @@ TEST_F(AutoNotifyWithGuardTest, GuardInvokesNotifyWhenDestroyed)
 {
     server->setAutoNotifyPolicy(RCSResourceObject::AutoNotifyPolicy::NEVER);
 
-    mocks.ExpectCallFuncOverload(static_cast< NotifyAllObservers >(
-            OC::OCPlatform::notifyAllObservers)).Return(OC_STACK_OK);
+    mocks.ExpectCall(
+            mockFakePlatform, FakeOCPlatform::notifyAllObservers)
+                    .Return(OC_STACK_OK);
 
     {
         RCSResourceObject::LockGuard guard{ server, RCSResourceObject::AutoNotifyPolicy::ALWAYS };
         server->setAttribute(KEY, VALUE);
     }
 
-    mocks.NeverCallFuncOverload(static_cast< NotifyAllObservers >(
-               OC::OCPlatform::notifyAllObservers)).Return(OC_STACK_OK);
+    mocks.NeverCall(
+            mockFakePlatform, FakeOCPlatform::notifyAllObservers)
+                    .Return(OC_STACK_OK);
 
     server->setAttribute(KEY, VALUE);
 }
@@ -374,6 +415,7 @@ public:
         auto request = make_shared<OCResourceRequest>();
 
         OCEntityHandlerRequest ocEntityHandlerRequest;
+
         memset(&ocEntityHandlerRequest, 0, sizeof(OCEntityHandlerRequest));
         OC::MessageContainer mc;
 
@@ -383,15 +425,22 @@ public:
         ocEntityHandlerRequest.resource = fakeResourceHandle;
         ocEntityHandlerRequest.method = method;
         ocEntityHandlerRequest.payload = reinterpret_cast<OCPayload*>(mc.getPayload());
+        ocEntityHandlerRequest.query = NULL;
 
         if(!interface.empty())
         {
             const string query = string("if=" + interface);
-            ocEntityHandlerRequest.query = strdup(query.c_str());
+            auto cQuery = new char [query.size()+1];
+            std::strcpy(cQuery, query.c_str());
+            ocEntityHandlerRequest.query = const_cast<char *> (cQuery);
         }
 
         formResourceRequest(OC_REQUEST_FLAG, &ocEntityHandlerRequest, request);
 
+        OCRepPayloadDestroy((OCRepPayload *)ocEntityHandlerRequest.payload);
+
+        delete[] ocEntityHandlerRequest.query;
+
         return request;
     }
 
@@ -405,11 +454,13 @@ protected:
 
     void initMocks()
     {
-        mocks.OnCallFuncOverload(
-            static_cast<registerResource>(OCPlatform::registerResource)).Do(
+        mocks.OnCall(
+                mockFakePlatform, FakeOCPlatform::registerResource).Do(
                     bind(&ResourceObjectHandlingRequestTest::registerResourceFake,
                             this, _1, _2, _3, _4, _5, _6));
-        mocks.OnCallFunc(OCPlatform::unregisterResource).Return(OC_STACK_OK);
+        mocks.OnCall(
+                mockFakePlatform, FakeOCPlatform::unregisterResource)
+                        .Return(OC_STACK_OK);
     }
 };
 
@@ -420,21 +471,23 @@ const OCResourceHandle  ResourceObjectHandlingRequestTest::fakeResourceHandle =
 
 TEST_F(ResourceObjectHandlingRequestTest, CallSendResponseWhenReceiveRequest)
 {
-    mocks.ExpectCallFunc(OCPlatform::sendResponse).Return(OC_STACK_OK);
+    mocks.ExpectCall(
+            mockFakePlatform, FakeOCPlatform::sendResponse).Return(OC_STACK_OK);
 
     ASSERT_EQ(OC_EH_OK, handler(createRequest()));
 }
 
 TEST_F(ResourceObjectHandlingRequestTest, ReturnErrorCodeWhenSendResponseFailed)
 {
-    mocks.ExpectCallFunc(OCPlatform::sendResponse).Return(OC_STACK_ERROR);
+    mocks.ExpectCall(
+            mockFakePlatform, FakeOCPlatform::sendResponse).Return(OC_STACK_ERROR);
 
     ASSERT_EQ(OC_EH_ERROR, handler(createRequest()));
 }
 
 TEST_F(ResourceObjectHandlingRequestTest, SendResponseWithSameHandlesPassedByRequest)
 {
-    mocks.ExpectCallFunc(OCPlatform::sendResponse).Match(
+    mocks.ExpectCall(mockFakePlatform, FakeOCPlatform::sendResponse).Match(
             [](const shared_ptr<OCResourceResponse> response)
             {
                 return response->getRequestHandle() == fakeRequestHandle &&
@@ -469,7 +522,7 @@ TEST_F(ResourceObjectHandlingRequestTest, SetMethodOfSeparateResponseInvokesSend
     );
     handler(createRequest(OC_REST_GET));
 
-    mocks.ExpectCallFunc(OCPlatform::sendResponse).Return(OC_STACK_OK);
+    mocks.ExpectCall(mockFakePlatform, FakeOCPlatform::sendResponse).Return(OC_STACK_OK);
 
     RCSSeparateResponse(aRequest).set();
 }
@@ -540,7 +593,7 @@ protected:
 
     void SetUp()
     {
-        TestWithMock::SetUp();
+        TestWithExternMock::SetUp();
 
         initMocks();
     }
@@ -549,7 +602,9 @@ protected:
     {
         ResourceObjectHandlingRequestTest::initMocks();
 
-        mocks.OnCallFunc(OCPlatform::bindInterfaceToResource).Return(OC_STACK_OK);
+        mocks.OnCall(
+                mockFakePlatform, FakeOCPlatform::bindInterfaceToResource)
+                        .Return(OC_STACK_OK);
     }
 };
 
@@ -559,7 +614,8 @@ TEST_F(ResourceObjectInterfaceHandlerTest, GetResponseForBaselineContainsAllProp
 
     OCRepresentation ocRep;
 
-    mocks.ExpectCallFunc(OCPlatform::sendResponse).Match(
+    mocks.ExpectCall(
+            mockFakePlatform, FakeOCPlatform::sendResponse).Match(
             [=](const shared_ptr<OCResourceResponse> response)
             {
                 RCSResourceObject::LockGuard guard{ server };
@@ -567,7 +623,6 @@ TEST_F(ResourceObjectInterfaceHandlerTest, GetResponseForBaselineContainsAllProp
                 return checkResponse(response->getResourceRepresentation(),
                         server->getAttributes(), server->getInterfaces(), server->getTypes(),
                         server->getUri());
-
             }
     ).Return(OC_STACK_OK);
 
@@ -581,7 +636,8 @@ TEST_F(ResourceObjectInterfaceHandlerTest, SetResponseForActuatorContainsOnlyReq
     OCRepresentation ocRep;
     ocRep[KEY] = VALUE;
 
-    mocks.ExpectCallFunc(OCPlatform::sendResponse).Match(
+    mocks.ExpectCall(
+            mockFakePlatform, FakeOCPlatform::sendResponse).Match(
             [&ocRep](const shared_ptr<OCResourceResponse> response)
             {
                 return checkResponse(response->getResourceRepresentation(),
@@ -599,7 +655,8 @@ TEST_F(ResourceObjectInterfaceHandlerTest, SetResponseForBaselineContainsApplied
     OCRepresentation ocRep;
     ocRep["NEWKEY"] = std::string("NEWVALUE");
 
-    mocks.ExpectCallFunc(OCPlatform::sendResponse).Match(
+    mocks.ExpectCall(
+            mockFakePlatform, FakeOCPlatform::sendResponse).Match(
             [=](const shared_ptr<OCResourceResponse> response)
             {
                 RCSResourceObject::LockGuard guard{ server };
@@ -621,7 +678,8 @@ TEST_F(ResourceObjectInterfaceHandlerTest, GetResponseForCustomEqualsResponseFor
     OCRepresentation repArray[2];
     int cnt = 0;
 
-    mocks.OnCallFunc(OCPlatform::sendResponse).Do(
+    mocks.OnCall(
+            mockFakePlatform, FakeOCPlatform::sendResponse).Do(
             [&repArray, &cnt](const shared_ptr<OCResourceResponse> response)
             {
                 repArray[cnt++] = response->getResourceRepresentation();
@@ -660,8 +718,6 @@ TEST_F(ResourceObjectInterfaceHandlerTest, SettingDefaultInterfaceEqualsGetDefau
     EXPECT_EQ(BASELINE_INTERFACE, server->getDefaultInterface());
 }
 
-
-
 class SetRequestHandlerPolicyTest: public ResourceObjectHandlingRequestTest
 {
 public:
@@ -678,7 +734,9 @@ public:
     void initMocks()
     {
         ResourceObjectHandlingRequestTest::initMocks();
-        mocks.OnCallFunc(OCPlatform::sendResponse).Return(OC_STACK_OK);
+        mocks.OnCall(
+                mockFakePlatform, FakeOCPlatform::sendResponse)
+                        .Return(OC_STACK_OK);
     }
 };
 
@@ -716,8 +774,6 @@ TEST_F(SetRequestHandlerPolicyTest, WithAcceptancePolicy_AcceptedEvenIfKeyIsNew)
     ASSERT_TRUE(server->getAttributes().contains(KEY));
 }
 
-
-
 class ResourceObjectSynchronizationTest: public ResourceObjectHandlingRequestTest
 {
 public:
@@ -752,14 +808,24 @@ TEST_F(ResourceObjectSynchronizationTest, MultipleAccessToServerResource)
 
     server->setAttribute(KEY, 0);
 
-    for (int i = 20; i >= 0; --i) {
+    for (int i = 20; i >= 0; --i)
+    {
+#ifdef HIPPOMOCKS_ISSUE
+        int count = 50 + i * 1;
+#else
         int count = 5000 + i * 100;
+#endif
         threads.push_back(thread { withLock, server, count });
         expected += count;
     }
 
-    for (int i = 20; i >= 0; --i) {
+    for (int i = 20; i >= 0; --i)
+    {
+#ifdef HIPPOMOCKS_ISSUE
+        int count = 50 + i * 1;
+#else
         int count = 5000 + i * 100;
+#endif
         threads.push_back(thread { withSetter, server, count });
         expected +=count;
     }
@@ -777,18 +843,29 @@ TEST_F(ResourceObjectSynchronizationTest, MultipleAccessToServerResourceWithRequ
     int expected { 0 };
     vector<thread> threads;
 
-    mocks.OnCallFunc(OCPlatform::sendResponse).Return(OC_STACK_OK);
+    mocks.OnCall(mockFakePlatform, FakeOCPlatform::sendResponse)
+            .Return(OC_STACK_OK);
 
     server->setAttribute(KEY, 0);
 
-    for (int i = 20; i >= 0; --i) {
+    for (int i = 20; i >= 0; --i)
+    {
+#ifdef HIPPOMOCKS_ISSUE
+        int count = 50 + i * 1;
+#else
         int count = 5000 + i * 100;
+#endif
         threads.push_back(thread{ withLock, server, count });
         expected += count;
     }
 
-    for (int i = 20; i >= 0; --i) {
+    for (int i = 20; i >= 0; --i)
+    {
+#ifdef HIPPOMOCKS_ISSUE
+        int count = 50 + i * 1;
+#else
         int count = 5000 + i * 100;
+#endif
         threads.push_back(thread{ withSetter, server, count });
         expected +=count;
     }
@@ -796,7 +873,11 @@ TEST_F(ResourceObjectSynchronizationTest, MultipleAccessToServerResourceWithRequ
     threads.push_back(thread{
         [this]()
         {
+#ifdef HIPPOMOCKS_ISSUE
+            for (int i=0; i<100; ++i)
+#else
             for (int i=0; i<10000; ++i)
+#endif
             {
                 if (i % 5 == 0) handler(createRequest(OC_REST_OBSERVE));
                 handler(createRequest((i & 1) ? OC_REST_GET : OC_REST_POST));
@@ -812,8 +893,6 @@ TEST_F(ResourceObjectSynchronizationTest, MultipleAccessToServerResourceWithRequ
     ASSERT_EQ(expected, server->getAttribute<int>(KEY));
 }
 
-
-
 class AttributeUpdatedListenerTest: public ResourceObjectHandlingRequestTest
 {
 public:
@@ -831,7 +910,9 @@ protected:
     void SetUp()
     {
         ResourceObjectHandlingRequestTest::SetUp();
-        mocks.OnCallFunc(OCPlatform::sendResponse).Return(OC_STACK_OK);
+        mocks.OnCall(
+                mockFakePlatform, FakeOCPlatform::sendResponse)
+                        .Return(OC_STACK_OK);
 
         server->setAttribute(KEY, 0);
     }
@@ -846,7 +927,6 @@ public:
     virtual ~AttributeUpdatedListener() {}
 };
 
-
 TEST_F(AttributeUpdatedListenerTest, RemoveListenerReturnsFalseIfListenerIsNotAdded)
 {
     ASSERT_FALSE(server->removeAttributeUpdatedListener(KEY));
index bc3b53e..77738a5 100644 (file)
@@ -18,7 +18,7 @@
 //
 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
-#include "UnitTestHelper.h"
+#include "UnitTestHelperWithFakeOCPlatform.h"
 
 #include "RequestHandler.h"
 #include "RCSResourceObject.h"
@@ -59,8 +59,7 @@ TEST(RequestHandlerTest, CustomRepresentationContainsSameAttributesPassedToConst
     ASSERT_EQ(attrs, converted);
 }
 
-
-class SetRequestHandlerAcceptanceTest: public TestWithMock
+class SetRequestHandlerAcceptanceTest: public TestWithExternMock
 {
 public:
     RCSResourceObject::Ptr server;
@@ -72,12 +71,15 @@ public:
 protected:
     void SetUp()
     {
-        TestWithMock::SetUp();
+        TestWithExternMock::SetUp();
 
-        mocks.OnCallFuncOverload(static_cast<RegisterResource>(OC::OCPlatform::registerResource))
-                .Return(OC_STACK_OK);
+        mocks.OnCall(
+                mockFakePlatform, FakeOCPlatform::registerResource)
+                        .Return(OC_STACK_OK);
 
-        mocks.OnCallFunc(OC::OCPlatform::unregisterResource).Return(OC_STACK_OK);
+        mocks.OnCall(
+                mockFakePlatform, FakeOCPlatform::unregisterResource)
+                        .Return(OC_STACK_OK);
 
         server = RCSResourceObject::Builder("a/test", "resourcetype", "").build();
 
index e158db2..6675c5f 100644 (file)
@@ -18,7 +18,8 @@
 //
 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
-#include "UnitTestHelper.h"
+#include <gtest/gtest.h>
+#include <HippoMocks/hippomocks.h>
 
 #include "RCSRemoteResourceObject.h"
 #include "RCSDiscoveryManager.h"
@@ -116,6 +117,7 @@ TEST(DiscoveryManagerTest, ThrowIfDiscoverWithMultipleTypesThatContainEmptyStrin
 
 TEST(DiscoveryManagerTest, DiscoverInvokesFindResource)
 {
+#ifndef HIPPOMOCKS_ISSUE
     MockRepository mocks;
     mocks.ExpectCallFuncOverload(static_cast<OCFindResource>(findResource)).Match(
         [](const std::string& host, const std::string& resourceURI, OCConnectivityType, FindCallback)
@@ -124,12 +126,15 @@ TEST(DiscoveryManagerTest, DiscoverInvokesFindResource)
                     (std::string(OC_RSRVD_WELL_KNOWN_URI) + "?rt=" + RESOURCE_TYPE);
         }
     ).Return(OC_STACK_OK);
-
+#endif
     ScopedTask task {RCSDiscoveryManager::getInstance()->discoverResourceByType(
             RCSAddress::multicast(), RESOURCE_URI, RESOURCE_TYPE, onResourceDiscovered)};
 }
-
+#ifdef HIPPOMOCKS_ISSUE
+TEST(DiscoveryManagerTest, DISABLED_DiscoverWithMultipleTypesInvokesFindResourceMultipleTimes)
+#else
 TEST(DiscoveryManagerTest, DiscoverWithMultipleTypesInvokesFindResourceMultipleTimes)
+#endif
 {
     MockRepository mocks;
     const std::vector< std::string > resourceTypes{ RESOURCE_TYPE, SECOND_RESOURCETYPE };
@@ -161,8 +166,11 @@ TEST(DiscoveryManagerTest, TaskCanBeCanceled)
     ASSERT_FALSE(aTask->isCanceled());
     ASSERT_TRUE(aTaskToBeCanceled->isCanceled());
 }
-
+#ifdef HIPPOMOCKS_ISSUE
+TEST(DiscoveryManagerTest, DISABLED_CallbackWouldNotBeCalledForSameRemoteResource)
+#else
 TEST(DiscoveryManagerTest, CallbackWouldNotBeCalledForSameRemoteResource)
+#endif
 {
     FindCallback callback;
 
index 78e443d..a025407 100644 (file)
@@ -27,6 +27,7 @@
 #include "RCSResourceObject.h"
 #include "RCSAddress.h"
 #include "RCSRequest.h"
+#include "cainterface.h"
 
 #include <condition_variable>
 #include <mutex>
@@ -90,7 +91,8 @@ protected:
 private:
     void CreateResource()
     {
-        server = RCSResourceObject::Builder(RESOURCEURI, RESOURCETYPE, RESOURCEINTERFACE).build();
+        server = RCSResourceObject::Builder(RESOURCEURI, RESOURCETYPE, RESOURCEINTERFACE)
+        .setDefaultInterface(RESOURCEINTERFACE).build();
         server->setAttribute(ATTR_KEY, ATTR_VALUE);
     }
 
@@ -121,7 +123,6 @@ private:
 
         Proceed();
     }
-
 private:
     std::condition_variable cond;
     std::mutex mutex;
@@ -132,17 +133,28 @@ TEST_F(RemoteResourceObjectTest, GetRemoteAttributesDoesNotAllowEmptyFunction)
     ASSERT_THROW(object->getRemoteAttributes({ }), RCSInvalidParameterException);
 }
 
+class TestRemoteAttributesCallback
+{
+public:
+    virtual void getRemoteAttributesCallback(const RCSResourceAttributes&, int) = 0;
+    virtual void setRemoteAttributesCallback(const RCSResourceAttributes&, int) = 0;
+    virtual ~TestRemoteAttributesCallback() { }
+};
+
 TEST_F(RemoteResourceObjectTest, GetRemoteAttributesGetsAttributesOfServer)
 {
-    mocks.ExpectCallFunc(getRemoteAttributesCallback).Match(
-            [this](const RCSResourceAttributes& attrs, int)
+    auto mockCallback = mocks.Mock< TestRemoteAttributesCallback >();
+    mocks.ExpectCall(mockCallback, TestRemoteAttributesCallback::getRemoteAttributesCallback).
+            Match([this](const RCSResourceAttributes& attrs, int)
             {
                 RCSResourceObject::LockGuard lock{ server };
                 return attrs == server->getAttributes();
             }
     ).Do([this](const RCSResourceAttributes&, int){ Proceed(); });
 
-    object->getRemoteAttributes(getRemoteAttributesCallback);
+    object->getRemoteAttributes(std::bind(
+            &TestRemoteAttributesCallback::getRemoteAttributesCallback, mockCallback,
+            std::placeholders::_1, std::placeholders::_2));
 
     Wait();
 }
@@ -158,10 +170,13 @@ TEST_F(RemoteResourceObjectTest, SetRemoteAttributesSetsAttributesOfServer)
     RCSResourceAttributes newAttrs;
     newAttrs[ATTR_KEY] = newValue;
 
-    mocks.ExpectCallFunc(setRemoteAttributesCallback).
+    auto mockCallback = mocks.Mock< TestRemoteAttributesCallback >();
+    mocks.ExpectCall(mockCallback, TestRemoteAttributesCallback::setRemoteAttributesCallback).
             Do([this](const RCSResourceAttributes&, int){ Proceed(); });
 
-    object->setRemoteAttributes(newAttrs, setRemoteAttributesCallback);
+    object->setRemoteAttributes(newAttrs, std::bind(
+            &TestRemoteAttributesCallback::setRemoteAttributesCallback, mockCallback,
+            std::placeholders::_1, std::placeholders::_2));
     Wait();
 
     ASSERT_EQ(newValue, server->getAttributeValue(ATTR_KEY).get<int>());
@@ -176,6 +191,13 @@ TEST_F(RemoteResourceObjectTest, SetRemoteRepresentationDoesNotAllowEmptyFunctio
 
 TEST_F(RemoteResourceObjectTest, SetRemoteRepresentationSetsRepresentationOfServer)
 {
+    class TestSetRemoteRepresentationCallback
+    {
+    public:
+        virtual void
+        setRemoteRepresentationCallback(const HeaderOpts&, const RCSRepresentation&, int) = 0;
+        virtual ~TestSetRemoteRepresentationCallback() { }
+    };
     RCSRepresentation rcsRep;
     RCSQueryParams queryParams;
     constexpr int newValue = ATTR_VALUE + 1;
@@ -184,10 +206,14 @@ TEST_F(RemoteResourceObjectTest, SetRemoteRepresentationSetsRepresentationOfServ
 
     rcsRep.setAttributes(newAttrs);
 
-    mocks.ExpectCallFunc(setRemoteRepresentationCallback).
+    auto mockCallback = mocks.Mock< TestSetRemoteRepresentationCallback >();
+    mocks.ExpectCall(mockCallback,
+                     TestSetRemoteRepresentationCallback::setRemoteRepresentationCallback).
             Do([this](const HeaderOpts&, const RCSRepresentation&, int){ Proceed(); });
 
-    object->set(queryParams, rcsRep, setRemoteRepresentationCallback);
+    object->set(queryParams, rcsRep, std::bind(
+            &TestSetRemoteRepresentationCallback::setRemoteRepresentationCallback,
+            mockCallback, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
     Wait();
 
     ASSERT_EQ(newValue, server->getAttributeValue(ATTR_KEY).get<int>());
@@ -205,10 +231,6 @@ TEST_F(RemoteResourceObjectTest, QueryParamsForGetWillBePassedToBase)
     constexpr char PARAM_KEY[] { "aKey" };
     constexpr char VALUE[] { "value" };
 
-    object->get(RCSQueryParams().setResourceInterface(RESOURCEINTERFACE).setResourceType(RESOURCETYPE).
-            put(PARAM_KEY, VALUE),
-            [](const HeaderOpts&, const RCSRepresentation&, int){});
-
     auto mockHandler = mocks.Mock< CustomHandler >();
 
     mocks.ExpectCall(mockHandler, CustomHandler::handle).
@@ -228,6 +250,10 @@ TEST_F(RemoteResourceObjectTest, QueryParamsForGetWillBePassedToBase)
     server->setGetRequestHandler(std::bind(&CustomHandler::handle, mockHandler,
             std::placeholders::_1, std::placeholders::_2));
 
+    object->get(RCSQueryParams().setResourceInterface(RESOURCEINTERFACE).setResourceType(RESOURCETYPE).
+            put(PARAM_KEY, VALUE),
+            [](const HeaderOpts&, const RCSRepresentation&, int){});
+
     Wait();
 }
 
@@ -291,12 +317,21 @@ TEST_F(RemoteResourceObjectTest, CacheStateIsUnreadyAfterStartCaching)
     ASSERT_EQ(CacheState::UNREADY, object->getCacheState());
 }
 
+class TestCacheUpdatedCallback
+{
+public:
+    virtual void cacheUpdatedCallback(const RCSResourceAttributes&, int) = 0;
+    virtual ~TestCacheUpdatedCallback() { }
+};
+
 TEST_F(RemoteResourceObjectTest, CacheStateIsReadyAfterCacheUpdated)
 {
-    mocks.ExpectCallFunc(cacheUpdatedCallback).
-                Do([this](const RCSResourceAttributes&, int){ Proceed(); });
+    auto mockCallback = mocks.Mock< TestCacheUpdatedCallback >();
+    mocks.ExpectCall(mockCallback, TestCacheUpdatedCallback::cacheUpdatedCallback).
+            Do([this](const RCSResourceAttributes&, int){ Proceed(); });
 
-    object->startCaching(cacheUpdatedCallback);
+    object->startCaching(std::bind(&TestCacheUpdatedCallback::cacheUpdatedCallback,
+                           mockCallback, std::placeholders::_1, std::placeholders::_2));
     Wait();
 
     ASSERT_EQ(CacheState::READY, object->getCacheState());
@@ -304,10 +339,12 @@ TEST_F(RemoteResourceObjectTest, CacheStateIsReadyAfterCacheUpdated)
 
 TEST_F(RemoteResourceObjectTest, IsCachedAvailableReturnsTrueWhenCacheIsReady)
 {
-    mocks.ExpectCallFunc(cacheUpdatedCallback).
+    auto mockCallback = mocks.Mock< TestCacheUpdatedCallback >();
+    mocks.ExpectCall(mockCallback, TestCacheUpdatedCallback::cacheUpdatedCallback).
                 Do([this](const RCSResourceAttributes&, int){ Proceed(); });
 
-    object->startCaching(cacheUpdatedCallback);
+    object->startCaching(std::bind(&TestCacheUpdatedCallback::cacheUpdatedCallback,
+                                   mockCallback, std::placeholders::_1, std::placeholders::_2));
     Wait();
 
     ASSERT_TRUE(object->isCachedAvailable());
@@ -315,12 +352,15 @@ TEST_F(RemoteResourceObjectTest, IsCachedAvailableReturnsTrueWhenCacheIsReady)
 
 TEST_F(RemoteResourceObjectTest, DISABLED_CacheUpdatedCallbackBeCalledWheneverCacheUpdated)
 {
-    mocks.OnCallFunc(cacheUpdatedCallback).
-            Do([this](const RCSResourceAttributes&, int){ Proceed(); });
-    object->startCaching(cacheUpdatedCallback);
+    auto mockCallback = mocks.Mock< TestCacheUpdatedCallback >();
+
+    mocks.OnCall(mockCallback, TestCacheUpdatedCallback::cacheUpdatedCallback).
+                Do([this](const RCSResourceAttributes&, int){ Proceed(); });
+    object->startCaching(std::bind(&TestCacheUpdatedCallback::cacheUpdatedCallback,
+                                   mockCallback, std::placeholders::_1, std::placeholders::_2));
     Wait();
 
-    mocks.ExpectCallFunc(cacheUpdatedCallback).
+    mocks.ExpectCall(mockCallback, TestCacheUpdatedCallback::cacheUpdatedCallback).
             Do([this](const RCSResourceAttributes&, int){ Proceed(); });
 
     server->setAttribute(ATTR_KEY, ATTR_VALUE + 1);
@@ -332,12 +372,13 @@ TEST_F(RemoteResourceObjectTest, DISABLED_CacheUpdatedCallbackBeCalledWithUpdate
 {
     constexpr int newValue = ATTR_VALUE + 1;
 
-    mocks.OnCallFunc(cacheUpdatedCallback).
+    auto mockCallback = mocks.Mock< TestCacheUpdatedCallback >();
+    mocks.OnCall(mockCallback, TestCacheUpdatedCallback::cacheUpdatedCallback).
             Do([this](const RCSResourceAttributes&, int){ Proceed(); });
-    object->startCaching(cacheUpdatedCallback);
+    object->startCaching(std::bind(&TestCacheUpdatedCallback::cacheUpdatedCallback,
+                   mockCallback, std::placeholders::_1, std::placeholders::_2));
     Wait();
-
-    mocks.ExpectCallFunc(cacheUpdatedCallback).
+    mocks.ExpectCall(mockCallback, TestCacheUpdatedCallback::cacheUpdatedCallback).
             Match([this](const RCSResourceAttributes& attrs, int){
                 return attrs.at(ATTR_KEY) == newValue;
             }).
@@ -355,9 +396,11 @@ TEST_F(RemoteResourceObjectTest, GetCachedAttributesThrowsIfCachingIsNotStarted)
 
 TEST_F(RemoteResourceObjectTest, CachedAttributesHasSameAttributesWithServer)
 {
-    mocks.OnCallFunc(cacheUpdatedCallback).
+    auto mockCallback = mocks.Mock< TestCacheUpdatedCallback >();
+    mocks.OnCall(mockCallback, TestCacheUpdatedCallback::cacheUpdatedCallback).
             Do([this](const RCSResourceAttributes&, int){ Proceed(); });
-    object->startCaching(cacheUpdatedCallback);
+    object->startCaching(std::bind(&TestCacheUpdatedCallback::cacheUpdatedCallback,
+               mockCallback, std::placeholders::_1, std::placeholders::_2));
     Wait();
 
     RCSResourceObject::LockGuard lock{ server };
@@ -372,9 +415,11 @@ TEST_F(RemoteResourceObjectTest, GetCachedAttributeThrowsIfCachingIsNotStarted)
 
 TEST_F(RemoteResourceObjectTest, GetCachedAttributeThrowsIfKeyIsInvalid)
 {
-    mocks.OnCallFunc(cacheUpdatedCallback).
+    auto mockCallback = mocks.Mock< TestCacheUpdatedCallback >();
+    mocks.OnCall(mockCallback, TestCacheUpdatedCallback::cacheUpdatedCallback).
             Do([this](const RCSResourceAttributes&, int){ Proceed(); });
-    object->startCaching(cacheUpdatedCallback);
+    object->startCaching(std::bind(&TestCacheUpdatedCallback::cacheUpdatedCallback,
+                   mockCallback, std::placeholders::_1, std::placeholders::_2));
     Wait();
 
     ASSERT_THROW(object->getCachedAttribute(""), RCSInvalidKeyException);
index 3e0df64..06813a8 100644 (file)
@@ -94,13 +94,13 @@ rcs_client_test = rcs_test_env.Program('rcs_client_test', rcs_test_src)
 Alias("rcs_client_test", rcs_client_test)
 rcs_test_env.AppendTarget('rcs_client_test')
 
-# TODO: fix this test on linux and remove commented lines
+
 if rcs_test_env.get('TEST') == '1':
     if target_os in ['linux']:
+        rcs_test_env.AppendUnique(CPPDEFINES=['HIPPOMOCKS_ISSUE'])
         run_test(
             rcs_test_env,
-#           'service_resource-encapsulation_unittests_rcs_client_test.memcheck',
-            '',
+            'service_resource-encapsulation_unittests_rcs_client_test.memcheck',
             'service/resource-encapsulation/unittests/rcs_client_test')
 else:
     rcs_test_env.AppendUnique(CPPDEFINES=['LOCAL_RUNNING'])