|
| 1 | +using System.Net; |
| 2 | +using System.Net.Http; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using DotNetCoreDocs; |
| 5 | +using DotNetCoreDocs.Writers; |
| 6 | +using JsonApiDotNetCoreExample; |
| 7 | +using Microsoft.AspNetCore.Hosting; |
| 8 | +using Microsoft.AspNetCore.TestHost; |
| 9 | +using Newtonsoft.Json; |
| 10 | +using Xunit; |
| 11 | +using JsonApiDotNetCore.Models; |
| 12 | +using JsonApiDotNetCoreExample.Data; |
| 13 | +using System.Linq; |
| 14 | +using System; |
| 15 | + |
| 16 | +namespace JsonApiDotNetCoreExampleTests.Acceptance.Spec |
| 17 | +{ |
| 18 | + [Collection("WebHostCollection")] |
| 19 | + public class Relationships |
| 20 | + { |
| 21 | + private DocsFixture<Startup, JsonDocWriter> _fixture; |
| 22 | + private AppDbContext _context; |
| 23 | + public Relationships(DocsFixture<Startup, JsonDocWriter> fixture) |
| 24 | + { |
| 25 | + _fixture = fixture; |
| 26 | + _context = fixture.GetService<AppDbContext>(); |
| 27 | + } |
| 28 | + |
| 29 | + [Fact] |
| 30 | + public async Task Correct_RelationshipObjects_For_ManyToOne_Relationships() |
| 31 | + { |
| 32 | + // arrange |
| 33 | + var builder = new WebHostBuilder() |
| 34 | + .UseStartup<Startup>(); |
| 35 | + |
| 36 | + var httpMethod = new HttpMethod("GET"); |
| 37 | + var route = $"/api/v1/todo-items"; |
| 38 | + |
| 39 | + var server = new TestServer(builder); |
| 40 | + var client = server.CreateClient(); |
| 41 | + var request = new HttpRequestMessage(httpMethod, route); |
| 42 | + |
| 43 | + // act |
| 44 | + var response = await client.SendAsync(request); |
| 45 | + var documents = JsonConvert.DeserializeObject<Documents>(await response.Content.ReadAsStringAsync()); |
| 46 | + var data = documents.Data[0]; |
| 47 | + var expectedOwnerSelfLink = $"http://localhost/api/v1/todo-items/{data.Id}/relationships/owner"; |
| 48 | + var expectedOwnerRelatedLink = $"http://localhost/api/v1/todo-items/{data.Id}/owner"; |
| 49 | + |
| 50 | + // assert |
| 51 | + Assert.Equal(HttpStatusCode.OK, response.StatusCode); |
| 52 | + Assert.Equal(expectedOwnerSelfLink, data.Relationships["owner"].Links.Self); |
| 53 | + Assert.Equal(expectedOwnerRelatedLink, data.Relationships["owner"].Links.Related); |
| 54 | + } |
| 55 | + |
| 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 | + |
| 85 | + [Fact] |
| 86 | + public async Task Correct_RelationshipObjects_For_OneToMany_Relationships() |
| 87 | + { |
| 88 | + // arrange |
| 89 | + var builder = new WebHostBuilder() |
| 90 | + .UseStartup<Startup>(); |
| 91 | + |
| 92 | + var httpMethod = new HttpMethod("GET"); |
| 93 | + var route = $"/api/v1/people"; |
| 94 | + |
| 95 | + var server = new TestServer(builder); |
| 96 | + var client = server.CreateClient(); |
| 97 | + var request = new HttpRequestMessage(httpMethod, route); |
| 98 | + |
| 99 | + // act |
| 100 | + var response = await client.SendAsync(request); |
| 101 | + var documents = JsonConvert.DeserializeObject<Documents>(await response.Content.ReadAsStringAsync()); |
| 102 | + var data = documents.Data[0]; |
| 103 | + var expectedOwnerSelfLink = $"http://localhost/api/v1/people/{data.Id}/relationships/todo-items"; |
| 104 | + var expectedOwnerRelatedLink = $"http://localhost/api/v1/people/{data.Id}/todo-items"; |
| 105 | + |
| 106 | + // assert |
| 107 | + Assert.Equal(HttpStatusCode.OK, response.StatusCode); |
| 108 | + Assert.Equal(expectedOwnerSelfLink, data.Relationships["todo-items"].Links.Self); |
| 109 | + Assert.Equal(expectedOwnerRelatedLink, data.Relationships["todo-items"].Links.Related); |
| 110 | + } |
| 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 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments