Skip to content

Commit cf699c9

Browse files
committed
add tests, run ci build for openapi, use build props for openapi projects
1 parent 9798f4f commit cf699c9

12 files changed

+1195
-7
lines changed

Directory.Build.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<AspNetCoreVersion>5.0.*</AspNetCoreVersion>
55
<EFCoreVersion>5.0.*</EFCoreVersion>
66
<NpgsqlPostgreSQLVersion>5.0.*</NpgsqlPostgreSQLVersion>
7+
<SwashbuckleVersion>6.1.*</SwashbuckleVersion>
78
<CodeAnalysisRuleSet>$(MSBuildThisFileDirectory)CodingGuidelines.ruleset</CodeAnalysisRuleSet>
89
</PropertyGroup>
910

appveyor.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ environment:
1515
branches:
1616
only:
1717
- master
18+
- openapi
1819
- develop
1920
- unstable
2021
- /release\/.+/
@@ -36,6 +37,12 @@ for:
3637
services:
3738
- postgresql101
3839
# REF: https://github.com/docascode/docfx-seed/blob/master/appveyor.yml
40+
clone_script:
41+
# This ensures the base branch is loaded which is required for regitlint to run.
42+
- ps: |
43+
git clone --branch=$env:APPVEYOR_REPO_BRANCH git@github.com:$env:APPVEYOR_REPO_NAME.git $env:APPVEYOR_BUILD_FOLDER
44+
git fetch -q origin +refs/pull/$env:APPVEYOR_PULL_REQUEST_NUMBER/merge:
45+
git checkout -qf FETCH_HEAD
3946
before_build:
4047
- pwsh: |
4148
if (-Not $env:APPVEYOR_PULL_REQUEST_TITLE) {

src/OpenApi/OpenApi.csproj

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
<Project Sdk="Microsoft.NET.Sdk">
2-
32
<PropertyGroup>
4-
<TargetFramework>net5.0</TargetFramework>
3+
<TargetFramework>$(NetCoreAppVersion)</TargetFramework>
54
</PropertyGroup>
65

76
<ItemGroup>
8-
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.1" />
9-
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.2.1" />
10-
<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" Version="6.2.1" />
11-
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.2.1" />
7+
<PackageReference Include="Swashbuckle.AspNetCore" Version="$(SwashbuckleVersion)"/>
8+
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="$(SwashbuckleVersion)" />
9+
<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" Version="$(SwashbuckleVersion)" />
1210
</ItemGroup>
1311

1412
<ItemGroup>
1513
<ProjectReference Include="..\JsonApiDotNetCore\JsonApiDotNetCore.csproj" />
1614
</ItemGroup>
17-
1815
</Project>

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: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 : IntegrationTestContext<OpenApiStartup<OpenApiDbContext>, OpenApiDbContext>
13+
{
14+
public OpenApiDocumentTests()
15+
{
16+
UseController<AirplanesController>();
17+
UseController<FlightsController>();
18+
}
19+
20+
[Fact]
21+
public async Task Retrieved_document_should_match_expected_document()
22+
{
23+
// Arrange
24+
string embeddedResourceName = $"{nameof(OpenApiTests)}.openapi.json";
25+
string expectedDocument = await LoadEmbeddedResourceAsync(embeddedResourceName);
26+
string requestUrl = $"swagger/{nameof(OpenApiTests)}/swagger.json";
27+
28+
// Act
29+
string actualDocument = await GetAsync(requestUrl);
30+
31+
// Assert
32+
actualDocument.Should().BeJson(expectedDocument);
33+
}
34+
35+
private async Task<string> GetAsync(string requestUrl)
36+
{
37+
var request = new HttpRequestMessage(HttpMethod.Get, requestUrl);
38+
39+
using HttpClient client = Factory.CreateClient();
40+
HttpResponseMessage responseMessage = await client.SendAsync(request);
41+
42+
return await responseMessage.Content.ReadAsStringAsync();
43+
}
44+
45+
private static async Task<string> LoadEmbeddedResourceAsync(string name)
46+
{
47+
var assembly = Assembly.GetExecutingAssembly();
48+
await using Stream stream = assembly.GetManifestResourceStream(name);
49+
50+
if (stream == null)
51+
{
52+
throw new Exception($"Failed to load embedded resource '{name}'. Set Build Action to Embedded Resource in properties.");
53+
}
54+
55+
using var reader = new StreamReader(stream);
56+
return await reader.ReadToEndAsync();
57+
}
58+
}
59+
}

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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
<EmbeddedResource Include="openapi.json" />
26+
</ItemGroup>
27+
</Project>

0 commit comments

Comments
 (0)