blob: 3b3083bc15248f1a61e74a7df1eb44faa3202a4e [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
Rolf Badorekef2bf512019-08-20 11:17:15 +030086TEST_F(DatabaseConfigurationImplTest, CanApplyNewAddressesOneByOneAndReturnAllAddresses)
87{
88 databaseConfigurationImpl->checkAndApplyServerAddress("dummydatabaseaddress.local");
89 databaseConfigurationImpl->checkAndApplyServerAddress("10.20.30.40:65535");
90 const auto retAddresses(databaseConfigurationImpl->getServerAddresses());
91 EXPECT_EQ(2U, retAddresses.size());
92 EXPECT_EQ("dummydatabaseaddress.local", retAddresses.at(0).getHost());
93 EXPECT_EQ(6379U, ntohs(retAddresses.at(0).getPort()));
94 EXPECT_EQ("10.20.30.40", retAddresses.at(1).getHost());
95 EXPECT_EQ(65535U, ntohs(retAddresses.at(1).getPort()));
96}
97
98TEST_F(DatabaseConfigurationImplTest, CanThrowIfIllegalDbTypeIsApplied)
99{
100 EXPECT_THROW(databaseConfigurationImpl->checkAndApplyDbType("bad_db_type"), DatabaseConfiguration::InvalidDbType);
101}
102
103TEST_F(DatabaseConfigurationImplTest, CanApplyIPv6AddressAndReturnIt)
104{
105 databaseConfigurationImpl->checkAndApplyServerAddress("[2001::123]:12345");
106 const auto retAddresses(databaseConfigurationImpl->getServerAddresses());
107 EXPECT_EQ(1U, retAddresses.size());
108 EXPECT_EQ("2001::123", retAddresses.at(0).getHost());
109 EXPECT_EQ(12345U, ntohs(retAddresses.at(0).getPort()));
110}
111
112TEST_F(DatabaseConfigurationImplTest, IsEmptyReturnsCorrectInformation)
113{
114 EXPECT_TRUE(databaseConfigurationImpl->isEmpty());
115 databaseConfigurationImpl->checkAndApplyServerAddress("[2001::123]:12345");
116 EXPECT_FALSE(databaseConfigurationImpl->isEmpty());
117}
Rolf Badorek2dcf9402019-10-01 18:33:58 +0300118
119TEST_F(DatabaseConfigurationImplTest, DefaultSentinelAddressIsNone)
120{
121 EXPECT_EQ(boost::none, databaseConfigurationImpl->getSentinelAddress());
122}
123
124TEST_F(DatabaseConfigurationImplTest, CanApplyAndReturnSentinelAddress)
125{
126 databaseConfigurationImpl->checkAndApplySentinelAddress("dummydatabaseaddress.local:1234");
127 auto address = databaseConfigurationImpl->getSentinelAddress();
128 EXPECT_NE(boost::none, databaseConfigurationImpl->getSentinelAddress());
129 EXPECT_EQ("dummydatabaseaddress.local", address->getHost());
130 EXPECT_EQ(1234, ntohs(address->getPort()));
131}
132
133TEST_F(DatabaseConfigurationImplTest, DefaultSentinelMasterNameIsEmpty)
134{
135 EXPECT_EQ("", databaseConfigurationImpl->getSentinelMasterName());
136}
137
138TEST_F(DatabaseConfigurationImplTest, CanApplyAndReturnSentinelMasterName)
139{
140 databaseConfigurationImpl->checkAndApplySentinelMasterName("mymaster");
141 EXPECT_EQ("mymaster", databaseConfigurationImpl->getSentinelMasterName());
142}