Skip to content

Commit 99a80b9

Browse files
committed
Clean up tests; refactor to use async
1 parent 8521082 commit 99a80b9

22 files changed

+186
-200
lines changed

src/Microsoft.OpenApi/Models/OpenApiDocument.cs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public void SerializeAsV31(IOpenApiWriter writer)
137137

138138
writer.WriteStartObject();
139139

140-
// openApi;
140+
// openApi
141141
writer.WriteProperty(OpenApiConstants.OpenApi, "3.1.1");
142142

143143
// jsonSchemaDialect
@@ -535,17 +535,6 @@ private static string ConvertByteArrayToString(byte[] hash)
535535
return Workspace?.ResolveReference<IOpenApiReferenceable>(uriLocation);
536536
}
537537

538-
/// <summary>
539-
/// Parses a local file path or Url into an Open API document.
540-
/// </summary>
541-
/// <param name="url"> The path to the OpenAPI file.</param>
542-
/// <param name="settings"></param>
543-
/// <returns></returns>
544-
public static ReadResult Load(string url, OpenApiReaderSettings? settings = null)
545-
{
546-
return OpenApiModelFactory.Load(url, settings);
547-
}
548-
549538
/// <summary>
550539
/// Reads the stream input and parses it into an Open API document.
551540
/// </summary>

test/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiFilterServiceTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,15 +224,15 @@ public void ThrowsInvalidOperationExceptionInCreatePredicateWhenInvalidArguments
224224
}
225225

226226
[Fact]
227-
public void CopiesOverAllReferencedComponentsToTheSubsetDocumentCorrectly()
227+
public async Task CopiesOverAllReferencedComponentsToTheSubsetDocumentCorrectly()
228228
{
229229
// Arrange
230230
var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "UtilityFiles", "docWithReusableHeadersAndExamples.yaml");
231231
var operationIds = "getItems";
232232

233233
// Act
234234
using var stream = File.OpenRead(filePath);
235-
var doc = OpenApiDocument.Load(stream, "yaml").OpenApiDocument;
235+
var doc = (await OpenApiDocument.LoadAsync(stream, "yaml")).OpenApiDocument;
236236

237237
var predicate = OpenApiFilterService.CreatePredicate(operationIds: operationIds);
238238
var subsetOpenApiDocument = OpenApiFilterService.CreateFilteredDocument(doc, predicate);

test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/OpenApiDiagnosticTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@ public OpenApiDiagnosticTests()
2323
}
2424

2525
[Fact]
26-
public void DetectedSpecificationVersionShouldBeV2_0()
26+
public async Task DetectedSpecificationVersionShouldBeV2_0()
2727
{
28-
var actual = OpenApiDocument.Load("V2Tests/Samples/basic.v2.yaml");
28+
var actual = await OpenApiDocument.LoadAsync("V2Tests/Samples/basic.v2.yaml");
2929

3030
actual.OpenApiDiagnostic.Should().NotBeNull();
3131
actual.OpenApiDiagnostic.SpecificationVersion.Should().Be(OpenApiSpecVersion.OpenApi2_0);
3232
}
3333

3434
[Fact]
35-
public void DetectedSpecificationVersionShouldBeV3_0()
35+
public async Task DetectedSpecificationVersionShouldBeV3_0()
3636
{
37-
var actual = OpenApiDocument.Load("V3Tests/Samples/OpenApiDocument/minimalDocument.yaml");
37+
var actual = await OpenApiDocument.LoadAsync("V3Tests/Samples/OpenApiDocument/minimalDocument.yaml");
3838

3939
actual.OpenApiDiagnostic.Should().NotBeNull();
4040
actual.OpenApiDiagnostic.SpecificationVersion.Should().Be(OpenApiSpecVersion.OpenApi3_0);

test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/OpenApiStreamReaderTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,20 @@ public OpenApiStreamReaderTests()
2121
}
2222

2323
[Fact]
24-
public void StreamShouldCloseIfLeaveStreamOpenSettingEqualsFalse()
24+
public async Task StreamShouldCloseIfLeaveStreamOpenSettingEqualsFalse()
2525
{
2626
using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "petStore.yaml"));
2727
var settings = new OpenApiReaderSettings { LeaveStreamOpen = false };
28-
_ = OpenApiDocument.Load(stream, "yaml", settings);
28+
_ = await OpenApiDocument.LoadAsync(stream, "yaml", settings);
2929
Assert.False(stream.CanRead);
3030
}
3131

3232
[Fact]
33-
public void StreamShouldNotCloseIfLeaveStreamOpenSettingEqualsTrue()
33+
public async Task StreamShouldNotCloseIfLeaveStreamOpenSettingEqualsTrue()
3434
{
3535
using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "petStore.yaml"));
3636
var settings = new OpenApiReaderSettings { LeaveStreamOpen = true };
37-
_ = OpenApiDocument.Load(stream, "yaml", settings);
37+
_ = await OpenApiDocument.LoadAsync(stream, "yaml", settings);
3838
Assert.True(stream.CanRead);
3939
}
4040

@@ -48,7 +48,7 @@ public async Task StreamShouldNotBeDisposedIfLeaveStreamOpenSettingIsTrueAsync()
4848
memoryStream.Position = 0;
4949
var stream = memoryStream;
5050

51-
var result = OpenApiDocument.Load(stream, "yaml", new OpenApiReaderSettings { LeaveStreamOpen = true });
51+
_ = await OpenApiDocument.LoadAsync(stream, "yaml", new OpenApiReaderSettings { LeaveStreamOpen = true });
5252
stream.Seek(0, SeekOrigin.Begin); // does not throw an object disposed exception
5353
Assert.True(stream.CanRead);
5454
}
@@ -64,7 +64,7 @@ public async Task StreamShouldReadWhenInitializedAsync()
6464
var stream = await httpClient.GetStreamAsync("20fe7a7b720a0e48e5842d002ac418b12a8201df/tests/v3.0/pass/petstore.yaml");
6565

6666
// Read V3 as YAML
67-
var result = OpenApiDocument.Load(stream, "yaml");
67+
var result = await OpenApiDocument.LoadAsync(stream, "yaml");
6868
Assert.NotNull(result.OpenApiDocument);
6969
}
7070
}

test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/UnsupportedSpecVersionTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4+
using System.Threading.Tasks;
45
using FluentAssertions;
56
using Microsoft.OpenApi.Exceptions;
67
using Microsoft.OpenApi.Models;
@@ -12,11 +13,11 @@ namespace Microsoft.OpenApi.Readers.Tests.OpenApiReaderTests
1213
public class UnsupportedSpecVersionTests
1314
{
1415
[Fact]
15-
public void ThrowOpenApiUnsupportedSpecVersionException()
16+
public async Task ThrowOpenApiUnsupportedSpecVersionException()
1617
{
1718
try
1819
{
19-
_ = OpenApiDocument.Load("OpenApiReaderTests/Samples/unsupported.v1.yaml");
20+
_ = await OpenApiDocument.LoadAsync("OpenApiReaderTests/Samples/unsupported.v1.yaml");
2021
}
2122
catch (OpenApiUnsupportedSpecVersionException exception)
2223
{

test/Microsoft.OpenApi.Readers.Tests/ReferenceService/TryLoadReferenceV2Tests.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Collections.Generic;
55
using System.IO;
6+
using System.Threading.Tasks;
67
using FluentAssertions;
78
using Microsoft.OpenApi.Models;
89
using Microsoft.OpenApi.Models.References;
@@ -22,10 +23,10 @@ public TryLoadReferenceV2Tests()
2223
}
2324

2425
[Fact]
25-
public void LoadParameterReference()
26+
public async Task LoadParameterReference()
2627
{
2728
// Arrange
28-
var result = OpenApiDocument.Load(Path.Combine(SampleFolderPath, "multipleReferences.v2.yaml"));
29+
var result = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "multipleReferences.v2.yaml"));
2930
var reference = new OpenApiParameterReference("skipParam", result.OpenApiDocument);
3031

3132
// Assert
@@ -47,9 +48,9 @@ public void LoadParameterReference()
4748
}
4849

4950
[Fact]
50-
public void LoadSecuritySchemeReference()
51+
public async Task LoadSecuritySchemeReference()
5152
{
52-
var result = OpenApiDocument.Load(Path.Combine(SampleFolderPath, "multipleReferences.v2.yaml"));
53+
var result = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "multipleReferences.v2.yaml"));
5354

