Skip to content

Commit

Permalink
Refactor OpenApiReaderTests and OpenApiDocumentExtensions
Browse files Browse the repository at this point in the history
  • Loading branch information
christianhelle committed Jan 4, 2024
1 parent fd4e270 commit 403f148
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 48 deletions.
17 changes: 17 additions & 0 deletions src/OasReader.Tests/EmbeddedResources.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Reflection;

namespace OasReader.Tests;

internal class EmbeddedResources
{
public static string GetStream(string name)
{
var assembly = Assembly.GetExecutingAssembly();
var resourceName = $"OasReader.Tests.Resources.{name}";

using var stream = assembly.GetManifestResourceStream(resourceName);
using var reader = new StreamReader(stream ?? throw new InvalidOperationException($"Could not find the embedded resource {name}"));

return reader.ReadToEnd();
}
}
35 changes: 35 additions & 0 deletions src/OasReader.Tests/OpenApiDocumentExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using FluentAssertions;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Readers;
using Xunit;

namespace OasReader.Tests;

public class OpenApiDocumentExtensionsTests
{
[Theory]
[InlineData("bot.yaml", "bot.components.yaml")]
[InlineData("petstore.yaml", "petstore.components.yaml")]
public async Task Returns_NotNull(string apiFile, string componentsFile)
{
var folder = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(folder);

var componentsFilename = Path.Combine(folder, componentsFile);
var openapiFilename = Path.Combine(folder, apiFile);

var componentContents = EmbeddedResources.GetStream(componentsFile);
var apiContents = EmbeddedResources.GetStream(apiFile);

await File.WriteAllTextAsync(componentsFilename, componentContents);
await File.WriteAllTextAsync(openapiFilename, apiContents);

var file = File.OpenRead(openapiFilename);
var textReader = new StreamReader(file);
var reader = new OpenApiTextReaderReader();
var result = await reader.ReadAsync(textReader, CancellationToken.None);
OpenApiDocument sut = result.OpenApiDocument;

sut.ContainsExternalReferences().Should().BeTrue();
}
}
94 changes: 47 additions & 47 deletions src/OasReader.Tests/OpenApiReaderTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Reflection;
using FluentAssertions;
using FluentAssertions;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Readers;
using Xunit;
Expand All @@ -8,66 +7,67 @@ namespace OasReader.Tests;

public class OpenApiReaderTests
{
[Fact]
public async Task Returns_NotNull()
[Theory]
[InlineData("bot.yaml", "bot.components.yaml")]
[InlineData("petstore.yaml", "petstore.components.yaml")]
public async Task Returns_NotNull(string apiFile, string componentsFile)
{
OpenApiDocument result = await Arrange();
OpenApiDocument result = await Arrange(apiFile, componentsFile);
result.Should().NotBeNull();
}

[Theory]
[InlineData("BaseGuildID")]
[InlineData("BaseChannelID")]
[InlineData("BaseChannelCategoryID")]
[InlineData("BaseUserID")]
[InlineData("BaseRoleID")]
[InlineData("BaseMessageID")]
[InlineData("BaseSequenceInChannel")]
[InlineData("BaseScheduleID")]
[InlineData("Guild")]
[InlineData("User")]
[InlineData("Channel")]
[InlineData("Member")]
[InlineData("Role")]
[InlineData("ChannelPermissions")]
[InlineData("Message")]
public async Task Returns_Document_With_External_Schemas(string schemaName)
[InlineData("bot.yaml", "bot.components.yaml")]
[InlineData("petstore.yaml", "petstore.components.yaml")]
public async Task Returns_Components_Schemas_NotNull(string apiFile, string componentsFile)
{
OpenApiDocument result = await Arrange();
OpenApiDocument result = await Arrange(apiFile, componentsFile);
result.Components.Should().NotBeNull();
result.Components.Schemas.Should().NotBeNull();
}

[Theory]
[InlineData("bot.yaml", "bot.components.yaml", "BaseGuildID")]
[InlineData("bot.yaml", "bot.components.yaml", "BaseChannelID")]
[InlineData("bot.yaml", "bot.components.yaml", "BaseChannelCategoryID")]
[InlineData("bot.yaml", "bot.components.yaml", "BaseUserID")]
[InlineData("bot.yaml", "bot.components.yaml", "BaseRoleID")]
[InlineData("bot.yaml", "bot.components.yaml", "BaseMessageID")]
[InlineData("bot.yaml", "bot.components.yaml", "BaseSequenceInChannel")]
[InlineData("bot.yaml", "bot.components.yaml", "BaseScheduleID")]
[InlineData("bot.yaml", "bot.components.yaml", "Guild")]
[InlineData("bot.yaml", "bot.components.yaml", "User")]
[InlineData("bot.yaml", "bot.components.yaml", "Channel")]
[InlineData("bot.yaml", "bot.components.yaml", "Member")]
[InlineData("bot.yaml", "bot.components.yaml", "Role")]
[InlineData("bot.yaml", "bot.components.yaml", "ChannelPermissions")]
[InlineData("bot.yaml", "bot.components.yaml", "Message")]
[InlineData("petstore.yaml", "petstore.components.yaml", "Order")]
[InlineData("petstore.yaml", "petstore.components.yaml", "Pet")]
[InlineData("petstore.yaml", "petstore.components.yaml", "Tag")]
[InlineData("petstore.yaml", "petstore.components.yaml", "Category")]
[InlineData("petstore.yaml", "petstore.components.yaml", "ApiResponse")]
public async Task Returns_Document_With_External_Schemas(string apiFile, string componentsFile, string schemaName)
{
OpenApiDocument result = await Arrange(apiFile, componentsFile);
result.Components.Schemas.Should().ContainKey(schemaName);

}

private static async Task<OpenApiDocument> Arrange()
private static async Task<OpenApiDocument> Arrange(string apiFile, string componentsFile)
{
var folder = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(folder);

var componentsFilename = Path.Combine(folder, "components.yaml");
var openapiFilename = Path.Combine(folder, "openapi.yaml");

await File.WriteAllTextAsync(componentsFilename, EmbeddedResources.Components);
await File.WriteAllTextAsync(openapiFilename, EmbeddedResources.OpenApi);

var result = await OpenApiMultiFileReader.Read(openapiFilename);
return result;
}
}
var componentsFilename = Path.Combine(folder, componentsFile);
var openapiFilename = Path.Combine(folder, apiFile);

internal class EmbeddedResources
{
public static string GetStream(string name)
{
var assembly = Assembly.GetExecutingAssembly();
var resourceName = $"OasReader.Tests.Resources.{name}";
var componentContents = EmbeddedResources.GetStream(componentsFile);
var apiContents = EmbeddedResources.GetStream(apiFile);

using var stream = assembly.GetManifestResourceStream(resourceName);
using var reader = new StreamReader(stream ?? throw new InvalidOperationException($"Could not find the embedded resource {name}"));
await File.WriteAllTextAsync(componentsFilename, componentContents);
await File.WriteAllTextAsync(openapiFilename, apiContents);

return reader.ReadToEnd();
return await OpenApiMultiFileReader.Read(openapiFilename);
}

public static string OpenApi => GetStream("openapi.yaml");

public static string Components => GetStream("components.yaml");
}
}
16 changes: 15 additions & 1 deletion src/OasReader/OpenApiDocumentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ public static OpenApiDocument MergeExternalReferences(this OpenApiDocument docum
}

public static bool ContainsExternalReferences(this OpenApiDocument document) =>
document.Paths.Any(kvp => kvp.Value.Parameters.Any(p => p.Reference?.IsExternal == true));
document.Paths
.Any(kvp =>
kvp.Value.Parameters.Any(
p => p.Reference?.IsExternal == true ||
p.Schema.Reference?.IsExternal == true ||
p.Content.Any(c => c.Value.Schema?.Reference?.IsExternal == true)) ||

Check warning on line 51 in src/OasReader/OpenApiDocumentExtensions.cs

View check run for this annotation

Codecov / codecov/patch

src/OasReader/OpenApiDocumentExtensions.cs#L51

Added line #L51 was not covered by tests
kvp.Value.Operations.Any(
o => o.Value.Parameters.Any(p =>
p.Reference?.IsExternal == true ||
p.Schema.Reference?.IsExternal == true ||
p.Content.Any(c => c.Value.Schema?.Reference?.IsExternal == true)) ||

Check warning on line 56 in src/OasReader/OpenApiDocumentExtensions.cs

View check run for this annotation

Codecov / codecov/patch

src/OasReader/OpenApiDocumentExtensions.cs#L54-L56

Added lines #L54 - L56 were not covered by tests
o.Value.RequestBody?.Content?.Any(c => c.Value.Schema?.Reference?.IsExternal == true) == true ||
o.Value.Responses.Any(r =>
r.Value.Content.Any(c => c.Value.Schema?.Reference?.IsExternal == true) ||
r.Value.Headers.Any(h => h.Value.Schema?.Reference?.IsExternal == true))));

Check warning on line 60 in src/OasReader/OpenApiDocumentExtensions.cs

View check run for this annotation

Codecov / codecov/patch

src/OasReader/OpenApiDocumentExtensions.cs#L59-L60

Added lines #L59 - L60 were not covered by tests
}
}

0 comments on commit 403f148

Please sign in to comment.