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 | |
| 17 | #include <gtest/gtest.h> |
| 18 | #include <gmock/gmock.h> |
| 19 | #include <arpa/inet.h> |
| 20 | #include "private/databaseconfigurationimpl.hpp" |
| 21 | |
| 22 | using namespace shareddatalayer; |
| 23 | using namespace testing; |
| 24 | |
| 25 | namespace |
| 26 | { |
| 27 | class DatabaseConfigurationImplTest: public testing::Test |
| 28 | { |
| 29 | public: |
| 30 | std::unique_ptr<DatabaseConfigurationImpl> databaseConfigurationImpl; |
| 31 | |
| 32 | DatabaseConfigurationImplTest() |
| 33 | { |
| 34 | InSequence dummy; |
| 35 | databaseConfigurationImpl.reset(new DatabaseConfigurationImpl()); |
| 36 | } |
| 37 | }; |
| 38 | } |
| 39 | |
| 40 | TEST_F(DatabaseConfigurationImplTest, CanReturnDefaultAddress) |
| 41 | { |
| 42 | const auto retAddresses(databaseConfigurationImpl->getDefaultServerAddresses()); |
| 43 | EXPECT_EQ(1U, retAddresses.size()); |
| 44 | EXPECT_EQ("localhost", retAddresses.back().getHost()); |
| 45 | EXPECT_EQ(6379U, ntohs(retAddresses.back().getPort())); |
| 46 | } |
| 47 | |
| 48 | TEST_F(DatabaseConfigurationImplTest, CanReturnEmptyAddressListIfNoAddressesAreApplied) |
| 49 | { |
| 50 | const auto retAddresses(databaseConfigurationImpl->getServerAddresses()); |
| 51 | EXPECT_TRUE(retAddresses.empty()); |
| 52 | } |
| 53 | |
| 54 | TEST_F(DatabaseConfigurationImplTest, CanReturnUnknownTypeIfNoRedisDbTypeIsApplied) |
| 55 | { |
| 56 | const auto retDbType(databaseConfigurationImpl->getDbType()); |
| 57 | EXPECT_EQ(DatabaseConfiguration::DbType::UNKNOWN, retDbType); |
| 58 | } |
| 59 | |
| 60 | TEST_F(DatabaseConfigurationImplTest, CanApplyRedisDbTypeStringAndReturnType) |
| 61 | { |
| 62 | databaseConfigurationImpl->checkAndApplyDbType("redis-standalone"); |
| 63 | const auto retDbType(databaseConfigurationImpl->getDbType()); |
| 64 | EXPECT_EQ(DatabaseConfiguration::DbType::REDIS_STANDALONE, retDbType); |
| 65 | } |
| 66 | |
| 67 | TEST_F(DatabaseConfigurationImplTest, CanApplyRedisClusterDbTypeStringAndReturnType) |
| 68 | { |
| 69 | databaseConfigurationImpl->checkAndApplyDbType("redis-cluster"); |
| 70 | const auto retDbType(databaseConfigurationImpl->getDbType()); |
| 71 | EXPECT_EQ(DatabaseConfiguration::DbType::REDIS_CLUSTER, retDbType); |
| 72 | } |
| 73 | |
| 74 | TEST_F(DatabaseConfigurationImplTest, CanApplyNewAddressesOneByOneAndReturnAllAddresses) |
| 75 | { |
| 76 | databaseConfigurationImpl->checkAndApplyServerAddress("dummydatabaseaddress.local"); |
| 77 | databaseConfigurationImpl->checkAndApplyServerAddress("10.20.30.40:65535"); |
| 78 | const auto retAddresses(databaseConfigurationImpl->getServerAddresses()); |
| 79 | EXPECT_EQ(2U, retAddresses.size()); |
| 80 | EXPECT_EQ("dummydatabaseaddress.local", retAddresses.at(0).getHost()); |
| 81 | EXPECT_EQ(6379U, ntohs(retAddresses.at(0).getPort())); |
| 82 | EXPECT_EQ("10.20.30.40", retAddresses.at(1).getHost()); |
| 83 | EXPECT_EQ(65535U, ntohs(retAddresses.at(1).getPort())); |
| 84 | } |
| 85 | |
| 86 | TEST_F(DatabaseConfigurationImplTest, CanThrowIfIllegalDbTypeIsApplied) |
| 87 | { |
| 88 | EXPECT_THROW(databaseConfigurationImpl->checkAndApplyDbType("bad_db_type"), DatabaseConfiguration::InvalidDbType); |
| 89 | } |
| 90 | |
| 91 | TEST_F(DatabaseConfigurationImplTest, CanApplyIPv6AddressAndReturnIt) |
| 92 | { |
| 93 | databaseConfigurationImpl->checkAndApplyServerAddress("[2001::123]:12345"); |
| 94 | const auto retAddresses(databaseConfigurationImpl->getServerAddresses()); |
| 95 | EXPECT_EQ(1U, retAddresses.size()); |
| 96 | EXPECT_EQ("2001::123", retAddresses.at(0).getHost()); |
| 97 | EXPECT_EQ(12345U, ntohs(retAddresses.at(0).getPort())); |
| 98 | } |
| 99 | |
| 100 | TEST_F(DatabaseConfigurationImplTest, IsEmptyReturnsCorrectInformation) |
| 101 | { |
| 102 | EXPECT_TRUE(databaseConfigurationImpl->isEmpty()); |
| 103 | databaseConfigurationImpl->checkAndApplyServerAddress("[2001::123]:12345"); |
| 104 | EXPECT_FALSE(databaseConfigurationImpl->isEmpty()); |
| 105 | } |