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 <arpa/inet.h> |
| 18 | #include <type_traits> |
| 19 | #include <gtest/gtest.h> |
| 20 | #include <gmock/gmock.h> |
| 21 | #include "private/redis/databaseinfo.hpp" |
| 22 | |
| 23 | using namespace shareddatalayer; |
| 24 | using namespace shareddatalayer::redis; |
| 25 | using namespace testing; |
| 26 | |
| 27 | namespace |
| 28 | { |
| 29 | static const std::string defaultAddress = "address"; |
| 30 | static const uint16_t defaultPort = 3333; |
| 31 | |
| 32 | DatabaseInfo getDatabaseInfo(DatabaseInfo::Type type = DatabaseInfo::Type::SINGLE, |
| 33 | std::string address = defaultAddress, |
| 34 | uint16_t port = defaultPort) |
| 35 | { |
| 36 | DatabaseInfo databaseInfo; |
| 37 | databaseInfo.type = type; |
| 38 | databaseInfo.hosts.push_back({address, htons(port)}); |
| 39 | databaseInfo.ns = "namespace"; |
| 40 | return databaseInfo; |
| 41 | } |
| 42 | |
| 43 | DatabaseInfo& addHost(DatabaseInfo& databaseInfo, |
| 44 | std::string address, |
| 45 | uint16_t port) |
| 46 | { |
| 47 | databaseInfo.hosts.push_back({address, htons(port)}); |
| 48 | return databaseInfo; |
| 49 | } |
| 50 | |
| 51 | class DatabaseInfoTest: public testing::Test |
| 52 | { |
| 53 | public: |
| 54 | DatabaseInfoTest() |
| 55 | { |
| 56 | } |
| 57 | |
| 58 | virtual ~DatabaseInfoTest() |
| 59 | { |
| 60 | } |
| 61 | }; |
| 62 | } |
| 63 | |
| 64 | TEST_F(DatabaseInfoTest, EqualAndUnequalOperatorsIdenticalInfos) |
| 65 | { |
| 66 | InSequence dummy; |
| 67 | DatabaseInfo databaseInfo1; |
| 68 | DatabaseInfo databaseInfo2; |
| 69 | |
| 70 | databaseInfo1 = getDatabaseInfo(); |
| 71 | databaseInfo2 = getDatabaseInfo(); |
| 72 | |
| 73 | EXPECT_TRUE(databaseInfo1 == databaseInfo2); |
| 74 | EXPECT_FALSE(databaseInfo1 != databaseInfo2); |
| 75 | } |
| 76 | |
| 77 | TEST_F(DatabaseInfoTest, EqualOperatorDifferentAmountOfHosts) |
| 78 | { |
| 79 | InSequence dummy; |
| 80 | DatabaseInfo databaseInfo1; |
| 81 | DatabaseInfo databaseInfo2; |
| 82 | |
| 83 | databaseInfo1 = getDatabaseInfo(); |
| 84 | databaseInfo2 = getDatabaseInfo(); |
| 85 | databaseInfo2 = addHost(databaseInfo2, |
| 86 | "address2", |
| 87 | 123); |
| 88 | |
| 89 | EXPECT_FALSE(databaseInfo1 == databaseInfo2); |
| 90 | } |
| 91 | |
| 92 | TEST_F(DatabaseInfoTest, EqualOperatorHostsInDifferentOrder) |
| 93 | { |
| 94 | InSequence dummy; |
| 95 | DatabaseInfo databaseInfo1; |
| 96 | DatabaseInfo databaseInfo2; |
| 97 | |
| 98 | databaseInfo1 = getDatabaseInfo(DatabaseInfo::Type::SINGLE, |
| 99 | "address2", |
| 100 | 123); |
| 101 | databaseInfo1 = addHost(databaseInfo1, |
| 102 | defaultAddress, |
| 103 | defaultPort); |
| 104 | databaseInfo2 = getDatabaseInfo(); |
| 105 | databaseInfo2 = addHost(databaseInfo2, |
| 106 | "address2", |
| 107 | 123); |
| 108 | |
| 109 | EXPECT_TRUE(databaseInfo1 == databaseInfo2); |
| 110 | } |
| 111 | |
| 112 | TEST_F(DatabaseInfoTest, EqualOperatorDiffrentAddressInHosts) |
| 113 | { |
| 114 | InSequence dummy; |
| 115 | DatabaseInfo databaseInfo1; |
| 116 | DatabaseInfo databaseInfo2; |
| 117 | |
| 118 | databaseInfo1 = getDatabaseInfo(DatabaseInfo::Type::SINGLE, |
| 119 | "address1", |
| 120 | 123); |
| 121 | databaseInfo2 = getDatabaseInfo(DatabaseInfo::Type::SINGLE, |
| 122 | "address2", |
| 123 | 123); |
| 124 | |
| 125 | EXPECT_FALSE(databaseInfo1 == databaseInfo2); |
| 126 | } |
| 127 | |
| 128 | TEST_F(DatabaseInfoTest, EqualOperatorDiffrentPortInHosts) |
| 129 | { |
| 130 | InSequence dummy; |
| 131 | DatabaseInfo databaseInfo1; |
| 132 | DatabaseInfo databaseInfo2; |
| 133 | |
| 134 | databaseInfo1 = getDatabaseInfo(DatabaseInfo::Type::SINGLE, |
| 135 | "address1", |
| 136 | 123); |
| 137 | databaseInfo2 = getDatabaseInfo(DatabaseInfo::Type::SINGLE, |
| 138 | "address1", |
| 139 | 1234); |
| 140 | |
| 141 | EXPECT_FALSE(databaseInfo1 == databaseInfo2); |
| 142 | } |