22using System . Net ;
33using System . Net . Http ;
44using System . Threading . Tasks ;
5+ using Bogus ;
56using DotNetCoreDocs ;
67using DotNetCoreDocs . Writers ;
8+ using JsonApiDotNetCore . Models ;
79using JsonApiDotNetCore . Serialization ;
810using JsonApiDotNetCore . Services ;
911using JsonApiDotNetCoreExample ;
1315using Microsoft . AspNetCore . TestHost ;
1416using Newtonsoft . Json ;
1517using Xunit ;
18+ using Person = JsonApiDotNetCoreExample . Models . Person ;
1619
1720namespace JsonApiDotNetCoreExampleTests . Acceptance . Spec
1821{
@@ -21,11 +24,19 @@ public class FetchingDataTests
2124 {
2225 private DocsFixture < Startup , JsonDocWriter > _fixture ;
2326 private IJsonApiContext _jsonApiContext ;
27+ private Faker < TodoItem > _todoItemFaker ;
28+ private Faker < Person > _personFaker ;
2429
2530 public FetchingDataTests ( DocsFixture < Startup , JsonDocWriter > fixture )
2631 {
2732 _fixture = fixture ;
2833 _jsonApiContext = fixture . GetService < IJsonApiContext > ( ) ;
34+ _todoItemFaker = new Faker < TodoItem > ( )
35+ . RuleFor ( t => t . Description , f => f . Lorem . Sentence ( ) )
36+ . RuleFor ( t => t . Ordinal , f => f . Random . Number ( ) ) ;
37+ _personFaker = new Faker < Person > ( )
38+ . RuleFor ( p => p . FirstName , f => f . Name . FirstName ( ) )
39+ . RuleFor ( p => p . LastName , f => f . Name . LastName ( ) ) ;
2940 }
3041
3142 [ Fact ]
@@ -60,5 +71,38 @@ public async Task Request_ForEmptyCollection_Returns_EmptyDataCollection()
6071
6172 context . Dispose ( ) ;
6273 }
74+
75+ [ Fact ]
76+ public async Task Included_Records_Contain_Relationship_Links ( )
77+ {
78+ // arrange
79+ var context = _fixture . GetService < AppDbContext > ( ) ;
80+ var todoItem = _todoItemFaker . Generate ( ) ;
81+ var person = _personFaker . Generate ( ) ;
82+ todoItem . Owner = person ;
83+ context . TodoItems . Add ( todoItem ) ;
84+ await context . SaveChangesAsync ( ) ;
85+
86+ var builder = new WebHostBuilder ( )
87+ . UseStartup < Startup > ( ) ;
88+ var httpMethod = new HttpMethod ( "GET" ) ;
89+ var route = $ "/api/v1/todo-items/{ todoItem . Id } ?include=owner";
90+ var server = new TestServer ( builder ) ;
91+ var client = server . CreateClient ( ) ;
92+ var request = new HttpRequestMessage ( httpMethod , route ) ;
93+
94+ // act
95+ var response = await client . SendAsync ( request ) ;
96+ var body = await response . Content . ReadAsStringAsync ( ) ;
97+ var deserializedBody = JsonConvert . DeserializeObject < Document > ( body ) ;
98+
99+ // assert
100+ Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
101+ Assert . Equal ( person . StringId , deserializedBody . Included [ 0 ] . Id ) ;
102+ Assert . NotNull ( deserializedBody . Included [ 0 ] . Relationships ) ;
103+ Assert . Equal ( $ "http://localhost/api/v1/people/{ person . Id } /todo-items", deserializedBody . Included [ 0 ] . Relationships [ "todo-items" ] . Links . Related ) ;
104+ Assert . Equal ( $ "http://localhost/api/v1/people/{ person . Id } /relationships/todo-items", deserializedBody . Included [ 0 ] . Relationships [ "todo-items" ] . Links . Self ) ;
105+ context . Dispose ( ) ;
106+ }
63107 }
64108}
0 commit comments