replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / resource-encapsulation / unittests / ResourceClientTest.cpp
index 171531b..78e443d 100644 (file)
@@ -20,6 +20,8 @@
 
 #include "UnitTestHelper.h"
 
+#include "OCResource.h"
+
 #include "RCSRemoteResourceObject.h"
 #include "RCSDiscoveryManager.h"
 #include "RCSResourceObject.h"
@@ -43,8 +45,9 @@ 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&) {}
+void cacheUpdatedCallback(const RCSResourceAttributes&, int) {}
 
 class RemoteResourceObjectTest: public TestWithMock
 {
@@ -161,7 +164,33 @@ TEST_F(RemoteResourceObjectTest, SetRemoteAttributesSetsAttributesOfServer)
     object->setRemoteAttributes(newAttrs, setRemoteAttributesCallback);
     Wait();
 
-    ASSERT_EQ(newValue, server->getAttributeValue(ATTR_KEY));
+    ASSERT_EQ(newValue, server->getAttributeValue(ATTR_KEY).get<int>());
+}
+
+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).get<int>());
 }
 
 TEST_F(RemoteResourceObjectTest, QueryParamsForGetWillBePassedToBase)
@@ -265,7 +294,7 @@ TEST_F(RemoteResourceObjectTest, CacheStateIsUnreadyAfterStartCaching)
 TEST_F(RemoteResourceObjectTest, CacheStateIsReadyAfterCacheUpdated)
 {
     mocks.ExpectCallFunc(cacheUpdatedCallback).
-                Do([this](const RCSResourceAttributes&){ Proceed(); });
+                Do([this](const RCSResourceAttributes&, int){ Proceed(); });
 
     object->startCaching(cacheUpdatedCallback);
     Wait();
@@ -276,7 +305,7 @@ TEST_F(RemoteResourceObjectTest, CacheStateIsReadyAfterCacheUpdated)
 TEST_F(RemoteResourceObjectTest, IsCachedAvailableReturnsTrueWhenCacheIsReady)
 {
     mocks.ExpectCallFunc(cacheUpdatedCallback).
-                Do([this](const RCSResourceAttributes&){ Proceed(); });
+                Do([this](const RCSResourceAttributes&, int){ Proceed(); });
 
     object->startCaching(cacheUpdatedCallback);
     Wait();
@@ -287,12 +316,12 @@ TEST_F(RemoteResourceObjectTest, IsCachedAvailableReturnsTrueWhenCacheIsReady)
 TEST_F(RemoteResourceObjectTest, DISABLED_CacheUpdatedCallbackBeCalledWheneverCacheUpdated)
 {
     mocks.OnCallFunc(cacheUpdatedCallback).
-            Do([this](const RCSResourceAttributes&){ Proceed(); });
+            Do([this](const RCSResourceAttributes&, int){ Proceed(); });
     object->startCaching(cacheUpdatedCallback);
     Wait();
 
     mocks.ExpectCallFunc(cacheUpdatedCallback).
-            Do([this](const RCSResourceAttributes&){ Proceed(); });
+            Do([this](const RCSResourceAttributes&, int){ Proceed(); });
 
     server->setAttribute(ATTR_KEY, ATTR_VALUE + 1);
 
@@ -304,15 +333,15 @@ TEST_F(RemoteResourceObjectTest, DISABLED_CacheUpdatedCallbackBeCalledWithUpdate
     constexpr int newValue = ATTR_VALUE + 1;
 
     mocks.OnCallFunc(cacheUpdatedCallback).
-            Do([this](const RCSResourceAttributes&){ Proceed(); });
+            Do([this](const RCSResourceAttributes&, int){ Proceed(); });
     object->startCaching(cacheUpdatedCallback);
     Wait();
 
     mocks.ExpectCallFunc(cacheUpdatedCallback).
-            Match([this](const RCSResourceAttributes& attrs){
+            Match([this](const RCSResourceAttributes& attrs, int){
                 return attrs.at(ATTR_KEY) == newValue;
             }).
-            Do([this](const RCSResourceAttributes&){ Proceed(); });
+            Do([this](const RCSResourceAttributes&, int){ Proceed(); });
 
     server->setAttribute(ATTR_KEY, newValue);
 
@@ -327,7 +356,7 @@ TEST_F(RemoteResourceObjectTest, GetCachedAttributesThrowsIfCachingIsNotStarted)
 TEST_F(RemoteResourceObjectTest, CachedAttributesHasSameAttributesWithServer)
 {
     mocks.OnCallFunc(cacheUpdatedCallback).
-            Do([this](const RCSResourceAttributes&){ Proceed(); });
+            Do([this](const RCSResourceAttributes&, int){ Proceed(); });
     object->startCaching(cacheUpdatedCallback);
     Wait();
 
@@ -344,7 +373,7 @@ TEST_F(RemoteResourceObjectTest, GetCachedAttributeThrowsIfCachingIsNotStarted)
 TEST_F(RemoteResourceObjectTest, GetCachedAttributeThrowsIfKeyIsInvalid)
 {
     mocks.OnCallFunc(cacheUpdatedCallback).
-            Do([this](const RCSResourceAttributes&){ Proceed(); });
+            Do([this](const RCSResourceAttributes&, int){ Proceed(); });
     object->startCaching(cacheUpdatedCallback);
     Wait();
 
@@ -366,3 +395,12 @@ TEST_F(RemoteResourceObjectTest, HasSameInterfaceWithServer)
     EXPECT_EQ(RESOURCEINTERFACE, object->getInterfaces()[0]);
 }
 
+TEST_F(RemoteResourceObjectTest, GetValidOCResourceTest)
+{
+    std::shared_ptr<OC::OCResource> ocRes = RCSRemoteResourceObject::toOCResource(object);
+
+    EXPECT_NE(nullptr, ocRes);
+
+    EXPECT_EQ(RESOURCEURI, ocRes->uri());
+}
+