|
| 1 | +// Licensed to Elasticsearch B.V under one or more agreements. |
| 2 | +// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. |
| 3 | +// See the LICENSE file in the project root for more information |
| 4 | + |
| 5 | +using System.Text.Json; |
| 6 | +using Elastic.Documentation; |
| 7 | +using Elastic.Documentation.AppliesTo; |
| 8 | +using FluentAssertions; |
| 9 | + |
| 10 | +namespace Elastic.Markdown.Tests.AppliesTo; |
| 11 | + |
| 12 | +public class ApplicableToJsonConverterSerializationTests |
| 13 | +{ |
| 14 | + private readonly JsonSerializerOptions _options = new() { WriteIndented = true }; |
| 15 | + |
| 16 | + [Fact] |
| 17 | + public void Serialize_Stack_ProducesCorrectJson() |
| 18 | + { |
| 19 | + var applicableTo = new ApplicableTo |
| 20 | + { |
| 21 | + Stack = AppliesCollection.GenerallyAvailable |
| 22 | + }; |
| 23 | + |
| 24 | + var json = JsonSerializer.Serialize(applicableTo, _options); |
| 25 | + |
| 26 | + json.Should().Be(""" |
| 27 | +[ |
| 28 | + { |
| 29 | + "type": "stack", |
| 30 | + "sub-type": "stack", |
| 31 | + "lifecycle": "ga", |
| 32 | + "version": "9999.9999.9999" |
| 33 | + } |
| 34 | +] |
| 35 | +"""); |
| 36 | + } |
| 37 | + |
| 38 | + [Fact] |
| 39 | + public void Serialize_StackWithVersion_ProducesCorrectJson() |
| 40 | + { |
| 41 | + var applicableTo = new ApplicableTo |
| 42 | + { |
| 43 | + Stack = new AppliesCollection([new Applicability { Lifecycle = ProductLifecycle.Beta, Version = (SemVersion)"8.0.0" }]) |
| 44 | + }; |
| 45 | + |
| 46 | + var json = JsonSerializer.Serialize(applicableTo, _options); |
| 47 | + |
| 48 | + json.Should().Be(""" |
| 49 | +[ |
| 50 | + { |
| 51 | + "type": "stack", |
| 52 | + "sub-type": "stack", |
| 53 | + "lifecycle": "beta", |
| 54 | + "version": "8.0.0" |
| 55 | + } |
| 56 | +] |
| 57 | +"""); |
| 58 | + } |
| 59 | + |
| 60 | + [Fact] |
| 61 | + public void Serialize_MultipleApplicabilities_ProducesCorrectJson() |
| 62 | + { |
| 63 | + var applicableTo = new ApplicableTo |
| 64 | + { |
| 65 | + Stack = new AppliesCollection( |
| 66 | + [ |
| 67 | + new Applicability { Lifecycle = ProductLifecycle.GenerallyAvailable, Version = (SemVersion)"8.0.0" }, |
| 68 | + new Applicability { Lifecycle = ProductLifecycle.Beta, Version = (SemVersion)"7.17.0" } |
| 69 | + ]) |
| 70 | + }; |
| 71 | + |
| 72 | + var json = JsonSerializer.Serialize(applicableTo, _options); |
| 73 | + |
| 74 | + json.Should().Be(""" |
| 75 | +[ |
| 76 | + { |
| 77 | + "type": "stack", |
| 78 | + "sub-type": "stack", |
| 79 | + "lifecycle": "ga", |
| 80 | + "version": "8.0.0" |
| 81 | + }, |
| 82 | + { |
| 83 | + "type": "stack", |
| 84 | + "sub-type": "stack", |
| 85 | + "lifecycle": "beta", |
| 86 | + "version": "7.17.0" |
| 87 | + } |
| 88 | +] |
| 89 | +"""); |
| 90 | + } |
| 91 | + |
| 92 | + [Fact] |
| 93 | + public void Serialize_Deployment_ProducesCorrectJson() |
| 94 | + { |
| 95 | + var applicableTo = new ApplicableTo |
| 96 | + { |
| 97 | + Deployment = new DeploymentApplicability |
| 98 | + { |
| 99 | + Ece = new AppliesCollection([new Applicability { Lifecycle = ProductLifecycle.GenerallyAvailable, Version = (SemVersion)"3.0.0" }]), |
| 100 | + Ess = AppliesCollection.GenerallyAvailable |
| 101 | + } |
| 102 | + }; |
| 103 | + |
| 104 | + var json = JsonSerializer.Serialize(applicableTo, _options); |
| 105 | + |
| 106 | + json.Should().Be(""" |
| 107 | +[ |
| 108 | + { |
| 109 | + "type": "deployment", |
| 110 | + "sub-type": "ece", |
| 111 | + "lifecycle": "ga", |
| 112 | + "version": "3.0.0" |
| 113 | + }, |
| 114 | + { |
| 115 | + "type": "deployment", |
| 116 | + "sub-type": "ess", |
| 117 | + "lifecycle": "ga", |
| 118 | + "version": "9999.9999.9999" |
| 119 | + } |
| 120 | +] |
| 121 | +"""); |
| 122 | + } |
| 123 | + |
| 124 | + [Fact] |
| 125 | + public void Serialize_Serverless_ProducesCorrectJson() |
| 126 | + { |
| 127 | + var applicableTo = new ApplicableTo |
| 128 | + { |
| 129 | + Serverless = new ServerlessProjectApplicability |
| 130 | + { |
| 131 | + Elasticsearch = new AppliesCollection([new Applicability { Lifecycle = ProductLifecycle.Beta, Version = (SemVersion)"1.0.0" }]), |
| 132 | + Security = AppliesCollection.GenerallyAvailable |
| 133 | + } |
| 134 | + }; |
| 135 | + |
| 136 | + var json = JsonSerializer.Serialize(applicableTo, _options); |
| 137 | + |
| 138 | + json.Should().Be(""" |
| 139 | +[ |
| 140 | + { |
| 141 | + "type": "serverless", |
| 142 | + "sub-type": "elasticsearch", |
| 143 | + "lifecycle": "beta", |
| 144 | + "version": "1.0.0" |
| 145 | + }, |
| 146 | + { |
| 147 | + "type": "serverless", |
| 148 | + "sub-type": "security", |
| 149 | + "lifecycle": "ga", |
| 150 | + "version": "9999.9999.9999" |
| 151 | + } |
| 152 | +] |
| 153 | +"""); |
| 154 | + } |
| 155 | + |
| 156 | + [Fact] |
| 157 | + public void Serialize_Product_ProducesCorrectJson() |
| 158 | + { |
| 159 | + var applicableTo = new ApplicableTo |
| 160 | + { |
| 161 | + Product = new AppliesCollection([new Applicability { Lifecycle = ProductLifecycle.TechnicalPreview, Version = (SemVersion)"0.5.0" }]) |
| 162 | + }; |
| 163 | + |
| 164 | + var json = JsonSerializer.Serialize(applicableTo, _options); |
| 165 | + |
| 166 | + json.Should().Be(""" |
| 167 | +[ |
| 168 | + { |
| 169 | + "type": "product", |
| 170 | + "sub-type": "product", |
| 171 | + "lifecycle": "preview", |
| 172 | + "version": "0.5.0" |
| 173 | + } |
| 174 | +] |
| 175 | +"""); |
| 176 | + } |
| 177 | + |
| 178 | + [Fact] |
| 179 | + public void Serialize_ProductApplicability_ProducesCorrectJson() |
| 180 | + { |
| 181 | + var applicableTo = new ApplicableTo |
| 182 | + { |
| 183 | + ProductApplicability = new ProductApplicability |
| 184 | + { |
| 185 | + Ecctl = new AppliesCollection([new Applicability { Lifecycle = ProductLifecycle.Deprecated, Version = (SemVersion)"5.0.0" }]), |
| 186 | + ApmAgentDotnet = AppliesCollection.GenerallyAvailable |
| 187 | + } |
| 188 | + }; |
| 189 | + |
| 190 | + var json = JsonSerializer.Serialize(applicableTo, _options); |
| 191 | + |
| 192 | + json.Should().Be(""" |
| 193 | +[ |
| 194 | + { |
| 195 | + "type": "product", |
| 196 | + "sub-type": "ecctl", |
| 197 | + "lifecycle": "deprecated", |
| 198 | + "version": "5.0.0" |
| 199 | + }, |
| 200 | + { |
| 201 | + "type": "product", |
| 202 | + "sub-type": "apm-agent-dotnet", |
| 203 | + "lifecycle": "ga", |
| 204 | + "version": "9999.9999.9999" |
| 205 | + } |
| 206 | +] |
| 207 | +"""); |
| 208 | + } |
| 209 | + |
| 210 | + [Fact] |
| 211 | + public void Serialize_AllLifecycles_ProducesCorrectJson() |
| 212 | + { |
| 213 | + var applicableTo = new ApplicableTo |
| 214 | + { |
| 215 | + Stack = new AppliesCollection( |
| 216 | + [ |
| 217 | + new Applicability { Lifecycle = ProductLifecycle.TechnicalPreview, Version = (SemVersion)"1.0.0" }, |
| 218 | + new Applicability { Lifecycle = ProductLifecycle.Beta, Version = (SemVersion)"1.0.0" }, |
| 219 | + new Applicability { Lifecycle = ProductLifecycle.GenerallyAvailable, Version = (SemVersion)"1.0.0" }, |
| 220 | + new Applicability { Lifecycle = ProductLifecycle.Deprecated, Version = (SemVersion)"1.0.0" }, |
| 221 | + new Applicability { Lifecycle = ProductLifecycle.Removed, Version = (SemVersion)"1.0.0" } |
| 222 | + ]) |
| 223 | + }; |
| 224 | + |
| 225 | + var json = JsonSerializer.Serialize(applicableTo, _options); |
| 226 | + |
| 227 | + json.Should().Contain("\"lifecycle\": \"preview\""); |
| 228 | + json.Should().Contain("\"lifecycle\": \"beta\""); |
| 229 | + json.Should().Contain("\"lifecycle\": \"ga\""); |
| 230 | + json.Should().Contain("\"lifecycle\": \"deprecated\""); |
| 231 | + json.Should().Contain("\"lifecycle\": \"removed\""); |
| 232 | + } |
| 233 | + |
| 234 | + [Fact] |
| 235 | + public void Serialize_Complex_ProducesCorrectJson() |
| 236 | + { |
| 237 | + var applicableTo = new ApplicableTo |
| 238 | + { |
| 239 | + Stack = new AppliesCollection([new Applicability { Lifecycle = ProductLifecycle.GenerallyAvailable, Version = (SemVersion)"8.0.0" }]), |
| 240 | + Deployment = new DeploymentApplicability |
| 241 | + { |
| 242 | + Ece = AppliesCollection.GenerallyAvailable |
| 243 | + }, |
| 244 | + Product = AppliesCollection.GenerallyAvailable |
| 245 | + }; |
| 246 | + |
| 247 | + var json = JsonSerializer.Serialize(applicableTo, _options); |
| 248 | + |
| 249 | + // Verify it's an array with 3 items |
| 250 | + var jsonDoc = JsonDocument.Parse(json); |
| 251 | + jsonDoc.RootElement.GetArrayLength().Should().Be(3); |
| 252 | + |
| 253 | + // Verify each type is present |
| 254 | + json.Should().Contain("\"type\": \"stack\""); |
| 255 | + json.Should().Contain("\"type\": \"deployment\""); |
| 256 | + json.Should().Contain("\"type\": \"product\""); |
| 257 | + |
| 258 | + // Verify sub-types |
| 259 | + json.Should().Contain("\"sub-type\": \"stack\""); |
| 260 | + json.Should().Contain("\"sub-type\": \"ece\""); |
| 261 | + json.Should().Contain("\"sub-type\": \"product\""); |
| 262 | + } |
| 263 | + |
| 264 | + [Fact] |
| 265 | + public void Serialize_Empty_ProducesEmptyArray() |
| 266 | + { |
| 267 | + var applicableTo = new ApplicableTo(); |
| 268 | + |
| 269 | + var json = JsonSerializer.Serialize(applicableTo, _options); |
| 270 | + |
| 271 | + json.Should().Be("[]"); |
| 272 | + } |
| 273 | + |
| 274 | + [Fact] |
| 275 | + public void Serialize_ValidatesJsonStructure() |
| 276 | + { |
| 277 | + var original = new ApplicableTo |
| 278 | + { |
| 279 | + Stack = AppliesCollection.GenerallyAvailable, |
| 280 | + Deployment = new DeploymentApplicability |
| 281 | + { |
| 282 | + Ece = new AppliesCollection([new Applicability { Lifecycle = ProductLifecycle.Beta, Version = (SemVersion)"3.0.0" }]) |
| 283 | + } |
| 284 | + }; |
| 285 | + |
| 286 | + var json = JsonSerializer.Serialize(original, _options); |
| 287 | + var jsonDoc = JsonDocument.Parse(json); |
| 288 | + var root = jsonDoc.RootElement; |
| 289 | + |
| 290 | + root.ValueKind.Should().Be(JsonValueKind.Array); |
| 291 | + var array = root.EnumerateArray().ToList(); |
| 292 | + |
| 293 | + array.Should().HaveCount(2); // Stack + Deployment.Ece |
| 294 | + |
| 295 | + var stackEntry = array[0]; |
| 296 | + stackEntry.GetProperty("type").GetString().Should().Be("stack"); |
| 297 | + stackEntry.GetProperty("sub-type").GetString().Should().Be("stack"); |
| 298 | + stackEntry.GetProperty("lifecycle").GetString().Should().Be("ga"); |
| 299 | + stackEntry.GetProperty("version").GetString().Should().Be("9999.9999.9999"); |
| 300 | + |
| 301 | + var deploymentEntry = array[1]; |
| 302 | + deploymentEntry.GetProperty("type").GetString().Should().Be("deployment"); |
| 303 | + deploymentEntry.GetProperty("sub-type").GetString().Should().Be("ece"); |
| 304 | + deploymentEntry.GetProperty("lifecycle").GetString().Should().Be("beta"); |
| 305 | + deploymentEntry.GetProperty("version").GetString().Should().Be("3.0.0"); |
| 306 | + } |
| 307 | +} |
0 commit comments