6
6
using System . Net . Http . Headers ;
7
7
using System . Threading . Tasks ;
8
8
using Bogus ;
9
+ using JsonApiDotNetCore . Builders ;
10
+ using JsonApiDotNetCore . Serialization . Client ;
9
11
using JsonApiDotNetCoreExample ;
10
12
using JsonApiDotNetCoreExample . Data ;
11
13
using JsonApiDotNetCoreExample . Models ;
14
+ using JsonApiDotNetCoreExampleTests . Helpers . Models ;
12
15
using JsonApiDotNetCoreExampleTests . Startups ;
13
16
using Microsoft . AspNetCore . Hosting ;
14
17
using Microsoft . AspNetCore . TestHost ;
@@ -130,6 +133,8 @@ public async Task Can_Create_Entity_With_Client_Defined_Id_If_Configured()
130
133
{
131
134
// arrange
132
135
var context = _fixture . GetService < AppDbContext > ( ) ;
136
+ context . RemoveRange ( context . TodoItems ) ;
137
+ await context . SaveChangesAsync ( ) ;
133
138
var builder = new WebHostBuilder ( )
134
139
. UseStartup < ClientGeneratedIdsStartup > ( ) ;
135
140
var httpMethod = new HttpMethod ( "POST" ) ;
@@ -139,28 +144,20 @@ public async Task Can_Create_Entity_With_Client_Defined_Id_If_Configured()
139
144
var request = new HttpRequestMessage ( httpMethod , route ) ;
140
145
var todoItem = _todoItemFaker . Generate ( ) ;
141
146
const int clientDefinedId = 9999 ;
142
- var content = new
143
- {
144
- data = new
145
- {
146
- type = "todo-items" ,
147
- id = $ "{ clientDefinedId } ",
148
- attributes = new
149
- {
150
- description = todoItem . Description ,
151
- ordinal = todoItem . Ordinal ,
152
- createdDate = DateTime . Now
153
- }
154
- }
155
- } ;
147
+ var serializer = _fixture . GetSerializer < TodoItem > ( ti => new { ti . CreatedDate , ti . Description , ti . Ordinal } ) ;
148
+ todoItem . Id = clientDefinedId ;
149
+ var content = serializer . Serialize ( todoItem ) ;
156
150
157
- request . Content = new StringContent ( JsonConvert . SerializeObject ( content ) ) ;
151
+ request . Content = new StringContent ( content ) ;
158
152
request . Content . Headers . ContentType = new MediaTypeHeaderValue ( "application/vnd.api+json" ) ;
159
153
154
+ var graph = new ResourceGraphBuilder ( ) . AddResource < TodoItemClient > ( "todo-items" ) . Build ( ) ;
155
+ var deserializer = new ResponseDeserializer ( graph ) ;
156
+
160
157
// act
161
158
var response = await client . SendAsync ( request ) ;
162
159
var body = await response . Content . ReadAsStringAsync ( ) ;
163
- var deserializedBody = _fixture . GetDeserializer ( ) . DeserializeSingle < TodoItem > ( body ) . Data ;
160
+ var deserializedBody = deserializer . DeserializeSingle < TodoItemClient > ( body ) . Data ;
164
161
165
162
// assert
166
163
Assert . Equal ( HttpStatusCode . Created , response . StatusCode ) ;
0 commit comments