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 <gtest/gtest.h>
23 #include <type_traits>
25 #include "private/asyncdummystorage.hpp"
26 #include "private/asyncstorageimpl.hpp"
27 #include "private/createlogger.hpp"
28 #include "private/logger.hpp"
29 #include "private/redis/asyncredisstorage.hpp"
30 #include "private/tst/enginemock.hpp"
31 #include "private/tst/asyncdatabasediscoverymock.hpp"
32 #include "private/tst/databaseconfigurationmock.hpp"
33 #include "private/tst/namespaceconfigurationsmock.hpp"
35 using namespace shareddatalayer;
36 using namespace shareddatalayer::tst;
37 using namespace testing;
41 class AsyncStorageImplTest: public testing::Test
44 std::shared_ptr<StrictMock<EngineMock>> engineMock;
45 std::shared_ptr<DatabaseConfiguration> dummyDatabaseConfiguration;
46 std::shared_ptr<StrictMock<NamespaceConfigurationsMock>> namespaceConfigurationsMock;
47 std::shared_ptr<NiceMock<AsyncDatabaseDiscoveryMock>> discoveryMock;
49 AsyncStorage::Namespace ns;
50 Engine::Callback storedCallback;
51 std::unique_ptr<AsyncStorageImpl> asyncStorageImpl;
52 std::shared_ptr<Logger> logger;
54 AsyncStorageImplTest():
55 engineMock(std::make_shared<StrictMock<EngineMock>>()),
56 dummyDatabaseConfiguration(std::make_shared<DatabaseConfigurationImpl>()),
57 namespaceConfigurationsMock(std::make_shared<StrictMock<NamespaceConfigurationsMock>>()),
58 discoveryMock(std::make_shared<NiceMock<AsyncDatabaseDiscoveryMock>>()),
60 ns("someKnownNamespace"),
61 logger(createLogger(SDL_LOG_PREFIX))
63 dummyDatabaseConfiguration->checkAndApplyDbType("redis-standalone");
64 dummyDatabaseConfiguration->checkAndApplyServerAddress("dummydatabaseaddress.local");
65 asyncStorageImpl.reset(new AsyncStorageImpl(engineMock,
67 dummyDatabaseConfiguration,
68 namespaceConfigurationsMock,
70 std::bind(&AsyncStorageImplTest::asyncDatabaseDiscoveryCreator,
72 std::placeholders::_1,
73 std::placeholders::_2,
74 std::placeholders::_3,
75 std::placeholders::_4,
76 std::placeholders::_5)));
79 std::shared_ptr<redis::AsyncDatabaseDiscovery> asyncDatabaseDiscoveryCreator(std::shared_ptr<Engine>,
81 const DatabaseConfiguration&,
82 const boost::optional<std::size_t>&,
83 std::shared_ptr<Logger>)
88 void expectNamespaceConfigurationIsDbBackendUseEnabled_returnFalse()
90 EXPECT_CALL(*namespaceConfigurationsMock, isDbBackendUseEnabled(ns)).
91 WillOnce(Return(false));
94 void expectNamespaceConfigurationIsDbBackendUseEnabled_returnTrue()
96 EXPECT_CALL(*namespaceConfigurationsMock, isDbBackendUseEnabled(ns)).
97 WillOnce(Return(true));
100 void expectPostCallback()
102 EXPECT_CALL(*engineMock, postCallback(_))
108 TEST_F(AsyncStorageImplTest, IsNotCopyableAndIsNotMovable)
110 EXPECT_FALSE(std::is_copy_assignable<AsyncStorageImpl>::value);
111 EXPECT_FALSE(std::is_move_assignable<AsyncStorageImpl>::value);
112 EXPECT_FALSE(std::is_copy_constructible<AsyncStorageImpl>::value);
113 EXPECT_FALSE(std::is_move_constructible<AsyncStorageImpl>::value);
116 TEST_F(AsyncStorageImplTest, ImplementsAsyncStorage)
118 EXPECT_TRUE((std::is_base_of<AsyncStorage, AsyncStorageImpl>::value));
121 TEST_F(AsyncStorageImplTest, CanGetFd)
123 EXPECT_CALL(*engineMock, fd())
125 .WillOnce(Return(fd));
126 EXPECT_EQ(fd, asyncStorageImpl->fd());
129 TEST_F(AsyncStorageImplTest, CanHandleEvents)
131 EXPECT_CALL(*engineMock, handleEvents())
133 asyncStorageImpl->handleEvents();
136 TEST_F(AsyncStorageImplTest, CorrectHandlerIsUsedBasedOnConfiguration)
139 expectNamespaceConfigurationIsDbBackendUseEnabled_returnTrue();
140 AsyncStorage& returnedHandler1 = asyncStorageImpl->getOperationHandler(ns);
141 EXPECT_EQ(typeid(AsyncRedisStorage&), typeid(returnedHandler1));
143 expectNamespaceConfigurationIsDbBackendUseEnabled_returnFalse();
144 AsyncStorage& returnedHandler2 = asyncStorageImpl->getOperationHandler(ns);
145 EXPECT_EQ(typeid(AsyncDummyStorage&), typeid(returnedHandler2));
148 TEST_F(AsyncStorageImplTest, CorrectSdlClusterHandlerIsUsedBasedOnConfiguration)
150 expectNamespaceConfigurationIsDbBackendUseEnabled_returnTrue();
151 dummyDatabaseConfiguration->checkAndApplyDbType("sdl-sentinel-cluster");
152 AsyncStorage& returnedHandler = asyncStorageImpl->getOperationHandler(ns);
153 EXPECT_EQ(typeid(AsyncRedisStorage&), typeid(returnedHandler));