Skip to content

Commit 6c8f66e

Browse files
committed
Add missing test to client generation modes
1 parent 895b8fc commit 6c8f66e

File tree

2 files changed

+97
-6
lines changed

2 files changed

+97
-6
lines changed

test/OpenApiKiotaEndToEndTests/ClientIdGenerationModes/ClientIdGenerationModesTests.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,49 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
183183
});
184184
}
185185

186+
[Fact]
187+
public async Task Cannot_create_resource_with_existing_ID_when_supplying_ID_is_allowed()
188+
{
189+
// Arrange
190+
Game existingGame = _fakers.Game.Generate();
191+
192+
await _testContext.RunOnDatabaseAsync(async dbContext =>
193+
{
194+
dbContext.Games.Add(existingGame);
195+
await dbContext.SaveChangesAsync();
196+
});
197+
198+
using HttpClientRequestAdapter requestAdapter = _requestAdapterFactory.CreateAdapter(_testContext.Factory);
199+
ClientIdGenerationModesClient apiClient = new(requestAdapter);
200+
201+
var requestBody = new GamePostRequestDocument
202+
{
203+
Data = new GameDataInPostRequest
204+
{
205+
Type = GameResourceType.Games,
206+
Id = existingGame.StringId,
207+
Attributes = new GameAttributesInPostRequest
208+
{
209+
Title = existingGame.Title,
210+
PurchasePrice = (double)existingGame.PurchasePrice
211+
}
212+
}
213+
};
214+
215+
// Act
216+
Func<Task> action = async () => _ = await apiClient.Games.PostAsync(requestBody);
217+
218+
// Assert
219+
ErrorResponseDocument exception = (await action.Should().ThrowExactlyAsync<ErrorResponseDocument>()).Which;
220+
exception.ResponseStatusCode.Should().Be((int)HttpStatusCode.Conflict);
221+
exception.Errors.ShouldHaveCount(1);
222+
223+
ErrorObject error = exception.Errors.ElementAt(0);
224+
error.Status.Should().Be("409");
225+
error.Title.Should().Be("Another resource with the specified ID already exists.");
226+
error.Detail.Should().Be($"Another resource of type 'games' with ID '{existingGame.StringId}' already exists.");
227+
}
228+
186229
[Fact]
187230
public async Task Can_create_resource_without_ID_when_supplying_ID_is_forbidden()
188231
{

test/OpenApiNSwagEndToEndTests/ClientIdGenerationModes/ClientIdGenerationModesTests.cs

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Net;
12
using FluentAssertions;
23
using FluentAssertions.Specialized;
34
using JsonApiDotNetCore.OpenApi.Client.NSwag;
@@ -7,18 +8,22 @@
78
using OpenApiTests.ClientIdGenerationModes;
89
using TestBuildingBlocks;
910
using Xunit;
11+
using Xunit.Abstractions;
1012

1113
namespace OpenApiNSwagEndToEndTests.ClientIdGenerationModes;
1214

1315
public sealed class ClientIdGenerationModesTests
1416
: IClassFixture<IntegrationTestContext<OpenApiStartup<ClientIdGenerationDbContext>, ClientIdGenerationDbContext>>
1517
{
1618
private readonly IntegrationTestContext<OpenApiStartup<ClientIdGenerationDbContext>, ClientIdGenerationDbContext> _testContext;
19+
private readonly XUnitLogHttpMessageHandler _logHttpMessageHandler;
1720
private readonly ClientIdGenerationFakers _fakers = new();
1821

19-
public ClientIdGenerationModesTests(IntegrationTestContext<OpenApiStartup<ClientIdGenerationDbContext>, ClientIdGenerationDbContext> testContext)
22+
public ClientIdGenerationModesTests(IntegrationTestContext<OpenApiStartup<ClientIdGenerationDbContext>, ClientIdGenerationDbContext> testContext,
23+
ITestOutputHelper testOutputHelper)
2024
{
2125
_testContext = testContext;
26+
_logHttpMessageHandler = new XUnitLogHttpMessageHandler(testOutputHelper);
2227

2328
testContext.UseController<PlayersController>();
2429
testContext.UseController<GamesController>();
@@ -31,7 +36,7 @@ public async Task Cannot_create_resource_without_ID_when_supplying_ID_is_require
3136
// Arrange
3237
Player newPlayer = _fakers.Player.Generate();
3338

34-
using HttpClient httpClient = _testContext.Factory.CreateClient();
39+
using HttpClient httpClient = _testContext.Factory.CreateDefaultClient(_logHttpMessageHandler);
3540
ClientIdGenerationModesClient apiClient = new(httpClient);
3641

3742
var requestBody = new PlayerPostRequestDocument
@@ -60,7 +65,7 @@ public async Task Can_create_resource_with_ID_when_supplying_ID_is_required()
6065
Player newPlayer = _fakers.Player.Generate();
6166
newPlayer.Id = Guid.NewGuid();
6267

63-
using HttpClient httpClient = _testContext.Factory.CreateClient();
68+
using HttpClient httpClient = _testContext.Factory.CreateDefaultClient(_logHttpMessageHandler);
6469
ClientIdGenerationModesClient apiClient = new(httpClient);
6570

6671
var requestBody = new PlayerPostRequestDocument
@@ -95,7 +100,7 @@ public async Task Can_create_resource_without_ID_when_supplying_ID_is_allowed()
95100
// Arrange
96101
Game newGame = _fakers.Game.Generate();
97102

98-
using HttpClient httpClient = _testContext.Factory.CreateClient();
103+
using HttpClient httpClient = _testContext.Factory.CreateDefaultClient(_logHttpMessageHandler);
99104
ClientIdGenerationModesClient apiClient = new(httpClient);
100105

101106
var requestBody = new GamePostRequestDocument
@@ -135,7 +140,7 @@ public async Task Can_create_resource_with_ID_when_supplying_ID_is_allowed()
135140
Game newGame = _fakers.Game.Generate();
136141
newGame.Id = Guid.NewGuid();
137142

138-
using HttpClient httpClient = _testContext.Factory.CreateClient();
143+
using HttpClient httpClient = _testContext.Factory.CreateDefaultClient(_logHttpMessageHandler);
139144
ClientIdGenerationModesClient apiClient = new(httpClient);
140145

141146
var requestBody = new GamePostRequestDocument
@@ -166,13 +171,56 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
166171
});
167172
}
168173

