public void Returns_Unsuccessful_When_Test_Fails()
        {
            // Given
            var target = new ExampleTarget
            {
                ValueType = 1234,
                Child     = new ExampleTargetChild
                {
                    ChildName = "I am a child"
                }
            };

            // When
            var result = _jsonPatch.Patch(
                "[" +
                "   { \"op\": \"test\", \"path\" : \"/ValueType\", \"value\" : 909090}," +
                "   { \"op\": \"replace\", \"path\" : \"/ValueType\", \"value\" : 999}" +
                "]",
                target);

            // Then
            result.Succeeded.ShouldBeFalse();
            result.FailureReason.ShouldEqual(JsonPatchFailureReason.TestFailed);
            result.Message.ShouldContain("Test operation failed. 'ValueType' property did not match");
        }
        public void Returns_Unsuccessful_When_Remove_Operation_Fails()
        {
            // Given
            var target = new ExampleTarget
            {
                ChildList = new List <ExampleTargetChild>
                {
                    new ExampleTargetChild {
                        ChildName = "Child 1"
                    },
                    new ExampleTargetChild {
                        ChildName = "Child 2"
                    },
                }
            };

            // When
            var result = _jsonPatch.Patch(
                "[" +
                "   { \"op\": \"remove\", \"path\" : \"/ChildList/89\" }" +
                "]",
                target);

            // Then
            result.Succeeded.ShouldBeFalse();
            result.Message.ShouldEqual("Could not find item 89 in the collection");
            result.FailureReason.ShouldEqual(JsonPatchFailureReason.OperationFailed);
        }
        public void Performs_Operations_On_Collections()
        {
            // Given
            var target = new ExampleTarget
            {
                ChildList = new List <ExampleTargetChild>
                {
                    new ExampleTargetChild {
                        ChildName = "Child 1"
                    },
                    new ExampleTargetChild {
                        ChildName = "Child 2"
                    },
                    new ExampleTargetChild {
                        ChildName = "Child 3"
                    },
                }
            };

            // When
            var result = _jsonPatch.Patch(
                "[" +
                "   { \"op\": \"move\", \"path\" : \"/Child\", \"from\" : \"/ChildList/2\"}," +
                "   { \"op\": \"replace\", \"path\" : \"/ChildList/1/ChildName\", \"value\" : \"Replaced\"}," +
                "   { \"op\": \"remove\", \"path\" : \"/ChildList/0\" }" +
                "]",
                target);

            // Then
            target.Child.ChildName.ShouldEqual("Child 3");
            target.ChildList.Count.ShouldEqual(1);
            target.ChildList[0].ChildName.ShouldEqual("Replaced");
            result.Succeeded.ShouldBeTrue();
        }
        public void Performs_Remove_Operations()
        {
            // Given
            var target = new ExampleTarget
            {
                ChildList = new List <ExampleTargetChild>
                {
                    new ExampleTargetChild {
                        ChildName = "Child 1"
                    },
                    new ExampleTargetChild {
                        ChildName = "Child 2"
                    },
                }
            };

            // When
            var result = _jsonPatch.Patch(
                "[" +
                "   { \"op\": \"remove\", \"path\" : \"/ChildList/0\" }" +
                "]",
                target);

            // Then
            target.ChildList.Count.ShouldEqual(1);
            target.ChildList[0].ChildName.ShouldEqual("Child 2");
            result.Succeeded.ShouldBeTrue();
        }
        public void Test_Throws_If_Complex_Objects_Do_Not_Match_When_Serialised()
        {
            // Given
            var example = new ExampleTarget
            {
                Child = new ExampleTargetChild
                {
                    ChildName = "Child Property"
                }
            };

            var path = new JsonPatchPath
            {
                TargetPropertyName = "Child",
                TargetObject       = example
            };

            // When
            var result = _executor.Test(path, new ExampleTargetChild
            {
                ChildName = "No Matching Property"
            });

            // Then
            result.Succeeded.ShouldBeFalse();
            result.Message.ShouldEqual("Test operation failed. 'Child' property did not match");
        }
        public void Performs_Add_Operations()
        {
            // Given
            var target = new ExampleTarget
            {
                ChildList = new List <ExampleTargetChild>
                {
                    new ExampleTargetChild {
                        ChildName = "Child 1"
                    },
                    new ExampleTargetChild {
                        ChildName = "Child 2"
                    },
                }
            };

            // When
            var result = _jsonPatch.Patch(
                "[" +
                "   { \"op\": \"add\", \"path\" : \"/ChildList/1\", \"value\" : { \"ChildName\" : \"New Entry\" }}" +
                "]",
                target);

            // Then
            target.ChildList.Count.ShouldEqual(3);
            target.ChildList[0].ChildName.ShouldEqual("Child 1");
            target.ChildList[1].ChildName.ShouldEqual("New Entry");
            target.ChildList[2].ChildName.ShouldEqual("Child 2");
            result.Succeeded.ShouldBeTrue();
        }
        public void Returns_Unsuccessful_When_Add_Operation_Fails()
        {
            // Given
            var target = new ExampleTarget
            {
                ChildList = new List <ExampleTargetChild>
                {
                    new ExampleTargetChild {
                        ChildName = "Child 1"
                    },
                    new ExampleTargetChild {
                        ChildName = "Child 2"
                    },
                }
            };

            // When
            var result = _jsonPatch.Patch(
                "[" +
                "   { \"op\": \"add\", \"path\" : \"/ChildList/1\", \"value\" : 789 }" +
                "]",
                target);

            // Then
            result.Succeeded.ShouldBeFalse();
            result.Message.ShouldEqual("The value could not be converted to type ExampleTargetChild");
            result.FailureReason.ShouldEqual(JsonPatchFailureReason.OperationFailed);
        }
        public void Adds_Items_To_End_Of_Collection_If_Indexer_Is_Minus()
        {
            // Given
            var exampleTarget = new ExampleTarget
            {
                IntList = new List <int> {
                    3, 4, 5
                }
            };

            var path = new JsonPatchPath
            {
                TargetObject       = exampleTarget.IntList,
                IsCollection       = true,
                TargetPropertyName = "-"
            };

            // When
            var result = _executor.Add(path, 999);

            // Then
            exampleTarget.IntList[0].ShouldEqual(3);
            exampleTarget.IntList[1].ShouldEqual(4);
            exampleTarget.IntList[2].ShouldEqual(5);
            exampleTarget.IntList[3].ShouldEqual(999);
            result.Succeeded.ShouldBeTrue();
        }
        public void Replaces_Reference_Types()
        {
            // Given
            var exampleTarget = new ExampleTarget
            {
                Child = new ExampleTargetChild
                {
                    ChildName = "Old Child"
                }
            };

            var path = new JsonPatchPath
            {
                TargetObject       = exampleTarget,
                IsCollection       = false,
                TargetPropertyName = "Child"
            };

            // When
            var result = _executor.Replace(path, new Dictionary <string, object>
            {
                { "ChildName", "New Child" }
            });

            // Then
            exampleTarget.Child.ChildName.ShouldEqual("New Child");
            result.Succeeded.ShouldBeTrue();
        }
