Added API for setting representation in RCS Client of Resource Encapsulation.
authorAbhishek Pandey <abhi.siso@samsung.com>
Thu, 21 Jul 2016 08:57:52 +0000 (14:27 +0530)
committerAshok Babu Channa <ashok.channa@samsung.com>
Thu, 4 Aug 2016 05:01:18 +0000 (05:01 +0000)
RCSRemoteResourceObject of RE layer provides an API to get remote resource
representation using RCSRepresentation Object, but it is missing the API to
set RCSRepresentation.

Setting of RCSRepresentation is required for collection resources.

In this patch I have added the set API which accepts RCSRepresentation as input
parameter so it can be used to send POST request on collection resources where
payload contains attributes to be set on multiple child resources.

Signed-off-by: Abhishek Pandey <abhi.siso@samsung.com>
Change-Id: Ic8957296b3cfe3e6abe1218ef060614d9cdc5618
Reviewed-on: https://gerrit.iotivity.org/gerrit/9545
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Jay Sharma <jay.sharma@samsung.com>
Reviewed-by: Junghyun Oh <junghyun.oh@samsung.com>
Reviewed-by: Hun-je Yeon <hunje.yeon@samsung.com>
Reviewed-by: Ashok Babu Channa <ashok.channa@samsung.com>
service/resource-encapsulation/include/RCSRemoteResourceObject.h
service/resource-encapsulation/src/common/primitiveResource/include/PrimitiveResource.h
service/resource-encapsulation/src/common/primitiveResource/include/PrimitiveResourceImpl.h
service/resource-encapsulation/src/resourceClient/RCSRemoteResourceObject.cpp
service/resource-encapsulation/unittests/ResourceClientTest.cpp

index 5a27f0a..7611478 100644 (file)
@@ -30,6 +30,7 @@
 #include <vector>
 
 #include "RCSResourceAttributes.h"
+#include "RCSRepresentation.h"
 
 namespace OC
 {
@@ -507,6 +508,26 @@ namespace OIC
                     SetCallback cb);
 
             /**
+             * Sends a set request with resource representation to the server.
+             *
+             * The SetRequest behavior depends on query parameters and the server.
+             *
+             * @param queryParams Query parameters
+             * @param rep Representation to set
+             * @param cb A callback to receive the response.
+             *
+             * @throws PlatformException If the operation failed
+             * @throws InvalidParameterException If cb is an empty function or null.
+             *
+             * @see RCSResourceObject
+             * @see RCSResourceObject::SetRequestHandlerPolicy
+             *
+             * @note The callback will be invoked in an internal thread.
+             */
+            void set(const RCSQueryParams& queryParams, const RCSRepresentation &rep,
+                    SetCallback cb);
+
+            /**
              * Returns the uri of the resource.
              *
              */
index e55e4cc..8ba772b 100644 (file)
@@ -77,6 +77,11 @@ namespace OIC
                     const OC::QueryParamsMap& queryParametersMap,
                     const RCSResourceAttributes&, GetCallback) = 0;
 
+            virtual void requestSetWith(const std::string& resourceType,
+                    const std::string& resourceInterface,
+                    const OC::QueryParamsMap& queryParametersMap,
+                    const RCSRepresentation&, SetCallback) = 0;
+
             virtual void requestPut(const RCSResourceAttributes&, PutCallback) = 0;
             virtual void requestObserve(ObserveCallback) = 0;
             virtual void cancelObserve() = 0;
index 5d741ef..b531b34 100644 (file)
@@ -132,6 +132,23 @@ namespace OIC
                                 _1, _2, _3));
             }
 
+            void requestSetWith(const std::string& resourceType,
+                          const std::string& resourceInterface,
+                          const OC::QueryParamsMap& queryParametersMap,
+                          const RCSRepresentation & rep, SetCallback callback)
+            {
+                using namespace std::placeholders;
+
+                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),
+                        resourceType, resourceInterface,
+                        RCSRepresentation::toOCRepresentation(rep), queryParametersMap,
+                        std::bind(safeCallback< SetCallback >, WeakFromThis(), std::move(callback),
+                                _1, _2, _3));
+            }
 
             void requestPut(const RCSResourceAttributes& attrs, PutCallback callback)
             {
index 54e7ce8..b10cad6 100644 (file)
@@ -463,5 +463,23 @@ namespace OIC
                     std::move(cb));
         }
 
+        void RCSRemoteResourceObject::set(const RCSQueryParams& queryParams,
+                const RCSRepresentation& rep, 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() }, rep,
+                    std::move(cb));
+        }
+
     }
 }
index 171531b..5236eab 100644 (file)
@@ -43,6 +43,7 @@ constexpr int DEFAULT_WAITING_TIME_IN_MILLIS = 3000;
 
 void getRemoteAttributesCallback(const RCSResourceAttributes&, int) {}
 void setRemoteAttributesCallback(const RCSResourceAttributes&, int) {}
+void setRemoteRepresentationCallback(const HeaderOpts&, const RCSRepresentation&, int) {}
 void resourceStateChanged(ResourceState) { }
 void cacheUpdatedCallback(const RCSResourceAttributes&) {}
 
@@ -164,6 +165,32 @@ TEST_F(RemoteResourceObjectTest, SetRemoteAttributesSetsAttributesOfServer)
     ASSERT_EQ(newValue, server->getAttributeValue(ATTR_KEY));
 }
 
+TEST_F(RemoteResourceObjectTest, SetRemoteRepresentationDoesNotAllowEmptyFunction)
+{
+    RCSQueryParams queryParams;
+    RCSRepresentation rcsRep;
+    ASSERT_THROW(object->set(queryParams, rcsRep, {}), RCSInvalidParameterException);
+}
+
+TEST_F(RemoteResourceObjectTest, SetRemoteRepresentationSetsRepresentationOfServer)
+{
+    RCSRepresentation rcsRep;
+    RCSQueryParams queryParams;
+    constexpr int newValue = ATTR_VALUE + 1;
+    RCSResourceAttributes newAttrs;
+    newAttrs[ATTR_KEY] = newValue;
+
+    rcsRep.setAttributes(newAttrs);
+
+    mocks.ExpectCallFunc(setRemoteRepresentationCallback).
+            Do([this](const HeaderOpts&, const RCSRepresentation&, int){ Proceed(); });
+
+    object->set(queryParams, rcsRep, setRemoteRepresentationCallback);
+    Wait();
+
+    ASSERT_EQ(newValue, server->getAttributeValue(ATTR_KEY));
+}
+
 TEST_F(RemoteResourceObjectTest, QueryParamsForGetWillBePassedToBase)
 {
     class CustomHandler