Skip to content

Commit 42392cd

Browse files
author
Chris Santero
committed
require registration of resource types
1 parent 85d3a04 commit 42392cd

12 files changed

Lines changed: 229 additions & 68 deletions

File tree

JSONAPI.EntityFramework.Tests.TestWebApp/Startup.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ private static HttpConfiguration GetWebApiConfiguration()
6666

6767
var pluralizationService = new PluralizationService();
6868
var modelManager = new ModelManager(pluralizationService);
69+
modelManager.RegisterResourceType(typeof(Comment));
70+
modelManager.RegisterResourceType(typeof(Post));
71+
modelManager.RegisterResourceType(typeof(Tag));
72+
modelManager.RegisterResourceType(typeof(User));
73+
modelManager.RegisterResourceType(typeof(UserGroup));
6974

7075
var formatter = new JsonApiFormatter(modelManager);
7176
config.Formatters.Clear();

JSONAPI.EntityFramework.Tests/EntityConverterTests.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,10 @@ public void SetupEntities()
107107
public void SerializeTest()
108108
{
109109
// Arrange
110-
JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter(new JSONAPI.Core.PluralizationService());
110+
var modelManager = new ModelManager(new Core.PluralizationService());
111+
modelManager.RegisterResourceType(typeof(Post));
112+
113+
JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter(modelManager);
111114
MemoryStream stream = new MemoryStream();
112115

113116
// Act
@@ -123,7 +126,10 @@ public void SerializeTest()
123126
public async Task UnderpostingTest()
124127
{
125128
// Arrange
126-
JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter(new JSONAPI.Core.PluralizationService());
129+
var modelManager = new ModelManager(new Core.PluralizationService());
130+
modelManager.RegisterResourceType(typeof(Post));
131+
132+
JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter(modelManager);
127133
MemoryStream stream = new MemoryStream();
128134

129135
EntityFrameworkMaterializer materializer = new EntityFrameworkMaterializer(context, MetadataManager.Instance);

JSONAPI.Tests/ActionFilters/EnableSortingAttributeTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ private HttpResponseMessage GetActionFilterResponse(string uri)
7575
{
7676
{ "Dummy", "Dummies" }
7777
}));
78+
modelManager.RegisterResourceType(typeof(Dummy));
7879

7980
var filter = new EnableSortingAttribute(modelManager);
8081

