-
-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathIncludedResourceObjectBuilderTests.cs
217 lines (180 loc) · 9.25 KB
/
IncludedResourceObjectBuilderTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
using System.Collections.Generic;
using System.Linq;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Queries;
using JsonApiDotNetCore.Resources;
using JsonApiDotNetCore.Resources.Annotations;
using JsonApiDotNetCore.Serialization;
using JsonApiDotNetCore.Serialization.Building;
using JsonApiDotNetCore.Serialization.Objects;
using Moq;
using UnitTests.TestModels;
using Xunit;
namespace UnitTests.Serialization.Server
{
public sealed class IncludedResourceObjectBuilderTests : SerializerTestsSetup
{
[Fact]
public void BuildIncluded_DeeplyNestedCircularChainOfSingleData_CanBuild()
{
// Arrange
(Article article, Person author, _, Person reviewer, _) = GetAuthorChainInstances();
IReadOnlyCollection<RelationshipAttribute> authorChain = GetIncludedRelationshipsChain("author.blogs.reviewer.favoriteFood");
IncludedResourceObjectBuilder builder = GetBuilder();
// Act
builder.IncludeRelationshipChain(authorChain, article);
IList<ResourceObject> result = builder.Build();
// Assert
Assert.Equal(6, result.Count);
ResourceObject authorResourceObject = result.Single(ro => ro.Type == "people" && ro.Id == author.StringId);
ResourceIdentifierObject authorFoodRelation = authorResourceObject.Relationships["favoriteFood"].Data.SingleValue;
Assert.Equal(author.FavoriteFood.StringId, authorFoodRelation.Id);
ResourceObject reviewerResourceObject = result.Single(ro => ro.Type == "people" && ro.Id == reviewer.StringId);
ResourceIdentifierObject reviewerFoodRelation = reviewerResourceObject.Relationships["favoriteFood"].Data.SingleValue;
Assert.Equal(reviewer.FavoriteFood.StringId, reviewerFoodRelation.Id);
}
[Fact]
public void BuildIncluded_DeeplyNestedCircularChainOfManyData_BuildsWithoutDuplicates()
{
// Arrange
(Article article, Person author, _, _, _) = GetAuthorChainInstances();
Article secondArticle = ArticleFaker.Generate();
secondArticle.Author = author;
IncludedResourceObjectBuilder builder = GetBuilder();
// Act
IReadOnlyCollection<RelationshipAttribute> authorChain = GetIncludedRelationshipsChain("author.blogs.reviewer.favoriteFood");
builder.IncludeRelationshipChain(authorChain, article);
builder.IncludeRelationshipChain(authorChain, secondArticle);
// Assert
IList<ResourceObject> result = builder.Build();
Assert.Equal(6, result.Count);
}
[Fact]
public void BuildIncluded_OverlappingDeeplyNestedCircularChains_CanBuild()
{
// Arrange
IReadOnlyCollection<RelationshipAttribute> authorChain = GetIncludedRelationshipsChain("author.blogs.reviewer.favoriteFood");
(Article article, Person author, _, Person reviewer, Food reviewerFood) = GetAuthorChainInstances();
Blog sharedBlog = author.Blogs.First();
Person sharedBlogAuthor = reviewer;
Song authorSong = GetReviewerChainInstances(article, sharedBlog, sharedBlogAuthor);
IReadOnlyCollection<RelationshipAttribute> reviewerChain = GetIncludedRelationshipsChain("reviewer.blogs.author.favoriteSong");
IncludedResourceObjectBuilder builder = GetBuilder();
// Act
builder.IncludeRelationshipChain(authorChain, article);
builder.IncludeRelationshipChain(reviewerChain, article);
IList<ResourceObject> result = builder.Build();
// Assert
Assert.Equal(10, result.Count);
ResourceObject overlappingBlogResourceObject = result.Single(ro => ro.Type == "blogs" && ro.Id == sharedBlog.StringId);
Assert.Equal(2, overlappingBlogResourceObject.Relationships.Keys.Count);
List<ResourceObject> nonOverlappingBlogs = result.Where(ro => ro.Type == "blogs" && ro.Id != sharedBlog.StringId).ToList();
foreach (ResourceObject blog in nonOverlappingBlogs)
{
Assert.Single(blog.Relationships.Keys);
}
Assert.Equal(authorSong.StringId, sharedBlogAuthor.FavoriteSong.StringId);
Assert.Equal(reviewerFood.StringId, sharedBlogAuthor.FavoriteFood.StringId);
}
[Fact]
public void BuildIncluded_DuplicateChildrenMultipleChains_OnceInOutput()
{
Person person = PersonFaker.Generate();
List<Article> articles = ArticleFaker.Generate(5);
articles.ForEach(article => article.Author = person);
articles.ForEach(article => article.Reviewer = person);
IncludedResourceObjectBuilder builder = GetBuilder();
IReadOnlyCollection<RelationshipAttribute> authorChain = GetIncludedRelationshipsChain("author");
IReadOnlyCollection<RelationshipAttribute> reviewerChain = GetIncludedRelationshipsChain("reviewer");
foreach (Article article in articles)
{
builder.IncludeRelationshipChain(authorChain, article);
builder.IncludeRelationshipChain(reviewerChain, article);
}
IList<ResourceObject> result = builder.Build();
Assert.Single(result);
Assert.Equal(person.Name, result[0].Attributes["name"]);
Assert.Equal(person.StringId, result[0].Id);
}
private Song GetReviewerChainInstances(Article article, Blog sharedBlog, Person sharedBlogAuthor)
{
Person reviewer = PersonFaker.Generate();
article.Reviewer = reviewer;
List<Blog> blogs = BlogFaker.Generate(1);
blogs.Add(sharedBlog);
reviewer.Blogs = blogs.ToHashSet();
blogs[0].Author = reviewer;
Person author = PersonFaker.Generate();
blogs[1].Author = sharedBlogAuthor;
Song authorSong = SongFaker.Generate();
author.FavoriteSong = authorSong;
sharedBlogAuthor.FavoriteSong = authorSong;
Song reviewerSong = SongFaker.Generate();
reviewer.FavoriteSong = reviewerSong;
return authorSong;
}
private AuthorChainInstances GetAuthorChainInstances()
{
Article article = ArticleFaker.Generate();
Person author = PersonFaker.Generate();
article.Author = author;
List<Blog> blogs = BlogFaker.Generate(2);
author.Blogs = blogs.ToHashSet();
blogs[0].Reviewer = author;
Person reviewer = PersonFaker.Generate();
blogs[1].Reviewer = reviewer;
Food authorFood = FoodFaker.Generate();
author.FavoriteFood = authorFood;
Food reviewerFood = FoodFaker.Generate();
reviewer.FavoriteFood = reviewerFood;
return new AuthorChainInstances(article, author, authorFood, reviewer, reviewerFood);
}
private IReadOnlyCollection<RelationshipAttribute> GetIncludedRelationshipsChain(string chain)
{
var parsedChain = new List<RelationshipAttribute>();
ResourceContext resourceContext = ResourceGraph.GetResourceContext<Article>();
string[] splitPath = chain.Split('.');
foreach (string requestedRelationship in splitPath)
{
RelationshipAttribute relationship = resourceContext.GetRelationshipByPublicName(requestedRelationship);
parsedChain.Add(relationship);
resourceContext = ResourceGraph.GetResourceContext(relationship.RightType);
}
return parsedChain;
}
private IncludedResourceObjectBuilder GetBuilder()
{
IFieldsToSerialize fields = GetSerializableFields();
ILinkBuilder links = GetLinkBuilder();
IResourceDefinitionAccessor resourceDefinitionAccessor = new Mock<IResourceDefinitionAccessor>().Object;
var queryStringAccessor = new FakeRequestQueryStringAccessor();
var options = new JsonApiOptions();
return new IncludedResourceObjectBuilder(fields, links, ResourceGraph, Enumerable.Empty<IQueryConstraintProvider>(), resourceDefinitionAccessor,
queryStringAccessor, options);
}
private sealed class AuthorChainInstances
{
public Article Article { get; }
public Person Author { get; }
public Food AuthorFood { get; }
public Person Reviewer { get; }
public Food ReviewerFood { get; }
public AuthorChainInstances(Article article, Person author, Food authorFood, Person reviewer, Food reviewerFood)
{
Article = article;
Author = author;
AuthorFood = authorFood;
Reviewer = reviewer;
ReviewerFood = reviewerFood;
}
public void Deconstruct(out Article article, out Person author, out Food authorFood, out Person reviewer, out Food reviewerFood)
{
article = Article;
author = Author;
authorFood = AuthorFood;
reviewer = Reviewer;
reviewerFood = ReviewerFood;
}
}
}
}