From 57d8a92c6553b6bb9c4e10c57c915d1f29e20015 Mon Sep 17 00:00:00 2001 From: Younghyun Joo Date: Wed, 16 Sep 2015 20:20:36 +0900 Subject: [PATCH] Modify ResourceContainer UnitTest code for including Resource RCS Client module - add UnitTest case for DiscoverResourceUnit - add UnitTest case for RemoteResourceUnit Signed-off-by: Younghyun Joo Change-Id: I789db92d51ece367ff42babcf599b4d4c1093874 Reviewed-on: https://gerrit.iotivity.org/gerrit/2583 Tested-by: jenkins-iotivity Reviewed-by: Madan Lanka --- .../resourceContainer/include/RemoteResourceUnit.h | 12 +- .../resourceContainer/src/RemoteResourceUnit.cpp | 38 ++++- .../unittests/ResourceContainerTest.cpp | 170 ++++++++++++++++++++ .../ResourceEncapsulationTesetSimulator.h | 173 +++++++++++++++++++++ 4 files changed, 387 insertions(+), 6 deletions(-) create mode 100644 service/resource-encapsulation/src/resourceContainer/unittests/ResourceEncapsulationTesetSimulator.h diff --git a/service/resource-encapsulation/src/resourceContainer/include/RemoteResourceUnit.h b/service/resource-encapsulation/src/resourceContainer/include/RemoteResourceUnit.h index 7025e3b..272f576 100644 --- a/service/resource-encapsulation/src/resourceContainer/include/RemoteResourceUnit.h +++ b/service/resource-encapsulation/src/resourceContainer/include/RemoteResourceUnit.h @@ -39,7 +39,8 @@ namespace OIC }; typedef std::shared_ptr Ptr; - typedef std::function UpdatedCBFromServer; + typedef std::function UpdatedCBFromServer; RemoteResourceUnit(); ~RemoteResourceUnit(); @@ -57,8 +58,13 @@ namespace OIC public: static RemoteResourceUnit::Ptr createRemoteResourceInfo( - RCSRemoteResourceObject::Ptr ptr, - UpdatedCBFromServer updatedCB); + RCSRemoteResourceObject::Ptr ptr, UpdatedCBFromServer updatedCB); + static RemoteResourceUnit::Ptr createRemoteResourceInfoWithStateCB( + RCSRemoteResourceObject::Ptr ptr, UpdatedCBFromServer updatedCB, + RCSRemoteResourceObject::StateChangedCallback stateCB); + static RemoteResourceUnit::Ptr createRemoteResourceInfoWithCacheCB( + RCSRemoteResourceObject::Ptr ptr, UpdatedCBFromServer updatedCB, + RCSRemoteResourceObject::CacheUpdatedCallback cacheCB); RCSRemoteResourceObject::Ptr getRemoteResourceObject() const; std::string getRemoteResourceUri() const; diff --git a/service/resource-encapsulation/src/resourceContainer/src/RemoteResourceUnit.cpp b/service/resource-encapsulation/src/resourceContainer/src/RemoteResourceUnit.cpp index d6ab867..80d1523 100644 --- a/service/resource-encapsulation/src/resourceContainer/src/RemoteResourceUnit.cpp +++ b/service/resource-encapsulation/src/resourceContainer/src/RemoteResourceUnit.cpp @@ -34,8 +34,14 @@ RemoteResourceUnit::~RemoteResourceUnit() { if (remoteObject) { - remoteObject->stopCaching(); - remoteObject->stopMonitoring(); + if(remoteObject->isCaching()) + { + remoteObject->stopCaching(); + } + if(remoteObject->isMonitoring()) + { + remoteObject->stopMonitoring(); + } } } @@ -45,7 +51,33 @@ RemoteResourceUnit::Ptr RemoteResourceUnit::createRemoteResourceInfo( RemoteResourceUnit::Ptr retRemoteResourceUnit = std::make_shared(); retRemoteResourceUnit->remoteObject = ptr; retRemoteResourceUnit->pUpdatedCB = updatedCB; - + + return retRemoteResourceUnit; +} + +RemoteResourceUnit::Ptr RemoteResourceUnit::createRemoteResourceInfoWithStateCB( + RCSRemoteResourceObject::Ptr ptr, UpdatedCBFromServer updatedCB, + RCSRemoteResourceObject::StateChangedCallback stateCB) +{ + RemoteResourceUnit::Ptr retRemoteResourceUnit = std::make_shared(); + retRemoteResourceUnit->remoteObject = ptr; + retRemoteResourceUnit->pUpdatedCB = updatedCB; + + retRemoteResourceUnit->pStateChangedCB = stateCB; + + return retRemoteResourceUnit; +} + +RemoteResourceUnit::Ptr RemoteResourceUnit::createRemoteResourceInfoWithCacheCB( + RCSRemoteResourceObject::Ptr ptr, UpdatedCBFromServer updatedCB, + RCSRemoteResourceObject::CacheUpdatedCallback cacheCB) +{ + RemoteResourceUnit::Ptr retRemoteResourceUnit = std::make_shared(); + retRemoteResourceUnit->remoteObject = ptr; + retRemoteResourceUnit->pUpdatedCB = updatedCB; + + retRemoteResourceUnit->pCacheUpdateCB = cacheCB; + return retRemoteResourceUnit; } diff --git a/service/resource-encapsulation/src/resourceContainer/unittests/ResourceContainerTest.cpp b/service/resource-encapsulation/src/resourceContainer/unittests/ResourceContainerTest.cpp index c1c8a66..e760572 100644 --- a/service/resource-encapsulation/src/resourceContainer/unittests/ResourceContainerTest.cpp +++ b/service/resource-encapsulation/src/resourceContainer/unittests/ResourceContainerTest.cpp @@ -37,8 +37,12 @@ #include "RCSResourceContainer.h" #include "ResourceContainerBundleAPI.h" #include "ResourceContainerImpl.h" +#include "RemoteResourceUnit.h" #include "RCSResourceObject.h" +#include "RCSRemoteResourceObject.h" + +#include "ResourceEncapsulationTesetSimulator.h" using namespace std; using namespace testing; @@ -648,3 +652,169 @@ TEST(ConfigurationTest, BundleResourceConfigurationNotParsedWithInvalidBundleId) EXPECT_TRUE(bundles.empty()); } + +namespace +{ + void discoverdCB(RCSRemoteResourceObject::Ptr); + void onUpdate(RemoteResourceUnit::UPDATE_MSG, RCSRemoteResourceObject::Ptr); +} + +class DiscoverResourceUnitTest: public TestWithMock +{ + private: + typedef std::function values)> + UpdatedCB; + public: + ResourceEncapsulationTestSimulator::Ptr testObject; + DiscoverResourceUnit::Ptr m_pDiscoverResourceUnit; + std::string m_bundleId; + UpdatedCB m_updatedCB; + + protected: + void SetUp() + { + TestWithMock::SetUp(); + + testObject = std::make_shared(); + testObject->createResource(); + m_bundleId = "/a/TempHumSensor/Container"; + m_pDiscoverResourceUnit = std::make_shared< DiscoverResourceUnit >( m_bundleId ); + m_updatedCB = ([](const std::string, std::vector< RCSResourceAttributes::Value >) { }); + } + + void TearDown() + { + m_pDiscoverResourceUnit.reset(); + testObject.reset(); + TestWithMock::TearDown(); + } +}; + +TEST_F(DiscoverResourceUnitTest, startDiscover) +{ + std::string type = "Resource.Container"; + std::string attributeName = "TestResourceContainer"; + + m_pDiscoverResourceUnit->startDiscover( + DiscoverResourceUnit::DiscoverResourceInfo("", type, attributeName), m_updatedCB); + + std::chrono::milliseconds interval(400); + std::this_thread::sleep_for(interval); +} + +TEST_F(DiscoverResourceUnitTest, onUpdateCalled) +{ + std::string type = "Resource.Container"; + std::string attributeName = "TestResourceContainer"; + + m_pDiscoverResourceUnit->startDiscover( + DiscoverResourceUnit::DiscoverResourceInfo("", type, attributeName), m_updatedCB); + + std::chrono::milliseconds interval(400); + std::this_thread::sleep_for(interval); + + testObject->ChangeAttributeValue(); + +} + +namespace +{ + void onStateCB(ResourceState) { } + void onCacheCB(const RCSResourceAttributes &) { } +} + +class RemoteResourceUnitTest: public TestWithMock +{ + private: + typedef std::function + UpdatedCBFromServer; + + public: + ResourceEncapsulationTestSimulator::Ptr testObject; + RemoteResourceUnit::Ptr m_pRemoteResourceUnit; + RCSRemoteResourceObject::Ptr m_pRCSRemoteResourceObject; + UpdatedCBFromServer m_updatedCBFromServer; + + protected: + void SetUp() + { + TestWithMock::SetUp(); + + testObject = std::make_shared(); + testObject->defaultRunSimulator(); + m_pRCSRemoteResourceObject = testObject->getRemoteResource(); + m_updatedCBFromServer = ([](RemoteResourceUnit::UPDATE_MSG, RCSRemoteResourceObject::Ptr) {}); + } + + void TearDown() + { + m_pRCSRemoteResourceObject.reset(); + testObject.reset(); + TestWithMock::TearDown(); + } +}; + +TEST_F(RemoteResourceUnitTest, createRemoteResourceInfo) +{ + EXPECT_NE(nullptr, m_pRemoteResourceUnit->createRemoteResourceInfo(m_pRCSRemoteResourceObject, + m_updatedCBFromServer)); +} + +TEST_F(RemoteResourceUnitTest, getRemoteResourceObject) +{ + RemoteResourceUnit::Ptr ptr = m_pRemoteResourceUnit->createRemoteResourceInfo( + m_pRCSRemoteResourceObject, m_updatedCBFromServer); + EXPECT_EQ(m_pRCSRemoteResourceObject, ptr->getRemoteResourceObject()); +} + +TEST_F(RemoteResourceUnitTest, getRemoteResourceUri) +{ + RemoteResourceUnit::Ptr ptr = m_pRemoteResourceUnit->createRemoteResourceInfo( + m_pRCSRemoteResourceObject, m_updatedCBFromServer); + EXPECT_NE("", ptr->getRemoteResourceUri()); +} + +TEST_F(RemoteResourceUnitTest, startCaching) +{ + RemoteResourceUnit::Ptr ptr = m_pRemoteResourceUnit->createRemoteResourceInfo( + m_pRCSRemoteResourceObject, m_updatedCBFromServer); + ptr->startCaching(); +} + +TEST_F(RemoteResourceUnitTest, startMonitoring) +{ + RemoteResourceUnit::Ptr ptr = m_pRemoteResourceUnit->createRemoteResourceInfo( + m_pRCSRemoteResourceObject, m_updatedCBFromServer); + ptr->startMonitoring(); +} + +TEST_F(RemoteResourceUnitTest, onCacheCBCalled) +{ + bool isCalled = false; + mocks.ExpectCallFunc(onCacheCB).Do( + [this, &isCalled](const RCSResourceAttributes &) + { + isCalled = true; + }); + RemoteResourceUnit::Ptr ptr = m_pRemoteResourceUnit->createRemoteResourceInfoWithCacheCB( + m_pRCSRemoteResourceObject, m_updatedCBFromServer, onCacheCB); + ptr->startCaching(); + testObject->ChangeAttributeValue(); + EXPECT_TRUE(isCalled); +} + +TEST_F(RemoteResourceUnitTest, onStateCBCalled) +{ + bool isCalled = false; + mocks.ExpectCallFunc(onStateCB).Do( + [this, &isCalled](ResourceState) + { + isCalled = true; + }); + RemoteResourceUnit::Ptr ptr = m_pRemoteResourceUnit->createRemoteResourceInfoWithStateCB( + m_pRCSRemoteResourceObject, m_updatedCBFromServer, onStateCB); + ptr->startMonitoring(); + testObject->ChangeResourceState(); + EXPECT_TRUE(isCalled); +} \ No newline at end of file diff --git a/service/resource-encapsulation/src/resourceContainer/unittests/ResourceEncapsulationTesetSimulator.h b/service/resource-encapsulation/src/resourceContainer/unittests/ResourceEncapsulationTesetSimulator.h new file mode 100644 index 0000000..cf681a6 --- /dev/null +++ b/service/resource-encapsulation/src/resourceContainer/unittests/ResourceEncapsulationTesetSimulator.h @@ -0,0 +1,173 @@ +//ResourceEncapsulationTestSimulator.h +#include +#include +#include + +#include "UnitTestHelper.h" + +#include "RCSDiscoveryManager.h" +#include "RCSRemoteResourceObject.h" +#include "RCSResourceAttributes.h" +#include "RCSAddress.h" + +//#include "RequestObject.h" + +using namespace testing; +using namespace OIC::Service; + +class ResourceEncapsulationTestSimulator + : public std::enable_shared_from_this +{ + public: + typedef std::shared_ptr Ptr; + + RCSResourceObject::Ptr server; + RCSRemoteResourceObject::Ptr remoteResource; + + private: + std::mutex mutexForDiscovery; + + std::string MULTICASTURI; + std::string RESOURCEURI; + std::string RESOURCETYPE; + std::string RESOURCEINTERFACE; + std::string ATTR_KEY; + int ATTR_VALUE; + + public: + ResourceEncapsulationTestSimulator() + : server(nullptr), remoteResource(nullptr), + mutexForDiscovery(), + MULTICASTURI("/oic/res"), + RESOURCEURI("/a/TempHumSensor/Container"), + RESOURCETYPE("Resource.Container"), + RESOURCEINTERFACE("oic.if.baseline"), + ATTR_KEY("Temperature"), + ATTR_VALUE(0) + { } + + ~ResourceEncapsulationTestSimulator() + { + if (remoteResource != nullptr && remoteResource->isCaching()) + { + remoteResource->stopCaching(); + } + if (remoteResource != nullptr && remoteResource->isMonitoring()) + { + remoteResource->stopMonitoring(); + } + } + + private: + void onDiscoveryResource_Impl(RCSRemoteResourceObject::Ptr resourceObject) + { + if (remoteResource != nullptr) + { + return; + } + + if (RESOURCEURI.compare(resourceObject->getUri()) != 0) + { + return; + } + + remoteResource = resourceObject; + mutexForDiscovery.unlock(); + } + + static void onDiscoveryResource(RCSRemoteResourceObject::Ptr resourceObject, + std::weak_ptr rPtr) + { + std::shared_ptr ptr = rPtr.lock(); + if (ptr != nullptr) + { + ptr->onDiscoveryResource_Impl(resourceObject); + } + else + { + std::cout << "Aleady delete simulator\n"; + } + } + void waitForDiscovery() + { + std::chrono::milliseconds interval(100); + while (true) + { + if (mutexForDiscovery.try_lock()) + { + mutexForDiscovery.unlock(); + return; + } + std::this_thread::sleep_for(interval); + } + } + + public: + void defaultRunSimulator() + { + createResource(); + discoveryResource(); + waitForDiscovery(); + } + + void createResource() + { + server = RCSResourceObject::Builder(RESOURCEURI, RESOURCETYPE, RESOURCEINTERFACE) + .setDiscoverable(true).setObservable(true).build(); + server->setAttribute(ATTR_KEY, ATTR_VALUE); + } + + void discoveryResource() + { + discoveryResource(RESOURCETYPE); + } + + void discoveryResource(std::string &resourceType) + { + try + { + RCSDiscoveryManager::getInstance()->discoverResourceByType(RCSAddress::multicast(), + MULTICASTURI, resourceType, + std::bind(onDiscoveryResource, + std::placeholders::_1, + std::weak_ptr(shared_from_this()))); + mutexForDiscovery.lock(); + } + catch (std::exception &e) + { + std::cout << "exception : " << e.what() << std::endl; + } + } + + std::string getServerUri() const + { + return RESOURCEURI; + } + + RCSResourceObject::Ptr getResourceServer() const + { + return server; + } + + RCSRemoteResourceObject::Ptr getRemoteResource() const + { + return remoteResource; + } + + void ChangeAttributeValue() + { + std::chrono::milliseconds interval(100); + if (server != nullptr) + server->setAttribute(ATTR_KEY, ATTR_VALUE + 10); + std::this_thread::sleep_for(interval); + } + + void ChangeResourceState() + { + std::chrono::milliseconds interval(400); + if (server != nullptr) + server = nullptr; + std::this_thread::sleep_for(interval); + } + +}; -- 2.7.4