22using System . Net . Http ;
33using System . Threading . Tasks ;
44using DotNetCoreDocs ;
5- using DotNetCoreDocs . Models ;
65using DotNetCoreDocs . Writers ;
76using JsonApiDotNetCoreExample ;
87using Microsoft . AspNetCore . Hosting ;
98using Microsoft . AspNetCore . TestHost ;
109using Newtonsoft . Json ;
1110using Xunit ;
12- using JsonApiDotNetCore . Internal ;
1311using JsonApiDotNetCore . Models ;
12+ using JsonApiDotNetCoreExample . Data ;
13+ using System . Linq ;
14+ using System ;
1415
1516namespace JsonApiDotNetCoreExampleTests . Acceptance . Spec
1617{
1718 [ Collection ( "WebHostCollection" ) ]
1819 public class Relationships
1920 {
2021 private DocsFixture < Startup , JsonDocWriter > _fixture ;
22+ private AppDbContext _context ;
2123 public Relationships ( DocsFixture < Startup , JsonDocWriter > fixture )
2224 {
2325 _fixture = fixture ;
26+ _context = fixture . GetService < AppDbContext > ( ) ;
2427 }
2528
2629 [ Fact ]
@@ -50,6 +53,35 @@ public async Task Correct_RelationshipObjects_For_ManyToOne_Relationships()
5053 Assert . Equal ( expectedOwnerRelatedLink , data . Relationships [ "owner" ] . Links . Related ) ;
5154 }
5255
56+ [ Fact ]
57+ public async Task Correct_RelationshipObjects_For_ManyToOne_Relationships_ById ( )
58+ {
59+ // arrange
60+ var todoItemId = _context . TodoItems . Last ( ) . Id ;
61+
62+ var builder = new WebHostBuilder ( )
63+ . UseStartup < Startup > ( ) ;
64+
65+ var httpMethod = new HttpMethod ( "GET" ) ;
66+ var route = $ "/api/v1/todo-items/{ todoItemId } ";
67+
68+ var server = new TestServer ( builder ) ;
69+ var client = server . CreateClient ( ) ;
70+ var request = new HttpRequestMessage ( httpMethod , route ) ;
71+
72+ // act
73+ var response = await client . SendAsync ( request ) ;
74+ var responseString = await response . Content . ReadAsStringAsync ( ) ;
75+ var data = JsonConvert . DeserializeObject < Document > ( responseString ) . Data ;
76+ var expectedOwnerSelfLink = $ "http://localhost/api/v1/todo-items/{ todoItemId } /relationships/owner";
77+ var expectedOwnerRelatedLink = $ "http://localhost/api/v1/todo-items/{ todoItemId } /owner";
78+
79+ // assert
80+ Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
81+ Assert . Equal ( expectedOwnerSelfLink , data . Relationships [ "owner" ] . Links ? . Self ) ;
82+ Assert . Equal ( expectedOwnerRelatedLink , data . Relationships [ "owner" ] . Links . Related ) ;
83+ }
84+
5385 [ Fact ]
5486 public async Task Correct_RelationshipObjects_For_OneToMany_Relationships ( )
5587 {
@@ -76,5 +108,34 @@ public async Task Correct_RelationshipObjects_For_OneToMany_Relationships()
76108 Assert . Equal ( expectedOwnerSelfLink , data . Relationships [ "todo-items" ] . Links . Self ) ;
77109 Assert . Equal ( expectedOwnerRelatedLink , data . Relationships [ "todo-items" ] . Links . Related ) ;
78110 }
111+
112+ [ Fact ]
113+ public async Task Correct_RelationshipObjects_For_OneToMany_Relationships_ById ( )
114+ {
115+ // arrange
116+ var personId = _context . People . Last ( ) . Id ;
117+
118+ var builder = new WebHostBuilder ( )
119+ . UseStartup < Startup > ( ) ;
120+
121+ var httpMethod = new HttpMethod ( "GET" ) ;
122+ var route = $ "/api/v1/people/{ personId } ";
123+
124+ var server = new TestServer ( builder ) ;
125+ var client = server . CreateClient ( ) ;
126+ var request = new HttpRequestMessage ( httpMethod , route ) ;
127+
128+ // act
129+ var response = await client . SendAsync ( request ) ;
130+ var responseString = await response . Content . ReadAsStringAsync ( ) ;
131+ var data = JsonConvert . DeserializeObject < Document > ( responseString ) . Data ;
132+ var expectedOwnerSelfLink = $ "http://localhost/api/v1/people/{ personId } /relationships/todo-items";
133+ var expectedOwnerRelatedLink = $ "http://localhost/api/v1/people/{ personId } /todo-items";
134+
135+ // assert
136+ Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
137+ Assert . Equal ( expectedOwnerSelfLink , data . Relationships [ "todo-items" ] . Links ? . Self ) ;
138+ Assert . Equal ( expectedOwnerRelatedLink , data . Relationships [ "todo-items" ] . Links . Related ) ;
139+ }
79140 }
80141}
0 commit comments