Skip to content

Commit cc883a6

Browse files
committed
add full document test, remove swaggerui dependency
1 parent 9798f4f commit cc883a6

10 files changed

+1190
-1
lines changed

src/OpenApi/OpenApi.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.1" />
99
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.2.1" />
1010
<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" Version="6.2.1" />
11-
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.2.1" />
1211
</ItemGroup>
1312

1413
<ItemGroup>

test/OpenApiTests/Airplane.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using JetBrains.Annotations;
4+
using JsonApiDotNetCore.Resources;
5+
using JsonApiDotNetCore.Resources.Annotations;
6+
7+
namespace OpenApiTests
8+
{
9+
[UsedImplicitly(ImplicitUseTargetFlags.Members)]
10+
public sealed class Airplane : Identifiable<int>
11+
{
12+
[Attr]
13+
public int SeatingCapacity { get; set; }
14+
15+
[Attr]
16+
public DateTimeOffset ManufacturedAt { get; set; }
17+
18+
[HasMany]
19+
public ISet<Flight> Flights { get; set; }
20+
}
21+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using JsonApiDotNetCore.Configuration;
2+
using JsonApiDotNetCore.Controllers;
3+
using JsonApiDotNetCore.Services;
4+
using Microsoft.Extensions.Logging;
5+
6+
namespace OpenApiTests
7+
{
8+
public sealed class AirplanesController : JsonApiController<Airplane, int>
9+
{
10+
public AirplanesController(IJsonApiOptions options, ILoggerFactory loggerFactory, IResourceService<Airplane, int> resourceService)
11+
: base(options, loggerFactory, resourceService)
12+
{
13+
}
14+
}
15+
}

test/OpenApiTests/Flight.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using JetBrains.Annotations;
3+
using JsonApiDotNetCore.Resources;
4+
using JsonApiDotNetCore.Resources.Annotations;
5+
6+
namespace OpenApiTests
7+
{
8+
[UsedImplicitly(ImplicitUseTargetFlags.Members)]
9+
public sealed class Flight : Identifiable<int>
10+
{
11+
[Attr]
12+
public string Destination { get; set; }
13+
14+
[Attr]
15+
public DateTimeOffset PlannedDeparture { get; set; }
16+
}
17+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using JsonApiDotNetCore.Configuration;
2+
using JsonApiDotNetCore.Controllers;
3+
using JsonApiDotNetCore.Services;
4+
using Microsoft.Extensions.Logging;
5+
6+
namespace OpenApiTests
7+
{
8+
public sealed class FlightsController : JsonApiController<Flight, int>
9+
{
10+
public FlightsController(IJsonApiOptions options, ILoggerFactory loggerFactory, IResourceService<Flight, int> resourceService)
11+
: base(options, loggerFactory, resourceService)
12+
{
13+
}
14+
}
15+
}

test/OpenApiTests/OpenApiDbContext.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using JetBrains.Annotations;
2+
using Microsoft.EntityFrameworkCore;
3+
4+
// @formatter:wrap_chained_method_calls chop_always
5+
// @formatter:keep_existing_linebreaks true
6+
7+
namespace OpenApiTests
8+
{
9+
[UsedImplicitly(ImplicitUseTargetFlags.Members)]
10+
public sealed class OpenApiDbContext : DbContext
11+
{
12+
public DbSet<Airplane> Airplanes { get; set; }
13+
public DbSet<Flight> Flights { get; set; }
14+
15+
public OpenApiDbContext(DbContextOptions<OpenApiDbContext> options)
16+
: base(options)
17+
{
18+
}
19+
20+
protected override void OnModelCreating(ModelBuilder builder)
21+
{
22+
builder.Entity<Airplane>()
23+
.HasMany(airplane => airplane.Flights);
24+
}
25+
}
26+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System;
2+
using System.IO;
3+
using System.Net.Http;
4+
using System.Reflection;
5+
using System.Threading.Tasks;
6+
using FluentAssertions;
7+
using TestBuildingBlocks;
8+
using Xunit;
9+
10+
namespace OpenApiTests
11+
{
12+
public sealed class OpenApiDocumentTests
13+
{
14+
private readonly IntegrationTestContext<OpenApiStartup<OpenApiDbContext>, OpenApiDbContext> _testContext;
15+
16+
public OpenApiDocumentTests()
17+
{
18+
_testContext = new IntegrationTestContext<OpenApiStartup<OpenApiDbContext>, OpenApiDbContext>();
19+
_testContext.UseController<AirplanesController>();
20+
_testContext.UseController<FlightsController>();
21+
}
22+
23+
[Fact]
24+
public async Task Retrieved_document_should_match_expected_document()
25+
{
26+
// Arrange
27+
string embeddedResourceName = $"{nameof(OpenApiTests)}.openapi.json";
28+
string expectedDocument = await LoadEmbeddedResourceAsync(embeddedResourceName);
29+
string requestUrl = $"swagger/{nameof(OpenApiTests)}/swagger.json";
30+
31+
// Act
32+
string actualDocument = await GetAsync(requestUrl);
33+
34+
// Assert
35+
actualDocument.Should().BeJson(expectedDocument);
36+
}
37+
38+
private async Task<string> GetAsync(string requestUrl)
39+
{
40+
var request = new HttpRequestMessage(HttpMethod.Get, requestUrl);
41+
42+
using HttpClient client = _testContext.Factory.CreateClient();
43+
HttpResponseMessage responseMessage = await client.SendAsync(request);
44+
45+
return await responseMessage.Content.ReadAsStringAsync();
46+
}
47+
48+
private static async Task<string> LoadEmbeddedResourceAsync(string name)
49+
{
50+
var assembly = Assembly.GetExecutingAssembly();
51+
await using Stream stream = assembly.GetManifestResourceStream(name);
52+
53+
if (stream == null)
54+
{
55+
throw new Exception($"Failed to load embedded resource '{name}'. Set Build Action to Embedded Resource in properties.");
56+
}
57+
58+
using var reader = new StreamReader(stream);
59+
return await reader.ReadToEndAsync();
60+
}
61+
}
62+
}

test/OpenApiTests/OpenApiStartup.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using JsonApiDotNetCore.Configuration;
2+
using Microsoft.AspNetCore.Builder;
3+
using Microsoft.AspNetCore.Hosting;
4+
using Microsoft.EntityFrameworkCore;
5+
using Microsoft.Extensions.DependencyInjection;
6+
using Microsoft.Extensions.Logging;
7+
using Microsoft.OpenApi.Models;
8+
using OpenApi;
9+
using TestBuildingBlocks;
10+
11+
namespace OpenApiTests
12+
{
13+
public sealed class OpenApiStartup<TDbContext> : TestableStartup<TDbContext>
14+
where TDbContext : DbContext
15+
{
16+
public override void ConfigureServices(IServiceCollection services)
17+
{
18+
IMvcCoreBuilder mvcCoreBuilder = services.AddMvcCore();
19+
20+
services.AddJsonApi<TDbContext>(SetJsonApiOptions, mvcBuilder: mvcCoreBuilder);
21+
22+
services.AddOpenApi(mvcCoreBuilder, options => options.SwaggerDoc(nameof(OpenApiTests), new OpenApiInfo
23+
{
24+
Title = nameof(OpenApiTests),
25+
Version = "1"
26+
}));
27+
}
28+
29+
public override void Configure(IApplicationBuilder app, IWebHostEnvironment environment, ILoggerFactory loggerFactory)
30+
{
31+
app.UseRouting();
32+
33+
app.UseJsonApi();
34+
35+
app.UseSwagger();
36+
37+
app.UseEndpoints(endpoints => endpoints.MapControllers());
38+
}
39+
}
40+
}

test/OpenApiTests/OpenApiTests.csproj

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>$(NetCoreAppVersion)</TargetFramework>
4+
</PropertyGroup>
5+
6+
<ItemGroup>
7+
<None Update="xunit.runner.json">
8+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
9+
</None>
10+
</ItemGroup>
11+
12+
<ItemGroup>
13+
<ProjectReference Include="..\..\src\Examples\JsonApiDotNetCoreExample\JsonApiDotNetCoreExample.csproj" />
14+
<ProjectReference Include="..\..\src\OpenApi\OpenApi.csproj" />
15+
<ProjectReference Include="..\TestBuildingBlocks\TestBuildingBlocks.csproj" />
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<PackageReference Include="coverlet.collector" Version="$(CoverletVersion)" PrivateAssets="All" />
20+
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="$(AspNetCoreVersion)" />
21+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(TestSdkVersion)" />
22+
</ItemGroup>
23+
24+
<ItemGroup>
25+
<None Remove="openapi.json" />
26+
</ItemGroup>
27+
28+
<ItemGroup>
29+
<EmbeddedResource Include="openapi.json" />
30+
</ItemGroup>
31+
</Project>

0 commit comments

Comments
 (0)