JSONAPI.Tests/Core/MetadataManagerTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ public void PropertyWasPresentTest()
1616
using (var inputStream = File.OpenRead("MetadataManagerPropertyWasPresentRequest.json"))
1717
{
1818
// Arrange
19-
JsonApiFormatter formatter = new JsonApiFormatter(new PluralizationService());
19+
var modelManager = new ModelManager(new PluralizationService());
20+
modelManager.RegisterResourceType(typeof(Post));
21+
JsonApiFormatter formatter = new JsonApiFormatter(modelManager);
2022

2123
var p = (Post) formatter.ReadFromStreamAsync(typeof(Post), inputStream, null, null).Result;
2224

JSONAPI.Tests/Core/ModelManagerTests.cs

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Reflection;
66
using System.Collections.Generic;
77
using System.Collections;
8+
using FluentAssertions;
89

910
namespace JSONAPI.Tests.Core
1011
{
@@ -64,18 +65,22 @@ public void FindsIdFromAttribute()
6465
}
6566

6667
[TestMethod]
67-
public void GetJsonKeyForTypeTest()
68+
public void GetResourceTypeName_returns_correct_value_for_registered_types()
6869
{
6970
// Arrange
7071
var pluralizationService = new PluralizationService();
7172
var mm = new ModelManager(pluralizationService);
73+
mm.RegisterResourceType(typeof(Post));
74+
mm.RegisterResourceType(typeof(Author));
75+
mm.RegisterResourceType(typeof(Comment));
76+
mm.RegisterResourceType(typeof(UserGroup));
7277

7378
// Act
74-
var postKey = mm.GetJsonKeyForType(typeof(Post));
75-
var authorKey = mm.GetJsonKeyForType(typeof(Author));
76-
var commentKey = mm.GetJsonKeyForType(typeof(Comment));
77-
var manyCommentKey = mm.GetJsonKeyForType(typeof(Comment[]));
78-
var userGroupsKey = mm.GetJsonKeyForType(typeof(UserGroup));
79+
var postKey = mm.GetResourceTypeNameForType(typeof(Post));
80+
var authorKey = mm.GetResourceTypeNameForType(typeof(Author));
81+
var commentKey = mm.GetResourceTypeNameForType(typeof(Comment));
82+
var manyCommentKey = mm.GetResourceTypeNameForType(typeof(Comment[]));
83+
var userGroupsKey = mm.GetResourceTypeNameForType(typeof(UserGroup));
7984

8085
// Assert
8186
Assert.AreEqual("posts", postKey);
@@ -85,6 +90,64 @@ public void GetJsonKeyForTypeTest()
8590
Assert.AreEqual("user-groups", userGroupsKey);
8691
}
8792

93+
[TestMethod]
94+
public void GetResourceTypeNameForType_fails_when_getting_unregistered_type()
95+
{
96+
// Arrange
97+
var pluralizationService = new PluralizationService();
98+
var mm = new ModelManager(pluralizationService);
99+
100+
// Act
101+
Action action = () =>
102+
{
103+
mm.GetResourceTypeNameForType(typeof(Post));
104+
};
105+
106+
// Assert
107+
action.ShouldThrow<InvalidOperationException>().WithMessage("The type `JSONAPI.Tests.Models.Post` was not registered.");
108+
}
109+
110+
[TestMethod]
111+
public void GetTypeByResourceTypeName_returns_correct_value_for_registered_names()
112+
{
113+
// Arrange
114+
var pluralizationService = new PluralizationService();
115+
var mm = new ModelManager(pluralizationService);
116+
mm.RegisterResourceType(typeof(Post));
117+
mm.RegisterResourceType(typeof(Author));
118+
mm.RegisterResourceType(typeof(Comment));
119+
mm.RegisterResourceType(typeof(UserGroup));
120+
121+
// Act
122+
var postType = mm.GetTypeByResourceTypeName("posts");
123+
var authorType = mm.GetTypeByResourceTypeName("authors");
124+
var commentType = mm.GetTypeByResourceTypeName("comments");
125+
var userGroupType = mm.GetTypeByResourceTypeName("user-groups");
126+
127+
// Assert
128+
postType.Should().Be(typeof (Post));
129+
authorType.Should().Be(typeof (Author));
130+
commentType.Should().Be(typeof (Comment));
131+
userGroupType.Should().Be(typeof (UserGroup));
132+
}
133+
134+
[TestMethod]
135+
public void GetTypeByResourceTypeName_fails_when_getting_unregistered_name()
136+
{
137+
// Arrange
138+
var pluralizationService = new PluralizationService();
139+
var mm = new ModelManager(pluralizationService);
140+
141+
// Act
142+
Action action = () =>
143+
{
144+
mm.GetTypeByResourceTypeName("posts");
145+
};
146+
147+
// Assert
148+
action.ShouldThrow<InvalidOperationException>().WithMessage("The resource type name `posts` was not registered.");
149+
}
150+
88151
[TestMethod]
89152
public void GetJsonKeyForPropertyTest()
90153
{

JSONAPI.Tests/Json/JsonApiMediaFormatterTests.cs

Lines changed: 56 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -205,16 +205,14 @@ public void SetupModels()
205205
public void SerializerIntegrationTest()
206206
{
207207
// Arrange
208-
//PayloadConverter pc = new PayloadConverter();
209-
//ModelConverter mc = new ModelConverter();
210-
//ContractResolver.PluralizationService = new PluralizationService();
211-
212-
JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter(new JSONAPI.Core.PluralizationService());
208+
var modelManager = new ModelManager(new PluralizationService());
209+
modelManager.RegisterResourceType(typeof(Author));
210+
modelManager.RegisterResourceType(typeof(Comment));
211+
modelManager.RegisterResourceType(typeof(Post));
212+
var formatter = new JsonApiFormatter(modelManager);
213213
MemoryStream stream = new MemoryStream();
214214

215215
// Act
216-
//Payload payload = new Payload(a.Posts);
217-
//js.Serialize(jw, payload);
218216
formatter.WriteToStreamAsync(typeof(Post), new[] { p, p2, p3, p4 }.ToList(), stream, (System.Net.Http.HttpContent)null, (System.Net.TransportContext)null);
219217

220218
// Assert
@@ -230,16 +228,14 @@ public void SerializerIntegrationTest()
230228
public void SerializeArrayIntegrationTest()
231229
{
232230
// Arrange
233-
//PayloadConverter pc = new PayloadConverter();
234-
//ModelConverter mc = new ModelConverter();
235-
//ContractResolver.PluralizationService = new PluralizationService();
236-
237-
JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter(new JSONAPI.Core.PluralizationService());
231+
var modelManager = new ModelManager(new PluralizationService());
232+
modelManager.RegisterResourceType(typeof(Author));
233+
modelManager.RegisterResourceType(typeof(Comment));
234+
modelManager.RegisterResourceType(typeof(Post));
235+
var formatter = new JsonApiFormatter(modelManager);
238236
MemoryStream stream = new MemoryStream();
239237

240238
// Act
241-
//Payload payload = new Payload(a.Posts);
242-
//js.Serialize(jw, payload);
243239
formatter.WriteToStreamAsync(typeof(Post), new[] { p, p2, p3, p4 }, stream, (System.Net.Http.HttpContent)null, (System.Net.TransportContext)null);
244240

245241
// Assert
@@ -255,7 +251,9 @@ public void SerializeArrayIntegrationTest()
255251
public void Serializes_attributes_properly()
256252
{
257253
// Arrang
258-
JsonApiFormatter formatter = new JsonApiFormatter(new PluralizationService());
254+
var modelManager = new ModelManager(new PluralizationService());
255+
modelManager.RegisterResourceType(typeof(Sample));
256+
var formatter = new JsonApiFormatter(modelManager);
259257
MemoryStream stream = new MemoryStream();
260258

261259
// Act
@@ -273,7 +271,9 @@ public void Serializes_attributes_properly()
273271
public void Serializes_byte_ids_properly()
274272
{
275273
// Arrang
276-
JsonApiFormatter formatter = new JsonApiFormatter(new PluralizationService());
274+
var modelManager = new ModelManager(new PluralizationService());
275+
modelManager.RegisterResourceType(typeof(Tag));
276+
var formatter = new JsonApiFormatter(modelManager);
277277
MemoryStream stream = new MemoryStream();
278278

279279
// Act
@@ -291,7 +291,9 @@ public void Serializes_byte_ids_properly()
291291
public void Reformats_raw_json_string_with_unquoted_keys()
292292
{
293293
// Arrange
294-
JsonApiFormatter formatter = new JsonApiFormatter(new PluralizationService());
294+
var modelManager = new ModelManager(new PluralizationService());
295+
modelManager.RegisterResourceType(typeof(Comment));
296+
var formatter = new JsonApiFormatter(modelManager);
295297
MemoryStream stream = new MemoryStream();
296298

297299
// Act
@@ -310,7 +312,9 @@ public void Reformats_raw_json_string_with_unquoted_keys()
310312
public void Does_not_serialize_malformed_raw_json_string()
311313
{
312314
// Arrange
313-
JsonApiFormatter formatter = new JsonApiFormatter(new PluralizationService());
315+
var modelManager = new ModelManager(new PluralizationService());
316+
modelManager.RegisterResourceType(typeof(Comment));
317+
var formatter = new JsonApiFormatter(modelManager);
314318
MemoryStream stream = new MemoryStream();
315319

316320
// Act
@@ -329,7 +333,8 @@ public void Does_not_serialize_malformed_raw_json_string()
329333
public void Should_serialize_error()
330334
{
331335
// Arrange
332-
var formatter = new JSONAPI.Json.JsonApiFormatter(new MockErrorSerializer());
336+
var modelManager = new ModelManager(new PluralizationService());
337+
var formatter = new JsonApiFormatter(modelManager, new MockErrorSerializer());
333338
var stream = new MemoryStream();
334339

335340
// Act
@@ -348,7 +353,8 @@ public void Should_serialize_error()
348353
public void SerializeErrorIntegrationTest()
349354
{
350355
// Arrange
351-
JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter(new JSONAPI.Core.PluralizationService());
356+
var modelManager = new ModelManager(new PluralizationService());
357+
var formatter = new JsonApiFormatter(modelManager);
352358
MemoryStream stream = new MemoryStream();
353359

354360
var mockInnerException = new Mock<Exception>(MockBehavior.Strict);
@@ -384,7 +390,9 @@ public void Deserializes_collections_properly()
384390
using (var inputStream = File.OpenRead("DeserializeCollectionRequest.json"))
385391
{
386392
// Arrange
387-
JsonApiFormatter formatter = new JsonApiFormatter(new PluralizationService());
393+
var modelManager = new ModelManager(new PluralizationService());
394+
modelManager.RegisterResourceType(typeof(Post));
395+
var formatter = new JsonApiFormatter(modelManager);
388396

389397
// Act
390398
var posts = (IList<Post>)formatter.ReadFromStreamAsync(typeof(Post), inputStream, null, null).Result;
@@ -407,7 +415,9 @@ public async Task Deserializes_attributes_properly()
407415
using (var inputStream = File.OpenRead("DeserializeAttributeRequest.json"))
408416
{
409417
// Arrange
410-
JsonApiFormatter formatter = new JsonApiFormatter(new PluralizationService());
418+
var modelManager = new ModelManager(new PluralizationService());
419+
modelManager.RegisterResourceType(typeof(Sample));
420+
var formatter = new JsonApiFormatter(modelManager);
411421

412422
// Act
413423
var deserialized = (IList<Sample>)await formatter.ReadFromStreamAsync(typeof(Sample), inputStream, null, null);
@@ -426,7 +436,9 @@ public async Task DeserializeRawJsonTest()
426436
using (var inputStream = File.OpenRead("DeserializeRawJsonTest.json"))
427437
{
428438
// Arrange
429-
var formatter = new JsonApiFormatter(new PluralizationService());
439+
var modelManager = new ModelManager(new PluralizationService());
440+
modelManager.RegisterResourceType(typeof(Comment));
441+
var formatter = new JsonApiFormatter(modelManager);
430442

431443
// Act
432444
var comments = ((IEnumerable<Comment>)await formatter.ReadFromStreamAsync(typeof (Comment), inputStream, null, null)).ToArray();
@@ -442,7 +454,10 @@ public async Task DeserializeRawJsonTest()
442454
[TestMethod(), Timeout(1000)]
443455
public void DeserializeExtraPropertyTest()
444456
{
445-
JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter(new JSONAPI.Core.PluralizationService());
457+
// Arrange
458+
var modelManager = new ModelManager(new PluralizationService());
459+
modelManager.RegisterResourceType(typeof(Author));
460+
var formatter = new JsonApiFormatter(modelManager);
446461
MemoryStream stream = new MemoryStream();
447462

448463
stream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(@"{""authors"":{""id"":13,""name"":""Jason Hater"",""bogus"":""PANIC!"",""links"":{""posts"":{""type"": ""posts"",""ids"": []}}}"));
@@ -459,7 +474,10 @@ public void DeserializeExtraPropertyTest()
459474
[TestMethod(), Timeout(1000)]
460475
public void DeserializeExtraRelationshipTest()
461476
{
462-
JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter(new JSONAPI.Core.PluralizationService());
477+
// Arrange
478+
var modelManager = new ModelManager(new PluralizationService());
479+
modelManager.RegisterResourceType(typeof(Author));
480+
var formatter = new JsonApiFormatter(modelManager);
463481
MemoryStream stream = new MemoryStream();
464482

465483
stream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(@"{""authors"":{""id"":13,""name"":""Jason Hater"",""links"":{""posts"":{""type"": ""posts"",""ids"": []},""bogus"":[""PANIC!""]}}}"));
@@ -476,7 +494,10 @@ public void DeserializeExtraRelationshipTest()
476494
[DeploymentItem(@"Data\NonStandardIdTest.json")]
477495
public void SerializeNonStandardIdTest()
478496
{
479-
var formatter = new JSONAPI.Json.JsonApiFormatter(new PluralizationService());
497+
// Arrange
498+
var modelManager = new ModelManager(new PluralizationService());
499+
modelManager.RegisterResourceType(typeof(NonStandardIdThing));
500+
var formatter = new JsonApiFormatter(modelManager);
480501
var stream = new MemoryStream();
481502
var payload = new List<NonStandardIdThing> {
482503
new NonStandardIdThing { Uuid = new Guid("0657fd6d-a4ab-43c4-84e5-0933c84b4f4f"), Data = "Swap" }
@@ -498,7 +519,9 @@ public void SerializeNonStandardIdTest()
498519
[DeploymentItem(@"Data\NonStandardIdTest.json")]
499520
public void DeserializeNonStandardIdTest()
500521
{
501-
var formatter = new JSONAPI.Json.JsonApiFormatter(new PluralizationService());
522+
var modelManager = new ModelManager(new PluralizationService());
523+
modelManager.RegisterResourceType(typeof(NonStandardIdThing));
524+
var formatter = new JsonApiFormatter(modelManager);
502525
var stream = new FileStream("NonStandardIdTest.json",FileMode.Open);
503526

504527
// Act
@@ -515,7 +538,9 @@ public void DeserializeNonStandardIdTest()
515538
[DeploymentItem(@"Data\NonStandardIdTest.json")]
516539
public void DeserializeNonStandardIdWithIdOnly()
517540
{
518-
var formatter = new JSONAPI.Json.JsonApiFormatter(new PluralizationService());
541+
var modelManager = new ModelManager(new PluralizationService());
542+
modelManager.RegisterResourceType(typeof(NonStandardIdThing));
543+
var formatter = new JsonApiFormatter(modelManager);
519544
string json = File.ReadAllText("NonStandardIdTest.json");
520545
json = Regex.Replace(json, @"""uuid"":\s*""0657fd6d-a4ab-43c4-84e5-0933c84b4f4f""\s*,",""); // remove the uuid attribute
521546
var stream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(json));
@@ -534,7 +559,9 @@ public void DeserializeNonStandardIdWithIdOnly()
534559
[DeploymentItem(@"Data\NonStandardIdTest.json")]
535560
public void DeserializeNonStandardIdWithoutId()
536561
{
537-
var formatter = new JSONAPI.Json.JsonApiFormatter(new PluralizationService());
562+
var modelManager = new ModelManager(new PluralizationService());
563+
modelManager.RegisterResourceType(typeof(NonStandardIdThing));
564+
var formatter = new JsonApiFormatter(modelManager);
538565
string json = File.ReadAllText("NonStandardIdTest.json");
539566
json = Regex.Replace(json, @"""id"":\s*""0657fd6d-a4ab-43c4-84e5-0933c84b4f4f""\s*,", ""); // remove the uuid attribute
540567
var stream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(json));

JSONAPI.Tests/Json/LinkTemplateTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Diagnostics;
55
using System.IO;
66
using System.Text;
7+
using JSONAPI.Core;
78

89
namespace JSONAPI.Tests.Json
910
{
@@ -49,10 +50,9 @@ public void SetupModels()
4950
[DeploymentItem(@"Data\LinkTemplateTest.json")]
5051
public void GetResourceWithLinkTemplateRelationship()
5152
{
52-
var formatter = new JsonApiFormatter
53-
(
54-
new JSONAPI.Core.PluralizationService()
55-
);
53+
var modelManager = new ModelManager(new PluralizationService());
54+
modelManager.RegisterResourceType(typeof(Post));
55+
var formatter = new JsonApiFormatter(modelManager);
5656
var stream = new MemoryStream();
5757

5858
formatter.WriteToStreamAsync(typeof(Post), ThePost, stream, null, null);

0 commit comments

Comments
 (0)