Inheritance: IJsonApiTransformer
Ejemplo n.º 1
0
        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();
        }
Ejemplo n.º 2
0
        public void GIVEN_Exception_WHEN_OnActionExecuted_THEN_ExceptionIsInCompoundDocument()
        {
            // Arrange
            var transformer = new JsonApiTransformer(null, null, null);
            var exceptionFilter = new JsonApiExceptionFilter(transformer);


            var post = new PostBuilder()
                .WithAuthor(PostBuilder.Asimov)
                .Build();

            var context = new FilterContextBuilder()
                .WithException("Test exception message")
                .BuildException();

            // Act
            exceptionFilter.OnException(context);

            // Assert
            var result = (ObjectResult)context.Result;
            var value = (CompoundDocument)result.Value;

            Assert.Equal(1, value.Errors.Count());
            Assert.Equal("Test exception message", value.Errors.First().Detail);
            Assert.Equal(500, value.Errors.First().Status);
        }
Ejemplo n.º 3
0
        public void Apply(HttpConfiguration configuration)
        {
            var serializer = GetJsonSerializer();
            var helper = new TransformationHelper();
            var transformer = new JsonApiTransformer { Serializer = serializer, TransformationHelper = helper };

            var filter = new JsonApiActionFilter(transformer, this);
            configuration.Filters.Add(filter);

            var formatter = new JsonApiFormatter(this, serializer);
            configuration.Formatters.Add(formatter);
        }
Ejemplo n.º 4
0
        public void Creates_CompondDocument_for_metadatawrapper_single_not_nested_class_and_propertly_map_id()
        {
            // Arrange
            var context = CreateContext();
            MetaDataWrapper<SampleClass> objectToTransform = CreateObjectToTransform();
            var sut = new JsonApiTransformer() { TransformationHelper = new TransformationHelper() };

            // Act
            CompoundDocument result = sut.Transform(objectToTransform, context);

            // Assert
            var transformedObject = result.Data as SingleResource;
            transformedObject.Id.ShouldEqual(objectToTransform.Value.Id.ToString());
        }
Ejemplo n.º 5
0
        public void Creates_CompondDocument_for_single_not_nested_class_and_propertly_map_href()
        {
            // Arrange
            var context = CreateContext();
            SampleClass objectToTransform = CreateObjectToTransform();
            var sut = new JsonApiTransformer() { TransformationHelper = new TransformationHelper() };

            // Act
            CompoundDocument result = sut.Transform(objectToTransform, context);

            // Assert
            var transformedObject = result.Data as SingleResource;
            transformedObject.Links["self"].ToString().ShouldEqual("http://sampleclass/1");
        }
Ejemplo n.º 6
0
        public void Creates_CompondDocument_for_single_not_nested_class_and_propertly_map_resourceName()
        {
            // Arrange
            var context = CreateContext();
            SampleClass objectToTransform = CreateObjectToTransform();
            var sut = new JsonApiTransformer() { TransformationHelper = new TransformationHelper() };

            // Act
            CompoundDocument result = sut.Transform(objectToTransform, context);

            // Assert
            result.Data.ShouldNotBeNull();
            var transformedObject = result.Data as SingleResource;
            transformedObject.ShouldNotBeNull();
        }
Ejemplo n.º 7
0
        public void Serilized_properly()
        {
            // Arrange
            var configuration = CreateContext();
            var objectToTransform = CreateObjectToTransform();
            var sut = new JsonApiTransformer() { TransformationHelper = new TransformationHelper() };

            var transformed = sut.Transform(objectToTransform, configuration);

            // Act
            var json = JsonConvert.SerializeObject(transformed);

            // Assert
            json.ShouldNotContain("Data");
        }
Ejemplo n.º 8
0
        public void Creates_CompondDocument_for_single_not_nested_class_and_propertly_map_properties()
        {
            // Arrange
            var context = CreateContext();
            SampleClass objectToTransform = CreateObjectToTransform();
            var sut = new JsonApiTransformer() { TransformationHelper = new TransformationHelper() };

            // Act
            CompoundDocument result = sut.Transform(objectToTransform, context);

            // Assert
            var transformedObject = result.Data as SingleResource;
            transformedObject.Attributes["someValue"].ShouldEqual(objectToTransform.SomeValue);
            transformedObject.Attributes["date"].ShouldEqual(objectToTransform.DateTime);
            transformedObject.Attributes.ShouldHaveCountOf(2);
        }
Ejemplo n.º 9
0
        public void Creates_CompondDocument_for_metadatawrapper_single_not_nested_class_and_propertly_map_metadata()
        {
            // Arrange
            const string pagingValue = "1";
            const string countValue = "2";

            var context = CreateContext();
            MetaDataWrapper<SampleClass> objectToTransform = CreateObjectToTransform();
            objectToTransform.MetaData.Add("Paging", pagingValue);
            objectToTransform.MetaData.Add("Count", countValue);
            var sut = new JsonApiTransformer() { TransformationHelper = new TransformationHelper() };

            // Act
            CompoundDocument result = sut.Transform(objectToTransform, context);

            // Assert
            var transformedObjectMetadata = result.Meta;
            transformedObjectMetadata["Paging"].ShouldEqual(pagingValue);
            transformedObjectMetadata["Count"].ShouldEqual(countValue);
        }
Ejemplo n.º 10
0
        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();
        }
Ejemplo n.º 11
0
 public JsonApiActionFilter(JsonApiTransformer jsonApiTransformer, Configuration configuration)
 {
     this.jsonApiTransformer = jsonApiTransformer;
     this.configuration      = configuration;
 }
Ejemplo n.º 12
0
 public void Initialize()
 {
     transformer = new JsonApiTransformer(){TransformationHelper = transformationHelper};
 }
Ejemplo n.º 13
0
 public JsonApiActionFilter(JsonApiTransformer jsonApiTransformer, Configuration configuration)
 {
     this.jsonApiTransformer = jsonApiTransformer;
     this.configuration = configuration;
 }