From a628f76c2a5f03623c698b924185895d1d0d4919 Mon Sep 17 00:00:00 2001 From: Erich Keane Date: Thu, 26 Mar 2015 15:43:30 -0700 Subject: [PATCH] Adding OCException unit tests 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 Reviewed-on: https://gerrit.iotivity.org/gerrit/584 Tested-by: jenkins-iotivity Reviewed-by: Sakthivel Samidurai Reviewed-by: Sudarshan Prasad --- resource/include/OCException.h | 5 -- resource/unittests/OCExceptionTest.cpp | 152 +++++++++++++++++++++++++++++++++ resource/unittests/SConscript | 3 +- 3 files changed, 154 insertions(+), 6 deletions(-) create mode 100644 resource/unittests/OCExceptionTest.cpp diff --git a/resource/include/OCException.h b/resource/include/OCException.h index 8b74bd8..2852202 100644 --- a/resource/include/OCException.h +++ b/resource/include/OCException.h @@ -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 index 0000000..0e0d4a6 --- /dev/null +++ b/resource/unittests/OCExceptionTest.cpp @@ -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 +#include +#include +#include + +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(i)) + == std::end(resultCodes)) + { + OCException ex {"", static_cast(i)}; + EXPECT_EQ(OC::Exception::UNKNOWN_ERROR, ex.reason()); + } + } + } + } //namespace OCExceptionTests + } //namespace test +} //namespace OC diff --git a/resource/unittests/SConscript b/resource/unittests/SConscript index ed74fbd..c7d93c8 100644 --- a/resource/unittests/SConscript +++ b/resource/unittests/SConscript @@ -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]) -- 2.7.4