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);
        }
Ejemplo n.º 2
0
        public void NestedCopyToEndOfList()
        {
            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/-");

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

            deserialized.ApplyTo(doc);

            Assert.Equal(new List <int>()
            {
                1, 2, 3, 5
            }, doc.SimpleDTO.IntegerList);
        }
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
0
        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);
        }
Ejemplo n.º 6
0
        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);
        }
Ejemplo n.º 7
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);
        }
Ejemplo n.º 8
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);
        }
        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);
        }
Ejemplo n.º 11
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);
        }
Ejemplo 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);
        }
Ejemplo n.º 13
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);
        }
        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);
        }
Ejemplo n.º 15
0
        public void NestedCopyToEndOfList()
        {
            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/-");

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

            Assert.Equal(new List<int>() { 1, 2, 3, 5 }, doc.SimpleDTO.IntegerList);
        }
Ejemplo n.º 16
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);
        }