private DefaultSortingTransformer GetTransformer() { var pluralizationService = new PluralizationService(new Dictionary<string, string> { {"Dummy", "Dummies"} }); var modelManager = new ModelManager(pluralizationService); modelManager.RegisterResourceType(typeof(Dummy)); return new DefaultSortingTransformer(modelManager); }
private static HttpConfiguration GetWebApiConfiguration() { var pluralizationService = new PluralizationService(); pluralizationService.AddMapping("todo", "todos"); var modelManager = new ModelManager(pluralizationService); modelManager.RegisterResourceType(typeof (Todo)); var httpConfig = new HttpConfiguration(); // Configure JSON API new JsonApiConfiguration(modelManager) .UsingDefaultQueryablePayloadBuilder(c => c.EnumerateQueriesAsynchronously()) .Apply(httpConfig); // Web API routes httpConfig.Routes.MapHttpRoute("DefaultApi", "{controller}/{id}", new { id = RouteParameter.Optional }); return httpConfig; }
public string GetJsonKeyForType(Type type) { string key = null; var keyCache = _jsonKeysForType.Value; lock (keyCache) { if (IsSerializedAsMany(type)) { type = GetElementType(type); } if (keyCache.TryGetValue(type, out key)) { return(key); } var attrs = type.CustomAttributes.Where(x => x.AttributeType == typeof(Newtonsoft.Json.JsonObjectAttribute)).ToList(); string title = type.Name; if (attrs.Any()) { var titles = attrs.First().NamedArguments.Where(arg => arg.MemberName == "Title") .Select(arg => arg.TypedValue.Value.ToString()).ToList(); if (titles.Any()) { title = titles.First(); } } key = FormatPropertyName(PluralizationService.Pluralize(title)); keyCache.Add(type, key); } return(key); }
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.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); Assert.AreEqual("authors", authorKey); Assert.AreEqual("comments", commentKey); Assert.AreEqual("comments", manyCommentKey); Assert.AreEqual("user-groups", userGroupsKey); }
public void Cant_register_type_with_two_properties_with_the_same_name() { var pluralizationService = new PluralizationService(); var mm = new ModelManager(pluralizationService); Type saladType = typeof(Salad); // Act Action action = () => mm.RegisterResourceType(saladType); // Assert action.ShouldThrow<InvalidOperationException>().Which.Message.Should().Be("The type `salads` already contains a property keyed at `salad-type`."); }
public void GetPropertyForJsonKey_returns_correct_value_for_JsonProperty_attribute() { // Arrange var pluralizationService = new PluralizationService(); var mm = new ModelManager(pluralizationService); Type bandType = typeof(Band); mm.RegisterResourceType(bandType); // Act var prop = mm.GetPropertyForJsonKey(bandType, "THE-GENRE"); // Assert prop.Property.Should().BeSameAs(bandType.GetProperty("Genre")); prop.Should().BeOfType<FieldModelProperty>(); }
public void GetPropertyForJsonKey_returns_correct_value_for_custom_id() { // Arrange var pluralizationService = new PluralizationService(); var mm = new ModelManager(pluralizationService); Type bandType = typeof(Band); mm.RegisterResourceType(bandType); // Act var idProp = mm.GetPropertyForJsonKey(bandType, "id"); // Assert idProp.Property.Should().BeSameAs(bandType.GetProperty("BandName")); idProp.Should().BeOfType<FieldModelProperty>(); }
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."); }
public void GetJsonKeyForPropertyTest() { // Arrange var pluralizationService = new PluralizationService(); var mm = new ModelManager(pluralizationService); // Act var idKey = mm.CalculateJsonKeyForProperty(typeof(Author).GetProperty("Id")); var nameKey = mm.CalculateJsonKeyForProperty(typeof(Author).GetProperty("Name")); var postsKey = mm.CalculateJsonKeyForProperty(typeof(Author).GetProperty("Posts")); // Assert Assert.AreEqual("id", idKey); Assert.AreEqual("name", nameKey); Assert.AreEqual("posts", postsKey); }
public void TypeIsRegistered_returns_false_for_collection_of_unregistered_types() { // Arrange var pluralizationService = new PluralizationService(); var mm = new ModelManager(pluralizationService); // Act var isRegistered = mm.TypeIsRegistered(typeof(ICollection<Comment>)); // Assert isRegistered.Should().BeFalse(); }
public void TypeIsRegistered_returns_false_if_no_type_in_hierarchy_is_registered() { // Arrange var pluralizationService = new PluralizationService(); var mm = new ModelManager(pluralizationService); // Act var isRegistered = mm.TypeIsRegistered(typeof(Comment)); // Assert isRegistered.Should().BeFalse(); }
public void TypeIsRegistered_returns_true_for_collection_of_children_of_registered_types() { // Arrange var pluralizationService = new PluralizationService(); var mm = new ModelManager(pluralizationService); mm.RegisterResourceType(typeof(Post)); // Act var isRegistered = mm.TypeIsRegistered(typeof(ICollection<DerivedPost>)); // Assert isRegistered.Should().BeTrue(); }
public void TypeIsRegistered_returns_true_if_parent_type_is_registered() { // Arrange var pluralizationService = new PluralizationService(); var mm = new ModelManager(pluralizationService); mm.RegisterResourceType(typeof(Post)); // Act var isRegistered = mm.TypeIsRegistered(typeof(DerivedPost)); // Assert isRegistered.Should().BeTrue(); }
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."); }
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)); }
private DefaultFilteringTransformer GetTransformer() { var pluralizationService = new PluralizationService(new Dictionary<string, string> { {"Dummy", "Dummies"} }); var registrar = new ResourceTypeRegistrar(new DefaultNamingConventions(pluralizationService)); var registry = new ResourceTypeRegistry(); registry.AddRegistration(registrar.BuildRegistration(typeof(Dummy))); registry.AddRegistration(registrar.BuildRegistration(typeof(RelatedItemWithId))); return new DefaultFilteringTransformer(registry); }
public void GetPropertyForJsonKeyTest() { // Arrange var pluralizationService = new PluralizationService(); var mm = new ModelManager(pluralizationService); Type authorType = typeof(Author); mm.RegisterResourceType(authorType); // Act var idProp = mm.GetPropertyForJsonKey(authorType, "id"); var nameProp = mm.GetPropertyForJsonKey(authorType, "name"); var postsProp = mm.GetPropertyForJsonKey(authorType, "posts"); // Assert idProp.Property.Should().BeSameAs(authorType.GetProperty("Id")); idProp.Should().BeOfType<FieldModelProperty>(); nameProp.Property.Should().BeSameAs(authorType.GetProperty("Name")); nameProp.Should().BeOfType<FieldModelProperty>(); postsProp.Property.Should().BeSameAs(authorType.GetProperty("Posts")); postsProp.Should().BeOfType<RelationshipModelProperty>(); }
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"); }