|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | +using System.Collections.Generic; |
| 4 | +using NUnit.Framework; |
| 5 | + |
| 6 | +namespace System.ClientModel.Primitives.Tests; |
| 7 | + |
| 8 | +public class ClientConnectionTests |
| 9 | +{ |
| 10 | + [Test] |
| 11 | + [TestCase(null, false)] |
| 12 | + [TestCase(CredentialKind.TokenCredential, false)] |
| 13 | + [TestCase(CredentialKind.TokenCredential, true)] |
| 14 | + public void TestClientConnectionConstructor(CredentialKind? credentialKind, bool hasMetadata) |
| 15 | + { |
| 16 | + ClientConnection conn; |
| 17 | + if (credentialKind is null) |
| 18 | + { |
| 19 | + conn = new(id: "123", locator: "www.microsoft.com"); |
| 20 | + Assert.IsNull(conn.Credential); |
| 21 | + Assert.AreEqual(CredentialKind.None, conn.CredentialKind); |
| 22 | + Assert.AreEqual(0, conn.Metadata.Count); |
| 23 | + } |
| 24 | + else |
| 25 | + { |
| 26 | + object credential = new { foo = "bar" }; |
| 27 | + if (!hasMetadata) |
| 28 | + { |
| 29 | + conn = new(id: "123", locator: "www.microsoft.com", credential: credential, credentialKind: credentialKind.Value); |
| 30 | + Assert.AreEqual(conn.Metadata.Count, 0); |
| 31 | + } |
| 32 | + else |
| 33 | + { |
| 34 | + var metadata = new Dictionary<string, string> |
| 35 | + { |
| 36 | + { "test1", "123"}, |
| 37 | + { "test2", "456" } |
| 38 | + }; |
| 39 | + conn = new(id: "123", locator: "www.microsoft.com", credential: credential, credentialKind: credentialKind.Value, metadata: metadata); |
| 40 | + Assert.AreEqual(conn.Metadata.Count, metadata.Count); |
| 41 | + } |
| 42 | + Assert.AreEqual(conn.CredentialKind, credentialKind); |
| 43 | + Assert.AreEqual(conn.Credential, credential); |
| 44 | + } |
| 45 | + Assert.AreEqual(conn.Id, "123"); |
| 46 | + Assert.AreEqual(conn.Locator, "www.microsoft.com"); |
| 47 | + } |
| 48 | + |
| 49 | +#nullable disable |
| 50 | + [Test] |
| 51 | + [TestCase(null, "Id cannot be null or empty.")] |
| 52 | + [TestCase("123", "Locator cannot be null or empty.")] |
| 53 | + public void TestConstructorThrows(string id, string expected) |
| 54 | + { |
| 55 | + ArgumentException exception = Assert.Throws<ArgumentException>(() => new ClientConnection(id: id, locator: null)); |
| 56 | + Assert.That(exception.Message.StartsWith(expected)); |
| 57 | + } |
| 58 | + |
| 59 | + [Test] |
| 60 | + [TestCase(null, null, "Id cannot be null or empty.")] |
| 61 | + [TestCase("123", null, "Locator cannot be null or empty.")] |
| 62 | + [TestCase("", "", "Id cannot be null or empty.")] |
| 63 | + [TestCase("123", "", "Locator cannot be null or empty.")] |
| 64 | + public void TestConstructorWithCredentialsThrows(string id, string locator, string expected) |
| 65 | + { |
| 66 | + ArgumentException exception = Assert.Throws<ArgumentException>(() => new ClientConnection(id: id, locator: locator, credentialKind: CredentialKind.TokenCredential, credential: null)); |
| 67 | + Assert.That(exception.Message.StartsWith(expected)); |
| 68 | + } |
| 69 | + |
| 70 | + [Test] |
| 71 | + public void TestConstructorWithCredentialsThrowsArgumentNull() |
| 72 | + { |
| 73 | + ArgumentException exception = Assert.Throws<ArgumentNullException>( |
| 74 | + () => new ClientConnection(id: "123", locator: "www.microsoft.com", credentialKind: CredentialKind.TokenCredential, credential: null)); |
| 75 | + Assert.That(exception.Message.StartsWith("Credential cannot be null.")); |
| 76 | + } |
| 77 | + |
| 78 | + [Test] |
| 79 | + public void TestConstructorWithCredentialsNone() |
| 80 | + { |
| 81 | + ClientConnection conn = new(id: "123", locator: "www.microsoft.com", credential: null, credentialKind: CredentialKind.None); |
| 82 | + Assert.AreEqual("123", conn.Id); |
| 83 | + Assert.AreEqual("www.microsoft.com", conn.Locator); |
| 84 | + Assert.AreEqual(CredentialKind.None, conn.CredentialKind); |
| 85 | + Assert.AreEqual(0, conn.Metadata.Count); |
| 86 | + } |
| 87 | + |
| 88 | + [Test] |
| 89 | + [TestCase(null, null, null, "Id cannot be null or empty.")] |
| 90 | + [TestCase("123", null, null, "Locator cannot be null or empty.")] |
| 91 | + [TestCase("", "", null, "Id cannot be null or empty.")] |
| 92 | + [TestCase("123", "", null, "Locator cannot be null or empty.")] |
| 93 | + public void TestConstructorWithMetadataThrows(string id, string locator, object credential, string expected) |
| 94 | + { |
| 95 | + ArgumentException exception = Assert.Throws<ArgumentException>(() => new ClientConnection(id: id, locator: locator, credentialKind: CredentialKind.TokenCredential, credential: credential, metadata: null)); |
| 96 | + Assert.That(exception.Message.StartsWith(expected), $"Expected: {expected}, got: {exception.Message}"); |
| 97 | + } |
| 98 | + |
| 99 | + [Test] |
| 100 | + public void TestConstructorWithMetadataThrowsArgumentNull() |
| 101 | + { |
| 102 | + ArgumentException exception = Assert.Throws<ArgumentNullException>(() => new ClientConnection(id: "123", locator: "www.microsoft.com", credentialKind: CredentialKind.TokenCredential, credential: null, metadata: null)); |
| 103 | + Assert.That(exception.Message.StartsWith("Credential cannot be null."), $"Expected: \"Credential cannot be null.\", got: {exception.Message}"); |
| 104 | + } |
| 105 | + |
| 106 | + [Test] |
| 107 | + [TestCase(0)] |
| 108 | + [TestCase(2)] |
| 109 | + public void TestConstructorMetadataWithNull(int dictionarySize) |
| 110 | + { |
| 111 | + Dictionary<string, string> metadata = null; |
| 112 | + if (dictionarySize > 0) |
| 113 | + { |
| 114 | + metadata = []; |
| 115 | + for (int i = 0; i < dictionarySize; i++) |
| 116 | + { |
| 117 | + metadata[$"key{i}"] = $"value{i}"; |
| 118 | + } |
| 119 | + } |
| 120 | + ClientConnection conn = new(id: "123", locator: "www.microsoft.com", credential: null, credentialKind: CredentialKind.None, metadata: metadata); |
| 121 | + Assert.AreEqual("123", conn.Id); |
| 122 | + Assert.AreEqual("www.microsoft.com", conn.Locator); |
| 123 | + Assert.AreEqual(CredentialKind.None, conn.CredentialKind); |
| 124 | + Assert.AreEqual(dictionarySize, conn.Metadata.Count); |
| 125 | + } |
| 126 | +#nullable enable |
| 127 | +} |
0 commit comments