174+
[Fact]
175+
public async Task Cannot_create_resource_with_existing_ID_when_supplying_ID_is_allowed()
176+
{
177+
// Arrange
178+
Game existingGame = _fakers.Game.Generate();
179+
180+
await _testContext.RunOnDatabaseAsync(async dbContext =>
181+
{
182+
dbContext.Games.Add(existingGame);
183+
await dbContext.SaveChangesAsync();
184+
});
185+
186+
using HttpClient httpClient = _testContext.Factory.CreateDefaultClient(_logHttpMessageHandler);
187+
ClientIdGenerationModesClient apiClient = new(httpClient);
188+
189+
var requestBody = new GamePostRequestDocument
190+
{
191+
Data = new GameDataInPostRequest
192+
{
193+
Id = existingGame.StringId!,
194+
Attributes = new GameAttributesInPostRequest
195+
{
196+
Title = existingGame.Title,
197+
PurchasePrice = (double)existingGame.PurchasePrice
198+
}
199+
}
200+
};
201+
202+
// Act
203+
Func<Task> action = async () => _ = await apiClient.PostGameAsync(null, requestBody);
204+
205+
// Assert
206+
ApiException<ErrorResponseDocument> exception = (await action.Should().ThrowExactlyAsync<ApiException<ErrorResponseDocument>>()).Which;
207+
exception.StatusCode.Should().Be((int)HttpStatusCode.Conflict);
208+
exception.Message.Should().Be("HTTP 409: The request body contains conflicting information or another resource with the same ID already exists.");
209+
exception.Result.Errors.ShouldHaveCount(1);
210+
211+
ErrorObject error = exception.Result.Errors.ElementAt(0);
212+
error.Status.Should().Be("409");
213+
error.Title.Should().Be("Another resource with the specified ID already exists.");
214+
error.Detail.Should().Be($"Another resource of type 'games' with ID '{existingGame.StringId}' already exists.");
215+
}
216+
169217
[Fact]
170218
public async Task Can_create_resource_without_ID_when_supplying_ID_is_forbidden()
171219
{
172220
// Arrange
173221
PlayerGroup newPlayerGroup = _fakers.Group.Generate();
174222

175-
using HttpClient httpClient = _testContext.Factory.CreateClient();
223+
using HttpClient httpClient = _testContext.Factory.CreateDefaultClient(_logHttpMessageHandler);
176224
ClientIdGenerationModesClient apiClient = new(httpClient);
177225

178226
var requestBody = new PlayerGroupPostRequestDocument

0 commit comments

Comments
 (0)