Added get/set apis with query params in RCSRemoteResourceObject.
authorcoderhyme <jhyo.kim@samsung.com>
Mon, 1 Feb 2016 09:03:14 +0000 (01:03 -0800)
committerUze Choi <uzchoi@samsung.com>
Wed, 3 Feb 2016 08:29:09 +0000 (08:29 +0000)
These apis work with callbacks taking the RCSRepresentation.
An operation retrieving the interface of a request added in the RCSRequest.
An operation to create the RCSRemoteResourceObject from the OCResource added.

Change-Id: I630727e61068326c262dc0fe4ae6c19f324adcc5
Signed-off-by: coderhyme <jhyo.kim@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/4905
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
service/resource-encapsulation/include/RCSRemoteResourceObject.h
service/resource-encapsulation/include/RCSRequest.h
service/resource-encapsulation/src/common/primitiveResource/include/PrimitiveResource.h
service/resource-encapsulation/src/common/primitiveResource/include/PrimitiveResourceImpl.h
service/resource-encapsulation/src/common/primitiveResource/unittests/PrimitiveResourceTest.cpp
service/resource-encapsulation/src/resourceClient/RCSRemoteResourceObject.cpp
service/resource-encapsulation/src/serverBuilder/src/RCSRequest.cpp
service/resource-encapsulation/src/serverBuilder/src/RCSResourceObject.cpp
service/resource-encapsulation/unittests/ResourceClientTest.cpp

index bd66a2f..5960766 100644 (file)
 
 #include "RCSResourceAttributes.h"
 
