resourcecache_env = lib_env.Clone()
target_os = env.get('TARGET_OS')
+
######################################################################
# Build flags
######################################################################
+
resourcecache_env.AppendUnique(CPPPATH = ['include'])
resourcecache_env.AppendUnique(CPPPATH = ['../../include'])
resourcecache_env.AppendUnique(CPPPATH = ['../common/primitiveResource/include'])
resourcecache_env.AppendUnique(CPPPATH = ['../common/expiryTimer/src'])
resourcecache_env.PrependUnique(LIBS = ['service_common', 'ExpiryTimer', 'oc', 'octbstack', 'oc_logger', 'connectivity_abstraction', 'libcoap'])
+
if target_os not in ['windows', 'winrt']:
resourcecache_env.AppendUnique(CXXFLAGS = ['-O2', '-g', '-Wall', '-fmessage-length=0', '-std=c++0x'])
# Go to build sample apps
#SConscript('SampleApp/SConscript')
+# Go to Unit Test
+SConscript('unittests/SConscript')
+
class DataCache;
- #define CACHE_TAG PCF("CACHE")
- #define CACHE_DEFAULT_REPORT_MILLITIME 10000
- #define CACHE_DEFAULT_EXPIRED_MILLITIME 15000
+#define CACHE_TAG PCF("CACHE")
+#define CACHE_DEFAULT_REPORT_MILLITIME 10000
+#define CACHE_DEFAULT_EXPIRED_MILLITIME 15000
enum class REPORT_FREQUENCY
{
typedef int CacheID;
typedef std::function<OCStackResult(std::shared_ptr<PrimitiveResource>,
- const ResourceAttributes &)> CacheCB;
+ const ResourceAttributes &)> CacheCB;
typedef std::map<int, std::pair<Report_Info, CacheCB>> SubscriberInfo;
typedef std::pair<int, std::pair<Report_Info, CacheCB>> SubscriberInfoPair;
{
class DataCache
{
- public:
- typedef unsigned int TimerID;
- typedef std::function<void *(TimerID)> TimerCB;
-
- public:
- DataCache();
- ~DataCache();
-
- void initializeDataCache(PrimitiveResourcePtr pResource);
-
- CacheID addSubscriber(CacheCB func, REPORT_FREQUENCY rf, long repeatTime);
- CacheID deleteSubscriber(CacheID id);
-
- CACHE_STATE getCacheState() const;
- const ResourceAttributes getCachedData() const;
- const PrimitiveResourcePtr getPrimitiveResource() const;
-
- void requestGet();
- bool isEmptySubscriber() const;
-
- private:
- // resource instance
- PrimitiveResourcePtr sResource;
- std::shared_ptr<BaseResource> baseHandler;
-
- // cached data info
- ResourceAttributes attributes;
- CACHE_STATE state;
-
- // subscriber info
- std::unique_ptr<SubscriberInfo> subscriberList;
-
- ExpiryTimer networkTimer;
- ExpiryTimer pollingTimer;
- TimerID networkTimeOutHandle;
- TimerID pollingHandle;
-
- ObserveCB pObserveCB;
- GetCB pGetCB;
- TimerCB pTimerCB;
- TimerCB pPollingCB;
-
- // for requestCB from base
- void onObserve(const HeaderOptions& _hos,
- const ResponseStatement& _rep, int _result, int _seq);
- void onGet(const HeaderOptions& _hos, const ResponseStatement& _rep, int _result);
- void *onTimeOut(const unsigned int timerID);
- void *onPollingOut(const unsigned int timerID);
-
- CacheID generateCacheID();
- SubscriberInfoPair findSubscriber(CacheID id);
- void notifyObservers(ResourceAttributes Att);
+ public:
+ typedef unsigned int TimerID;
+ typedef std::function<void(TimerID)> TimerCB;
+
+ public:
+ DataCache();
+ ~DataCache();
+
+ void initializeDataCache(PrimitiveResourcePtr pResource);
+
+ CacheID addSubscriber(CacheCB func, REPORT_FREQUENCY rf, long repeatTime);
+ CacheID deleteSubscriber(CacheID id);
+
+ CACHE_STATE getCacheState() const;
+ const ResourceAttributes getCachedData() const;
+ const PrimitiveResourcePtr getPrimitiveResource() const;
+
+ void requestGet();
+ bool isEmptySubscriber() const;
+
+ private:
+ // resource instance
+ PrimitiveResourcePtr sResource;
+ std::shared_ptr<BaseResource> baseHandler;
+
+ // cached data info
+ ResourceAttributes attributes;
+ CACHE_STATE state;
+
+ // subscriber info
+ std::unique_ptr<SubscriberInfo> subscriberList;
+
+ ExpiryTimer networkTimer;
+ ExpiryTimer pollingTimer;
+ TimerID networkTimeOutHandle;
+ TimerID pollingHandle;
+
+ ObserveCB pObserveCB;
+ GetCB pGetCB;
+ TimerCB pTimerCB;
+ TimerCB pPollingCB;
+
+ // for requestCB from base
+ void onObserve(const HeaderOptions &_hos,
+ const ResponseStatement &_rep, int _result, int _seq);
+ void onGet(const HeaderOptions &_hos, const ResponseStatement &_rep, int _result);
+ void onTimeOut(const unsigned int timerID);
+ void onPollingOut(const unsigned int timerID);
+
+ CacheID generateCacheID();
+ SubscriberInfoPair findSubscriber(CacheID id);
+ void notifyObservers(ResourceAttributes Att);
};
} // namespace Service
} // namespace OIC
{
class ResourceCacheManager
{
- public:
- class InvalidParameterException: public RCSException
- {
public:
- InvalidParameterException(std::string&& what)
- : RCSException{ std::move(what) } {}
- };
+ class InvalidParameterException: public RCSException
+ {
+ public:
+ InvalidParameterException(std::string &&what)
+ : RCSException { std::move(what) } {}
+ };
- static ResourceCacheManager * getInstance();
+ static ResourceCacheManager *getInstance();
- CacheID requestResourceCache(
+ CacheID requestResourceCache(
PrimitiveResourcePtr pResource, CacheCB func = NULL,
REPORT_FREQUENCY rf = REPORT_FREQUENCY::NONE, long time = 0l);
- void cancelResourceCache(CacheID id);
+ void cancelResourceCache(CacheID id);
- void updateResourceCache(PrimitiveResourcePtr pResource) const;
- void updateResourceCache(CacheID id) const;
+ void updateResourceCache(PrimitiveResourcePtr pResource) const;
+ void updateResourceCache(CacheID id) const;
- const ResourceAttributes getCachedData(PrimitiveResourcePtr pResource) const;
- const ResourceAttributes getCachedData(CacheID id) const;
+ const ResourceAttributes getCachedData(PrimitiveResourcePtr pResource) const;
+ const ResourceAttributes getCachedData(CacheID id) const;
- CACHE_STATE getResourceCacheState(PrimitiveResourcePtr pResource) const;
- CACHE_STATE getResourceCacheState(CacheID id) const;
+ CACHE_STATE getResourceCacheState(PrimitiveResourcePtr pResource) const;
+ CACHE_STATE getResourceCacheState(CacheID id) const;
- private:
- static ResourceCacheManager * s_instance;
- static std::mutex s_mutexForCreation;
- static std::unique_ptr<std::list<DataCachePtr>> s_cacheDataList;
- std::map<CacheID, DataCachePtr> cacheIDmap;
+ private:
+ static ResourceCacheManager *s_instance;
+ static std::mutex s_mutexForCreation;
+ static std::unique_ptr<std::list<DataCachePtr>> s_cacheDataList;
+ std::map<CacheID, DataCachePtr> cacheIDmap;
- ResourceCacheManager() = default;
- ~ResourceCacheManager();
- ResourceCacheManager(const ResourceCacheManager&) = delete;
- ResourceCacheManager(ResourceCacheManager&&) = delete;
+ ResourceCacheManager() = default;
+ ~ResourceCacheManager();
+ ResourceCacheManager(const ResourceCacheManager &) = delete;
+ ResourceCacheManager(ResourceCacheManager &&) = delete;
- ResourceCacheManager& operator=(const ResourceCacheManager&) const = delete;
- ResourceCacheManager& operator=(ResourceCacheManager&&) const = delete;
+ ResourceCacheManager &operator=(const ResourceCacheManager &) const = delete;
+ ResourceCacheManager &operator=(ResourceCacheManager && ) const = delete;
- static void initializeResourceCacheManager();
- DataCachePtr findDataCache(PrimitiveResourcePtr pResource) const;
- DataCachePtr findDataCache(CacheID id) const;
+ static void initializeResourceCacheManager();
+ DataCachePtr findDataCache(PrimitiveResourcePtr pResource) const;
+ DataCachePtr findDataCache(CacheID id) const;
};
} // namespace Service
} // namespace OIC
pollingHandle = 0;
pObserveCB = (ObserveCB)(std::bind(&DataCache::onObserve, this,
- std::placeholders::_1, std::placeholders::_2,
- std::placeholders::_3, std::placeholders::_4));
+ std::placeholders::_1, std::placeholders::_2,
+ std::placeholders::_3, std::placeholders::_4));
pGetCB = (GetCB)(std::bind(&DataCache::onGet, this,
- std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
+ std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
pTimerCB = (TimerCB)(std::bind(&DataCache::onTimeOut, this, std::placeholders::_1));
pPollingCB = (TimerCB)(std::bind(&DataCache::onPollingOut, this, std::placeholders::_1));
state = CACHE_STATE::DESTROYED;
sResource->cancelObserve();
- if(subscriberList != nullptr)
+ if (subscriberList != nullptr)
{
subscriberList->clear();
subscriberList.release();
sResource = pResource;
sResource->requestGet(pGetCB);
- if(sResource->isObservable())
+ if (sResource->isObservable())
{
sResource->requestObserve(pObserveCB);
}
newItem.reportID = generateCacheID();
- if(subscriberList != nullptr)
+ if (subscriberList != nullptr)
{
subscriberList->insert(
- std::make_pair(newItem.reportID, std::make_pair(newItem, func)));
+ std::make_pair(newItem.reportID, std::make_pair(newItem, func)));
}
return newItem.reportID;
CacheID ret = 0;
SubscriberInfoPair pair = findSubscriber(id);
- if(pair.first != 0)
+ if (pair.first != 0)
{
ret = pair.first;
subscriberList->erase(pair.first);
{
SubscriberInfoPair ret;
- for(auto & i : *subscriberList)
+ for (auto &i : *subscriberList)
{
- if(i.first == id)
+ if (i.first == id)
{
ret = std::make_pair(i.first, std::make_pair((Report_Info)i.second.first,
- (CacheCB)i.second.second));
+ (CacheCB)i.second.second));
break;
}
}
const PrimitiveResourcePtr DataCache::getPrimitiveResource() const
{
- return (sResource!=nullptr)?sResource:nullptr;
+ return (sResource != nullptr) ? sResource : nullptr;
}
const ResourceAttributes DataCache::getCachedData() const
{
- if(state != CACHE_STATE::READY || attributes.empty())
+ if (state != CACHE_STATE::READY || attributes.empty())
{
return ResourceAttributes();
}
}
void DataCache::onObserve(
- const HeaderOptions& _hos, const ResponseStatement& _rep, int _result, int _seq)
+ const HeaderOptions &_hos, const ResponseStatement &_rep, int _result, int _seq)
{
- if(_result != OC_STACK_OK || _rep.getAttributes().empty())
+ if (_result != OC_STACK_OK || _rep.getAttributes().empty())
{
return;
}
- if(state != CACHE_STATE::READY)
+ if (state != CACHE_STATE::READY)
{
state = CACHE_STATE::READY;
}
notifyObservers(_rep.getAttributes());
}
- void DataCache::onGet(const HeaderOptions& _hos,
- const ResponseStatement& _rep, int _result)
+ void DataCache::onGet(const HeaderOptions &_hos,
+ const ResponseStatement &_rep, int _result)
{
- if(_result != OC_STACK_OK || _rep.getAttributes().empty())
+ if (_result != OC_STACK_OK || _rep.getAttributes().empty())
{
return;
}
- if(state != CACHE_STATE::READY)
+ if (state != CACHE_STATE::READY)
{
state = CACHE_STATE::READY;
}
- if(!sResource->isObservable())
+ if (!sResource->isObservable())
{
networkTimer.cancelTimer(networkTimeOutHandle);
networkTimeOutHandle = networkTimer.postTimer(
- CACHE_DEFAULT_EXPIRED_MILLITIME, pTimerCB);
+ CACHE_DEFAULT_EXPIRED_MILLITIME, pTimerCB);
pollingHandle = pollingTimer.postTimer(CACHE_DEFAULT_REPORT_MILLITIME, pPollingCB);
}
void DataCache::notifyObservers(ResourceAttributes Att)
{
- if(attributes == Att)
+ if (attributes == Att)
{
return;
}
attributes = Att;
ResourceAttributes retAtt = Att;
- for(auto & i : * subscriberList)
+ for (auto &i : * subscriberList)
{
- if(i.second.first.rf == REPORT_FREQUENCY::UPTODATE)
+ if (i.second.first.rf == REPORT_FREQUENCY::UPTODATE)
{
i.second.second(this->sResource, retAtt);
}
return state;
}
- void *DataCache::onTimeOut(unsigned int timerID)
+ void DataCache::onTimeOut(unsigned int timerID)
{
state = CACHE_STATE::LOST_SIGNAL;
- return NULL;
+ return;
}
- void *DataCache::onPollingOut(const unsigned int timerID)
+ void DataCache::onPollingOut(const unsigned int timerID)
{
- if(sResource != nullptr)
+ if (sResource != nullptr)
{
sResource->requestGet(pGetCB);
}
- return NULL;
+ return;
}
CacheID DataCache::generateCacheID()
CacheID retID = 0;
srand(time(NULL));
- while(1)
+ while (1)
{
- if(findSubscriber(retID).first == 0 && retID != 0)
+ if (findSubscriber(retID).first == 0 && retID != 0)
{
break;
}
void DataCache::requestGet()
{
state = CACHE_STATE::UPDATING;
- if(sResource != nullptr)
+ if (sResource != nullptr)
{
sResource->requestGet(pGetCB);
}
bool DataCache::isEmptySubscriber() const
{
- return (subscriberList!=nullptr)?subscriberList->empty():true;
+ return (subscriberList != nullptr) ? subscriberList->empty() : true;
}
} // namespace Service
} // namespace OIC
{
namespace Service
{
- ResourceCacheManager * ResourceCacheManager::s_instance = NULL;
+ ResourceCacheManager *ResourceCacheManager::s_instance = NULL;
std::mutex ResourceCacheManager::s_mutexForCreation;
std::unique_ptr<std::list<DataCachePtr>> ResourceCacheManager::s_cacheDataList(nullptr);
ResourceCacheManager::~ResourceCacheManager()
{
- if(s_cacheDataList != nullptr)
+ if (s_cacheDataList != nullptr)
{
s_cacheDataList->clear();
}
}
- ResourceCacheManager * ResourceCacheManager::getInstance()
+ ResourceCacheManager *ResourceCacheManager::getInstance()
{
- if(s_instance == nullptr)
+ if (s_instance == nullptr)
{
s_mutexForCreation.lock();
- if(s_instance == nullptr)
+ if (s_instance == nullptr)
{
s_instance = new ResourceCacheManager();
s_instance->initializeResourceCacheManager();
}
CacheID ResourceCacheManager::requestResourceCache(
- PrimitiveResourcePtr pResource, CacheCB func,
- REPORT_FREQUENCY rf, long reportTime)
+ PrimitiveResourcePtr pResource, CacheCB func,
+ REPORT_FREQUENCY rf, long reportTime)
{
- if(pResource == nullptr)
+ if (pResource == nullptr)
{
- throw InvalidParameterException{"[requestResourceCache] Primitive Resource is invaild"};
+ throw InvalidParameterException {"[requestResourceCache] Primitive Resource is invaild"};
}
CacheID retID = 0;
- if(rf != REPORT_FREQUENCY::NONE)
+ if (rf != REPORT_FREQUENCY::NONE)
{
- if(func == NULL || func == nullptr)
+ if (func == NULL || func == nullptr)
{
- throw InvalidParameterException{"[requestResourceCache] CacheCB is invaild"};
+ throw InvalidParameterException {"[requestResourceCache] CacheCB is invaild"};
}
- if(!reportTime)
+ if (!reportTime)
{
// default setting
reportTime = CACHE_DEFAULT_REPORT_MILLITIME;
}
DataCachePtr newHandler = findDataCache(pResource);
- if(newHandler == nullptr)
+ if (newHandler == nullptr)
{
newHandler.reset(new DataCache());
newHandler->initializeDataCache(pResource);
void ResourceCacheManager::cancelResourceCache(CacheID id)
{
- if(id == 0 || cacheIDmap.find(id) == cacheIDmap.end())
+ if (id == 0 || cacheIDmap.find(id) == cacheIDmap.end())
{
- throw InvalidParameterException{"[cancelResourceCache] CacheID is invaild"};
+ throw InvalidParameterException {"[cancelResourceCache] CacheID is invaild"};
}
DataCachePtr foundCacheHandler = findDataCache(id);
- if(foundCacheHandler != nullptr)
+ if (foundCacheHandler != nullptr)
{
CacheID retID = foundCacheHandler->deleteSubscriber(id);
- if(retID == id)
+ if (retID == id)
{
cacheIDmap.erase(id);
}
- if(foundCacheHandler->isEmptySubscriber())
+ if (foundCacheHandler->isEmptySubscriber())
{
s_cacheDataList->remove(foundCacheHandler);
foundCacheHandler.reset();
void ResourceCacheManager::updateResourceCache(PrimitiveResourcePtr pResource) const
{
- if(pResource == nullptr)
+ if (pResource == nullptr)
{
throw InvalidParameterException
{"[updateResourceCache] Primitive Resource is invaild"};
}
DataCachePtr foundCache = findDataCache(pResource);
- if(foundCache == nullptr)
+ if (foundCache == nullptr)
{
throw InvalidParameterException
{"[updateResourceCache] Primitive Resource is invaild"};
}
void ResourceCacheManager::updateResourceCache(CacheID updateId) const
{
- if(updateId == 0)
+ if (updateId == 0)
{
- throw InvalidParameterException{"[getCachedData] CacheID is invaild"};
+ throw InvalidParameterException {"[getCachedData] CacheID is invaild"};
}
DataCachePtr foundCache = findDataCache(updateId);
- if(foundCache == nullptr)
+ if (foundCache == nullptr)
{
- throw InvalidParameterException{"[getCachedData] CacheID is invaild"};
+ throw InvalidParameterException {"[getCachedData] CacheID is invaild"};
}
foundCache->requestGet();
}
const ResourceAttributes ResourceCacheManager::getCachedData(
- PrimitiveResourcePtr pResource) const
+ PrimitiveResourcePtr pResource) const
{
- if(pResource == nullptr)
+ if (pResource == nullptr)
{
- throw InvalidParameterException{"[getCachedData] Primitive Resource is invaild"};
+ throw InvalidParameterException {"[getCachedData] Primitive Resource is invaild"};
}
DataCachePtr handler = findDataCache(pResource);
- if(handler == nullptr)
+ if (handler == nullptr)
{
- throw InvalidParameterException{"[getCachedData] Primitive Resource is invaild"};
+ throw InvalidParameterException {"[getCachedData] Primitive Resource is invaild"};
}
return handler->getCachedData();
}
const ResourceAttributes ResourceCacheManager::getCachedData(CacheID id) const
{
- if(id == 0)
+ if (id == 0)
{
- throw InvalidParameterException{"[getCachedData] CacheID is invaild"};
+ throw InvalidParameterException {"[getCachedData] CacheID is invaild"};
}
DataCachePtr handler = findDataCache(id);
- if(handler == nullptr)
+ if (handler == nullptr)
{
- throw InvalidParameterException{"[getCachedData] CacheID is invaild"};
+ throw InvalidParameterException {"[getCachedData] CacheID is invaild"};
}
return handler->getCachedData();
}
CACHE_STATE ResourceCacheManager::getResourceCacheState(
- PrimitiveResourcePtr pResource) const
+ PrimitiveResourcePtr pResource) const
{
- if(pResource == nullptr)
+ if (pResource == nullptr)
{
- throw InvalidParameterException{"[getResourceCacheState] Primitive Resource is invaild"};
+ throw InvalidParameterException {"[getResourceCacheState] Primitive Resource is invaild"};
}
DataCachePtr handler = findDataCache(pResource);
- if(handler == nullptr)
+ if (handler == nullptr)
{
return CACHE_STATE::NONE;
}
CACHE_STATE ResourceCacheManager::getResourceCacheState(CacheID id) const
{
- if(id == 0)
+ if (id == 0)
{
- throw InvalidParameterException{"[getResourceCacheState] CacheID is invaild"};
+ throw InvalidParameterException {"[getResourceCacheState] CacheID is invaild"};
}
DataCachePtr handler = findDataCache(id);
- if(handler == nullptr)
+ if (handler == nullptr)
{
return CACHE_STATE::NONE;
}
void ResourceCacheManager::initializeResourceCacheManager()
{
- if(s_cacheDataList == nullptr)
+ if (s_cacheDataList == nullptr)
{
s_cacheDataList
- = std::unique_ptr<std::list<DataCachePtr>>(new std::list<DataCachePtr>);
+ = std::unique_ptr<std::list<DataCachePtr>>(new std::list<DataCachePtr>);
}
}
DataCachePtr ResourceCacheManager::findDataCache(PrimitiveResourcePtr pResource) const
{
DataCachePtr retHandler = nullptr;
- for (auto & i : * s_cacheDataList)
+ for (auto &i : * s_cacheDataList)
{
- if(i->getPrimitiveResource()->getUri() == pResource->getUri() &&
- i->getPrimitiveResource()->getHost() == pResource->getHost())
+ if (i->getPrimitiveResource()->getUri() == pResource->getUri() &&
+ i->getPrimitiveResource()->getHost() == pResource->getHost())
{
retHandler = i;
break;
DataCachePtr ResourceCacheManager::findDataCache(CacheID id) const
{
DataCachePtr retHandler = nullptr;
- for(auto it : cacheIDmap)
+ for (auto it : cacheIDmap)
{
- if(it.first == id)
+ if (it.first == id)
{
retHandler = it.second;
break;
--- /dev/null
+#include <iostream>
+#include <gtest/gtest.h>
+#include <HippoMocks/hippomocks.h>
+
+#include "ResourceCacheManager.h"
+#include "DataCache.h"
+#include "ResourceAttributes.h"
+#include "ResponseStatement.h"
+
+using namespace OIC::Service;
+
+class DataCacheTest : public ::testing::Test
+{
+ public:
+ typedef std::function <
+ void(const OIC::Service::HeaderOptions &, const OIC::Service::ResponseStatement &,
+ int) > GetCallback;
+
+ typedef std::function <
+ void(const OIC::Service::HeaderOptions &, const OIC::Service::ResponseStatement &, int,
+ int) > ObserveCallback;
+ public:
+ MockRepository mocks;
+ DataCache *cacheHandler;
+ PrimitiveResource::Ptr pResource;
+ CacheCB cb;
+ CacheID id;
+
+ protected:
+ DataCacheTest()
+ {
+
+ }
+
+ virtual ~DataCacheTest() noexcept(true)
+ {
+
+ }
+
+ virtual void SetUp() override
+ {
+ pResource = PrimitiveResource::Ptr(mocks.Mock< PrimitiveResource >(), [](PrimitiveResource *) {});
+ cacheHandler = new DataCache();
+ cb = ([](std::shared_ptr<PrimitiveResource >, const ResourceAttributes &)->OCStackResult {return OC_STACK_OK;});
+ }
+
+ virtual void TearDown()
+ {
+ delete cacheHandler;
+ }
+};
+
+TEST_F(DataCacheTest, initializeDataCache_normalCase)
+{
+
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::requestGet);
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::isObservable).Return(true);
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::requestObserve);
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::cancelObserve);
+
+ cacheHandler->initializeDataCache(pResource);
+}
+
+TEST_F(DataCacheTest, initializeDataCache_normalCaseObservable)
+{
+
+ mocks.OnCall(pResource.get(), PrimitiveResource::requestGet).Do(
+ [](GetCallback callback)
+ {
+ OIC::Service::HeaderOptions hos;
+
+ OIC::Service::ResourceAttributes attr;
+ OIC::Service::ResponseStatement rep(attr);
+ callback(hos, rep, OC_STACK_OK);
+ return;
+ }
+ );
+ mocks.OnCall(pResource.get(), PrimitiveResource::isObservable).Return(true);
+ mocks.OnCall(pResource.get(), PrimitiveResource::requestObserve).Do(
+ [](ObserveCallback callback)
+ {
+ OIC::Service::HeaderOptions hos;
+ OIC::Service::ResourceAttributes attr;
+ OIC::Service::ResponseStatement rep(attr);
+ int seq;
+ callback(hos, rep, OC_STACK_OK, seq);
+ return;
+ }
+ );
+ mocks.OnCall(pResource.get(), PrimitiveResource::cancelObserve);
+
+ cacheHandler->initializeDataCache(pResource);
+}
+
+TEST_F(DataCacheTest, initializeDataCache_normalCaseNonObservable)
+{
+
+ mocks.OnCall(pResource.get(), PrimitiveResource::requestGet).Do(
+ [](GetCallback callback)
+ {
+ OIC::Service::HeaderOptions hos;
+
+ OIC::Service::ResourceAttributes attr;
+ OIC::Service::ResponseStatement rep(attr);
+ callback(hos, rep, OC_STACK_OK);
+ return;
+ }
+ );
+ mocks.OnCall(pResource.get(), PrimitiveResource::isObservable).Return(false);
+ mocks.OnCall(pResource.get(), PrimitiveResource::cancelObserve);
+
+ cacheHandler->initializeDataCache(pResource);
+
+ sleep(3);
+}
+
+TEST_F(DataCacheTest, initializeDataCache_normalCaseTimeOut)
+{
+
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::requestGet);
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::isObservable).Return(true);
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::requestObserve);
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::cancelObserve);
+
+ cacheHandler->initializeDataCache(pResource);
+
+ sleep(3);
+}
+
+TEST_F(DataCacheTest, addSubscriber_normalCase)
+{
+
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::requestGet);
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::isObservable).Return(true);
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::requestObserve);
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::cancelObserve);
+
+ cacheHandler->initializeDataCache(pResource);
+ REPORT_FREQUENCY rf = REPORT_FREQUENCY::UPTODATE;
+ long reportTime = 1l;
+
+ ASSERT_NE(cacheHandler->addSubscriber(cb, rf, reportTime), 0);
+}
+
+TEST_F(DataCacheTest, deleteSubsciber_normalCase)
+{
+
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::requestGet);
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::isObservable).Return(true);
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::requestObserve);
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::cancelObserve);
+
+ cacheHandler->initializeDataCache(pResource);
+
+ REPORT_FREQUENCY rf = REPORT_FREQUENCY::UPTODATE;
+ long reportTime = 1l;
+
+ id = cacheHandler->addSubscriber(cb, rf, reportTime);
+
+ ASSERT_NE(cacheHandler->deleteSubscriber(id), 0);
+}
+
+TEST_F(DataCacheTest, getCacheState_normalCase)
+{
+
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::requestGet);
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::isObservable).Return(true);
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::requestObserve);
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::cancelObserve);
+
+ cacheHandler->initializeDataCache(pResource);
+
+ ASSERT_EQ(cacheHandler->getCacheState(), CACHE_STATE::READY_YET);
+}
+
+TEST_F(DataCacheTest, getCachedData_normalCase)
+{
+
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::requestGet);
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::isObservable).Return(true);
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::requestObserve);
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::cancelObserve);
+
+ cacheHandler->initializeDataCache(pResource);
+
+ ASSERT_EQ(cacheHandler->getCachedData(), ResourceAttributes());
+}
+
+TEST_F(DataCacheTest, getPrimitiveResource_normalCase)
+{
+
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::requestGet);
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::isObservable).Return(true);
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::requestObserve);
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::cancelObserve);
+
+ cacheHandler->initializeDataCache(pResource);
+
+ ASSERT_EQ(cacheHandler->getPrimitiveResource(), pResource);
+}
+
+TEST_F(DataCacheTest, requestGet_normalCase)
+{
+
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::requestGet);
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::isObservable).Return(true);
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::requestObserve);
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::requestGet);
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::cancelObserve);
+
+ cacheHandler->initializeDataCache(pResource);
+
+ cacheHandler->requestGet();
+}
+
+TEST_F(DataCacheTest, isEmptySubscriber_normalCase)
+{
+
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::requestGet);
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::isObservable).Return(true);
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::requestObserve);
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::cancelObserve);
+
+ cacheHandler->initializeDataCache(pResource);
+
+ ASSERT_EQ(cacheHandler->isEmptySubscriber(), true);
+}
+
+TEST_F(DataCacheTest, requestGet_normalCasetest)
+{
+
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::requestGet);
+ mocks.OnCall(pResource.get(), PrimitiveResource::isObservable).Return(true);
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::requestObserve);
+ mocks.OnCall(pResource.get(), PrimitiveResource::requestGet).Do(
+ [](GetCallback callback)
+ {
+ std::cout << "HelloWorld" << std::endl;
+ OIC::Service::HeaderOptions hos;
+
+ OIC::Service::ResourceAttributes attr;
+ //attr = mocks.Mcok< OIC::Service::ResourceAttributes >();
+ OIC::Service::ResponseStatement rep(attr);
+ //rep = mocks.Mock< OIC::Service::ResponseStatement >(attr);
+ //fakeResource = mocks.Mock< FakeOCResource >();
+ //mocks.OnCallFunc(ResourceAttributes::empty()).Return(false);
+ callback(hos, rep, OC_STACK_OK);
+ return;
+ }
+ );
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::cancelObserve);
+
+ cacheHandler->initializeDataCache(pResource);
+
+ cacheHandler->requestGet();
+}
\ No newline at end of file
--- /dev/null
+#include <iostream>
+#include <gtest/gtest.h>
+#include <HippoMocks/hippomocks.h>
+
+#include "ResourceCacheManager.h"
+
+using namespace OIC::Service;
+
+
+
+class ResourceCacheManagerTest : public ::testing::Test
+{
+ public:
+ MockRepository mocks;
+ ResourceCacheManager *cacheInstance;
+ PrimitiveResource::Ptr pResource;
+ CacheCB cb;
+ CacheID id;
+
+ protected:
+ ResourceCacheManagerTest()
+ {
+
+ }
+
+ virtual ~ResourceCacheManagerTest() noexcept(true)
+ {
+
+ }
+
+ virtual void SetUp() override
+ {
+ cacheInstance = ResourceCacheManager::getInstance();
+ pResource = PrimitiveResource::Ptr(mocks.Mock< PrimitiveResource >(), [](PrimitiveResource *) {});
+ cb = ([](std::shared_ptr<PrimitiveResource >, const ResourceAttributes &)->OCStackResult {return OC_STACK_OK;});
+ }
+
+ virtual void TearDown()
+ {
+
+ }
+};
+
+TEST_F(ResourceCacheManagerTest, requestResourceCache_resourceIsNULL)
+{
+
+ CacheCB func = cb;
+ REPORT_FREQUENCY rf = REPORT_FREQUENCY::UPTODATE;
+ long reportTime = 20l;
+
+ ASSERT_THROW(cacheInstance->requestResourceCache(NULL, func, rf, reportTime),
+ ResourceCacheManager::InvalidParameterException);
+}
+
+TEST_F(ResourceCacheManagerTest, requestResourceCache_cacheCBIsNULL)
+{
+
+ CacheCB func = NULL;
+ REPORT_FREQUENCY rf = REPORT_FREQUENCY::UPTODATE;
+ long reportTime = 20l;
+
+ ASSERT_THROW(cacheInstance->requestResourceCache(pResource, func, rf, reportTime),
+ ResourceCacheManager::InvalidParameterException);
+}
+
+TEST_F(ResourceCacheManagerTest, requestResourceCache_reportTimeIsNULL)
+{
+
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::requestGet);
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::isObservable).Return(true);
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::requestObserve);
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::cancelObserve);
+
+ CacheCB func = cb;
+ REPORT_FREQUENCY rf = REPORT_FREQUENCY::UPTODATE;
+
+ id = cacheInstance->requestResourceCache(pResource, func, rf);
+ cacheInstance->cancelResourceCache(id);
+
+ ASSERT_NE(id, 0);
+
+}
+
+TEST_F(ResourceCacheManagerTest, requestResourceCache_normalCase)
+{
+
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::requestGet);
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::isObservable).Return(true);
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::requestObserve);
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::cancelObserve);
+
+ CacheCB func = cb;
+ REPORT_FREQUENCY rf = REPORT_FREQUENCY::UPTODATE;
+ long reportTime = 20l;
+
+ id = cacheInstance->requestResourceCache(pResource, func, rf, reportTime);
+ cacheInstance->cancelResourceCache(id);
+
+ ASSERT_NE(id, 0);
+}
+
+TEST_F(ResourceCacheManagerTest, cancelResourceCache_cacheIDIsZero)
+{
+
+ ASSERT_THROW(cacheInstance->cancelResourceCache(0),
+ ResourceCacheManager::InvalidParameterException);
+}
+
+TEST_F(ResourceCacheManagerTest, cancelResourceCache_normalCase)
+{
+
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::requestGet);
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::isObservable).Return(true);
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::requestObserve);
+ mocks.ExpectCall(pResource.get(), PrimitiveResource::cancelObserve);
+
+ CacheCB func = cb;
+ REPORT_FREQUENCY rf = REPORT_FREQUENCY::UPTODATE;
+ long reportTime = 20l;
+
+ id = cacheInstance->requestResourceCache(pResource, func, rf, reportTime);
+
+ cacheInstance->cancelResourceCache(id);
+}
+
+TEST_F(ResourceCacheManagerTest, updateResourceCachePrimitiveResource_resourceIsNULL)
+{
+
+ pResource = NULL;
+
+ ASSERT_THROW(cacheInstance->updateResourceCache(pResource),
+ ResourceCacheManager::InvalidParameterException);
+}
+
+TEST_F(ResourceCacheManagerTest, updateResourceCachePrimitiveResource_cacheIsNULL)
+{
+
+ ASSERT_THROW(cacheInstance->updateResourceCache(pResource),
+ ResourceCacheManager::InvalidParameterException);
+}
+
+TEST_F(ResourceCacheManagerTest, updateResourceCachePrimitiveResource_normalCase)
+{
+
+ mocks.OnCall(pResource.get(), PrimitiveResource::requestGet);
+ mocks.OnCall(pResource.get(), PrimitiveResource::isObservable).Return(true);
+ mocks.OnCall(pResource.get(), PrimitiveResource::requestObserve);
+ mocks.OnCall(pResource.get(), PrimitiveResource::getUri).Return("testUri");
+ mocks.OnCall(pResource.get(), PrimitiveResource::getHost).Return("testHost");
+ mocks.OnCall(pResource.get(), PrimitiveResource::cancelObserve);
+
+ CacheCB func = cb;
+ REPORT_FREQUENCY rf = REPORT_FREQUENCY::UPTODATE;
+ long reportTime = 20l;
+
+ id = cacheInstance->requestResourceCache(pResource, func, rf, reportTime);
+
+ cacheInstance->updateResourceCache(pResource);
+
+ cacheInstance->cancelResourceCache(id);
+}
+
+TEST_F(ResourceCacheManagerTest, updateResourceCacheCacheID_cacheIDIsZero)
+{
+
+ ASSERT_THROW(cacheInstance->updateResourceCache(0),
+ ResourceCacheManager::InvalidParameterException);
+}
+
+TEST_F(ResourceCacheManagerTest, updateResourceCacheCacheID_cacheIsNULL)
+{
+
+ ASSERT_THROW(cacheInstance->updateResourceCache(id),
+ ResourceCacheManager::InvalidParameterException);
+}
+
+TEST_F(ResourceCacheManagerTest, updateResourceCacheCacheID_normalCase)
+{
+
+ mocks.OnCall(pResource.get(), PrimitiveResource::requestGet);
+ mocks.OnCall(pResource.get(), PrimitiveResource::isObservable).Return(true);
+ mocks.OnCall(pResource.get(), PrimitiveResource::requestObserve);
+ mocks.OnCall(pResource.get(), PrimitiveResource::getUri).Return("testUri");
+ mocks.OnCall(pResource.get(), PrimitiveResource::getHost).Return("testHost");
+ mocks.OnCall(pResource.get(), PrimitiveResource::cancelObserve);
+
+ CacheCB func = cb;
+ REPORT_FREQUENCY rf = REPORT_FREQUENCY::UPTODATE;
+ long reportTime = 20l;
+
+ id = cacheInstance->requestResourceCache(pResource, func, rf, reportTime);
+
+ cacheInstance->updateResourceCache(id);
+
+ cacheInstance->cancelResourceCache(id);
+}
+
+TEST_F(ResourceCacheManagerTest, getCachedDataPrimitiveResource_resourceIsNULL)
+{
+
+ pResource = NULL;
+
+ ASSERT_THROW(cacheInstance->getCachedData(pResource),
+ ResourceCacheManager::InvalidParameterException);
+}
+
+TEST_F(ResourceCacheManagerTest, getCachedDataPrimitiveResource_handlerIsNULL)
+{
+
+ ASSERT_THROW(cacheInstance->getCachedData(pResource),
+ ResourceCacheManager::InvalidParameterException);
+}
+
+TEST_F(ResourceCacheManagerTest, getCachedDataCachID_resourceIsNULL)
+{
+
+ ASSERT_THROW(cacheInstance->getCachedData(0), ResourceCacheManager::InvalidParameterException);
+}
+
+TEST_F(ResourceCacheManagerTest, getCachedDataCachID_handlerIsNULL)
+{
+
+ ASSERT_THROW(cacheInstance->getCachedData(id), ResourceCacheManager::InvalidParameterException);
+}
+
+TEST_F(ResourceCacheManagerTest, getResourceCacheStatePrimitiveResource_resourceIsNULL)
+{
+
+ pResource = NULL;
+
+ ASSERT_THROW(cacheInstance->getResourceCacheState(pResource),
+ ResourceCacheManager::InvalidParameterException);
+}
+
+TEST_F(ResourceCacheManagerTest, getResourceCacheStatePrimitiveResource_handlerIsNULL)
+{
+
+ ASSERT_EQ(cacheInstance->getResourceCacheState(pResource), CACHE_STATE::NONE);
+}
+
+TEST_F(ResourceCacheManagerTest, getResourceCacheStatePrimitiveResource_normalCase)
+{
+
+ mocks.OnCall(pResource.get(), PrimitiveResource::requestGet);
+ mocks.OnCall(pResource.get(), PrimitiveResource::isObservable).Return(true);
+ mocks.OnCall(pResource.get(), PrimitiveResource::requestObserve);
+ mocks.OnCall(pResource.get(), PrimitiveResource::getUri).Return("testUri");
+ mocks.OnCall(pResource.get(), PrimitiveResource::getHost).Return("testHost");
+ mocks.OnCall(pResource.get(), PrimitiveResource::cancelObserve);
+
+ CacheCB func = cb;
+ REPORT_FREQUENCY rf = REPORT_FREQUENCY::UPTODATE;
+ long reportTime = 20l;
+
+ id = cacheInstance->requestResourceCache(pResource, func, rf, reportTime);
+ CACHE_STATE state = cacheInstance->getResourceCacheState(pResource);
+
+ cacheInstance->cancelResourceCache(id);
+
+ ASSERT_EQ(state, CACHE_STATE::READY_YET);
+}
+
+TEST_F(ResourceCacheManagerTest, getResourceCacheStateCacheID_cacheIDIsZero)
+{
+
+ ASSERT_THROW(cacheInstance->getResourceCacheState(0),
+ ResourceCacheManager::InvalidParameterException);
+}
+
+TEST_F(ResourceCacheManagerTest, getResourceCacheStateCacheID_handlerIsNULL)
+{
+
+ ASSERT_EQ(cacheInstance->getResourceCacheState(id), CACHE_STATE::NONE);
+}
+
+TEST_F(ResourceCacheManagerTest, getResourceCacheStateCacheID_normalCase)
+{
+
+ mocks.OnCall(pResource.get(), PrimitiveResource::requestGet);
+ mocks.OnCall(pResource.get(), PrimitiveResource::isObservable).Return(true);
+ mocks.OnCall(pResource.get(), PrimitiveResource::requestObserve);
+ mocks.OnCall(pResource.get(), PrimitiveResource::getUri).Return("testUri");
+ mocks.OnCall(pResource.get(), PrimitiveResource::getHost).Return("testHost");
+ mocks.OnCall(pResource.get(), PrimitiveResource::cancelObserve);
+
+ CacheCB func = cb;
+ REPORT_FREQUENCY rf = REPORT_FREQUENCY::UPTODATE;
+ long reportTime = 20l;
+
+ id = cacheInstance->requestResourceCache(pResource, func, rf, reportTime);
+ CACHE_STATE state = cacheInstance->getResourceCacheState(id);
+
+ cacheInstance->cancelResourceCache(id);
+
+ ASSERT_EQ(state, CACHE_STATE::READY_YET);
+}
\ No newline at end of file
--- /dev/null
+#******************************************************************
+#
+# 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.
+#
+#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+##
+# ResourceCache Unit Test build script
+##
+
+Import('env')
+
+if env.get('RELEASE'):
+ env.AppendUnique(CCFLAGS = ['-Os'])
+ env.AppendUnique(CPPDEFINES = ['NDEBUG'])
+else:
+ env.AppendUnique(CCFLAGS = ['-g'])
+
+if env.get('LOGGING'):
+ env.AppendUnique(CPPDEFINES = ['TB_LOG'])
+
+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'
+######################################################################
+
+cache_test_env = lib_env.Clone()
+
+target_os = env.get('TARGET_OS')
+
+######################################################################
+# Build flags
+######################################################################
+gtest = File(gtest_dir + '/lib/.libs/libgtest.a')
+gtest_main = File(gtest_dir + '/lib/.libs/libgtest_main.a')
+
+cache_test_env.AppendUnique(CPPPATH = ['../include'])
+cache_test_env.AppendUnique(CPPPATH = ['../../../include'])
+cache_test_env.AppendUnique(CPPPATH = ['../../common/primitiveResource/include'])
+cache_test_env.AppendUnique(CPPPATH = ['../../common/expiryTimer/include'])
+cache_test_env.AppendUnique(CPPPATH = ['../../common/expiryTimer/src'])
+cache_test_env.PrependUnique(CPPPATH = [env.get('SRC_DIR')+'/extlibs/hippomocks-master',
+ gtest_dir + '/include'])
+cache_test_env.AppendUnique(LIBPATH = [env.get('BUILD_DIR')])
+cache_test_env.PrependUnique(LIBS = ['ResourceCache', 'service_common', 'ExpiryTimer', 'oc', 'octbstack', 'oc_logger', 'connectivity_abstraction', 'coap', gtest, gtest_main])
+
+
+if target_os not in ['windows', 'winrt']:
+ cache_test_env.AppendUnique(CXXFLAGS = ['-O2', '-g', '-Wall', '-fmessage-length=0', '-std=c++0x'])
+ cache_test_env.AppendUnique(CCFLAGS = ['-Wnoexcept'])
+
+if target_os == 'linux':
+ cache_test_env.AppendUnique(LIBS = ['pthread'])
+
+######################################################################
+# Build Test
+######################################################################
+
+
+cache_test_src = env.Glob('./*.cpp')
+
+cache_test = cache_test_env.Program('cache_test', cache_test_src)
+Alias("cache_test", cache_test)
+env.AppendTarget('cache_test')