Added Unittests and Added LICENSE
authorjaesick.shin <jaesick.shin@samsung.com>
Thu, 10 Mar 2016 01:56:45 +0000 (10:56 +0900)
committerJungHo Kim <jhyo.kim@samsung.com>
Thu, 17 Mar 2016 04:40:22 +0000 (04:40 +0000)
Generate response test(Actuator Interface, Custom Interface)
InterfaceHandler.cpp file not contains LICENSE
Added LICENSE

Change-Id: I034f43f6524ed630ed806173ffa3db267308dec5
Signed-off-by: jaesick.shin <jaesick.shin@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/5657
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: JungHo Kim <jhyo.kim@samsung.com>
(cherry picked from commit b683f1a68f66f9db2743fe20e8d9e3b92b6675c3)
Reviewed-on: https://gerrit.iotivity.org/gerrit/5809

service/resource-encapsulation/src/serverBuilder/src/InterfaceHandler.cpp
service/resource-encapsulation/src/serverBuilder/unittests/RCSResourceObjectTest.cpp

index 14287c4031e164c936638cd4b31cdb37c9084938..ea50568e163862b3c4a03c9f98b19b9f7b338d48 100644 (file)
@@ -1,3 +1,23 @@
+//******************************************************************
+//
+// 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.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
 #include "InterfaceHandler.h"
 
 #include "OCResourceRequest.h"
