Added unit test for OCResourceResponse.cpp
authorShilpa Sodani <shilpa.a.sodani@intel.com>
Sun, 12 Apr 2015 12:36:15 +0000 (05:36 -0700)
committerErich Keane <erich.keane@intel.com>
Wed, 20 May 2015 17:34:38 +0000 (17:34 +0000)
Added unit test for public APIs in OCResourceResponse.cpp

Change-Id: Ie33a95d4a34459577cc6cb92eb049c609ccad3d2
Signed-off-by: Shilpa Sodani <shilpa.a.sodani@intel.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/739
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Erich Keane <erich.keane@intel.com>
resource/csdk/connectivity/test/SConscript
resource/include/OCResourceResponse.h
resource/unittests/OCResourceResponseTest.cpp [new file with mode: 0644]
resource/unittests/SConscript

index 8e5d0fa..7189319 100644 (file)
@@ -36,7 +36,7 @@ catest_env.PrependUnique(CPPPATH = [
                 '../../extlibs/cjson',
                 '../../../oc_logger/include',
                 '../../../../extlibs/gtest/gtest-1.7.0/include'
-                ])
+               ])
 
 tmplist = catest_env['LINKFLAGS'][:]
 if '-lpthread' in tmplist:
index 54ffadb..c654834 100644 (file)
@@ -222,7 +222,10 @@ namespace OC
         /**
         * Get error code
         */
