Commit 7c14e07d authored by doil.kwon's avatar doil.kwon Committed by Uze Choi

Modify UnitTest Code for 'ResourceBroker'

- Bug fix : Segmentation Fault Issue
- Support 4.6 gcc compiler and 4.7 gcc compiler

Modify WhiteSpace Error

Change-Id: I9fa65b24b7133f08fcd5ecb03fd163fa6de106b7
Signed-off-by: default avatardoil.kwon <doil.kwon@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/1808Tested-by: default avatarjenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: default avatarHun-je Yeon <hunje.yeon@samsung.com>
Reviewed-by: default avatarUze Choi <uzchoi@samsung.com>
parent 9a2042c8
......@@ -110,7 +110,6 @@ broker_test_env.PrependUnique(LIBS = [
])
broker_test_src = env.Glob('unittest/*.cpp')
#broker_test_src = ['unittest/ResourcePresenceUnitTest.cpp']
broker_test = broker_test_env.Program('broker_test', broker_test_src)
Alias("broker_test", broker_test)
env.AppendTarget('broker_test')
......
......@@ -49,14 +49,14 @@ namespace OIC
void DevicePresence::initializeDevicePresence(PrimitiveResourcePtr pResource)
{
OC_LOG_V(DEBUG, BROKER_TAG, "%s",pResource->getHost().c_str());
address = pResource->getHost();
OC_LOG_V(DEBUG, BROKER_TAG, "%s",address.c_str());
try
{
presenceSubscriber
= PresenceSubscriber(pResource->getHost(), BROKER_TRANSPORT, pSubscribeRequestCB);
= PresenceSubscriber(address, BROKER_TRANSPORT, pSubscribeRequestCB);
OC_LOG_V(DEBUG, BROKER_TAG, "subscribe Presence");
} catch(PlatformException &e)
{
......
......@@ -29,6 +29,7 @@ public:
DevicePresencePtr device;
PrimitiveResource::Ptr pResource;
protected:
void setMockingFunc()
{
mocks.OnCall(pResource.get(), PrimitiveResource::requestGet);
......@@ -43,13 +44,14 @@ protected:
instance->addDevice(device);
}
void SetUp() override {
void SetUp()
{
instance = DeviceAssociation::getInstance();
device = (DevicePresencePtr)new DevicePresence();
pResource = PrimitiveResource::Ptr(mocks.Mock< PrimitiveResource >(), [](PrimitiveResource*){});
}
void TearDown() override
void TearDown()
{
device.reset();
pResource.reset();
......@@ -64,23 +66,28 @@ protected:
TEST_F(DeviceAssociationTest,findDevice_ReturnNormalValueIfNormalParam)
{
SetUp();
SetAssociationDevice();
//pResource->getHost()
ASSERT_NE(nullptr,instance->findDevice(pResource->getHost()));
TearDown();
}
TEST_F(DeviceAssociationTest,addDevice_NormalHandlingIfNormalParam)
{
SetUp();
SetAssociationDevice();
ASSERT_FALSE(instance->isEmptyDeviceList());
TearDown();
}
TEST_F(DeviceAssociationTest,removeDevice_NormalHandlingIfNormalParam)
{
SetUp();
SetAssociationDevice();
instance->removeDevice(device);
ASSERT_TRUE(instance->isEmptyDeviceList());
TearDown();
}
......@@ -15,7 +15,6 @@ using namespace OC;
typedef OCStackResult (*subscribePresenceSig1)(OC::OCPlatform::OCPresenceHandle&,
const std::string&, OCConnectivityType, SubscribeCallback);
class DevicePresenceTest : public Test
{
public:
......@@ -27,14 +26,16 @@ public:
BrokerID id;
protected:
void SetUp() override {
void SetUp()
{
instance = (DevicePresence*)new DevicePresence();
pResource = PrimitiveResource::Ptr(mocks.Mock< PrimitiveResource >(), [](PrimitiveResource*){});
cb = ([](BROKER_STATE)->OCStackResult{return OC_STACK_OK;});
id = 0;
}
void TearDown() override
void TearDown()
{
pResource.reset();
id = 0;
......@@ -47,6 +48,7 @@ protected:
mocks.OnCall(pResource.get(), PrimitiveResource::getHost).Return(std::string());
mocks.OnCallFuncOverload(static_cast< subscribePresenceSig1 >(OC::OCPlatform::subscribePresence)).Return(OC_STACK_OK);
}
virtual ~DevicePresenceTest() noexcept(true)
{
}
......@@ -55,43 +57,55 @@ protected:
TEST_F(DevicePresenceTest,initializeDevicePresence_NormalHandlingIfNormalResource)
{
SetUp();
MockingFunc();
ASSERT_NO_THROW(instance->initializeDevicePresence(pResource));
TearDown();
}
TEST_F(DevicePresenceTest,initializeDevicePresence_ErrorHandlingIfAbnormalResource)
{
SetUp();
MockingFunc();
mocks.OnCallFuncOverload(static_cast< subscribePresenceSig1 >(OC::OCPlatform::subscribePresence)).Return(OC_STACK_ERROR);
ASSERT_THROW(instance->initializeDevicePresence(pResource),PlatformException);
TearDown();
}
TEST_F(DevicePresenceTest,addPresenceResource_NormalHandlingIfNormalResource)
{
SetUp();
ResourcePresence * resource = (ResourcePresence *)new ResourcePresence();
instance->addPresenceResource(resource);
ASSERT_EQ(false,instance->isEmptyResourcePresence());
TearDown();
}
TEST_F(DevicePresenceTest,isEmptyResourcePresence_NormalHandling)
{
SetUp();
MockingFunc();
ASSERT_TRUE(instance->isEmptyResourcePresence());
TearDown();
}
TEST_F(DevicePresenceTest,getAddress_NormalHandling)
{
SetUp();
MockingFunc();
instance->initializeDevicePresence(pResource);
instance->getAddress();
TearDown();
}
TEST_F(DevicePresenceTest,NormalHandlingWhenReceivedCallbackMessage)
{
SetUp();
MockingFunc();
TearDown();
}
/*
* 01.[P]HostResource_ReturnNormalValueIfNormalParams
* 02.[N]HostResource_NormalErrorHandlingIfResourceNull
* 03.[N]HostResource_NormalErrorHandlingIfCbFuncNull
* 04.[P]CancelHostResource_NoThrowIfNormalParams
* 05.[N]CancelHostResource_NormalErrorHandlingIfAbNormalId
* 06.[N]CancelHostResource_NormalErrorHandlingIfAbNormalIdOutOfRangeValue
* 07.[P]getResourceState_ReturnNormalValueIfNormalId
* 08.[P]getResourceState_ReturnNormalValueIfNormalResource
* 09.[N]getResourceState_NormalErrorHandlingIfResourceNull
* 10.[N]getResourceState_NormalErrorHandlingIfAbnormalResource
* 11.[N]getResourceState_NormalErrorHandlingIfAbnormalId
*/
#include "gtest/gtest.h"
#include "HippoMocks/hippomocks.h"
......@@ -38,91 +24,93 @@ public:
BrokerID id;
protected:
void SetUp() override {
void SetUp()
{
brokerInstance = ResourceBroker::getInstance();
pResource = PrimitiveResource::Ptr(mocks.Mock< PrimitiveResource >(), [](PrimitiveResource*){});
cb = ([](BROKER_STATE)->OCStackResult{return OC_STACK_OK;});
id = 0;
}
void TearDown() override
void TearDown()
{
pResource.reset();
id = 0;
cb = nullptr;
}
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);
}
virtual ~ResourceBrokerTest() noexcept(true)
{
}
};
/*
* [P]HostResource_ReturnNormalValueIfNormalParams
*/
TEST_F(ResourceBrokerTest,HostResource_ReturnNormalValueIfNormalParams)
{
SetUp();
MockingFunc();
BrokerID ret;
ASSERT_NE(ret = brokerInstance->hostResource(pResource,cb),0);
brokerInstance->cancelHostResource(ret);
TearDown();
}
/*
* [N]HostResource_NormalErrorHandlingIfResourceNull
*/
TEST_F(ResourceBrokerTest, HostResource_NormalErrorHandlingIfResourceNull)
{
SetUp();
ASSERT_THROW(brokerInstance->hostResource(nullptr, cb),ResourceBroker::InvalidParameterException);
TearDown();
}
/*
* [N]HostResource_NormalErrorHandlingIfCbFuncNull
*/
TEST_F(ResourceBrokerTest, HostResource_NormalErrorHandlingIfCbFuncNull)
{
SetUp();
ASSERT_THROW(brokerInstance->hostResource(pResource,nullptr),ResourceBroker::InvalidParameterException);
TearDown();
}
/*
* [P]CancelHostResource_NoThrowIfNormalParams
*/
TEST_F(ResourceBrokerTest,CancelHostResource_NoThrowIfNormalParams)
{
SetUp();
MockingFunc();
BrokerID ret;
ret = brokerInstance->hostResource(pResource,cb);
ASSERT_NO_THROW(brokerInstance->cancelHostResource(ret));
TearDown();
}
/*
* [N]CancelHostResource_NormalErrorHandlingIfAbNormalIdZero
*/
TEST_F(ResourceBrokerTest,CancelHostResource_NormalErrorHandlingIfAbNormalIdZero)
{
SetUp();
id = 0;
ASSERT_THROW(brokerInstance->cancelHostResource(id),ResourceBroker::InvalidParameterException);
TearDown();
}
/*
* [N]CancelHostResource_NormalErrorHandlingIfAbNormalIdOutOfRangeValue
*/
TEST_F(ResourceBrokerTest,CancelHostResource_NormalErrorHandlingIfAbNormalIdOutOfRangeValue)
{
SetUp();
id = -1;
ASSERT_THROW(brokerInstance->cancelHostResource(id),ResourceBroker::InvalidParameterException);
TearDown();
}
/*
* [P]getResourceState_ReturnNormalValueIfNormalId
*/
TEST_F(ResourceBrokerTest,getResourceState_ReturnNormalValueIfNormalId)
{
SetUp();
MockingFunc();
BrokerID ret;
......@@ -131,20 +119,21 @@ TEST_F(ResourceBrokerTest,getResourceState_ReturnNormalValueIfNormalId)
ASSERT_NE(brokerInstance->getResourceState(ret),BROKER_STATE::NONE);
brokerInstance->cancelHostResource(ret);
TearDown();
}
TEST_F(ResourceBrokerTest,getResourceState_NormalErrorHandlingIfIdZero)
{
SetUp();
id = 0;
ASSERT_THROW(brokerInstance->getResourceState(id),ResourceBroker::InvalidParameterException);
TearDown();
}
/*
* [P]getResourceState_ReturnNormalValueIfNormalResource
*/
TEST_F(ResourceBrokerTest,getResourceState_ReturnNormalValueIfNormalResource)
{
SetUp();
MockingFunc();
BrokerID ret;
......@@ -153,20 +142,19 @@ TEST_F(ResourceBrokerTest,getResourceState_ReturnNormalValueIfNormalResource)
ASSERT_NE(brokerInstance->getResourceState(pResource),BROKER_STATE::NONE);
brokerInstance->cancelHostResource(ret);
TearDown();
}
/*
* [N]getResourceState_NormalErrorHandlingIfResourceNull
*/
TEST_F(ResourceBrokerTest,getResourceState_NormalErrorHandlingIfResourceNull)
{
SetUp();
ASSERT_THROW(brokerInstance->getResourceState((PrimitiveResource::Ptr)nullptr),ResourceBroker::InvalidParameterException);
TearDown();
}
/*
* [N]getResourceState_NormalErrorHandlingIfAbnormalResource
*/
TEST_F(ResourceBrokerTest,getResourceState_NormalErrorHandlingIfAbnormalResource)
{
SetUp();
MockingFunc();
PrimitiveResource::Ptr resource[3];
......@@ -183,17 +171,19 @@ TEST_F(ResourceBrokerTest,getResourceState_NormalErrorHandlingIfAbnormalResource
EXPECT_EQ(brokerInstance->getResourceState(pResource),BROKER_STATE::NONE);
for(int i=0;i!=3;i++)
{
brokerInstance->cancelHostResource(id[i]);
}
TearDown();
}
/*
* [N]getResourceState_NormalErrorHandlingIfAbnormalId
*/
TEST_F(ResourceBrokerTest,getResourceState_NormalErrorHandlingIfAbnormalId)
{
SetUp();
id = -1;
ASSERT_THROW(brokerInstance->getResourceState(id),ResourceBroker::InvalidParameterException);
TearDown();
}
......@@ -25,8 +25,8 @@ class ResourcePresenceTest : public Test
{
public:
using GetCallback = std::function<
void(const OIC::Service::HeaderOptions&, const OIC::Service::ResponseStatement&, int)>;
typedef std::function<void(const HeaderOptions&, const ResponseStatement&, int)> GetCallback;
MockRepository mocks;
ResourcePresence * instance;
PrimitiveResource::Ptr pResource;
......@@ -34,14 +34,16 @@ public:
BrokerID id;
protected:
void SetUp() override {
void SetUp()
{
instance = (ResourcePresence*)new ResourcePresence();
pResource = PrimitiveResource::Ptr(mocks.Mock< PrimitiveResource >(), [](PrimitiveResource*){});
cb = ([](BROKER_STATE)->OCStackResult{return OC_STACK_OK;});
id = 0;
}
void TearDown() override
void TearDown()
{
pResource.reset();
id = 0;
......@@ -60,32 +62,31 @@ protected:
};
TEST_F(ResourcePresenceTest,isEmptyRequester_NormalHandling)
{
ASSERT_TRUE(instance->isEmptyRequester());
}
TEST_F(ResourcePresenceTest,initializeResourcePresence_NormalhandlingIfNormalResource)
{
SetUp();
MockingFunc();
instance->initializeResourcePresence(pResource);
ASSERT_NE(nullptr,instance->getPrimitiveResource());
TearDown();
}
TEST_F(ResourcePresenceTest,addBrokerRequester_ReturnNormalValueIfNormalParams)
{
SetUp();
MockingFunc();
instance->initializeResourcePresence(pResource);
id = 1;
instance->addBrokerRequester(id,cb);
EXPECT_FALSE(instance->isEmptyRequester());
TearDown();
}
TEST_F(ResourcePresenceTest,removeBrokerRequester_NormalHandlingIfNormalId)
{
SetUp();
MockingFunc();
instance->initializeResourcePresence(pResource);
......@@ -96,11 +97,12 @@ TEST_F(ResourcePresenceTest,removeBrokerRequester_NormalHandlingIfNormalId)
instance->removeBrokerRequester(id);
ASSERT_EQ(1,instance->requesterListSize());
TearDown();
}
TEST_F(ResourcePresenceTest,removeAllBrokerRequester_NormalHandling)
{
SetUp();
MockingFunc();
instance->initializeResourcePresence(pResource);
......@@ -111,60 +113,72 @@ TEST_F(ResourcePresenceTest,removeAllBrokerRequester_NormalHandling)
instance->removeAllBrokerRequester();
ASSERT_TRUE(instance->isEmptyRequester());
TearDown();
}
TEST_F(ResourcePresenceTest,removeAllBrokerRequester_ErrorHandlingIfListNull)
{
SetUp();
MockingFunc();
instance->initializeResourcePresence(pResource);
instance->removeAllBrokerRequester();
TearDown();
}
TEST_F(ResourcePresenceTest,requestResourceState_NormalHandling)
{
SetUp();
MockingFunc();
instance->initializeResourcePresence(pResource);
ASSERT_NO_THROW(instance->requestResourceState());
TearDown();
}
TEST_F(ResourcePresenceTest,changePresenceMode_NormalHandlingIfNewModeDifferent)
{
SetUp();
MockingFunc();
instance->initializeResourcePresence(pResource);
instance->changePresenceMode(BROKER_MODE::DEVICE_PRESENCE_MODE);
TearDown();
}
TEST_F(ResourcePresenceTest,getResourceState_NormalHandling)
{
SetUp();
ASSERT_EQ(BROKER_STATE::REQUESTED,instance->getResourceState());
TearDown();
}
TEST_F(ResourcePresenceTest,changePresenceMode_NormalHandlingIfNewModeSame)
{
SetUp();
MockingFunc();
instance->initializeResourcePresence(pResource);
instance->changePresenceMode(BROKER_MODE::NON_PRESENCE_MODE);
TearDown();
}
TEST_F(ResourcePresenceTest,getPrimitiveResource_NormalHandling)
{
SetUp();
MockingFunc();
instance->initializeResourcePresence(pResource);
ASSERT_NE(nullptr,instance->getPrimitiveResource());
TearDown();
}
TEST_F(ResourcePresenceTest,getCB_NormalHandlingIfMessageOC_STACK_OK)
{
SetUp();
mocks.ExpectCall(pResource.get(), PrimitiveResource::requestGet).Do(
[](GetCallback callback){
......@@ -191,5 +205,18 @@ TEST_F(ResourcePresenceTest,getCB_NormalHandlingIfMessageOC_STACK_OK)
instance->initializeResourcePresence(pResource);
sleep(3);
TearDown();
}
TEST_F(ResourcePresenceTest,isEmptyRequester_NormalHandling)
{
SetUp();
MockingFunc();
instance->initializeResourcePresence(pResource);
id = 1;
instance->addBrokerRequester(id,cb);
instance->removeAllBrokerRequester();
ASSERT_TRUE(instance->isEmptyRequester());
TearDown();
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment