public void Transform_UpdateDocument_To_Delta_Containing_ComplexObject()
        {
            // Arrange
            var dimensions = new Dimensions()
            {
                Height = "80mm", Width = "10mm"
            };
            var updateDocument = new UpdateDocument
            {
                Data = new SingleResource()
                {
                    Id         = "123",
                    Type       = "product",
                    Attributes = new Dictionary <string, object>()
                    {
                        { "name", "Widget" },
                        { "dimensions", dimensions }
                    }
                }
            };

            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(Product), context);

            // Assert
            Assert.True(resultDelta.ObjectPropertyValues.ContainsKey("name"));
            Assert.True(resultDelta.ObjectPropertyValues.ContainsKey("dimensions"));
            Assert.Equal(dimensions, resultDelta.ObjectPropertyValues["dimensions"]);
        }
Beispiel #2
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"));
        }
Beispiel #3
0
        private JsonApiActionFilter GetActionFilterForTestModel()
        {
            var config      = TestModelConfigurationBuilder.BuilderForEverything.Build();
            var transformer = new JsonApiTransformerBuilder().With(config).Build();

            return(new JsonApiActionFilter(transformer, config, JsonSerializerBuilder.Build()));
        }
Beispiel #4
0
        public void Creates_CompondDocument_for_collection_not_nested_class_and_propertly_map_properties()
        {
            // Arrange
            var context = CreateContext();
            IEnumerable <SampleClass> objectsToTransform = CreateObjectToTransform();
            var transformer = new JsonApiTransformerBuilder()
                              .With(CreateConfiguration())
                              .Build();

            // Act
            CompoundDocument result = transformer.Transform(objectsToTransform, context);

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

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

            assertSame(transformedObject[0], objectsToTransform.First());
            assertSame(transformedObject[1], objectsToTransform.Last());
        }
Beispiel #5
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 transformer = new JsonApiTransformerBuilder().Build();

            // Act => Assert
            Assert.Throws <NotSupportedException>(() => transformer.Transform(objectsToTransform, configuration));
        }
        public void Creates_CompoundDocument_for_TopLevelDocument_single_not_nested_class_and_properly_map_id()
        {
            // Arrange
            var context = CreateContext();
            TopLevelDocument <SampleClass> objectToTransform = CreateObjectToTransform();
            var transfomer = new JsonApiTransformerBuilder()
                             .With(CreateConfiguration())
                             .Build();

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

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

            Assert.Equal(transformedObject.Id, objectToTransform.Value.Id.ToString());
        }
        public void Serilized_properly()
        {
            // Arrange
            var context           = CreateContext();
            var objectToTransform = CreateObjectToTransform();
            var transformer       = new JsonApiTransformerBuilder()
                                    .With(CreateConfiguration())
                                    .Build();

            var transformed = transformer.Transform(objectToTransform, context);

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

            // Assert
            Assert.DoesNotContain(json, "Data");
        }
Beispiel #8
0
        public void Creates_CompoundDocument_for_single_not_nested_class_and_properly_map_type()
        {
            // Arrange
            var         context           = CreateContext();
            SampleClass objectToTransform = CreateObjectToTransform();
            var         transformer       = new JsonApiTransformerBuilder()
                                            .With(CreateConfiguration())
                                            .Build();

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

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

            Assert.Equal(transformedObject.Type, "sampleClasses");
        }
Beispiel #9
0
        public void Creates_CompoundDocument_for_single_class_with_nometadata_and_properly_map_nometadata()
        {
            // Arrange
            var         context           = CreateContext();
            SampleClass objectToTransform = CreateObjectToTransform();
            var         transformer       = new JsonApiTransformerBuilder()
                                            .With(CreateConfiguration())
                                            .Build();

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

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

            Assert.Null(transformedObject.MetaData);
        }
Beispiel #10
0
        public void Creates_CompoundDocument_for_single_class_with_metadata_and_properly_map_metadata()
        {
            // Arrange
            var context = CreateContext();
            SampleClassWithObjectMetadata objectToTransform = CreateObjectWithMetadataToTransform();
            var transformer = new JsonApiTransformerBuilder()
                              .With(CreateConfiguration())
                              .Build();

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

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

            Assert.Equal("value1", transformedObject.MetaData["meta1"]);
            Assert.Equal("value2", transformedObject.MetaData["meta2"]);
        }
Beispiel #11
0
        public void Creates_CompondDocument_for_metadatawrapper_single_not_nested_class_and_propertly_map_resourceName()
        {
            // Arrange
            var context = CreateContext();
            MetaDataWrapper <SampleClass> objectToTransform = CreateObjectToTransform();
            var transfomer = new JsonApiTransformerBuilder()
                             .With(CreateConfiguration())
                             .Build();

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

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

            Assert.NotNull(transformedObject);
        }
Beispiel #12
0
        public void Creates_CompoundDocument_for_single_class_with_links_and_properly_map_links()
        {
            // Arrange
            var context = CreateContext();
            SampleClassWithObjectLinks objectToTransform = CreateObjectWithLinkDataToTransform();
            var transformer = new JsonApiTransformerBuilder()
                              .With(CreateConfiguration())
                              .Build();

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

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

            Assert.Equal("url1", ((ISimpleLink)transformedObject.Links["link1"]).Href);
            Assert.Equal("url2", ((ISimpleLink)transformedObject.Links["link2"]).Href);
        }
Beispiel #13
0
        public void Creates_CompondDocument_for_collection_not_nested_class_and_propertly_map_id()
        {
            // Arrange
            var configuration = CreateContext();
            IEnumerable <SampleClass> objectsToTransform = CreateObjectToTransform();
            var transformer = new JsonApiTransformerBuilder()
                              .With(CreateConfiguration())
                              .Build();

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

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

            Assert.Equal(transformedObject[0].Id, objectsToTransform.First().Id.ToString());
            Assert.Equal(transformedObject[1].Id, objectsToTransform.Last().Id.ToString());
        }
Beispiel #14
0
        public void Creates_CompondDocument_for_collection_not_nested_single_class()
        {
            // Arrange
            var configuration = CreateContext();
            IEnumerable <SampleClass> objectsToTransform = CreateObjectToTransform().Take(1);
            var transformer = new JsonApiTransformerBuilder()
                              .With(CreateConfiguration())
                              .Build();

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

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

            Assert.NotNull(transformedObject);
        }
        public void Serialized_properly()
        {
            // Arrange
            var context           = CreateContext();
            var objectToTransform = CreateObjectToTransform();
            var transformer       = new JsonApiTransformerBuilder()
                                    .With(CreateConfiguration())
                                    .Build();

            var transformed = transformer.Transform(objectToTransform, context);

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

            // Assert
            Assert.DoesNotContain(json, "Data");
            Assert.True(json.Contains("\"complexAttribute\":{\"Label\":\"This is complex attribute class\",\"InnerComplexAttribute\":{\"AnotherLabel\":\"This is inner complex attribute class\"}}"));
            Assert.True(json.Contains("\"listAttribute\":[{\"Label\":\"Complex 1\",\"InnerComplexAttribute\":{\"AnotherLabel\":\"This is inner complex attribute class\"}},{\"Label\":\"Complex 2\",\"InnerComplexAttribute\":{\"AnotherLabel\":\"This is inner complex attribute class\"}}]"));
        }
Beispiel #16
0
        public void Creates_CompoundDocument_for_single_not_nested_class_and_properly_map_properties()
        {
            // Arrange
            var         context           = CreateContext();
            SampleClass objectToTransform = CreateObjectToTransform();
            var         transformer       = new JsonApiTransformerBuilder()
                                            .With(CreateConfiguration())
                                            .Build();

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

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

            Assert.Equal(transformedObject.Attributes["someValue"], objectToTransform.SomeValue);
            Assert.Equal(transformedObject.Attributes["date"], objectToTransform.DateTime);
            Assert.Equal(transformedObject.Attributes.Count, 2);
        }
Beispiel #17
0
        public void Creates_CompoundDocument_for_single_class_with_relationship_metadata_and_properly_map_metadata()
        {
            // Arrange
            var context = CreateContext();
            SampleClassWithRelatedClassesWithRelationshipMetadata objectToTransform = CreateObjectWithRelationshipMetadataToTransform();
            var transformer = new JsonApiTransformerBuilder()
                              .With(CreateConfiguration())
                              .Build();

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

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

            Assert.Equal("value100", ((SingleResourceIdentifier)(transformedObject.Relationships["relatedObject"]).Data).MetaData["meta100"]);
            Assert.Equal("value1", ((MultipleResourceIdentifiers)(transformedObject.Relationships["relatedObjects"]).Data)[0].MetaData["meta1"]);
            Assert.Equal("value2", ((MultipleResourceIdentifiers)(transformedObject.Relationships["relatedObjects"]).Data)[1].MetaData["meta2"]);
        }
