public void TryTest_DoesNotThrowException_IfTestSuccessful()
        {
            var     adapter = new DynamicObjectAdapter();
            dynamic target  = new DynamicTestObject();

            target.NewProperty = "Joana";
            var segment  = "NewProperty";
            var resolver = new DefaultContractResolver();

            // Act
            var testStatus = adapter.TryTest(target, segment, resolver, "Joana", out string errorMessage);

            // Assert
            Assert.Equal("Joana", target.NewProperty);
            Assert.True(testStatus);
            Assert.True(string.IsNullOrEmpty(errorMessage), "Expected no error message");
        }
        public void TryReplace_ThrowsPropertyInvalidException_IfNewValueIsNotTheSameTypeAsInitialValue()
        {
            // Arrange
            var     adapter = new DynamicObjectAdapter();
            dynamic target  = new DynamicTestObject();

            target.NewProperty = 1;
            var segment  = "NewProperty";
            var resolver = new DefaultContractResolver();

            // Act
            var status = adapter.TryReplace(target, segment, resolver, "test", out string errorMessage);

            // Assert
            Assert.False(status);
            Assert.Equal($"The value 'test' is invalid for target location.", errorMessage);
        }
        public void TryTraverse_ThrowsPathNotFoundException_ForNonExistingProperty()
        {
            // Arrange
            var     adapter = new DynamicObjectAdapter();
            dynamic target  = new DynamicTestObject();

            target.NestedObject = new DynamicTestObject();
            var segment  = "NewProperty";
            var resolver = new DefaultContractResolver();

            // Act
            var status = adapter.TryTraverse(target.NestedObject, segment, resolver, out object nextTarget, out string errorMessage);

            // Assert
            Assert.False(status);
            Assert.Equal($"The target location specified by path segment '{segment}' was not found.", errorMessage);
        }
        public void TryTest_ThrowsJsonPatchException_IfTestFails()
        {
            // Arrange
            var     adapter = new DynamicObjectAdapter();
            dynamic target  = new DynamicTestObject();

            target.NewProperty = "Joana";
            var segment              = "NewProperty";
            var resolver             = new DefaultContractResolver();
            var expectedErrorMessage = $"The current value 'Joana' at path '{segment}' is not equal to the test value 'John'.";

            // Act
            var testStatus = adapter.TryTest(target, segment, resolver, "John", out string errorMessage);

            // Assert
            Assert.False(testStatus);
            Assert.Equal(expectedErrorMessage, errorMessage);
        }
        public void TryReplace_ReplacesPropertyValue()
        {
            // Arrange
            var     adapter = new DynamicObjectAdapter();
            dynamic target  = new DynamicTestObject();

            target.NewProperty = new object();
            var segment  = "NewProperty";
            var resolver = new DefaultContractResolver();

            // Act
            var status = adapter.TryReplace(target, segment, resolver, "new", out string errorMessage);

            // Assert
            Assert.True(status);
            Assert.Null(errorMessage);
            Assert.Equal("new", target.NewProperty);
        }
        public void TryTraverse_FindsNextTarget()
        {
            // Arrange
            var     adapter = new DynamicObjectAdapter();
            dynamic target  = new DynamicTestObject();

            target.NestedObject             = new DynamicTestObject();
            target.NestedObject.NewProperty = "A";
            var segment  = "NestedObject";
            var resolver = new DefaultContractResolver();

            // Act
            var status = adapter.TryTraverse(target, segment, resolver, out object nextTarget, out string errorMessage);

            // Assert
            Assert.True(status);
            Assert.Null(errorMessage);
            Assert.Equal(target.NestedObject, nextTarget);
        }
예제 #7
0
        public void MoveToNonExistingProperty_InDynamicObject_ShouldAddNewProperty()
        {
            // Arrange
            dynamic dynamicTestObject = new DynamicTestObject();

            dynamicTestObject.StringProperty = "A";

            var patchDoc = new JsonPatchDocument();

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

            // Act
            patchDoc.ApplyTo(dynamicTestObject);
            dynamicTestObject.TryGetValue("StringProperty", out object valueFromDictionary);

            // Assert
            Assert.Equal("A", dynamicTestObject.AnotherStringProperty);
            Assert.Null(valueFromDictionary);
        }
예제 #8
0
        public void CopyProperties_InNestedDynamicObject()
        {
            // Arrange
            dynamic dynamicTestObject = new DynamicTestObject();

            dynamicTestObject.NestedDynamicObject = new DynamicTestObject();
            dynamicTestObject.NestedDynamicObject.StringProperty        = "A";
            dynamicTestObject.NestedDynamicObject.AnotherStringProperty = "B";

            var patchDoc = new JsonPatchDocument();

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

            // Act
            patchDoc.ApplyTo(dynamicTestObject);

            // Assert
            Assert.Equal("A", dynamicTestObject.NestedDynamicObject.AnotherStringProperty);
        }
