Ejemplo n.º 1
0
        public void GIVEN_ComplexObjectCollection_WHEN_DeltaApply_THEN_ValuesApplied()
        {
            //Arrange
            Setup();

            _mapping.Object.PropertySetters.Add("comments", (o, p) => { ((Article)o).Comments = ((JArray)p).ToObject <List <Comment> >(); });

            var article        = new Article();
            var classUnderTest = new Delta <Article>(_configuration.Object);

            var commentsJArray = JsonConvert.DeserializeObject("[ { 'body': 'comment 1 body' }, { 'body': 'comment 2 body' } ]");

            classUnderTest.ObjectPropertyValues =
                new Dictionary <string, object>()
            {
                { "comments", commentsJArray }
            };

            classUnderTest.Scan();

            //Act
            classUnderTest.ApplySimpleProperties(article);

            //Assert
            Assert.NotNull(article.Comments);
            Assert.Equal(2, article.Comments.Count);
            Assert.Equal("comment 1 body", article.Comments[0].Body);
            Assert.Equal("comment 2 body", article.Comments[1].Body);
        }
Ejemplo n.º 2
0
        public void GIVEN_IncompleteProperties_WHEN_DeltaApply_THEN_OnlyThoseSpecifiedApplied()
        {
            //Arrange
            Setup();
            _mapping.Object.PropertySetters.Add("id", (o, p) => { ((Author)o).Id = (int)p; });
            _mapping.Object.PropertySetters.Add("name", (o, p) => { ((Author)o).Name = (string)p; });
            _mapping.Object.PropertySetters.Add("dateTimeCreated", (o, p) => { ((Author)o).DateTimeCreated = (DateTime)p; });


            var author         = new Author();
            var classUnderTest = new Delta <Author>(_configuration.Object);

            classUnderTest.ObjectPropertyValues =
                new Dictionary <string, object>()
            {
                { "id", 1 },
                { "dateTimeCreated", new DateTime(2016, 1, 1) },
            };

            classUnderTest.Scan();

            //Act
            classUnderTest.ApplySimpleProperties(author);

            //Assert
            Assert.Equal(author.Id, 1);
            Assert.Equal(author.DateTimeCreated, new DateTime(2016, 1, 1));
            Assert.Null(author.Name);
        }
Ejemplo n.º 3
0
        public void GIVEN_SimpleTypeCollection_WHEN_DeltaApply_THEN_ValuesApplied()
        {
            //Arrange
            Setup();

            _mapping.Object.PropertySetters.Add("yearsPublished", (o, p) => { ((Article)o).YearsPublished = ((JArray)p).ToObject <List <int> >(); });

            var article        = new Article();
            var classUnderTest = new Delta <Article>(_configuration.Object);

            var commentsJArray = JsonConvert.DeserializeObject("[ 1234, 5678 ]");

            classUnderTest.ObjectPropertyValues =
                new Dictionary <string, object>()
            {
                { "yearsPublished", commentsJArray }
            };

            classUnderTest.Scan();

            //Act
            classUnderTest.ApplySimpleProperties(article);

            //Assert
            Assert.NotNull(article.YearsPublished);
            Assert.Equal(2, article.YearsPublished.Count);
            Assert.Equal(1234, article.YearsPublished[0]);
            Assert.Equal(5678, article.YearsPublished[1]);
        }
Ejemplo n.º 4
0
        public IActionResult Patch([FromBody] Delta <Article> update, int id)
        {
            var article = StaticPersistentStore.Articles.Single(w => w.Id == id);

            update.ApplySimpleProperties(article);
            return(new ObjectResult(article));
        }
Ejemplo n.º 5
0
        public void GIVEN_ComplexObjectProperty_WHEN_DeltaApply_THEN_ValuesApplied()
        {
            //Arrange
            Setup();

            _mapping.Object.PropertySetters.Add("author", (o, p) => { ((Article)o).Author = ((JObject)p).ToObject <Author>(); });

            var article        = new Article();
            var classUnderTest = new Delta <Article>(_configuration.Object);

            var authorJObject = JsonConvert.DeserializeObject("{ 'name': 'author name' }");

            classUnderTest.ObjectPropertyValues =
                new Dictionary <string, object>()
            {
                { "author", authorJObject }
            };

            classUnderTest.Scan();

            //Act
            classUnderTest.ApplySimpleProperties(article);

            //Assert
            Assert.NotNull(article.Author);
            Assert.Equal("author name", article.Author.Name);
        }
        public IHttpActionResult Patch([FromBody] Delta <Article> update, int id)
        {
            var article = StaticPersistentStore.Articles.SingleOrDefault(w => w.Id == id);

            if (article == null)
            {
                return(NotFound());
            }
            update.ApplySimpleProperties(article);

            return(Ok(article));
        }
