public void SerializeAndReplaceNestedObjectTest()
        {
            var doc = new SimpleObjectWithNestedObject()
            {
                SimpleObject = new SimpleObject()
                {
                    IntegerValue = 5,
                    IntegerList  = new List <int>()
                    {
                        1, 2, 3
                    }
                }
            };

            var newDTO = new SimpleObject()
            {
                DoubleValue = 1
            };

            // create patch
            var patchDoc = new JsonPatchDocument();

            patchDoc.Replace("SimpleObject", newDTO);

            // serialize & deserialize
            var serialized   = JsonConvert.SerializeObject(patchDoc);
            var deserialized = JsonConvert.DeserializeObject <JsonPatchDocument>(serialized);

            deserialized.ApplyTo(doc);

            Assert.Equal(1, doc.SimpleObject.DoubleValue);
            Assert.Equal(0, doc.SimpleObject.IntegerValue);
            Assert.Null(doc.SimpleObject.IntegerList);
        }
        public void NestedRemoveFromListInvalidPositionTooSmall()
        {
            var doc = new SimpleObjectWithNestedObject()
            {
                SimpleObject = new SimpleObject()
                {
                    IntegerList = new List <int>()
                    {
                        1, 2, 3
                    }
                }
            };

            // create patch
            var patchDoc = new JsonPatchDocument();

            patchDoc.Remove("SimpleObject/IntegerList/-1");

            var serialized   = JsonConvert.SerializeObject(patchDoc);
            var deserialized = JsonConvert.DeserializeObject <JsonPatchDocument>(serialized);

            var exception = Assert.Throws <JsonPatchException>(() =>
            {
                deserialized.ApplyTo(doc);
            });

            Assert.Equal(
                string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "-1"),
                exception.Message);
        }
        public void NestedRemoveFromEndOfList()
        {
            var doc = new SimpleObjectWithNestedObject()
            {
                SimpleObject = new SimpleObject()
                {
                    IntegerList = new List <int>()
                    {
                        1, 2, 3
                    }
                }
            };

            // create patch
            var patchDoc = new JsonPatchDocument();

            patchDoc.Remove("SimpleObject/IntegerList/-");

            var serialized   = JsonConvert.SerializeObject(patchDoc);
            var deserialized = JsonConvert.DeserializeObject <JsonPatchDocument>(serialized);

            deserialized.ApplyTo(doc);

            Assert.Equal(new List <int>()
            {
                1, 2
            }, doc.SimpleObject.IntegerList);
        }
        public void AddToListInList()
        {
            var doc = new SimpleObjectWithNestedObject()
            {
                ListOfSimpleObject = new List <SimpleObject>()
                {
                    new SimpleObject()
                    {
                        IntegerList = new List <int>()
                        {
                            1, 2, 3
                        }
                    }
                }
            };

            // create patch
            var patchDoc = new JsonPatchDocument();

            patchDoc.Add("ListOfSimpleObject/0/IntegerList/0", 4);

            var serialized   = JsonConvert.SerializeObject(patchDoc);
            var deserialized = JsonConvert.DeserializeObject <JsonPatchDocument>(serialized);

            deserialized.ApplyTo(doc);
            Assert.Equal(new List <int>()
            {
                4, 1, 2, 3
            }, doc.ListOfSimpleObject[0].IntegerList);
        }
        public void AddToListInListInvalidPositionTooLarge()
        {
            var doc = new SimpleObjectWithNestedObject()
            {
                ListOfSimpleObject = new List <SimpleObject>()
                {
                    new SimpleObject()
                    {
                        IntegerList = new List <int>()
                        {
                            1, 2, 3
                        }
                    }
                }
            };
            // create patch
            var patchDoc = new JsonPatchDocument();

            patchDoc.Add("ListOfSimpleObject/20/IntegerList/0", 4);

            var serialized   = JsonConvert.SerializeObject(patchDoc);
            var deserialized = JsonConvert.DeserializeObject <JsonPatchDocument>(serialized);

            var exception = Assert.Throws <JsonPatchException>(() =>
            {
                deserialized.ApplyTo(doc);
            });

            Assert.Equal(
                string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "20"),
                exception.Message);
        }
        public void NestedCopyFromNonListToList()
        {
            var doc = new SimpleObjectWithNestedObject()
            {
                SimpleObject = new SimpleObject()
                {
                    IntegerValue = 5,
                    IntegerList  = new List <int>()
                    {
                        1, 2, 3
                    }
                }
            };

            // create patch
            var patchDoc = new JsonPatchDocument();

            patchDoc.Copy("SimpleObject/IntegerValue", "SimpleObject/IntegerList/0");
            var serialized   = JsonConvert.SerializeObject(patchDoc);
            var deserialized = JsonConvert.DeserializeObject <JsonPatchDocument>(serialized);

            deserialized.ApplyTo(doc);

            Assert.Equal(new List <int>()
            {
                5, 1, 2, 3
            }, doc.SimpleObject.IntegerList);
        }
        public void NestedRemove()
        {
            var doc = new SimpleObjectWithNestedObject()
            {
                SimpleObject = new SimpleObject()
                {
                    StringProperty = "A"
                }
            };

            // create patch
            var patchDoc = new JsonPatchDocument();

            patchDoc.Remove("SimpleObject/StringProperty");

            var serialized   = JsonConvert.SerializeObject(patchDoc);
            var deserialized = JsonConvert.DeserializeObject <JsonPatchDocument>(serialized);

            deserialized.ApplyTo(doc);

            Assert.Null(doc.SimpleObject.StringProperty);
        }
        public void NestedCopy()
        {
            var doc = new SimpleObjectWithNestedObject()
            {
                SimpleObject = new SimpleObject()
                {
                    StringProperty        = "A",
                    AnotherStringProperty = "B"
                }
            };

            // create patch
            var patchDoc = new JsonPatchDocument();

            patchDoc.Copy("SimpleObject/StringProperty", "SimpleObject/AnotherStringProperty");

            var serialized   = JsonConvert.SerializeObject(patchDoc);
            var deserialized = JsonConvert.DeserializeObject <JsonPatchDocument>(serialized);

            deserialized.ApplyTo(doc);

            Assert.Equal("A", doc.SimpleObject.AnotherStringProperty);
        }