1
+ using System ;
1
2
using JsonApiDotNetCore . Configuration ;
2
3
using JsonApiDotNetCore . Internal ;
3
4
using JsonApiDotNetCore . Internal . Contracts ;
@@ -18,6 +19,8 @@ public class LinkBuilderTests
18
19
private readonly IPageService _pageService ;
19
20
private readonly Mock < IResourceGraph > _provider = new Mock < IResourceGraph > ( ) ;
20
21
private const string _host = "http://www.example.com" ;
22
+ private const int _baseId = 123 ;
23
+ private const string _relationshipName = "author" ;
21
24
private const string _topSelf = "http://www.example.com/articles" ;
22
25
private const string _resourceSelf = "http://www.example.com/articles/123" ;
23
26
private const string _relSelf = "http://www.example.com/articles/123/relationships/author" ;
@@ -47,7 +50,7 @@ public void BuildResourceLinks_GlobalAndResourceConfiguration_ExpectedResult(Lin
47
50
var builder = new LinkBuilder ( config , GetRequestManager ( ) , null , _provider . Object ) ;
48
51
49
52
// Act
50
- var links = builder . GetResourceLinks ( "articles" , "123" ) ;
53
+ var links = builder . GetResourceLinks ( "articles" , _baseId . ToString ( ) ) ;
51
54
52
55
// Assert
53
56
if ( expectedResult == null )
@@ -96,7 +99,7 @@ public void BuildRelationshipLinks_GlobalResourceAndAttrConfiguration_ExpectedLi
96
99
var attr = new HasOneAttribute ( links : relationship ) { RightType = typeof ( Author ) , PublicRelationshipName = "author" } ;
97
100
98
101
// Act
99
- var links = builder . GetRelationshipLinks ( attr , new Article { Id = 123 } ) ;
102
+ var links = builder . GetRelationshipLinks ( attr , new Article { Id = _baseId } ) ;
100
103
101
104
// Assert
102
105
if ( expectedSelfLink == null && expectedRelatedLink == null )
@@ -131,20 +134,32 @@ public void BuildRelationshipLinks_GlobalResourceAndAttrConfiguration_ExpectedLi
131
134
[ InlineData ( Link . None , Link . Self , _topSelf , false ) ]
132
135
[ InlineData ( Link . None , Link . Paging , null , true ) ]
133
136
[ InlineData ( Link . None , Link . None , null , false ) ]
137
+ [ InlineData ( Link . All , Link . Self , _resourceSelf , false ) ]
138
+ [ InlineData ( Link . Self , Link . Self , _resourceSelf , false ) ]
139
+ [ InlineData ( Link . Paging , Link . Self , _resourceSelf , false ) ]
140
+ [ InlineData ( Link . None , Link . Self , _resourceSelf , false ) ]
141
+ [ InlineData ( Link . All , Link . Self , _relRelated , false ) ]
142
+ [ InlineData ( Link . Self , Link . Self , _relRelated , false ) ]
143
+ [ InlineData ( Link . Paging , Link . Self , _relRelated , false ) ]
144
+ [ InlineData ( Link . None , Link . Self , _relRelated , false ) ]
134
145
public void BuildTopLevelLinks_GlobalAndResourceConfiguration_ExpectedLinks ( Link global ,
135
146
Link resource ,
136
- object expectedSelfLink ,
147
+ string expectedSelfLink ,
137
148
bool pages )
138
149
{
139
150
// Arrange
140
151
var config = GetConfiguration ( topLevelLinks : global ) ;
141
152
var primaryResource = GetResourceContext < Article > ( topLevelLinks : resource ) ;
142
153
_provider . Setup ( m => m . GetResourceContext < Article > ( ) ) . Returns ( primaryResource ) ;
143
154
144
- var builder = new LinkBuilder ( config , GetRequestManager ( ) , _pageService , _provider . Object ) ;
155
+ bool useBaseId = expectedSelfLink != _topSelf ;
156
+ string relationshipName = expectedSelfLink == _relRelated ? _relationshipName : null ;
157
+ ICurrentRequest currentRequest = GetRequestManager ( primaryResource , useBaseId , relationshipName ) ;
158
+
159
+ var builder = new LinkBuilder ( config , currentRequest , _pageService , _provider . Object ) ;
145
160
146
161
// Act
147
- var links = builder . GetTopLevelLinks ( primaryResource ) ;
162
+ var links = builder . GetTopLevelLinks ( ) ;
148
163
149
164
// Assert
150
165
if ( ! pages && expectedSelfLink == null )
@@ -153,11 +168,11 @@ public void BuildTopLevelLinks_GlobalAndResourceConfiguration_ExpectedLinks(Link
153
168
}
154
169
else
155
170
{
156
- Assert . True ( CheckPages ( links , pages ) ) ;
171
+ Assert . True ( CheckLinks ( links , pages , expectedSelfLink ) ) ;
157
172
}
158
173
}
159
174
160
- private bool CheckPages ( TopLevelLinks links , bool pages )
175
+ private bool CheckLinks ( TopLevelLinks links , bool pages , string expectedSelfLink )
161
176
{
162
177
if ( pages )
163
178
{
@@ -167,13 +182,16 @@ private bool CheckPages(TopLevelLinks links, bool pages)
167
182
&& links . Next == $ "{ _host } /articles?page[size]=10&page[number]=3"
168
183
&& links . Last == $ "{ _host } /articles?page[size]=10&page[number]=3";
169
184
}
170
- return links . First == null && links . Prev == null && links . Next == null && links . Last == null ;
185
+
186
+ return links . Self == expectedSelfLink && links . First == null && links . Prev == null && links . Next == null && links . Last == null ;
171
187
}
172
188
173
- private ICurrentRequest GetRequestManager ( ResourceContext resourceContext = null )
189
+ private ICurrentRequest GetRequestManager ( ResourceContext resourceContext = null , bool useBaseId = false , string relationshipName = null )
174
190
{
175
191
var mock = new Mock < ICurrentRequest > ( ) ;
176
192
mock . Setup ( m => m . BasePath ) . Returns ( _host ) ;
193
+ mock . Setup ( m => m . BaseId ) . Returns ( useBaseId ? _baseId . ToString ( ) : null ) ;
194
+ mock . Setup ( m => m . RequestRelationship ) . Returns ( relationshipName != null ? new HasOneAttribute ( relationshipName ) : null ) ;
177
195
mock . Setup ( m => m . GetRequestResource ( ) ) . Returns ( resourceContext ) ;
178
196
return mock . Object ;
179
197
}
0 commit comments