Skip to content

Commit 6ed2a4f

Browse files
committed
test(base-controller): add unit tests
1 parent a894f69 commit 6ed2a4f

File tree

4 files changed

+270
-4
lines changed

4 files changed

+270
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
*.user
44
.couscous/
55
docs/Template-Dark/
6+
.idea/

src/JsonApiDotNetCore/Controllers/BaseJsonApiController.cs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,34 @@
88

99
namespace JsonApiDotNetCore.Controllers
1010
{
11+
public class BaseJsonApiController<T>
12+
: BaseJsonApiController<T, int>
13+
where T : class, IIdentifiable<int>
14+
{
15+
public BaseJsonApiController(
16+
IJsonApiContext jsonApiContext,
17+
IResourceService<T, int> resourceService
18+
) : base(jsonApiContext, resourceService) { }
19+
20+
public BaseJsonApiController(
21+
IJsonApiContext jsonApiContext,
22+
IResourceQueryService<T, int> queryService = null,
23+
IResourceCmdService<T, int> cmdService = null
24+
) : base(jsonApiContext, queryService, cmdService) { }
25+
26+
public BaseJsonApiController(
27+
IJsonApiContext jsonApiContext,
28+
IGetAllService<T, int> getAll = null,
29+
IGetByIdService<T, int> getById = null,
30+
IGetRelationshipService<T, int> getRelationship = null,
31+
IGetRelationshipsService<T, int> getRelationships = null,
32+
ICreateService<T, int> create = null,
33+
IUpdateService<T, int> update = null,
34+
IUpdateRelationshipService<T, int> updateRelationships = null,
35+
IDeleteService<T, int> delete = null
36+
) : base(jsonApiContext, getAll, getById, getRelationship, getRelationships, create, update, updateRelationships, delete) { }
37+
}
38+
1139
public class BaseJsonApiController<T, TId>
1240
: JsonApiControllerMixin
1341
where T : class, IIdentifiable<TId>
@@ -22,7 +50,7 @@ public class BaseJsonApiController<T, TId>
2250
private readonly IDeleteService<T, TId> _delete;
2351
private readonly IJsonApiContext _jsonApiContext;
2452

25-
protected BaseJsonApiController(
53+
public BaseJsonApiController(
2654
IJsonApiContext jsonApiContext,
2755
IResourceService<T, TId> resourceService)
2856
{
@@ -37,7 +65,7 @@ protected BaseJsonApiController(
3765
_delete = resourceService;
3866
}
3967

40-
protected BaseJsonApiController(
68+
public BaseJsonApiController(
4169
IJsonApiContext jsonApiContext,
4270
IResourceQueryService<T, TId> queryService = null,
4371
IResourceCmdService<T, TId> cmdService = null)
@@ -53,7 +81,7 @@ protected BaseJsonApiController(
5381
_delete = cmdService;
5482
}
5583

56-
protected BaseJsonApiController(
84+
public BaseJsonApiController(
5785
IJsonApiContext jsonApiContext,
5886
IGetAllService<T, TId> getAll = null,
5987
IGetByIdService<T, TId> getById = null,
Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
using System;
2+
using JsonApiDotNetCore.Controllers;
3+
using JsonApiDotNetCore.Models;
4+
using JsonApiDotNetCore.Services;
5+
using Moq;
6+
using Xunit;
7+
using System.Threading.Tasks;
8+
using JsonApiDotNetCore.Internal;
9+
10+
namespace UnitTests
11+
{
12+
public class BaseJsonApiController_Tests
13+
{
14+
public class Resource : Identifiable { }
15+
private Mock<IJsonApiContext> _jsonApiContextMock = new Mock<IJsonApiContext>();
16+
17+
[Fact]
18+
public async Task GetAsync_Calls_Service()
19+
{
20+
// arrange
21+
var serviceMock = new Mock<IGetAllService<Resource>>();
22+
var controller = new BaseJsonApiController<Resource>(_jsonApiContextMock.Object, getAll: serviceMock.Object);
23+
24+
// act
25+
await controller.GetAsync();
26+
27+
// assert
28+
serviceMock.Verify(m => m.GetAsync(), Times.Once);
29+
VerifyApplyContext();
30+
}
31+
32+
[Fact]
33+
public async Task GetAsync_Throws_405_If_No_Service()
34+
{
35+
// arrange
36+
var serviceMock = new Mock<IGetAllService<Resource>>();
37+
var controller = new BaseJsonApiController<Resource>(_jsonApiContextMock.Object, null);
38+
39+
// act
40+
var exception = await Assert.ThrowsAsync<JsonApiException>(() => controller.GetAsync());
41+
42+
// assert
43+
Assert.Equal(405, exception.GetStatusCode());
44+
}
45+
46+
[Fact]
47+
public async Task GetAsyncById_Calls_Service()
48+
{
49+
// arrange
50+
const int id = 0;
51+
var serviceMock = new Mock<IGetByIdService<Resource>>();
52+
var controller = new BaseJsonApiController<Resource>(_jsonApiContextMock.Object, getById: serviceMock.Object);
53+
54+
// act
55+
await controller.GetAsync(id);
56+
57+
// assert
58+
serviceMock.Verify(m => m.GetAsync(id), Times.Once);
59+
VerifyApplyContext();
60+
}
61+
62+
[Fact]
63+
public async Task GetAsyncById_Throws_405_If_No_Service()
64+
{
65+
// arrange
66+
const int id = 0;
67+
var serviceMock = new Mock<IGetByIdService<Resource>>();
68+
var controller = new BaseJsonApiController<Resource>(_jsonApiContextMock.Object, getById: null);
69+
70+
// act
71+
var exception = await Assert.ThrowsAsync<JsonApiException>(() => controller.GetAsync(id));
72+
73+
// assert
74+
Assert.Equal(405, exception.GetStatusCode());
75+
}
76+
77+
[Fact]
78+
public async Task GetRelationshipsAsync_Calls_Service()
79+
{
80+
// arrange
81+
const int id = 0;
82+
var serviceMock = new Mock<IGetRelationshipsService<Resource>>();
83+
var controller = new BaseJsonApiController<Resource>(_jsonApiContextMock.Object, getRelationships: serviceMock.Object);
84+
85+
// act
86+
await controller.GetRelationshipsAsync(id, string.Empty);
87+
88+
// assert
89+
serviceMock.Verify(m => m.GetRelationshipsAsync(id, string.Empty), Times.Once);
90+
VerifyApplyContext();
91+
}
92+
93+
[Fact]
94+
public async Task GetRelationshipsAsync_Throws_405_If_No_Service()
95+
{
96+
// arrange
97+
const int id = 0;
98+
var serviceMock = new Mock<IGetRelationshipsService<Resource>>();
99+
var controller = new BaseJsonApiController<Resource>(_jsonApiContextMock.Object, getRelationships: null);
100+
101+
// act
102+
var exception = await Assert.ThrowsAsync<JsonApiException>(() => controller.GetRelationshipsAsync(id, string.Empty));
103+
104+
// assert
105+
Assert.Equal(405, exception.GetStatusCode());
106+
}
107+
108+
[Fact]
109+
public async Task GetRelationshipAsync_Calls_Service()
110+
{
111+
// arrange
112+
const int id = 0;
113+
var serviceMock = new Mock<IGetRelationshipService<Resource>>();
114+
var controller = new BaseJsonApiController<Resource>(_jsonApiContextMock.Object, getRelationship: serviceMock.Object);
115+
116+
// act
117+
await controller.GetRelationshipAsync(id, string.Empty);
118+
119+
// assert
120+
serviceMock.Verify(m => m.GetRelationshipAsync(id, string.Empty), Times.Once);
121+
VerifyApplyContext();
122+
}
123+
124+
[Fact]
125+
public async Task GetRelationshipAsync_Throws_405_If_No_Service()
126+
{
127+
// arrange
128+
const int id = 0;
129+
var serviceMock = new Mock<IGetRelationshipService<Resource>>();
130+
var controller = new BaseJsonApiController<Resource>(_jsonApiContextMock.Object, getRelationship: null);
131+
132+
// act
133+
var exception = await Assert.ThrowsAsync<JsonApiException>(() => controller.GetRelationshipAsync(id, string.Empty));
134+
135+
// assert
136+
Assert.Equal(405, exception.GetStatusCode());
137+
}
138+
139+
[Fact]
140+
public async Task PatchAsync_Calls_Service()
141+
{
142+
// arrange
143+
const int id = 0;
144+
var resource = new Resource();
145+
var serviceMock = new Mock<IUpdateService<Resource>>();
146+
var controller = new BaseJsonApiController<Resource>(_jsonApiContextMock.Object, update: serviceMock.Object);
147+
148+
// act
149+
await controller.PatchAsync(id, resource);
150+
151+
// assert
152+
serviceMock.Verify(m => m.UpdateAsync(id, It.IsAny<Resource>()), Times.Once);
153+
VerifyApplyContext();
154+
}
155+
156+
[Fact]
157+
public async Task PatchAsync_Throws_405_If_No_Service()
158+
{
159+
// arrange
160+
const int id = 0;
161+
var serviceMock = new Mock<IUpdateService<Resource>>();
162+
var controller = new BaseJsonApiController<Resource>(_jsonApiContextMock.Object, update: null);
163+
164+
// act
165+
var exception = await Assert.ThrowsAsync<JsonApiException>(() => controller.PatchAsync(id, It.IsAny<Resource>()));
166+
167+
// assert
168+
Assert.Equal(405, exception.GetStatusCode());
169+
}
170+
171+
[Fact]
172+
public async Task PatchRelationshipsAsync_Calls_Service()
173+
{
174+
// arrange
175+
const int id = 0;
176+
var resource = new Resource();
177+
var serviceMock = new Mock<IUpdateRelationshipService<Resource>>();
178+
var controller = new BaseJsonApiController<Resource>(_jsonApiContextMock.Object, updateRelationships: serviceMock.Object);
179+
180+
// act
181+
await controller.PatchRelationshipsAsync(id, string.Empty, null);
182+
183+
// assert
184+
serviceMock.Verify(m => m.UpdateRelationshipsAsync(id, string.Empty, null), Times.Once);
185+
VerifyApplyContext();
186+
}
187+
188+
[Fact]
189+
public async Task PatchRelationshipsAsync_Throws_405_If_No_Service()
190+
{
191+
// arrange
192+
const int id = 0;
193+
var serviceMock = new Mock<IUpdateRelationshipService<Resource>>();
194+
var controller = new BaseJsonApiController<Resource>(_jsonApiContextMock.Object, updateRelationships: null);
195+
196+
// act
197+
var exception = await Assert.ThrowsAsync<JsonApiException>(() => controller.PatchRelationshipsAsync(id, string.Empty, null));
198+
199+
// assert
200+
Assert.Equal(405, exception.GetStatusCode());
201+
}
202+
203+
[Fact]
204+
public async Task DeleteAsync_Calls_Service()
205+
{
206+
// arrange
207+
const int id = 0;
208+
var resource = new Resource();
209+
var serviceMock = new Mock<IDeleteService<Resource>>();
210+
var controller = new BaseJsonApiController<Resource>(_jsonApiContextMock.Object, delete: serviceMock.Object);
211+
212+
// act
213+
await controller.DeleteAsync(id);
214+
215+
// assert
216+
serviceMock.Verify(m => m.DeleteAsync(id), Times.Once);
217+
VerifyApplyContext();
218+
}
219+
220+
[Fact]
221+
public async Task DeleteAsync_Throws_405_If_No_Service()
222+
{
223+
// arrange
224+
const int id = 0;
225+
var serviceMock = new Mock<IUpdateRelationshipService<Resource>>();
226+
var controller = new BaseJsonApiController<Resource>(_jsonApiContextMock.Object, delete: null);
227+
228+
// act
229+
var exception = await Assert.ThrowsAsync<JsonApiException>(() => controller.DeleteAsync(id));
230+
231+
// assert
232+
Assert.Equal(405, exception.GetStatusCode());
233+
}
234+
235+
private void VerifyApplyContext()
236+
=> _jsonApiContextMock.Verify(m => m.ApplyContext<Resource>(It.IsAny<BaseJsonApiController<Resource>>()), Times.Once);
237+
}
238+
}

test/UnitTests/Controllers/JsonApiControllerMixin_Tests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ public void Errors_Correctly_Infers_Status_Code()
3838
new Error("502", "really bad specific"),
3939
}
4040
};
41-
4241

4342
// act
4443
var result422 = this.Errors(errors422);

0 commit comments

Comments
 (0)