Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 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 | |
Timo Tietavainen | a0745d2 | 2019-11-28 09:55:22 +0200 | [diff] [blame^] | 17 | /* |
| 18 | * This source code is part of the near-RT RIC (RAN Intelligent Controller) |
| 19 | * platform project (RICP). |
| 20 | */ |
| 21 | |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 22 | #include <gtest/gtest.h> |
| 23 | #include <gmock/gmock.h> |
| 24 | #include <arpa/inet.h> |
| 25 | #include "private/databaseconfigurationimpl.hpp" |
| 26 | |
| 27 | using namespace shareddatalayer; |
| 28 | using namespace testing; |
| 29 | |
| 30 | namespace |
| 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 | |
| 45 | TEST_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 | |
| 53 | TEST_F(DatabaseConfigurationImplTest, CanReturnEmptyAddressListIfNoAddressesAreApplied) |
| 54 | { |
| 55 | const auto retAddresses(databaseConfigurationImpl->getServerAddresses()); |
| 56 | EXPECT_TRUE(retAddresses.empty()); |
| 57 | } |
| 58 | |
| 59 | TEST_F(DatabaseConfigurationImplTest, CanReturnUnknownTypeIfNoRedisDbTypeIsApplied) |
| 60 | { |
| 61 | const auto retDbType(databaseConfigurationImpl->getDbType()); |
| 62 | EXPECT_EQ(DatabaseConfiguration::DbType::UNKNOWN, retDbType); |
| 63 | } |
| 64 | |
| 65 | TEST_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 | |
| 72 | TEST_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 Badorek | 2dcf940 | 2019-10-01 18:33:58 +0300 | [diff] [blame] | 79 | TEST_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 Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 86 | TEST_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 | |
| 98 | TEST_F(DatabaseConfigurationImplTest, CanThrowIfIllegalDbTypeIsApplied) |
| 99 | { |
| 100 | EXPECT_THROW(databaseConfigurationImpl->checkAndApplyDbType("bad_db_type"), DatabaseConfiguration::InvalidDbType); |
| 101 | } |
| 102 | |
| 103 | TEST_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 | |
| 112 | TEST_F(DatabaseConfigurationImplTest, IsEmptyReturnsCorrectInformation) |
| 113 | { |
| 114 | EXPECT_TRUE(databaseConfigurationImpl->isEmpty()); |
| 115 | databaseConfigurationImpl->checkAndApplyServerAddress("[2001::123]:12345"); |
| 116 | EXPECT_FALSE(databaseConfigurationImpl->isEmpty()); |
| 117 | } |
Rolf Badorek | 2dcf940 | 2019-10-01 18:33:58 +0300 | [diff] [blame] | 118 | |
| 119 | TEST_F(DatabaseConfigurationImplTest, DefaultSentinelAddressIsNone) |
| 120 | { |
| 121 | EXPECT_EQ(boost::none, databaseConfigurationImpl->getSentinelAddress()); |
| 122 | } |
| 123 | |
| 124 | TEST_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 | |
| 133 | TEST_F(DatabaseConfigurationImplTest, DefaultSentinelMasterNameIsEmpty) |
| 134 | { |
| 135 | EXPECT_EQ("", databaseConfigurationImpl->getSentinelMasterName()); |
| 136 | } |
| 137 | |
| 138 | TEST_F(DatabaseConfigurationImplTest, CanApplyAndReturnSentinelMasterName) |
| 139 | { |
| 140 | databaseConfigurationImpl->checkAndApplySentinelMasterName("mymaster"); |
| 141 | EXPECT_EQ("mymaster", databaseConfigurationImpl->getSentinelMasterName()); |
| 142 | } |