blob: 5a5356c91334daf47d3ecb05996bdf784c6280fe [file] [log] [blame]
Rolf Badorekef2bf512019-08-20 11:17:15 +03001/*
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
Timo Tietavainena0745d22019-11-28 09:55:22 +020017/*
18 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
19 * platform project (RICP).
20*/
21
Rolf Badorekef2bf512019-08-20 11:17:15 +030022#include <gtest/gtest.h>
23#include <gmock/gmock.h>
24#include <arpa/inet.h>
25#include "private/databaseconfigurationimpl.hpp"
26
27using namespace shareddatalayer;
28using namespace testing;
29
30namespace
31{
32 class DatabaseConfigurationImplTest: public testing::Test
33 {
34 public:
35 std::unique_ptr<DatabaseConfigurationImpl> databaseConfigurationImpl;
36
37 DatabaseConfigurationImplTest()
38 {
39 InSequence dummy;
40 databaseConfigurationImpl.reset(new DatabaseConfigurationImpl());
41 }
42 };
43}
44
45TEST_F(DatabaseConfigurationImplTest, CanReturnDefaultAddress)
46{
47 const auto retAddresses(databaseConfigurationImpl->getDefaultServerAddresses());
48 EXPECT_EQ(1U, retAddresses.size());
49 EXPECT_EQ("localhost", retAddresses.back().getHost());
50 EXPECT_EQ(6379U, ntohs(retAddresses.back().getPort()));
51}
52
53TEST_F(DatabaseConfigurationImplTest, CanReturnEmptyAddressListIfNoAddressesAreApplied)
54{
55 const auto retAddresses(databaseConfigurationImpl->getServerAddresses());
56 EXPECT_TRUE(retAddresses.empty());
57}
58
59TEST_F(DatabaseConfigurationImplTest, CanReturnUnknownTypeIfNoRedisDbTypeIsApplied)
60{
61 const auto retDbType(databaseConfigurationImpl->getDbType());
62 EXPECT_EQ(DatabaseConfiguration::DbType::UNKNOWN, retDbType);
63}
64
65TEST_F(DatabaseConfigurationImplTest, CanApplyRedisDbTypeStringAndReturnType)
66{
67 databaseConfigurationImpl->checkAndApplyDbType("redis-standalone");
68 const auto retDbType(databaseConfigurationImpl->getDbType());
69 EXPECT_EQ(DatabaseConfiguration::DbType::REDIS_STANDALONE, retDbType);
70}
71
72TEST_F(DatabaseConfigurationImplTest, CanApplyRedisClusterDbTypeStringAndReturnType)
73{
74 databaseConfigurationImpl->checkAndApplyDbType("redis-cluster");
75 const auto retDbType(databaseConfigurationImpl->getDbType());
76 EXPECT_EQ(DatabaseConfiguration::DbType::REDIS_CLUSTER, retDbType);
77}
78
Rolf Badorek2dcf9402019-10-01 18:33:58 +030079TEST_F(DatabaseConfigurationImplTest, CanApplyRedisSentinelDbTypeStringAndReturnType)
80{
81 databaseConfigurationImpl->checkAndApplyDbType("redis-sentinel");
82 const auto retDbType(databaseConfigurationImpl->getDbType());
83 EXPECT_EQ(DatabaseConfiguration::DbType::REDIS_SENTINEL, retDbType);
84}
85
Petri Ovaskaece67082021-04-15 11:08:13 +030086TEST_F(DatabaseConfigurationImplTest, CanApplySdlClusterDbTypeStringAndReturnType)
87{
88 databaseConfigurationImpl->checkAndApplyDbType("sdl-cluster");
89 const auto retDbType(databaseConfigurationImpl->getDbType());
90 EXPECT_EQ(DatabaseConfiguration::DbType::SDL_CLUSTER, retDbType);
91}
92
Rolf Badorekef2bf512019-08-20 11:17:15 +030093TEST_F(DatabaseConfigurationImplTest, CanApplyNewAddressesOneByOneAndReturnAllAddresses)
94{
95 databaseConfigurationImpl->checkAndApplyServerAddress("dummydatabaseaddress.local");
96 databaseConfigurationImpl->checkAndApplyServerAddress("10.20.30.40:65535");
97 const auto retAddresses(databaseConfigurationImpl->getServerAddresses());
98 EXPECT_EQ(2U, retAddresses.size());
99 EXPECT_EQ("dummydatabaseaddress.local", retAddresses.at(0).getHost());
100 EXPECT_EQ(6379U, ntohs(retAddresses.at(0).getPort()));
101 EXPECT_EQ("10.20.30.40", retAddresses.at(1).getHost());
102 EXPECT_EQ(65535U, ntohs(retAddresses.at(1).getPort()));
103}
104
105TEST_F(DatabaseConfigurationImplTest, CanThrowIfIllegalDbTypeIsApplied)
106{
107 EXPECT_THROW(databaseConfigurationImpl->checkAndApplyDbType("bad_db_type"), DatabaseConfiguration::InvalidDbType);
108}
109
110TEST_F(DatabaseConfigurationImplTest, CanApplyIPv6AddressAndReturnIt)
111{
112 databaseConfigurationImpl->checkAndApplyServerAddress("[2001::123]:12345");
113 const auto retAddresses(databaseConfigurationImpl->getServerAddresses());
114 EXPECT_EQ(1U, retAddresses.size());
115 EXPECT_EQ("2001::123", retAddresses.at(0).getHost());
116 EXPECT_EQ(12345U, ntohs(retAddresses.at(0).getPort()));
117}
118
119TEST_F(DatabaseConfigurationImplTest, IsEmptyReturnsCorrectInformation)
120{
121 EXPECT_TRUE(databaseConfigurationImpl->isEmpty());
122 databaseConfigurationImpl->checkAndApplyServerAddress("[2001::123]:12345");
123 EXPECT_FALSE(databaseConfigurationImpl->isEmpty());
124}
Rolf Badorek2dcf9402019-10-01 18:33:58 +0300125
126TEST_F(DatabaseConfigurationImplTest, DefaultSentinelAddressIsNone)
127{
Petri Ovaskaece67082021-04-15 11:08:13 +0300128 EXPECT_EQ(boost::none, databaseConfigurationImpl->getSentinelAddress(boost::none));
Rolf Badorek2dcf9402019-10-01 18:33:58 +0300129}
130
131TEST_F(DatabaseConfigurationImplTest, CanApplyAndReturnSentinelAddress)
132{
133 databaseConfigurationImpl->checkAndApplySentinelAddress("dummydatabaseaddress.local:1234");
Petri Ovaskaece67082021-04-15 11:08:13 +0300134 auto address = databaseConfigurationImpl->getSentinelAddress(boost::none);
135 EXPECT_NE(boost::none, databaseConfigurationImpl->getSentinelAddress(boost::none));
Rolf Badorek2dcf9402019-10-01 18:33:58 +0300136 EXPECT_EQ("dummydatabaseaddress.local", address->getHost());
137 EXPECT_EQ(1234, ntohs(address->getPort()));
138}
139
140TEST_F(DatabaseConfigurationImplTest, DefaultSentinelMasterNameIsEmpty)
141{
142 EXPECT_EQ("", databaseConfigurationImpl->getSentinelMasterName());
143}
144
145TEST_F(DatabaseConfigurationImplTest, CanApplyAndReturnSentinelMasterName)
146{
147 databaseConfigurationImpl->checkAndApplySentinelMasterName("mymaster");
148 EXPECT_EQ("mymaster", databaseConfigurationImpl->getSentinelMasterName());
149}
Petri Ovaskaece67082021-04-15 11:08:13 +0300150
151TEST_F(DatabaseConfigurationImplTest, CanReturnSDLClusterAddress)
152{
153 databaseConfigurationImpl->checkAndApplyDbType("sdl-cluster");
154 databaseConfigurationImpl->checkAndApplyServerAddress("cluster-0.local");
155 databaseConfigurationImpl->checkAndApplyServerAddress("cluster-1.local");
156 databaseConfigurationImpl->checkAndApplyServerAddress("cluster-2.local");
157 databaseConfigurationImpl->checkAndApplySentinelAddress("cluster-0.local:54321");
158 auto address0 = databaseConfigurationImpl->getSentinelAddress(0);
159 auto address1 = databaseConfigurationImpl->getSentinelAddress(1);
160 auto address2 = databaseConfigurationImpl->getSentinelAddress(2);
161 EXPECT_NE(boost::none, databaseConfigurationImpl->getSentinelAddress(0));
162 EXPECT_NE(boost::none, databaseConfigurationImpl->getSentinelAddress(1));
163 EXPECT_NE(boost::none, databaseConfigurationImpl->getSentinelAddress(2));
164 EXPECT_EQ("cluster-0.local", address0->getHost());
165 EXPECT_EQ("cluster-1.local", address1->getHost());
166 EXPECT_EQ("cluster-2.local", address2->getHost());
167 EXPECT_EQ(54321, ntohs(address0->getPort()));
168}
169
170TEST_F(DatabaseConfigurationImplTest, CanReturnDefaultPortForSDLClusterAddress)
171{
172 databaseConfigurationImpl->checkAndApplyServerAddress("cluster-0.local");
173 databaseConfigurationImpl->checkAndApplySentinelAddress("cluster-0.local");
174 auto address0 = databaseConfigurationImpl->getSentinelAddress(0);
175 EXPECT_NE(boost::none, databaseConfigurationImpl->getSentinelAddress(0));
176 EXPECT_EQ("cluster-0.local", address0->getHost());
177 EXPECT_EQ(26379, ntohs(address0->getPort()));
178}