Esempio n. 10
0
        public void Returns_Null_With_Error_If_Target_Is_Not_Collection_But_Last_Part_Of_Path_Is_Indexer()
        {
            // Given
            var target = new ExampleTarget
            {
                ChildList = new List <ExampleTargetChild>
                {
                    new ExampleTargetChild {
                        ChildName = "Item 1"
                    },
                    new ExampleTargetChild {
                        ChildName = "Item 2"
                    },
                    new ExampleTargetChild {
                        ChildName = "Item 3"
                    },
                }
            };

            // When
            var result = _pathParser.ParsePath("/ChildList/1/ChildName/2", target);

            // Then
            result.Path.ShouldBeNull();
            result.Error.ShouldEqual("Could not access path '/ChildList/1/ChildName/2' in target object. Parent object for '2' is not a collection");
        }
        public void Copy_Performs_Add_With_ValueTypes()
        {
            // Given
            var exampleTarget = new ExampleTarget
            {
                IntList = new List <int> {
                    1, 2, 3, 4, 5
                }
            };

            var from = new JsonPatchPath
            {
                TargetObject       = exampleTarget.IntList,
                IsCollection       = true,
                TargetPropertyName = "1"
            };

            var path = new JsonPatchPath
            {
                TargetObject       = exampleTarget.IntList,
                IsCollection       = true,
                TargetPropertyName = "4"
            };

            // When
            var result = _executor.Copy(from, path);

            // Then
            result.Succeeded.ShouldBeTrue();
            exampleTarget.IntList
            .SequenceEqual(new[] { 1, 2, 3, 4, 2, 5 })
            .ShouldBeTrue();
        }
        public void Move_Performs_Copy_Of_From_To_Path_Followed_By_Remove_Of_From_Value()
        {
            // Given
            var exampleTarget = new ExampleTarget
            {
                Child = new ExampleTargetChild {
                    ChildName = "Original Child"
                },
                Name = "Old Name"
            };

            var from = new JsonPatchPath
            {
                TargetObject       = exampleTarget.Child,
                TargetPropertyName = "ChildName"
            };

            var path = new JsonPatchPath
            {
                TargetObject       = exampleTarget,
                TargetPropertyName = "Name"
            };

            // When
            var result = _executor.Move(from, path);

            // Then
            exampleTarget.Name.ShouldEqual("Original Child");
            exampleTarget.Child.ChildName.ShouldBeNull();
            result.Succeeded.ShouldBeTrue();
        }
