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

Add UnitTest for "Resource Broker"

- Modify Sconscript to build UnitTest

- Add UnitTest Code After Generating "UnitTest" Folder

Add API for "DeviceAssociation" and "ResourcePresence"

- for checking empty list

- for checking list size

Modify Code to support 4.6 compiler

Modify White Space

Change-Id: I70c56d77da3540b95048fd13baa18b6044321115
Signed-off-by: default avatardoil.kwon <doil.kwon@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/1742Tested-by: default avatarjenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: default avatarUze Choi <uzchoi@samsung.com>
parent 7e25b6a3
......@@ -35,6 +35,10 @@ if env.get('LOGGING'):
lib_env = env.Clone()
SConscript(env.get('SRC_DIR') + '/service/third_party_libs.scons', 'lib_env')
######################### unit test setting ##########################
src_dir = lib_env.get('SRC_DIR')
gtest_dir = src_dir + '/extlibs/gtest/gtest-1.7.0'
######################################################################
resourcebroker_env = lib_env.Clone()
target_os = env.get('TARGET_OS')
......@@ -75,7 +79,43 @@ else :
resourcebroker_env.InstallTarget(resourcebrokersdk, 'libResouceBroker')
resourcebroker_env.UserInstallTargetLib(resourcebrokersdk, 'libResouceBroker')
######################################################################
# Build Test
######################################################################
broker_test_env = resourcebroker_env.Clone();
broker_test_env.AppendUnique(LIBPATH = [env.get('BUILD_DIR')])
broker_test_env.AppendUnique(CCFLAGS = ['-Wnoexcept'])
broker_test_env.PrependUnique(CPPPATH = [
env.get('SRC_DIR')+'/extlibs/hippomocks-master',
gtest_dir + '/include'
])
gtest = File(gtest_dir + '/lib/.libs/libgtest.a')
gtest_main = File(gtest_dir + '/lib/.libs/libgtest_main.a')
broker_test_env.PrependUnique(LIBS = [
'oc',
'octbstack',
'oc_logger',
'connectivity_abstraction',
'coap',
'service_common',
'pthread',
'ResourceBroker',
'ExpiryTimer',
gtest,
gtest_main
])
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')
######################################################################
# Go to build sample apps
#SConscript('SampleApp/SConscript')
......
......@@ -42,6 +42,7 @@ namespace OIC
DevicePresencePtr findDevice(const std::string & address);
void addDevice(DevicePresencePtr dPresence);
void removeDevice(DevicePresencePtr dPresence);
bool isEmptyDeviceList();
private:
DeviceAssociation();
......
......@@ -51,7 +51,7 @@ namespace OIC
void changePresenceMode(BROKER_MODE newMode);
bool isEmptyRequester() const;
int requesterListSize() const;
const PrimitiveResourcePtr getPrimitiveResource() const;
BROKER_STATE getResourceState() const;
......
......@@ -86,5 +86,10 @@ namespace OIC
foundDevice.reset();
}
}
bool DeviceAssociation::isEmptyDeviceList()
{
return s_deviceList.empty();
}
} // namespace Service
} // namespace OIC
......@@ -128,6 +128,10 @@ namespace OIC
return requesterList->empty();
}
int ResourcePresence::requesterListSize() const {
return (requesterList!=nullptr)?requesterList->size():0;
}
void ResourcePresence::requestResourceState() const
{
OC_LOG_V(DEBUG, BROKER_TAG, "Request Get\n");
......
#include <iostream>
#include "gtest/gtest.h"
#include "HippoMocks/hippomocks.h"
#include "OCPlatform.h"
#include "DevicePresence.h"
#include "DeviceAssociation.h"
#include "ResourcePresence.h"
#include "PrimitiveResource.h"
#include "ResponseStatement.h"
using namespace testing;
using namespace OIC::Service;
using namespace OC;
#define STRING_VALUE "10.242.34.235"
typedef OCStackResult (*subscribePresenceSig1)(OC::OCPlatform::OCPresenceHandle&,
const std::string&, OCConnectivityType, SubscribeCallback);
class DeviceAssociationTest : public Test
{
public:
MockRepository mocks;
DeviceAssociation * instance;
DevicePresencePtr device;
PrimitiveResource::Ptr pResource;
protected:
void setMockingFunc()
{
mocks.OnCall(pResource.get(), PrimitiveResource::requestGet);
mocks.OnCall(pResource.get(), PrimitiveResource::getHost).Return(STRING_VALUE);
mocks.OnCallFuncOverload(static_cast< subscribePresenceSig1 >(OC::OCPlatform::subscribePresence)).Return(OC_STACK_OK);
}
void SetAssociationDevice()
{
setMockingFunc();
device->initializeDevicePresence(pResource);
instance->addDevice(device);
}
void SetUp() override {
instance = DeviceAssociation::getInstance();
device = (DevicePresencePtr)new DevicePresence();
pResource = PrimitiveResource::Ptr(mocks.Mock< PrimitiveResource >(), [](PrimitiveResource*){});
}
void TearDown() override
{
device.reset();
pResource.reset();
}
virtual ~DeviceAssociationTest() noexcept(true)
{
}
};
TEST_F(DeviceAssociationTest,findDevice_ReturnNormalValueIfNormalParam)
{
SetAssociationDevice();
//pResource->getHost()
ASSERT_NE(nullptr,instance->findDevice(pResource->getHost()));
}
TEST_F(DeviceAssociationTest,addDevice_NormalHandlingIfNormalParam)
{
SetAssociationDevice();
ASSERT_FALSE(instance->isEmptyDeviceList());
}
TEST_F(DeviceAssociationTest,removeDevice_NormalHandlingIfNormalParam)
{
SetAssociationDevice();
instance->removeDevice(device);
ASSERT_TRUE(instance->isEmptyDeviceList());
}
#include "gtest/gtest.h"
#include "HippoMocks/hippomocks.h"
#include "PrimitiveResource.h"
#include "ResponseStatement.h"
#include "OCPlatform.h"
#include "DevicePresence.h"
#include "ResourcePresence.h"
using namespace testing;
using namespace OIC::Service;
using namespace OC;
typedef OCStackResult (*subscribePresenceSig1)(OC::OCPlatform::OCPresenceHandle&,
const std::string&, OCConnectivityType, SubscribeCallback);
class DevicePresenceTest : public Test
{
public:
MockRepository mocks;
DevicePresence * instance;
PrimitiveResource::Ptr pResource;
BrokerCB cb;
BrokerID id;
protected:
void SetUp() override {
instance = (DevicePresence*)new DevicePresence();
pResource = PrimitiveResource::Ptr(mocks.Mock< PrimitiveResource >(), [](PrimitiveResource*){});
cb = ([](BROKER_STATE)->OCStackResult{return OC_STACK_OK;});
id = 0;
}
void TearDown() override
{
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 ~DevicePresenceTest() noexcept(true)
{
}
};
TEST_F(DevicePresenceTest,initializeDevicePresence_NormalHandlingIfNormalResource)
{
MockingFunc();
ASSERT_NO_THROW(instance->initializeDevicePresence(pResource));
}
TEST_F(DevicePresenceTest,initializeDevicePresence_ErrorHandlingIfAbnormalResource)
{
MockingFunc();
mocks.OnCallFuncOverload(static_cast< subscribePresenceSig1 >(OC::OCPlatform::subscribePresence)).Return(OC_STACK_ERROR);
ASSERT_THROW(instance->initializeDevicePresence(pResource),PlatformException);
}
TEST_F(DevicePresenceTest,addPresenceResource_NormalHandlingIfNormalResource)
{
ResourcePresence * resource = (ResourcePresence *)new ResourcePresence();
instance->addPresenceResource(resource);
ASSERT_EQ(false,instance->isEmptyResourcePresence());
}
TEST_F(DevicePresenceTest,isEmptyResourcePresence_NormalHandling)
{
MockingFunc();
ASSERT_TRUE(instance->isEmptyResourcePresence());
}
TEST_F(DevicePresenceTest,getAddress_NormalHandling)
{
MockingFunc();
instance->initializeDevicePresence(pResource);
instance->getAddress();
}
TEST_F(DevicePresenceTest,NormalHandlingWhenReceivedCallbackMessage)
{
MockingFunc();
}
/*
* 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"
#include "OCPlatform.h"
#include "PrimitiveResource.h"
#include "ResponseStatement.h"
#include "ResourceBroker.h"
using namespace testing;
using namespace OIC::Service;
using namespace OC;
typedef OCStackResult (*subscribePresenceSig1)(OC::OCPlatform::OCPresenceHandle&,
const std::string&, OCConnectivityType, SubscribeCallback);
class ResourceBrokerTest : public Test
{
public:
MockRepository mocks;
ResourceBroker * brokerInstance;
PrimitiveResource::Ptr pResource;
BrokerCB cb;
BrokerID id;
protected:
void SetUp() override {
brokerInstance = ResourceBroker::getInstance();
pResource = PrimitiveResource::Ptr(mocks.Mock< PrimitiveResource >(), [](PrimitiveResource*){});
cb = ([](BROKER_STATE)->OCStackResult{return OC_STACK_OK;});
id = 0;
}
void TearDown() override
{
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)
{
MockingFunc();
BrokerID ret;
ASSERT_NE(ret = brokerInstance->hostResource(pResource,cb),0);
brokerInstance->cancelHostResource(ret);
}
/*
* [N]HostResource_NormalErrorHandlingIfResourceNull
*/
TEST_F(ResourceBrokerTest, HostResource_NormalErrorHandlingIfResourceNull)
{
ASSERT_THROW(brokerInstance->hostResource(nullptr, cb),ResourceBroker::InvalidParameterException);
}
/*
* [N]HostResource_NormalErrorHandlingIfCbFuncNull
*/
TEST_F(ResourceBrokerTest, HostResource_NormalErrorHandlingIfCbFuncNull)
{
ASSERT_THROW(brokerInstance->hostResource(pResource,nullptr),ResourceBroker::InvalidParameterException);
}
/*
* [P]CancelHostResource_NoThrowIfNormalParams
*/
TEST_F(ResourceBrokerTest,CancelHostResource_NoThrowIfNormalParams)
{
MockingFunc();
BrokerID ret;
ret = brokerInstance->hostResource(pResource,cb);
ASSERT_NO_THROW(brokerInstance->cancelHostResource(ret));
}
/*
* [N]CancelHostResource_NormalErrorHandlingIfAbNormalIdZero
*/
TEST_F(ResourceBrokerTest,CancelHostResource_NormalErrorHandlingIfAbNormalIdZero)
{
id = 0;
ASSERT_THROW(brokerInstance->cancelHostResource(id),ResourceBroker::InvalidParameterException);
}
/*
* [N]CancelHostResource_NormalErrorHandlingIfAbNormalIdOutOfRangeValue
*/
TEST_F(ResourceBrokerTest,CancelHostResource_NormalErrorHandlingIfAbNormalIdOutOfRangeValue)
{
id = -1;
ASSERT_THROW(brokerInstance->cancelHostResource(id),ResourceBroker::InvalidParameterException);
}
/*
* [P]getResourceState_ReturnNormalValueIfNormalId
*/
TEST_F(ResourceBrokerTest,getResourceState_ReturnNormalValueIfNormalId)
{
MockingFunc();
BrokerID ret;
ret = brokerInstance->hostResource(pResource,cb);
ASSERT_NE(brokerInstance->getResourceState(ret),BROKER_STATE::NONE);
brokerInstance->cancelHostResource(ret);
}
TEST_F(ResourceBrokerTest,getResourceState_NormalErrorHandlingIfIdZero)
{
id = 0;
ASSERT_THROW(brokerInstance->getResourceState(id),ResourceBroker::InvalidParameterException);
}
/*
* [P]getResourceState_ReturnNormalValueIfNormalResource
*/
TEST_F(ResourceBrokerTest,getResourceState_ReturnNormalValueIfNormalResource)
{
MockingFunc();
BrokerID ret;
ret = brokerInstance->hostResource(pResource,cb);
ASSERT_NE(brokerInstance->getResourceState(pResource),BROKER_STATE::NONE);
brokerInstance->cancelHostResource(ret);
}
/*
* [N]getResourceState_NormalErrorHandlingIfResourceNull
*/
TEST_F(ResourceBrokerTest,getResourceState_NormalErrorHandlingIfResourceNull)
{
ASSERT_THROW(brokerInstance->getResourceState((PrimitiveResource::Ptr)nullptr),ResourceBroker::InvalidParameterException);
}
/*
* [N]getResourceState_NormalErrorHandlingIfAbnormalResource
*/
TEST_F(ResourceBrokerTest,getResourceState_NormalErrorHandlingIfAbnormalResource)
{
MockingFunc();
PrimitiveResource::Ptr resource[3];
BrokerID id[3];
for(int i=0;i!=3;i++)
{
resource[i] = PrimitiveResource::Ptr(mocks.Mock< PrimitiveResource >(), [](PrimitiveResource*){});
mocks.OnCall(resource[i].get(), PrimitiveResource::requestGet);
mocks.OnCall(resource[i].get(), PrimitiveResource::getHost).Return(std::string());
mocks.OnCallFuncOverload(static_cast< subscribePresenceSig1 >(OC::OCPlatform::subscribePresence)).Return(OC_STACK_OK);
id[i] = brokerInstance->hostResource(resource[i],cb);
}
EXPECT_EQ(brokerInstance->getResourceState(pResource),BROKER_STATE::NONE);
for(int i=0;i!=3;i++)
{
brokerInstance->cancelHostResource(id[i]);
}
}
/*
* [N]getResourceState_NormalErrorHandlingIfAbnormalId
*/
TEST_F(ResourceBrokerTest,getResourceState_NormalErrorHandlingIfAbnormalId)
{
id = -1;
ASSERT_THROW(brokerInstance->getResourceState(id),ResourceBroker::InvalidParameterException);
}
#include <iostream>
#include <vector>
#include <unistd.h>
#include "gtest/gtest.h"
#include "HippoMocks/hippomocks.h"
#include "OCResource.h"
#include "OCPlatform.h"
#include "PrimitiveResource.h"
#include "ResponseStatement.h"
#include "ResourceAttributes.h"
#include "ResourcePresence.h"
using namespace testing;
using namespace OIC::Service;
using namespace OC;
typedef OCStackResult (*subscribePresenceSig1)(OC::OCPlatform::OCPresenceHandle&,
const std::string&, OCConnectivityType, SubscribeCallback);
class ResourcePresenceTest : public Test
{
public:
using GetCallback = std::function<
void(const OIC::Service::HeaderOptions&, const OIC::Service::ResponseStatement&, int)>;
MockRepository mocks;
ResourcePresence * instance;
PrimitiveResource::Ptr pResource;
BrokerCB cb;
BrokerID id;
protected:
void SetUp() override {
instance = (ResourcePresence*)new ResourcePresence();
pResource = PrimitiveResource::Ptr(mocks.Mock< PrimitiveResource >(), [](PrimitiveResource*){});
cb = ([](BROKER_STATE)->OCStackResult{return OC_STACK_OK;});
id = 0;
}
void TearDown() override
{
pResource.reset();
id = 0;
cb = nullptr;
}
void MockingFunc()
{
mocks.OnCall(pResource.get(), PrimitiveResource::requestGet).Do([](GetCallback cb){});
mocks.OnCall(pResource.get(), PrimitiveResource::getHost).Return(std::string());
mocks.OnCallFuncOverload(static_cast< subscribePresenceSig1 >(OC::OCPlatform::subscribePresence)).Return(OC_STACK_OK);
}
virtual ~ResourcePresenceTest() noexcept(true)
{
}
};
TEST_F(ResourcePresenceTest,isEmptyRequester_NormalHandling)
{
ASSERT_TRUE(instance->isEmptyRequester());
}
TEST_F(ResourcePresenceTest,initializeResourcePresence_NormalhandlingIfNormalResource)
{
MockingFunc();
instance->initializeResourcePresence(pResource);
ASSERT_NE(nullptr,instance->getPrimitiveResource());
}
TEST_F(ResourcePresenceTest,addBrokerRequester_ReturnNormalValueIfNormalParams)
{
MockingFunc();
instance->initializeResourcePresence(pResource);
id = 1;
instance->addBrokerRequester(id,cb);
EXPECT_FALSE(instance->isEmptyRequester());
}
TEST_F(ResourcePresenceTest,removeBrokerRequester_NormalHandlingIfNormalId)
{
MockingFunc();
instance->initializeResourcePresence(pResource);
id = 1;
instance->addBrokerRequester(id,cb);
id = 2;
instance->addBrokerRequester(id,cb);
instance->removeBrokerRequester(id);
ASSERT_EQ(1,instance->requesterListSize());
}
TEST_F(ResourcePresenceTest,removeAllBrokerRequester_NormalHandling)
{
MockingFunc();
instance->initializeResourcePresence(pResource);
id = 1;
instance->addBrokerRequester(id,cb);
id = 2;
instance->addBrokerRequester(id,cb);
instance->removeAllBrokerRequester();
ASSERT_TRUE(instance->isEmptyRequester());
}
TEST_F(ResourcePresenceTest,removeAllBrokerRequester_ErrorHandlingIfListNull)
{
MockingFunc();