Skip to content

require registration of resource types #80

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 26, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions JSONAPI.EntityFramework.Tests.TestWebApp/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ private static HttpConfiguration GetWebApiConfiguration()

var pluralizationService = new PluralizationService();
var modelManager = new ModelManager(pluralizationService);
modelManager.RegisterResourceType(typeof(Comment));
modelManager.RegisterResourceType(typeof(Post));
modelManager.RegisterResourceType(typeof(Tag));
modelManager.RegisterResourceType(typeof(User));
modelManager.RegisterResourceType(typeof(UserGroup));

var formatter = new JsonApiFormatter(modelManager);
config.Formatters.Clear();
Expand Down
10 changes: 8 additions & 2 deletions JSONAPI.EntityFramework.Tests/EntityConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ public void SetupEntities()
public void SerializeTest()
{
// Arrange
JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter(new JSONAPI.Core.PluralizationService());
var modelManager = new ModelManager(new Core.PluralizationService());
modelManager.RegisterResourceType(typeof(Post));

JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter(modelManager);
MemoryStream stream = new MemoryStream();

// Act
Expand All @@ -123,7 +126,10 @@ public void SerializeTest()
public async Task UnderpostingTest()
{
// Arrange
JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter(new JSONAPI.Core.PluralizationService());
var modelManager = new ModelManager(new Core.PluralizationService());
modelManager.RegisterResourceType(typeof(Post));

JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter(modelManager);
MemoryStream stream = new MemoryStream();

EntityFrameworkMaterializer materializer = new EntityFrameworkMaterializer(context, MetadataManager.Instance);
Expand Down
1 change: 1 addition & 0 deletions JSONAPI.Tests/ActionFilters/EnableSortingAttributeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ private HttpResponseMessage GetActionFilterResponse(string uri)
{
{ "Dummy", "Dummies" }
}));
modelManager.RegisterResourceType(typeof(Dummy));

var filter = new EnableSortingAttribute(modelManager);