Esempio n. 13
0
        public void Finds_Objects_With_Collection_Indexer_In_Path()
        {
            // Given
            var target = new ExampleTarget
            {
                ChildList = new List <ExampleTargetChild>
                {
                    new ExampleTargetChild {
                        ChildName = "Item 1"
                    },
                    new ExampleTargetChild {
                        ChildName = "Item 2"
                    },
                    new ExampleTargetChild {
                        ChildName = "Item 3"
                    },
                }
            };

            // When
            var result = _pathParser.ParsePath("/ChildList/1/ChildName", target);

            // Then
            result.Path.IsCollection.ShouldBeFalse();
            result.Path.TargetObject.ShouldEqual(target.ChildList[1]);
            result.Path.TargetPropertyName.ShouldEqual("ChildName");
        }
        public void Test_Returns_True_If_Complex_Objects_Match_When_Serialised()
        {
            // Given
            var example = new ExampleTarget
            {
                Child = new ExampleTargetChild
                {
                    ChildName = "Child Property"
                }
            };

            var path = new JsonPatchPath
            {
                TargetPropertyName = "Child",
                TargetObject       = example
            };

            // When
            var result = _executor.Test(path, new ExampleTargetChild
            {
                ChildName = "Child Property"
            });

            // Then
            result.Succeeded.ShouldBeTrue();
        }
        public void Replace_Returns_False_If_Cannot_Cast_To_Original_Property_Type()
        {
            // Given
            var exampleTarget = new ExampleTarget
            {
                Child = new ExampleTargetChild
                {
                    ChildName = "Old Child"
                }
            };

            var path = new JsonPatchPath
            {
                TargetObject       = exampleTarget,
                IsCollection       = false,
                TargetPropertyName = "Child"
            };

            // When
            var result = _executor.Replace(path, "This is an invalid value for the Child Type");

            // Then
            result.Succeeded.ShouldBeFalse();
            result.Message.ShouldEqual("The value could not be converted to type ExampleTargetChild");
        }
        public void Replaces_Value_Types_In_Collections()
        {
            // Given
            var exampleTarget = new ExampleTarget
            {
                IntList = new List <int> {
                    3, 4, 5
                }
            };

            var path = new JsonPatchPath
            {
                TargetObject       = exampleTarget.IntList,
                IsCollection       = true,
                TargetPropertyName = "1"
            };

            // When
            var result = _executor.Replace(path, 999);

            // Then
            exampleTarget.IntList[0].ShouldEqual(3);
            exampleTarget.IntList[1].ShouldEqual(999);
            exampleTarget.IntList[2].ShouldEqual(5);
            result.Succeeded.ShouldBeTrue();
        }
Esempio n. 17
0
        public void Returns_Null_With_Error_If_Target_Field_Has_No_Setter()
        {
            // Given
            var target = new ExampleTarget();

            // When
            var result = _pathParser.ParsePath("/CantSetMe", target);

            // Then
            result.Path.ShouldBeNull();
            result.Error.ShouldEqual("Property '/CantSetMe' on target object cannot be set");
        }
Esempio n. 18
0
        public void Returns_Null_With_Error_If_Target_Field_Does_Not_Exist()
        {
            // Given
            var target = new ExampleTarget();

            // When
            var result = _pathParser.ParsePath("/DoesntExist", target);

            // Then
            result.Path.ShouldBeNull();
            result.Error.ShouldEqual("Could not find path '/DoesntExist' in target object");
        }