Beispiel #18
0
        public void Creates_CompoundDocument_for_list_class_and_property_map_type()
        {
            // Arrange
            var             context           = CreateContext();
            SampleListClass objectToTransform = CreateListObjectToTransform();
            var             transformer       = new JsonApiTransformerBuilder()
                                                .With(CreateConfigurationForListType())
                                                .Build();

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

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

            Assert.Equal(transformedObject.Type, "sampleListClasses");
            Assert.Equal(transformedObject.Attributes.Count, 2);
            Assert.True(transformedObject.Attributes.ContainsKey("listAttribute"));
            Assert.Equal(objectToTransform.ListAttribute, transformedObject.Attributes["listAttribute"]);
        }
Beispiel #19
0
        public void Creates_CompoundDocument_for_single_class_with_no_links_and_properly_map_no_links()
        {
            // Arrange
            var         context           = CreateContext();
            SampleClass objectToTransform = CreateObjectToTransform();
            var         transformer       = new JsonApiTransformerBuilder()
                                            .With(CreateConfiguration())
                                            .Build();

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

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

            Assert.NotNull(transformedObject);
            Assert.Equal(1, transformedObject.Links.Count);
            Assert.Equal("http://example.com/", ((ISimpleLink)((LinkData)transformedObject.Links)["self"]).Href);
        }
        public void Transform_UpdateDocument_To_Delta_Containing_ListAttribute()
        {
            //DS: TODO - should widgetParts be provided as a JArray?
            // Arrange
            var widgetParts = new List <WidgetPart>()
            {
                new WidgetPart()
                {
                    PartNumber = "WIDGET-001"
                },
                new WidgetPart()
                {
                    PartNumber = "WIDGET-002"
                }
            };
            var updateDocument = new UpdateDocument
            {
                Data = new SingleResource()
                {
                    Id         = "123",
                    Type       = "widget",
                    Attributes = new Dictionary <string, object>()
                    {
                        { "name", "A widget" },
                        { "parts", widgetParts }
                    }
                }
            };

            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(Widget), context);

            // Assert
            Assert.True(resultDelta.ObjectPropertyValues.ContainsKey("name"));
            Assert.True(resultDelta.ObjectPropertyValues.ContainsKey("parts"));
            Assert.Equal(resultDelta.ObjectPropertyValues["parts"], widgetParts);
        }
Beispiel #21
0
        public void Creates_CompoundDocument_for_single_not_nested_class_and_properly_suppress_nullable_properties()
        {
            // Arrange
            var context = CreateContext();
            SampleClassWithNullableProperty objectToTransform = CreateObjectWithNullPropertyToTransform();
            var config = CreateConfiguration();

            config.GetJsonSerializerSettings().NullValueHandling = NullValueHandling.Ignore;
            var transformer = new JsonApiTransformerBuilder()
                              .With(config)
                              .Build();

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

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

            Assert.Equal(transformedObject.Attributes["someValue"], objectToTransform.SomeValue);
            Assert.False(transformedObject.Attributes.ContainsKey("date"));
            Assert.Equal(transformedObject.Attributes.Count, 1);
        }
        public void Creates_CompoundDocument_for_TopLevelDocument_single_not_nested_class_and_properly_map_links()
        {
            // Arrange
            SimpleLink linkSome  = new SimpleLink(new Uri("http://somehost/"));
            SimpleLink linkOther = new SimpleLink(new Uri("http://otherhost/"));

            var context = CreateContext();
            TopLevelDocument <SampleClass> objectToTransform = CreateObjectToTransform();

            objectToTransform.Links.Add("linkSome", linkSome);
            objectToTransform.Links.Add("linkOther", linkOther);
            var transfomer = new JsonApiTransformerBuilder()
                             .With(CreateConfiguration())
                             .Build();

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

            // Assert
            var transformedObjectLinks = result.Links;

            Assert.Same(linkSome, transformedObjectLinks["linkSome"]);
            Assert.Same(linkOther, transformedObjectLinks["linkOther"]);
        }
        public void Creates_CompoundDocument_for_TopLevelDocument_single_not_nested_class_and_properly_map_metadata()
        {
            // Arrange
            const string pagingValue = "1";
            const string countValue  = "2";

            var context = CreateContext();
            TopLevelDocument <SampleClass> objectToTransform = CreateObjectToTransform();

            objectToTransform.GetMetaData().Add("Paging", pagingValue);
            objectToTransform.GetMetaData().Add("Count", countValue);
            var transfomer = new JsonApiTransformerBuilder()
                             .With(CreateConfiguration())
                             .Build();

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

            // Assert
            var transformedObjectMetadata = result.Meta;

            Assert.Equal(transformedObjectMetadata["Paging"], pagingValue);
            Assert.Equal(transformedObjectMetadata["Count"], countValue);
        }