Add public helper classes for UT mocking
[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         }
76
77         std::shared_ptr<redis::AsyncDatabaseDiscovery> asyncDatabaseDiscoveryCreator(std::shared_ptr<Engine>,
78                                                                               const DatabaseConfiguration&,
79                                                                               std::shared_ptr<Logger>)
80         {
81             return discoveryMock;
82         }
83
84         void expectNamespaceConfigurationIsDbBackendUseEnabled_returnFalse()
85         {
86             EXPECT_CALL(*namespaceConfigurationsMock, isDbBackendUseEnabled(ns)).
87                 WillOnce(Return(false));
88         }
89
90         void expectNamespaceConfigurationIsDbBackendUseEnabled_returnTrue()
91         {
92             EXPECT_CALL(*namespaceConfigurationsMock, isDbBackendUseEnabled(ns)).
93                 WillOnce(Return(true));
94         }
95
96         void expectPostCallback()
97         {
98             EXPECT_CALL(*engineMock, postCallback(_))
99                 .Times(1);
100         }
101     };
102 }
103
104 TEST_F(AsyncStorageImplTest, IsNotCopyableAndIsNotMovable)
105 {
106     EXPECT_FALSE(std::is_copy_assignable<AsyncStorageImpl>::value);
107     EXPECT_FALSE(std::is_move_assignable<AsyncStorageImpl>::value);
108     EXPECT_FALSE(std::is_copy_constructible<AsyncStorageImpl>::value);
109     EXPECT_FALSE(std::is_move_constructible<AsyncStorageImpl>::value);
110 }
111
112 TEST_F(AsyncStorageImplTest, ImplementsAsyncStorage)
113 {
114     EXPECT_TRUE((std::is_base_of<AsyncStorage, AsyncStorageImpl>::value));
115 }
116
117 TEST_F(AsyncStorageImplTest, CanGetFd)
118 {
119     EXPECT_CALL(*engineMock, fd())
120         .Times(1)
121         .WillOnce(Return(fd));
122     EXPECT_EQ(fd, asyncStorageImpl->fd());
123 }
124
125 TEST_F(AsyncStorageImplTest, CanHandleEvents)
126 {
127     EXPECT_CALL(*engineMock, handleEvents())
128         .Times(1);
129     asyncStorageImpl->handleEvents();
130 }
131
132 TEST_F(AsyncStorageImplTest, CorrectHandlerIsUsedBasedOnConfiguration)
133 {
134     InSequence dummy;
135     expectNamespaceConfigurationIsDbBackendUseEnabled_returnTrue();
136     AsyncStorage& returnedHandler1 = asyncStorageImpl->getOperationHandler(ns);
137     EXPECT_EQ(typeid(AsyncRedisStorage&), typeid(returnedHandler1));
138
139     expectNamespaceConfigurationIsDbBackendUseEnabled_returnFalse();
140     AsyncStorage& returnedHandler2 = asyncStorageImpl->getOperationHandler(ns);
141     EXPECT_EQ(typeid(AsyncDummyStorage&), typeid(returnedHandler2));
142 }