Adding OCException unit tests
authorErich Keane <erich.keane@intel.com>
Thu, 26 Mar 2015 22:43:30 +0000 (15:43 -0700)
committerSudarshan Prasad <sudarshan.prasad@intel.com>
Sun, 29 Mar 2015 18:01:20 +0000 (18:01 +0000)
Adding a bunch of unit tests to validate the functionality of
OCException.

Also removed the unused 'reason' function that didn't use 'this'

Change-Id: I34bac1c06b2bc755a0af19e3ddcc1ac9ae3611fc
Signed-off-by: Erich Keane <erich.keane@intel.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/584
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Sakthivel Samidurai <sakthivel.samidurai@intel.com>
Reviewed-by: Sudarshan Prasad <sudarshan.prasad@intel.com>
resource/include/OCException.h
resource/unittests/OCExceptionTest.cpp [new file with mode: 0644]
resource/unittests/SConscript

index 8b74bd8..2852202 100644 (file)
@@ -42,11 +42,6 @@ class OCException : public std::runtime_error
             return reason(m_reason);
         }
 
-        std::string reason(const OC::OCException& e) const
-        {
-            return e.reason();
-        }
-
         OCStackResult code() const
         {
             return m_reason;
diff --git a/resource/unittests/OCExceptionTest.cpp b/resource/unittests/OCExceptionTest.cpp
new file mode 100644 (file)
index 0000000..0e0d4a6
--- /dev/null
@@ -0,0 +1,152 @@
+//******************************************************************
+//
+// 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 <algorithm>
+#include <gtest/gtest.h>
+#include <OCException.h>
+#include <StringConstants.h>
+
+namespace OC
+{
+    namespace test
+    {
+        namespace OCExceptionTests
+        {
+            using namespace OC;
+
+            OCStackResult resultCodes[] =
+            {
+                OC_STACK_OK,
+                OC_STACK_RESOURCE_CREATED,
+                OC_STACK_RESOURCE_DELETED,
+                OC_STACK_CONTINUE,
+                OC_STACK_INVALID_URI,
+                OC_STACK_INVALID_QUERY,
+                OC_STACK_INVALID_IP,
+                OC_STACK_INVALID_PORT,
+                OC_STACK_INVALID_CALLBACK,
+                OC_STACK_INVALID_METHOD,
+                OC_STACK_INVALID_PARAM,
+                OC_STACK_INVALID_OBSERVE_PARAM,
+                OC_STACK_NO_MEMORY,
+                OC_STACK_COMM_ERROR,
+                OC_STACK_TIMEOUT,
+                OC_STACK_ADAPTER_NOT_ENABLED,
+                OC_STACK_NOTIMPL,
+                OC_STACK_NO_RESOURCE,
+                OC_STACK_RESOURCE_ERROR,
+                OC_STACK_SLOW_RESOURCE,
+                OC_STACK_NO_OBSERVERS,
+                OC_STACK_OBSERVER_NOT_FOUND,
+                OC_STACK_VIRTUAL_DO_NOT_HANDLE,
+                OC_STACK_INVALID_OPTION,
+                OC_STACK_MALFORMED_RESPONSE,
+                OC_STACK_PERSISTENT_BUFFER_REQUIRED,
+                OC_STACK_INVALID_REQUEST_HANDLE,
+                OC_STACK_INVALID_DEVICE_INFO,
+                OC_STACK_INVALID_JSON,
+                OC_STACK_PRESENCE_STOPPED,
+                OC_STACK_PRESENCE_TIMEOUT,
+                OC_STACK_PRESENCE_DO_NOT_HANDLE,
+                OC_STACK_ERROR
+            };
+
+            std::string resultMessages[]=
+            {
+                OC::Exception::NO_ERROR,
+                OC::Exception::RESOURCE_CREATED,
+                OC::Exception::RESOURCE_DELETED,
+                OC::Exception::STACK_CONTINUE,
+                OC::Exception::INVALID_URI,
+                OC::Exception::INVALID_QUERY,
+                OC::Exception::INVALID_IP,
+                OC::Exception::INVALID_PORT,
+                OC::Exception::INVALID_CB,
+                OC::Exception::INVALID_METHOD,
+                OC::Exception::INVALID_PARAM,
+                OC::Exception::INVALID_OBESERVE,
+                OC::Exception::NO_MEMORY,
+                OC::Exception::COMM_ERROR,
+                OC::Exception::TIMEOUT,
+                OC::Exception::ADAPTER_NOT_ENABLED,
+                OC::Exception::NOT_IMPL,
+                OC::Exception::NOT_FOUND,
+                OC::Exception::RESOURCE_ERROR,
+                OC::Exception::SLOW_RESOURCE,
+                OC::Exception::NO_OBSERVERS,
+                OC::Exception::OBSV_NO_FOUND,
+                OC::Exception::VIRTUAL_DO_NOT_HANDLE,
+                OC::Exception::INVALID_OPTION,
+                OC::Exception::MALFORMED_STACK_RESPONSE,
+                OC::Exception::PERSISTENT_BUFFER_REQUIRED,
+                OC::Exception::INVALID_REQUEST_HANDLE,
+                OC::Exception::INVALID_DEVICE_INFO,
+                OC::Exception::INVALID_REPRESENTATION,
+                OC::Exception::PRESENCE_STOPPED,
+                OC::Exception::PRESENCE_TIMEOUT,
+                OC::Exception::PRESENCE_NOT_HANDLED,
+                OC::Exception::GENERAL_FAULT
+            };
+            TEST(OCExceptionTest, ReasonCodeMatches)
+            {
+                for(OCStackResult res : resultCodes)
+                {
+                    OCException ex{"", res};
+                    EXPECT_EQ(res, ex.code());
+                }
+            }
+
+            TEST(OCExceptionTest, MessageCodeMatches)
+            {
+                std::string exceptionMessage = "This is the exception message!";
+                OCException ex {exceptionMessage, OC_STACK_OK};
+
+                EXPECT_EQ(exceptionMessage, ex.what());
+            }
+
+            TEST(OCExceptionTest, ReasonMapping)
+            {
+                int i=0;
+                for(OCStackResult res : resultCodes)
+                {
+                    OCException ex{"", res};
+                    EXPECT_EQ(resultMessages[i], ex.reason());
+                    ++i;
+                }
+            }
+
+            TEST(OCExceptionTest, UnknownReasonMappings)
+            {
+                for(int i = 0; i < OC_STACK_ERROR; ++i)
+                {
+                    if(std::find(
+                                std::begin(resultCodes),
+                                std::end(resultCodes),
+                                static_cast<OCStackResult>(i))
+                            == std::end(resultCodes))
+                    {
+                        OCException ex {"", static_cast<OCStackResult>(i)};
+                        EXPECT_EQ(OC::Exception::UNKNOWN_ERROR, ex.reason());
+                    }
+                }
+            }
+        } //namespace OCExceptionTests
+    } //namespace test
+} //namespace OC
index ed74fbd..c7d93c8 100644 (file)
@@ -66,7 +66,8 @@ if not env.get('RELEASE'):
 unittests = unittests_env.Program('unittests', ['ConstructResourceTest.cpp',
                                                 'OCPlatformTest.cpp',
                                                 'OCRepresentationTest.cpp',
-                                                'OCResourceTest.cpp'])
+                                                'OCResourceTest.cpp',
+                                                'OCExceptionTest.cpp'])
 
 Alias("unittests", [unittests])