RIC:1060: Change in PTL
[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 /*
18  * This source code is part of the near-RT RIC (RAN Intelligent Controller)
19  * platform project (RICP).
20 */
21
22 #include <gtest/gtest.h>
23 #include <type_traits>
24 #include "config.h"
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"
34
35 using namespace shareddatalayer;
36 using namespace shareddatalayer::tst;
37 using namespace testing;
38
39 namespace
40 {
41     class AsyncStorageImplTest: public testing::Test
42     {
43     public:
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;
48         int fd;
49         AsyncStorage::Namespace ns;
50         Engine::Callback storedCallback;
51         std::unique_ptr<AsyncStorageImpl> asyncStorageImpl;
52         std::shared_ptr<Logger> logger;
53
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>>()),
59             fd(10),
60             ns("someKnownNamespace"),
61             logger(createLogger(SDL_LOG_PREFIX))
62         {
63             dummyDatabaseConfiguration->checkAndApplyDbType("redis-standalone");
64             dummyDatabaseConfiguration->checkAndApplyServerAddress("dummydatabaseaddress.local");
65             asyncStorageImpl.reset(new AsyncStorageImpl(engineMock,
66                                                         boost::none,
67                                                         dummyDatabaseConfiguration,
68                                                         namespaceConfigurationsMock,
69                                                         logger,
70                                                         std::bind(&AsyncStorageImplTest::asyncDatabaseDiscoveryCreator,
71                                                                   this,
72                                                                   std::placeholders::_1,
73                                                                   std::placeholders::_2,
74                                                                   std::placeholders::_3,
75                                                                   std::placeholders::_4,
76                                                                   std::placeholders::_5)));
77         }
78
79         std::shared_ptr<redis::AsyncDatabaseDiscovery> asyncDatabaseDiscoveryCreator(std::shared_ptr<Engine>,
80                                                                                      const std::string&,
81                                                                                      const DatabaseConfiguration&,
82                                                                                      const boost::optional<std::size_t>&,
83                                                                                      std::shared_ptr<Logger>)
84         {
85             return discoveryMock;
86         }
87
88         void expectNamespaceConfigurationIsDbBackendUseEnabled_returnFalse()
89         {
90             EXPECT_CALL(*namespaceConfigurationsMock, isDbBackendUseEnabled(ns)).
91                 WillOnce(Return(false));
92         }
93
94         void expectNamespaceConfigurationIsDbBackendUseEnabled_returnTrue()
95         {
96             EXPECT_CALL(*namespaceConfigurationsMock, isDbBackendUseEnabled(ns)).
97                 WillOnce(Return(true));
98         }
99
100         void expectPostCallback()
101         {
102             EXPECT_CALL(*engineMock, postCallback(_))
103                 .Times(1);
104         }
105     };
106 }
107
108 TEST_F(AsyncStorageImplTest, IsNotCopyableAndIsNotMovable)
109 {
110     EXPECT_FALSE(std::is_copy_assignable<AsyncStorageImpl>::value);
111     EXPECT_FALSE(std::is_move_assignable<AsyncStorageImpl>::value);
112     EXPECT_FALSE(std::is_copy_constructible<AsyncStorageImpl>::value);
113     EXPECT_FALSE(std::is_move_constructible<AsyncStorageImpl>::value);
114 }
115
116 TEST_F(AsyncStorageImplTest, ImplementsAsyncStorage)
117 {
118     EXPECT_TRUE((std::is_base_of<AsyncStorage, AsyncStorageImpl>::value));
119 }
120
121 TEST_F(AsyncStorageImplTest, CanGetFd)
122 {
123     EXPECT_CALL(*engineMock, fd())
124         .Times(1)
125         .WillOnce(Return(fd));
126     EXPECT_EQ(fd, asyncStorageImpl->fd());
127 }
128
129 TEST_F(AsyncStorageImplTest, CanHandleEvents)
130 {
131     EXPECT_CALL(*engineMock, handleEvents())
132         .Times(1);
133     asyncStorageImpl->handleEvents();
134 }
135
136 TEST_F(AsyncStorageImplTest, CorrectHandlerIsUsedBasedOnConfiguration)
137 {
138     InSequence dummy;
139     expectNamespaceConfigurationIsDbBackendUseEnabled_returnTrue();
140     AsyncStorage& returnedHandler1 = asyncStorageImpl->getOperationHandler(ns);
141     EXPECT_EQ(typeid(AsyncRedisStorage&), typeid(returnedHandler1));
142
143     expectNamespaceConfigurationIsDbBackendUseEnabled_returnFalse();
144     AsyncStorage& returnedHandler2 = asyncStorageImpl->getOperationHandler(ns);
145     EXPECT_EQ(typeid(AsyncDummyStorage&), typeid(returnedHandler2));
146 }
147
148 TEST_F(AsyncStorageImplTest, CorrectSdlClusterHandlerIsUsedBasedOnConfiguration)
149 {
150     expectNamespaceConfigurationIsDbBackendUseEnabled_returnTrue();
151     dummyDatabaseConfiguration->checkAndApplyDbType("sdl-sentinel-cluster");
152     AsyncStorage& returnedHandler = asyncStorageImpl->getOperationHandler(ns);
153     EXPECT_EQ(typeid(AsyncRedisStorage&), typeid(returnedHandler));
154 }