5455
var reference = new OpenApiSecuritySchemeReference("api_key_sample", result.OpenApiDocument);
5556

@@ -65,9 +66,9 @@ public void LoadSecuritySchemeReference()
6566
}
6667

6768
[Fact]
68-
public void LoadResponseReference()
69+
public async Task LoadResponseReference()
6970
{
70-
var result = OpenApiDocument.Load(Path.Combine(SampleFolderPath, "multipleReferences.v2.yaml"));
71+
var result = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "multipleReferences.v2.yaml"));
7172

7273
var reference = new OpenApiResponseReference("NotFound", result.OpenApiDocument);
7374

@@ -85,9 +86,9 @@ public void LoadResponseReference()
8586
}
8687

8788
[Fact]
88-
public void LoadResponseAndSchemaReference()
89+
public async Task LoadResponseAndSchemaReference()
8990
{
90-
var result = OpenApiDocument.Load(Path.Combine(SampleFolderPath, "multipleReferences.v2.yaml"));
91+
var result = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "multipleReferences.v2.yaml"));
9192
var reference = new OpenApiResponseReference("GeneralError", result.OpenApiDocument);
9293

9394
// Assert

test/Microsoft.OpenApi.Readers.Tests/V2Tests/ComparisonTests.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

44
using System.IO;
5+
using System.Threading.Tasks;
56
using FluentAssertions;
67
using Microsoft.OpenApi.Models;
78
using Microsoft.OpenApi.Reader;
@@ -18,13 +19,13 @@ public class ComparisonTests
1819
[InlineData("minimal")]
1920
[InlineData("basic")]
2021
//[InlineData("definitions")] //Currently broken due to V3 references not behaving the same as V2
21-
public void EquivalentV2AndV3DocumentsShouldProduceEquivalentObjects(string fileName)
22+
public async Task EquivalentV2AndV3DocumentsShouldProduceEquivalentObjects(string fileName)
2223
{
2324
OpenApiReaderRegistry.RegisterReader(OpenApiConstants.Yaml, new OpenApiYamlReader());
2425
using var streamV2 = Resources.GetStream(Path.Combine(SampleFolderPath, $"{fileName}.v2.yaml"));
2526
using var streamV3 = Resources.GetStream(Path.Combine(SampleFolderPath, $"{fileName}.v3.yaml"));
26-
var result1 = OpenApiDocument.Load(Path.Combine(SampleFolderPath, $"{fileName}.v2.yaml"));
27-
var result2 = OpenApiDocument.Load(Path.Combine(SampleFolderPath, $"{fileName}.v3.yaml"));
27+
var result1 = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, $"{fileName}.v2.yaml"));
28+
var result2 = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, $"{fileName}.v3.yaml"));
2829

2930
result2.OpenApiDocument.Should().BeEquivalentTo(result1.OpenApiDocument,
3031
options => options.Excluding(x => x.Workspace).Excluding(y => y.BaseUri));

test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiDocumentTests.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.IO;
77
using System.Linq;
88
using System.Threading;
9+
using System.Threading.Tasks;
910
using FluentAssertions;
1011
using FluentAssertions.Equivalency;
1112
using Microsoft.OpenApi.Any;
@@ -96,13 +97,13 @@ public void ParseDocumentWithDifferentCultureShouldSucceed(string culture)
9697
.Excluding((IMemberInfo memberInfo) =>
9798
memberInfo.Path.EndsWith("Parent"))
9899
.Excluding((IMemberInfo memberInfo) =>
99-
memberInfo.Path.EndsWith("Root")));;
100+
memberInfo.Path.EndsWith("Root")));
100101
}
101102

102103
[Fact]
103-
public void ShouldParseProducesInAnyOrder()
104+
public async Task ShouldParseProducesInAnyOrder()
104105
{
105-
var result = OpenApiDocument.Load(Path.Combine(SampleFolderPath, "twoResponses.json"));
106+
var result = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "twoResponses.json"));
106107

107108
var okSchema = new OpenApiSchema
108109
{
@@ -259,10 +260,10 @@ public void ShouldParseProducesInAnyOrder()
259260
}
260261