예제 #9
0
        public void RemoveNestedProperty_FromDynamicObject()
        {
            // Arrange
            dynamic dynamicTestObject = new DynamicTestObject();

            dynamicTestObject.Test             = new DynamicTestObject();
            dynamicTestObject.Test.AnotherTest = "A";

            var patchDoc = new JsonPatchDocument();

            patchDoc.Remove("Test");

            // Act
            patchDoc.ApplyTo(dynamicTestObject);
            dynamicTestObject.TryGetValue("Test", out object valueFromDictionary);

            // Assert
            Assert.Null(valueFromDictionary);
        }
예제 #10
0
        public void TestPropertyValue_FromListToNonList_InNestedTypedObject_InDynamicObject()
        {
            // Arrange
            dynamic dynamicTestObject = new DynamicTestObject();

            dynamicTestObject.Nested = new SimpleObject()
            {
                IntegerList = new List <int>()
                {
                    1, 2, 3
                }
            };

            var patchDoc = new JsonPatchDocument();

            patchDoc.Test("Nested/IntegerList/1", 2);

            // Act & Assert
            patchDoc.ApplyTo(dynamicTestObject);
        }
예제 #11
0
        public void AddResults_ShouldReplaceExistingPropertyValue_InNestedDynamicObject()
        {
            // Arrange
            dynamic dynamicTestObject = new DynamicTestObject();

            dynamicTestObject.Nested = new NestedObject();
            dynamicTestObject.Nested.DynamicProperty = new DynamicTestObject();
            dynamicTestObject.Nested.DynamicProperty.InBetweenFirst = new DynamicTestObject();
            dynamicTestObject.Nested.DynamicProperty.InBetweenFirst.InBetweenSecond = new DynamicTestObject();
            dynamicTestObject.Nested.DynamicProperty.InBetweenFirst.InBetweenSecond.StringProperty = "A";

            var patchDoc = new JsonPatchDocument();

            patchDoc.Add("/Nested/DynamicProperty/InBetweenFirst/InBetweenSecond/StringProperty", "B");

            // Act
            patchDoc.ApplyTo(dynamicTestObject);

            // Assert
            Assert.Equal("B", dynamicTestObject.Nested.DynamicProperty.InBetweenFirst.InBetweenSecond.StringProperty);
        }
예제 #12
0
        public void MovePropertyValue_FromDynamicObject_ToTypedObject()
        {
            // Arrange
            dynamic dynamicTestObject = new DynamicTestObject();

            dynamicTestObject.StringProperty = "A";
            dynamicTestObject.SimpleObject   = new SimpleObject()
            {
                AnotherStringProperty = "B"
            };

            var patchDoc = new JsonPatchDocument();

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

            // Act
            patchDoc.ApplyTo(dynamicTestObject);
            dynamicTestObject.TryGetValue("StringProperty", out object valueFromDictionary);

            // Assert
            Assert.Equal("A", dynamicTestObject.SimpleObject.AnotherStringProperty);
            Assert.Null(valueFromDictionary);
        }
        public void TryGet_GetsPropertyValue_ForExistingProperty()
        {
            // Arrange
            var     adapter  = new DynamicObjectAdapter();
            dynamic target   = new DynamicTestObject();
            var     segment  = "NewProperty";
            var     resolver = new DefaultContractResolver();

            // Act 1
            var addStatus = adapter.TryAdd(target, segment, resolver, "new", out string errorMessage);

            // Assert 1
            Assert.True(addStatus);
            Assert.Null(errorMessage);
            Assert.Equal("new", target.NewProperty);

            // Act 2
            var getStatus = adapter.TryGet(target, segment, resolver, out object getValue, out string getErrorMessage);

            // Assert 2
            Assert.True(getStatus);
            Assert.Null(getErrorMessage);
            Assert.Equal(getValue, target.NewProperty);
        }
        public void TryRemove_SetsPropertyToDefaultOrNull(object value, object expectedValue)
        {
            // Arrange
            var     adapter  = new DynamicObjectAdapter();
            dynamic target   = new DynamicTestObject();
            var     segment  = "NewProperty";
            var     resolver = new DefaultContractResolver();

            // Act 1
            var addStatus = adapter.TryAdd(target, segment, resolver, value, out string errorMessage);

            // Assert 1
            Assert.True(addStatus);
            Assert.Null(errorMessage);
            Assert.Equal(value, target.NewProperty);

            // Act 2
            var removeStatus = adapter.TryRemove(target, segment, resolver, out string removeErrorMessage);

            // Assert 2
            Assert.True(removeStatus);
            Assert.Null(removeErrorMessage);
            Assert.Equal(expectedValue, target.NewProperty);
        }