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>
19 #include "private/asyncdummystorage.hpp"
20 #include "private/asyncstorageimpl.hpp"
21 #include "private/createlogger.hpp"
22 #include "private/logger.hpp"
23 #include "private/redis/asyncredisstorage.hpp"
24 #include "private/tst/enginemock.hpp"
25 #include "private/tst/databaseconfigurationmock.hpp"
26 #include "private/tst/namespaceconfigurationsmock.hpp"
28 using namespace shareddatalayer;
29 using namespace shareddatalayer::tst;
30 using namespace testing;
34 class AsyncStorageImplTest: public testing::Test
37 std::shared_ptr<StrictMock<EngineMock>> engineMock;
38 std::shared_ptr<DatabaseConfiguration> dummyDatabaseConfiguration;
39 std::shared_ptr<StrictMock<NamespaceConfigurationsMock>> namespaceConfigurationsMock;
41 AsyncStorage::Namespace ns;
42 Engine::Callback storedCallback;
43 std::unique_ptr<AsyncStorageImpl> asyncStorageImpl;
44 std::shared_ptr<Logger> logger;
46 AsyncStorageImplTest():
47 engineMock(std::make_shared<StrictMock<EngineMock>>()),
48 dummyDatabaseConfiguration(std::make_shared<DatabaseConfigurationImpl>()),
49 namespaceConfigurationsMock(std::make_shared<StrictMock<NamespaceConfigurationsMock>>()),
51 ns("someKnownNamespace"),
52 logger(createLogger(SDL_LOG_PREFIX))
54 dummyDatabaseConfiguration->checkAndApplyDbType("redis-standalone");
55 dummyDatabaseConfiguration->checkAndApplyServerAddress("dummydatabaseaddress.local");
56 asyncStorageImpl.reset(new AsyncStorageImpl(engineMock, boost::none, dummyDatabaseConfiguration, namespaceConfigurationsMock, logger));
59 void expectNamespaceConfigurationIsDbBackendUseEnabled_returnFalse()
61 EXPECT_CALL(*namespaceConfigurationsMock, isDbBackendUseEnabled(ns)).
62 WillOnce(Return(false));
65 void expectNamespaceConfigurationIsDbBackendUseEnabled_returnTrue()
67 EXPECT_CALL(*namespaceConfigurationsMock, isDbBackendUseEnabled(ns)).
68 WillOnce(Return(true));
71 void expectPostCallback()
73 EXPECT_CALL(*engineMock, postCallback(_))
79 TEST_F(AsyncStorageImplTest, IsNotCopyableAndIsNotMovable)
81 EXPECT_FALSE(std::is_copy_assignable<AsyncStorageImpl>::value);
82 EXPECT_FALSE(std::is_move_assignable<AsyncStorageImpl>::value);
83 EXPECT_FALSE(std::is_copy_constructible<AsyncStorageImpl>::value);
84 EXPECT_FALSE(std::is_move_constructible<AsyncStorageImpl>::value);
87 TEST_F(AsyncStorageImplTest, ImplementsAsyncStorage)
89 EXPECT_TRUE((std::is_base_of<AsyncStorage, AsyncStorageImpl>::value));
92 TEST_F(AsyncStorageImplTest, CanGetFd)
94 EXPECT_CALL(*engineMock, fd())
96 .WillOnce(Return(fd));
97 EXPECT_EQ(fd, asyncStorageImpl->fd());
100 TEST_F(AsyncStorageImplTest, CanHandleEvents)
102 EXPECT_CALL(*engineMock, handleEvents())
104 asyncStorageImpl->handleEvents();
107 TEST_F(AsyncStorageImplTest, CorrectHandlerIsUsedBasedOnConfiguration)
110 expectNamespaceConfigurationIsDbBackendUseEnabled_returnTrue();
111 //AsyncRedisStorage creation causes AsyncHiredisDatabaseDiscovery to post stateChanged callback
112 expectPostCallback();
113 AsyncStorage& returnedHandler1 = asyncStorageImpl->getOperationHandler(ns);
114 EXPECT_EQ(typeid(AsyncRedisStorage&), typeid(returnedHandler1));
116 expectNamespaceConfigurationIsDbBackendUseEnabled_returnFalse();
117 AsyncStorage& returnedHandler2 = asyncStorageImpl->getOperationHandler(ns);
118 EXPECT_EQ(typeid(AsyncDummyStorage&), typeid(returnedHandler2));