[CONPRO-1337] Disabled Presence Feature
[platform/upstream/iotivity.git] / resource / unittests / OCExceptionTest.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include <algorithm>
22 #include <gtest/gtest.h>
23 #include <OCException.h>
24 #include <StringConstants.h>
25
26 namespace OC
27 {
28     namespace test
29     {
30         namespace OCExceptionTests
31         {
32             using namespace OC;
33
34             OCStackResult resultCodes[] =
35             {
36                 OC_STACK_OK,
37                 OC_STACK_RESOURCE_CREATED,
38                 OC_STACK_RESOURCE_DELETED,
39                 OC_STACK_CONTINUE,
40                 OC_STACK_RESOURCE_CHANGED,
41                 OC_STACK_INVALID_URI,
42                 OC_STACK_INVALID_QUERY,
43                 OC_STACK_INVALID_IP,
44                 OC_STACK_INVALID_PORT,
45                 OC_STACK_INVALID_CALLBACK,
46                 OC_STACK_INVALID_METHOD,
47                 OC_STACK_INVALID_PARAM,
48                 OC_STACK_INVALID_OBSERVE_PARAM,
49                 OC_STACK_NO_MEMORY,
50                 OC_STACK_COMM_ERROR,
51                 OC_STACK_TIMEOUT,
52                 OC_STACK_ADAPTER_NOT_ENABLED,
53                 OC_STACK_NOTIMPL,
54                 OC_STACK_NO_RESOURCE,
55                 OC_STACK_RESOURCE_ERROR,
56                 OC_STACK_SLOW_RESOURCE,
57                 OC_STACK_DUPLICATE_REQUEST,
58                 OC_STACK_NO_OBSERVERS,
59                 OC_STACK_OBSERVER_NOT_FOUND,
60                 OC_STACK_VIRTUAL_DO_NOT_HANDLE,
61                 OC_STACK_INVALID_OPTION,
62                 OC_STACK_MALFORMED_RESPONSE,
63                 OC_STACK_PERSISTENT_BUFFER_REQUIRED,
64                 OC_STACK_INVALID_REQUEST_HANDLE,
65                 OC_STACK_INVALID_DEVICE_INFO,
66                 OC_STACK_INVALID_JSON,
67                 OC_STACK_UNAUTHORIZED_REQ,
68                 OC_STACK_TOO_LARGE_REQ,
69                 OC_STACK_PDM_IS_NOT_INITIALIZED,
70                 OC_STACK_DUPLICATE_UUID,
71                 OC_STACK_INCONSISTENT_DB,
72                 OC_STACK_SVR_DB_NOT_EXIST,
73                 OC_STACK_AUTHENTICATION_FAILURE,
74                 OC_STACK_NOT_ALLOWED_OXM,
75 #ifdef WITH_PRESENCE
76                 OC_STACK_PRESENCE_STOPPED,
77                 OC_STACK_PRESENCE_TIMEOUT,
78                 OC_STACK_PRESENCE_DO_NOT_HANDLE,
79 #endif
80                 OC_STACK_USER_DENIED_REQ,
81                 OC_STACK_NOT_ACCEPTABLE,
82                 OC_STACK_METHOD_NOT_ALLOWED,
83                 OC_STACK_FORBIDDEN_REQ,
84                 OC_STACK_INTERNAL_SERVER_ERROR,
85                 OC_STACK_NOT_IMPLEMENTED,
86                 OC_STACK_BAD_GATEWAY,
87                 OC_STACK_SERVICE_UNAVAILABLE,
88                 OC_STACK_GATEWAY_TIMEOUT,
89                 OC_STACK_PROXY_NOT_SUPPORTED,
90                 OC_STACK_ERROR
91             };
92
93             std::string resultMessages[]=
94             {
95                 OC::Exception::NO_ERROR,
96                 OC::Exception::RESOURCE_CREATED,
97                 OC::Exception::RESOURCE_DELETED,
98                 OC::Exception::STACK_CONTINUE,
99                 OC::Exception::RESOURCE_CHANGED,
100                 OC::Exception::INVALID_URI,
101                 OC::Exception::INVALID_QUERY,
102                 OC::Exception::INVALID_IP,
103                 OC::Exception::INVALID_PORT,
104                 OC::Exception::INVALID_CB,
105                 OC::Exception::INVALID_METHOD,
106                 OC::Exception::INVALID_PARAM,
107                 OC::Exception::INVALID_OBESERVE,
108                 OC::Exception::NO_MEMORY,
109                 OC::Exception::COMM_ERROR,
110                 OC::Exception::TIMEOUT,
111                 OC::Exception::ADAPTER_NOT_ENABLED,
112                 OC::Exception::NOT_IMPL,
113                 OC::Exception::NOT_FOUND,
114                 OC::Exception::RESOURCE_ERROR,
115                 OC::Exception::SLOW_RESOURCE,
116                 OC::Exception::DUPLICATE_REQUEST,
117                 OC::Exception::NO_OBSERVERS,
118                 OC::Exception::OBSV_NO_FOUND,
119                 OC::Exception::VIRTUAL_DO_NOT_HANDLE,
120                 OC::Exception::INVALID_OPTION,
121                 OC::Exception::MALFORMED_STACK_RESPONSE,
122                 OC::Exception::PERSISTENT_BUFFER_REQUIRED,
123                 OC::Exception::INVALID_REQUEST_HANDLE,
124                 OC::Exception::INVALID_DEVICE_INFO,
125                 OC::Exception::INVALID_REPRESENTATION,
126                 OC::Exception::UNAUTHORIZED_REQUEST,
127                 OC::Exception::TOO_LARGE_REQ,
128                 OC::Exception::PDM_DB_NOT_INITIALIZED,
129                 OC::Exception::DUPLICATE_UUID,
130                 OC::Exception::INCONSISTENT_DB,
131                 OC::Exception::SVR_DB_NOT_EXIST,
132                 OC::Exception::AUTHENTICATION_FAILURE,
133                 OC::Exception::NOT_ALLOWED_OXM,
134                 OC::Exception::PRESENCE_STOPPED,
135                 OC::Exception::PRESENCE_TIMEOUT,
136                 OC::Exception::PRESENCE_NOT_HANDLED,
137                 OC::Exception::USER_DENIED_REQ,
138                 OC::Exception::NOT_ACCEPTABLE,
139                 OC::Exception::METHOD_NOT_ALLOWED,
140                 OC::Exception::FORBIDDEN_REQ,
141                 OC::Exception::INTERNAL_SERVER_ERROR,
142                 OC::Exception::NOT_IMPLEMENTED,
143                 OC::Exception::BAD_GATEWAY,
144                 OC::Exception::SERVICE_UNAVAILABLE,
145                 OC::Exception::GATEWAY_TIMEOUT,
146                 OC::Exception::PROXY_NOT_SUPPORTED,
147                 OC::Exception::GENERAL_FAULT
148             };
149             TEST(OCExceptionTest, ReasonCodeMatches)
150             {
151                 for(OCStackResult res : resultCodes)
152                 {
153                     OCException ex{"", res};
154                     EXPECT_EQ(res, ex.code());
155                 }
156             }
157
158             TEST(OCExceptionTest, MessageCodeMatches)
159             {
160                 std::string exceptionMessage = "This is the exception message!";
161                 OCException ex {exceptionMessage, OC_STACK_OK};
162
163                 EXPECT_EQ(exceptionMessage, ex.what());
164             }
165
166             TEST(OCExceptionTest, ReasonMapping)
167             {
168                 int i=0;
169                 for(OCStackResult res : resultCodes)
170                 {
171                     OCException ex{"", res};
172                     EXPECT_EQ(resultMessages[i], ex.reason());
173                     ++i;
174                 }
175             }
176
177             TEST(OCExceptionTest, UnknownReasonMappings)
178             {
179                 for(int i = 0; i < OC_STACK_ERROR; ++i)
180                 {
181                     if(std::find(
182                                 std::begin(resultCodes),
183                                 std::end(resultCodes),
184                                 static_cast<OCStackResult>(i))
185                             == std::end(resultCodes))
186                     {
187                         OCException ex {"", static_cast<OCStackResult>(i)};
188                         EXPECT_EQ(OC::Exception::UNKNOWN_ERROR, ex.reason());
189                     }
190                 }
191             }
192         } //namespace OCExceptionTests
193     } //namespace test
194 } //namespace OC