86e5134059b246e02736b0f4987b915c1b153e1c
[ric-plt/sdl.git] / tst / error_test.cpp
1 /*
2    Copyright (c) 2018-2019 Nokia.
3
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7
8        http://www.apache.org/licenses/LICENSE-2.0
9
10    Unless required by applicable law or agreed to in writing, software
11    distributed under the License is distributed on an "AS IS" BASIS,
12    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    See the License for the specific language governing permissions and
14    limitations under the License.
15 */
16
17 #include <type_traits>
18 #include <memory>
19 #include <cstring>
20 #include <gtest/gtest.h>
21 #include <async.h>
22 #include <sdl/errorqueries.hpp>
23 #include "private/redis/asyncredisstorage.hpp"
24 #include "private/error.hpp"
25
26 using namespace shareddatalayer;
27 using namespace shareddatalayer::redis;
28 using namespace testing;
29
30 namespace
31 {
32     std::string getErrorCodeMessage(std::error_code ec)
33     {
34         return ec.message();
35     }
36
37     class ErrorCodesTest: public testing::Test
38     {
39     public:
40         ErrorCodesTest()
41         {
42         }
43
44         virtual ~ErrorCodesTest()
45         {
46         }
47     };
48 }
49
50 TEST_F(ErrorCodesTest, AllAsyncRedisCommandDispatcherErrorCodesHaveCorrectDescriptionMessage)
51 {
52     std::error_code ec;
53
54     for (AsyncRedisCommandDispatcherErrorCode aec = AsyncRedisCommandDispatcherErrorCode::SUCCESS; aec != AsyncRedisCommandDispatcherErrorCode::END_MARKER; ++aec)
55     {
56         switch (aec)
57         {
58             case AsyncRedisCommandDispatcherErrorCode::SUCCESS:
59                 ec = aec;
60                 EXPECT_EQ(std::error_code().message(), getErrorCodeMessage(ec));
61                 break;
62             case AsyncRedisCommandDispatcherErrorCode::UNKNOWN_ERROR:
63                 ec = aec;
64                 EXPECT_EQ("redis error", getErrorCodeMessage(ec));
65                 break;
66             case AsyncRedisCommandDispatcherErrorCode::CONNECTION_LOST:
67                 ec = aec;
68                 EXPECT_EQ("redis connection lost", getErrorCodeMessage(ec));
69                 break;
70             case AsyncRedisCommandDispatcherErrorCode::PROTOCOL_ERROR:
71                 ec = aec;
72                 EXPECT_EQ("redis protocol error", getErrorCodeMessage(ec));
73                 break;
74             case AsyncRedisCommandDispatcherErrorCode::OUT_OF_MEMORY:
75                 ec = aec;
76                 EXPECT_EQ("redis out of memory", getErrorCodeMessage(ec));
77                 break;
78             case AsyncRedisCommandDispatcherErrorCode::DATASET_LOADING:
79                 ec = aec;
80                 EXPECT_EQ("redis dataset still being loaded into memory", getErrorCodeMessage(ec));
81                 break;
82             case AsyncRedisCommandDispatcherErrorCode::NOT_CONNECTED:
83                 ec = aec;
84                 EXPECT_EQ("not connected to redis, SDL operation not started", getErrorCodeMessage(ec));
85                 break;
86             case AsyncRedisCommandDispatcherErrorCode::IO_ERROR:
87                 ec = aec;
88                 EXPECT_EQ("redis I/O error", getErrorCodeMessage(ec));
89                 break;
90             case AsyncRedisCommandDispatcherErrorCode::END_MARKER:
91                 ec = aec;
92                 EXPECT_EQ("unsupported error code for message()", getErrorCodeMessage(ec));
93                 break;
94             default:
95                 FAIL() << "No mapping for AsyncRedisCommandDispatcherErrorCode value: " << aec;
96                 break;
97         }
98     }
99 }
100
101 TEST_F(ErrorCodesTest, AllAsyncRedisCommandDispatcherErrorCodesAreMappedToCorrectSDLInternalError)
102 {
103     /* If this test case detects missing error code, remember to add new error code also to AllAsyncRedisCommandDispatcherErrorCodesAreMappedToCorrectClientErrorCode
104      * test case (and add also mapping implementation from InternalError to Error if needed).
105      */
106     std::error_code ec;
107
108     for (AsyncRedisCommandDispatcherErrorCode aec = AsyncRedisCommandDispatcherErrorCode::SUCCESS; aec != AsyncRedisCommandDispatcherErrorCode::END_MARKER; ++aec)
109     {
110         switch (aec)
111         {
112             case AsyncRedisCommandDispatcherErrorCode::SUCCESS:
113                 ec = aec;
114                 EXPECT_TRUE(ec == InternalError::SUCCESS);
115                 break;
116             case AsyncRedisCommandDispatcherErrorCode::UNKNOWN_ERROR:
117                 ec = aec;
118                 EXPECT_TRUE(ec == InternalError::BACKEND_ERROR);
119                 break;
120             case AsyncRedisCommandDispatcherErrorCode::CONNECTION_LOST:
121                 ec = aec;
122                 EXPECT_TRUE(ec == InternalError::BACKEND_CONNECTION_LOST);
123                 break;
124             case AsyncRedisCommandDispatcherErrorCode::PROTOCOL_ERROR:
125                 ec = aec;
126                 EXPECT_TRUE(ec == InternalError::BACKEND_REJECTED_REQUEST);
127                 break;
128             case AsyncRedisCommandDispatcherErrorCode::OUT_OF_MEMORY:
129                 ec = aec;
130                 EXPECT_TRUE(ec == InternalError::BACKEND_ERROR);
131                 break;
132             case AsyncRedisCommandDispatcherErrorCode::DATASET_LOADING:
133                 ec = aec;
134                 EXPECT_TRUE(ec == InternalError::BACKEND_NOT_READY);
135                 break;
136             case AsyncRedisCommandDispatcherErrorCode::NOT_CONNECTED:
137                 ec = aec;
138                 EXPECT_TRUE(ec == InternalError::SDL_NOT_CONNECTED_TO_BACKEND);
139                 break;
140             case AsyncRedisCommandDispatcherErrorCode::IO_ERROR:
141                 ec = aec;
142                 EXPECT_TRUE(ec == InternalError::BACKEND_ERROR);
143                 break;
144             case AsyncRedisCommandDispatcherErrorCode::END_MARKER:
145                 ec = aec;
146                 EXPECT_TRUE(ec == InternalError::SDL_ERROR_CODE_LOGIC_ERROR);
147                 break;
148             default:
149                 FAIL() << "No mapping for AsyncRedisCommandDispatcherErrorCode value: " << aec;
150                 break;
151         }
152     }
153 }
154
155 TEST_F(ErrorCodesTest, AllErrorCodeEnumsAreMappedToCorrectClientErrorCode)
156 {
157     std::error_code ec;
158
159     ec = std::error_code();
160     EXPECT_TRUE(ec == shareddatalayer::Error::SUCCESS);
161
162     ec = AsyncRedisStorage::ErrorCode::SUCCESS;
163     EXPECT_TRUE(ec == shareddatalayer::Error::SUCCESS);
164     ec = AsyncRedisStorage::ErrorCode::REDIS_NOT_YET_DISCOVERED;
165     EXPECT_TRUE(ec == shareddatalayer::Error::NOT_CONNECTED);
166     ec = AsyncRedisStorage::ErrorCode::INVALID_NAMESPACE;
167     EXPECT_TRUE(ec == shareddatalayer::Error::REJECTED_BY_SDL);
168     ec = AsyncRedisStorage::ErrorCode::END_MARKER;
169     EXPECT_TRUE(ec == shareddatalayer::Error::BACKEND_FAILURE);
170
171     ec = AsyncRedisCommandDispatcherErrorCode::SUCCESS;
172     EXPECT_TRUE(ec == shareddatalayer::Error::SUCCESS);
173     ec = AsyncRedisCommandDispatcherErrorCode::UNKNOWN_ERROR;
174     EXPECT_TRUE(ec == shareddatalayer::Error::BACKEND_FAILURE);
175     ec = AsyncRedisCommandDispatcherErrorCode::CONNECTION_LOST;
176     EXPECT_TRUE(ec == shareddatalayer::Error::OPERATION_INTERRUPTED);
177     ec = AsyncRedisCommandDispatcherErrorCode::PROTOCOL_ERROR;
178     EXPECT_TRUE(ec == shareddatalayer::Error::REJECTED_BY_BACKEND);
179     ec = AsyncRedisCommandDispatcherErrorCode::OUT_OF_MEMORY;
180     EXPECT_TRUE(ec == shareddatalayer::Error::BACKEND_FAILURE);
181     ec = AsyncRedisCommandDispatcherErrorCode::DATASET_LOADING;
182     EXPECT_TRUE(ec == shareddatalayer::Error::NOT_CONNECTED);
183     ec = AsyncRedisCommandDispatcherErrorCode::NOT_CONNECTED;
184     EXPECT_TRUE(ec == shareddatalayer::Error::NOT_CONNECTED);
185     ec = AsyncRedisCommandDispatcherErrorCode::IO_ERROR;
186     EXPECT_TRUE(ec == shareddatalayer::Error::BACKEND_FAILURE);
187     ec = AsyncRedisCommandDispatcherErrorCode::END_MARKER;
188     EXPECT_TRUE(ec == shareddatalayer::Error::BACKEND_FAILURE);
189 }
190
191 TEST_F(ErrorCodesTest, ErrorCodeEnumsDoNotMapToIncorrectClientErrorCode)
192 {
193     std::error_code ec;
194
195     ec = AsyncRedisStorage::ErrorCode::SUCCESS;
196     EXPECT_TRUE(ec != shareddatalayer::Error::BACKEND_FAILURE);
197     ec = AsyncRedisStorage::ErrorCode::REDIS_NOT_YET_DISCOVERED;
198     EXPECT_TRUE(ec != shareddatalayer::Error::BACKEND_FAILURE);
199     ec = AsyncRedisStorage::ErrorCode::INVALID_NAMESPACE;
200     EXPECT_TRUE(ec != shareddatalayer::Error::BACKEND_FAILURE);
201     ec = AsyncRedisStorage::ErrorCode::END_MARKER;
202     EXPECT_TRUE(ec != shareddatalayer::Error::SUCCESS);
203
204     ec = AsyncRedisCommandDispatcherErrorCode::SUCCESS;
205     EXPECT_TRUE(ec != shareddatalayer::Error::NOT_CONNECTED);
206     ec = AsyncRedisCommandDispatcherErrorCode::UNKNOWN_ERROR;
207     EXPECT_TRUE(ec != shareddatalayer::Error::SUCCESS);
208     ec = AsyncRedisCommandDispatcherErrorCode::CONNECTION_LOST;
209     EXPECT_TRUE(ec != shareddatalayer::Error::BACKEND_FAILURE);
210     ec = AsyncRedisCommandDispatcherErrorCode::PROTOCOL_ERROR;
211     EXPECT_TRUE(ec != shareddatalayer::Error::NOT_CONNECTED);
212     ec = AsyncRedisCommandDispatcherErrorCode::OUT_OF_MEMORY;
213     EXPECT_TRUE(ec != shareddatalayer::Error::NOT_CONNECTED);
214     ec = AsyncRedisCommandDispatcherErrorCode::DATASET_LOADING;
215     EXPECT_TRUE(ec != shareddatalayer::Error::SUCCESS);
216     ec = AsyncRedisCommandDispatcherErrorCode::NOT_CONNECTED;
217     EXPECT_TRUE(ec != shareddatalayer::Error::SUCCESS);
218     ec = AsyncRedisCommandDispatcherErrorCode::IO_ERROR;
219     EXPECT_TRUE(ec != shareddatalayer::Error::OPERATION_INTERRUPTED);
220     ec = AsyncRedisCommandDispatcherErrorCode::END_MARKER;
221     EXPECT_TRUE(ec != shareddatalayer::Error::OPERATION_INTERRUPTED);
222 }
223
224 TEST_F(ErrorCodesTest, AllErrorCodeEnumClassesHaveCategory)
225 {
226     EXPECT_STREQ("asyncrediscommanddispatcher",
227                  std::error_code(AsyncRedisCommandDispatcherErrorCode::SUCCESS).category().name());
228
229     EXPECT_STREQ("asyncredisstorage",
230                  std::error_code(AsyncRedisStorage::ErrorCode::SUCCESS).category().name());
231 }