public void Transform_UpdateDocument_To_Delta_TwoFields()
        {
            // Arrange
            var updateDocument = new UpdateDocument
            {
                Data = new Dictionary <string, object>
                {
                    {
                        "posts", JObject.FromObject(new PostUpdateTwoFields()
                        {
                            Title    = "Food",
                            AuthorId = 0
                        })
                    }
                }
            };

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


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

            // Assert
            resultDelta.ObjectPropertyValues.ContainsKey("title").ShouldBeTrue();
            resultDelta.ObjectPropertyValues.ContainsKey("authorId").ShouldBeTrue();
        }
Beispiel #2
0
 public void Initialize()
 {
     transformer = new JsonApiTransformer()
     {
         TransformationHelper = transformationHelper
     };
 }
        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);
        }
Beispiel #4
0
        public void Transform_UpdateDocument_To_Delta_TwoFields()
        {
            // Arrange
            var updateDocument = new UpdateDocument
            {
                Data = new Dictionary <string, object>()
                {
                    { "data", JObject.Parse("{ \"id\":123, \"type\":\"post\", \"attributes\" : { \"title\": \"someTitle\", \"authorId\" : \"1234\"}}") }
                }
            };

            var configuration = (new ConfigurationBuilder())
                                .Resource <Post>()
                                .WithSimpleProperty(x => x.AuthorId)
                                .WithSimpleProperty(x => x.Title);
            var context = new Context(configuration.ConfigurationBuilder.Build(), new Uri("http://fakehost:1234", UriKind.Absolute));
            var sut     = new JsonApiTransformer();


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

            // Assert
            Assert.True(resultDelta.ObjectPropertyValues.ContainsKey("title"));
            Assert.True(resultDelta.ObjectPropertyValues.ContainsKey("authorId"));
        }
Beispiel #5
0
        private JsonApiActionFilter GetActionFilterForTestModel()
        {
            var config      = TestModelConfigurationBuilder.BuilderForEverything.Build();
            var transformer = new JsonApiTransformer();

            return(new JsonApiActionFilter(transformer, config));
        }
Beispiel #6
0
        public void GIVEN_Exception_WHEN_OnActionExecuted_THEN_ExceptionIsInCompoundDocument()
        {
            // Arrange
            var transformer     = new JsonApiTransformer();
            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);
        }
        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();

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

            // Assert
            resultDelta.ObjectPropertyValues.ContainsKey("id").ShouldBeTrue();
        }
Beispiel #8
0
        public void Creates_CompondDocument_for_collection_not_nested_class_and_propertly_map_properties()
        {
            // Arrange
            var configuration = CreateContext();
            IEnumerable <SampleClass> objectsToTransform = CreateObjectToTransform();
            var sut = new JsonApiTransformer()
            {
                TransformationHelper = new TransformationHelper()
            };

            // Act
            CompoundDocument result = sut.Transform(objectsToTransform, configuration);

            // Assert
            var transformedObject = result.Data as ResourceCollection;

            Action <SingleResource, SampleClass> assertSame = (actual, expected) =>
            {
                actual.Attributes["someValue"].ShouldEqual(expected.SomeValue);
                actual.Attributes["date"].ShouldEqual(expected.DateTime);
                actual.Attributes.ShouldHaveCountOf(2);
            };

            assertSame(transformedObject[0], objectsToTransform.First());
            assertSame(transformedObject[1], objectsToTransform.Last());
        }
        public JsonApiInputFormatter(JsonSerializer jsonSerializer, Configuration configuration, JsonApiTransformer jsonApiTransformer)
        {
            this.jsonSerializer     = jsonSerializer;
            this.configuration      = configuration;
            this.jsonApiTransformer = jsonApiTransformer;

            SupportedMediaTypes.Clear();
            SupportedMediaTypes.Add(configuration.DefaultJsonApiMediaType);
        }
Beispiel #10
0
        public void Creates_CompondDocument_for_collectione_of_metadatawrapper_throws_notSupportedException()
        {
            // Arrange
            var configuration      = CreateContext();
            var objectsToTransform = new List <MetaDataWrapper <SampleClass> >
            {
                new MetaDataWrapper <SampleClass>(new SampleClass())
            };
            var sut = new JsonApiTransformer();

            // Act => Assert
            Assert.Throws <NotSupportedException>(() => sut.Transform(objectsToTransform, configuration));
        }
Beispiel #11
0
        public void Creates_CompondDocument_for_single_not_nested_class_and_propertly_map_type()
        {
            // Arrange
            var         context           = CreateContext();
            SampleClass objectToTransform = CreateObjectToTransform();
            var         sut = new JsonApiTransformer();

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

            // Assert
            var transformedObject = result.Data as SingleResource;

            transformedObject.Type.ShouldEqual("sampleClasses");
        }
Beispiel #12
0
        public void Serilized_properly()
        {
            // Arrange
            var configuration     = CreateContext();
            var objectToTransform = CreateObjectToTransform();
            var sut = new JsonApiTransformer();

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

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

            // Assert
            Assert.DoesNotContain(json, "Data");
        }
Beispiel #13
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();

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

            // Assert
            var transformedObject = result.Data as SingleResource;

            transformedObject.Links["self"].ToString().ShouldEqual("http://sampleclass/1");
        }
Beispiel #14
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();

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

            // Assert
            var transformedObject = result.Data as SingleResource;

            Assert.Equal(transformedObject.Id, objectToTransform.Value.Id.ToString());
        }
