|
1 | 1 | using System; |
2 | 2 | using System.Net; |
3 | | -using System.Net.Http; |
4 | | -using System.Net.Http.Headers; |
5 | 3 | using System.Threading.Tasks; |
6 | | -using JsonApiDotNetCore.Middleware; |
| 4 | +using FluentAssertions; |
7 | 5 | using JsonApiDotNetCore.Serialization.Objects; |
8 | | -using Microsoft.AspNetCore.Mvc.Testing; |
9 | | -using Microsoft.Extensions.DependencyInjection; |
10 | | -using Newtonsoft.Json; |
11 | 6 | using NoEntityFrameworkExample; |
12 | 7 | using NoEntityFrameworkExample.Data; |
13 | 8 | using NoEntityFrameworkExample.Models; |
| 9 | +using TestBuildingBlocks; |
14 | 10 | using Xunit; |
15 | 11 |
|
16 | 12 | namespace NoEntityFrameworkTests |
17 | 13 | { |
18 | | - public sealed class WorkItemTests : IClassFixture<WebApplicationFactory<Startup>> |
| 14 | + public sealed class WorkItemTests : IClassFixture<RemoteIntegrationTestContext<Startup, AppDbContext>> |
19 | 15 | { |
20 | | - private readonly WebApplicationFactory<Startup> _factory; |
| 16 | + private readonly RemoteIntegrationTestContext<Startup, AppDbContext> _testContext; |
21 | 17 |
|
22 | | - public WorkItemTests(WebApplicationFactory<Startup> factory) |
| 18 | + public WorkItemTests(RemoteIntegrationTestContext<Startup, AppDbContext> testContext) |
23 | 19 | { |
24 | | - _factory = factory; |
| 20 | + _testContext = testContext; |
25 | 21 | } |
26 | 22 |
|
27 | 23 | [Fact] |
28 | | - public async Task Can_Get_WorkItems() |
| 24 | + public async Task Can_get_WorkItems() |
29 | 25 | { |
30 | 26 | // Arrange |
31 | | - await ExecuteOnDbContextAsync(async dbContext => |
| 27 | + await _testContext.RunOnDatabaseAsync(async dbContext => |
32 | 28 | { |
33 | 29 | dbContext.WorkItems.Add(new WorkItem()); |
34 | 30 | await dbContext.SaveChangesAsync(); |
35 | 31 | }); |
36 | 32 |
|
37 | | - var client = _factory.CreateClient(); |
38 | | - |
39 | | - var request = new HttpRequestMessage(HttpMethod.Get, "/api/v1/workItems"); |
| 33 | + var route = "/api/v1/workItems"; |
40 | 34 |
|
41 | 35 | // Act |
42 | | - var response = await client.SendAsync(request); |
| 36 | + var (httpResponse, responseDocument) = await _testContext.ExecuteGetAsync<Document>(route); |
43 | 37 |
|
44 | 38 | // Assert |
45 | | - AssertStatusCode(HttpStatusCode.OK, response); |
46 | | - |
47 | | - string responseBody = await response.Content.ReadAsStringAsync(); |
48 | | - var document = JsonConvert.DeserializeObject<Document>(responseBody); |
| 39 | + httpResponse.Should().HaveStatusCode(HttpStatusCode.OK); |
49 | 40 |
|
50 | | - Assert.NotEmpty(document.ManyData); |
| 41 | + responseDocument.ManyData.Should().NotBeEmpty(); |
51 | 42 | } |
52 | 43 |
|
53 | 44 | [Fact] |
54 | | - public async Task Can_Get_WorkItem_By_Id() |
| 45 | + public async Task Can_get_WorkItem_by_ID() |
55 | 46 | { |
56 | 47 | // Arrange |
57 | 48 | var workItem = new WorkItem(); |
58 | 49 |
|
59 | | - await ExecuteOnDbContextAsync(async dbContext => |
| 50 | + await _testContext.RunOnDatabaseAsync(async dbContext => |
60 | 51 | { |
61 | 52 | dbContext.WorkItems.Add(workItem); |
62 | 53 | await dbContext.SaveChangesAsync(); |
63 | 54 | }); |
64 | 55 |
|
65 | | - var client = _factory.CreateClient(); |
66 | | - |
67 | | - var request = new HttpRequestMessage(HttpMethod.Get, "/api/v1/workItems/" + workItem.StringId); |
| 56 | + var route = "/api/v1/workItems/" + workItem.StringId; |
68 | 57 |
|
69 | 58 | // Act |
70 | | - var response = await client.SendAsync(request); |
| 59 | + var (httpResponse, responseDocument) = await _testContext.ExecuteGetAsync<Document>(route); |
71 | 60 |
|
72 | 61 | // Assert |
73 | | - AssertStatusCode(HttpStatusCode.OK, response); |
| 62 | + httpResponse.Should().HaveStatusCode(HttpStatusCode.OK); |
74 | 63 |
|
75 | | - string responseBody = await response.Content.ReadAsStringAsync(); |
76 | | - var document = JsonConvert.DeserializeObject<Document>(responseBody); |
77 | | - |
78 | | - Assert.NotNull(document.SingleData); |
79 | | - Assert.Equal(workItem.StringId, document.SingleData.Id); |
| 64 | + responseDocument.SingleData.Should().NotBeNull(); |
| 65 | + responseDocument.SingleData.Id.Should().Be(workItem.StringId); |
80 | 66 | } |
81 | 67 |
|
82 | 68 | [Fact] |
83 | | - public async Task Can_Create_WorkItem() |
| 69 | + public async Task Can_create_WorkItem() |
84 | 70 | { |
85 | 71 | // Arrange |
86 | | - var title = Guid.NewGuid().ToString(); |
| 72 | + var newTitle = Guid.NewGuid().ToString(); |
87 | 73 |
|
88 | | - var requestContent = new |
| 74 | + var requestBody = new |
89 | 75 | { |
90 | 76 | data = new |
91 | 77 | { |
92 | 78 | type = "workItems", |
93 | 79 | attributes = new |
94 | 80 | { |
95 | | - title, |
| 81 | + title = newTitle, |
96 | 82 | ordinal = 1 |
97 | 83 | } |
98 | 84 | } |
99 | 85 | }; |
100 | 86 |
|
101 | | - var requestBody = JsonConvert.SerializeObject(requestContent); |
102 | | - |
103 | | - var request = new HttpRequestMessage(HttpMethod.Post, "/api/v1/workItems/") |
104 | | - { |
105 | | - Content = new StringContent(requestBody) |
106 | | - }; |
107 | | - request.Content.Headers.ContentType = new MediaTypeHeaderValue(HeaderConstants.MediaType); |
108 | | - |
109 | | - var client = _factory.CreateClient(); |
| 87 | + var route = "/api/v1/workItems/"; |
110 | 88 |
|
111 | 89 | // Act |
112 | | - var response = await client.SendAsync(request); |
| 90 | + var (httpResponse, responseDocument) = await _testContext.ExecutePostAsync<Document>(route, requestBody); |
113 | 91 |
|
114 | 92 | // Assert |
115 | | - AssertStatusCode(HttpStatusCode.Created, response); |
116 | | - |
117 | | - string responseBody = await response.Content.ReadAsStringAsync(); |
118 | | - var document = JsonConvert.DeserializeObject<Document>(responseBody); |
| 93 | + httpResponse.Should().HaveStatusCode(HttpStatusCode.Created); |
119 | 94 |
|
120 | | - Assert.NotNull(document.SingleData); |
121 | | - Assert.Equal(title, document.SingleData.Attributes["title"]); |
| 95 | + responseDocument.SingleData.Should().NotBeNull(); |
| 96 | + responseDocument.SingleData.Attributes["title"].Should().Be(newTitle); |
122 | 97 | } |
123 | 98 |
|
124 | 99 | [Fact] |
125 | | - public async Task Can_Delete_WorkItem() |
| 100 | + public async Task Can_delete_WorkItem() |
126 | 101 | { |
127 | 102 | // Arrange |
128 | 103 | var workItem = new WorkItem(); |
129 | 104 |
|
130 | | - await ExecuteOnDbContextAsync(async dbContext => |
| 105 | + await _testContext.RunOnDatabaseAsync(async dbContext => |
131 | 106 | { |
132 | 107 | dbContext.WorkItems.Add(workItem); |
133 | 108 | await dbContext.SaveChangesAsync(); |
134 | 109 | }); |
135 | 110 |
|
136 | | - var client = _factory.CreateClient(); |
137 | | - |
138 | | - var request = new HttpRequestMessage(HttpMethod.Delete, "/api/v1/workItems/" + workItem.StringId); |
| 111 | + var route = "/api/v1/workItems/" + workItem.StringId; |
139 | 112 |
|
140 | 113 | // Act |
141 | | - var response = await client.SendAsync(request); |
| 114 | + var (httpResponse, responseDocument) = await _testContext.ExecuteDeleteAsync<string>(route); |
142 | 115 |
|
143 | 116 | // Assert |
144 | | - AssertStatusCode(HttpStatusCode.NoContent, response); |
145 | | - |
146 | | - string responseBody = await response.Content.ReadAsStringAsync(); |
147 | | - Assert.Empty(responseBody); |
148 | | - } |
149 | | - |
150 | | - private async Task ExecuteOnDbContextAsync(Func<AppDbContext, Task> asyncAction) |
151 | | - { |
152 | | - using IServiceScope scope = _factory.Services.CreateScope(); |
153 | | - var dbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>(); |
| 117 | + httpResponse.Should().HaveStatusCode(HttpStatusCode.NoContent); |
154 | 118 |
|
155 | | - await asyncAction(dbContext); |
156 | | - } |
157 | | - |
158 | | - private static void AssertStatusCode(HttpStatusCode expected, HttpResponseMessage response) |
159 | | - { |
160 | | - if (expected != response.StatusCode) |
161 | | - { |
162 | | - var responseBody = response.Content.ReadAsStringAsync().Result; |
163 | | - Assert.True(expected == response.StatusCode, $"Got {response.StatusCode} status code instead of {expected}. Response body: {responseBody}"); |
164 | | - } |
| 119 | + responseDocument.Should().BeEmpty(); |
165 | 120 | } |
166 | 121 | } |
167 | 122 | } |
0 commit comments