261262
[Fact]
262-
public void ShouldAssignSchemaToAllResponses()
263+
public async Task ShouldAssignSchemaToAllResponses()
263264
{
264265
using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "multipleProduces.json"));
265-
var result = OpenApiDocument.Load(stream, OpenApiConstants.Json);
266+
var result = await OpenApiDocument.LoadAsync(stream, OpenApiConstants.Json);
266267

267268
Assert.Equal(OpenApiSpecVersion.OpenApi2_0, result.OpenApiDiagnostic.SpecificationVersion);
268269

@@ -289,10 +290,10 @@ public void ShouldAssignSchemaToAllResponses()
289290
}
290291

291292
[Fact]
292-
public void ShouldAllowComponentsThatJustContainAReference()
293+
public async Task ShouldAllowComponentsThatJustContainAReference()
293294
{
294295
// Act
295-
var actual = OpenApiDocument.Load(Path.Combine(SampleFolderPath, "ComponentRootReference.json")).OpenApiDocument;
296+
var actual = (await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "ComponentRootReference.json"))).OpenApiDocument;
296297
var schema1 = actual.Components.Schemas["AllPets"];
297298
Assert.False(schema1.UnresolvedReference);
298299
var schema2 = actual.ResolveReferenceTo<OpenApiSchema>(schema1.Reference);
@@ -304,14 +305,14 @@ public void ShouldAllowComponentsThatJustContainAReference()
304305
}
305306

306307
[Fact]
307-
public void ParseDocumentWithDefaultContentTypeSettingShouldSucceed()
308+
public async Task ParseDocumentWithDefaultContentTypeSettingShouldSucceed()
308309
{
309310
var settings = new OpenApiReaderSettings
310311
{
311312
DefaultContentType = ["application/json"]
312313
};
313314

314-
var actual = OpenApiDocument.Load(Path.Combine(SampleFolderPath, "docWithEmptyProduces.yaml"), settings);
315+
var actual = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "docWithEmptyProduces.yaml"), settings);
315316
var mediaType = actual.OpenApiDocument.Paths["/example"].Operations[OperationType.Get].Responses["200"].Content;
316317
Assert.Contains("application/json", mediaType);
317318
}
@@ -320,8 +321,7 @@ public void ParseDocumentWithDefaultContentTypeSettingShouldSucceed()
320321
public void testContentType()
321322
{
322323
var contentType = "application/json; charset = utf-8";
323-
var res = contentType.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).First();
324-
var expected = res.Split('/').LastOrDefault();
324+
var res = contentType.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)[0];
325325
Assert.Equal("application/json", res);
326326
}
327327
}

test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiDocumentTests.cs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ public OpenApiDocumentTests()
2626
}
2727

2828
[Fact]
29-
public void ParseDocumentWithWebhooksShouldSucceed()
29+
public async Task ParseDocumentWithWebhooksShouldSucceed()
3030
{
3131
// Arrange and Act
32-
var actual = OpenApiDocument.Load(Path.Combine(SampleFolderPath, "documentWithWebhooks.yaml"));
32+
var actual = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "documentWithWebhooks.yaml"));
3333
var petSchema = new OpenApiSchemaReference("petSchema", actual.OpenApiDocument);
3434

3535
var newPetSchema = new OpenApiSchemaReference("newPetSchema", actual.OpenApiDocument);
@@ -205,10 +205,10 @@ public void ParseDocumentWithWebhooksShouldSucceed()
205205
}
206206

