1 //******************************************************************
3 // Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 #include <gtest/gtest.h>
23 #include <OCException.h>
24 #include <StringConstants.h>
30 namespace OCExceptionTests
34 OCStackResult resultCodes[] =
37 OC_STACK_RESOURCE_CREATED,
38 OC_STACK_RESOURCE_DELETED,
41 OC_STACK_INVALID_QUERY,
43 OC_STACK_INVALID_PORT,
44 OC_STACK_INVALID_CALLBACK,
45 OC_STACK_INVALID_METHOD,
46 OC_STACK_INVALID_PARAM,
47 OC_STACK_INVALID_OBSERVE_PARAM,
51 OC_STACK_ADAPTER_NOT_ENABLED,
54 OC_STACK_RESOURCE_ERROR,
55 OC_STACK_SLOW_RESOURCE,
56 OC_STACK_DUPLICATE_REQUEST,
57 OC_STACK_NO_OBSERVERS,
58 OC_STACK_OBSERVER_NOT_FOUND,
59 OC_STACK_VIRTUAL_DO_NOT_HANDLE,
60 OC_STACK_INVALID_OPTION,
61 OC_STACK_MALFORMED_RESPONSE,
62 OC_STACK_PERSISTENT_BUFFER_REQUIRED,
63 OC_STACK_INVALID_REQUEST_HANDLE,
64 OC_STACK_INVALID_DEVICE_INFO,
65 OC_STACK_INVALID_JSON,
66 OC_STACK_UNAUTHORIZED_REQ,
67 OC_STACK_TOO_LARGE_REQ,
68 OC_STACK_PRESENCE_STOPPED,
69 OC_STACK_PRESENCE_TIMEOUT,
70 OC_STACK_PRESENCE_DO_NOT_HANDLE,
72 OC_STACK_PDM_IS_NOT_INITIALIZED,
73 OC_STACK_DUPLICATE_UUID,
74 OC_STACK_INCONSISTENT_DB,
75 OC_STACK_AUTHENTICATION_FAILURE
78 std::string resultMessages[]=
80 OC::Exception::NO_ERROR,
81 OC::Exception::RESOURCE_CREATED,
82 OC::Exception::RESOURCE_DELETED,
83 OC::Exception::STACK_CONTINUE,
84 OC::Exception::INVALID_URI,
85 OC::Exception::INVALID_QUERY,
86 OC::Exception::INVALID_IP,
87 OC::Exception::INVALID_PORT,
88 OC::Exception::INVALID_CB,
89 OC::Exception::INVALID_METHOD,
90 OC::Exception::INVALID_PARAM,
91 OC::Exception::INVALID_OBESERVE,
92 OC::Exception::NO_MEMORY,
93 OC::Exception::COMM_ERROR,
94 OC::Exception::TIMEOUT,
95 OC::Exception::ADAPTER_NOT_ENABLED,
96 OC::Exception::NOT_IMPL,
97 OC::Exception::NOT_FOUND,
98 OC::Exception::RESOURCE_ERROR,
99 OC::Exception::SLOW_RESOURCE,
100 OC::Exception::DUPLICATE_REQUEST,
101 OC::Exception::NO_OBSERVERS,
102 OC::Exception::OBSV_NO_FOUND,
103 OC::Exception::VIRTUAL_DO_NOT_HANDLE,
104 OC::Exception::INVALID_OPTION,
105 OC::Exception::MALFORMED_STACK_RESPONSE,
106 OC::Exception::PERSISTENT_BUFFER_REQUIRED,
107 OC::Exception::INVALID_REQUEST_HANDLE,
108 OC::Exception::INVALID_DEVICE_INFO,
109 OC::Exception::INVALID_REPRESENTATION,
110 OC::Exception::UNAUTHORIZED_REQUEST,
111 OC::Exception::TOO_LARGE_REQ,
112 OC::Exception::PRESENCE_STOPPED,
113 OC::Exception::PRESENCE_TIMEOUT,
114 OC::Exception::PRESENCE_NOT_HANDLED,
115 OC::Exception::GENERAL_FAULT,
116 OC::Exception::PDM_DB_NOT_INITIALIZED,
117 OC::Exception::DUPLICATE_UUID,
118 OC::Exception::INCONSISTENT_DB,
119 OC::Exception::AUTHENTICATION_FAILURE
121 TEST(OCExceptionTest, ReasonCodeMatches)
123 for(OCStackResult res : resultCodes)
125 OCException ex{"", res};
126 EXPECT_EQ(res, ex.code());
130 TEST(OCExceptionTest, MessageCodeMatches)
132 std::string exceptionMessage = "This is the exception message!";
133 OCException ex {exceptionMessage, OC_STACK_OK};
135 EXPECT_EQ(exceptionMessage, ex.what());
138 TEST(OCExceptionTest, ReasonMapping)
141 for(OCStackResult res : resultCodes)
143 OCException ex{"", res};
144 EXPECT_EQ(resultMessages[i], ex.reason());
149 TEST(OCExceptionTest, UnknownReasonMappings)
151 for(int i = 0; i < OC_STACK_ERROR; ++i)
154 std::begin(resultCodes),
155 std::end(resultCodes),
156 static_cast<OCStackResult>(i))
157 == std::end(resultCodes))
159 OCException ex {"", static_cast<OCStackResult>(i)};
160 EXPECT_EQ(OC::Exception::UNKNOWN_ERROR, ex.reason());
164 } //namespace OCExceptionTests