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(); }
public object SerializeObject(ConfigurationBuilder serializerConfig, object obj) { var config = serializerConfig.Build(); var sut = new JsonApiTransformer() { TransformationHelper = new TransformationHelper() }; CompoundDocument result = sut.Transform(obj, new Context() { Configuration = config, RoutePrefix = _routePrefix }); return result; }
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_collectione_of_metadatawrapper_throws_notSupportedException() { // Arrange var configuration = CreateContext(); var objectsToTransform = new List<MetaDataWrapper<SampleClass>> { new MetaDataWrapper<SampleClass>(new SampleClass()) }; var sut = new JsonApiTransformer() { TransformationHelper = new TransformationHelper() }; // Act => Assert ThrowsAssert.Throws<NotSupportedException>(() => sut.Transform(objectsToTransform, configuration)); }
public void Creates_CompondDocument_for_single_not_nested_class_and_propertly_map_id() { // 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.Id.ShouldEqual(objectToTransform.Id.ToString()); }
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"); }
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() { TransformationHelper = new TransformationHelper() }; // 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()); }
public void Creates_CompondDocument_for_metadatawrapper_single_not_nested_class_and_propertly_map_href() { // 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.Links["self"].ToString().ShouldEqual("http://sampleclass/1"); }
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); }
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); }
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 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(); }
public JsonApiActionFilter(JsonApiTransformer jsonApiTransformer, Configuration configuration) { this.jsonApiTransformer = jsonApiTransformer; this.configuration = configuration; }
public void Initialize() { transformer = new JsonApiTransformer(){TransformationHelper = transformationHelper}; }
public void Creates_CompondDocument_for_collection_not_nested_single_class() { // Arrange var configuration = CreateContext(); IEnumerable<SampleClass> objectsToTransform = CreateObjectToTransform().Take(1); var sut = new JsonApiTransformer() { TransformationHelper = new TransformationHelper() }; // Act CompoundDocument result = sut.Transform(objectsToTransform, configuration); // Assert result.Data.ShouldNotBeNull(); var transformedObject = result.Data as ResourceCollection; transformedObject.ShouldNotBeNull(); }
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() { TransformationHelper = new TransformationHelper() }; // Act CompoundDocument result = sut.Transform(objectToTransform, context); // Assert result.Data.ShouldNotBeNull(); var transformedObject = result.Data as SingleResource; transformedObject.ShouldNotBeNull(); }