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)));
77 std::shared_ptr<redis::AsyncDatabaseDiscovery> asyncDatabaseDiscoveryCreator(std::shared_ptr<Engine>,
78 const DatabaseConfiguration&,
79 std::shared_ptr<Logger>)
84 void expectNamespaceConfigurationIsDbBackendUseEnabled_returnFalse()
86 EXPECT_CALL(*namespaceConfigurationsMock, isDbBackendUseEnabled(ns)).
87 WillOnce(Return(false));
90 void expectNamespaceConfigurationIsDbBackendUseEnabled_returnTrue()
92 EXPECT_CALL(*namespaceConfigurationsMock, isDbBackendUseEnabled(ns)).
93 WillOnce(Return(true));
96 void expectPostCallback()
98 EXPECT_CALL(*engineMock, postCallback(_))
104 TEST_F(AsyncStorageImplTest, IsNotCopyableAndIsNotMovable)
106 EXPECT_FALSE(std::is_copy_assignable<AsyncStorageImpl>::value);
107 EXPECT_FALSE(std::is_move_assignable<AsyncStorageImpl>::value);
108 EXPECT_FALSE(std::is_copy_constructible<AsyncStorageImpl>::value);
109 EXPECT_FALSE(std::is_move_constructible<AsyncStorageImpl>::value);
112 TEST_F(AsyncStorageImplTest, ImplementsAsyncStorage)
114 EXPECT_TRUE((std::is_base_of<AsyncStorage, AsyncStorageImpl>::value));
117 TEST_F(AsyncStorageImplTest, CanGetFd)
119 EXPECT_CALL(*engineMock, fd())
121 .WillOnce(Return(fd));
122 EXPECT_EQ(fd, asyncStorageImpl->fd());
125 TEST_F(AsyncStorageImplTest, CanHandleEvents)
127 EXPECT_CALL(*engineMock, handleEvents())
129 asyncStorageImpl->handleEvents();
132 TEST_F(AsyncStorageImplTest, CorrectHandlerIsUsedBasedOnConfiguration)
135 expectNamespaceConfigurationIsDbBackendUseEnabled_returnTrue();
136 AsyncStorage& returnedHandler1 = asyncStorageImpl->getOperationHandler(ns);
137 EXPECT_EQ(typeid(AsyncRedisStorage&), typeid(returnedHandler1));
139 expectNamespaceConfigurationIsDbBackendUseEnabled_returnFalse();
140 AsyncStorage& returnedHandler2 = asyncStorageImpl->getOperationHandler(ns);
141 EXPECT_EQ(typeid(AsyncDummyStorage&), typeid(returnedHandler2));