|
| 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 Person = JsonApiDotNetCoreExample.Models.Person; |
| 12 | +using JsonApiDotNetCore.Models; |
| 13 | +using JsonApiDotNetCoreExample.Data; |
| 14 | +using Bogus; |
| 15 | +using JsonApiDotNetCoreExample.Models; |
| 16 | +using System; |
| 17 | + |
| 18 | +namespace JsonApiDotNetCoreExampleTests.Acceptance.Spec.DocumentTests |
| 19 | +{ |
| 20 | + [Collection("WebHostCollection")] |
| 21 | + public class Included |
| 22 | + { |
| 23 | + private DocsFixture<Startup, JsonDocWriter> _fixture; |
| 24 | + private AppDbContext _context; |
| 25 | + private Faker<Person> _personFaker; |
| 26 | + private Faker<TodoItem> _todoItemFaker; |
| 27 | + |
| 28 | + public Included(DocsFixture<Startup, JsonDocWriter> fixture) |
| 29 | + { |
| 30 | + _fixture = fixture; |
| 31 | + _context = fixture.GetService<AppDbContext>(); |
| 32 | + _personFaker = new Faker<Person>() |
| 33 | + .RuleFor(p => p.FirstName, f => f.Name.FirstName()) |
| 34 | + .RuleFor(p => p.LastName, f => f.Name.LastName()); |
| 35 | + |
| 36 | + _todoItemFaker = new Faker<TodoItem>() |
| 37 | + .RuleFor(t => t.Description, f => f.Lorem.Sentence()) |
| 38 | + .RuleFor(t => t.Ordinal, f => f.Random.Number()); |
| 39 | + } |
| 40 | + |
| 41 | + [Fact] |
| 42 | + public async Task GET_Included_Contains_SideloadedData_ForManyToOne() |
| 43 | + { |
| 44 | + // arrange |
| 45 | + var builder = new WebHostBuilder() |
| 46 | + .UseStartup<Startup>(); |
| 47 | + |
| 48 | + var httpMethod = new HttpMethod("GET"); |
| 49 | + var route = $"/api/v1/todo-items?include=owner"; |
| 50 | + |
| 51 | + var server = new TestServer(builder); |
| 52 | + var client = server.CreateClient(); |
| 53 | + var request = new HttpRequestMessage(httpMethod, route); |
| 54 | + |
| 55 | + // act |
| 56 | + var response = await client.SendAsync(request); |
| 57 | + var documents = JsonConvert.DeserializeObject<Documents>(await response.Content.ReadAsStringAsync()); |
| 58 | + var data = documents.Data[0]; |
| 59 | + |
| 60 | + // assert |
| 61 | + Assert.Equal(HttpStatusCode.OK, response.StatusCode); |
| 62 | + Assert.NotEmpty(documents.Included); |
| 63 | + Assert.Equal(documents.Data.Count, documents.Included.Count); |
| 64 | + } |
| 65 | + |
| 66 | + [Fact] |
| 67 | + public async Task GET_ById_Included_Contains_SideloadedData_ForManyToOne() |
| 68 | + { |
| 69 | + // arrange |
| 70 | + var person = _personFaker.Generate(); |
| 71 | + var todoItem = _todoItemFaker.Generate(); |
| 72 | + todoItem.Owner = person; |
| 73 | + _context.TodoItems.Add(todoItem); |
| 74 | + _context.SaveChanges(); |
| 75 | + |
| 76 | + var builder = new WebHostBuilder() |
| 77 | + .UseStartup<Startup>(); |
| 78 | + |
| 79 | + var httpMethod = new HttpMethod("GET"); |
| 80 | + |
| 81 | + var route = $"/api/v1/todo-items/{todoItem.Id}?include=owner"; |
| 82 | + |
| 83 | + var server = new TestServer(builder); |
| 84 | + var client = server.CreateClient(); |
| 85 | + var request = new HttpRequestMessage(httpMethod, route); |
| 86 | + |
| 87 | + // act |
| 88 | + var response = await client.SendAsync(request); |
| 89 | + var responseString = await response.Content.ReadAsStringAsync(); |
| 90 | + var document = JsonConvert.DeserializeObject<Document>(responseString); |
| 91 | + |
| 92 | + // assert |
| 93 | + Assert.Equal(HttpStatusCode.OK, response.StatusCode); |
| 94 | + Assert.NotEmpty(document.Included); |
| 95 | + Assert.Equal(person.Id.ToString(), document.Included[0].Id); |
| 96 | + Assert.Equal(person.FirstName, document.Included[0].Attributes["first-name"]); |
| 97 | + Assert.Equal(person.LastName, document.Included[0].Attributes["last-name"]); |
| 98 | + } |
| 99 | + |
| 100 | + [Fact] |
| 101 | + public async Task GET_Included_Contains_SideloadedData_OneToMany() |
| 102 | + { |
| 103 | + // arrange |
| 104 | + _context.People.RemoveRange(_context.People); // ensure all people have todo-items |
| 105 | + var person = _personFaker.Generate(); |
| 106 | + var todoItem = _todoItemFaker.Generate(); |
| 107 | + todoItem.Owner = person; |
| 108 | + _context.TodoItems.Add(todoItem); |
| 109 | + _context.SaveChanges(); |
| 110 | + |
| 111 | + var builder = new WebHostBuilder() |
| 112 | + .UseStartup<Startup>(); |
| 113 | + |
| 114 | + var httpMethod = new HttpMethod("GET"); |
| 115 | + var route = $"/api/v1/people?include=todo-items"; |
| 116 | + |
| 117 | + var server = new TestServer(builder); |
| 118 | + var client = server.CreateClient(); |
| 119 | + var request = new HttpRequestMessage(httpMethod, route); |
| 120 | + |
| 121 | + // act |
| 122 | + var response = await client.SendAsync(request); |
| 123 | + var documents = JsonConvert.DeserializeObject<Documents>(await response.Content.ReadAsStringAsync()); |
| 124 | + var data = documents.Data[0]; |
| 125 | + |
| 126 | + // assert |
| 127 | + Assert.Equal(HttpStatusCode.OK, response.StatusCode); |
| 128 | + Assert.NotEmpty(documents.Included); |
| 129 | + Assert.Equal(documents.Data.Count, documents.Included.Count); |
| 130 | + } |
| 131 | + |
| 132 | + [Fact] |
| 133 | + public async Task GET_ById_Included_Contains_SideloadedData_ForOneToMany() |
| 134 | + { |
| 135 | + // arrange |
| 136 | + const int numberOfTodoItems = 5; |
| 137 | + var person = _personFaker.Generate(); |
| 138 | + for (var i = 0; i < numberOfTodoItems; i++) |
| 139 | + { |
| 140 | + var todoItem = _todoItemFaker.Generate(); |
| 141 | + todoItem.Owner = person; |
| 142 | + _context.TodoItems.Add(todoItem); |
| 143 | + _context.SaveChanges(); |
| 144 | + } |
| 145 | + |
| 146 | + var builder = new WebHostBuilder() |
| 147 | + .UseStartup<Startup>(); |
| 148 | + |
| 149 | + var httpMethod = new HttpMethod("GET"); |
| 150 | + |
| 151 | + var route = $"/api/v1/people/{person.Id}?include=todo-items"; |
| 152 | + |
| 153 | + var server = new TestServer(builder); |
| 154 | + var client = server.CreateClient(); |
| 155 | + var request = new HttpRequestMessage(httpMethod, route); |
| 156 | + |
| 157 | + // act |
| 158 | + var response = await client.SendAsync(request); |
| 159 | + var responseString = await response.Content.ReadAsStringAsync(); |
| 160 | + var document = JsonConvert.DeserializeObject<Document>(responseString); |
| 161 | + |
| 162 | + // assert |
| 163 | + Assert.Equal(HttpStatusCode.OK, response.StatusCode); |
| 164 | + Assert.NotEmpty(document.Included); |
| 165 | + Assert.Equal(numberOfTodoItems, document.Included.Count); |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments