2 Copyright (c) 2018-2019 Nokia.
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
18 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
19 * platform project (RICP).
22 #include <type_traits>
25 #include <gtest/gtest.h>
27 #include <sdl/errorqueries.hpp>
28 #include "private/redis/asyncredisstorage.hpp"
29 #include "private/error.hpp"
31 using namespace shareddatalayer;
32 using namespace shareddatalayer::redis;
33 using namespace testing;
37 std::string getErrorCodeMessage(std::error_code ec)
42 class ErrorCodesTest: public testing::Test
49 virtual ~ErrorCodesTest()
55 TEST_F(ErrorCodesTest, AllAsyncRedisCommandDispatcherErrorCodesHaveCorrectDescriptionMessage)
59 for (AsyncRedisCommandDispatcherErrorCode aec = AsyncRedisCommandDispatcherErrorCode::SUCCESS; aec != AsyncRedisCommandDispatcherErrorCode::END_MARKER; ++aec)
63 case AsyncRedisCommandDispatcherErrorCode::SUCCESS:
65 EXPECT_EQ(std::error_code().message(), getErrorCodeMessage(ec));
67 case AsyncRedisCommandDispatcherErrorCode::UNKNOWN_ERROR:
69 EXPECT_EQ("redis error", getErrorCodeMessage(ec));
71 case AsyncRedisCommandDispatcherErrorCode::CONNECTION_LOST:
73 EXPECT_EQ("redis connection lost", getErrorCodeMessage(ec));
75 case AsyncRedisCommandDispatcherErrorCode::PROTOCOL_ERROR:
77 EXPECT_EQ("redis protocol error", getErrorCodeMessage(ec));
79 case AsyncRedisCommandDispatcherErrorCode::OUT_OF_MEMORY:
81 EXPECT_EQ("redis out of memory", getErrorCodeMessage(ec));
83 case AsyncRedisCommandDispatcherErrorCode::DATASET_LOADING:
85 EXPECT_EQ("redis dataset still being loaded into memory", getErrorCodeMessage(ec));
87 case AsyncRedisCommandDispatcherErrorCode::NOT_CONNECTED:
89 EXPECT_EQ("not connected to redis, SDL operation not started", getErrorCodeMessage(ec));
91 case AsyncRedisCommandDispatcherErrorCode::IO_ERROR:
93 EXPECT_EQ("redis I/O error", getErrorCodeMessage(ec));
95 case AsyncRedisCommandDispatcherErrorCode::WRITING_TO_SLAVE:
97 EXPECT_EQ("writing to slave", getErrorCodeMessage(ec));
99 case AsyncRedisCommandDispatcherErrorCode::END_MARKER:
101 EXPECT_EQ("unsupported error code for message()", getErrorCodeMessage(ec));
104 FAIL() << "No mapping for AsyncRedisCommandDispatcherErrorCode value: " << aec;
110 TEST_F(ErrorCodesTest, AllAsyncRedisCommandDispatcherErrorCodesAreMappedToCorrectSDLInternalError)
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).
117 for (AsyncRedisCommandDispatcherErrorCode aec = AsyncRedisCommandDispatcherErrorCode::SUCCESS; aec != AsyncRedisCommandDispatcherErrorCode::END_MARKER; ++aec)
121 case AsyncRedisCommandDispatcherErrorCode::SUCCESS:
123 EXPECT_TRUE(ec == InternalError::SUCCESS);
125 case AsyncRedisCommandDispatcherErrorCode::UNKNOWN_ERROR:
127 EXPECT_TRUE(ec == InternalError::BACKEND_ERROR);
129 case AsyncRedisCommandDispatcherErrorCode::CONNECTION_LOST:
131 EXPECT_TRUE(ec == InternalError::BACKEND_CONNECTION_LOST);
133 case AsyncRedisCommandDispatcherErrorCode::PROTOCOL_ERROR:
135 EXPECT_TRUE(ec == InternalError::BACKEND_REJECTED_REQUEST);
137 case AsyncRedisCommandDispatcherErrorCode::OUT_OF_MEMORY:
139 EXPECT_TRUE(ec == InternalError::BACKEND_ERROR);
141 case AsyncRedisCommandDispatcherErrorCode::DATASET_LOADING:
143 EXPECT_TRUE(ec == InternalError::BACKEND_NOT_READY);
145 case AsyncRedisCommandDispatcherErrorCode::NOT_CONNECTED:
147 EXPECT_TRUE(ec == InternalError::SDL_NOT_CONNECTED_TO_BACKEND);
149 case AsyncRedisCommandDispatcherErrorCode::IO_ERROR:
151 EXPECT_TRUE(ec == InternalError::BACKEND_ERROR);
153 case AsyncRedisCommandDispatcherErrorCode::WRITING_TO_SLAVE:
155 EXPECT_TRUE(ec == InternalError::BACKEND_ERROR);
157 case AsyncRedisCommandDispatcherErrorCode::END_MARKER:
159 EXPECT_TRUE(ec == InternalError::SDL_ERROR_CODE_LOGIC_ERROR);
162 FAIL() << "No mapping for AsyncRedisCommandDispatcherErrorCode value: " << aec;
168 TEST_F(ErrorCodesTest, AllErrorCodeEnumsAreMappedToCorrectClientErrorCode)
172 ec = std::error_code();
173 EXPECT_TRUE(ec == shareddatalayer::Error::SUCCESS);
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);
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);
204 TEST_F(ErrorCodesTest, ErrorCodeEnumsDoNotMapToIncorrectClientErrorCode)
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);
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);
237 TEST_F(ErrorCodesTest, AllErrorCodeEnumClassesHaveCategory)
239 EXPECT_STREQ("asyncrediscommanddispatcher",
240 std::error_code(AsyncRedisCommandDispatcherErrorCode::SUCCESS).category().name());
242 EXPECT_STREQ("asyncredisstorage",
243 std::error_code(AsyncRedisStorage::ErrorCode::SUCCESS).category().name());