Expand Down
4 changes: 3 additions & 1 deletion JSONAPI.Tests/Core/MetadataManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ public void PropertyWasPresentTest()
using (var inputStream = File.OpenRead("MetadataManagerPropertyWasPresentRequest.json"))
{
// Arrange
JsonApiFormatter formatter = new JsonApiFormatter(new PluralizationService());
var modelManager = new ModelManager(new PluralizationService());
modelManager.RegisterResourceType(typeof(Post));
JsonApiFormatter formatter = new JsonApiFormatter(modelManager);

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

Expand Down
95 changes: 89 additions & 6 deletions JSONAPI.Tests/Core/ModelManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Reflection;
using System.Collections.Generic;
using System.Collections;
using FluentAssertions;

namespace JSONAPI.Tests.Core
{
Expand All @@ -24,6 +25,11 @@ private class CustomIdModel
public string Data { get; set; }
}

private class DerivedPost : Post
{

}

[TestMethod]
public void FindsIdNamedId()
{
Expand Down Expand Up @@ -64,18 +70,22 @@ public void FindsIdFromAttribute()
}

[TestMethod]
public void GetJsonKeyForTypeTest()
public void GetResourceTypeName_returns_correct_value_for_registered_types()
{
// Arrange
var pluralizationService = new PluralizationService();
var mm = new ModelManager(pluralizationService);
mm.RegisterResourceType(typeof(Post));
mm.RegisterResourceType(typeof(Author));
mm.RegisterResourceType(typeof(Comment));
mm.RegisterResourceType(typeof(UserGroup));

// Act
var postKey = mm.GetJsonKeyForType(typeof(Post));
var authorKey = mm.GetJsonKeyForType(typeof(Author));
var commentKey = mm.GetJsonKeyForType(typeof(Comment));
var manyCommentKey = mm.GetJsonKeyForType(typeof(Comment[]));
var userGroupsKey = mm.GetJsonKeyForType(typeof(UserGroup));
var postKey = mm.GetResourceTypeNameForType(typeof(Post));
var authorKey = mm.GetResourceTypeNameForType(typeof(Author));
var commentKey = mm.GetResourceTypeNameForType(typeof(Comment));
var manyCommentKey = mm.GetResourceTypeNameForType(typeof(Comment[]));
var userGroupsKey = mm.GetResourceTypeNameForType(typeof(UserGroup));

// Assert
Assert.AreEqual("posts", postKey);
Expand All @@ -85,6 +95,79 @@ public void GetJsonKeyForTypeTest()
Assert.AreEqual("user-groups", userGroupsKey);
}

[TestMethod]
public void GetResourceTypeNameForType_gets_name_for_closest_registered_base_type_for_unregistered_type()
{
// Arrange
var pluralizationService = new PluralizationService();
var mm = new ModelManager(pluralizationService);
mm.RegisterResourceType(typeof(Post));

// Act
var resourceTypeName = mm.GetResourceTypeNameForType(typeof(DerivedPost));

// Assert
resourceTypeName.Should().Be("posts");
}

[TestMethod]
public void GetResourceTypeNameForType_fails_when_getting_unregistered_type()
{
// Arrange
var pluralizationService = new PluralizationService();
var mm = new ModelManager(pluralizationService);

// Act
Action action = () =>
{
mm.GetResourceTypeNameForType(typeof(Post));
};

// Assert
action.ShouldThrow<InvalidOperationException>().WithMessage("The type `JSONAPI.Tests.Models.Post` was not registered.");
}

[TestMethod]
public void GetTypeByResourceTypeName_returns_correct_value_for_registered_names()
{
// Arrange
var pluralizationService = new PluralizationService();
var mm = new ModelManager(pluralizationService);
mm.RegisterResourceType(typeof(Post));
mm.RegisterResourceType(typeof(Author));
mm.RegisterResourceType(typeof(Comment));
mm.RegisterResourceType(typeof(UserGroup));

// Act
var postType = mm.GetTypeByResourceTypeName("posts");
var authorType = mm.GetTypeByResourceTypeName("authors");
var commentType = mm.GetTypeByResourceTypeName("comments");
var userGroupType = mm.GetTypeByResourceTypeName("user-groups");

// Assert
postType.Should().Be(typeof (Post));
authorType.Should().Be(typeof (Author));
commentType.Should().Be(typeof (Comment));
userGroupType.Should().Be(typeof (UserGroup));
}

[TestMethod]
public void GetTypeByResourceTypeName_fails_when_getting_unregistered_name()
{
// Arrange
var pluralizationService = new PluralizationService();
var mm = new ModelManager(pluralizationService);

// Act
Action action = () =>
{
mm.GetTypeByResourceTypeName("posts");
};

// Assert
action.ShouldThrow<InvalidOperationException>().WithMessage("The resource type name `posts` was not registered.");
}

[TestMethod]
public void GetJsonKeyForPropertyTest()
{
Expand Down
85 changes: 56 additions & 29 deletions JSONAPI.Tests/Json/JsonApiMediaFormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,16 +205,14 @@ public void SetupModels()
public void SerializerIntegrationTest()
{
// Arrange
//PayloadConverter pc = new PayloadConverter();
//ModelConverter mc = new ModelConverter();
//ContractResolver.PluralizationService = new PluralizationService();

JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter(new JSONAPI.Core.PluralizationService());
var modelManager = new ModelManager(new PluralizationService());
modelManager.RegisterResourceType(typeof(Author));
modelManager.RegisterResourceType(typeof(Comment));
modelManager.RegisterResourceType(typeof(Post));
var formatter = new JsonApiFormatter(modelManager);
MemoryStream stream = new MemoryStream();

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

// Assert
Expand All @@ -230,16 +228,14 @@ public void SerializerIntegrationTest()
public void SerializeArrayIntegrationTest()
{
// Arrange
//PayloadConverter pc = new PayloadConverter();
//ModelConverter mc = new ModelConverter();
//ContractResolver.PluralizationService = new PluralizationService();

JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter(new JSONAPI.Core.PluralizationService());
var modelManager = new ModelManager(new PluralizationService());
modelManager.RegisterResourceType(typeof(Author));
modelManager.RegisterResourceType(typeof(Comment));
modelManager.RegisterResourceType(typeof(Post));
var formatter = new JsonApiFormatter(modelManager);
MemoryStream stream = new MemoryStream();

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

// Assert
Expand All @@ -255,7 +251,9 @@ public void SerializeArrayIntegrationTest()
public void Serializes_attributes_properly()
{
// Arrang
JsonApiFormatter formatter = new JsonApiFormatter(new PluralizationService());
var modelManager = new ModelManager(new PluralizationService());
modelManager.RegisterResourceType(typeof(Sample));
var formatter = new JsonApiFormatter(modelManager);
MemoryStream stream = new MemoryStream();

// Act
Expand All @@ -273,7 +271,9 @@ public void Serializes_attributes_properly()
public void Serializes_byte_ids_properly()
{
// Arrang
JsonApiFormatter formatter = new JsonApiFormatter(new PluralizationService());
var modelManager = new ModelManager(new PluralizationService());
modelManager.RegisterResourceType(typeof(Tag));
var formatter = new JsonApiFormatter(modelManager);
MemoryStream stream = new MemoryStream();

// Act
Expand All @@ -291,7 +291,9 @@ public void Serializes_byte_ids_properly()
public void Reformats_raw_json_string_with_unquoted_keys()
{
// Arrange
JsonApiFormatter formatter = new JsonApiFormatter(new PluralizationService());
var modelManager = new ModelManager(new PluralizationService());
modelManager.RegisterResourceType(typeof(Comment));
var formatter = new JsonApiFormatter(modelManager);
MemoryStream stream = new MemoryStream();

// Act
Expand All @@ -310,7 +312,9 @@ public void Reformats_raw_json_string_with_unquoted_keys()
public void Does_not_serialize_malformed_raw_json_string()
{
// Arrange
JsonApiFormatter formatter = new JsonApiFormatter(new PluralizationService());
var modelManager = new ModelManager(new PluralizationService());
modelManager.RegisterResourceType(typeof(Comment));
var formatter = new JsonApiFormatter(modelManager);
MemoryStream stream = new MemoryStream();

// Act
Expand All @@ -329,7 +333,8 @@ public void Does_not_serialize_malformed_raw_json_string()
public void Should_serialize_error()
{
// Arrange
var formatter = new JSONAPI.Json.JsonApiFormatter(new MockErrorSerializer());
var modelManager = new ModelManager(new PluralizationService());
var formatter = new JsonApiFormatter(modelManager, new MockErrorSerializer());
var stream = new MemoryStream();

// Act
Expand All @@ -348,7 +353,8 @@ public void Should_serialize_error()
public void SerializeErrorIntegrationTest()
{
// Arrange
JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter(new JSONAPI.Core.PluralizationService());
var modelManager = new ModelManager(new PluralizationService());
var formatter = new JsonApiFormatter(modelManager);
MemoryStream stream = new MemoryStream();

var mockInnerException = new Mock<Exception>(MockBehavior.Strict);
Expand Down Expand Up @@ -384,7 +390,9 @@ public void Deserializes_collections_properly()
using (var inputStream = File.OpenRead("DeserializeCollectionRequest.json"))
{
// Arrange
JsonApiFormatter formatter = new JsonApiFormatter(new PluralizationService());
var modelManager = new ModelManager(new PluralizationService());
modelManager.RegisterResourceType(typeof(Post));
var formatter = new JsonApiFormatter(modelManager);

// Act
var posts = (IList<Post>)formatter.ReadFromStreamAsync(typeof(Post), inputStream, null, null).Result;
Expand All @@ -407,7 +415,9 @@ public async Task Deserializes_attributes_properly()
using (var inputStream = File.OpenRead("DeserializeAttributeRequest.json"))
{
// Arrange
JsonApiFormatter formatter = new JsonApiFormatter(new PluralizationService());
var modelManager = new ModelManager(new PluralizationService());
modelManager.RegisterResourceType(typeof(Sample));
var formatter = new JsonApiFormatter(modelManager);

// Act
var deserialized = (IList<Sample>)await formatter.ReadFromStreamAsync(typeof(Sample), inputStream, null, null);
Expand All @@ -426,7 +436,9 @@ public async Task DeserializeRawJsonTest()
using (var inputStream = File.OpenRead("DeserializeRawJsonTest.json"))
{
// Arrange
var formatter = new JsonApiFormatter(new PluralizationService());
var modelManager = new ModelManager(new PluralizationService());
modelManager.RegisterResourceType(typeof(Comment));
var formatter = new JsonApiFormatter(modelManager);

// Act
var comments = ((IEnumerable<Comment>)await formatter.ReadFromStreamAsync(typeof (Comment), inputStream, null, null)).ToArray();
Expand All @@ -442,7 +454,10 @@ public async Task DeserializeRawJsonTest()
[TestMethod(), Timeout(1000)]
public void DeserializeExtraPropertyTest()
{
JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter(new JSONAPI.Core.PluralizationService());
// Arrange
var modelManager = new ModelManager(new PluralizationService());
modelManager.RegisterResourceType(typeof(Author));
var formatter = new JsonApiFormatter(modelManager);
MemoryStream stream = new MemoryStream();

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

stream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(@"{""authors"":{""id"":13,""name"":""Jason Hater"",""links"":{""posts"":{""type"": ""posts"",""ids"": []},""bogus"":[""PANIC!""]}}}"));
Expand All @@ -476,7 +494,10 @@ public void DeserializeExtraRelationshipTest()
[DeploymentItem(@"Data\NonStandardIdTest.json")]
public void SerializeNonStandardIdTest()
{
var formatter = new JSONAPI.Json.JsonApiFormatter(new PluralizationService());
// Arrange
var modelManager = new ModelManager(new PluralizationService());
modelManager.RegisterResourceType(typeof(NonStandardIdThing));
var formatter = new JsonApiFormatter(modelManager);
var stream = new MemoryStream();
var payload = new List<NonStandardIdThing> {
new NonStandardIdThing { Uuid = new Guid("0657fd6d-a4ab-43c4-84e5-0933c84b4f4f"), Data = "Swap" }
Expand All @@ -498,7 +519,9 @@ public void SerializeNonStandardIdTest()
[DeploymentItem(@"Data\NonStandardIdTest.json")]
public void DeserializeNonStandardIdTest()
{
var formatter = new JSONAPI.Json.JsonApiFormatter(new PluralizationService());
var modelManager = new ModelManager(new PluralizationService());
modelManager.RegisterResourceType(typeof(NonStandardIdThing));
var formatter = new JsonApiFormatter(modelManager);
var stream = new FileStream("NonStandardIdTest.json",FileMode.Open);

// Act
Expand All @@ -515,7 +538,9 @@ public void DeserializeNonStandardIdTest()
[DeploymentItem(@"Data\NonStandardIdTest.json")]
public void DeserializeNonStandardIdWithIdOnly()
{
var formatter = new JSONAPI.Json.JsonApiFormatter(new PluralizationService());
var modelManager = new ModelManager(new PluralizationService());
modelManager.RegisterResourceType(typeof(NonStandardIdThing));
var formatter = new JsonApiFormatter(modelManager);
string json = File.ReadAllText("NonStandardIdTest.json");
json = Regex.Replace(json, @"""uuid"":\s*""0657fd6d-a4ab-43c4-84e5-0933c84b4f4f""\s*,",""); // remove the uuid attribute
var stream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(json));
Expand All @@ -534,7 +559,9 @@ public void DeserializeNonStandardIdWithIdOnly()
[DeploymentItem(@"Data\NonStandardIdTest.json")]
public void DeserializeNonStandardIdWithoutId()
{
var formatter = new JSONAPI.Json.JsonApiFormatter(new PluralizationService());
var modelManager = new ModelManager(new PluralizationService());
modelManager.RegisterResourceType(typeof(NonStandardIdThing));
var formatter = new JsonApiFormatter(modelManager);
string json = File.ReadAllText("NonStandardIdTest.json");
json = Regex.Replace(json, @"""id"":\s*""0657fd6d-a4ab-43c4-84e5-0933c84b4f4f""\s*,", ""); // remove the uuid attribute
var stream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(json));
Expand Down
Loading