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,
40 OC_STACK_RESOURCE_CHANGED,
42 OC_STACK_INVALID_QUERY,
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,
52 OC_STACK_ADAPTER_NOT_ENABLED,
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_PRESENCE_STOPPED,
70 OC_STACK_PRESENCE_TIMEOUT,
71 OC_STACK_PRESENCE_DO_NOT_HANDLE,
73 OC_STACK_PDM_IS_NOT_INITIALIZED,
74 OC_STACK_DUPLICATE_UUID,
75 OC_STACK_INCONSISTENT_DB,
76 OC_STACK_AUTHENTICATION_FAILURE
79 std::string resultMessages[]=
81 OC::Exception::NO_ERROR,
82 OC::Exception::RESOURCE_CREATED,
83 OC::Exception::RESOURCE_DELETED,
84 OC::Exception::STACK_CONTINUE,
85 OC::Exception::RESOURCE_CHANGED,
86 OC::Exception::INVALID_URI,
87 OC::Exception::INVALID_QUERY,
88 OC::Exception::INVALID_IP,
89 OC::Exception::INVALID_PORT,
90 OC::Exception::INVALID_CB,
91 OC::Exception::INVALID_METHOD,
92 OC::Exception::INVALID_PARAM,
93 OC::Exception::INVALID_OBESERVE,
94 OC::Exception::NO_MEMORY,
95 OC::Exception::COMM_ERROR,
96 OC::Exception::TIMEOUT,
97 OC::Exception::ADAPTER_NOT_ENABLED,
98 OC::Exception::NOT_IMPL,
99 OC::Exception::NOT_FOUND,
100 OC::Exception::RESOURCE_ERROR,
101 OC::Exception::SLOW_RESOURCE,
102 OC::Exception::DUPLICATE_REQUEST,
103 OC::Exception::NO_OBSERVERS,
104 OC::Exception::OBSV_NO_FOUND,
105 OC::Exception::VIRTUAL_DO_NOT_HANDLE,
106 OC::Exception::INVALID_OPTION,
107 OC::Exception::MALFORMED_STACK_RESPONSE,
108 OC::Exception::PERSISTENT_BUFFER_REQUIRED,
109 OC::Exception::INVALID_REQUEST_HANDLE,
110 OC::Exception::INVALID_DEVICE_INFO,
111 OC::Exception::INVALID_REPRESENTATION,
112 OC::Exception::UNAUTHORIZED_REQUEST,
113 OC::Exception::TOO_LARGE_REQ,
114 OC::Exception::PRESENCE_STOPPED,
115 OC::Exception::PRESENCE_TIMEOUT,
116 OC::Exception::PRESENCE_NOT_HANDLED,
117 OC::Exception::GENERAL_FAULT,
118 OC::Exception::PDM_DB_NOT_INITIALIZED,
119 OC::Exception::DUPLICATE_UUID,
120 OC::Exception::INCONSISTENT_DB,
121 OC::Exception::AUTHENTICATION_FAILURE
123 TEST(OCExceptionTest, ReasonCodeMatches)
125 for(OCStackResult res : resultCodes)
127 OCException ex{"", res};
128 EXPECT_EQ(res, ex.code());
132 TEST(OCExceptionTest, MessageCodeMatches)
134 std::string exceptionMessage = "This is the exception message!";
135 OCException ex {exceptionMessage, OC_STACK_OK};
137 EXPECT_EQ(exceptionMessage, ex.what());
140 TEST(OCExceptionTest, ReasonMapping)
143 for(OCStackResult res : resultCodes)
145 OCException ex{"", res};
146 EXPECT_EQ(resultMessages[i], ex.reason());
151 TEST(OCExceptionTest, UnknownReasonMappings)
153 for(int i = 0; i < OC_STACK_ERROR; ++i)
156 std::begin(resultCodes),
157 std::end(resultCodes),
158 static_cast<OCStackResult>(i))
159 == std::end(resultCodes))
161 OCException ex {"", static_cast<OCStackResult>(i)};
162 EXPECT_EQ(OC::Exception::UNKNOWN_ERROR, ex.reason());
166 } //namespace OCExceptionTests