|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.ClientModel.Primitives; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Text.Json; |
| 8 | +using Azure.ResourceManager.Elastic.Models; |
| 9 | +using NUnit.Framework; |
| 10 | + |
| 11 | +namespace Azure.ResourceManager.Elastic.Tests |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Regression tests for ElasticCloudDeployment custom serialization fixes for issue #50974 |
| 15 | + /// </summary> |
| 16 | + public class ElasticCloudDeploymentRegressionTests |
| 17 | + { |
| 18 | + [Test] |
| 19 | + public void SerializeAndDeserialize_AllUriProperties_WithAbsoluteUris() |
| 20 | + { |
| 21 | + // Arrange |
| 22 | + var elasticsearchServiceUri = new Uri("https://elasticsearch.example.com:9200"); |
| 23 | + var kibanaServiceUri = new Uri("https://kibana.example.com:5601"); |
| 24 | + var kibanaSsoUri = new Uri("https://kibana.example.com:5601/sso"); |
| 25 | + |
| 26 | + var deployment = new ElasticCloudDeployment( |
| 27 | + name: "test-deployment", |
| 28 | + deploymentId: "dep-123", |
| 29 | + azureSubscriptionId: "sub-456", |
| 30 | + elasticsearchRegion: "eastus", |
| 31 | + elasticsearchServiceUri: elasticsearchServiceUri, |
| 32 | + kibanaServiceUri: kibanaServiceUri, |
| 33 | + kibanaSsoUri: kibanaSsoUri, |
| 34 | + serializedAdditionalRawData: new Dictionary<string, BinaryData>() |
| 35 | + ); |
| 36 | + |
| 37 | + // Act - Serialize |
| 38 | + var model = deployment as IPersistableModel<ElasticCloudDeployment>; |
| 39 | + var wire = ModelReaderWriter.Write(model); |
| 40 | + var jsonString = wire.ToString(); |
| 41 | + |
| 42 | + // Verify serialized JSON contains absolute URIs |
| 43 | + var jsonDoc = JsonDocument.Parse(jsonString); |
| 44 | + var root = jsonDoc.RootElement; |
| 45 | + |
| 46 | + if (root.TryGetProperty("elasticsearchServiceUrl", out var esElement)) |
| 47 | + { |
| 48 | + Assert.AreEqual("https://elasticsearch.example.com:9200/", esElement.GetString()); |
| 49 | + } |
| 50 | + if (root.TryGetProperty("kibanaServiceUrl", out var kibanaElement)) |
| 51 | + { |
| 52 | + Assert.AreEqual("https://kibana.example.com:5601/", kibanaElement.GetString()); |
| 53 | + } |
| 54 | + if (root.TryGetProperty("kibanaSsoUrl", out var kibanaSsoElement)) |
| 55 | + { |
| 56 | + Assert.AreEqual("https://kibana.example.com:5601/sso", kibanaSsoElement.GetString()); |
| 57 | + } |
| 58 | + |
| 59 | + // Act - Deserialize |
| 60 | + var deserialized = ModelReaderWriter.Read<ElasticCloudDeployment>(wire); |
| 61 | + |
| 62 | + // Assert |
| 63 | + Assert.NotNull(deserialized); |
| 64 | + Assert.AreEqual(elasticsearchServiceUri, deserialized.ElasticsearchServiceUri); |
| 65 | + Assert.AreEqual(kibanaServiceUri, deserialized.KibanaServiceUri); |
| 66 | + Assert.AreEqual(kibanaSsoUri, deserialized.KibanaSsoUri); |
| 67 | + |
| 68 | + // Verify all URIs are absolute |
| 69 | + Assert.IsTrue(deserialized.ElasticsearchServiceUri.IsAbsoluteUri); |
| 70 | + Assert.IsTrue(deserialized.KibanaServiceUri.IsAbsoluteUri); |
| 71 | + Assert.IsTrue(deserialized.KibanaSsoUri.IsAbsoluteUri); |
| 72 | + } |
| 73 | + |
| 74 | + [Test] |
| 75 | + public void SerializeAndDeserialize_AllUriProperties_WithRelativeUris() |
| 76 | + { |
| 77 | + // Arrange - Use relative paths that don't start with "/" to avoid platform-specific interpretation |
| 78 | + var elasticsearchServiceUri = new Uri("elasticsearch", UriKind.Relative); |
| 79 | + var kibanaServiceUri = new Uri("kibana", UriKind.Relative); |
| 80 | + var kibanaSsoUri = new Uri("kibana/sso", UriKind.Relative); |
| 81 | + |
| 82 | + var deployment = new ElasticCloudDeployment( |
| 83 | + name: "test-deployment", |
| 84 | + deploymentId: "dep-123", |
| 85 | + azureSubscriptionId: "sub-456", |
| 86 | + elasticsearchRegion: "eastus", |
| 87 | + elasticsearchServiceUri: elasticsearchServiceUri, |
| 88 | + kibanaServiceUri: kibanaServiceUri, |
| 89 | + kibanaSsoUri: kibanaSsoUri, |
| 90 | + serializedAdditionalRawData: new Dictionary<string, BinaryData>() |
| 91 | + ); |
| 92 | + |
| 93 | + // Act - Serialize |
| 94 | + var model = deployment as IPersistableModel<ElasticCloudDeployment>; |
| 95 | + var wire = ModelReaderWriter.Write(model); |
| 96 | + var jsonString = wire.ToString(); |
| 97 | + |
| 98 | + // Verify serialized JSON contains relative URIs as original strings |
| 99 | + var jsonDoc = JsonDocument.Parse(jsonString); |
| 100 | + var root = jsonDoc.RootElement; |
| 101 | + |
| 102 | + if (root.TryGetProperty("elasticsearchServiceUrl", out var esElement)) |
| 103 | + { |
| 104 | + Assert.AreEqual("elasticsearch", esElement.GetString()); |
| 105 | + } |
| 106 | + if (root.TryGetProperty("kibanaServiceUrl", out var kibanaElement)) |
| 107 | + { |
| 108 | + Assert.AreEqual("kibana", kibanaElement.GetString()); |
| 109 | + } |
| 110 | + if (root.TryGetProperty("kibanaSsoUrl", out var kibanaSsoElement)) |
| 111 | + { |
| 112 | + Assert.AreEqual("kibana/sso", kibanaSsoElement.GetString()); |
| 113 | + } |
| 114 | + |
| 115 | + // Act - Deserialize |
| 116 | + var deserialized = ModelReaderWriter.Read<ElasticCloudDeployment>(wire); |
| 117 | + |
| 118 | + // Assert |
| 119 | + Assert.NotNull(deserialized); |
| 120 | + Assert.AreEqual(elasticsearchServiceUri, deserialized.ElasticsearchServiceUri); |
| 121 | + Assert.AreEqual(kibanaServiceUri, deserialized.KibanaServiceUri); |
| 122 | + Assert.AreEqual(kibanaSsoUri, deserialized.KibanaSsoUri); |
| 123 | + |
| 124 | + // Verify all URIs are relative |
| 125 | + Assert.IsFalse(deserialized.ElasticsearchServiceUri.IsAbsoluteUri); |
| 126 | + Assert.IsFalse(deserialized.KibanaServiceUri.IsAbsoluteUri); |
| 127 | + Assert.IsFalse(deserialized.KibanaSsoUri.IsAbsoluteUri); |
| 128 | + |
| 129 | + // Verify original strings are preserved |
| 130 | + Assert.AreEqual("elasticsearch", deserialized.ElasticsearchServiceUri.OriginalString); |
| 131 | + Assert.AreEqual("kibana", deserialized.KibanaServiceUri.OriginalString); |
| 132 | + Assert.AreEqual("kibana/sso", deserialized.KibanaSsoUri.OriginalString); |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments