b6a2319843b46e2ae75021eb48b3450ef9460d77
[ric-plt/sdl.git] / tst / asyncstorageimpl_test.cpp
1 /*
2    Copyright (c) 2018-2019 Nokia.
3
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
7
8        http://www.apache.org/licenses/LICENSE-2.0
9
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.
15 */
16
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"
27
28 using namespace shareddatalayer;
29 using namespace shareddatalayer::tst;
30 using namespace testing;
31
32 namespace
33 {
34     class AsyncStorageImplTest: public testing::Test
35     {
36     public:
37         std::shared_ptr<StrictMock<EngineMock>> engineMock;
38         std::shared_ptr<DatabaseConfiguration> dummyDatabaseConfiguration;
39         std::shared_ptr<StrictMock<NamespaceConfigurationsMock>> namespaceConfigurationsMock;
40         int fd;
41         AsyncStorage::Namespace ns;
42         Engine::Callback storedCallback;
43         std::unique_ptr<AsyncStorageImpl> asyncStorageImpl;
44         std::shared_ptr<Logger> logger;
45
46         AsyncStorageImplTest():
47             engineMock(std::make_shared<StrictMock<EngineMock>>()),
48             dummyDatabaseConfiguration(std::make_shared<DatabaseConfigurationImpl>()),
49             namespaceConfigurationsMock(std::make_shared<StrictMock<NamespaceConfigurationsMock>>()),
50             fd(10),
51             ns("someKnownNamespace"),
52             logger(createLogger(SDL_LOG_PREFIX))
53         {
54             dummyDatabaseConfiguration->checkAndApplyDbType("redis-standalone");
55             dummyDatabaseConfiguration->checkAndApplyServerAddress("dummydatabaseaddress.local");
56             asyncStorageImpl.reset(new AsyncStorageImpl(engineMock, boost::none, dummyDatabaseConfiguration, namespaceConfigurationsMock, logger));
57         }
58
59         void expectNamespaceConfigurationIsDbBackendUseEnabled_returnFalse()
60         {
61             EXPECT_CALL(*namespaceConfigurationsMock, isDbBackendUseEnabled(ns)).
62                 WillOnce(Return(false));
63         }
64
65         void expectNamespaceConfigurationIsDbBackendUseEnabled_returnTrue()
66         {
67             EXPECT_CALL(*namespaceConfigurationsMock, isDbBackendUseEnabled(ns)).
68                 WillOnce(Return(true));
69         }
70
71         void expectPostCallback()
72         {
73             EXPECT_CALL(*engineMock, postCallback(_))
74                 .Times(1);
75         }
76     };
77 }
78
79 TEST_F(AsyncStorageImplTest, IsNotCopyableAndIsNotMovable)
80 {
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);
85 }
86
87 TEST_F(AsyncStorageImplTest, ImplementsAsyncStorage)
88 {
89     EXPECT_TRUE((std::is_base_of<AsyncStorage, AsyncStorageImpl>::value));
90 }
91
92 TEST_F(AsyncStorageImplTest, CanGetFd)
93 {
94     EXPECT_CALL(*engineMock, fd())
95         .Times(1)
96         .WillOnce(Return(fd));
97     EXPECT_EQ(fd, asyncStorageImpl->fd());
98 }
99
100 TEST_F(AsyncStorageImplTest, CanHandleEvents)
101 {
102     EXPECT_CALL(*engineMock, handleEvents())
103         .Times(1);
104     asyncStorageImpl->handleEvents();
105 }
106
107 TEST_F(AsyncStorageImplTest, CorrectHandlerIsUsedBasedOnConfiguration)
108 {
109     InSequence dummy;
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));
115
116     expectNamespaceConfigurationIsDbBackendUseEnabled_returnFalse();
117     AsyncStorage& returnedHandler2 = asyncStorageImpl->getOperationHandler(ns);
118     EXPECT_EQ(typeid(AsyncDummyStorage&), typeid(returnedHandler2));
119 }