示例#1
0
        public void Transform_UpdateDocument_To_Delta_TwoFields()
        {
            // Arrange
            var updateDocument = new UpdateDocument
            {
                Data = new SingleResource()
                {
                    Id = "123",
                    Type = "post",
                    Attributes = new Dictionary<string, object>()
                    {
                        {"title", "someTitle" },
                        {"authorId", "1234" },
                    }
                }
            };

            var config = TestModelConfigurationBuilder.BuilderForEverything.Build();
            var context = new Context(new Uri("http://fakehost:1234", UriKind.Absolute));
            var transformer = new JsonApiTransformerBuilder().With(config).Build();


            // Act
            var resultDelta = transformer.TransformBack(updateDocument, typeof(Post), context);

            // Assert
            Assert.True(resultDelta.ObjectPropertyValues.ContainsKey("title"));
            Assert.True(resultDelta.ObjectPropertyValues.ContainsKey("authorId"));
        }
        public void Transform_UpdateDocument_To_Delta_OneField()
        {
            // Arrange
            var updateDocument = new UpdateDocument
            {
                Data = new Dictionary<string, object>
                {
                    {
                        "posts", JObject.FromObject(new PostUpdateOneField()
                        {
                            Title = "Food"
                        })
                    }
                }
            };

            var configuration = (new ConfigurationBuilder())
                .Resource<Post>()
                .WithSimpleProperty(x => x.AuthorId)
                .WithSimpleProperty(x => x.Id)
                .WithSimpleProperty(x => x.Title);
            var context = new Context { Configuration = configuration.ConfigurationBuilder.Build() };
            var sut = new JsonApiTransformer() { TransformationHelper = new TransformationHelper() };

            // Act
            var resultDelta = sut.TransformBack(updateDocument, typeof(Post), context);

            // Assert
            resultDelta.ObjectPropertyValues.ContainsKey("title").ShouldBeTrue();
        }
示例#3
0
        private void ValidateUpdateDocument(UpdateDocument updateDocument)
        {
            if (updateDocument == null)
            {
                throw new JsonException("Json body can not be empty or whitespace.");
            }

            if (updateDocument.Data == null)
            {
                throw new JsonException("Json body should contain some content.");
            }
        }
        public void Transform_properties_with_reserverd_keyword()
        {
            var updateDocument = new UpdateDocument()
            {
                Data = new Dictionary<string, object>()
                {
                    { "posts", JObject.Parse("{ \"_id\":123, \"title\": \"someTitle\" }") }
                }
            };

            var configuration = (new ConfigurationBuilder())
                .Resource<Post>()
                .WithSimpleProperty(x => x.AuthorId)
                .WithSimpleProperty(x => x.Id)
                .WithSimpleProperty(x => x.Title);
            var context = new Context { Configuration = configuration.ConfigurationBuilder.Build() };
            var sut = new JsonApiTransformer() { TransformationHelper = new TransformationHelper() };

            // Act
            var resultDelta = sut.TransformBack(updateDocument, typeof(Post), context);

            // Assert
            resultDelta.ObjectPropertyValues.ContainsKey("id").ShouldBeTrue();
        }
示例#5
0
        public IDelta TransformBack(UpdateDocument updateDocument, Type type, Context context)
        {
            var mapping           = configuration.GetMapping(type);
            var openGeneric       = typeof(Delta <>);
            var closedGenericType = openGeneric.MakeGenericType(type);
            var delta             = Activator.CreateInstance(closedGenericType, this.configuration) as IDelta;

            if (delta == null)
            {
                return(null);
            }

            delta.ObjectPropertyValues = mapping.GetValuesFromAttributes(updateDocument.Data.Attributes);

            if (updateDocument.Data.Relationships != null)
            {
                foreach (var relMapping in mapping.Relationships)
                {
                    var relatedTypeMapping = configuration.GetMapping(relMapping.RelatedBaseType);
                    if (!updateDocument.Data.Relationships.ContainsKey(relMapping.RelationshipName))
                    {
                        continue;
                    }
                    var relationship = updateDocument.Data.Relationships[relMapping.RelationshipName];

                    if (relMapping.IsCollection && relationship.Data is MultipleResourceIdentifiers)
                    {
                        var multipleIDs                 = (MultipleResourceIdentifiers)relationship.Data;
                        var openGenericCollection       = typeof(CollectionDelta <>);
                        var closedGenericTypeCollection = openGenericCollection.MakeGenericType(relMapping.RelatedBaseType);
                        var collection            = Activator.CreateInstance(closedGenericTypeCollection, relatedTypeMapping.IdGetter) as ICollectionDelta;
                        var openGenericList       = typeof(List <>);
                        var closedGenericTypeList = openGenericList.MakeGenericType(relMapping.RelatedBaseType);
                        collection.Elements = Activator.CreateInstance(closedGenericTypeList) as IList;
                        foreach (var id in multipleIDs)
                        {
                            var colProp = relMapping.RelatedProperty;

                            var newInstance = Activator.CreateInstance(relMapping.RelatedBaseType);
                            relatedTypeMapping.IdSetter(newInstance, id.Id);
                            (collection.Elements as IList).Add(newInstance);
                        }
                        delta.CollectionDeltas.Add(relMapping.RelationshipName, collection);
                    }
                    else if (!relMapping.IsCollection && relationship.Data is SingleResourceIdentifier)
                    {
                        var    singleId = relationship.Data as SingleResourceIdentifier;
                        object instance;
                        try
                        {
                            instance = Activator.CreateInstance(relMapping.RelatedBaseType);
                        }
                        catch (MissingMethodException ex)
                        {
                            throw new Exception("Could not create the resource with the link given, ensure that you have a parameterless constructor on the linked entity", ex);
                        }
                        relatedTypeMapping.IdSetter(instance, singleId.Id);
                        delta.ObjectPropertyValues.Add(relMapping.RelationshipName, instance);
                    }
                    else
                    {
                        throw new InvalidOperationException();
                    }
                }
            }

            if (updateDocument.MetaData?.Count > 0)
            {
                delta.TopLevelMetaData = updateDocument.MetaData;
            }
            if (updateDocument.Data.MetaData?.Count > 0)
            {
                delta.ObjectMetaData = updateDocument.Data.MetaData;
            }

            delta.Scan();

            return(delta);
        }
