mongo-csharp-driver /tests/MongoDB.Driver.Tests/ClusterKeyTests.cs

Language C# Lines 141
MD5 Hash a91301bd0b7d6e0d457d96db43f5b126 Estimated Cost $2,916 (why?)
Repository git://github.com/mongodb/mongo-csharp-driver.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* Copyright 2010-2015 MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System;
using System.Linq;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using FluentAssertions;
using MongoDB.Bson;
using MongoDB.Driver;
using Xunit;

namespace MongoDB.Driver.Tests
{
    public class ClusterKeyTests
    {
        [Fact]
        public void Equals_should_return_true_if_all_fields_are_equal()
        {
            var subject1 = CreateSubject();
            var subject2 = CreateSubject();
            subject1.Should().NotBeSameAs(subject2);
            subject1.Equals(subject2).Should().BeTrue();
            subject1.GetHashCode().Should().Be(subject2.GetHashCode());
        }

        [Theory]
        [InlineData("ConnectionMode", true)]
        [InlineData("ConnectTimeout", true)]
        [InlineData("Credentials", false)]
        [InlineData("IPv6", true)]
        [InlineData("MaxConnectionIdleTime", true)]
        [InlineData("MaxConnectionLifeTime", true)]
        [InlineData("MaxConnectionPoolSize", true)]
        [InlineData("MinConnectionPoolSize", true)]
        [InlineData("ReplicaSetName", true)]
        [InlineData("LocalThreshold", true)]
        [InlineData("Servers", false)]
        [InlineData("ServerSelectionTimeout", true)]
        [InlineData("SocketTimeout", true)]
        [InlineData("SslSettings", true)]
        [InlineData("UseSsl", true)]
        [InlineData("VerifySslCertificate", true)]
        [InlineData("WaitQueueSize", true)]
        [InlineData("WaitQueueTimeout", true)]
        public void Equals_should_return_false_if_any_field_is_not_equal(string notEqualFieldName, bool expectEqualHashCode)
        {
            var subject1 = CreateSubject();
            var subject2 = CreateSubject(notEqualFieldName);
            subject1.Should().NotBeSameAs(subject2);
            subject1.Equals(subject2).Should().BeFalse();
            subject1.GetHashCode().Equals(subject2.GetHashCode()).Should().Be(expectEqualHashCode);
        }

        private ClusterKey CreateSubject(string notEqualFieldName = null)
        {
            var connectionMode = ConnectionMode.Direct;
            var connectTimeout = TimeSpan.FromSeconds(1);
            var credentials = new[] { MongoCredential.CreateMongoCRCredential("source", "username", "password") };
            var guidRepresentation = GuidRepresentation.Standard;
            var ipv6 = false;
            var localThreshold = TimeSpan.FromMilliseconds(20);
            var maxConnectionIdleTime = TimeSpan.FromSeconds(2);
            var maxConnectionLifeTime = TimeSpan.FromSeconds(3);
            var maxConnectionPoolSize = 50;
            var minConnectionPoolSize = 5;
            var replicaSetName = "abc";
            var servers = new[] { new MongoServerAddress("localhost") };
            var serverSelectionTimeout = TimeSpan.FromSeconds(6);
            var socketTimeout = TimeSpan.FromSeconds(4);
            var sslSettings = new SslSettings
            {
                CheckCertificateRevocation = true,
                EnabledSslProtocols = SslProtocols.Tls
            };
            var useSsl = false;
            var verifySslCertificate = false;
            var waitQueueSize = 20;
            var waitQueueTimeout = TimeSpan.FromSeconds(5);

            switch (notEqualFieldName)
            {
                case "ConnectionMode": connectionMode = ConnectionMode.ReplicaSet; break;
                case "ConnectTimeout": connectTimeout = TimeSpan.FromSeconds(99); break;
                case "Credentials": credentials = new[] { MongoCredential.CreateMongoCRCredential("different", "different", "different") }; break;
                case "IPv6": ipv6 = !ipv6; break;
                case "LocalThreshold": localThreshold = TimeSpan.FromMilliseconds(99); break;
                case "MaxConnectionIdleTime": maxConnectionIdleTime = TimeSpan.FromSeconds(99); break;
                case "MaxConnectionLifeTime": maxConnectionLifeTime = TimeSpan.FromSeconds(99); break;
                case "MaxConnectionPoolSize": maxConnectionPoolSize = 99; break;
                case "MinConnectionPoolSize": minConnectionPoolSize = 99; break;
                case "ReplicaSetName": replicaSetName = "different"; break;
                case "Servers": servers = new[] { new MongoServerAddress("different") }; break;
                case "ServerSelectionTimeout": serverSelectionTimeout = TimeSpan.FromSeconds(98); break;
                case "SocketTimeout": socketTimeout = TimeSpan.FromSeconds(99); break;
                case "SslSettings": sslSettings.CheckCertificateRevocation = !sslSettings.CheckCertificateRevocation; break;
                case "UseSsl": useSsl = !useSsl; break;
                case "VerifySslCertificate": verifySslCertificate = !verifySslCertificate; break;
                case "WaitQueueSize": waitQueueSize = 99; break;
                case "WaitQueueTimeout": waitQueueTimeout = TimeSpan.FromSeconds(99); break;
            }

            var clientSettings = new MongoClientSettings
            {
                ConnectionMode = connectionMode,
                ConnectTimeout = connectTimeout,
                Credentials = credentials,
                GuidRepresentation = guidRepresentation,
                IPv6 = ipv6,
                MaxConnectionIdleTime = maxConnectionIdleTime,
                MaxConnectionLifeTime = maxConnectionLifeTime,
                MaxConnectionPoolSize = maxConnectionPoolSize,
                MinConnectionPoolSize = minConnectionPoolSize,
                ReplicaSetName = replicaSetName,
                LocalThreshold = localThreshold,
                Servers = servers,
                ServerSelectionTimeout = serverSelectionTimeout,
                SocketTimeout = socketTimeout,
                SslSettings = sslSettings,
                UseSsl = useSsl,
                VerifySslCertificate = verifySslCertificate,
                WaitQueueSize = waitQueueSize,
                WaitQueueTimeout = waitQueueTimeout
            };

            return clientSettings.ToClusterKey();
        }
    }
}
Back to Top