Add SDL developer and user guides
[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 /*
18  * This source code is part of the near-RT RIC (RAN Intelligent Controller)
19  * platform project (RICP).
20 */
21
22 #include <type_traits>
23 #include <memory>
24 #include <cstring>
25 #include <gtest/gtest.h>
26 #include <async.h>
27 #include <sdl/errorqueries.hpp>
28 #include "private/redis/asyncredisstorage.hpp"
29 #include "private/error.hpp"
30
31 using namespace shareddatalayer;
32 using namespace shareddatalayer::redis;
33 using namespace testing;
34
35 namespace
36 {
37     std::string getErrorCodeMessage(std::error_code ec)
38     {
39         return ec.message();
40     }
41
42     class ErrorCodesTest: public testing::Test
43     {
44     public:
45         ErrorCodesTest()
46         {
47         }
48
49         virtual ~ErrorCodesTest()
50         {
51         }
52     };
53 }
54
55 TEST_F(ErrorCodesTest, AllAsyncRedisCommandDispatcherErrorCodesHaveCorrectDescriptionMessage)
56 {
57     std::error_code ec;
58
59     for (AsyncRedisCommandDispatcherErrorCode aec = AsyncRedisCommandDispatcherErrorCode::SUCCESS; aec != AsyncRedisCommandDispatcherErrorCode::END_MARKER; ++aec)
60     {
61         switch (aec)
62         {
63             case AsyncRedisCommandDispatcherErrorCode::SUCCESS:
64                 ec = aec;
65                 EXPECT_EQ(std::error_code().message(), getErrorCodeMessage(ec));
66                 break;
67             case AsyncRedisCommandDispatcherErrorCode::UNKNOWN_ERROR:
68                 ec = aec;
69                 EXPECT_EQ("redis error", getErrorCodeMessage(ec));
70                 break;
71             case AsyncRedisCommandDispatcherErrorCode::CONNECTION_LOST:
72                 ec = aec;
73                 EXPECT_EQ("redis connection lost", getErrorCodeMessage(ec));
74                 break;
75             case AsyncRedisCommandDispatcherErrorCode::PROTOCOL_ERROR:
76                 ec = aec;
77                 EXPECT_EQ("redis protocol error", getErrorCodeMessage(ec));
78                 break;
79             case AsyncRedisCommandDispatcherErrorCode::OUT_OF_MEMORY:
80                 ec = aec;
81                 EXPECT_EQ("redis out of memory", getErrorCodeMessage(ec));
82                 break;
83             case AsyncRedisCommandDispatcherErrorCode::DATASET_LOADING:
84                 ec = aec;
85                 EXPECT_EQ("redis dataset still being loaded into memory", getErrorCodeMessage(ec));
86                 break;
87             case AsyncRedisCommandDispatcherErrorCode::NOT_CONNECTED:
88                 ec = aec;
89                 EXPECT_EQ("not connected to redis, SDL operation not started", getErrorCodeMessage(ec));
90                 break;
91             case AsyncRedisCommandDispatcherErrorCode::IO_ERROR:
92                 ec = aec;
93                 EXPECT_EQ("redis I/O error", getErrorCodeMessage(ec));
94                 break;
95             case AsyncRedisCommandDispatcherErrorCode::WRITING_TO_SLAVE:
96                 ec = aec;
97                 EXPECT_EQ("writing to slave", getErrorCodeMessage(ec));
98                 break;
99             case AsyncRedisCommandDispatcherErrorCode::END_MARKER:
100                 ec = aec;
101                 EXPECT_EQ("unsupported error code for message()", getErrorCodeMessage(ec));
102                 break;
103             default:
104                 FAIL() << "No mapping for AsyncRedisCommandDispatcherErrorCode value: " << aec;
105                 break;
106         }
107     }
108 }
109
110 TEST_F(ErrorCodesTest, AllAsyncRedisCommandDispatcherErrorCodesAreMappedToCorrectSDLInternalError)
111 {
112     /* If this test case detects missing error code, remember to add new error code also to AllAsyncRedisCommandDispatcherErrorCodesAreMappedToCorrectClientErrorCode
113      * test case (and add also mapping implementation from InternalError to Error if needed).
114      */
115     std::error_code ec;
116
117     for (AsyncRedisCommandDispatcherErrorCode aec = AsyncRedisCommandDispatcherErrorCode::SUCCESS; aec != AsyncRedisCommandDispatcherErrorCode::END_MARKER; ++aec)
118     {
119         switch (aec)
120         {
121             case AsyncRedisCommandDispatcherErrorCode::SUCCESS:
122                 ec = aec;
123                 EXPECT_TRUE(ec == InternalError::SUCCESS);
124                 break;
125             case AsyncRedisCommandDispatcherErrorCode::UNKNOWN_ERROR:
126                 ec = aec;
127                 EXPECT_TRUE(ec == InternalError::BACKEND_ERROR);
128                 break;
129             case AsyncRedisCommandDispatcherErrorCode::CONNECTION_LOST:
130                 ec = aec;
131                 EXPECT_TRUE(ec == InternalError::BACKEND_CONNECTION_LOST);
132                 break;
133             case AsyncRedisCommandDispatcherErrorCode::PROTOCOL_ERROR:
134                 ec = aec;
135                 EXPECT_TRUE(ec == InternalError::BACKEND_REJECTED_REQUEST);
136                 break;
137             case AsyncRedisCommandDispatcherErrorCode::OUT_OF_MEMORY:
138                 ec = aec;
139                 EXPECT_TRUE(ec == InternalError::BACKEND_ERROR);
140                 break;
141             case AsyncRedisCommandDispatcherErrorCode::DATASET_LOADING:
142                 ec = aec;
143                 EXPECT_TRUE(ec == InternalError::BACKEND_NOT_READY);
144                 break;
145             case AsyncRedisCommandDispatcherErrorCode::NOT_CONNECTED:
146                 ec = aec;
147                 EXPECT_TRUE(ec == InternalError::SDL_NOT_CONNECTED_TO_BACKEND);
148                 break;
149             case AsyncRedisCommandDispatcherErrorCode::IO_ERROR:
150                 ec = aec;
151                 EXPECT_TRUE(ec == InternalError::BACKEND_ERROR);
152                 break;
153             case AsyncRedisCommandDispatcherErrorCode::WRITING_TO_SLAVE:
154                 ec = aec;
155                 EXPECT_TRUE(ec == InternalError::BACKEND_ERROR);
156                 break;
157             case AsyncRedisCommandDispatcherErrorCode::END_MARKER:
158                 ec = aec;
159                 EXPECT_TRUE(ec == InternalError::SDL_ERROR_CODE_LOGIC_ERROR);
160                 break;
161             default:
162                 FAIL() << "No mapping for AsyncRedisCommandDispatcherErrorCode value: " << aec;
163                 break;
164         }
165     }
166 }
167
168 TEST_F(ErrorCodesTest, AllErrorCodeEnumsAreMappedToCorrectClientErrorCode)
169 {
170     std::error_code ec;
171
172     ec = std::error_code();
173     EXPECT_TRUE(ec == shareddatalayer::Error::SUCCESS);
174
175     ec = AsyncRedisStorage::ErrorCode::SUCCESS;
176     EXPECT_TRUE(ec == shareddatalayer::Error::SUCCESS);
177     ec = AsyncRedisStorage::ErrorCode::REDIS_NOT_YET_DISCOVERED;
178     EXPECT_TRUE(ec == shareddatalayer::Error::NOT_CONNECTED);
179     ec = AsyncRedisStorage::ErrorCode::INVALID_NAMESPACE;
180     EXPECT_TRUE(ec == shareddatalayer::Error::REJECTED_BY_SDL);
181     ec = AsyncRedisStorage::ErrorCode::END_MARKER;
182     EXPECT_TRUE(ec == shareddatalayer::Error::BACKEND_FAILURE);
183
184     ec = AsyncRedisCommandDispatcherErrorCode::SUCCESS;
185     EXPECT_TRUE(ec == shareddatalayer::Error::SUCCESS);
186     ec = AsyncRedisCommandDispatcherErrorCode::UNKNOWN_ERROR;
187     EXPECT_TRUE(ec == shareddatalayer::Error::BACKEND_FAILURE);
188     ec = AsyncRedisCommandDispatcherErrorCode::CONNECTION_LOST;
189     EXPECT_TRUE(ec == shareddatalayer::Error::OPERATION_INTERRUPTED);
190     ec = AsyncRedisCommandDispatcherErrorCode::PROTOCOL_ERROR;
191     EXPECT_TRUE(ec == shareddatalayer::Error::REJECTED_BY_BACKEND);
192     ec = AsyncRedisCommandDispatcherErrorCode::OUT_OF_MEMORY;
193     EXPECT_TRUE(ec == shareddatalayer::Error::BACKEND_FAILURE);
194     ec = AsyncRedisCommandDispatcherErrorCode::DATASET_LOADING;
195     EXPECT_TRUE(ec == shareddatalayer::Error::NOT_CONNECTED);
196     ec = AsyncRedisCommandDispatcherErrorCode::NOT_CONNECTED;
197     EXPECT_TRUE(ec == shareddatalayer::Error::NOT_CONNECTED);
198     ec = AsyncRedisCommandDispatcherErrorCode::IO_ERROR;
199     EXPECT_TRUE(ec == shareddatalayer::Error::BACKEND_FAILURE);
200     ec = AsyncRedisCommandDispatcherErrorCode::END_MARKER;
201     EXPECT_TRUE(ec == shareddatalayer::Error::BACKEND_FAILURE);
202 }
203
204 TEST_F(ErrorCodesTest, ErrorCodeEnumsDoNotMapToIncorrectClientErrorCode)
205 {
206     std::error_code ec;
207
208     ec = AsyncRedisStorage::ErrorCode::SUCCESS;
209     EXPECT_TRUE(ec != shareddatalayer::Error::BACKEND_FAILURE);
210     ec = AsyncRedisStorage::ErrorCode::REDIS_NOT_YET_DISCOVERED;
211     EXPECT_TRUE(ec != shareddatalayer::Error::BACKEND_FAILURE);
212     ec = AsyncRedisStorage::ErrorCode::INVALID_NAMESPACE;
213     EXPECT_TRUE(ec != shareddatalayer::Error::BACKEND_FAILURE);
214     ec = AsyncRedisStorage::ErrorCode::END_MARKER;
215     EXPECT_TRUE(ec != shareddatalayer::Error::SUCCESS);
216
217     ec = AsyncRedisCommandDispatcherErrorCode::SUCCESS;
218     EXPECT_TRUE(ec != shareddatalayer::Error::NOT_CONNECTED);
219     ec = AsyncRedisCommandDispatcherErrorCode::UNKNOWN_ERROR;
220     EXPECT_TRUE(ec != shareddatalayer::Error::SUCCESS);
221     ec = AsyncRedisCommandDispatcherErrorCode::CONNECTION_LOST;
222     EXPECT_TRUE(ec != shareddatalayer::Error::BACKEND_FAILURE);
223     ec = AsyncRedisCommandDispatcherErrorCode::PROTOCOL_ERROR;
224     EXPECT_TRUE(ec != shareddatalayer::Error::NOT_CONNECTED);
225     ec = AsyncRedisCommandDispatcherErrorCode::OUT_OF_MEMORY;
226     EXPECT_TRUE(ec != shareddatalayer::Error::NOT_CONNECTED);
227     ec = AsyncRedisCommandDispatcherErrorCode::DATASET_LOADING;
228     EXPECT_TRUE(ec != shareddatalayer::Error::SUCCESS);
229     ec = AsyncRedisCommandDispatcherErrorCode::NOT_CONNECTED;
230     EXPECT_TRUE(ec != shareddatalayer::Error::SUCCESS);
231     ec = AsyncRedisCommandDispatcherErrorCode::IO_ERROR;
232     EXPECT_TRUE(ec != shareddatalayer::Error::OPERATION_INTERRUPTED);
233     ec = AsyncRedisCommandDispatcherErrorCode::END_MARKER;
234     EXPECT_TRUE(ec != shareddatalayer::Error::OPERATION_INTERRUPTED);
235 }
236
237 TEST_F(ErrorCodesTest, AllErrorCodeEnumClassesHaveCategory)
238 {
239     EXPECT_STREQ("asyncrediscommanddispatcher",
240                  std::error_code(AsyncRedisCommandDispatcherErrorCode::SUCCESS).category().name());
241
242     EXPECT_STREQ("asyncredisstorage",
243                  std::error_code(AsyncRedisStorage::ErrorCode::SUCCESS).category().name());
244 }