+namespace OC
+{
+    class OCResource;
+
+    namespace HeaderOption
+    {
+        class OCHeaderOption;
+    }
+}
+
 namespace OIC
 {
     namespace Service
     {
+
+        class RCSRepresentation;
+
+        typedef std::vector< OC::HeaderOption::OCHeaderOption > HeaderOpts;
+
         /**
          * The states of caching.
          *
@@ -68,6 +83,36 @@ namespace OIC
 
         class PrimitiveResource;
 
+        class RCSQueryParams
+        {
+        public:
+            typedef std::unordered_map< std::string, std::string > Map;
+
+        public:
+            RCSQueryParams& setResourceInterface(const std::string&);
+            RCSQueryParams& setResourceInterface(std::string&&);
+
+            RCSQueryParams& setResuorceType(const std::string&);
+            RCSQueryParams& setResuorceType(std::string&&);
+
+            RCSQueryParams& put(const std::string&, const std::string&);
+            RCSQueryParams& put(std::string&&, std::string&&);
+            RCSQueryParams& put(const std::string&, std::string&&);
+            RCSQueryParams& put(std::string&&, const std::string&);
+
+            std::string getResourceInterface() const;
+            std::string getResourceType() const;
+            std::string get(const std::string&) const;
+
+            const Map& getAll() const;
+
+        private:
+            std::string m_resourceInterface;
+            std::string m_resourceType;
+
+            std::unordered_map< std::string, std::string > m_map;
+        };
+
         /**
          *
          * This represents a remote resource and provides simple ways to interact with it.
@@ -111,6 +156,9 @@ namespace OIC
             typedef std::function< void(const RCSResourceAttributes& attrs, int eCode) >
                 RemoteAttributesGetCallback;
 
+            typedef std::function< void(const HeaderOpts&, const RCSRepresentation& rep, int eCode) >
+                GetCallback;
+
             /**
              * Callback definition to be invoked when the response of setRemoteAttributes is
              * received.
@@ -123,6 +171,9 @@ namespace OIC
             typedef std::function< void(const RCSResourceAttributes&, int) >
                 RemoteAttributesSetCallback;
 
+            typedef std::function< void(const HeaderOpts&, const RCSRepresentation& rep, int eCode) >
+                SetCallback;
+
         private:
             typedef int CacheID;
             typedef unsigned int BrokerID;
@@ -134,6 +185,8 @@ namespace OIC
 
             ~RCSRemoteResourceObject();
 
+            static RCSRemoteResourceObject::Ptr fromOCResource(std::shared_ptr< OC::OCResource >);
+
             /**
              * Returns whether monitoring is enabled.
              *
@@ -309,6 +362,9 @@ namespace OIC
              */
             void getRemoteAttributes(RemoteAttributesGetCallback cb);
 
+            void get(GetCallback cb);
+            void get(const RCSQueryParams&, GetCallback cb);
+
             /**
              * Sends a set request with resource attributes to the server.
              *
@@ -328,6 +384,9 @@ namespace OIC
             void setRemoteAttributes(const RCSResourceAttributes& attributes,
                     RemoteAttributesSetCallback cb);
 
+            void set(const RCSResourceAttributes& attributes, SetCallback cb);
+            void set(const RCSQueryParams&, const RCSResourceAttributes& attributes,  SetCallback cb);
+
             /**
              * Returns the uri of the resource.
              *
index d711778..0b45032 100644 (file)
@@ -27,6 +27,7 @@
 #define SERVERBUILDER_PRIMITIVEREQUEST_H
 
 #include <memory>
+#include <map>
 
 namespace OC
 {
@@ -63,6 +64,10 @@ namespace OIC
 
                 const std::shared_ptr< OC::OCResourceRequest >& getOCRequest() const;
 
+                const std::map< std::string, std::string >& getQueryParams() const;
+
+                std::string getInterface() const;
+
             private:
                 const std::shared_ptr< OC::OCResourceRequest > m_ocRequest;
         };
index 5afdc82..e55e4cc 100644 (file)
@@ -71,6 +71,12 @@ namespace OIC
                     const OC::QueryParamsMap& queryParametersMap, GetCallback) = 0;
 
             virtual void requestSet(const RCSResourceAttributes&, SetCallback) = 0;
+
+            virtual void requestSetWith(const std::string& resourceType,
+                    const std::string& resourceInterface,
+                    const OC::QueryParamsMap& queryParametersMap,
+                    const RCSResourceAttributes&, GetCallback) = 0;
+
             virtual void requestPut(const RCSResourceAttributes&, PutCallback) = 0;
             virtual void requestObserve(ObserveCallback) = 0;
             virtual void cancelObserve() = 0;
index 7a9543a..5d741ef 100644 (file)
@@ -111,19 +111,28 @@ namespace OIC
 
             void requestSet(const RCSResourceAttributes& attrs, SetCallback callback)
             {
+                requestSetWith("", "", {}, attrs, std::move(callback));
+            }
+
+            void requestSetWith(const std::string& resourceType,
+                          const std::string& resourceInterface,
+                          const OC::QueryParamsMap& queryParametersMap,
+                          const RCSResourceAttributes& attrs, GetCallback callback)
+            {
                 using namespace std::placeholders;
 
-                typedef OCStackResult(BaseResource::*PostFunc)(
-                        const OC::OCRepresentation&,
-                        const OC::QueryParamsMap&, OC::PostCallback);
+                typedef OCStackResult (BaseResource::*PostFunc)(const std::string&,
+                        const std::string&, const OC::OCRepresentation&, const OC::QueryParamsMap&,
+                        OC::GetCallback);
 
                 invokeOC(m_baseResource, static_cast< PostFunc >(&BaseResource::post),
-                        ResourceAttributesConverter::toOCRepresentation(attrs),
-                        OC::QueryParamsMap{ },
-                        std::bind(safeCallback< SetCallback >, WeakFromThis(),
-                                std::move(callback), _1, _2, _3));
+                        resourceType, resourceInterface,
+                        ResourceAttributesConverter::toOCRepresentation(attrs), queryParametersMap,
+                        std::bind(safeCallback< SetCallback >, WeakFromThis(), std::move(callback),
+                                _1, _2, _3));
             }
 
+
             void requestPut(const RCSResourceAttributes& attrs, PutCallback callback)
             {
                 using namespace std::placeholders;
index a634ddd..b1aa1a0 100644 (file)
@@ -41,7 +41,7 @@ public:
     virtual OCStackResult put(
             const OC::OCRepresentation&, const OC::QueryParamsMap&, OC::PutCallback) = 0;
 
-    virtual OCStackResult post(
+    virtual OCStackResult post(const std::string&, const std::string&,
             const OC::OCRepresentation&, const OC::QueryParamsMap&, OC::PostCallback) = 0;
 
     virtual OCStackResult observe(
@@ -111,7 +111,8 @@ TEST_F(PrimitiveResourceTest, RequestSetPassResourceAttributesToOCResourcePost)
     RCSResourceAttributes attrs;
 
     mocks.ExpectCall(fakeResource, FakeOCResource::post).Match(
-            [](const OC::OCRepresentation& ocRep, const OC::QueryParamsMap&, OC::PutCallback)
+            [](const std::string&, const std::string&, const OC::OCRepresentation& ocRep,
+                    const OC::QueryParamsMap&, OC::PutCallback)
             {
                 return ocRep.getValue<int>(KEY) == value;
             }
index 33bf8bd..e9a7908 100644 (file)
@@ -99,7 +99,7 @@ namespace
         return OC_STACK_OK;
     }
 
-    void setCallback(const HeaderOptions&, const ResponseStatement& response, int eCode,
+    void setRemoteAttributesCb(const HeaderOptions&, const ResponseStatement& response, int eCode,
             RCSRemoteResourceObject::RemoteAttributesSetCallback onRemoteAttributesSet)
     {
         SCOPE_LOG_F(DEBUG, TAG);
@@ -107,7 +107,7 @@ namespace
         onRemoteAttributesSet(response.getAttributes(), eCode);
     }
 
-    void getCallback(const HeaderOptions&, const ResponseStatement& response, int eCode,
+    void getRemoteAttributesCb(const HeaderOptions&, const ResponseStatement& response, int eCode,
             RCSRemoteResourceObject::RemoteAttributesGetCallback onRemoteAttributesReceived)
     {
         SCOPE_LOG_F(DEBUG, TAG);
@@ -120,6 +120,76 @@ namespace OIC
 {
     namespace Service
     {
+
+        RCSQueryParams& RCSQueryParams::setResourceInterface(const std::string& resourceInterface)
+        {
+            m_resourceInterface = resourceInterface;
+            return *this;
+        }
+
+        RCSQueryParams& RCSQueryParams::setResourceInterface(std::string&& resourceInterface)
+        {
+            m_resourceInterface = std::move(resourceInterface);
+            return *this;
+        }
+
+        RCSQueryParams& RCSQueryParams::setResuorceType(const std::string& resourceType)
+        {
+            m_resourceType = resourceType;
+            return *this;
+        }
+
+        RCSQueryParams& RCSQueryParams::setResuorceType(std::string&& resourceType)
+        {
+            m_resourceType = std::move(resourceType);
+            return *this;
+        }
+
+        RCSQueryParams& RCSQueryParams::put(const std::string& key, const std::string& value)
+        {
+            m_map[key] = value;
+            return *this;
+        }
+
+        RCSQueryParams& RCSQueryParams::put(std::string&& key, std::string&& value)
+        {
+            m_map[std::move(key)] = std::move(value);
+            return *this;
+        }
+
+        RCSQueryParams& RCSQueryParams::put(const std::string& key, std::string&& value)
+        {
+            m_map[key] = std::move(value);
+            return *this;
+        }
+
+        RCSQueryParams& RCSQueryParams::put(std::string&& key, const std::string& value)
+        {
+            m_map[std::move(key)] = value;
+            return *this;
+        }
+
+        std::string RCSQueryParams::getResourceInterface() const
+        {
+            return m_resourceInterface;
+        }
+
+        std::string RCSQueryParams::getResourceType() const
+        {
+            return m_resourceType;
+        }
+
+        std::string RCSQueryParams::get(const std::string& key) const
+        {
+            return m_map.at(key);
+        }
+
+        const RCSQueryParams::Map& RCSQueryParams::getAll() const
+        {
+            return m_map;
+        }
+
+
         RCSRemoteResourceObject::RCSRemoteResourceObject(
                 std::shared_ptr< PrimitiveResource > pResource) :
                 m_primitiveResource{ pResource },
@@ -136,6 +206,18 @@ namespace OIC
             stopMonitoring();
         }
 
+        RCSRemoteResourceObject::Ptr RCSRemoteResourceObject::fromOCResource(
+                std::shared_ptr< OC::OCResource > ocResource)
+        {
+            if (!ocResource)
+            {
+                throw RCSInvalidParameterException("the oc resource must not be nullptr.");
+            }
+
+            return std::make_shared< RCSRemoteResourceObject >(
+                    PrimitiveResource::create(ocResource));
+        }
+
         bool RCSRemoteResourceObject::isMonitoring() const
         {
             return m_brokerId != 0;
@@ -320,10 +402,41 @@ namespace OIC
             }
 
             m_primitiveResource->requestGet(
-                    std::bind(getCallback, std::placeholders::_1, std::placeholders::_2,
+                    std::bind(getRemoteAttributesCb, std::placeholders::_1, std::placeholders::_2,
                             std::placeholders::_3, std::move(cb)));
         }
 
+        void RCSRemoteResourceObject::get(GetCallback cb)
+        {
+            SCOPE_LOG_F(DEBUG, TAG);
+
+            if (!cb)
+            {
+                throw RCSInvalidParameterException{ "get : Callback is empty" };
+            }
+
+            m_primitiveResource->requestGet(std::move(cb));
+        }
+
+        void RCSRemoteResourceObject::get(const RCSQueryParams& queryParams, GetCallback cb)
+        {
+            SCOPE_LOG_F(DEBUG, TAG);
+
+            if (!cb)
+            {
+                throw RCSInvalidParameterException{ "get : Callback is empty" };
+            }
+
+            const auto& paramMap = queryParams.getAll();
+
+            std::cout << queryParams.getResourceInterface() << "??\n";
+
+            m_primitiveResource->requestGetWith(
+                    queryParams.getResourceType(), queryParams.getResourceInterface(),
+                    OC::QueryParamsMap{ paramMap.begin(), paramMap.end() },
+                    std::move(cb));
+        }
+
         void RCSRemoteResourceObject::setRemoteAttributes(const RCSResourceAttributes& attribute,
                 RemoteAttributesSetCallback cb)
         {
@@ -335,8 +448,39 @@ namespace OIC
             }
 
             m_primitiveResource->requestSet(attribute,
-                    std::bind(setCallback, std::placeholders::_1, std::placeholders::_2,
+                    std::bind(setRemoteAttributesCb, std::placeholders::_1, std::placeholders::_2,
                             std::placeholders::_3, cb));
         }
+
+        void RCSRemoteResourceObject::set(const RCSResourceAttributes& attributes, SetCallback cb)
+        {
+            SCOPE_LOG_F(DEBUG, TAG);
+
+            if (!cb)
+            {
+                throw RCSInvalidParameterException{ "set : Callback is empty" };
+            }
+
+            m_primitiveResource->requestSet(attributes, std::move(cb));
+        }
+
+        void RCSRemoteResourceObject::set(const RCSQueryParams& queryParams,
+                const RCSResourceAttributes& attributes, SetCallback cb)
+        {
+            SCOPE_LOG_F(DEBUG, TAG);
+
+            if (!cb)
+            {
+                throw RCSInvalidParameterException{ "set : Callback is empty" };
+            }
+
+            const auto& paramMap = queryParams.getAll();
+
+            m_primitiveResource->requestSetWith(
+                    queryParams.getResourceType(), queryParams.getResourceInterface(),
+                    OC::QueryParamsMap{ paramMap.begin(), paramMap.end() }, attributes,
+                    std::move(cb));
+        }
+
     }
 }
index a220e71..b6d5dfc 100644 (file)
@@ -47,5 +47,21 @@ namespace OIC
         {
             return m_ocRequest;
         }
+
+        const std::map< std::string, std::string >& RCSRequest::getQueryParams() const
+        {
+            return m_ocRequest->getQueryParameters();
+        }
+
+        std::string RCSRequest::getInterface() const
+        {
+            const auto& params = m_ocRequest->getQueryParameters();
+
+            auto it = params.find(OC::Key::INTERFACESKEY);
+
+            if (it == params.end()) return "";
+
+            return it->second;
+        }
     }
 }
index 5a69cd0..5b4c8b7 100644 (file)
@@ -106,7 +106,7 @@ namespace
     {
         if (handler)
         {
-            return (*handler)(RCSRequest{ ocRequest->getResourceUri() }, attrs);
+            return (*handler)(RCSRequest{ ocRequest }, attrs);
         }
 
         return RESPONSE::defaultAction();
@@ -258,7 +258,6 @@ namespace OIC
                 invokeOCFunc(OC::OCPlatform::bindTypeToResource, handle, typeName);
             });
 
-
             server->m_resourceHandle = handle;
             server->m_interfaces = m_interfaces;
             server->m_types = m_types;
index a0e5274..2804930 100644 (file)
@@ -24,6 +24,7 @@
 #include "RCSDiscoveryManager.h"
 #include "RCSResourceObject.h"
 #include "RCSAddress.h"
+#include "RCSRequest.h"
 
 #include <condition_variable>
 #include <mutex>
@@ -163,6 +164,44 @@ TEST_F(RemoteResourceObjectTest, SetRemoteAttributesSetsAttributesOfServer)
     ASSERT_EQ(newValue, server->getAttributeValue(ATTR_KEY));
 }
 
+TEST_F(RemoteResourceObjectTest, QueryParamsForGetWillBePassedToBase)
+{
+    class CustomHandler
+    {
+    public:
+        virtual RCSGetResponse handle(const RCSRequest&, RCSResourceAttributes&) = 0;
+        virtual ~CustomHandler() {}
+    };
+
+    constexpr char PARAM_KEY[] { "aKey" };
+    constexpr char VALUE[] { "value" };
+
+    object->get(RCSQueryParams().setResourceInterface(RESOURCEINTERFACE).setResuorceType(RESOURCETYPE).
+            put(PARAM_KEY, VALUE),
+            [](const HeaderOpts&, const RCSRepresentation&, int){});
+
+    auto mockHandler = mocks.Mock< CustomHandler >();
+
+    mocks.ExpectCall(mockHandler, CustomHandler::handle).
+            Match([](const RCSRequest& request, RCSResourceAttributes&)
+            {
+                return request.getInterface() == RESOURCEINTERFACE &&
+                        request.getQueryParams().at(PARAM_KEY) == VALUE;
+            }
+    ).
+            Do([this](const RCSRequest&, RCSResourceAttributes&)
+            {
+                Proceed();
+                return RCSGetResponse::defaultAction();
+            }
+    );
+
+    server->setGetRequestHandler(std::bind(&CustomHandler::handle, mockHandler,
+            std::placeholders::_1, std::placeholders::_2));
+
+    Wait();
+}
+
 TEST_F(RemoteResourceObjectTest, MonitoringIsNotStartedByDefault)
 {
     ASSERT_FALSE(object->isMonitoring());