-        int getErrorCode() const;
+        int getErrorCode() const
+        {
+            return m_errorCode;
+        }
 
         /**
          * Get the Response Representation
diff --git a/resource/unittests/OCResourceResponseTest.cpp b/resource/unittests/OCResourceResponseTest.cpp
new file mode 100644 (file)
index 0000000..0768836
--- /dev/null
@@ -0,0 +1,189 @@
+//******************************************************************
+//
+// Copyright 2015 Intel Mobile Communications GmbH 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 <OCPlatform.h>
+#include <OCApi.h>
+#include <gtest/gtest.h>
+#include <OCResourceResponse.h>
+#include <ocserverrequest.h>
+#include <InProcServerWrapper.h>
+
+namespace PH = std::placeholders;
+namespace OCResourceResponseTest
+{
+    using namespace OC;
+    using namespace std;
+
+    TEST(ErrorCodeTest, SetGetErrorCodeValidCode)
+    {
+        OCResourceResponse response;
+        int setCode = 200;
+        EXPECT_NO_THROW(response.setErrorCode(setCode));
+        EXPECT_EQ(setCode, response.getErrorCode());
+    }
+
+    TEST(NewResourceUriTest, SetGetNewResourceUriValidUri)
+    {
+        OCResourceResponse response;
+        std::string uri = "/a/light";
+        EXPECT_NO_THROW(response.setNewResourceUri(uri));
+        EXPECT_EQ(uri, response.getNewResourceUri());
+    }
+
+    TEST(NewResourceUriTest, SetGetNewResourceUriEmpty)
+    {
+        OCResourceResponse response;
+        std::string uri = "/a/light";
+        EXPECT_NO_THROW(response.setNewResourceUri(uri));
+        EXPECT_NE("", response.getNewResourceUri());
+    }
+
+    TEST(ResposeHeaderOptionsTest, SetGetHeaderOptionsValidOption)
+    {
+        OCResourceResponse response;
+        const uint16_t API_VERSION = 2048;
+        const std::string FRIDGE_CLIENT_API_VERSION = "v.1.0";
+        HeaderOptions headerOptions;
+        HeaderOption::OCHeaderOption apiVersion(API_VERSION, FRIDGE_CLIENT_API_VERSION);
+        headerOptions.push_back(apiVersion);
+
+        EXPECT_NO_THROW(response.setHeaderOptions(headerOptions));
+        EXPECT_FALSE(headerOptions.empty());
+        EXPECT_EQ(apiVersion.getOptionID(),
+                response.getHeaderOptions()[0].getOptionID());
+        EXPECT_EQ(apiVersion.getOptionData(),
+                response.getHeaderOptions()[0].getOptionData());
+     }
+
+    TEST(ResposeHeaderOptionsTest, SetGetHeaderOptionsEmpty)
+    {
+        OCResourceResponse response;
+        HeaderOptions headerOptions;
+
+        EXPECT_NO_THROW(response.setHeaderOptions(headerOptions));
+        EXPECT_TRUE(headerOptions.empty());
+    }
+
+    TEST(RequestHandleTest, SetGetRequestHandleValidHandle)
+    {
+        char query[] = "?rt=core.light";
+        char address[] = "127.0.0.1";
+        OCResourceResponse response;
+        OCServerRequest request;
+        request.method = OC_REST_GET;
+        strncpy(request.query, query, sizeof(query));
+        request.connectivityType = CA_IPV4;
+        strncpy(request.addressInfo.IP.ipAddress, address, sizeof(query));
+        request.addressInfo.IP.port = 5364;
+        request.qos =  OC_LOW_QOS;
+        request.coapID = 0;
+        request.delayedResNeeded = 0;
+        request.secured = 0;
+
+        OCRequestHandle handle = static_cast<OCRequestHandle>(&request);
+        EXPECT_EQ(NULL, response.getRequestHandle());
+        EXPECT_NO_THROW(response.setRequestHandle(handle));
+        EXPECT_NE(static_cast<OCRequestHandle>(NULL), response.getRequestHandle());
+    }
+
+    TEST(RequestHandleTest, SetGetRequestHandleNullHandle)
+    {
+        OCResourceResponse response;
+        OCRequestHandle handle = nullptr;
+
+        EXPECT_EQ(NULL, response.getRequestHandle());
+        EXPECT_NO_THROW(response.setRequestHandle(handle));
+        EXPECT_EQ(NULL, response.getRequestHandle());
+    }
+
+    TEST(ResourceHandleTest, SetGetResourceHandleValidHandle)
+    {
+        OCResourceResponse response;
+        OCResourceHandle resHandle;
+
+        std::string resourceURI = "/a/light1";
+        std::string resourceTypeName = "core.light";
+        std::string resourceInterface = DEFAULT_INTERFACE;
+        uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
+
+        EXPECT_EQ(OC_STACK_OK, OCCreateResource(&resHandle, resourceURI.c_str(),
+                resourceTypeName.c_str(), resourceInterface.c_str(), nullptr,
+                resourceProperty));
+        EXPECT_EQ(NULL, response.getResourceHandle());
+        EXPECT_NO_THROW(response.setResourceHandle(resHandle));
+        EXPECT_NE(static_cast<OCResourceHandle>(NULL), response.getResourceHandle());
+    }
+
+    TEST(ResourceHandleTest, SetGetResourceHandleNullHandle)
+    {
+        OCResourceResponse response;
+        OCResourceHandle handle = nullptr;
+
+        EXPECT_EQ(NULL, response.getResourceHandle());
+        EXPECT_NO_THROW(response.setResourceHandle(handle));
+        EXPECT_EQ(NULL, response.getResourceHandle());
+    }
+
+    TEST(ResponseResultTest, SetGetResponseResultValidInput)
+    {
+        OCResourceResponse response;
+        OCEntityHandlerResult result = OC_EH_SLOW;
+        EXPECT_NO_THROW(response.setResponseResult(result));
+        EXPECT_EQ(result, response.getResponseResult());
+    }
+
+    TEST(ResourceRepresentation, SetGetResourceRepresentationWithValidRepresentation)
+    {
+        OCResourceResponse response;
+        OCRepresentation lightRepresentation;
+        const std::string LIGHT_RESOURCE_KEY = "light";
+        lightRepresentation.setValue(LIGHT_RESOURCE_KEY, 0);
+        EXPECT_TRUE(response.getResourceRepresentation().emptyData());
+        EXPECT_NO_THROW(response.setResourceRepresentation(lightRepresentation));
+        EXPECT_FALSE(response.getResourceRepresentation().emptyData());
+    }
+
+    TEST(ResourceRepresentation,
+            SetGetResourceRepresentationWithRepresentationAndEmptyInterface)
+    {
+        OCResourceResponse response;
+        OCRepresentation lightRepresentation;
+
+        const std::string LIGHT_RESOURCE_KEY = "light";
+        lightRepresentation.setValue(LIGHT_RESOURCE_KEY, 0);
+        EXPECT_TRUE(response.getResourceRepresentation().emptyData());
+        EXPECT_NO_THROW(response.setResourceRepresentation(lightRepresentation, ""));
+        EXPECT_FALSE(response.getResourceRepresentation().emptyData());
+    }
+
+    TEST(ResourceRepresentation,
+            SetGetResourceRepresentationWithRepresentationAndInterface)
+    {
+        OCResourceResponse response;
+        OCRepresentation lightRepresentation;
+
+        const std::string LIGHT_RESOURCE_KEY = "light";
+        lightRepresentation.setValue(LIGHT_RESOURCE_KEY, 0);
+        EXPECT_TRUE(response.getResourceRepresentation().emptyData());
+        EXPECT_NO_THROW(response.setResourceRepresentation(lightRepresentation,
+                LINK_INTERFACE));
+        EXPECT_FALSE(response.getResourceRepresentation().emptyData());
+    }
+}
index 35118f4..cdb9ca7 100644 (file)
@@ -31,6 +31,9 @@ unittests_env.PrependUnique(CPPPATH = [
                '../include',
                '../oc_logger/include',
                '../csdk/stack/include',
+               '../csdk/security/include',
+               '../csdk/stack/include/internal',
+               '../csdk/connectivity/api',
                '../csdk/ocsocket/include',
                '../csdk/ocrandom/include',
                '../csdk/logger/include',
@@ -67,6 +70,7 @@ unittests = unittests_env.Program('unittests', ['ConstructResourceTest.cpp',
                                                 'OCRepresentationTest.cpp',
                                                 'OCResourceTest.cpp',
                                                 'OCExceptionTest.cpp',
+                                                'OCResourceResponseTest.cpp',
                                                 'OCHeaderOptionTest.cpp'])
 
 Alias("unittests", [unittests])