//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-#include <RCSResourceObject.h>
+#include "RCSResourceObject.h"
#include <string>
#include <functional>
#include <vector>
-#include <RequestHandler.h>
-#include <AssertUtils.h>
-#include <AtomicHelper.h>
-#include <ResourceAttributesConverter.h>
-#include <ResourceAttributesUtils.h>
+#include "RequestHandler.h"
+#include "AssertUtils.h"
+#include "AtomicHelper.h"
+#include "ResourceAttributesConverter.h"
+#include "ResourceAttributesUtils.h"
+#include "RCSRequest.h"
-#include <logger.h>
-#include <OCPlatform.h>
+#include "logger.h"
+#include "OCPlatform.h"
#define LOG_TAG "RCSResourceObject"
template <typename RESPONSE>
OCEntityHandlerResult sendResponse(RCSResourceObject& resource,
- std::shared_ptr< OC::OCResourceRequest > ocRequest, RESPONSE&& response)
+ const std::shared_ptr< OC::OCResourceRequest >& ocRequest, RESPONSE&& response)
{
auto ocResponse = response.getHandler()->buildResponse(resource);
ocResponse->setRequestHandle(ocRequest->getRequestHandle());
}
RCSResourceAttributes getAttributesFromOCRequest(
- std::shared_ptr< OC::OCResourceRequest > request)
+ const std::shared_ptr< OC::OCResourceRequest >& request)
{
return ResourceAttributesConverter::fromOCRepresentation(
request->getResourceRepresentation());
template< typename HANDLER, typename RESPONSE =
typename std::decay<HANDLER>::type::result_type >
RESPONSE invokeHandler(RCSResourceAttributes& attrs,
- std::shared_ptr< OC::OCResourceRequest > ocRequest, HANDLER&& handler)
+ const std::shared_ptr< OC::OCResourceRequest >& ocRequest,
+ std::shared_ptr< HANDLER > handler)
{
if (handler)
{
- return handler(RCSRequest{ ocRequest->getResourceUri() }, attrs);
+ return (*handler)(RCSRequest{ ocRequest->getResourceUri() }, attrs);
}
return RESPONSE::defaultAction();
typedef void (RCSResourceObject::* AutoNotifyFunc)
(bool, RCSResourceObject::AutoNotifyPolicy) const;
- std::function <void ()> createAutoNotifyInvoker(AutoNotifyFunc autoNotifyFunc,
+ std::function<void()> createAutoNotifyInvoker(AutoNotifyFunc autoNotifyFunc,
const RCSResourceObject& resourceObject, const RCSResourceAttributes& resourceAttributes,
RCSResourceObject::AutoNotifyPolicy autoNotifyPolicy)
{
void RCSResourceObject::setGetRequestHandler(GetRequestHandler h)
{
- m_getRequestHandler = std::move(h);
+ m_getRequestHandler = std::make_shared< GetRequestHandler >(std::move(h));
}
void RCSResourceObject::setSetRequestHandler(SetRequestHandler h)
{
- m_setRequestHandler = std::move(h);
+ m_setRequestHandler = std::make_shared< SetRequestHandler >(std::move(h));
}
void RCSResourceObject::notify() const
}
OCEntityHandlerResult RCSResourceObject::entityHandler(
- std::shared_ptr< OC::OCResourceRequest > request)
+ const std::shared_ptr< OC::OCResourceRequest >& request)
{
OC_LOG(WARNING, LOG_TAG, "entityHandler");
if (!request)
}
OCEntityHandlerResult RCSResourceObject::handleRequest(
- std::shared_ptr< OC::OCResourceRequest > request)
+ const std::shared_ptr< OC::OCResourceRequest >& request)
{
assert(request != nullptr);
}
OCEntityHandlerResult RCSResourceObject::handleRequestGet(
- std::shared_ptr< OC::OCResourceRequest > request)
+ const std::shared_ptr< OC::OCResourceRequest >& request)
{
assert(request != nullptr);
}
OCEntityHandlerResult RCSResourceObject::handleRequestSet(
- std::shared_ptr< OC::OCResourceRequest > request)
+ const std::shared_ptr< OC::OCResourceRequest >& request)
{
assert(request != nullptr);
}
OCEntityHandlerResult RCSResourceObject::handleObserve(
- std::shared_ptr< OC::OCResourceRequest >)
+ const std::shared_ptr< OC::OCResourceRequest >&)
{
if (!isObservable())
{
init();
}
- RCSResourceObject::LockGuard::~LockGuard()
+ RCSResourceObject::LockGuard::~LockGuard() noexcept(false)
{
- if (m_autoNotifyFunc) m_autoNotifyFunc();
+ if (!std::uncaught_exception() && m_autoNotifyFunc) m_autoNotifyFunc();
if (m_isOwningLock)
{
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-#include <UnitTestHelper.h>
+#include "UnitTestHelper.h"
-#include <RCSResourceObject.h>
+#include "RCSResourceObject.h"
-#include <OCPlatform.h>
+#include "OCPlatform.h"
using namespace std;
using namespace std::placeholders;
constexpr char RESOURCE_URI[]{ "a/test" };
constexpr char RESOURCE_TYPE[]{ "resourcetype" };
constexpr char KEY[]{ "key" };
-constexpr int value{ 100 };
+constexpr int VALUE{ 100 };
TEST(ResourceObjectBuilderCreateTest, ThrowIfUriIsInvalid)
{
{
RCSResourceObject::LockGuard lock{ server };
auto& attr = server->getAttributes();
- attr[KEY] = value;
+ attr[KEY] = VALUE;
}
- ASSERT_EQ(value, server->getAttribute<int>(KEY));
+ ASSERT_EQ(VALUE, server->getAttribute<int>(KEY));
}
TEST_F(ResourceObjectTest, ThrowIfTryToAccessAttributesWithoutGuard)
{
{
RCSResourceObject::LockGuard guard{ server };
- server->setAttribute(KEY, value);
+ server->setAttribute(KEY, VALUE);
}
- ASSERT_EQ(value, server->getAttribute<int>(KEY));
+ ASSERT_EQ(VALUE, server->getAttribute<int>(KEY));
}
TEST_F(ResourceObjectTest, SettingNestedAttributesIsSameToGettingNestedAttributes)
TEST_F(AutoNotifyTest, WithUpdatedPolicy_NeverBeNotifiedIfAttributeIsNotChanged)
{
server->setAutoNotifyPolicy(RCSResourceObject::AutoNotifyPolicy::UPDATED);
- server->setAttribute(KEY, value);
+ server->setAttribute(KEY, VALUE);
mocks.NeverCallFuncOverload(static_cast< NotifyAllObservers >(
OC::OCPlatform::notifyAllObservers));
- server->setAttribute(KEY, value);
+ server->setAttribute(KEY, VALUE);
}
TEST_F(AutoNotifyTest, WithUpdatedPolicy_WillBeNotifiedIfAttributeIsChanged)
{
server->setAutoNotifyPolicy(RCSResourceObject::AutoNotifyPolicy::UPDATED);
- server->setAttribute(KEY, value);
+ server->setAttribute(KEY, VALUE);
mocks.ExpectCallFuncOverload(static_cast< NotifyAllObservers >(
OC::OCPlatform::notifyAllObservers)).Return(OC_STACK_OK);
- server->setAttribute(KEY, value + 1);
+ server->setAttribute(KEY, VALUE + 1);
}
TEST_F(AutoNotifyTest, WithUpdatedPolicy_WillBeNotifiedIfValueIsAdded)
mocks.ExpectCallFuncOverload(static_cast< NotifyAllObservers >(
OC::OCPlatform::notifyAllObservers)).Return(OC_STACK_OK);
- server->setAttribute(newKey, value);
+ server->setAttribute(newKey, VALUE);
}
TEST_F(AutoNotifyTest, WithNeverPolicy_NeverBeNotifiedEvenIfAttributeIsChanged)
OC::OCPlatform::notifyAllObservers));
RCSResourceObject::LockGuard lock{ server };
- server->setAttribute(KEY, value);
+ server->setAttribute(KEY, VALUE);
}
TEST_F(AutoNotifyTest, WithUpdatePolicy_WillBeNotifiedIfAttributeIsDeleted)
{
server->setAutoNotifyPolicy(RCSResourceObject::AutoNotifyPolicy::UPDATED);
- server->setAttribute(KEY, value);
+ server->setAttribute(KEY, VALUE);
mocks.ExpectCallFuncOverload(static_cast< NotifyAllObservers >(
OC::OCPlatform::notifyAllObservers)).Return(OC_STACK_OK);
OC::OCPlatform::notifyAllObservers)).Return(OC_STACK_OK);
RCSResourceObject::LockGuard guard{ server };
- server->setAttribute(KEY, value);
+ server->setAttribute(KEY, VALUE);
}
TEST_F(AutoNotifyWithGuardTest, GuardCanOverridePolicy)
OC::OCPlatform::notifyAllObservers));
RCSResourceObject::LockGuard guard{ server, RCSResourceObject::AutoNotifyPolicy::NEVER };
- server->getAttributes()[KEY] = value;
+ server->getAttributes()[KEY] = VALUE;
}
TEST_F(AutoNotifyWithGuardTest, GuardInvokesNotifyWhenDestroyed)
{
RCSResourceObject::LockGuard guard{ server, RCSResourceObject::AutoNotifyPolicy::ALWAYS };
- server->setAttribute(KEY, value);
+ server->setAttribute(KEY, VALUE);
}
mocks.NeverCallFuncOverload(static_cast< NotifyAllObservers >(
OC::OCPlatform::notifyAllObservers)).Return(OC_STACK_OK);
- server->setAttribute(KEY, value);
+ server->setAttribute(KEY, VALUE);
}
TEST_F(ResourceObjectHandlingRequestTest, SendSetResponseWithCustomAttrs)
{
constexpr int errorCode{ 1999 };
- constexpr char value[]{ "value" };
+ constexpr char value[]{ "VALUE" };
server->setSetRequestHandler(
[](const RCSRequest&, RCSResourceAttributes&) -> RCSSetResponse
}
+
class SetRequestHandlerPolicyTest: public ResourceObjectHandlingRequestTest
{
public:
OCRepresentation createOCRepresentation()
{
OCRepresentation ocRep;
-
- vector<string> interface{"oic.if.baseline"};
- vector<string> type{"core.light"};
-
- ocRep.setUri(RESOURCE_URI);
- ocRep.setResourceInterfaces(interface);
- ocRep.setResourceTypes(type);
-
+ ocRep[KEY] = VALUE;
return ocRep;
}
server->getSetRequestHandlerPolicy());
}
-TEST_F(SetRequestHandlerPolicyTest, WithNeverPolicy_NotAddedIfReceivedNewKeyValuePair)
+TEST_F(SetRequestHandlerPolicyTest, WithNeverPolicy_DeniedIfKeyIsNew)
{
- OCRepresentation ocRep = createOCRepresentation();
- ocRep.setValue("NewKey", value);
server->setSetRequestHandlerPolicy(RCSResourceObject::SetRequestHandlerPolicy::NEVER);
- handler(createRequest(OC_REST_PUT, ocRep));
+ handler(createRequest(OC_REST_PUT, createOCRepresentation()));
RCSResourceObject::LockGuard guard{ server };
- ASSERT_FALSE((server->getAttributes()).contains("NewKey"));
+ ASSERT_FALSE(server->getAttributes().contains(KEY));
}
-TEST_F(SetRequestHandlerPolicyTest, WithAcceptancePolicy_WillBeAddedIfReceivedNewKeyValuePair)
+TEST_F(SetRequestHandlerPolicyTest, WithAcceptancePolicy_AcceptedEvenIfKeyIsNew)
{
- OCRepresentation ocRep = createOCRepresentation();
- ocRep.setValue("NewKey", value);
server->setSetRequestHandlerPolicy(RCSResourceObject::SetRequestHandlerPolicy::ACCEPTANCE);
- handler(createRequest(OC_REST_PUT, ocRep));
+ handler(createRequest(OC_REST_PUT, createOCRepresentation()));
RCSResourceObject::LockGuard guard{ server };
- ASSERT_TRUE((server->getAttributes()).contains("NewKey"));
+ ASSERT_TRUE(server->getAttributes().contains(KEY));
}
+
class ResourceObjectSynchronizationTest: public ResourceObjectHandlingRequestTest
{
public:
}
+
class AttributeUpdatedListenerTest: public ResourceObjectHandlingRequestTest
{
public:
OCRepresentation createOCRepresentation(void)
{
OCRepresentation ocRep;
-
- vector<string> interface{"oic.if.baseline"};
- vector<string> type{"core.light"};
-
- ocRep.setUri(RESOURCE_URI);
- ocRep.setResourceInterfaces(interface);
- ocRep.setResourceTypes(type);
- ocRep[KEY] = value;
-
+ ocRep[KEY] = VALUE;
return ocRep;
}
- void initMocks()
+protected:
+ void SetUp()
{
- ResourceObjectHandlingRequestTest::initMocks();
+ ResourceObjectHandlingRequestTest::SetUp();
mocks.OnCallFunc(OCPlatform::sendResponse).Return(OC_STACK_OK);
+
+ server->setAttribute(KEY, 0);
}
};
-class FunctionsForAttributeUpdatedListener
+class AttributeUpdatedListener
{
public:
- virtual void fCalled(const OIC::Service::RCSResourceAttributes::Value&,
- const OIC::Service::RCSResourceAttributes::Value&)=0;
- virtual void fNotCalled(const OIC::Service::RCSResourceAttributes::Value&,
+ virtual void onUpdated(const OIC::Service::RCSResourceAttributes::Value&,
const OIC::Service::RCSResourceAttributes::Value&)=0;
};
-TEST_F(AttributeUpdatedListenerTest, AddListenerRunsAddedFunction)
-{
- FunctionsForAttributeUpdatedListener *ptrMock =
- mocks.Mock<FunctionsForAttributeUpdatedListener>();
- server->setAttribute(KEY, 0);
+TEST_F(AttributeUpdatedListenerTest, RemoveListenerReturnsFalseIfListenerIsNotAdded)
+{
+ ASSERT_FALSE(server->removeAttributeUpdatedListener(KEY));
+}
- mocks.ExpectCall(ptrMock, FunctionsForAttributeUpdatedListener::fCalled);
+TEST_F(AttributeUpdatedListenerTest, RemoveListenerReturnsTrueIfListenerIsAdded)
+{
+ auto listener = mocks.Mock< AttributeUpdatedListener >();
server->addAttributeUpdatedListener(KEY,
- (std::bind(&FunctionsForAttributeUpdatedListener::fCalled, ptrMock, _1, _2)));
+ std::bind(&AttributeUpdatedListener::onUpdated, listener, _1, _2));
- handler(createRequest(OC_REST_PUT, createOCRepresentation()));
+ ASSERT_TRUE(server->removeAttributeUpdatedListener(KEY));
}
-TEST_F(AttributeUpdatedListenerTest, AddListenerRunsAccordingToLastAddedFunction)
+TEST_F(AttributeUpdatedListenerTest, AddListenerRunsAddedFunction)
{
- FunctionsForAttributeUpdatedListener *ptrMock =
- mocks.Mock<FunctionsForAttributeUpdatedListener>();
+ auto listener = mocks.Mock< AttributeUpdatedListener >();
- string duplicateKEY(KEY);
- server->setAttribute(KEY, 0);
-
- mocks.ExpectCall(ptrMock, FunctionsForAttributeUpdatedListener::fCalled);
- mocks.NeverCall(ptrMock, FunctionsForAttributeUpdatedListener::fNotCalled);
-
- server->addAttributeUpdatedListener(duplicateKEY,
- (std::bind(&FunctionsForAttributeUpdatedListener::fNotCalled, ptrMock, _1, _2)));
server->addAttributeUpdatedListener(KEY,
- (std::bind(&FunctionsForAttributeUpdatedListener::fCalled, ptrMock, _1, _2)));
+ std::bind(&AttributeUpdatedListener::onUpdated, listener, _1, _2));
+
+ mocks.ExpectCall(listener, AttributeUpdatedListener::onUpdated);
handler(createRequest(OC_REST_PUT, createOCRepresentation()));
}
-TEST_F(AttributeUpdatedListenerTest, RemoveListenerReturnsTrueIfListenerIsNotAdded)
+TEST_F(AttributeUpdatedListenerTest, ListenerWithSameKeyOverridesPreviousOne)
{
- ASSERT_FALSE(server->removeAttributeUpdatedListener(KEY));
-}
+ auto first = mocks.Mock< AttributeUpdatedListener >();
+ auto second = mocks.Mock< AttributeUpdatedListener >();
-TEST_F(AttributeUpdatedListenerTest, RemoveListenerReturnsTrueIfListenerIsAdded)
-{
- FunctionsForAttributeUpdatedListener *ptrMock =
- mocks.Mock<FunctionsForAttributeUpdatedListener>();
+ mocks.NeverCall(first, AttributeUpdatedListener::onUpdated);
+ mocks.ExpectCall(second, AttributeUpdatedListener::onUpdated);
server->addAttributeUpdatedListener(KEY,
- (std::bind(&FunctionsForAttributeUpdatedListener::fNotCalled, ptrMock, _1, _2)));
+ std::bind(&AttributeUpdatedListener::onUpdated, first, _1, _2));
+ server->addAttributeUpdatedListener(KEY,
+ std::bind(&AttributeUpdatedListener::onUpdated, second, _1, _2));
- ASSERT_TRUE(server->removeAttributeUpdatedListener(KEY));
+ handler(createRequest(OC_REST_PUT, createOCRepresentation()));
}
-TEST_F(AttributeUpdatedListenerTest, RemoveListenerNeverRunsRemovedFunc)
+TEST_F(AttributeUpdatedListenerTest, RemovedListenerNotBeInvoked)
{
- FunctionsForAttributeUpdatedListener *ptrMock =
- mocks.Mock<FunctionsForAttributeUpdatedListener>();
+ auto listener = mocks.Mock< AttributeUpdatedListener >();
+ server->addAttributeUpdatedListener(KEY,
+ std::bind(&AttributeUpdatedListener::onUpdated, listener, _1, _2));
- mocks.NeverCall(ptrMock, FunctionsForAttributeUpdatedListener::fNotCalled);
+ mocks.NeverCall(listener, AttributeUpdatedListener::onUpdated);
- server->setAttribute(KEY, 0);
- server->addAttributeUpdatedListener(KEY,
- (std::bind(&FunctionsForAttributeUpdatedListener::fNotCalled, ptrMock, _1, _2)));
server->removeAttributeUpdatedListener(KEY);
handler(createRequest(OC_REST_PUT, createOCRepresentation()));
}
-
-
-