Ejemplo n.º 7
0
        public void GIVEN_ScanNotCalled_WHEN_DeltaApplySimpleProperties_THEN_ExceptionThrown()
        {
            //Arrange
            Setup();

            var author         = new Author();
            var classUnderTest = new Delta <Author>(_configuration.Object);

            //Act/Assert
            var ex = Assert.Throws <Exception>(() => classUnderTest.ApplySimpleProperties(author));

            Assert.Equal("Scan must be called before this method", ex.Message);
        }
Ejemplo n.º 8
0
        public void GIVEN_NoProperties_WHEN_DeltaApply_THEN_OutputsAreDefault()
        {
            //Arrange
            var simpleObject    = new Author();
            var objectUnderTest = new Delta <Author>();

            //Act
            objectUnderTest.ApplySimpleProperties(simpleObject);

            //Assert
            Assert.Equal(simpleObject.Id, 0);
            Assert.Null(simpleObject.Name);
            Assert.Equal(simpleObject.DateTimeCreated, new DateTime());
        }
Ejemplo n.º 9
0
        public void TestEmptyPropertiesValues()
        {
            //Arrange
            var simpleObject = new SimpleTestClass();
            var objectUnderTest = new Delta<SimpleTestClass>();

            //Act
            objectUnderTest.FilterOut(t => t.Prop1NotIncluded);
            objectUnderTest.ApplySimpleProperties(simpleObject);

            //Assert
            Assert.Null(simpleObject.Prop1NotIncluded);
            Assert.Null(simpleObject.Prop1);
            Assert.Null(simpleObject.Prop2);
        }
Ejemplo n.º 10
0
        public void SimpleTestOfFunction()
        {
            //Arange
            var simpleObject = new SimpleTestClass();
            var classUnderTest = new Delta<SimpleTestClass>();

            classUnderTest.FilterOut(t => t.Prop1NotIncluded);
            classUnderTest.ObjectPropertyValues = new Dictionary<string, object>()
                                         {
                                           {"Prop2","b"}
                                         };
            //Act
            classUnderTest.ApplySimpleProperties(simpleObject);

            //Assert
            Assert.NotNull(simpleObject.Prop2);
            Assert.Equal(simpleObject.Prop2, "b");
            Assert.Null(simpleObject.Prop1NotIncluded);
        }
Ejemplo n.º 11
0
        public void GIVEN_IncompleteProperties_WHEN_DeltaApply_THEN_OnlyThoseSpecifiedApplied()
        {
            //Arange
            var author         = new Author();
            var classUnderTest = new Delta <Author>();

            classUnderTest.FilterOut(t => t.Name);
            classUnderTest.ObjectPropertyValues =
                new Dictionary <string, object>()
            {
                { "Id", 1 },
                { "DateTimeCreated", new DateTime(2016, 1, 1) }
            };
            //Act
            classUnderTest.ApplySimpleProperties(author);

            //Assert
            Assert.Equal(author.Id, 1);
            Assert.Equal(author.DateTimeCreated, new DateTime(2016, 1, 1));
            Assert.Null(author.Name);
        }
Ejemplo n.º 12
0
        public void GIVEN_RelatedResource_WHEN_DeltaApply_THEN_ValuesApplied()
        {
            //Arrange
            Setup();

            var articleAuthorRelatedProperty     = new Mock <IPropertyHandle>(MockBehavior.Strict);
            Action <object, object> actionSetter = (o, p) => { ((Article)o).Author = (Author)p; };

            articleAuthorRelatedProperty.SetupGet(o => o.SetterDelegate).Returns((Delegate)actionSetter);

            var articleAuthorRelationship = new Mock <IRelationshipMapping>(MockBehavior.Strict);

            articleAuthorRelationship.SetupGet(o => o.IsCollection).Returns(false);
            articleAuthorRelationship.SetupGet(o => o.RelationshipName).Returns("author");
            articleAuthorRelationship.SetupGet(o => o.RelatedProperty).Returns(articleAuthorRelatedProperty.Object);

            _mapping.Object.Relationships.Add(articleAuthorRelationship.Object);

            var article        = new Article();
            var classUnderTest = new Delta <Article>(_configuration.Object);

            classUnderTest.ObjectPropertyValues =
                new Dictionary <string, object>()
            {
                { "author", new Author {
                      Id = 1
                  } }
            };

            classUnderTest.Scan();

            //Act
            classUnderTest.ApplySimpleProperties(article);

            //Assert
            Assert.NotNull(article.Author);
            Assert.Equal(article.Author.Id, 1);
        }
Ejemplo n.º 13
0
        public void GIVEN_SimpleProperty_WHEN_DeltaApply_THEN_ValuesApplied()
        {
            //Arrange
            Setup();

            _mapping.Object.PropertySetters.Add("title", (o, p) => { ((Article)o).Title = (string)p; });

            var article        = new Article();
            var classUnderTest = new Delta <Article>(_configuration.Object);

            classUnderTest.ObjectPropertyValues =
                new Dictionary <string, object>()
            {
                { "title", "title value" }
            };

            classUnderTest.Scan();

            //Act
            classUnderTest.ApplySimpleProperties(article);

            //Assert
            Assert.Equal(article.Title, "title value");
        }