Adding OCException unit tests
[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_INVALID_URI,
41                 OC_STACK_INVALID_QUERY,
42                 OC_STACK_INVALID_IP,
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,
48                 OC_STACK_NO_MEMORY,
49                 OC_STACK_COMM_ERROR,
50                 OC_STACK_TIMEOUT,
51                 OC_STACK_ADAPTER_NOT_ENABLED,
52                 OC_STACK_NOTIMPL,
53                 OC_STACK_NO_RESOURCE,
54                 OC_STACK_RESOURCE_ERROR,
55                 OC_STACK_SLOW_RESOURCE,
56                 OC_STACK_NO_OBSERVERS,
57                 OC_STACK_OBSERVER_NOT_FOUND,
58                 OC_STACK_VIRTUAL_DO_NOT_HANDLE,
59                 OC_STACK_INVALID_OPTION,
60                 OC_STACK_MALFORMED_RESPONSE,
61                 OC_STACK_PERSISTENT_BUFFER_REQUIRED,
62                 OC_STACK_INVALID_REQUEST_HANDLE,
63                 OC_STACK_INVALID_DEVICE_INFO,
64                 OC_STACK_INVALID_JSON,
65                 OC_STACK_PRESENCE_STOPPED,
66                 OC_STACK_PRESENCE_TIMEOUT,
67                 OC_STACK_PRESENCE_DO_NOT_HANDLE,
68                 OC_STACK_ERROR
69             };
70
71             std::string resultMessages[]=
72             {
73                 OC::Exception::NO_ERROR,
74                 OC::Exception::RESOURCE_CREATED,
75                 OC::Exception::RESOURCE_DELETED,
76                 OC::Exception::STACK_CONTINUE,
77                 OC::Exception::INVALID_URI,
78                 OC::Exception::INVALID_QUERY,
79                 OC::Exception::INVALID_IP,
80                 OC::Exception::INVALID_PORT,
81                 OC::Exception::INVALID_CB,
82                 OC::Exception::INVALID_METHOD,
83                 OC::Exception::INVALID_PARAM,
84                 OC::Exception::INVALID_OBESERVE,
85                 OC::Exception::NO_MEMORY,
86                 OC::Exception::COMM_ERROR,
87                 OC::Exception::TIMEOUT,
88                 OC::Exception::ADAPTER_NOT_ENABLED,
89                 OC::Exception::NOT_IMPL,
90                 OC::Exception::NOT_FOUND,
91                 OC::Exception::RESOURCE_ERROR,
92                 OC::Exception::SLOW_RESOURCE,
93                 OC::Exception::NO_OBSERVERS,
94                 OC::Exception::OBSV_NO_FOUND,
95                 OC::Exception::VIRTUAL_DO_NOT_HANDLE,
96                 OC::Exception::INVALID_OPTION,
97                 OC::Exception::MALFORMED_STACK_RESPONSE,
98                 OC::Exception::PERSISTENT_BUFFER_REQUIRED,
99                 OC::Exception::INVALID_REQUEST_HANDLE,
100                 OC::Exception::INVALID_DEVICE_INFO,
101                 OC::Exception::INVALID_REPRESENTATION,
102                 OC::Exception::PRESENCE_STOPPED,
103                 OC::Exception::PRESENCE_TIMEOUT,
104                 OC::Exception::PRESENCE_NOT_HANDLED,
105                 OC::Exception::GENERAL_FAULT
106             };
107             TEST(OCExceptionTest, ReasonCodeMatches)
108             {
109                 for(OCStackResult res : resultCodes)
110                 {
111                     OCException ex{"", res};
112                     EXPECT_EQ(res, ex.code());
113                 }
114             }
115
116             TEST(OCExceptionTest, MessageCodeMatches)
117             {
118                 std::string exceptionMessage = "This is the exception message!";
119                 OCException ex {exceptionMessage, OC_STACK_OK};
120
121                 EXPECT_EQ(exceptionMessage, ex.what());
122             }
123
124             TEST(OCExceptionTest, ReasonMapping)
125             {
126                 int i=0;
127                 for(OCStackResult res : resultCodes)
128                 {
129                     OCException ex{"", res};
130                     EXPECT_EQ(resultMessages[i], ex.reason());
131                     ++i;
132                 }
133             }
134
135             TEST(OCExceptionTest, UnknownReasonMappings)
136             {
137                 for(int i = 0; i < OC_STACK_ERROR; ++i)
138                 {
139                     if(std::find(
140                                 std::begin(resultCodes),
141                                 std::end(resultCodes),
142                                 static_cast<OCStackResult>(i))
143                             == std::end(resultCodes))
144                     {
145                         OCException ex {"", static_cast<OCStackResult>(i)};
146                         EXPECT_EQ(OC::Exception::UNKNOWN_ERROR, ex.reason());
147                     }
148                 }
149             }
150         } //namespace OCExceptionTests
151     } //namespace test
152 } //namespace OC