index b3d934c7599abbbec61a5ab83370548624a9daf6..31269123704485a258d55668d228a0e438294afe 100644 (file)
@@ -39,14 +39,11 @@ typedef OCStackResult (*registerResource)(OCResourceHandle&, string&, const stri
 
 typedef OCStackResult (*NotifyAllObservers)(OCResourceHandle);
 
-typedef OCStackResult (bindInterfaceToResource)(const OCResourceHandle&, const std::string&);
-
-
 constexpr char RESOURCE_URI[]{ "a/test" };
 constexpr char RESOURCE_TYPE[]{ "resourcetype" };
 constexpr char KEY[]{ "key" };
-constexpr int VALUE{ 100 };
 constexpr char CUSTOM_INTERFACE[]{ "oic.if.custom" };
+constexpr int VALUE{ 100 };
 
 TEST(ResourceObjectBuilderCreateTest, ThrowIfUriIsInvalid)
 {
@@ -543,20 +540,24 @@ static bool checkResponse(const OCRepresentation& ocRep, const RCSResourceAttrib
             const std::vector<std::string>& interfaces,
             const std::vector<std::string>& resourceTypes, const std::string& resourceUri)
 {
-    if( ocRep.getUri() != resourceUri || ocRep.getResourceInterfaces() != interfaces ||
-            ocRep.getResourceTypes() != resourceTypes ||
-            rcsAttr != ResourceAttributesConverter::fromOCRepresentation(ocRep))
-    {
-        return false;
-    }
+    return resourceUri == ocRep.getUri() &&
+           interfaces == ocRep.getResourceInterfaces() &&
+           resourceTypes == ocRep.getResourceTypes() &&
+           rcsAttr == ResourceAttributesConverter::fromOCRepresentation(ocRep);
+}
 
-    return true;
+static bool compareResponse(const OCRepresentation& ocRep1, const OCRepresentation& ocRep2)
+{
+    return ocRep1.getUri() == ocRep2.getUri() &&
+           ocRep1.getResourceInterfaces() == ocRep2.getResourceInterfaces() &&
+           ocRep1.getResourceTypes() == ocRep2.getResourceTypes() &&
+           ResourceAttributesConverter::fromOCRepresentation(ocRep1) ==
+                   ResourceAttributesConverter::fromOCRepresentation(ocRep2);
 }
 
 class ResourceObjectInterfaceHandlerTest: public ResourceObjectHandlingRequestTest
 {
 public:
-
     void initServer(vector<string> interfaces,
                 const std::string& defaultInterface = BASELINE_INTERFACE)
     {
@@ -570,9 +571,6 @@ public:
 
         RCSResourceAttributes rcsAttr;
         rcsAttr[KEY] = 2;
-        rcsAttr["KEY1"] = "1";
-        rcsAttr["KEY2"] = true;
-        rcsAttr["KEY3"] = 3.14;
         initBuilder.setAttributes(rcsAttr);
         initBuilder.setDefaultInterface(defaultInterface);
 
@@ -581,13 +579,6 @@ public:
         server->setSetRequestHandlerPolicy(RCSResourceObject::SetRequestHandlerPolicy::ACCEPTANCE);
     }
 
-    OCRepresentation createOCRepresentation()
-    {
-        OCRepresentation ocRep;
-        ocRep[KEY] = VALUE;
-        return ocRep;
-    }
-
 protected:
 
     void SetUp()
@@ -609,72 +600,105 @@ TEST_F(ResourceObjectInterfaceHandlerTest, GetResponseForBaselineContainsAllProp
 {
     initServer({BASELINE_INTERFACE});
 
+    OCRepresentation ocRep;
+
     mocks.ExpectCallFunc(OCPlatform::sendResponse).Match(
-                [=](const shared_ptr<OCResourceResponse> response)
-                {
-                    RCSResourceObject::LockGuard guard{ server };
-                    return checkResponse(response->getResourceRepresentation(),
-                            server->getAttributes(), server->getInterfaces(), server->getTypes(),
-                            server->getUri());
+            [=](const shared_ptr<OCResourceResponse> response)
+            {
+                RCSResourceObject::LockGuard guard{ server };
 
-                }
-        ).Return(OC_STACK_OK);
+                return checkResponse(response->getResourceRepresentation(),
+                        server->getAttributes(), server->getInterfaces(), server->getTypes(),
+                        server->getUri());
 
-    handler(createRequest(OC_REST_GET, createOCRepresentation(), BASELINE_INTERFACE));
+            }
+    ).Return(OC_STACK_OK);
+
+    handler(createRequest(OC_REST_GET, ocRep, BASELINE_INTERFACE));
 }
 
-TEST_F(ResourceObjectInterfaceHandlerTest, SetResponseForActuatorContainsAllPropertiesOfServer)
+TEST_F(ResourceObjectInterfaceHandlerTest, SetResponseForActuatorContainsOnlyRequestedAttributes)
 {
     initServer({ACTUATOR_INTERFACE});
 
+    OCRepresentation ocRep;
+    ocRep[KEY] = VALUE;
+
     mocks.ExpectCallFunc(OCPlatform::sendResponse).Match(
-                [](const shared_ptr<OCResourceResponse> response)
-                {
-                    RCSResourceAttributes attrs;
-                    attrs[KEY] = VALUE;
-                    return checkResponse(response->getResourceRepresentation(),
-                            attrs, {}, {}, "");
+            [&ocRep](const shared_ptr<OCResourceResponse> response)
+            {
+                return checkResponse(response->getResourceRepresentation(),
+                        ResourceAttributesConverter::fromOCRepresentation(ocRep), {}, {}, "");
+            }
+    ).Return(OC_STACK_OK);
+
+    handler(createRequest(OC_REST_POST, ocRep, ACTUATOR_INTERFACE));
+}
+
+TEST_F(ResourceObjectInterfaceHandlerTest, SetResponseForBaselineContainsAppliedAttributes)
+{
+    initServer({BASELINE_INTERFACE});
+
+    OCRepresentation ocRep;
+    ocRep["NEWKEY"] = std::string("NEWVALUE");
+
+    mocks.ExpectCallFunc(OCPlatform::sendResponse).Match(
+            [=](const shared_ptr<OCResourceResponse> response)
+            {
+                RCSResourceObject::LockGuard guard{ server };
 
-                }
-        ).Return(OC_STACK_OK);
+                return checkResponse(response->getResourceRepresentation(),
+                        server->getAttributes(), server->getInterfaces(), server->getTypes(),
+                        server->getUri());
+            }
+    ).Return(OC_STACK_OK);
 
-    handler(createRequest(OC_REST_POST, createOCRepresentation(), ACTUATOR_INTERFACE));
+    handler(createRequest(OC_REST_POST, ocRep, BASELINE_INTERFACE));
 }
 
-TEST_F(ResourceObjectInterfaceHandlerTest, GetResponseForCustomInterfaceCompareToDefaultInterface)
+TEST_F(ResourceObjectInterfaceHandlerTest, GetResponseForCustomEqualsResponseForDefault)
 {
     initServer({CUSTOM_INTERFACE});
 
-    mocks.ExpectCallFunc(OCPlatform::sendResponse).Match(
-                [=](const shared_ptr<OCResourceResponse> response)
-                {
-                    RCSResourceObject::LockGuard guard{ server };
-                    return checkResponse(response->getResourceRepresentation(),
-                            server->getAttributes(), server->getInterfaces(), server->getTypes(),
-                            server->getUri());
+    OCRepresentation ocRep;
+    OCRepresentation repArray[2];
+    int cnt = 0;
 
-                }
-        ).Return(OC_STACK_OK);
+    mocks.OnCallFunc(OCPlatform::sendResponse).Do(
+            [&repArray, &cnt](const shared_ptr<OCResourceResponse> response)
+            {
+                repArray[cnt++] = response->getResourceRepresentation();
+                return OC_STACK_OK;
+            }
+    );
+
+    handler(createRequest(OC_REST_GET, ocRep, CUSTOM_INTERFACE));
+    handler(createRequest(OC_REST_GET, ocRep, server->getDefaultInterface()));
 
-    handler(createRequest(OC_REST_GET, createOCRepresentation(), CUSTOM_INTERFACE));
+    EXPECT_EQ(cnt, 2);
+    EXPECT_TRUE(compareResponse(repArray[0], repArray[1]));
 }
 
-TEST_F(ResourceObjectInterfaceHandlerTest, SetResponseForSensorHasNotSetHandler)
+TEST_F(ResourceObjectInterfaceHandlerTest, SetRequestForSensorGotNoHandler)
 {
     initServer({SENSOR_INTERFACE});
 
-    EXPECT_EQ(OC_EH_ERROR, handler(createRequest(OC_REST_POST, createOCRepresentation(),
-            SENSOR_INTERFACE)));
+    OCRepresentation ocRep;
+    ocRep[KEY] = VALUE;
+
+    EXPECT_EQ(OC_EH_ERROR, handler(createRequest(OC_REST_POST, ocRep, SENSOR_INTERFACE)));
 }
 
 TEST_F(ResourceObjectInterfaceHandlerTest, ThrowIfDefaultInterfaceIsInvalid)
 {
-    ASSERT_THROW(initServer({SENSOR_INTERFACE}, ACTUATOR_INTERFACE), RCSBadRequestException);
+    auto builder = RCSResourceObject::Builder(RESOURCE_URI, RESOURCE_TYPE, BASELINE_INTERFACE);
+
+    ASSERT_THROW(builder.setDefaultInterface(ACTUATOR_INTERFACE), RCSBadRequestException);
 }
 
-TEST_F(ResourceObjectInterfaceHandlerTest, DefaultInterfaceCompareToDefaultInterfaceOfServer)
+TEST_F(ResourceObjectInterfaceHandlerTest, SettingDefaultInterfaceEqualsGetDefaultInterface)
 {
-    initServer({SENSOR_INTERFACE}, BASELINE_INTERFACE);
+    initServer({ SENSOR_INTERFACE }, BASELINE_INTERFACE);
 
     EXPECT_EQ(BASELINE_INTERFACE, server->getDefaultInterface());
 }