Esempio n. 19
0
        public void Returns_Null_With_Error_If_Path_To_Nested_Child_Is_Null()
        {
            // Given
            var target = new ExampleTarget();

            // When
            var result = _pathParser.ParsePath("/Child/ChildName", target);

            // Then
            result.Path.ShouldBeNull();
            result.Error.ShouldEqual("Could not access path '/Child/ChildName' in target object. 'Child' is null");
        }
Esempio n. 20
0
        public void Find_Simple_Paths()
        {
            // Given
            var target = new ExampleTarget();

            // When
            var result = _pathParser.ParsePath("/Name", target);

            // Then
            result.Path.IsCollection.ShouldBeFalse();
            result.Path.TargetObject.ShouldEqual(target);
            result.Path.TargetPropertyName.ShouldEqual("Name");
        }
Esempio n. 21
0
        public void Returns_Null_With_Error_If_Indexer_In_Path_But_Target_Is_Not_Collection()
        {
            // Given
            var target = new ExampleTarget {
                Name = "Hello"
            };

            // When
            var result = _pathParser.ParsePath("/Name/1/Something", target);

            // Then
            result.Path.ShouldBeNull();
            result.Error.ShouldEqual("Could not access path '/Name/1/Something' in target object. Parent object for '1' is not a collection");
        }
        public void Copy_Performs_Add_With_Value_Of_Object_In_From_Path()
        {
            // Given
            var exampleTarget = new ExampleTarget
            {
                Child = new ExampleTargetChild {
                    ChildName = "Original Child"
                },
                ChildList = new List <ExampleTargetChild>
                {
                    new ExampleTargetChild {
                        ChildName = "Nested Child 1"
                    },
                    new ExampleTargetChild {
                        ChildName = "Nested Child 2"
                    },
                    new ExampleTargetChild {
                        ChildName = "Nested Child 3"
                    },
                }
            };

            var from = new JsonPatchPath
            {
                TargetObject       = exampleTarget,
                TargetPropertyName = "Child"
            };

            var path = new JsonPatchPath
            {
                TargetObject       = exampleTarget.ChildList,
                IsCollection       = true,
                TargetPropertyName = "1"
            };

            // When
            var result = _executor.Copy(from, path);

            // Then
            result.Succeeded.ShouldBeTrue();
            exampleTarget.Child.ChildName.ShouldEqual("Original Child");
            exampleTarget.ChildList[0].ChildName.ShouldEqual("Nested Child 1");
            exampleTarget.ChildList[1].ChildName.ShouldEqual("Original Child");
            exampleTarget.ChildList[2].ChildName.ShouldEqual("Nested Child 2");
            exampleTarget.ChildList[3].ChildName.ShouldEqual("Nested Child 3");
        }
        public void Remove_Operation_Sets_Default_Value_If_Target_Field_Not_Nullable()
        {
            // Given
            var target = new ExampleTarget {
                ValueType = 999
            };
            var path = new JsonPatchPath {
                TargetObject = target, TargetPropertyName = "ValueType"
            };

            // When
            var result = _executor.Remove(path);

            // Then
            target.ValueType.ShouldEqual(default(int));
            result.Succeeded.ShouldBeTrue();
        }
        public void Remove_Operation_Nulls_Field()
        {
            // Given
            var target = new ExampleTarget {
                Name = "Bob the Builder"
            };
            var path = new JsonPatchPath {
                TargetObject = target, TargetPropertyName = "Name"
            };

            // When
            var result = _executor.Remove(path);

            // Then
            target.Name.ShouldBeNull();
            result.Succeeded.ShouldBeTrue();
        }
        public void Move_Removes_Items_From_Collection()
        {
            // Given
            var exampleTarget = new ExampleTarget
            {
                Child = new ExampleTargetChild {
                    ChildName = "Original Child"
                },
                ChildList = new List <ExampleTargetChild>
                {
                    new ExampleTargetChild {
                        ChildName = "Nested Child 1"
                    },
                    new ExampleTargetChild {
                        ChildName = "Nested Child 2"
                    },
                    new ExampleTargetChild {
                        ChildName = "Nested Child 3"
                    },
                }
            };

            var from = new JsonPatchPath
            {
                TargetObject       = exampleTarget.ChildList,
                IsCollection       = true,
                TargetPropertyName = "0"
            };

            var path = new JsonPatchPath
            {
                TargetObject       = exampleTarget,
                TargetPropertyName = "Child"
            };

            // When
            var result = _executor.Move(from, path);

            // Then
            exampleTarget.Child.ChildName.ShouldEqual("Nested Child 1");
            exampleTarget.ChildList[0].ChildName.ShouldEqual("Nested Child 2");
            exampleTarget.ChildList[1].ChildName.ShouldEqual("Nested Child 3");
            exampleTarget.ChildList.Count.ShouldEqual(2);
            result.Succeeded.ShouldBeTrue();
        }
