From d428b94d60b37e7ee45f79156602fb5093c27fff Mon Sep 17 00:00:00 2001 From: "doil.kwon" Date: Thu, 30 Jul 2015 19:43:19 +0900 Subject: [PATCH] Update 'resource broker' unit test code. 1. whether is called timeoutCB correctly or not when is occurred timeover about 'subscribe' request. 2. whether is called subscribeCB correctly or not when is request 'subscribe'. 3. whether is called timeoutCB correctly or not when is occurred timeover about 'get' request. and, modify some bug. this bug is not occurred problem at previous version.but correct this point in order to upgrade our unitTest' performance. add license comment and delete white space. add api for 'device presence' in order to get device_state' value. Change-Id: Ib265c6d529f84610cf134808bc5581377340bec6 Signed-off-by: doil.kwon Reviewed-on: https://gerrit.iotivity.org/gerrit/1987 Tested-by: jenkins-iotivity Reviewed-by: Madan Lanka --- .../src/resourceBroker/include/DevicePresence.h | 2 +- .../src/resourceBroker/src/DevicePresence.cpp | 5 +++- .../unittest/DevicePresenceUnitTest.cpp | 32 +++++++++++++++++++--- .../unittest/ResourceBrokerUnitTest.cpp | 1 - .../unittest/ResourcePresenceUnitTest.cpp | 16 +++++++++-- 5 files changed, 46 insertions(+), 10 deletions(-) diff --git a/service/resource-encapsulation/src/resourceBroker/include/DevicePresence.h b/service/resource-encapsulation/src/resourceBroker/include/DevicePresence.h index 6d711d0..a964e65 100644 --- a/service/resource-encapsulation/src/resourceBroker/include/DevicePresence.h +++ b/service/resource-encapsulation/src/resourceBroker/include/DevicePresence.h @@ -49,7 +49,7 @@ namespace OIC bool isEmptyResourcePresence() const; const std::string getAddress() const; - + DEVICE_STATE getDeviceState() const; private: std::list resourcePresenceList; diff --git a/service/resource-encapsulation/src/resourceBroker/src/DevicePresence.cpp b/service/resource-encapsulation/src/resourceBroker/src/DevicePresence.cpp index b959366..04f7546 100644 --- a/service/resource-encapsulation/src/resourceBroker/src/DevicePresence.cpp +++ b/service/resource-encapsulation/src/resourceBroker/src/DevicePresence.cpp @@ -70,7 +70,10 @@ namespace OIC presenceTimerHandle = presenceTimer.postTimer(BROKER_DEVICE_PRESENCE_TIMEROUT, pTimeoutCB); } - + DEVICE_STATE DevicePresence::getDeviceState() const + { + return state; + } const std::string DevicePresence::getAddress() const { OC_LOG_V(DEBUG, BROKER_TAG, "getAddress()"); diff --git a/service/resource-encapsulation/src/resourceBroker/unittest/DevicePresenceUnitTest.cpp b/service/resource-encapsulation/src/resourceBroker/unittest/DevicePresenceUnitTest.cpp index 31f8935..6d4b3de 100644 --- a/service/resource-encapsulation/src/resourceBroker/unittest/DevicePresenceUnitTest.cpp +++ b/service/resource-encapsulation/src/resourceBroker/unittest/DevicePresenceUnitTest.cpp @@ -17,10 +17,12 @@ // limitations under the License. // //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +#include #include "gtest/gtest.h" #include "HippoMocks/hippomocks.h" +#include "BrokerTypes.h" #include "PrimitiveResource.h" #include "ResponseStatement.h" #include "OCPlatform.h" @@ -38,7 +40,7 @@ typedef OCStackResult (*subscribePresenceSig1)(OC::OCPlatform::OCPresenceHandle& class DevicePresenceTest : public TestWithMock { public: - + typedef std::function subscribeCallback; DevicePresence * instance; PrimitiveResource::Ptr pResource; BrokerCB cb; @@ -46,7 +48,7 @@ public: protected: - void SetUp() + void SetUp() { TestWithMock::SetUp(); instance = (DevicePresence*)new DevicePresence(); @@ -55,7 +57,7 @@ protected: id = 0; } - void TearDown() + void TearDown() { TestWithMock::TearDown(); pResource.reset(); @@ -65,11 +67,33 @@ protected: void MockingFunc() { - mocks.OnCall(pResource.get(), PrimitiveResource::requestGet); mocks.OnCall(pResource.get(), PrimitiveResource::getHost).Return(std::string()); mocks.OnCallFuncOverload(static_cast< subscribePresenceSig1 >(OC::OCPlatform::subscribePresence)).Return(OC_STACK_OK); } }; +TEST_F(DevicePresenceTest,timeoutCB_TimeOverWhenIsSubscribe) +{ + MockingFunc(); + instance->initializeDevicePresence(pResource); + std::cout<<"wait while done timeout device presence\n"; + sleep((BROKER_DEVICE_PRESENCE_TIMEROUT/1000)+1); + ASSERT_EQ(DEVICE_STATE::LOST_SIGNAL,instance->getDeviceState()); +} + +TEST_F(DevicePresenceTest,SubscribeCB_NormalHandlingIfMessageOC_STACK_OK) +{ + mocks.OnCall(pResource.get(), PrimitiveResource::getHost).Return(std::string()); + mocks.OnCallFuncOverload(static_cast< subscribePresenceSig1 >(OC::OCPlatform::subscribePresence)).Do( + [](OC::OCPlatform::OCPresenceHandle&, + const std::string&, OCConnectivityType, SubscribeCallback callback)->OCStackResult{ + + callback(OC_STACK_OK,0,std::string()); + return OC_STACK_OK; + + }).Return(OC_STACK_OK); + instance->initializeDevicePresence(pResource); + ASSERT_NE(DEVICE_STATE::LOST_SIGNAL,instance->getDeviceState()); +} TEST_F(DevicePresenceTest,initializeDevicePresence_NormalHandlingIfNormalResource) { diff --git a/service/resource-encapsulation/src/resourceBroker/unittest/ResourceBrokerUnitTest.cpp b/service/resource-encapsulation/src/resourceBroker/unittest/ResourceBrokerUnitTest.cpp index 1efebbc..2cddd25 100644 --- a/service/resource-encapsulation/src/resourceBroker/unittest/ResourceBrokerUnitTest.cpp +++ b/service/resource-encapsulation/src/resourceBroker/unittest/ResourceBrokerUnitTest.cpp @@ -17,7 +17,6 @@ // limitations under the License. // //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= - #include "gtest/gtest.h" #include "HippoMocks/hippomocks.h" diff --git a/service/resource-encapsulation/src/resourceBroker/unittest/ResourcePresenceUnitTest.cpp b/service/resource-encapsulation/src/resourceBroker/unittest/ResourcePresenceUnitTest.cpp index a8be8c0..6c07f58 100644 --- a/service/resource-encapsulation/src/resourceBroker/unittest/ResourcePresenceUnitTest.cpp +++ b/service/resource-encapsulation/src/resourceBroker/unittest/ResourcePresenceUnitTest.cpp @@ -17,7 +17,6 @@ // limitations under the License. // //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= - #include #include #include @@ -30,6 +29,7 @@ #include "OCPlatform.h" #include "PrimitiveResource.h" +#include "BrokerTypes.h" #include "ResponseStatement.h" #include "RCSResourceAttributes.h" #include "ResourcePresence.h" @@ -55,7 +55,7 @@ public: protected: - void SetUp() + void SetUp() { TestWithMock::SetUp(); instance.reset(new ResourcePresence()); @@ -64,7 +64,7 @@ protected: id = 0; } - void TearDown() + void TearDown() { TestWithMock::TearDown(); instance.reset(); @@ -81,6 +81,16 @@ protected: } }; +TEST_F(ResourcePresenceTest,timeoutCB_TimeOverWhenIsRequestGet) +{ + MockingFunc(); + instance->initializeResourcePresence(pResource); + std::cout<<"wait while done timeout requestGet\n"; + BROKER_STATE state; + state = instance->getResourceState(); + sleep((BROKER_DEVICE_PRESENCE_TIMEROUT/1000)+1); + ASSERT_EQ(state,instance->getResourceState()); +} TEST_F(ResourcePresenceTest,initializeResourcePresence_NormalhandlingIfNormalResource) { -- 2.7.4