Skip to content

Commit ed9cc82

Browse files
committed
Split ApplicableToJsonConverterTests into focused ApplicableToJsonConverterRoundTripTests.
1 parent 62f4015 commit ed9cc82

File tree

3 files changed

+312
-44
lines changed

3 files changed

+312
-44
lines changed

src/Elastic.Documentation/AppliesTo/ApplicableToJsonConverter.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,6 @@ public class ApplicableToJsonConverter : JsonConverter<ApplicableTo>
7474
var versionStr = reader.GetString();
7575
if (versionStr != null && SemVersionConverter.TryParse(versionStr, out var v))
7676
version = v;
77-
else if (versionStr == "all" || string.IsNullOrEmpty(versionStr))
78-
version = AllVersions.Instance;
7977
break;
8078
}
8179
}
@@ -264,11 +262,8 @@ private static void WriteApplicabilityEntries(Utf8JsonWriter writer, string type
264262
writer.WriteString("lifecycle", lifecycleName);
265263

266264
// Write version
267-
var isAllVersions = applicability.Version is null || ReferenceEquals(applicability.Version, AllVersions.Instance);
268-
if (!isAllVersions)
269-
writer.WriteString("version", applicability.Version!.ToString());
270-
else
271-
writer.WriteString("version", "all");
265+
if (applicability.Version is not null)
266+
writer.WriteString("version", applicability.Version.ToString());
272267

273268
writer.WriteEndObject();
274269
}

tests/Elastic.Markdown.Tests/AppliesTo/ApplicableToJsonConverterTests.cs renamed to tests/Elastic.Markdown.Tests/AppliesTo/ApplicableToJsonConverterRoundTripTests.cs

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
namespace Elastic.Markdown.Tests.AppliesTo;
1111

12-
public class ApplicableToJsonConverterTests
12+
public class ApplicableToJsonConverterRoundTripTests
1313
{
1414
private readonly JsonSerializerOptions _options = new() { WriteIndented = true };
1515

@@ -313,40 +313,6 @@ public void RoundTrip_MultipleApplicabilitiesInCollection()
313313
deserialized.Stack.Should().BeEquivalentTo(original.Stack);
314314
}
315315

316-
[Fact]
317-
public void Serialize_ValidatesJsonStructure()
318-
{
319-
var original = new ApplicableTo
320-
{
321-
Stack = AppliesCollection.GenerallyAvailable,
322-
Deployment = new DeploymentApplicability
323-
{
324-
Ece = new AppliesCollection([new Applicability { Lifecycle = ProductLifecycle.Beta, Version = (SemVersion)"3.0.0" }])
325-
}
326-
};
327-
328-
var json = JsonSerializer.Serialize(original, _options);
329-
var jsonDoc = JsonDocument.Parse(json);
330-
var root = jsonDoc.RootElement;
331-
332-
root.ValueKind.Should().Be(JsonValueKind.Array);
333-
var array = root.EnumerateArray().ToList();
334-
335-
array.Should().HaveCount(2); // Stack + Deployment.Ece
336-
337-
var stackEntry = array[0];
338-
stackEntry.GetProperty("type").GetString().Should().Be("stack");
339-
stackEntry.GetProperty("sub-type").GetString().Should().Be("stack");
340-
stackEntry.GetProperty("lifecycle").GetString().Should().Be("ga");
341-
stackEntry.GetProperty("version").GetString().Should().Be("all");
342-
343-
var deploymentEntry = array[1];
344-
deploymentEntry.GetProperty("type").GetString().Should().Be("deployment");
345-
deploymentEntry.GetProperty("sub-type").GetString().Should().Be("ece");
346-
deploymentEntry.GetProperty("lifecycle").GetString().Should().Be("beta");
347-
deploymentEntry.GetProperty("version").GetString().Should().Be("3.0.0");
348-
}
349-
350316
[Fact]
351317
public void RoundTrip_EmptyApplicableTo()
352318
{
@@ -375,15 +341,15 @@ public void RoundTrip_Null_ReturnsNull()
375341
}
376342

377343
[Fact]
378-
public void RoundTrip_AllVersions_SerializesAsAll()
344+
public void RoundTrip_AllVersions_SerializesAsSemanticVersion()
379345
{
380346
var original = new ApplicableTo
381347
{
382348
Stack = new AppliesCollection([new Applicability { Lifecycle = ProductLifecycle.GenerallyAvailable, Version = AllVersions.Instance }])
383349
};
384350

385351
var json = JsonSerializer.Serialize(original, _options);
386-
json.Should().Contain("\"version\": \"all\"");
352+
json.Should().Contain("\"version\": \"9999.9999.9999\"");
387353

388354
var deserialized = JsonSerializer.Deserialize<ApplicableTo>(json, _options);
389355
deserialized.Should().NotBeNull();
Lines changed: 307 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,307 @@
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

Comments
 (0)