public void AddToListInListInvalidPositionTooSmall()
        {
            var doc = new SimpleDTOWithNestedDTO()
            {
                ListOfSimpleDTO = new List <SimpleDTO>()
                {
                    new SimpleDTO()
                    {
                        IntegerList = new List <int>()
                        {
                            1, 2, 3
                        }
                    }
                }
            };

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

            patchDoc.Add("ListOfSimpleDTO/-1/IntegerList/0", 4);

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

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

            Assert.Equal(
                "The property at path '/ListOfSimpleDTO/-1/IntegerList/0' could not be added.",
                exception.Message);
        }
        public void SerializeAndReplaceNestedObjectTest()
        {
            var doc = new SimpleDTOWithNestedDTO()
            {
                SimpleDTO = new SimpleDTO()
                {
                    IntegerValue = 5,
                    IntegerList  = new List <int>()
                    {
                        1, 2, 3
                    }
                }
            };

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

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

            patchDoc.Replace("SimpleDTO", newDTO);

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

            deserialized.ApplyTo(doc);

            Assert.Equal(1, doc.SimpleDTO.DoubleValue);
            Assert.Equal(0, doc.SimpleDTO.IntegerValue);
            Assert.Equal(null, doc.SimpleDTO.IntegerList);
        }
        public void AddToListInList()
        {
            var doc = new SimpleDTOWithNestedDTO()
            {
                ListOfSimpleDTO = new List <SimpleDTO>()
                {
                    new SimpleDTO()
                    {
                        IntegerList = new List <int>()
                        {
                            1, 2, 3
                        }
                    }
                }
            };

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

            patchDoc.Add("ListOfSimpleDTO/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.ListOfSimpleDTO[0].IntegerList);
        }
Esempio n. 4
0
        public void NestedRemoveFromEndOfList()
        {
            var doc = new SimpleDTOWithNestedDTO()
            {
                SimpleDTO = new SimpleDTO()
                {
                    IntegerList = new List <int>()
                    {
                        1, 2, 3
                    }
                }
            };

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

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

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

            deserialized.ApplyTo(doc);

            Assert.Equal(new List <int>()
            {
                1, 2
            }, doc.SimpleDTO.IntegerList);
        }
Esempio n. 5
0
        public void AddToListInListInvalidPositionTooLarge()
        {
            var doc = new SimpleDTOWithNestedDTO()
            {
                ListOfSimpleDTO = new List<SimpleDTO>()
                {
                     new SimpleDTO()
                     {
                        IntegerList = new List<int>() { 1, 2, 3 }
                     }
                }
            };
            // create patch
            JsonPatchDocument patchDoc = new JsonPatchDocument();
            patchDoc.Add("ListOfSimpleDTO/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(
                "The property at path '/ListOfSimpleDTO/20/IntegerList/0' could not be added.",
                exception.Message);
        }
Esempio n. 6
0
        public void NestedRemoveFromListInvalidPositionTooSmall()
        {
            var doc = new SimpleDTOWithNestedDTO()
            {
                SimpleDTO = new SimpleDTO()
                {
                    IntegerList = new List <int>()
                    {
                        1, 2, 3
                    }
                }
            };

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

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

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

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

            Assert.Equal(
                "For operation 'remove' on array property at path '/SimpleDTO/IntegerList/-1', the index is negative.",
                exception.Message);
        }
Esempio n. 7
0
        public void NestedCopyFromNonListToList()
        {
            var doc = new SimpleDTOWithNestedDTO()
            {
                SimpleDTO = new SimpleDTO()
                {
                    IntegerValue = 5,
                    IntegerList  = new List <int>()
                    {
                        1, 2, 3
                    }
                }
            };

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

            patchDoc.Copy("SimpleDTO/IntegerValue", "SimpleDTO/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.SimpleDTO.IntegerList);
        }
        public void NestedRemoveFromList()
        {
            var doc = new SimpleDTOWithNestedDTO()
            {
                SimpleDTO = new SimpleDTO()
                {
                    IntegerList = new List<int>() { 1, 2, 3 }
                }
            };

            // create patch
            JsonPatchDocument patchDoc = new JsonPatchDocument();
            patchDoc.Remove("SimpleDTO/IntegerList/2");

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

            deserialized.ApplyTo(doc);

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

            // create patch
            JsonPatchDocument patchDoc = new JsonPatchDocument();
            patchDoc.Remove("SimpleDTO/StringProperty");

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

            deserialized.ApplyTo(doc);

            Assert.Equal(null, doc.SimpleDTO.StringProperty);
        }
Esempio n. 10
0
        public void NestedRemove()
        {
            var doc = new SimpleDTOWithNestedDTO()
            {
                SimpleDTO = new SimpleDTO()
                {
                    StringProperty = "A"
                }
            };

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

            patchDoc.Remove("SimpleDTO/StringProperty");

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

            deserialized.ApplyTo(doc);

            Assert.Equal(null, doc.SimpleDTO.StringProperty);
        }
Esempio n. 11
0
        public void AddToListInList()
        {
            var doc = new SimpleDTOWithNestedDTO()
            {
                ListOfSimpleDTO = new List<SimpleDTO>()
                {
                     new SimpleDTO()
                     {
                         IntegerList = new List<int>() { 1, 2, 3 }
                     }
                }
            };

            // create patch
            JsonPatchDocument patchDoc = new JsonPatchDocument();
            patchDoc.Add("ListOfSimpleDTO/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.ListOfSimpleDTO[0].IntegerList);
        }
Esempio n. 12
0
        public void NestedCopy()
        {
            var doc = new SimpleDTOWithNestedDTO()
            {
                SimpleDTO = new SimpleDTO()
                {
                    StringProperty        = "A",
                    AnotherStringProperty = "B"
                }
            };

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

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

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

            deserialized.ApplyTo(doc);

            Assert.Equal("A", doc.SimpleDTO.AnotherStringProperty);
        }
        public void NestedRemoveFromListInvalidPositionTooLarge()
        {
            var doc = new SimpleDTOWithNestedDTO()
            {
                SimpleDTO = new SimpleDTO()
                {
                    IntegerList = new List<int>() { 1, 2, 3 }
                }
            };

            // create patch
            JsonPatchDocument patchDoc = new JsonPatchDocument();
            patchDoc.Remove("SimpleDTO/IntegerList/3");

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

            var exception = Assert.Throws<JsonPatchException>(() =>
            {
                deserialized.ApplyTo(doc);
            });
            Assert.Equal(
               "For operation 'remove' on array property at path '/SimpleDTO/IntegerList/3', the index is larger than the array size.",
                exception.Message);
        }
        public void SerializeAndReplaceNestedObjectTest()
        {
            var doc = new SimpleDTOWithNestedDTO()
            {
                SimpleDTO = new SimpleDTO()
                {
                    IntegerValue = 5,
                    IntegerList = new List<int>() { 1, 2, 3 }
                }
            };

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

            // create patch
            JsonPatchDocument patchDoc = new JsonPatchDocument();
            patchDoc.Replace("SimpleDTO", newDTO);

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

            deserialized.ApplyTo(doc);

            Assert.Equal(1, doc.SimpleDTO.DoubleValue);
            Assert.Equal(0, doc.SimpleDTO.IntegerValue);
            Assert.Equal(null, doc.SimpleDTO.IntegerList);
        }