Beispiel #15
0
        public void Creates_CompondDocument_for_collection_not_nested_class_and_propertly_map_resourceName()
        {
            // Arrange
            var configuration = CreateContext();
            IEnumerable <SampleClass> objectsToTransform = CreateObjectToTransform();
            var sut = new JsonApiTransformer();

            // Act
            CompoundDocument result = sut.Transform(objectsToTransform, configuration);

            // Assert
            result.Data.ShouldNotBeNull();
            var transformedObject = result.Data as ResourceCollection;

            transformedObject.ShouldNotBeNull();
        }
Beispiel #16
0
        public void Creates_CompondDocument_for_collection_not_nested_class_and_propertly_map_id()
        {
            // Arrange
            var configuration = CreateContext();
            IEnumerable <SampleClass> objectsToTransform = CreateObjectToTransform();
            var sut = new JsonApiTransformer();

            // Act
            CompoundDocument result = sut.Transform(objectsToTransform, configuration);

            // Assert
            var transformedObject = result.Data as ResourceCollection;

            transformedObject[0].Id.ShouldEqual(objectsToTransform.First().Id.ToString());
            transformedObject[1].Id.ShouldEqual(objectsToTransform.Last().Id.ToString());
        }
Beispiel #17
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);
        }
        public void Creates_CompondDocument_for_metadatawrapper_single_not_nested_class_and_propertly_map_resourceName()
        {
            // Arrange
            var context = CreateContext();
            MetaDataWrapper <SampleClass> objectToTransform = CreateObjectToTransform();
            var sut = new JsonApiTransformer();

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

            // Assert
            result.Data.ShouldNotBeNull();
            var transformedObject = result.Data as SingleResource;

            transformedObject.ShouldNotBeNull();
        }
Beispiel #19
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();

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

            // Assert
            Assert.NotNull(result.Data);
            var transformedObject = result.Data as SingleResource;

            Assert.NotNull(transformedObject);
        }
Beispiel #20
0
        public void Creates_CompondDocument_for_collection_not_nested_class_and_propertly_map_href()
        {
            // Arrange
            var configuration = CreateContext();
            IEnumerable <SampleClass> objectsToTransform = CreateObjectToTransform();
            var sut = new JsonApiTransformer();

            // Act
            CompoundDocument result = sut.Transform(objectsToTransform, configuration);

            // Assert
            var transformedObject = result.Data as ResourceCollection;

            Assert.Equal(transformedObject[0].Links["self"].ToString(), "http://sampleclass/1");
            Assert.Equal(transformedObject[1].Links["self"].ToString(), "http://sampleclass/2");
        }
Beispiel #21
0
        public void Creates_CompondDocument_for_collection_not_nested_single_class()
        {
            // Arrange
            var configuration = CreateContext();
            IEnumerable <SampleClass> objectsToTransform = CreateObjectToTransform().Take(1);
            var sut = new JsonApiTransformer();

            // Act
            CompoundDocument result = sut.Transform(objectsToTransform, configuration);

            // Assert
            Assert.NotNull(result.Data);
            var transformedObject = result.Data as ResourceCollection;

            Assert.NotNull(transformedObject);
        }
Beispiel #22
0
        public void Apply(IServiceCollection services)
        {
            var serializer  = GetJsonSerializer();
            var transformer = new JsonApiTransformer(serializer);

            services.AddMvc(
                options =>
            {
                options.Filters.Add(typeof(JsonApiActionFilter));
                options.Filters.Add(typeof(JsonApiExceptionFilter));
                options.OutputFormatters.Insert(0, new JsonApiOutputFormatter(this));
                options.InputFormatters.Insert(0, new JsonApiInputFormatter(serializer, this, transformer));
            });

            services.AddSingleton <IJsonApiTransformer, JsonApiTransformer>();
            services.AddInstance <IConfiguration>(this);
        }
Beispiel #23
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();

            // 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);
        }
Beispiel #24
0
        public void Creates_CompondDocument_for_metadatawrapper_single_not_nested_class_and_propertly_map_properties()
        {
            // Arrange
            var context = CreateContext();
            MetaDataWrapper <SampleClass> objectToTransform = CreateObjectToTransform();
            var sut = new JsonApiTransformer();


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

            // Assert
            var transformedObject = result.Data as SingleResource;

            Assert.Equal(transformedObject.Attributes["someValue"], objectToTransform.Value.SomeValue);
            Assert.Equal(transformedObject.Attributes["date"], objectToTransform.Value.DateTime);
            Assert.Equal(transformedObject.Attributes.Count, 2);
        }
Beispiel #25
0
        public void Creates_CompondDocument_for_collection_not_nested_class_and_propertly_map_type()
        {
            // Arrange
            var configuration = CreateContext();
            IEnumerable <SampleClass> objectsToTransform = CreateObjectToTransform();
            var sut = new JsonApiTransformer()
            {
                TransformationHelper = new TransformationHelper()
            };

            // Act
            CompoundDocument result = sut.Transform(objectsToTransform, configuration);

            // Assert
            var transformedObject = result.Data as ResourceCollection;

            transformedObject[0].Type.ShouldEqual("sampleClasses");
            transformedObject[1].Type.ShouldEqual("sampleClasses");
        }
Beispiel #26
0
 public void Initialize()
 {
     transformer = new JsonApiTransformer();
 }
Beispiel #27
0
 public TestLinks()
 {
     transformer = new JsonApiTransformer();
 }