Skip to content

Commit 8edd198

Browse files
authored
Merge pull request #342 from Microsoft/perthcharern/AddTestForExampleInMediaType
Add more tests for examples in Media Type
2 parents 366b8c6 + 8b1cfba commit 8edd198

File tree

1 file changed

+321
-0
lines changed

1 file changed

+321
-0
lines changed

test/Microsoft.OpenApi.Tests/Models/OpenApiMediaTypeTests.cs

Lines changed: 321 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Microsoft.OpenApi.Extensions;
88
using Microsoft.OpenApi.Models;
99
using Xunit;
10+
using Xunit.Abstractions;
1011

1112
namespace Microsoft.OpenApi.Tests.Models
1213
{
@@ -24,6 +25,109 @@ public class OpenApiMediaTypeTests
2425
}
2526
};
2627

28+
public static OpenApiMediaType MediaTypeWithObjectExample = new OpenApiMediaType
29+
{
30+
Example = new OpenApiObject
31+
{
32+
["versions"] = new OpenApiArray
33+
{
34+
new OpenApiObject
35+
{
36+
["status"] = new OpenApiString("Status1"),
37+
["id"] = new OpenApiString("v1"),
38+
["links"] = new OpenApiArray
39+
{
40+
new OpenApiObject
41+
{
42+
["href"] = new OpenApiString("http://example.com/1"),
43+
["rel"] = new OpenApiString("sampleRel1")
44+
}
45+
}
46+
},
47+
48+
new OpenApiObject
49+
{
50+
["status"] = new OpenApiString("Status2"),
51+
["id"] = new OpenApiString("v2"),
52+
["links"] = new OpenApiArray
53+
{
54+
new OpenApiObject
55+
{
56+
["href"] = new OpenApiString("http://example.com/2"),
57+
["rel"] = new OpenApiString("sampleRel2")
58+
}
59+
}
60+
}
61+
}
62+
},
63+
Encoding = new Dictionary<string, OpenApiEncoding>
64+
{
65+
{"testEncoding", OpenApiEncodingTests.AdvanceEncoding}
66+
}
67+
};
68+
69+
public static OpenApiMediaType MediaTypeWithXmlExample = new OpenApiMediaType
70+
{
71+
Example = new OpenApiString("<xml>123</xml>"),
72+
Encoding = new Dictionary<string, OpenApiEncoding>
73+
{
74+
{"testEncoding", OpenApiEncodingTests.AdvanceEncoding}
75+
}
76+
};
77+
78+
public static OpenApiMediaType MediaTypeWithObjectExamples = new OpenApiMediaType
79+
{
80+
Examples = {
81+
["object1"] = new OpenApiExample
82+
{
83+
Value = new OpenApiObject
84+
{
85+
["versions"] = new OpenApiArray
86+
{
87+
new OpenApiObject
88+
{
89+
["status"] = new OpenApiString("Status1"),
90+
["id"] = new OpenApiString("v1"),
91+
["links"] = new OpenApiArray
92+
{
93+
new OpenApiObject
94+
{
95+
["href"] = new OpenApiString("http://example.com/1"),
96+
["rel"] = new OpenApiString("sampleRel1")
97+
}
98+
}
99+
},
100+
101+
new OpenApiObject
102+
{
103+
["status"] = new OpenApiString("Status2"),
104+
["id"] = new OpenApiString("v2"),
105+
["links"] = new OpenApiArray
106+
{
107+
new OpenApiObject
108+
{
109+
["href"] = new OpenApiString("http://example.com/2"),
110+
["rel"] = new OpenApiString("sampleRel2")
111+
}
112+
}
113+
}
114+
}
115+
}
116+
}
117+
},
118+
Encoding = new Dictionary<string, OpenApiEncoding>
119+
{
120+
{"testEncoding", OpenApiEncodingTests.AdvanceEncoding}
121+
}
122+
};
123+
124+
private readonly ITestOutputHelper _output;
125+
126+
public OpenApiMediaTypeTests(ITestOutputHelper output)
127+
{
128+
_output = output;
129+
}
130+
27131
[Theory]
28132
[InlineData(OpenApiFormat.Json, "{ }")]
29133
[InlineData(OpenApiFormat.Yaml, "{ }")]
@@ -85,5 +189,222 @@ public void SerializeAdvanceMediaTypeAsV3YamlWorks()
85189
expected = expected.MakeLineBreaksEnvironmentNeutral();
86190
actual.Should().Be(expected);
87191
}
192+
193+
[Fact]
194+
public void SerializeMediaTypeWithObjectExampleAsV3YamlWorks()
195+
{
196+
// Arrange
197+
var expected =
198+
@"example:
199+
versions:
200+
- status: Status1
201+
id: v1
202+
links:
203+
- href: http://example.com/1
204+
rel: sampleRel1
205+
- status: Status2
206+
id: v2
207+
links:
208+
- href: http://example.com/2
209+
rel: sampleRel2
210+
encoding:
211+
testEncoding:
212+
contentType: 'image/png, image/jpeg'
213+
style: simple
214+
explode: true
215+
allowReserved: true";
216+
217+
// Act
218+
var actual = MediaTypeWithObjectExample.SerializeAsYaml(OpenApiSpecVersion.OpenApi3_0);
219+
220+
// Assert
221+
actual = actual.MakeLineBreaksEnvironmentNeutral();
222+
expected = expected.MakeLineBreaksEnvironmentNeutral();
223+
actual.Should().Be(expected);
224+
}
225+
226+
[Fact]
227+
public void SerializeMediaTypeWithObjectExampleAsV3JsonWorks()
228+
{
229+
// Arrange
230+
var expected =
231+
@"{
232+
""example"": {
233+
""versions"": [
234+
{
235+
""status"": ""Status1"",
236+
""id"": ""v1"",
237+
""links"": [
238+
{
239+
""href"": ""http://example.com/1"",
240+
""rel"": ""sampleRel1""
241+
}
242+
]
243+
},
244+
{
245+
""status"": ""Status2"",
246+
""id"": ""v2"",
247+
""links"": [
248+
{
249+
""href"": ""http://example.com/2"",
250+
""rel"": ""sampleRel2""
251+
}
252+
]
253+
}
254+
]
255+
},
256+
""encoding"": {
257+
""testEncoding"": {
258+
""contentType"": ""image/png, image/jpeg"",
259+
""style"": ""simple"",
260+
""explode"": true,
261+
""allowReserved"": true
262+
}
263+
}
264+
}";
265+
266+
// Act
267+
var actual = MediaTypeWithObjectExample.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0);
268+
269+
// Assert
270+
actual = actual.MakeLineBreaksEnvironmentNeutral();
271+
expected = expected.MakeLineBreaksEnvironmentNeutral();
272+
actual.Should().Be(expected);
273+
}
274+
275+
[Fact]
276+
public void SerializeMediaTypeWithXmlExampleAsV3YamlWorks()
277+
{
278+
// Arrange
279+
var expected =
280+
@"example: <xml>123</xml>
281+
encoding:
282+
testEncoding:
283+
contentType: 'image/png, image/jpeg'
284+
style: simple
285+
explode: true
286+
allowReserved: true";
287+
288+
// Act
289+
var actual = MediaTypeWithXmlExample.SerializeAsYaml(OpenApiSpecVersion.OpenApi3_0);
290+
291+
// Assert
292+
actual = actual.MakeLineBreaksEnvironmentNeutral();
293+
expected = expected.MakeLineBreaksEnvironmentNeutral();
294+
actual.Should().Be(expected);
295+
}
296+
297+
[Fact]
298+
public void SerializeMediaTypeWithXmlExampleAsV3JsonWorks()
299+
{
300+
// Arrange
301+
var expected = @"{
302+
""example"": ""<xml>123</xml>"",
303+
""encoding"": {
304+
""testEncoding"": {
305+
""contentType"": ""image/png, image/jpeg"",
306+
""style"": ""simple"",
307+
""explode"": true,
308+
""allowReserved"": true
309+
}
310+
}
311+
}";
312+
313+
// Act
314+
var actual = MediaTypeWithXmlExample.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0);
315+
316+
// Assert
317+
actual = actual.MakeLineBreaksEnvironmentNeutral();
318+
expected = expected.MakeLineBreaksEnvironmentNeutral();
319+
actual.Should().Be(expected);
320+
}
321+
322+
[Fact]
323+
public void SerializeMediaTypeWithObjectExamplesAsV3YamlWorks()
324+
{
325+
// Arrange
326+
var expected = @"examples:
327+
object1:
328+
value:
329+
versions:
330+
- status: Status1
331+
id: v1
332+
links:
333+
- href: http://example.com/1
334+
rel: sampleRel1
335+
- status: Status2
336+
id: v2
337+
links:
338+
- href: http://example.com/2
339+
rel: sampleRel2
340+
encoding:
341+
testEncoding:
342+
contentType: 'image/png, image/jpeg'
343+
style: simple
344+
explode: true
345+
allowReserved: true";
346+
347+
// Act
348+
var actual = MediaTypeWithObjectExamples.SerializeAsYaml(OpenApiSpecVersion.OpenApi3_0);
349+
_output.WriteLine(actual);
350+
351+
// Assert
352+
actual = actual.MakeLineBreaksEnvironmentNeutral();
353+
expected = expected.MakeLineBreaksEnvironmentNeutral();
354+
actual.Should().Be(expected);
355+
}
356+
357+
[Fact]
358+
public void SerializeMediaTypeWithObjectExamplesAsV3JsonWorks()
359+
{
360+
// Arrange
361+
var expected = @"{
362+
""examples"": {
363+
""object1"": {
364+
""value"": {
365+
""versions"": [
366+
{
367+
""status"": ""Status1"",
368+
""id"": ""v1"",
369+
""links"": [
370+
{
371+
""href"": ""http://example.com/1"",
372+
""rel"": ""sampleRel1""
373+
}
374+
]
375+
},
376+
{
377+
""status"": ""Status2"",
378+
""id"": ""v2"",
379+
""links"": [
380+
{
381+
""href"": ""http://example.com/2"",
382+
""rel"": ""sampleRel2""
383+
}
384+
]
385+
}
386+
]
387+
}
388+
}
389+
},
390+
""encoding"": {
391+
""testEncoding"": {
392+
""contentType"": ""image/png, image/jpeg"",
393+
""style"": ""simple"",
394+
""explode"": true,
395+
""allowReserved"": true
396+
}
397+
}
398+
}";
399+
400+
// Act
401+
var actual = MediaTypeWithObjectExamples.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0);
402+
_output.WriteLine(actual);
403+
404+
// Assert
405+
actual = actual.MakeLineBreaksEnvironmentNeutral();
406+
expected = expected.MakeLineBreaksEnvironmentNeutral();
407+
actual.Should().Be(expected);
408+
}
88409
}
89410
}

0 commit comments

Comments
 (0)