17
17
using System . Collections . Generic ;
18
18
using System . Linq ;
19
19
using Microsoft . EntityFrameworkCore ;
20
+ using JsonApiDotNetCoreExampleTests . Startups ;
21
+ using System ;
20
22
21
23
namespace JsonApiDotNetCoreExampleTests . Acceptance . Spec
22
24
{
@@ -37,7 +39,7 @@ public CreatingDataTests(DocsFixture<Startup, JsonDocWriter> fixture)
37
39
}
38
40
39
41
[ Fact ]
40
- public async Task Can_Create_Guid_Identifiable_Entities ( )
42
+ public async Task Can_Create_Guid_Identifiable_Entity ( )
41
43
{
42
44
// arrange
43
45
var builder = new WebHostBuilder ( )
@@ -74,7 +76,7 @@ public async Task Can_Create_Guid_Identifiable_Entities()
74
76
} ;
75
77
request . Content = new StringContent ( JsonConvert . SerializeObject ( content ) ) ;
76
78
request . Content . Headers . ContentType = new MediaTypeHeaderValue ( "application/vnd.api+json" ) ;
77
-
79
+
78
80
// act
79
81
var response = await client . SendAsync ( request ) ;
80
82
@@ -83,9 +85,10 @@ public async Task Can_Create_Guid_Identifiable_Entities()
83
85
}
84
86
85
87
[ Fact ]
86
- public async Task Request_With_ClientGeneratedId_Returns_403 ( )
88
+ public async Task Cannot_Create_Entity_With_Client_Generate_Id ( )
87
89
{
88
90
// arrange
91
+ var context = _fixture . GetService < AppDbContext > ( ) ;
89
92
var builder = new WebHostBuilder ( )
90
93
. UseStartup < Startup > ( ) ;
91
94
var httpMethod = new HttpMethod ( "POST" ) ;
@@ -94,29 +97,124 @@ public async Task Request_With_ClientGeneratedId_Returns_403()
94
97
var client = server . CreateClient ( ) ;
95
98
var request = new HttpRequestMessage ( httpMethod , route ) ;
96
99
var todoItem = _todoItemFaker . Generate ( ) ;
100
+ const int clientDefinedId = 9999 ;
97
101
var content = new
98
102
{
99
103
data = new
100
104
{
101
105
type = "todo-items" ,
102
- id = "9999 ",
106
+ id = $ " { clientDefinedId } ",
103
107
attributes = new
104
108
{
105
109
description = todoItem . Description ,
106
110
ordinal = todoItem . Ordinal
107
111
}
108
112
}
109
113
} ;
114
+
110
115
request . Content = new StringContent ( JsonConvert . SerializeObject ( content ) ) ;
111
116
request . Content . Headers . ContentType = new MediaTypeHeaderValue ( "application/vnd.api+json" ) ;
112
-
117
+
113
118
// act
114
119
var response = await client . SendAsync ( request ) ;
115
120
116
121
// assert
117
122
Assert . Equal ( HttpStatusCode . Forbidden , response . StatusCode ) ;
118
123
}
119
124
125
+ [ Fact ]
126
+ public async Task Can_Create_Entity_With_Client_Defined_Id_If_Configured ( )
127
+ {
128
+ // arrange
129
+ var context = _fixture . GetService < AppDbContext > ( ) ;
130
+ var builder = new WebHostBuilder ( )
131
+ . UseStartup < ClientGeneratedIdsStartup > ( ) ;
132
+ var httpMethod = new HttpMethod ( "POST" ) ;
133
+ var route = "/api/v1/todo-items" ;
134
+ var server = new TestServer ( builder ) ;
135
+ var client = server . CreateClient ( ) ;
136
+ var request = new HttpRequestMessage ( httpMethod , route ) ;
137
+ var todoItem = _todoItemFaker . Generate ( ) ;
138
+ const int clientDefinedId = 9999 ;
139
+ var content = new
140
+ {
141
+ data = new
142
+ {
143
+ type = "todo-items" ,
144
+ id = $ "{ clientDefinedId } ",
145
+ attributes = new
146
+ {
147
+ description = todoItem . Description ,
148
+ ordinal = todoItem . Ordinal
149
+ }
150
+ }
151
+ } ;
152
+
153
+ request . Content = new StringContent ( JsonConvert . SerializeObject ( content ) ) ;
154
+ request . Content . Headers . ContentType = new MediaTypeHeaderValue ( "application/vnd.api+json" ) ;
155
+
156
+ // act
157
+ var response = await client . SendAsync ( request ) ;
158
+ var body = await response . Content . ReadAsStringAsync ( ) ;
159
+ var deserializedBody = ( TodoItem ) JsonApiDeSerializer . Deserialize ( body , _jsonApiContext , context ) ;
160
+
161
+ // assert
162
+ Assert . Equal ( HttpStatusCode . Created , response . StatusCode ) ;
163
+ Assert . Equal ( clientDefinedId , deserializedBody . Id ) ;
164
+ }
165
+
166
+
167
+ [ Fact ]
168
+ public async Task Can_Create_Guid_Identifiable_Entity_With_Client_Defined_Id_If_Configured ( )
169
+ {
170
+ // arrange
171
+ var builder = new WebHostBuilder ( )
172
+ . UseStartup < ClientGeneratedIdsStartup > ( ) ;
173
+ var httpMethod = new HttpMethod ( "POST" ) ;
174
+ var server = new TestServer ( builder ) ;
175
+ var client = server . CreateClient ( ) ;
176
+
177
+ var context = _fixture . GetService < AppDbContext > ( ) ;
178
+
179
+ var owner = new JsonApiDotNetCoreExample . Models . Person ( ) ;
180
+ context . People . Add ( owner ) ;
181
+ await context . SaveChangesAsync ( ) ;
182
+
183
+ var route = "/api/v1/todo-item-collections" ;
184
+ var request = new HttpRequestMessage ( httpMethod , route ) ;
185
+ var clientDefinedId = Guid . NewGuid ( ) ;
186
+ var content = new
187
+ {
188
+ data = new
189
+ {
190
+ type = "todo-item-collections" ,
191
+ id = $ "{ clientDefinedId } ",
192
+ relationships = new
193
+ {
194
+ owner = new
195
+ {
196
+ data = new
197
+ {
198
+ type = "people" ,
199
+ id = owner . Id . ToString ( )
200
+ }
201
+ }
202
+ }
203
+ }
204
+ } ;
205
+ request . Content = new StringContent ( JsonConvert . SerializeObject ( content ) ) ;
206
+ request . Content . Headers . ContentType = new MediaTypeHeaderValue ( "application/vnd.api+json" ) ;
207
+
208
+ // act
209
+ var response = await client . SendAsync ( request ) ;
210
+ var body = await response . Content . ReadAsStringAsync ( ) ;
211
+ var deserializedBody = ( TodoItemCollection ) JsonApiDeSerializer . Deserialize ( body , _jsonApiContext , context ) ;
212
+
213
+ // assert
214
+ Assert . Equal ( HttpStatusCode . Created , response . StatusCode ) ;
215
+ Assert . Equal ( clientDefinedId , deserializedBody . Id ) ;
216
+ }
217
+
120
218
[ Fact ]
121
219
public async Task Can_Create_And_Set_HasMany_Relationships ( )
122
220
{
@@ -167,14 +265,14 @@ public async Task Can_Create_And_Set_HasMany_Relationships()
167
265
168
266
request . Content = new StringContent ( JsonConvert . SerializeObject ( content ) ) ;
169
267
request . Content . Headers . ContentType = new MediaTypeHeaderValue ( "application/vnd.api+json" ) ;
170
-
268
+
171
269
// act
172
270
var response = await client . SendAsync ( request ) ;
173
271
var body = await response . Content . ReadAsStringAsync ( ) ;
174
272
var deserializedBody = ( TodoItemCollection ) JsonApiDeSerializer . Deserialize ( body , _jsonApiContext , context ) ;
175
273
var newId = deserializedBody . Id ;
176
274
var contextCollection = context . TodoItemCollections
177
- . Include ( c=> c . Owner )
275
+ . Include ( c => c . Owner )
178
276
. Include ( c => c . TodoItems )
179
277
. SingleOrDefault ( c => c . Id == newId ) ;
180
278
@@ -210,7 +308,7 @@ public async Task ShouldReceiveLocationHeader_InResponse()
210
308
} ;
211
309
request . Content = new StringContent ( JsonConvert . SerializeObject ( content ) ) ;
212
310
request . Content . Headers . ContentType = new MediaTypeHeaderValue ( "application/vnd.api+json" ) ;
213
-
311
+
214
312
// act
215
313
var response = await client . SendAsync ( request ) ;
216
314
var body = await response . Content . ReadAsStringAsync ( ) ;
@@ -247,7 +345,7 @@ public async Task Respond_409_ToIncorrectEntityType()
247
345
} ;
248
346
request . Content = new StringContent ( JsonConvert . SerializeObject ( content ) ) ;
249
347
request . Content . Headers . ContentType = new MediaTypeHeaderValue ( "application/vnd.api+json" ) ;
250
-
348
+
251
349
// act
252
350
var response = await client . SendAsync ( request ) ;
253
351
0 commit comments