207207
[Fact]
208-
public void ParseDocumentsWithReusablePathItemInWebhooksSucceeds()
208+
public async Task ParseDocumentsWithReusablePathItemInWebhooksSucceeds()
209209
{
210210
// Arrange && Act
211-
var actual = OpenApiDocument.Load("V31Tests/Samples/OpenApiDocument/documentWithReusablePaths.yaml");
211+
var actual = await OpenApiDocument.LoadAsync("V31Tests/Samples/OpenApiDocument/documentWithReusablePaths.yaml");
212212

213213
var components = new OpenApiComponents
214214
{
@@ -397,29 +397,28 @@ public void ParseDocumentsWithReusablePathItemInWebhooksSucceeds()
397397
var outputWriter = new StringWriter(CultureInfo.InvariantCulture);
398398
var writer = new OpenApiJsonWriter(outputWriter, new() { InlineLocalReferences = true });
399399
actual.OpenApiDocument.SerializeAsV31(writer);
400-
var serialized = outputWriter.ToString();
401400
}
402401

403402
[Fact]
404-
public void ParseDocumentWithExampleInSchemaShouldSucceed()
403+
public async Task ParseDocumentWithExampleInSchemaShouldSucceed()
405404
{
406405
// Arrange
407406
var outputStringWriter = new StringWriter(CultureInfo.InvariantCulture);
408407
var writer = new OpenApiJsonWriter(outputStringWriter, new OpenApiJsonWriterSettings { Terse = false });
409408

410409
// Act
411-
var actual = OpenApiDocument.Load(Path.Combine(SampleFolderPath, "docWithExample.yaml"));
410+
var actual = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "docWithExample.yaml"));
412411
actual.OpenApiDocument.SerializeAsV31(writer);
413412

414413
// Assert
415414
Assert.NotNull(actual);
416415
}
417416

418417
[Fact]
419-
public void ParseDocumentWithPatternPropertiesInSchemaWorks()
418+
public async Task ParseDocumentWithPatternPropertiesInSchemaWorks()
420419
{
421420
// Arrange and Act
422-
var result = OpenApiDocument.Load(Path.Combine(SampleFolderPath, "docWithPatternPropertiesInSchema.yaml"));
421+
var result = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "docWithPatternPropertiesInSchema.yaml"));
423422
var actualSchema = result.OpenApiDocument.Paths["/example"].Operations[OperationType.Get].Responses["200"].Content["application/json"].Schema;
424423

425424
var expectedSchema = new OpenApiSchema
@@ -473,10 +472,10 @@ public void ParseDocumentWithPatternPropertiesInSchemaWorks()
473472
}
474473

475474
[Fact]
476-
public void ParseDocumentWithReferenceByIdGetsResolved()
475+
public async Task ParseDocumentWithReferenceByIdGetsResolved()
477476
{
478477
// Arrange and Act
479-
var result = OpenApiDocument.Load(Path.Combine(SampleFolderPath, "docWithReferenceById.yaml"));
478+
var result = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "docWithReferenceById.yaml"));
480479

481480
var responseSchema = result.OpenApiDocument.Paths["/resource"].Operations[OperationType.Get].Responses["200"].Content["application/json"].Schema;
482481
var requestBodySchema = result.OpenApiDocument.Paths["/resource"].Operations[OperationType.Post].RequestBody.Content["application/json"].Schema;
@@ -523,9 +522,9 @@ public async Task ParseExternalDocumentDereferenceToOpenApiDocumentByIdWorks()
523522

524523
// Act
525524
var result = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "externalRefById.yaml"), settings);
526-
var doc2 = OpenApiDocument.Load(Path.Combine(SampleFolderPath, "externalResource.yaml")).OpenApiDocument;
525+
var doc2 = (await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "externalResource.yaml"))).OpenApiDocument;
527526

528-
var requestBodySchema = result.OpenApiDocument.Paths["/resource"].Operations[OperationType.Get].Parameters.First().Schema;
527+
var requestBodySchema = result.OpenApiDocument.Paths["/resource"].Operations[OperationType.Get].Parameters[0].Schema;
529528
result.OpenApiDocument.Workspace.RegisterComponents(doc2);
530529

531530
// Assert
@@ -536,10 +535,10 @@ public async Task ParseExternalDocumentDereferenceToOpenApiDocumentByIdWorks()
536535
public async Task ParseDocumentWith31PropertiesWorks()
537536
{
538537
var path = Path.Combine(SampleFolderPath, "documentWith31Properties.yaml");
539-
var doc = OpenApiDocument.Load(path).OpenApiDocument;
538+
var doc = (await OpenApiDocument.LoadAsync(path)).OpenApiDocument;
540539
var outputStringWriter = new StringWriter();
541540
doc.SerializeAsV31(new OpenApiYamlWriter(outputStringWriter));
542-
outputStringWriter.Flush();
541+
await outputStringWriter.FlushAsync();
543542
var actual = outputStringWriter.GetStringBuilder().ToString();
544543

545544
// Assert

0 commit comments

Comments
 (0)