示例#6
0
 public UpdateDocumentTypeWrapper(UpdateDocument updateDocument, Type type)
 {
     UpdateDocument = updateDocument;
     Type           = type;
 }
        public IDelta TransformBack(UpdateDocument updateDocument, Type type, Context context)
        {
            var mapping           = context.Configuration.GetMapping(type);
            var openGeneric       = typeof(Delta <>);
            var closedGenericType = openGeneric.MakeGenericType(type);
            var delta             = Activator.CreateInstance(closedGenericType) as IDelta;

            if (delta == null)
            {
                return(null);
            }

            var resourceKey = mapping.ResourceType;

            if (!updateDocument.Data.ContainsKey(resourceKey))
            {
                return(delta);
            }

            var resource = updateDocument.Data[resourceKey] as JObject;

            if (resource == null)
            {
                return(delta);
            }

            // Scan the data for which properties are only set
            foreach (var propertySetter in mapping.PropertySettersExpressions)
            {
                JToken value;
                resource.TryGetValue(propertySetter.Key, StringComparison.CurrentCultureIgnoreCase, out value);
                if (value == null)
                {
                    continue;
                }
                // Set only the properties that are present
                var methodCallExpression = propertySetter.Value.Body as MethodCallExpression;
                if (methodCallExpression != null)
                {
                    Type returnType = methodCallExpression.Arguments[0].Type;

                    var resultValue = TransformationHelper.GetValue(value, returnType);

                    string key = propertySetter.Key.TrimStart('_');
                    delta.ObjectPropertyValues.Add(key, resultValue);
                }
            }

            JToken linksToken;

            resource.TryGetValue("links", StringComparison.CurrentCultureIgnoreCase, out linksToken);
            JObject links = linksToken as JObject;

            if (links != null)
            {
                foreach (var link in mapping.Relationships)
                {
                    JToken value;
                    links.TryGetValue(link.RelationshipName, StringComparison.CurrentCultureIgnoreCase, out value);
                    if (value == null)
                    {
                        continue;
                    }

                    if (link.IsCollection)
                    {
                        var property = link.RelatedCollectionProperty;
                        if (property != null)
                        {
                            var resultValue = TransformationHelper.GetCollection(value, link);

                            string key = link.RelationshipName.TrimStart('_');
                            delta.ObjectPropertyValues.Add(key, resultValue);
                        }
                    }
                    else
                    {
                        delta.ObjectPropertyValues.Add(link.ParentResourceNavigationPropertyName, TransformationHelper.GetValue(value, link.ParentResourceNavigationPropertyType));
                    }
                }
            }

            return(delta);
        }
