1
+ using System . Net ;
1
2
using FluentAssertions ;
2
3
using FluentAssertions . Specialized ;
3
4
using JsonApiDotNetCore . OpenApi . Client . NSwag ;
7
8
using OpenApiTests . ClientIdGenerationModes ;
8
9
using TestBuildingBlocks ;
9
10
using Xunit ;
11
+ using Xunit . Abstractions ;
10
12
11
13
namespace OpenApiNSwagEndToEndTests . ClientIdGenerationModes ;
12
14
13
15
public sealed class ClientIdGenerationModesTests
14
16
: IClassFixture < IntegrationTestContext < OpenApiStartup < ClientIdGenerationDbContext > , ClientIdGenerationDbContext > >
15
17
{
16
18
private readonly IntegrationTestContext < OpenApiStartup < ClientIdGenerationDbContext > , ClientIdGenerationDbContext > _testContext ;
19
+ private readonly XUnitLogHttpMessageHandler _logHttpMessageHandler ;
17
20
private readonly ClientIdGenerationFakers _fakers = new ( ) ;
18
21
19
- public ClientIdGenerationModesTests ( IntegrationTestContext < OpenApiStartup < ClientIdGenerationDbContext > , ClientIdGenerationDbContext > testContext )
22
+ public ClientIdGenerationModesTests ( IntegrationTestContext < OpenApiStartup < ClientIdGenerationDbContext > , ClientIdGenerationDbContext > testContext ,
23
+ ITestOutputHelper testOutputHelper )
20
24
{
21
25
_testContext = testContext ;
26
+ _logHttpMessageHandler = new XUnitLogHttpMessageHandler ( testOutputHelper ) ;
22
27
23
28
testContext . UseController < PlayersController > ( ) ;
24
29
testContext . UseController < GamesController > ( ) ;
@@ -31,7 +36,7 @@ public async Task Cannot_create_resource_without_ID_when_supplying_ID_is_require
31
36
// Arrange
32
37
Player newPlayer = _fakers . Player . Generate ( ) ;
33
38
34
- using HttpClient httpClient = _testContext . Factory . CreateClient ( ) ;
39
+ using HttpClient httpClient = _testContext . Factory . CreateDefaultClient ( _logHttpMessageHandler ) ;
35
40
ClientIdGenerationModesClient apiClient = new ( httpClient ) ;
36
41
37
42
var requestBody = new PlayerPostRequestDocument
@@ -60,7 +65,7 @@ public async Task Can_create_resource_with_ID_when_supplying_ID_is_required()
60
65
Player newPlayer = _fakers . Player . Generate ( ) ;
61
66
newPlayer . Id = Guid . NewGuid ( ) ;
62
67
63
- using HttpClient httpClient = _testContext . Factory . CreateClient ( ) ;
68
+ using HttpClient httpClient = _testContext . Factory . CreateDefaultClient ( _logHttpMessageHandler ) ;
64
69
ClientIdGenerationModesClient apiClient = new ( httpClient ) ;
65
70
66
71
var requestBody = new PlayerPostRequestDocument
@@ -95,7 +100,7 @@ public async Task Can_create_resource_without_ID_when_supplying_ID_is_allowed()
95
100
// Arrange
96
101
Game newGame = _fakers . Game . Generate ( ) ;
97
102
98
- using HttpClient httpClient = _testContext . Factory . CreateClient ( ) ;
103
+ using HttpClient httpClient = _testContext . Factory . CreateDefaultClient ( _logHttpMessageHandler ) ;
99
104
ClientIdGenerationModesClient apiClient = new ( httpClient ) ;
100
105
101
106
var requestBody = new GamePostRequestDocument
@@ -135,7 +140,7 @@ public async Task Can_create_resource_with_ID_when_supplying_ID_is_allowed()
135
140
Game newGame = _fakers . Game . Generate ( ) ;
136
141
newGame . Id = Guid . NewGuid ( ) ;
137
142
138
- using HttpClient httpClient = _testContext . Factory . CreateClient ( ) ;
143
+ using HttpClient httpClient = _testContext . Factory . CreateDefaultClient ( _logHttpMessageHandler ) ;
139
144
ClientIdGenerationModesClient apiClient = new ( httpClient ) ;
140
145
141
146
var requestBody = new GamePostRequestDocument
@@ -166,13 +171,56 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
166
171
} ) ;
167
172
}
168
173
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
+
169
217
[ Fact ]
170
218
public async Task Can_create_resource_without_ID_when_supplying_ID_is_forbidden ( )
171
219
{
172
220
// Arrange
173
221
PlayerGroup newPlayerGroup = _fakers . Group . Generate ( ) ;
174
222
175
- using HttpClient httpClient = _testContext . Factory . CreateClient ( ) ;
223
+ using HttpClient httpClient = _testContext . Factory . CreateDefaultClient ( _logHttpMessageHandler ) ;
176
224
ClientIdGenerationModesClient apiClient = new ( httpClient ) ;
177
225
178
226
var requestBody = new PlayerGroupPostRequestDocument
0 commit comments