Skip to content

Commit c4a1b19

Browse files
committed
Added openapi client sample tests for casing convention
1 parent 891cc15 commit c4a1b19

File tree

6 files changed

+136
-6
lines changed

6 files changed

+136
-6
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using JetBrains.Annotations;
4+
5+
// ReSharper disable once CheckNamespace
6+
namespace OpenApiClientTests.Exceptions
7+
{
8+
// We cannot rely on a generated ApiException as soon as we are generating multiple clients, see https://github.com/RicoSuter/NSwag/issues/2839#issuecomment-776647377.
9+
[UsedImplicitly(ImplicitUseTargetFlags.Members)]
10+
internal class ApiException : Exception
11+
{
12+
public int StatusCode { get; }
13+
14+
public string Response { get; }
15+
16+
public IReadOnlyDictionary<string, IEnumerable<string>> Headers { get; }
17+
18+
public ApiException(string message, int statusCode, string response, IReadOnlyDictionary<string, IEnumerable<string>> headers, Exception innerException)
19+
: base(
20+
message + "\n\nStatus: " + statusCode + "\nResponse: \n" +
21+
(response == null ? "(null)" : response.Substring(0, response.Length >= 512 ? 512 : response.Length)), innerException)
22+
{
23+
StatusCode = statusCode;
24+
Response = response;
25+
Headers = headers;
26+
}
27+
28+
public override string ToString()
29+
{
30+
return $"HTTP Response: \n\n{Response}\n\n{base.ToString()}";
31+
}
32+
}
33+
34+
[UsedImplicitly(ImplicitUseTargetFlags.Members)]
35+
internal sealed class ApiException<TResult> : ApiException
36+
{
37+
public TResult Result { get; }
38+
39+
public ApiException(string message, int statusCode, string response, IReadOnlyDictionary<string, IEnumerable<string>> headers, TResult result,
40+
Exception innerException)
41+
: base(message, statusCode, response, headers, innerException)
42+
{
43+
Result = result;
44+
}
45+
}
46+
}

test/OpenApiClientTests/LegacyClient/ApiResponse.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Threading.Tasks;
33
using JsonApiDotNetCore.OpenApi.Client;
4-
using OpenApiClientTests.LegacyClient.GeneratedCode;
4+
using OpenApiClientTests.Exceptions;
55

66
#pragma warning disable AV1008 // Class should not be static
77