Esempio n. 26
0
        public void Returns_Collection_And_Indexer_If_Minus_Passed()
        {
            // Given
            var target = new ExampleTarget
            {
                StringList = new List <string> {
                    "foo", "bar", "baz"
                }
            };

            // When
            var result = _pathParser.ParsePath("/StringList/-", target);

            // Then
            result.Path.IsCollection.ShouldBeTrue();
            result.Path.TargetObject.ShouldEqual(target.StringList);
            result.Path.TargetPropertyName.ShouldEqual("-");
        }
Esempio n. 27
0
        public void Refers_To_Items_In_Collections()
        {
            // Given
            var target = new ExampleTarget
            {
                StringList = new List <string> {
                    "foo", "bar", "baz"
                }
            };

            // When
            var result = _pathParser.ParsePath("/StringList/1", target);

            // Then
            result.Path.IsCollection.ShouldBeTrue();
            result.Path.TargetObject.ShouldEqual(target.StringList);
            result.Path.TargetPropertyName.ShouldEqual("1");
        }
Esempio n. 28
0
        public void Finds_Nested_Fields()
        {
            // Given
            var target = new ExampleTarget
            {
                Child = new ExampleTargetChild {
                    ChildName = "Peter Pan "
                }
            };

            // When
            var result = _pathParser.ParsePath("/Child/ChildName", target);

            // Then
            result.Path.IsCollection.ShouldBeFalse();
            result.Path.TargetObject.ShouldEqual(target.Child);
            result.Path.TargetPropertyName.ShouldEqual("ChildName");
        }
        public void Test_Returns_False_If_Collections_No_not_Match_When_Serialised()
        {
            // Given
            var exampleTarget = new ExampleTarget
            {
                ChildList = new List <ExampleTargetChild>
                {
                    new ExampleTargetChild {
                        ChildName = "Child 1"
                    },
                    new ExampleTargetChild {
                        ChildName = "Child 2"
                    },
                    new ExampleTargetChild {
                        ChildName = "Child 3"
                    },
                }
            };

            var path = new JsonPatchPath
            {
                TargetPropertyName = "ChildList",
                TargetObject       = exampleTarget
            };

            // When
            var result = _executor.Test(path, new[]
            {
                new ExampleTargetChild {
                    ChildName = "Child 1"
                },
                new ExampleTargetChild {
                    ChildName = "Child 3"
                },
                new ExampleTargetChild {
                    ChildName = "Child 2"
                },
            });

            // Then
            result.Succeeded.ShouldBeFalse();
            result.Message.ShouldEqual("Test operation failed. 'ChildList' property did not match");
        }
        public void Test_Returns_True_If_Collections_Match_When_Serialised()
        {
            // Given
            var exampleTarget = new ExampleTarget
            {
                ChildList = new List <ExampleTargetChild>
                {
                    new ExampleTargetChild {
                        ChildName = "Child 1"
                    },
                    new ExampleTargetChild {
                        ChildName = "Child 2"
                    },
                    new ExampleTargetChild {
                        ChildName = "Child 3"
                    },
                }
            };

            var path = new JsonPatchPath
            {
                TargetPropertyName = "ChildList",
                TargetObject       = exampleTarget
            };

            // When
            var result = _executor.Test(path, new[]
            {
                new ExampleTargetChild {
                    ChildName = "Child 1"
                },
                new ExampleTargetChild {
                    ChildName = "Child 2"
                },
                new ExampleTargetChild {
                    ChildName = "Child 3"
                },
            });

            // Then
            result.Succeeded.ShouldBeTrue();
        }