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.
17 #include <gtest/gtest.h>
18 #include <type_traits>
20 #include "private/asyncdummystorage.hpp"
21 #include "private/asyncstorageimpl.hpp"
22 #include "private/createlogger.hpp"
23 #include "private/logger.hpp"
24 #include "private/redis/asyncredisstorage.hpp"
25 #include "private/tst/enginemock.hpp"
26 #include "private/tst/asyncdatabasediscoverymock.hpp"
27 #include "private/tst/databaseconfigurationmock.hpp"
28 #include "private/tst/namespaceconfigurationsmock.hpp"
30 using namespace shareddatalayer;
31 using namespace shareddatalayer::tst;
32 using namespace testing;
36 class AsyncStorageImplTest: public testing::Test
39 std::shared_ptr<StrictMock<EngineMock>> engineMock;
40 std::shared_ptr<DatabaseConfiguration> dummyDatabaseConfiguration;
41 std::shared_ptr<StrictMock<NamespaceConfigurationsMock>> namespaceConfigurationsMock;
42 std::shared_ptr<NiceMock<AsyncDatabaseDiscoveryMock>> discoveryMock;
44 AsyncStorage::Namespace ns;
45 Engine::Callback storedCallback;
46 std::unique_ptr<AsyncStorageImpl> asyncStorageImpl;
47 std::shared_ptr<Logger> logger;
49 AsyncStorageImplTest():
50 engineMock(std::make_shared<StrictMock<EngineMock>>()),
51 dummyDatabaseConfiguration(std::make_shared<DatabaseConfigurationImpl>()),
52 namespaceConfigurationsMock(std::make_shared<StrictMock<NamespaceConfigurationsMock>>()),
53 discoveryMock(std::make_shared<NiceMock<AsyncDatabaseDiscoveryMock>>()),
55 ns("someKnownNamespace"),
56 logger(createLogger(SDL_LOG_PREFIX))
58 dummyDatabaseConfiguration->checkAndApplyDbType("redis-standalone");
59 dummyDatabaseConfiguration->checkAndApplyServerAddress("dummydatabaseaddress.local");
60 asyncStorageImpl.reset(new AsyncStorageImpl(engineMock,
62 dummyDatabaseConfiguration,
63 namespaceConfigurationsMock,
65 std::bind(&AsyncStorageImplTest::asyncDatabaseDiscoveryCreator,
67 std::placeholders::_1,
68 std::placeholders::_2,
69 std::placeholders::_3)));
72 std::shared_ptr<redis::AsyncDatabaseDiscovery> asyncDatabaseDiscoveryCreator(std::shared_ptr<Engine>,
73 const DatabaseConfiguration&,
74 std::shared_ptr<Logger>)
79 void expectNamespaceConfigurationIsDbBackendUseEnabled_returnFalse()
81 EXPECT_CALL(*namespaceConfigurationsMock, isDbBackendUseEnabled(ns)).
82 WillOnce(Return(false));
85 void expectNamespaceConfigurationIsDbBackendUseEnabled_returnTrue()
87 EXPECT_CALL(*namespaceConfigurationsMock, isDbBackendUseEnabled(ns)).
88 WillOnce(Return(true));
91 void expectPostCallback()
93 EXPECT_CALL(*engineMock, postCallback(_))
99 TEST_F(AsyncStorageImplTest, IsNotCopyableAndIsNotMovable)
101 EXPECT_FALSE(std::is_copy_assignable<AsyncStorageImpl>::value);
102 EXPECT_FALSE(std::is_move_assignable<AsyncStorageImpl>::value);
103 EXPECT_FALSE(std::is_copy_constructible<AsyncStorageImpl>::value);
104 EXPECT_FALSE(std::is_move_constructible<AsyncStorageImpl>::value);
107 TEST_F(AsyncStorageImplTest, ImplementsAsyncStorage)
109 EXPECT_TRUE((std::is_base_of<AsyncStorage, AsyncStorageImpl>::value));
112 TEST_F(AsyncStorageImplTest, CanGetFd)
114 EXPECT_CALL(*engineMock, fd())
116 .WillOnce(Return(fd));
117 EXPECT_EQ(fd, asyncStorageImpl->fd());
120 TEST_F(AsyncStorageImplTest, CanHandleEvents)
122 EXPECT_CALL(*engineMock, handleEvents())
124 asyncStorageImpl->handleEvents();
127 TEST_F(AsyncStorageImplTest, CorrectHandlerIsUsedBasedOnConfiguration)
130 expectNamespaceConfigurationIsDbBackendUseEnabled_returnTrue();
131 AsyncStorage& returnedHandler1 = asyncStorageImpl->getOperationHandler(ns);
132 EXPECT_EQ(typeid(AsyncRedisStorage&), typeid(returnedHandler1));
134 expectNamespaceConfigurationIsDbBackendUseEnabled_returnFalse();
135 AsyncStorage& returnedHandler2 = asyncStorageImpl->getOperationHandler(ns);
136 EXPECT_EQ(typeid(AsyncDummyStorage&), typeid(returnedHandler2));