test/OpenApiClientTests/LegacyClient/ResponseTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Threading.Tasks;
77
using FluentAssertions;
88
using FluentAssertions.Specialized;
9+
using OpenApiClientTests.Exceptions;
910
using OpenApiClientTests.LegacyClient.GeneratedCode;
1011
using Xunit;
1112

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
using FluentAssertions;
5+
using OpenApiClientTests.NamingConvention.KebabCase.GeneratedCode;
6+
using OpenApiTests.NamingConvention;
7+
using OpenApiTests.NamingConvention.KebabCase;
8+
using TestBuildingBlocks;
9+
using Xunit;
10+
using GeneratedSupermarketType = OpenApiClientTests.NamingConvention.KebabCase.GeneratedCode.SupermarketType;
11+
12+
namespace OpenApiClientTests.NamingConvention.KebabCase
13+
{
14+
public sealed class KebabCaseRoundTripTests
15+
: IClassFixture<IntegrationTestContext<KebabCaseNamingConventionStartup<NamingConventionDbContext>, NamingConventionDbContext>>
16+
{
17+
private const string HostPrefix = "http://localhost/";
18+
private readonly IntegrationTestContext<KebabCaseNamingConventionStartup<NamingConventionDbContext>, NamingConventionDbContext> _testContext;
19+
private readonly NamingConventionFakers _fakers = new();
20+
21+
public KebabCaseRoundTripTests(
22+
IntegrationTestContext<KebabCaseNamingConventionStartup<NamingConventionDbContext>, NamingConventionDbContext> testContext)
23+
{
24+
_testContext = testContext;
25+
testContext.UseController<SupermarketsController>();
26+
}
27+
28+
[Fact]
29+
public async Task Can_use_get_collection_endpoint_with_kebab_case_naming_convention()
30+
{
31+
// Arrange
32+
Supermarket supermarket = _fakers.Supermarket.Generate();
33+
34+
await _testContext.RunOnDatabaseAsync(async dbContext =>
35+
{
36+
await dbContext.ClearTableAsync<Supermarket>();
37+
dbContext.Supermarkets.Add(supermarket);
38+
await dbContext.SaveChangesAsync();
39+
});
40+
41+
KebabCaseClient apiClient = new(_testContext.Factory.CreateClient());
42+
43+
// Act
44+
SupermarketCollectionResponseDocument resourceCollection = await apiClient.GetSupermarketCollectionAsync();
45+
46+
// Assert
47+
resourceCollection.Links.First.Should().Be($"{HostPrefix}supermarkets");
48+
resourceCollection.Links.Self.Should().Be($"{HostPrefix}supermarkets");
49+
resourceCollection.Data.Count.Should().Be(1);
50+
51+
SupermarketDataInResponse resourceDataInResponse = resourceCollection.Data.First();
52+
53+
resourceDataInResponse.Links.Self.Should().Be($"{HostPrefix}supermarkets/{supermarket.StringId}");
54+
55+
resourceDataInResponse.Attributes.Kind.Should().Be(Enum.Parse<GeneratedSupermarketType>(supermarket.Kind.ToString()));
56+
resourceDataInResponse.Attributes.NameOfCity.Should().Be(supermarket.NameOfCity);
57+
58+
resourceDataInResponse.Relationships.Cashiers.Links.Self.Should().Be($"{HostPrefix}supermarkets/{supermarket.StringId}/relationships/cashiers");
59+
resourceDataInResponse.Relationships.Cashiers.Links.Related.Should().Be($"{HostPrefix}supermarkets/{supermarket.StringId}/cashiers");
60+
61+
resourceDataInResponse.Relationships.StoreManager.Links.Self.Should()
62+
.Be($"{HostPrefix}supermarkets/{supermarket.StringId}/relationships/store-manager");
63+
64+
resourceDataInResponse.Relationships.StoreManager.Links.Related.Should().Be($"{HostPrefix}supermarkets/{supermarket.StringId}/store-manager");
65+
}
66+
}
67+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using Bogus;
3+
using OpenApiTests.NamingConvention;
4+
using TestBuildingBlocks;
5+
6+
namespace OpenApiClientTests.NamingConvention.KebabCase
7+
{
8+
internal sealed class NamingConventionFakers : FakerContainer
9+
{
10+
private readonly Lazy<Faker<Supermarket>> _lazySupermarketFaker = new(() => new Faker<Supermarket>().UseSeed(GetFakerSeed())
11+
.RuleFor(supermarket => supermarket.NameOfCity, faker => faker.Address.City())
12+
.RuleFor(supermarket => supermarket.Kind, faker => faker.PickRandom<SupermarketType>()));
13+
14+
public Faker<Supermarket> Supermarket => _lazySupermarketFaker.Value;
15+
}
16+
}

test/OpenApiClientTests/OpenApiClientTests.csproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
<TargetFramework>$(NetCoreAppVersion)</TargetFramework>
44
</PropertyGroup>
55

6-
<ItemGroup>
7-
<ProjectReference Include="..\..\src\JsonApiDotNetCore.OpenApi.Client\JsonApiDotNetCore.OpenApi.Client.csproj"/>
8-
<ProjectReference Include="..\OpenApiTests\OpenApiTests.csproj"/>
9-
<ProjectReference Include="..\TestBuildingBlocks\TestBuildingBlocks.csproj"/>
10-
</ItemGroup>
6+
<ItemGroup>
7+
<ProjectReference Include="..\..\src\JsonApiDotNetCore.OpenApi.Client\JsonApiDotNetCore.OpenApi.Client.csproj" />
8+
<ProjectReference Include="..\OpenApiTests\OpenApiTests.csproj" />
9+
<ProjectReference Include="..\TestBuildingBlocks\TestBuildingBlocks.csproj" />
10+
</ItemGroup>
1111

1212
<ItemGroup>
1313
<PackageReference Include="coverlet.collector" Version="$(CoverletVersion)" PrivateAssets="All"/>

0 commit comments

Comments
 (0)