Beispiel #1
0
        public void ReplaceInListInListInvalidPosition()
        {
            var doc = new SimpleDTO()
            {
                SimpleDTOList = new List <SimpleDTO>()
                {
                    new SimpleDTO()
                    {
                        IntegerList = new List <int>()
                        {
                            1, 2, 3
                        }
                    }
                }
            };

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

            patchDoc.Replace("SimpleDTOList/0/IntegerList/10", 4);

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

            Assert.Throws <JsonPatchException>(() => { deserialized.ApplyTo(doc); });
        }
Beispiel #2
0
        public void ReplaceFullList()
        {
            var doc = new SimpleDTO()
            {
                IntegerList = new List <int>()
                {
                    1, 2, 3
                }
            };

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

            patchDoc.Replace("IntegerList", new List <int>()
            {
                4, 5, 6
            });

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

            deserialized.ApplyTo(doc);

            Assert.Equal(new List <int>()
            {
                4, 5, 6
            }, doc.IntegerList);
        }
Beispiel #3
0
        public void ReplaceInListInList()
        {
            var doc = new SimpleDTO()
            {
                SimpleDTOList = new List <SimpleDTO>()
                {
                    new SimpleDTO()
                    {
                        IntegerList = new List <int>()
                        {
                            1, 2, 3
                        }
                    }
                }
            };

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

            patchDoc.Replace("SimpleDTOList/0/IntegerList/0", 4);

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

            deserialized.ApplyTo(doc);

            Assert.Equal(4, doc.SimpleDTOList.First().IntegerList.First());
        }
        public void MoveFomListToNonList()
        {
            var doc = new SimpleDTO()
            {
                IntegerList = new List <int>()
                {
                    1, 2, 3
                }
            };

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

            patchDoc.Move("IntegerList/0", "IntegerValue");

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

            deserialized.ApplyTo(doc);

            Assert.Equal(new List <int>()
            {
                2, 3
            }, doc.IntegerList);
            Assert.Equal(1, doc.IntegerValue);
        }
Beispiel #5
0
        public void RemoveFromEndOfList()
        {
            var doc = new SimpleDTO()
            {
                IntegerList = new List <int>()
                {
                    1, 2, 3
                }
            };

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

            patchDoc.Remove("IntegerList/-");

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

            deserialized.ApplyTo(doc);

            Assert.Equal(new List <int>()
            {
                1, 2
            }, doc.IntegerList);
        }
Beispiel #6
0
        public void ReplaceNestedObjectTest()
        {
            dynamic doc = new ExpandoObject();

            doc.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 CopyInList()
        {
            var doc = new SimpleDTO()
            {
                IntegerList = new List <int>()
                {
                    1, 2, 3
                }
            };

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

            patchDoc.Copy("IntegerList/0", "IntegerList/1");

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

            deserialized.ApplyTo(doc);

            Assert.Equal(new List <int>()
            {
                1, 1, 2, 3
            }, doc.IntegerList);
        }
Beispiel #8
0
        public void NonGenericPatchDocToGenericMustSerialize()
        {
            var doc = new SimpleDTO()
            {
                StringProperty        = "A",
                AnotherStringProperty = "B"
            };

            JsonPatchDocument patchDoc = new JsonPatchDocument();

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

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

            deserialized.ApplyTo(doc);

            Assert.Equal("A", doc.AnotherStringProperty);
        }
Beispiel #9
0
        public void Remove()
        {
            var doc = new SimpleDTO()
            {
                StringProperty = "A"
            };

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

            patchDoc.Remove("StringProperty");

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

            deserialized.ApplyTo(doc);

            Assert.Equal(null, doc.StringProperty);
        }
Beispiel #10
0
        public void ReplaceInGenericList()
        {
            var doc = new SimpleDTO
            {
                IntegerGenericList = new List <int> {
                    1, 2, 3
                }
            };

            // create patch
            var patchDoc = new JsonPatchDocument <SimpleDTO>();

            patchDoc.Replace(o => o.IntegerGenericList, 5, 0);

            patchDoc.ApplyTo(doc);

            Assert.Equal(new List <int> {
                5, 2, 3
            }, doc.IntegerGenericList);
        }
        public void MoveInGenericList()
        {
            var doc = new SimpleDTO
            {
                IntegerGenericList = new List <int> {
                    1, 2, 3
                }
            };

            // create patch
            var patchDoc = new JsonPatchDocument <SimpleDTO>();

            patchDoc.Move(o => o.IntegerGenericList, 0, o => o.IntegerGenericList, 1);

            patchDoc.ApplyTo(doc);

            Assert.Equal(new List <int> {
                2, 1, 3
            }, doc.IntegerGenericList);
        }
Beispiel #12
0
        public void RemoveFromListInvalidPositionTooSmall()
        {
            var doc = new SimpleDTO()
            {
                IntegerList = new List <int>()
                {
                    1, 2, 3
                }
            };

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

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

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

            Assert.Throws <JsonPatchException>(() => { deserialized.ApplyTo(doc); });
        }
        public void Copy()
        {
            var doc = new SimpleDTO()
            {
                StringProperty        = "A",
                AnotherStringProperty = "B"
            };

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

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

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

            deserialized.ApplyTo(doc);

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

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

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

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

            Assert.Throws <JsonPatchException>(() => { deserialized.ApplyTo(doc); });
        }
Beispiel #15
0
        public void  ReplaceGuidTest()
        {
            var doc = new SimpleDTO()
            {
                GuidValue = Guid.NewGuid()
            };

            var newGuid = Guid.NewGuid();
            // create patch
            JsonPatchDocument patchDoc = new JsonPatchDocument();

            patchDoc.Replace("GuidValue", newGuid);

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

            deserizalized.ApplyTo(doc);

            Assert.Equal(newGuid, doc.GuidValue);
        }
 public SimpleDTOWithNestedDTO()
 {
     NestedDTO       = new NestedDTO();
     SimpleDTO       = new SimpleDTO();
     ListOfSimpleDTO = new List <SimpleDTO>();
 }