示例#8
0
        public IDelta TransformBack(UpdateDocument updateDocument, Type type, Context context)
        {
            var mapping = configuration.GetMapping(type);
            var openGeneric = typeof(Delta<>);
            var closedGenericType = openGeneric.MakeGenericType(type);
            var delta = Activator.CreateInstance(closedGenericType) as IDelta;

            if (delta == null)
            {
                return null;
            }

            // Scan the data for which properties are only set
            foreach (var propertySetter in mapping.PropertySettersExpressions)
            {
                object value;
                updateDocument.Data.Attributes.TryGetValue(propertySetter.Key, out value);
                if (value != null)
                    delta.ObjectPropertyValues.Add(propertySetter.Key, value);
            }

            if (updateDocument.Data.Relationships != null)
            {
                foreach (var relMapping in mapping.Relationships)
                {
                    var relatedTypeMapping = configuration.GetMapping(relMapping.RelatedBaseType);
                    var relationship = updateDocument.Data.Relationships[relMapping.RelationshipName];
                    if (relationship == null)
                    {
                        continue;
                    }

                    if (relMapping.IsCollection && relationship.Data is MultipleResourceIdentifiers)
                    {
                        var multipleIDs = (MultipleResourceIdentifiers)relationship.Data;
                        var openGenericCollection = typeof(CollectionDelta<>);
                        var closedGenericTypeCollection = openGenericCollection.MakeGenericType(relMapping.RelatedBaseType);
                        var collection = Activator.CreateInstance(closedGenericTypeCollection, relatedTypeMapping.IdGetter) as ICollectionDelta;
                        var openGenericList = typeof(List<>);
                        var closedGenericTypeList = openGenericList.MakeGenericType(relMapping.RelatedBaseType);
                        collection.Elements = Activator.CreateInstance(closedGenericTypeList) as IList;
                        foreach (var id in multipleIDs)
                        {
                            var colProp = relMapping.RelatedCollectionProperty;

                            var newInstance = Activator.CreateInstance(relMapping.RelatedBaseType);
                            relatedTypeMapping.IdSetter(newInstance, id.Id);
                            (collection.Elements as IList).Add(newInstance);
                        }
                        delta.CollectionDeltas.Add(relMapping.RelationshipName, collection);

                    }
                    else if (!relMapping.IsCollection && relationship.Data is SingleResourceIdentifier)
                    {
                        var singleId = relationship.Data as SingleResourceIdentifier;
                        var instance = Activator.CreateInstance(relMapping.RelatedBaseType);
                        relatedTypeMapping.IdSetter(instance, singleId.Id);
                        delta.ObjectPropertyValues.Add(relMapping.RelationshipName, instance);
                    }
                    else
                    {
                        throw new InvalidOperationException();
                    }
                }

            }
            return delta;
        }
 public UpdateDocumentTypeWrapper(UpdateDocument updateDocument, Type type)
 {
     UpdateDocument = updateDocument;
     Type = type;
 }
示例#10
0
        public IDelta TransformBack(UpdateDocument updateDocument, Type type, Context context)
        {
            var mapping = context.Configuration.GetMapping(type);
            var openGeneric = typeof(Delta<>);
            var closedGenericType = openGeneric.MakeGenericType(type);
            var delta = Activator.CreateInstance(closedGenericType) as IDelta;

            if (delta == null)
            {
                return null;
            }

            var resourceKey = mapping.ResourceType;
            if (!updateDocument.Data.ContainsKey(resourceKey))
            {
                return delta;
            }

            var resource = updateDocument.Data[resourceKey] as JObject;
            if (resource == null)
            {
                return delta;
            }

            // Scan the data for which properties are only set
            foreach (var propertySetter in mapping.PropertySettersExpressions)
            {
                JToken value;
                resource.TryGetValue(propertySetter.Key, StringComparison.CurrentCultureIgnoreCase, out value);
                if (value == null)
                {
                    continue;
                }
                // Set only the properties that are present
                var methodCallExpression = propertySetter.Value.Body as MethodCallExpression;
                if (methodCallExpression != null)
                {
                    Type returnType = methodCallExpression.Arguments[0].Type;

                    var resultValue = TransformationHelper.GetValue(value, returnType);

                    string key = propertySetter.Key.TrimStart('_');
                    delta.ObjectPropertyValues.Add(key, resultValue);
                }
            }

            JToken linksToken;
            resource.TryGetValue("links", StringComparison.CurrentCultureIgnoreCase, out linksToken);
            JObject links = linksToken as JObject;

            if (links != null)
            {
                foreach (var link in mapping.Relationships)
                {
                    JToken value;
                    links.TryGetValue(link.RelationshipName, StringComparison.CurrentCultureIgnoreCase, out value);
                    if (value == null)
                    {
                        continue;
                    }

                    if (link.IsCollection)
                    {
                        var property = link.RelatedCollectionProperty;
                        if (property != null)
                        {
                            var resultValue = TransformationHelper.GetCollection(value, link);

                            string key = link.RelationshipName.TrimStart('_');
                            delta.ObjectPropertyValues.Add(key, resultValue);
                        }
                    }
                    else
                    {
                        delta.ObjectPropertyValues.Add(link.ParentResourceNavigationPropertyName, TransformationHelper.GetValue(value, link.ParentResourceNavigationPropertyType));
                    }
                }
            }

            return delta;
        }
示例#11
0
        private void ValidateUpdateDocument(UpdateDocument updateDocument)
        {
            if (updateDocument == null)
            {
                throw new JsonException("Json body can not be empty or whitespace.");
            }

            if (updateDocument.Data == null)
            {
                throw new JsonException("Json body should contain some content.");
            }
        }