Add Redis Sentinel based database discovery
[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 "config.h"
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"
29
30 using namespace shareddatalayer;
31 using namespace shareddatalayer::tst;
32 using namespace testing;
33
34 namespace
35 {
36     class AsyncStorageImplTest: public testing::Test
37     {
38     public:
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;
43         int fd;
44         AsyncStorage::Namespace ns;
45         Engine::Callback storedCallback;
46         std::unique_ptr<AsyncStorageImpl> asyncStorageImpl;
47         std::shared_ptr<Logger> logger;
48
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>>()),
54             fd(10),
55             ns("someKnownNamespace"),
56             logger(createLogger(SDL_LOG_PREFIX))
57         {
58             dummyDatabaseConfiguration->checkAndApplyDbType("redis-standalone");
59             dummyDatabaseConfiguration->checkAndApplyServerAddress("dummydatabaseaddress.local");
60             asyncStorageImpl.reset(new AsyncStorageImpl(engineMock,
61                                                         boost::none,
62                                                         dummyDatabaseConfiguration,
63                                                         namespaceConfigurationsMock,
64                                                         logger,
65                                                         std::bind(&AsyncStorageImplTest::asyncDatabaseDiscoveryCreator,
66                                                                   this,
67                                                                   std::placeholders::_1,
68                                                                   std::placeholders::_2,
69                                                                   std::placeholders::_3)));
70         }
71
72         std::shared_ptr<redis::AsyncDatabaseDiscovery> asyncDatabaseDiscoveryCreator(std::shared_ptr<Engine>,
73                                                                               const DatabaseConfiguration&,
74                                                                               std::shared_ptr<Logger>)
75         {
76             return discoveryMock;
77         }
78
79         void expectNamespaceConfigurationIsDbBackendUseEnabled_returnFalse()
80         {
81             EXPECT_CALL(*namespaceConfigurationsMock, isDbBackendUseEnabled(ns)).
82                 WillOnce(Return(false));
83         }
84
85         void expectNamespaceConfigurationIsDbBackendUseEnabled_returnTrue()
86         {
87             EXPECT_CALL(*namespaceConfigurationsMock, isDbBackendUseEnabled(ns)).
88                 WillOnce(Return(true));
89         }
90
91         void expectPostCallback()
92         {
93             EXPECT_CALL(*engineMock, postCallback(_))
94                 .Times(1);
95         }
96     };
97 }
98
99 TEST_F(AsyncStorageImplTest, IsNotCopyableAndIsNotMovable)
100 {
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);
105 }
106
107 TEST_F(AsyncStorageImplTest, ImplementsAsyncStorage)
108 {
109     EXPECT_TRUE((std::is_base_of<AsyncStorage, AsyncStorageImpl>::value));
110 }
111
112 TEST_F(AsyncStorageImplTest, CanGetFd)
113 {
114     EXPECT_CALL(*engineMock, fd())
115         .Times(1)
116         .WillOnce(Return(fd));
117     EXPECT_EQ(fd, asyncStorageImpl->fd());
118 }
119
120 TEST_F(AsyncStorageImplTest, CanHandleEvents)
121 {
122     EXPECT_CALL(*engineMock, handleEvents())
123         .Times(1);
124     asyncStorageImpl->handleEvents();
125 }
126
127 TEST_F(AsyncStorageImplTest, CorrectHandlerIsUsedBasedOnConfiguration)
128 {
129     InSequence dummy;
130     expectNamespaceConfigurationIsDbBackendUseEnabled_returnTrue();
131     AsyncStorage& returnedHandler1 = asyncStorageImpl->getOperationHandler(ns);
132     EXPECT_EQ(typeid(AsyncRedisStorage&), typeid(returnedHandler1));
133
134     expectNamespaceConfigurationIsDbBackendUseEnabled_returnFalse();
135     AsyncStorage& returnedHandler2 = asyncStorageImpl->getOperationHandler(ns);
136     EXPECT_EQ(typeid(AsyncDummyStorage&), typeid(returnedHandler2));
137 }