From 8b74a56d90e0053c9f4371f6a5842696ffed73d3 Mon Sep 17 00:00:00 2001 From: Perth Charernwattanagul Date: Wed, 31 Oct 2018 15:04:16 -0700 Subject: [PATCH] Add more tests for examples in Media Type --- .../Models/OpenApiMediaTypeTests.cs | 321 ++++++++++++++++++ 1 file changed, 321 insertions(+) diff --git a/test/Microsoft.OpenApi.Tests/Models/OpenApiMediaTypeTests.cs b/test/Microsoft.OpenApi.Tests/Models/OpenApiMediaTypeTests.cs index d08dc13a9..da71c1acb 100644 --- a/test/Microsoft.OpenApi.Tests/Models/OpenApiMediaTypeTests.cs +++ b/test/Microsoft.OpenApi.Tests/Models/OpenApiMediaTypeTests.cs @@ -7,6 +7,7 @@ using Microsoft.OpenApi.Extensions; using Microsoft.OpenApi.Models; using Xunit; +using Xunit.Abstractions; namespace Microsoft.OpenApi.Tests.Models { @@ -24,6 +25,109 @@ public class OpenApiMediaTypeTests } }; + public static OpenApiMediaType MediaTypeWithObjectExample = new OpenApiMediaType + { + Example = new OpenApiObject + { + ["versions"] = new OpenApiArray + { + new OpenApiObject + { + ["status"] = new OpenApiString("Status1"), + ["id"] = new OpenApiString("v1"), + ["links"] = new OpenApiArray + { + new OpenApiObject + { + ["href"] = new OpenApiString("http://example.com/1"), + ["rel"] = new OpenApiString("sampleRel1") + } + } + }, + + new OpenApiObject + { + ["status"] = new OpenApiString("Status2"), + ["id"] = new OpenApiString("v2"), + ["links"] = new OpenApiArray + { + new OpenApiObject + { + ["href"] = new OpenApiString("http://example.com/2"), + ["rel"] = new OpenApiString("sampleRel2") + } + } + } + } + }, + Encoding = new Dictionary + { + {"testEncoding", OpenApiEncodingTests.AdvanceEncoding} + } + }; + + public static OpenApiMediaType MediaTypeWithXmlExample = new OpenApiMediaType + { + Example = new OpenApiString("123"), + Encoding = new Dictionary + { + {"testEncoding", OpenApiEncodingTests.AdvanceEncoding} + } + }; + + public static OpenApiMediaType MediaTypeWithObjectExamples = new OpenApiMediaType + { + Examples = { + ["object1"] = new OpenApiExample + { + Value = new OpenApiObject + { + ["versions"] = new OpenApiArray + { + new OpenApiObject + { + ["status"] = new OpenApiString("Status1"), + ["id"] = new OpenApiString("v1"), + ["links"] = new OpenApiArray + { + new OpenApiObject + { + ["href"] = new OpenApiString("http://example.com/1"), + ["rel"] = new OpenApiString("sampleRel1") + } + } + }, + + new OpenApiObject + { + ["status"] = new OpenApiString("Status2"), + ["id"] = new OpenApiString("v2"), + ["links"] = new OpenApiArray + { + new OpenApiObject + { + ["href"] = new OpenApiString("http://example.com/2"), + ["rel"] = new OpenApiString("sampleRel2") + } + } + } + } + } + } + }, + Encoding = new Dictionary + { + {"testEncoding", OpenApiEncodingTests.AdvanceEncoding} + } + }; + + private readonly ITestOutputHelper _output; + + public OpenApiMediaTypeTests(ITestOutputHelper output) + { + _output = output; + } + [Theory] [InlineData(OpenApiFormat.Json, "{ }")] [InlineData(OpenApiFormat.Yaml, "{ }")] @@ -85,5 +189,222 @@ public void SerializeAdvanceMediaTypeAsV3YamlWorks() expected = expected.MakeLineBreaksEnvironmentNeutral(); actual.Should().Be(expected); } + + [Fact] + public void SerializeMediaTypeWithObjectExampleAsV3YamlWorks() + { + // Arrange + var expected = + @"example: + versions: + - status: Status1 + id: v1 + links: + - href: http://example.com/1 + rel: sampleRel1 + - status: Status2 + id: v2 + links: + - href: http://example.com/2 + rel: sampleRel2 +encoding: + testEncoding: + contentType: 'image/png, image/jpeg' + style: simple + explode: true + allowReserved: true"; + + // Act + var actual = MediaTypeWithObjectExample.SerializeAsYaml(OpenApiSpecVersion.OpenApi3_0); + + // Assert + actual = actual.MakeLineBreaksEnvironmentNeutral(); + expected = expected.MakeLineBreaksEnvironmentNeutral(); + actual.Should().Be(expected); + } + + [Fact] + public void SerializeMediaTypeWithObjectExampleAsV3JsonWorks() + { + // Arrange + var expected = + @"{ + ""example"": { + ""versions"": [ + { + ""status"": ""Status1"", + ""id"": ""v1"", + ""links"": [ + { + ""href"": ""http://example.com/1"", + ""rel"": ""sampleRel1"" + } + ] + }, + { + ""status"": ""Status2"", + ""id"": ""v2"", + ""links"": [ + { + ""href"": ""http://example.com/2"", + ""rel"": ""sampleRel2"" + } + ] + } + ] + }, + ""encoding"": { + ""testEncoding"": { + ""contentType"": ""image/png, image/jpeg"", + ""style"": ""simple"", + ""explode"": true, + ""allowReserved"": true + } + } +}"; + + // Act + var actual = MediaTypeWithObjectExample.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0); + + // Assert + actual = actual.MakeLineBreaksEnvironmentNeutral(); + expected = expected.MakeLineBreaksEnvironmentNeutral(); + actual.Should().Be(expected); + } + + [Fact] + public void SerializeMediaTypeWithXmlExampleAsV3YamlWorks() + { + // Arrange + var expected = + @"example: 123 +encoding: + testEncoding: + contentType: 'image/png, image/jpeg' + style: simple + explode: true + allowReserved: true"; + + // Act + var actual = MediaTypeWithXmlExample.SerializeAsYaml(OpenApiSpecVersion.OpenApi3_0); + + // Assert + actual = actual.MakeLineBreaksEnvironmentNeutral(); + expected = expected.MakeLineBreaksEnvironmentNeutral(); + actual.Should().Be(expected); + } + + [Fact] + public void SerializeMediaTypeWithXmlExampleAsV3JsonWorks() + { + // Arrange + var expected = @"{ + ""example"": ""123"", + ""encoding"": { + ""testEncoding"": { + ""contentType"": ""image/png, image/jpeg"", + ""style"": ""simple"", + ""explode"": true, + ""allowReserved"": true + } + } +}"; + + // Act + var actual = MediaTypeWithXmlExample.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0); + + // Assert + actual = actual.MakeLineBreaksEnvironmentNeutral(); + expected = expected.MakeLineBreaksEnvironmentNeutral(); + actual.Should().Be(expected); + } + + [Fact] + public void SerializeMediaTypeWithObjectExamplesAsV3YamlWorks() + { + // Arrange + var expected = @"examples: + object1: + value: + versions: + - status: Status1 + id: v1 + links: + - href: http://example.com/1 + rel: sampleRel1 + - status: Status2 + id: v2 + links: + - href: http://example.com/2 + rel: sampleRel2 +encoding: + testEncoding: + contentType: 'image/png, image/jpeg' + style: simple + explode: true + allowReserved: true"; + + // Act + var actual = MediaTypeWithObjectExamples.SerializeAsYaml(OpenApiSpecVersion.OpenApi3_0); + _output.WriteLine(actual); + + // Assert + actual = actual.MakeLineBreaksEnvironmentNeutral(); + expected = expected.MakeLineBreaksEnvironmentNeutral(); + actual.Should().Be(expected); + } + + [Fact] + public void SerializeMediaTypeWithObjectExamplesAsV3JsonWorks() + { + // Arrange + var expected = @"{ + ""examples"": { + ""object1"": { + ""value"": { + ""versions"": [ + { + ""status"": ""Status1"", + ""id"": ""v1"", + ""links"": [ + { + ""href"": ""http://example.com/1"", + ""rel"": ""sampleRel1"" + } + ] + }, + { + ""status"": ""Status2"", + ""id"": ""v2"", + ""links"": [ + { + ""href"": ""http://example.com/2"", + ""rel"": ""sampleRel2"" + } + ] + } + ] + } + } + }, + ""encoding"": { + ""testEncoding"": { + ""contentType"": ""image/png, image/jpeg"", + ""style"": ""simple"", + ""explode"": true, + ""allowReserved"": true + } + } +}"; + + // Act + var actual = MediaTypeWithObjectExamples.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0); + _output.WriteLine(actual); + + // Assert + actual = actual.MakeLineBreaksEnvironmentNeutral(); + expected = expected.MakeLineBreaksEnvironmentNeutral(); + actual.Should().Be(expected); + } } } \ No newline at end of file