Skip to content

Commit 70768e1

Browse files
committed
Update tests to validate the load pattern
1 parent 25556cd commit 70768e1

12 files changed

+215
-431
lines changed

test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiCallbackTests.cs

Lines changed: 127 additions & 144 deletions
Large diffs are not rendered by default.

test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiContactTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@ public void ParseStringContactFragmentShouldSucceed()
2121
"email": "[email protected]"
2222
}
2323
""";
24-
var reader = new OpenApiStringReader();
2524

2625
// Act
27-
var contact = reader.ReadFragment<OpenApiContact>(input, OpenApiSpecVersion.OpenApi3_0, out var diagnostic);
26+
var contact = OpenApiContact.Parse(input, OpenApiSpecVersion.OpenApi3_0, out var diagnostic, OpenApiConstants.Json);
2827

2928
// Assert
3029
diagnostic.Should().BeEquivalentTo(new OpenApiDiagnostic());

test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiDiscriminatorTests.cs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,9 @@
22
// Licensed under the MIT license.
33

44
using System.IO;
5-
using System.Linq;
65
using FluentAssertions;
76
using Microsoft.OpenApi.Models;
87
using Microsoft.OpenApi.Reader;
9-
using Microsoft.OpenApi.Reader.ParseNodes;
10-
using Microsoft.OpenApi.Reader.V3;
11-
using SharpYaml.Serialization;
128
using Xunit;
139

1410
namespace Microsoft.OpenApi.Readers.Tests.V3Tests
@@ -18,23 +14,19 @@ public class OpenApiDiscriminatorTests
1814
{
1915
private const string SampleFolderPath = "V3Tests/Samples/OpenApiDiscriminator/";
2016

17+
public OpenApiDiscriminatorTests()
18+
{
19+
OpenApiReaderRegistry.RegisterReader(OpenApiConstants.Yaml, new OpenApiYamlReader());
20+
}
21+
2122
[Fact]
2223
public void ParseBasicDiscriminatorShouldSucceed()
2324
{
2425
// Arrange
2526
using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "basicDiscriminator.yaml"));
26-
var yamlStream = new YamlStream();
27-
yamlStream.Load(new StreamReader(stream));
28-
var yamlNode = yamlStream.Documents.First().RootNode;
29-
30-
var diagnostic = new OpenApiDiagnostic();
31-
var context = new ParsingContext(diagnostic);
32-
33-
var asJsonNode = yamlNode.ToJsonNode();
34-
var node = new MapNode(context, asJsonNode);
3527

3628
// Act
37-
var discriminator = OpenApiV3Deserializer.LoadDiscriminator(node);
29+
var discriminator = OpenApiDiscriminator.Load(stream, OpenApiConstants.Yaml, OpenApiSpecVersion.OpenApi3_0, out var diagnostic);
3830

3931
// Assert
4032
discriminator.Should().BeEquivalentTo(

test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiEncodingTests.cs

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,16 @@ public class OpenApiEncodingTests
1919
{
2020
private const string SampleFolderPath = "V3Tests/Samples/OpenApiEncoding/";
2121

22+
public OpenApiEncodingTests()
23+
{
24+
OpenApiReaderRegistry.RegisterReader(OpenApiConstants.Yaml, new OpenApiYamlReader());
25+
}
26+
2227
[Fact]
2328
public void ParseBasicEncodingShouldSucceed()
2429
{
25-
using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "basicEncoding.yaml"));
26-
var yamlStream = new YamlStream();
27-
yamlStream.Load(new StreamReader(stream));
28-
var yamlNode = yamlStream.Documents.First().RootNode;
29-
30-
var diagnostic = new OpenApiDiagnostic();
31-
var context = new ParsingContext(diagnostic);
32-
33-
var asJsonNode = yamlNode.ToJsonNode();
34-
var node = new MapNode(context, asJsonNode);
35-
3630
// Act
37-
var encoding = OpenApiV3Deserializer.LoadEncoding(node);
31+
var encoding = OpenApiEncoding.Load(Path.Combine(SampleFolderPath, "basicEncoding.yaml"), OpenApiSpecVersion.OpenApi3_0, out _);
3832

3933
// Assert
4034
encoding.Should().BeEquivalentTo(
@@ -48,18 +42,9 @@ public void ParseBasicEncodingShouldSucceed()
4842
public void ParseAdvancedEncodingShouldSucceed()
4943
{
5044
using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "advancedEncoding.yaml"));
51-
var yamlStream = new YamlStream();
52-
yamlStream.Load(new StreamReader(stream));
53-
var yamlNode = yamlStream.Documents.First().RootNode;
54-
55-
var diagnostic = new OpenApiDiagnostic();
56-
var context = new ParsingContext(diagnostic);
57-
58-
var asJsonNode = yamlNode.ToJsonNode();
59-
var node = new MapNode(context, asJsonNode);
6045

6146
// Act
62-
var encoding = OpenApiV3Deserializer.LoadEncoding(node);
47+
var encoding = OpenApiEncoding.Load(stream, OpenApiConstants.Yaml, OpenApiSpecVersion.OpenApi3_0, out var diagnostic);
6348

6449
// Assert
6550
encoding.Should().BeEquivalentTo(

test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiExampleTests.cs

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@
22
// Licensed under the MIT license.
33

44
using System.IO;
5-
using System.Linq;
65
using System.Text.Json.Nodes;
76
using FluentAssertions;
87
using Microsoft.OpenApi.Any;
98
using Microsoft.OpenApi.Models;
10-
using Microsoft.OpenApi.Reader.ParseNodes;
11-
using Microsoft.OpenApi.Reader.V3;
129
using Microsoft.OpenApi.Reader;
13-
using SharpYaml.Serialization;
1410
using Xunit;
1511

1612
namespace Microsoft.OpenApi.Readers.Tests.V3Tests
@@ -20,21 +16,15 @@ public class OpenApiExampleTests
2016
{
2117
private const string SampleFolderPath = "V3Tests/Samples/OpenApiExample/";
2218

19+
public OpenApiExampleTests()
20+
{
21+
OpenApiReaderRegistry.RegisterReader("yaml", new OpenApiYamlReader());
22+
}
23+
2324
[Fact]
2425
public void ParseAdvancedExampleShouldSucceed()
2526
{
26-
using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "advancedExample.yaml"));
27-
var yamlStream = new YamlStream();
28-
yamlStream.Load(new StreamReader(stream));
29-
var yamlNode = yamlStream.Documents.First().RootNode;
30-
31-
var diagnostic = new OpenApiDiagnostic();
32-
var context = new ParsingContext(diagnostic);
33-
34-
var asJsonNode = yamlNode.ToJsonNode();
35-
var node = new MapNode(context, asJsonNode);
36-
37-
var example = OpenApiV3Deserializer.LoadExample(node);
27+
var example = OpenApiExample.Load(Path.Combine(SampleFolderPath, "advancedExample.yaml"), OpenApiSpecVersion.OpenApi3_0, out var diagnostic);
3828
var expected = new OpenApiExample
3929
{
4030
Value = new OpenApiAny(new JsonObject
@@ -91,8 +81,7 @@ public void ParseAdvancedExampleShouldSucceed()
9181
[Fact]
9282
public void ParseExampleForcedStringSucceed()
9383
{
94-
using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "explicitString.yaml"));
95-
new OpenApiStreamReader().Read(stream, out var diagnostic);
84+
_ = OpenApiDocument.Load(Path.Combine(SampleFolderPath, "explicitString.yaml"), out var diagnostic);
9685
diagnostic.Errors.Should().BeEmpty();
9786
}
9887
}

test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiInfoTests.cs

Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,11 @@
33

44
using System;
55
using System.IO;
6-
using System.Linq;
76
using System.Text.Json.Nodes;
87
using FluentAssertions;
98
using Microsoft.OpenApi.Any;
109
using Microsoft.OpenApi.Models;
1110
using Microsoft.OpenApi.Reader;
12-
using Microsoft.OpenApi.Reader.ParseNodes;
13-
using Microsoft.OpenApi.Reader.V3;
14-
using SharpYaml.Serialization;
1511
using Xunit;
1612

1713
namespace Microsoft.OpenApi.Readers.Tests.V3Tests
@@ -21,23 +17,16 @@ public class OpenApiInfoTests
2117
{
2218
private const string SampleFolderPath = "V3Tests/Samples/OpenApiInfo/";
2319

20+
public OpenApiInfoTests()
21+
{
22+
OpenApiReaderRegistry.RegisterReader("yaml", new OpenApiYamlReader());
23+
}
24+
2425
[Fact]
2526
public void ParseAdvancedInfoShouldSucceed()
2627
{
27-
// Arrange
28-
using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "advancedInfo.yaml"));
29-
var yamlStream = new YamlStream();
30-
yamlStream.Load(new StreamReader(stream));
31-
var yamlNode = yamlStream.Documents.First().RootNode;
32-
33-
var diagnostic = new OpenApiDiagnostic();
34-
var context = new ParsingContext(diagnostic);
35-
36-
var asJsonNode = yamlNode.ToJsonNode();
37-
var node = new MapNode(context, asJsonNode);
38-
3928
// Act
40-
var openApiInfo = OpenApiV3Deserializer.LoadInfo(node);
29+
var openApiInfo = OpenApiInfo.Load(Path.Combine(SampleFolderPath, "advancedInfo.yaml"), OpenApiSpecVersion.OpenApi3_0, out var diagnostic);
4130

4231
// Assert
4332
openApiInfo.Should().BeEquivalentTo(
@@ -93,19 +82,8 @@ public void ParseAdvancedInfoShouldSucceed()
9382
[Fact]
9483
public void ParseBasicInfoShouldSucceed()
9584
{
96-
using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "basicInfo.yaml"));
97-
var yamlStream = new YamlStream();
98-
yamlStream.Load(new StreamReader(stream));
99-
var yamlNode = yamlStream.Documents.First().RootNode;
100-
101-
var diagnostic = new OpenApiDiagnostic();
102-
var context = new ParsingContext(diagnostic);
103-
104-
var asJsonNode = yamlNode.ToJsonNode();
105-
var node = new MapNode(context, asJsonNode);
106-
10785
// Act
108-
var openApiInfo = OpenApiV3Deserializer.LoadInfo(node);
86+
var openApiInfo = OpenApiInfo.Load(Path.Combine(SampleFolderPath, "basicInfo.yaml"), OpenApiSpecVersion.OpenApi3_0, out _);
10987

11088
// Assert
11189
openApiInfo.Should().BeEquivalentTo(
@@ -133,18 +111,9 @@ public void ParseBasicInfoShouldSucceed()
133111
public void ParseMinimalInfoShouldSucceed()
134112
{
135113
using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "minimalInfo.yaml"));
136-
var yamlStream = new YamlStream();
137-
yamlStream.Load(new StreamReader(stream));
138-
var yamlNode = yamlStream.Documents.First().RootNode;
139-
140-
var diagnostic = new OpenApiDiagnostic();
141-
var context = new ParsingContext(diagnostic);
142-
143-
var asJsonNode = yamlNode.ToJsonNode();
144-
var node = new MapNode(context, asJsonNode);
145114

146115
// Act
147-
var openApiInfo = OpenApiV3Deserializer.LoadInfo(node);
116+
var openApiInfo = OpenApiInfo.Load(stream, "yaml", OpenApiSpecVersion.OpenApi3_0, out _);
148117

149118
// Assert
150119
openApiInfo.Should().BeEquivalentTo(

test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiMediaTypeTests.cs

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
using Json.Schema;
77
using Microsoft.OpenApi.Any;
88
using Microsoft.OpenApi.Models;
9-
using Microsoft.OpenApi.Reader.ParseNodes;
10-
using Microsoft.OpenApi.Reader.V3;
9+
using Microsoft.OpenApi.Reader;
1110
using Xunit;
1211

1312
namespace Microsoft.OpenApi.Readers.Tests.V3Tests
@@ -17,18 +16,16 @@ public class OpenApiMediaTypeTests
1716
{
1817
private const string SampleFolderPath = "V3Tests/Samples/OpenApiMediaType/";
1918

19+
public OpenApiMediaTypeTests()
20+
{
21+
OpenApiReaderRegistry.RegisterReader("yaml", new OpenApiYamlReader());
22+
}
23+
2024
[Fact]
2125
public void ParseMediaTypeWithExampleShouldSucceed()
2226
{
23-
// Arrange
24-
MapNode node;
25-
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "mediaTypeWithExample.yaml")))
26-
{
27-
node = TestHelper.CreateYamlMapNode(stream);
28-
}
29-
3027
// Act
31-
var mediaType = OpenApiV3Deserializer.LoadMediaType(node);
28+
var mediaType = OpenApiMediaType.Load(Path.Combine(SampleFolderPath, "mediaTypeWithExample.yaml"), OpenApiSpecVersion.OpenApi3_0, out var diagnostic);
3229

3330
// Assert
3431
mediaType.Should().BeEquivalentTo(
@@ -44,15 +41,8 @@ public void ParseMediaTypeWithExampleShouldSucceed()
4441
[Fact]
4542
public void ParseMediaTypeWithExamplesShouldSucceed()
4643
{
47-
// Arrange
48-
MapNode node;
49-
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "mediaTypeWithExamples.yaml")))
50-
{
51-
node = TestHelper.CreateYamlMapNode(stream);
52-
}
53-
5444
// Act
55-
var mediaType = OpenApiV3Deserializer.LoadMediaType(node);
45+
var mediaType = OpenApiMediaType.Load(Path.Combine(SampleFolderPath, "mediaTypeWithExamples.yaml"), OpenApiSpecVersion.OpenApi3_0, out var diagnostic);
5646

5747
// Assert
5848
mediaType.Should().BeEquivalentTo(

test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiOperationTests.cs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
using FluentAssertions;
77
using Json.Schema;
88
using Microsoft.OpenApi.Models;
9-
using Microsoft.OpenApi.Reader.ParseNodes;
10-
using Microsoft.OpenApi.Reader.V3;
9+
using Microsoft.OpenApi.Reader;
1110
using Xunit;
1211

1312
namespace Microsoft.OpenApi.Readers.Tests.V3Tests
@@ -16,11 +15,15 @@ public class OpenApiOperationTests
1615
{
1716
private const string SampleFolderPath = "V3Tests/Samples/OpenApiOperation/";
1817

18+
public OpenApiOperationTests()
19+
{
20+
OpenApiReaderRegistry.RegisterReader("yaml", new OpenApiYamlReader());
21+
}
22+
1923
[Fact]
2024
public void OperationWithSecurityRequirementShouldReferenceSecurityScheme()
2125
{
22-
using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "securedOperation.yaml"));
23-
var openApiDoc = new OpenApiStreamReader().Read(stream, out var diagnostic);
26+
var openApiDoc = OpenApiDocument.Load(Path.Combine(SampleFolderPath, "securedOperation.yaml"), out var diagnostic);
2427

2528
var securityRequirement = openApiDoc.Paths["/"].Operations[OperationType.Get].Security.First();
2629

@@ -30,15 +33,8 @@ public void OperationWithSecurityRequirementShouldReferenceSecurityScheme()
3033
[Fact]
3134
public void ParseOperationWithParameterWithNoLocationShouldSucceed()
3235
{
33-
// Arrange
34-
MapNode node;
35-
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "operationWithParameterWithNoLocation.json")))
36-
{
37-
node = TestHelper.CreateYamlMapNode(stream);
38-
}
39-
4036
// Act
41-
var operation = OpenApiV3Deserializer.LoadOperation(node);
37+
var operation = OpenApiOperation.Load(Path.Combine(SampleFolderPath, "operationWithParameterWithNoLocation.json"), OpenApiSpecVersion.OpenApi3_0, out _);
4238

4339
// Assert
4440
operation.Should().BeEquivalentTo(new OpenApiOperation

0 commit comments

Comments
 (0)