From e453fafad52649daff689c24fa2374a143f43654 Mon Sep 17 00:00:00 2001 From: Jass Bagga Date: Thu, 26 Oct 2017 12:51:06 -0700 Subject: [PATCH] Reorganize tests (#117) Addresses #105 --- .../CustomNamingStrategyTests.cs | 153 ++ .../Dynamic/AddOperationTests.cs | 553 ---- .../Dynamic/AddTypedOperationTests.cs | 121 - .../Dynamic/CopyOperationTests.cs | 245 -- .../Dynamic/CopyTypedOperationTests.cs | 268 -- .../Dynamic/CustomNamingStrategyTests.cs | 154 -- .../Dynamic/InheritedObject.cs | 10 - .../Dynamic/MoveOperationTests.cs | 333 --- .../Dynamic/MoveTypedOperationTests.cs | 138 - .../Dynamic/PatchDocumentTests.cs | 95 - .../Dynamic/RemoveOperationTests.cs | 313 --- .../Dynamic/RemoveTypedOperationTests.cs | 244 -- .../Dynamic/ReplaceOperationTests.cs | 206 -- .../Dynamic/ReplaceTypedOperationTests.cs | 191 -- .../Dynamic/SimpleObject.cs | 21 - .../Dynamic/SimpleObjectWithNestedObject.cs | 22 - .../AnonymousObjectIntegrationTest.cs | 190 ++ .../DictionaryIntegrationTest.cs | 319 +++ ...est.cs => DynamicObjectIntegrationTest.cs} | 141 +- .../ExpandoObjectIntegrationTest.cs | 327 +++ .../IntegrationTests/ListIntegrationTest.cs | 366 +++ .../NestedObjectIntegrationTest.cs | 342 +++ .../SimpleObjectIntegrationTest.cs | 128 + .../DictionaryAdapterTest.cs | 34 +- .../DynamicObjectAdapterTest.cs | 12 +- .../{Adapters => Internal}/ListAdapterTest.cs | 40 +- .../{ => Internal}/ObjectVisitorTest.cs | 10 +- .../{ => Internal}/ParsedPathTests.cs | 7 +- .../{Adapters => Internal}/PocoAdapterTest.cs | 0 .../JsonPatchDocumentGetPathTest.cs | 4 +- ...nPatchDocumentJsonPropertyAttributeTest.cs | 271 +- .../JsonPatchDocumentTest.cs | 171 +- .../JsonPropertyComplexNameObject.cs | 16 - .../JsonPropertyObject.cs | 13 - .../JsonPropertyWithAnotherNameObject.cs | 10 - .../JsonPropertyWithInheritanceObject.cs | 15 - .../NestedObject.cs | 10 - .../NestedObjectTests.cs | 2272 ---------------- .../ObjectAdapterTests.cs | 2331 ----------------- ...mpleObjectWithNestedObjectWithNullCheck.cs | 15 - .../SimpleObjectWithNullCheck.cs | 30 - .../TestObjectModels/Customer.cs | 17 + .../DynamicTestObject.cs | 2 +- .../{ => TestObjectModels}/InheritedObject.cs | 0 .../NestedObject.cs | 4 +- .../{ => TestObjectModels}/SimpleObject.cs | 2 + .../SimpleObjectWithNestedObject.cs | 0 47 files changed, 2091 insertions(+), 8075 deletions(-) create mode 100644 test/Microsoft.AspNetCore.JsonPatch.Test/CustomNamingStrategyTests.cs delete mode 100644 test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/AddOperationTests.cs delete mode 100644 test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/AddTypedOperationTests.cs delete mode 100644 test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/CopyOperationTests.cs delete mode 100644 test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/CopyTypedOperationTests.cs delete mode 100644 test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/CustomNamingStrategyTests.cs delete mode 100644 test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/InheritedObject.cs delete mode 100644 test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/MoveOperationTests.cs delete mode 100644 test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/MoveTypedOperationTests.cs delete mode 100644 test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/PatchDocumentTests.cs delete mode 100644 test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/RemoveOperationTests.cs delete mode 100644 test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/RemoveTypedOperationTests.cs delete mode 100644 test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/ReplaceOperationTests.cs delete mode 100644 test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/ReplaceTypedOperationTests.cs delete mode 100644 test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/SimpleObject.cs delete mode 100644 test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/SimpleObjectWithNestedObject.cs create mode 100644 test/Microsoft.AspNetCore.JsonPatch.Test/IntegrationTests/AnonymousObjectIntegrationTest.cs create mode 100644 test/Microsoft.AspNetCore.JsonPatch.Test/IntegrationTests/DictionaryIntegrationTest.cs rename test/Microsoft.AspNetCore.JsonPatch.Test/IntegrationTests/{DynamicObjectTest.cs => DynamicObjectIntegrationTest.cs} (58%) create mode 100644 test/Microsoft.AspNetCore.JsonPatch.Test/IntegrationTests/ExpandoObjectIntegrationTest.cs create mode 100644 test/Microsoft.AspNetCore.JsonPatch.Test/IntegrationTests/ListIntegrationTest.cs create mode 100644 test/Microsoft.AspNetCore.JsonPatch.Test/IntegrationTests/NestedObjectIntegrationTest.cs create mode 100644 test/Microsoft.AspNetCore.JsonPatch.Test/IntegrationTests/SimpleObjectIntegrationTest.cs rename test/Microsoft.AspNetCore.JsonPatch.Test/{Adapters => Internal}/DictionaryAdapterTest.cs (91%) rename test/Microsoft.AspNetCore.JsonPatch.Test/{Adapters => Internal}/DynamicObjectAdapterTest.cs (97%) rename test/Microsoft.AspNetCore.JsonPatch.Test/{Adapters => Internal}/ListAdapterTest.cs (91%) rename test/Microsoft.AspNetCore.JsonPatch.Test/{ => Internal}/ObjectVisitorTest.cs (95%) rename test/Microsoft.AspNetCore.JsonPatch.Test/{ => Internal}/ParsedPathTests.cs (91%) rename test/Microsoft.AspNetCore.JsonPatch.Test/{Adapters => Internal}/PocoAdapterTest.cs (100%) delete mode 100644 test/Microsoft.AspNetCore.JsonPatch.Test/JsonPropertyComplexNameObject.cs delete mode 100644 test/Microsoft.AspNetCore.JsonPatch.Test/JsonPropertyObject.cs delete mode 100644 test/Microsoft.AspNetCore.JsonPatch.Test/JsonPropertyWithAnotherNameObject.cs delete mode 100644 test/Microsoft.AspNetCore.JsonPatch.Test/JsonPropertyWithInheritanceObject.cs delete mode 100644 test/Microsoft.AspNetCore.JsonPatch.Test/NestedObject.cs delete mode 100644 test/Microsoft.AspNetCore.JsonPatch.Test/NestedObjectTests.cs delete mode 100644 test/Microsoft.AspNetCore.JsonPatch.Test/ObjectAdapterTests.cs delete mode 100644 test/Microsoft.AspNetCore.JsonPatch.Test/SimpleObjectWithNestedObjectWithNullCheck.cs delete mode 100644 test/Microsoft.AspNetCore.JsonPatch.Test/SimpleObjectWithNullCheck.cs create mode 100644 test/Microsoft.AspNetCore.JsonPatch.Test/TestObjectModels/Customer.cs rename test/Microsoft.AspNetCore.JsonPatch.Test/{ => TestObjectModels}/DynamicTestObject.cs (98%) rename test/Microsoft.AspNetCore.JsonPatch.Test/{ => TestObjectModels}/InheritedObject.cs (100%) rename test/Microsoft.AspNetCore.JsonPatch.Test/{Dynamic => TestObjectModels}/NestedObject.cs (85%) rename test/Microsoft.AspNetCore.JsonPatch.Test/{ => TestObjectModels}/SimpleObject.cs (86%) rename test/Microsoft.AspNetCore.JsonPatch.Test/{ => TestObjectModels}/SimpleObjectWithNestedObject.cs (100%) diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/CustomNamingStrategyTests.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/CustomNamingStrategyTests.cs new file mode 100644 index 0000000000..ebc45874d9 --- /dev/null +++ b/test/Microsoft.AspNetCore.JsonPatch.Test/CustomNamingStrategyTests.cs @@ -0,0 +1,153 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System.Collections.Generic; +using System.Dynamic; +using Newtonsoft.Json.Serialization; +using Xunit; + +namespace Microsoft.AspNetCore.JsonPatch +{ + public class CustomNamingStrategyTests + { + [Fact] + public void AddProperty_ToDynamicTestObject_WithCustomNamingStrategy() + { + // Arrange + var contractResolver = new DefaultContractResolver + { + NamingStrategy = new TestNamingStrategy() + }; + + dynamic targetObject = new DynamicTestObject(); + targetObject.Test = 1; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Add("NewInt", 1); + patchDocument.ContractResolver = contractResolver; + + // Act + patchDocument.ApplyTo(targetObject); + + // Assert + Assert.Equal(1, targetObject.customNewInt); + Assert.Equal(1, targetObject.Test); + } + + [Fact] + public void CopyPropertyValue_ToDynamicTestObject_WithCustomNamingStrategy() + { + // Arrange + var contractResolver = new DefaultContractResolver + { + NamingStrategy = new TestNamingStrategy() + }; + + dynamic targetObject = new DynamicTestObject(); + targetObject.customStringProperty = "A"; + targetObject.customAnotherStringProperty = "B"; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Copy("StringProperty", "AnotherStringProperty"); + patchDocument.ContractResolver = contractResolver; + + // Act + patchDocument.ApplyTo(targetObject); + + // Assert + Assert.Equal("A", targetObject.customAnotherStringProperty); + } + + [Fact] + public void MovePropertyValue_ForExpandoObject_WithCustomNamingStrategy() + { + // Arrange + var contractResolver = new DefaultContractResolver + { + NamingStrategy = new TestNamingStrategy() + }; + + dynamic targetObject = new ExpandoObject(); + targetObject.customStringProperty = "A"; + targetObject.customAnotherStringProperty = "B"; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Move("StringProperty", "AnotherStringProperty"); + patchDocument.ContractResolver = contractResolver; + + // Act + patchDocument.ApplyTo(targetObject); + var cont = targetObject as IDictionary; + cont.TryGetValue("customStringProperty", out var valueFromDictionary); + + // Assert + Assert.Equal("A", targetObject.customAnotherStringProperty); + Assert.Null(valueFromDictionary); + } + + [Fact] + public void RemoveProperty_FromDictionaryObject_WithCustomNamingStrategy() + { + // Arrange + var contractResolver = new DefaultContractResolver + { + NamingStrategy = new TestNamingStrategy() + }; + + var targetObject = new Dictionary() + { + { "customTest", 1}, + }; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Remove("Test"); + patchDocument.ContractResolver = contractResolver; + + // Act + patchDocument.ApplyTo(targetObject); + var cont = targetObject as IDictionary; + cont.TryGetValue("customTest", out var valueFromDictionary); + + // Assert + Assert.Equal(0, valueFromDictionary); + } + + [Fact] + public void ReplacePropertyValue_ForExpandoObject_WithCustomNamingStrategy() + { + // Arrange + var contractResolver = new DefaultContractResolver + { + NamingStrategy = new TestNamingStrategy() + }; + + dynamic targetObject = new ExpandoObject(); + targetObject.customTest = 1; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Replace("Test", 2); + patchDocument.ContractResolver = contractResolver; + + // Act + patchDocument.ApplyTo(targetObject); + + // Assert + Assert.Equal(2, targetObject.customTest); + } + + private class TestNamingStrategy : NamingStrategy + { + public new bool ProcessDictionaryKeys => true; + + public override string GetDictionaryKey(string key) + { + return "custom" + key; + } + + protected override string ResolvePropertyName(string name) + { + return name; + } + } + } +} diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/AddOperationTests.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/AddOperationTests.cs deleted file mode 100644 index 493444066b..0000000000 --- a/test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/AddOperationTests.cs +++ /dev/null @@ -1,553 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using System.Dynamic; -using Microsoft.AspNetCore.JsonPatch.Exceptions; -using Newtonsoft.Json; -using Xunit; - -namespace Microsoft.AspNetCore.JsonPatch.Internal -{ - public class AddOperationTests - { - [Fact] - public void AddNewPropertyShouldFailIfRootIsNotAnExpandoObject() - { - dynamic doc = new - { - Test = 1 - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add("NewInt", 1); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - var exception = Assert.Throws(() => - { - deserialized.ApplyTo(doc); - }); - Assert.Equal( - string.Format("The target location specified by path segment '{0}' was not found.", "NewInt"), - exception.Message); - } - - [Fact] - public void AddNewProperty() - { - dynamic obj = new ExpandoObject(); - obj.Test = 1; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add("NewInt", 1); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(obj); - - Assert.Equal(1, obj.NewInt); - Assert.Equal(1, obj.Test); - } - - [Fact] - public void AddNewPropertyToNestedAnonymousObjectShouldFail() - { - dynamic doc = new - { - Test = 1, - nested = new { } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add("Nested/NewInt", 1); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - var exception = Assert.Throws(() => - { - deserialized.ApplyTo(doc); - }); - Assert.Equal( - string.Format("The target location specified by path segment '{0}' was not found.", "NewInt"), - exception.Message); - } - - [Fact] - public void AddNewPropertyToTypedObjectShouldFail() - { - dynamic doc = new - { - Test = 1, - nested = new NestedObject() - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add("Nested/NewInt", 1); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - var exception = Assert.Throws(() => - { - deserialized.ApplyTo(doc); - }); - Assert.Equal( - string.Format("The target location specified by path segment '{0}' was not found.", "NewInt"), - exception.Message); - } - - [Fact] - public void AddToExistingPropertyOnNestedObject() - { - dynamic doc = new - { - Test = 1, - nested = new NestedObject() - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add("Nested/StringProperty", "A"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal("A", doc.nested.StringProperty); - Assert.Equal(1, doc.Test); - } - - [Fact] - public void AddNewPropertyToExpandoOject() - { - dynamic doc = new - { - Test = 1, - nested = new ExpandoObject() - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add("Nested/NewInt", 1); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal(1, doc.nested.NewInt); - Assert.Equal(1, doc.Test); - } - - [Fact] - public void AddNewPropertyToExpandoOjectInTypedObject() - { - var doc = new NestedObject() - { - DynamicProperty = new ExpandoObject() - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add("DynamicProperty/NewInt", 1); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal(1, doc.DynamicProperty.NewInt); - } - - [Fact] - public void AddNewPropertyToTypedObjectInExpandoObject() - { - dynamic dynamicProperty = new ExpandoObject(); - dynamicProperty.StringProperty = "A"; - - var doc = new NestedObject() - { - DynamicProperty = dynamicProperty - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add("DynamicProperty/StringProperty", "B"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal("B", doc.DynamicProperty.StringProperty); - } - - [Fact] - public void AddNewPropertyToAnonymousObjectShouldFail() - { - dynamic doc = new - { - Test = 1 - }; - - dynamic valueToAdd = new { IntValue = 1, StringValue = "test", GuidValue = Guid.NewGuid() }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add("ComplexProperty", valueToAdd); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - var exception = Assert.Throws(() => - { - deserialized.ApplyTo(doc); - }); - Assert.Equal( - string.Format("The target location specified by path segment '{0}' was not found.", "ComplexProperty"), - exception.Message); - } - - [Fact] - public void AddResultsReplaceShouldFailOnAnonymousDueToNoSetter() - { - var doc = new - { - StringProperty = "A" - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add("StringProperty", "B"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - var exception = Assert.Throws(() => - { - deserialized.ApplyTo(doc); - }); - Assert.Equal( - string.Format("The property at path '{0}' could not be updated.", "StringProperty"), - exception.Message); - } - - [Fact] - public void AddResultsShouldReplace() - { - dynamic doc = new ExpandoObject(); - doc.StringProperty = "A"; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add("StringProperty", "B"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal("B", doc.StringProperty); - } - - [Fact] - public void AddResultsShouldReplaceInNested() - { - dynamic doc = new ExpandoObject(); - doc.InBetweenFirst = new ExpandoObject(); - doc.InBetweenFirst.InBetweenSecond = new ExpandoObject(); - doc.InBetweenFirst.InBetweenSecond.StringProperty = "A"; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add("/InBetweenFirst/InBetweenSecond/StringProperty", "B"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal("B", doc.InBetweenFirst.InBetweenSecond.StringProperty); - } - - [Fact] - public void AddResultsShouldReplaceInNestedInDynamic() - { - dynamic doc = new ExpandoObject(); - doc.Nested = new NestedObject(); - doc.Nested.DynamicProperty = new ExpandoObject(); - doc.Nested.DynamicProperty.InBetweenFirst = new ExpandoObject(); - doc.Nested.DynamicProperty.InBetweenFirst.InBetweenSecond = new ExpandoObject(); - doc.Nested.DynamicProperty.InBetweenFirst.InBetweenSecond.StringProperty = "A"; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add("/Nested/DynamicProperty/InBetweenFirst/InBetweenSecond/StringProperty", "B"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal("B", doc.Nested.DynamicProperty.InBetweenFirst.InBetweenSecond.StringProperty); - } - - [Fact] - public void ShouldNotBeAbleToAddToNonExistingPropertyThatIsNotTheRoot() - { - //Adding to a Nonexistent Target - // - // An example target JSON document: - // { "foo": "bar" } - // A JSON Patch document: - // [ - // { "op": "add", "path": "/baz/bat", "value": "qux" } - // ] - // This JSON Patch document, applied to the target JSON document above, - // would result in an error (therefore, it would not be applied), - // because the "add" operation's target location that references neither - // the root of the document, nor a member of an existing object, nor a - // member of an existing array. - - var doc = new NestedObject() - { - DynamicProperty = new ExpandoObject() - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add("DynamicProperty/OtherProperty/IntProperty", 1); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - var exception = Assert.Throws(() => - { - deserialized.ApplyTo(doc); - }); - Assert.Equal( - string.Format( - "For operation '{0}', the target location specified by path '{1}' was not found.", - "add", - "/DynamicProperty/OtherProperty/IntProperty"), - exception.Message); - } - - [Fact] - public void ShouldNotBeAbleToAddToNonExistingPropertyInNestedPropertyThatIsNotTheRoot() - { - //Adding to a Nonexistent Target - // - // An example target JSON document: - // { "foo": "bar" } - // A JSON Patch document: - // [ - // { "op": "add", "path": "/baz/bat", "value": "qux" } - // ] - // This JSON Patch document, applied to the target JSON document above, - // would result in an error (therefore, it would not be applied), - // because the "add" operation's target location that references neither - // the root of the document, nor a member of an existing object, nor a - // member of an existing array. - - var doc = new - { - Foo = "bar" - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add("baz/bat", "qux"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - var exception = Assert.Throws(() => - { - deserialized.ApplyTo(doc); - }); - Assert.Equal( - string.Format("The target location specified by path segment '{0}' was not found.", "baz"), - exception.Message); - } - - [Fact] - public void ShouldNotReplacePropertyWithDifferentCase() - { - dynamic doc = new ExpandoObject(); - doc.StringProperty = "A"; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add("stringproperty", "B"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal("A", doc.StringProperty); - Assert.Equal("B", doc.stringproperty); - } - - [Fact] - public void AddToList() - { - var doc = new - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add("IntegerList/0", 4); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal(new List() { 4, 1, 2, 3 }, doc.IntegerList); - } - - [Fact] - public void AddToListNegativePosition() - { - dynamic doc = new ExpandoObject(); - doc.IntegerList = new List() { 1, 2, 3 }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add("IntegerList/-1", 4); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - var exception = Assert.Throws(() => - { - deserialized.ApplyTo(doc); - }); - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "-1"), - exception.Message); - } - - [Fact] - public void ShouldAddToListWithDifferentCase() - { - var doc = new - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add("integerlist/0", 4); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal(new List() { 4, 1, 2, 3 }, doc.IntegerList); - } - - [Fact] - public void AddToListInvalidPositionTooLarge() - { - var doc = new - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add("IntegerList/4", 4); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - var exception = Assert.Throws(() => - { - deserialized.ApplyTo(doc); - }); - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "4"), - exception.Message); - } - - [Fact] - public void AddToListAtBeginning() - { - var doc = new - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add("IntegerList/0", 4); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal(new List() { 4, 1, 2, 3 }, doc.IntegerList); - } - - [Fact] - public void AddToListInvalidPositionTooSmall() - { - var doc = new - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add("IntegerList/-1", 4); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - var exception = Assert.Throws(() => - { - deserialized.ApplyTo(doc); - }); - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "-1"), - exception.Message); - } - - [Fact] - public void AddToListAppend() - { - var doc = new - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add("IntegerList/-", 4); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal(new List() { 1, 2, 3, 4 }, doc.IntegerList); - } - } -} \ No newline at end of file diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/AddTypedOperationTests.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/AddTypedOperationTests.cs deleted file mode 100644 index b2eb4d3a3c..0000000000 --- a/test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/AddTypedOperationTests.cs +++ /dev/null @@ -1,121 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System.Collections.Generic; -using Microsoft.AspNetCore.JsonPatch.Exceptions; -using Newtonsoft.Json; -using Xunit; - -namespace Microsoft.AspNetCore.JsonPatch.Internal -{ - public class AddTypedOperationTests - { - [Fact] - public void AddToListNegativePosition() - { - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add("IntegerList/-1", 4); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - var exception = Assert.Throws(() => - { - deserialized.ApplyTo(doc); - }); - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "-1"), - exception.Message); - } - - [Fact] - public void AddToListInList() - { - var doc = new SimpleObjectWithNestedObject() - { - ListOfSimpleObject = new List() - { - new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add("ListOfSimpleObject/0/IntegerList/0", 4); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - Assert.Equal(new List() { 4, 1, 2, 3 }, doc.ListOfSimpleObject[0].IntegerList); - } - - [Fact] - public void AddToListInListInvalidPositionTooSmall() - { - var doc = new SimpleObjectWithNestedObject() - { - ListOfSimpleObject = new List() - { - new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add("ListOfSimpleObject/-1/IntegerList/0", 4); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - var exception = Assert.Throws(() => - { - deserialized.ApplyTo(doc); - }); - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "-1"), - exception.Message); - } - - [Fact] - public void AddToListInListInvalidPositionTooLarge() - { - var doc = new SimpleObjectWithNestedObject() - { - ListOfSimpleObject = new List() - { - new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - } - }; - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add("ListOfSimpleObject/20/IntegerList/0", 4); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - var exception = Assert.Throws(() => - { - deserialized.ApplyTo(doc); - }); - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "20"), - exception.Message); - } - } -} diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/CopyOperationTests.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/CopyOperationTests.cs deleted file mode 100644 index b2faff25a0..0000000000 --- a/test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/CopyOperationTests.cs +++ /dev/null @@ -1,245 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System.Collections.Generic; -using System.Dynamic; -using Newtonsoft.Json; -using Xunit; - -namespace Microsoft.AspNetCore.JsonPatch.Internal -{ - public class CopyOperationTests - { - [Fact] - public void Copy() - { - dynamic doc = new ExpandoObject(); - - doc.StringProperty = "A"; - doc.AnotherStringProperty = "B"; - - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy("StringProperty", "AnotherStringProperty"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - deserialized.ApplyTo(doc); - - Assert.Equal("A", doc.AnotherStringProperty); - } - - [Fact] - public void CopyInList() - { - dynamic doc = new ExpandoObject(); - doc.IntegerList = new List() { 1, 2, 3 }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy("IntegerList/0", "IntegerList/1"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal(new List() { 1, 1, 2, 3 }, doc.IntegerList); - } - - [Fact] - public void CopyFromListToEndOfList() - { - dynamic doc = new ExpandoObject(); - doc.IntegerList = new List() { 1, 2, 3 }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy("IntegerList/0", "IntegerList/-"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - deserialized.ApplyTo(doc); - - Assert.Equal(new List() { 1, 2, 3, 1 }, doc.IntegerList); - } - - [Fact] - public void CopyFromListToNonList() - { - dynamic doc = new ExpandoObject(); - doc.IntegerList = new List() { 1, 2, 3 }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy("IntegerList/0", "IntegerValue"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal(1, doc.IntegerValue); - } - - [Fact] - public void CopyFromNonListToList() - { - dynamic doc = new ExpandoObject(); - doc.IntegerValue = 5; - doc.IntegerList = new List() { 1, 2, 3 }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy("IntegerValue", "IntegerList/0"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - deserialized.ApplyTo(doc); - - Assert.Equal(new List() { 5, 1, 2, 3 }, doc.IntegerList); - } - - [Fact] - public void CopyToEndOfList() - { - dynamic doc = new ExpandoObject(); - doc.IntegerValue = 5; - doc.IntegerList = new List() { 1, 2, 3 }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy("IntegerValue", "IntegerList/-"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal(new List() { 1, 2, 3, 5 }, doc.IntegerList); - } - - [Fact] - public void NestedCopy() - { - dynamic doc = new ExpandoObject(); - doc.SimpleObject = new SimpleObject() - { - StringProperty = "A", - AnotherStringProperty = "B" - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy("SimpleObject/StringProperty", "SimpleObject/AnotherStringProperty"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal("A", doc.SimpleObject.AnotherStringProperty); - } - - [Fact] - public void NestedCopyInList() - { - dynamic doc = new ExpandoObject(); - doc.SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy("SimpleObject/IntegerList/0", "SimpleObject/IntegerList/1"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - deserialized.ApplyTo(doc); - - Assert.Equal(new List() { 1, 1, 2, 3 }, doc.SimpleObject.IntegerList); - } - - [Fact] - public void NestedCopyFromListToEndOfList() - { - dynamic doc = new ExpandoObject(); - doc.SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy("SimpleObject/IntegerList/0", "SimpleObject/IntegerList/-"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal(new List() { 1, 2, 3, 1 }, doc.SimpleObject.IntegerList); - } - - [Fact] - public void NestedCopyFromListToNonList() - { - dynamic doc = new ExpandoObject(); - doc.SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy("SimpleObject/IntegerList/0", "SimpleObject/IntegerValue"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - deserialized.ApplyTo(doc); - - Assert.Equal(1, doc.SimpleObject.IntegerValue); - } - - [Fact] - public void NestedCopyFromNonListToList() - { - dynamic doc = new ExpandoObject(); - doc.SimpleObject = new SimpleObject() - { - IntegerValue = 5, - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy("SimpleObject/IntegerValue", "SimpleObject/IntegerList/0"); - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - deserialized.ApplyTo(doc); - - Assert.Equal(new List() { 5, 1, 2, 3 }, doc.SimpleObject.IntegerList); - } - - [Fact] - public void NestedCopyToEndOfList() - { - dynamic doc = new ExpandoObject(); - doc.SimpleObject = new SimpleObject() - { - IntegerValue = 5, - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy("SimpleObject/IntegerValue", "SimpleObject/IntegerList/-"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - deserialized.ApplyTo(doc); - - Assert.Equal(new List() { 1, 2, 3, 5 }, doc.SimpleObject.IntegerList); - } - } -} diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/CopyTypedOperationTests.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/CopyTypedOperationTests.cs deleted file mode 100644 index d0b0847c71..0000000000 --- a/test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/CopyTypedOperationTests.cs +++ /dev/null @@ -1,268 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System.Collections.Generic; -using Newtonsoft.Json; -using Xunit; - -namespace Microsoft.AspNetCore.JsonPatch.Internal -{ - public class CopyTypedOperationTests - { - [Fact] - public void Copy() - { - var doc = new SimpleObject() - { - StringProperty = "A", - AnotherStringProperty = "B" - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy("StringProperty", "AnotherStringProperty"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - deserialized.ApplyTo(doc); - - Assert.Equal("A", doc.AnotherStringProperty); - } - - [Fact] - public void CopyInList() - { - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy("IntegerList/0", "IntegerList/1"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal(new List() { 1, 1, 2, 3 }, doc.IntegerList); - } - - [Fact] - public void CopyFromListToEndOfList() - { - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy("IntegerList/0", "IntegerList/-"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - deserialized.ApplyTo(doc); - - Assert.Equal(new List() { 1, 2, 3, 1 }, doc.IntegerList); - } - - [Fact] - public void CopyFromListToNonList() - { - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy("IntegerList/0", "IntegerValue"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal(1, doc.IntegerValue); - } - - [Fact] - public void CopyFromNonListToList() - { - var doc = new SimpleObject() - { - IntegerValue = 5, - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy("IntegerValue", "IntegerList/0"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - deserialized.ApplyTo(doc); - - Assert.Equal(new List() { 5, 1, 2, 3 }, doc.IntegerList); - } - - [Fact] - public void CopyToEndOfList() - { - var doc = new SimpleObject() - { - IntegerValue = 5, - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy("IntegerValue", "IntegerList/-"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal(new List() { 1, 2, 3, 5 }, doc.IntegerList); - } - - [Fact] - public void NestedCopy() - { - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - StringProperty = "A", - AnotherStringProperty = "B" - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy("SimpleObject/StringProperty", "SimpleObject/AnotherStringProperty"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal("A", doc.SimpleObject.AnotherStringProperty); - } - - [Fact] - public void NestedCopyInList() - { - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy("SimpleObject/IntegerList/0", "SimpleObject/IntegerList/1"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - deserialized.ApplyTo(doc); - - Assert.Equal(new List() { 1, 1, 2, 3 }, doc.SimpleObject.IntegerList); - } - - [Fact] - public void NestedCopyFromListToEndOfList() - { - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy("SimpleObject/IntegerList/0", "SimpleObject/IntegerList/-"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal(new List() { 1, 2, 3, 1 }, doc.SimpleObject.IntegerList); - } - - [Fact] - public void NestedCopyFromListToNonList() - { - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy("SimpleObject/IntegerList/0", "SimpleObject/IntegerValue"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - deserialized.ApplyTo(doc); - - Assert.Equal(1, doc.SimpleObject.IntegerValue); - } - - [Fact] - public void NestedCopyFromNonListToList() - { - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerValue = 5, - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy("SimpleObject/IntegerValue", "SimpleObject/IntegerList/0"); - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - deserialized.ApplyTo(doc); - - Assert.Equal(new List() { 5, 1, 2, 3 }, doc.SimpleObject.IntegerList); - } - - [Fact] - public void NestedCopyToEndOfList() - { - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerValue = 5, - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy("SimpleObject/IntegerValue", "SimpleObject/IntegerList/-"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - deserialized.ApplyTo(doc); - - Assert.Equal(new List() { 1, 2, 3, 5 }, doc.SimpleObject.IntegerList); - } - } -} diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/CustomNamingStrategyTests.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/CustomNamingStrategyTests.cs deleted file mode 100644 index b829b81902..0000000000 --- a/test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/CustomNamingStrategyTests.cs +++ /dev/null @@ -1,154 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System.Collections.Generic; -using System.Dynamic; -using Newtonsoft.Json.Serialization; -using Xunit; - -namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic -{ - public class CustomNamingStrategyTests - { - [Fact] - public void AddProperty_ToExpandoObject_WithCustomNamingStrategy() - { - // Arrange - var contractResolver = new DefaultContractResolver - { - NamingStrategy = new TestNamingStrategy() - }; - - dynamic obj = new ExpandoObject(); - obj.Test = 1; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add("NewInt", 1); - patchDoc.ContractResolver = contractResolver; - - // Act - patchDoc.ApplyTo(obj); - - // Assert - Assert.Equal(1, obj.customNewInt); - Assert.Equal(1, obj.Test); - } - - [Fact] - public void CopyPropertyValue_ForExpandoObject_WithCustomNamingStrategy() - { - // Arrange - var contractResolver = new DefaultContractResolver - { - NamingStrategy = new TestNamingStrategy() - }; - - dynamic obj = new ExpandoObject(); - obj.customStringProperty = "A"; - obj.customAnotherStringProperty = "B"; - - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy("StringProperty", "AnotherStringProperty"); - patchDoc.ContractResolver = contractResolver; - - // Act - patchDoc.ApplyTo(obj); - - // Assert - Assert.Equal("A", obj.customAnotherStringProperty); - } - - [Fact] - public void MovePropertyValue_ForExpandoObject_WithCustomNamingStrategy() - { - // Arrange - var contractResolver = new DefaultContractResolver - { - NamingStrategy = new TestNamingStrategy() - }; - - dynamic obj = new ExpandoObject(); - obj.customStringProperty = "A"; - obj.customAnotherStringProperty = "B"; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move("StringProperty", "AnotherStringProperty"); - patchDoc.ContractResolver = contractResolver; - - // Act - patchDoc.ApplyTo(obj); - var cont = obj as IDictionary; - cont.TryGetValue("customStringProperty", out var valueFromDictionary); - - // Assert - Assert.Equal("A", obj.customAnotherStringProperty); - Assert.Null(valueFromDictionary); - } - - [Fact] - public void RemoveProperty_FromExpandoObject_WithCustomNamingStrategy() - { - // Arrange - var contractResolver = new DefaultContractResolver - { - NamingStrategy = new TestNamingStrategy() - }; - - dynamic obj = new ExpandoObject(); - obj.customTest = 1; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove("Test"); - patchDoc.ContractResolver = contractResolver; - - // Act - patchDoc.ApplyTo(obj); - var cont = obj as IDictionary; - cont.TryGetValue("customTest", out var valueFromDictionary); - - // Assert - Assert.Null(valueFromDictionary); - } - - [Fact] - public void ReplacePropertyValue_ForExpandoObject_WithCustomNamingStrategy() - { - // Arrange - var contractResolver = new DefaultContractResolver - { - NamingStrategy = new TestNamingStrategy() - }; - - dynamic obj = new ExpandoObject(); - obj.customTest = 1; - - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace("Test", 2); - patchDoc.ContractResolver = contractResolver; - - // Act - patchDoc.ApplyTo(obj); - - // Assert - Assert.Equal(2, obj.customTest); - } - - private class TestNamingStrategy : NamingStrategy - { - public new bool ProcessDictionaryKeys => true; - - public override string GetDictionaryKey(string key) - { - return "custom" + key; - } - - protected override string ResolvePropertyName(string name) - { - return name; - } - } - } -} diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/InheritedObject.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/InheritedObject.cs deleted file mode 100644 index 29424d591e..0000000000 --- a/test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/InheritedObject.cs +++ /dev/null @@ -1,10 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -namespace Microsoft.AspNetCore.JsonPatch.Internal -{ - public class InheritedObject : SimpleObject - { - public string AdditionalStringProperty { get; set; } - } -} \ No newline at end of file diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/MoveOperationTests.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/MoveOperationTests.cs deleted file mode 100644 index 31f58c4b82..0000000000 --- a/test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/MoveOperationTests.cs +++ /dev/null @@ -1,333 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System.Collections.Generic; -using System.Dynamic; -using Newtonsoft.Json; -using Xunit; - -namespace Microsoft.AspNetCore.JsonPatch.Internal -{ - public class MoveOperationTests - { - [Fact] - public void Move() - { - dynamic doc = new ExpandoObject(); - doc.StringProperty = "A"; - doc.AnotherStringProperty = "B"; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move("StringProperty", "AnotherStringProperty"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal("A", doc.AnotherStringProperty); - - var cont = doc as IDictionary; - cont.TryGetValue("StringProperty", out object valueFromDictionary); - Assert.Null(valueFromDictionary); - } - - [Fact] - public void MoveToNonExisting() - { - dynamic doc = new ExpandoObject(); - doc.StringProperty = "A"; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move("StringProperty", "AnotherStringProperty"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal("A", doc.AnotherStringProperty); - - var cont = doc as IDictionary; - cont.TryGetValue("StringProperty", out var valueFromDictionary); - Assert.Null(valueFromDictionary); - } - - [Fact] - public void MoveDynamicToTyped() - { - dynamic doc = new ExpandoObject(); - doc.StringProperty = "A"; - doc.SimpleDTO = new SimpleObject() { AnotherStringProperty = "B" }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move("StringProperty", "SimpleDTO/AnotherStringProperty"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal("A", doc.SimpleDTO.AnotherStringProperty); - - var cont = doc as IDictionary; - cont.TryGetValue("StringProperty", out object valueFromDictionary); - Assert.Null(valueFromDictionary); - } - - [Fact] - public void MoveTypedToDynamic() - { - dynamic doc = new ExpandoObject(); - doc.StringProperty = "A"; - doc.SimpleDTO = new SimpleObject() { AnotherStringProperty = "B" }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move("SimpleDTO/AnotherStringProperty", "StringProperty"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal("B", doc.StringProperty); - Assert.Null(doc.SimpleDTO.AnotherStringProperty); - } - - [Fact] - public void NestedMove() - { - dynamic doc = new ExpandoObject(); - doc.Nested = new SimpleObject() - { - StringProperty = "A", - AnotherStringProperty = "B" - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move("Nested/StringProperty", "Nested/AnotherStringProperty"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal("A", doc.Nested.AnotherStringProperty); - Assert.Null(doc.Nested.StringProperty); - } - - [Fact] - public void MoveInList() - { - dynamic doc = new ExpandoObject(); - doc.IntegerList = new List() { 1, 2, 3 }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move("IntegerList/0", "IntegerList/1"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal(new List() { 2, 1, 3 }, doc.IntegerList); - } - - [Fact] - public void NestedMoveInList() - { - dynamic doc = new ExpandoObject(); - doc.Nested = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move("Nested/IntegerList/0", "Nested/IntegerList/1"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal(new List() { 2, 1, 3 }, doc.Nested.IntegerList); - } - - [Fact] - public void MoveFromListToEndOfList() - { - dynamic doc = new ExpandoObject(); - doc.IntegerList = new List() { 1, 2, 3 }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move("IntegerList/0", "IntegerList/-"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - deserialized.ApplyTo(doc); - - Assert.Equal(new List() { 2, 3, 1 }, doc.IntegerList); - } - - [Fact] - public void NestedMoveFromListToEndOfList() - { - dynamic doc = new ExpandoObject(); - doc.Nested = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move("Nested/IntegerList/0", "Nested/IntegerList/-"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - deserialized.ApplyTo(doc); - - Assert.Equal(new List() { 2, 3, 1 }, doc.Nested.IntegerList); - } - - [Fact] - public void MoveFomListToNonList() - { - dynamic doc = new ExpandoObject(); - doc.IntegerList = new List() { 1, 2, 3 }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move("IntegerList/0", "IntegerValue"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal(new List() { 2, 3 }, doc.IntegerList); - Assert.Equal(1, doc.IntegerValue); - } - - [Fact] - public void NestedMoveFomListToNonList() - { - dynamic doc = new ExpandoObject(); - doc.Nested = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move("Nested/IntegerList/0", "Nested/IntegerValue"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal(new List() { 2, 3 }, doc.Nested.IntegerList); - Assert.Equal(1, doc.Nested.IntegerValue); - } - - [Fact] - public void MoveFromNonListToList() - { - dynamic doc = new ExpandoObject(); - doc.IntegerValue = 5; - doc.IntegerList = new List() { 1, 2, 3 }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move("IntegerValue", "IntegerList/0"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - var cont = doc as IDictionary; - cont.TryGetValue("IntegerValue", out object valueFromDictionary); - Assert.Null(valueFromDictionary); - - Assert.Equal(new List() { 5, 1, 2, 3 }, doc.IntegerList); - } - - [Fact] - public void NestedMoveFromNonListToList() - { - dynamic doc = new ExpandoObject(); - doc.Nested = new SimpleObject() - { - IntegerValue = 5, - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move("Nested/IntegerValue", "Nested/IntegerList/0"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal(0, doc.Nested.IntegerValue); - Assert.Equal(new List() { 5, 1, 2, 3 }, doc.Nested.IntegerList); - } - - [Fact] - public void MoveToEndOfList() - { - dynamic doc = new ExpandoObject(); - doc.IntegerValue = 5; - doc.IntegerList = new List() { 1, 2, 3 }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move("IntegerValue", "IntegerList/-"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - var cont = doc as IDictionary; - cont.TryGetValue("IntegerValue", out var valueFromDictionary); - Assert.Null(valueFromDictionary); - - Assert.Equal(new List() { 1, 2, 3, 5 }, doc.IntegerList); - } - - [Fact] - public void NestedMoveToEndOfList() - { - dynamic doc = new ExpandoObject(); - doc.Nested = new SimpleObject() - { - IntegerValue = 5, - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move("Nested/IntegerValue", "Nested/IntegerList/-"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal(0, doc.Nested.IntegerValue); - Assert.Equal(new List() { 1, 2, 3, 5 }, doc.Nested.IntegerList); - } - } -} diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/MoveTypedOperationTests.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/MoveTypedOperationTests.cs deleted file mode 100644 index 4638718a59..0000000000 --- a/test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/MoveTypedOperationTests.cs +++ /dev/null @@ -1,138 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System.Collections.Generic; -using Newtonsoft.Json; -using Xunit; - -namespace Microsoft.AspNetCore.JsonPatch.Internal -{ - public class MoveTypedOperationTests - { - [Fact] - public void Move() - { - var doc = new SimpleObject() - { - StringProperty = "A", - AnotherStringProperty = "B" - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move("StringProperty", "AnotherStringProperty"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal("A", doc.AnotherStringProperty); - Assert.Null(doc.StringProperty); - } - - [Fact] - public void MoveInList() - { - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move("IntegerList/0", "IntegerList/1"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal(new List() { 2, 1, 3 }, doc.IntegerList); - } - - [Fact] - public void MoveFromListToEndOfList() - { - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move("IntegerList/0", "IntegerList/-"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - deserialized.ApplyTo(doc); - - Assert.Equal(new List() { 2, 3, 1 }, doc.IntegerList); - } - - [Fact] - public void MoveFomListToNonList() - { - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move("IntegerList/0", "IntegerValue"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal(new List() { 2, 3 }, doc.IntegerList); - Assert.Equal(1, doc.IntegerValue); - } - - [Fact] - public void MoveFromNonListToList() - { - var doc = new SimpleObject() - { - IntegerValue = 5, - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move("IntegerValue", "IntegerList/0"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal(0, doc.IntegerValue); - Assert.Equal(new List() { 5, 1, 2, 3 }, doc.IntegerList); - } - - [Fact] - public void MoveToEndOfList() - { - var doc = new SimpleObject() - { - IntegerValue = 5, - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move("IntegerValue", "IntegerList/-"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal(0, doc.IntegerValue); - Assert.Equal(new List() { 1, 2, 3, 5 }, doc.IntegerList); - } - } -} diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/PatchDocumentTests.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/PatchDocumentTests.cs deleted file mode 100644 index 403238bf99..0000000000 --- a/test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/PatchDocumentTests.cs +++ /dev/null @@ -1,95 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using Microsoft.AspNetCore.JsonPatch.Exceptions; -using Newtonsoft.Json; -using Xunit; - -namespace Microsoft.AspNetCore.JsonPatch.Internal -{ - public class PatchDocumentTests - { - [Fact] - public void InvalidPathAtBeginningShouldThrowException() - { - var patchDoc = new JsonPatchDocument(); - var exception = Assert.Throws(() => - { - patchDoc.Add("//NewInt", 1); - }); - Assert.Equal( - "The provided string '//NewInt' is an invalid path.", - exception.Message); - } - - [Fact] - public void InvalidPathAtEndShouldThrowException() - { - var patchDoc = new JsonPatchDocument(); - var exception = Assert.Throws(() => - { - patchDoc.Add("NewInt//", 1); - }); - Assert.Equal( - "The provided string 'NewInt//' is an invalid path.", - exception.Message); - } - - [Fact] - public void InvalidPathWithDotShouldThrowException() - { - var patchDoc = new JsonPatchDocument(); - var exception = Assert.Throws(() => - { - patchDoc.Add("NewInt.Test", 1); - }); - Assert.Equal( - "The provided string 'NewInt.Test' is an invalid path.", - exception.Message); - } - - [Fact] - public void NonGenericPatchDocToGenericMustSerialize() - { - var doc = new SimpleObject() - { - StringProperty = "A", - AnotherStringProperty = "B" - }; - - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy("StringProperty", "AnotherStringProperty"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal("A", doc.AnotherStringProperty); - } - - [Fact] - public void GenericPatchDocToNonGenericMustSerialize() - { - var doc = new SimpleObject() - { - StringProperty = "A", - AnotherStringProperty = "B" - }; - - var patchDocTyped = new JsonPatchDocument(); - patchDocTyped.Copy(o => o.StringProperty, o => o.AnotherStringProperty); - - var patchDocUntyped = new JsonPatchDocument(); - patchDocUntyped.Copy("StringProperty", "AnotherStringProperty"); - - var serializedTyped = JsonConvert.SerializeObject(patchDocTyped); - var serializedUntyped = JsonConvert.SerializeObject(patchDocUntyped); - var deserialized = JsonConvert.DeserializeObject(serializedTyped); - - deserialized.ApplyTo(doc); - - Assert.Equal("A", doc.AnotherStringProperty); - } - } -} \ No newline at end of file diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/RemoveOperationTests.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/RemoveOperationTests.cs deleted file mode 100644 index 22707add87..0000000000 --- a/test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/RemoveOperationTests.cs +++ /dev/null @@ -1,313 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System.Collections.Generic; -using System.Dynamic; -using Microsoft.AspNetCore.JsonPatch.Exceptions; -using Newtonsoft.Json; -using Xunit; - -namespace Microsoft.AspNetCore.JsonPatch.Internal -{ - public class RemoveOperationTests - { - [Fact] - public void RemovePropertyShouldFailIfRootIsAnonymous() - { - dynamic doc = new - { - Test = 1 - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove("Test"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - var exception = Assert.Throws(() => - { - deserialized.ApplyTo(doc); - }); - Assert.Equal( - string.Format("The property at path '{0}' could not be updated.", "Test"), - exception.Message); - } - - [Fact] - public void RemovePropertyShouldFailIfItDoesntExist() - { - dynamic doc = new ExpandoObject(); - doc.Test = 1; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove("NonExisting"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - var exception = Assert.Throws(() => - { - deserialized.ApplyTo(doc); - }); - Assert.Equal( - string.Format("The target location specified by path segment '{0}' was not found.", "NonExisting"), - exception.Message); - } - - [Fact] - public void RemovePropertyFromExpandoObject() - { - dynamic obj = new ExpandoObject(); - obj.Test = 1; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove("Test"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(obj); - - var cont = obj as IDictionary; - cont.TryGetValue("Test", out object valueFromDictionary); - Assert.Null(valueFromDictionary); - } - - [Fact] - public void RemoveProperty_FromExpandoObject_MixedCase_ThrowsPathNotFoundException() - { - dynamic obj = new ExpandoObject(); - obj.Test = 1; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove("test"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - var exception = Assert.Throws(() => - { - deserialized.ApplyTo(obj); - }); - - Assert.Equal( - string.Format( - "The target location specified by path segment '{0}' was not found.", - "test"), - exception.Message); - } - - [Fact] - public void RemoveNestedPropertyFromExpandoObject() - { - dynamic obj = new ExpandoObject(); - obj.Test = new ExpandoObject(); - obj.Test.AnotherTest = "A"; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove("Test"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(obj); - - var cont = obj as IDictionary; - cont.TryGetValue("Test", out object valueFromDictionary); - Assert.Null(valueFromDictionary); - } - - [Fact] - public void RemoveNestedProperty_FromExpandoObject_MixedCase_ThrowsPathNotFoundException() - { - dynamic obj = new ExpandoObject(); - obj.Test = new ExpandoObject(); - obj.Test.AnotherTest = "A"; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove("test"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - var exception = Assert.Throws(() => - { - deserialized.ApplyTo(obj); - }); - - Assert.Equal( - string.Format( - "The target location specified by path segment '{0}' was not found.", - "test"), - exception.Message); - } - - [Fact] - public void NestedRemove() - { - dynamic doc = new ExpandoObject(); - doc.SimpleDTO = new SimpleObject() - { - StringProperty = "A" - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove("SimpleDTO/StringProperty"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - Assert.Null(doc.SimpleDTO.StringProperty); - } - - [Fact] - public void NestedRemove_MixedCase_ThrowsPathNotFoundException() - { - dynamic doc = new ExpandoObject(); - doc.SimpleObject = new SimpleObject() - { - StringProperty = "A" - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove("Simpleobject/stringProperty"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - var exception = Assert.Throws(() => - { - deserialized.ApplyTo(doc); - }); - - Assert.Equal( - string.Format( - "For operation '{0}', the target location specified by path '{1}' was not found.", - "remove", - "/Simpleobject/stringProperty"), - exception.Message); - } - - [Fact] - public void NestedRemoveFromList() - { - dynamic doc = new ExpandoObject(); - doc.SimpleDTO = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove("SimpleDTO/IntegerList/2"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal(new List() { 1, 2 }, doc.SimpleDTO.IntegerList); - } - - [Fact] - public void NestedRemoveFromList_MixedCase() - { - dynamic doc = new ExpandoObject(); - doc.SimpleDTO = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove("SimpleDTO/Integerlist/2"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal(new List() { 1, 2 }, doc.SimpleDTO.IntegerList); - } - - [Fact] - public void NestedRemoveFromListInvalidPositionTooLarge() - { - dynamic doc = new ExpandoObject(); - doc.SimpleDTO = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove("SimpleDTO/IntegerList/3"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - var exception = Assert.Throws(() => - { - deserialized.ApplyTo(doc); - }); - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "3"), - exception.Message); - } - - [Fact] - public void NestedRemoveFromListInvalidPositionTooSmall() - { - dynamic doc = new ExpandoObject(); - doc.SimpleDTO = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove("SimpleDTO/IntegerList/-1"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - var exception = Assert.Throws(() => - { - deserialized.ApplyTo(doc); - }); - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "-1"), - exception.Message); - } - - [Fact] - public void NestedRemoveFromEndOfList() - { - dynamic doc = new ExpandoObject(); - doc.SimpleDTO = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove("SimpleDTO/IntegerList/-"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - Assert.Equal(new List() { 1, 2 }, doc.SimpleDTO.IntegerList); - } - } -} diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/RemoveTypedOperationTests.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/RemoveTypedOperationTests.cs deleted file mode 100644 index 000b049b1b..0000000000 --- a/test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/RemoveTypedOperationTests.cs +++ /dev/null @@ -1,244 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System.Collections.Generic; -using Microsoft.AspNetCore.JsonPatch.Exceptions; -using Newtonsoft.Json; -using Xunit; - -namespace Microsoft.AspNetCore.JsonPatch.Internal -{ - public class RemoveTypedOperationTests - { - [Fact] - public void Remove() - { - var doc = new SimpleObject() - { - StringProperty = "A" - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove("StringProperty"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Null(doc.StringProperty); - } - - [Fact] - public void RemoveFromList() - { - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove("IntegerList/2"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal(new List() { 1, 2 }, doc.IntegerList); - } - - [Fact] - public void RemoveFromListInvalidPositionTooLarge() - { - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove("IntegerList/3"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - var exception = Assert.Throws(() => - { - deserialized.ApplyTo(doc); - }); - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "3"), - exception.Message); - } - - [Fact] - public void RemoveFromListInvalidPositionTooSmall() - { - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove("IntegerList/-1"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - var exception = Assert.Throws(() => - { - deserialized.ApplyTo(doc); - }); - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "-1"), - exception.Message); - } - - [Fact] - public void RemoveFromEndOfList() - { - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove("IntegerList/-"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal(new List() { 1, 2 }, doc.IntegerList); - } - - [Fact] - public void NestedRemove() - { - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - StringProperty = "A" - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove("SimpleObject/StringProperty"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Null(doc.SimpleObject.StringProperty); - } - - [Fact] - public void NestedRemoveFromList() - { - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove("SimpleObject/IntegerList/2"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal(new List() { 1, 2 }, doc.SimpleObject.IntegerList); - } - - [Fact] - public void NestedRemoveFromListInvalidPositionTooLarge() - { - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove("SimpleObject/IntegerList/3"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - var exception = Assert.Throws(() => - { - deserialized.ApplyTo(doc); - }); - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "3"), - exception.Message); - } - - [Fact] - public void NestedRemoveFromListInvalidPositionTooSmall() - { - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove("SimpleObject/IntegerList/-1"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - var exception = Assert.Throws(() => - { - deserialized.ApplyTo(doc); - }); - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "-1"), - exception.Message); - } - - [Fact] - public void NestedRemoveFromEndOfList() - { - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove("SimpleObject/IntegerList/-"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal(new List() { 1, 2 }, doc.SimpleObject.IntegerList); - } - } -} diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/ReplaceOperationTests.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/ReplaceOperationTests.cs deleted file mode 100644 index 5d1fa77d85..0000000000 --- a/test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/ReplaceOperationTests.cs +++ /dev/null @@ -1,206 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.Dynamic; -using Xunit; - -namespace Microsoft.AspNetCore.JsonPatch.Internal -{ - public class ReplaceOperationTests - { - [Fact] - public void ReplaceGuidTest() - { - dynamic doc = new SimpleObject() - { - GuidValue = Guid.NewGuid() - }; - - var newGuid = Guid.NewGuid(); - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace("GuidValue", newGuid); - - patchDoc.ApplyTo(doc); - - Assert.Equal(newGuid, doc.GuidValue); - } - - [Fact] - public void ReplaceGuidTestExpandoObject() - { - dynamic doc = new ExpandoObject(); - doc.GuidValue = Guid.NewGuid(); - - var newGuid = Guid.NewGuid(); - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace("GuidValue", newGuid); - - patchDoc.ApplyTo(doc); - - Assert.Equal(newGuid, doc.GuidValue); - } - - [Fact] - public void ReplaceGuidTestExpandoObjectInAnonymous() - { - dynamic nestedObject = new ExpandoObject(); - nestedObject.GuidValue = Guid.NewGuid(); - - dynamic doc = new - { - NestedObject = nestedObject - }; - - var newGuid = Guid.NewGuid(); - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace("nestedobject/GuidValue", newGuid); - - patchDoc.ApplyTo(doc); - - Assert.Equal(newGuid, doc.NestedObject.GuidValue); - } - - [Fact] - public void ReplaceNestedObjectTest() - { - dynamic doc = new ExpandoObject(); - doc.SimpleDTO = new SimpleObject() - { - IntegerValue = 5, - IntegerList = new List() { 1, 2, 3 } - }; - - var newDTO = new SimpleObject() - { - DoubleValue = 1 - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace("SimpleDTO", newDTO); - - patchDoc.ApplyTo(doc); - - Assert.Equal(1, doc.SimpleDTO.DoubleValue); - Assert.Equal(0, doc.SimpleDTO.IntegerValue); - Assert.Null(doc.SimpleDTO.IntegerList); - } - - [Fact] - public void ReplaceInList() - { - dynamic doc = new ExpandoObject(); - doc.IntegerList = new List() { 1, 2, 3 }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace("IntegerList/0", 5); - - patchDoc.ApplyTo(doc); - - Assert.Equal(new List() { 5, 2, 3 }, doc.IntegerList); - } - - [Fact] - public void ReplaceFullList() - { - dynamic doc = new ExpandoObject(); - doc.IntegerList = new List() { 1, 2, 3 }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace("IntegerList", new List() { 4, 5, 6 }); - - patchDoc.ApplyTo(doc); - - Assert.Equal(new List() { 4, 5, 6 }, doc.IntegerList); - } - - [Fact] - public void ReplaceInListInList() - { - dynamic doc = new ExpandoObject(); - doc.SimpleDTOList = new List() { - new SimpleObject() { - IntegerList = new List(){1,2,3} - }}; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace("SimpleDTOList/0/IntegerList/0", 4); - - patchDoc.ApplyTo(doc); - - Assert.Equal(4, doc.SimpleDTOList[0].IntegerList[0]); - } - - [Fact] - public void ReplaceInListInListAtEnd() - { - dynamic doc = new ExpandoObject(); - doc.SimpleDTOList = new List() { - new SimpleObject() { - IntegerList = new List(){1,2,3} - }}; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace("SimpleDTOList/0/IntegerList/-", 4); - - patchDoc.ApplyTo(doc); - - Assert.Equal(4, doc.SimpleDTOList[0].IntegerList[2]); - } - - [Fact] - public void ReplaceFullListFromEnumerable() - { - dynamic doc = new ExpandoObject(); - doc.IntegerList = new List() { 1, 2, 3 }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace("IntegerList", new List() { 4, 5, 6 }); - - patchDoc.ApplyTo(doc); - - Assert.Equal(new List() { 4, 5, 6 }, doc.IntegerList); - } - - [Fact] - public void ReplaceFullListWithCollection() - { - dynamic doc = new ExpandoObject(); - doc.IntegerList = new List() { 1, 2, 3 }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace("IntegerList", new Collection() { 4, 5, 6 }); - - patchDoc.ApplyTo(doc); - - Assert.Equal(new List() { 4, 5, 6 }, doc.IntegerList); - } - - [Fact] - public void ReplaceAtEndOfList() - { - dynamic doc = new ExpandoObject(); - doc.IntegerList = new List() { 1, 2, 3 }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace("IntegerList/-", 5); - - patchDoc.ApplyTo(doc); - - Assert.Equal(new List() { 1, 2, 5 }, doc.IntegerList); - } - } -} diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/ReplaceTypedOperationTests.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/ReplaceTypedOperationTests.cs deleted file mode 100644 index 94131ff3b5..0000000000 --- a/test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/ReplaceTypedOperationTests.cs +++ /dev/null @@ -1,191 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.Linq; -using Newtonsoft.Json; -using Xunit; - -namespace Microsoft.AspNetCore.JsonPatch.Internal -{ - public class ReplaceTypedOperationTests - { - [Fact] - public void ReplaceGuidTest() - { - var doc = new SimpleObject() - { - GuidValue = Guid.NewGuid() - }; - - var newGuid = Guid.NewGuid(); - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace("GuidValue", newGuid); - - // serialize & deserialize - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserizalized = JsonConvert.DeserializeObject(serialized); - - deserizalized.ApplyTo(doc); - - Assert.Equal(newGuid, doc.GuidValue); - } - - [Fact] - public void SerializeAndReplaceNestedObjectTest() - { - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerValue = 5, - IntegerList = new List() { 1, 2, 3 } - } - }; - - var newDTO = new SimpleObject() - { - DoubleValue = 1 - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace("SimpleObject", newDTO); - - // serialize & deserialize - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal(1, doc.SimpleObject.DoubleValue); - Assert.Equal(0, doc.SimpleObject.IntegerValue); - Assert.Null(doc.SimpleObject.IntegerList); - } - - [Fact] - public void ReplaceInList() - { - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace("IntegerList/0", 5); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal(new List() { 5, 2, 3 }, doc.IntegerList); - } - - [Fact] - public void ReplaceFullList() - { - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace("IntegerList", new List() { 4, 5, 6 }); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal(new List() { 4, 5, 6 }, doc.IntegerList); - } - - [Fact] - public void ReplaceInListInList() - { - var doc = new SimpleObject() - { - SimpleObjectList = new List() { - new SimpleObject() { - IntegerList = new List(){1,2,3} - }} - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace("SimpleObjectList/0/IntegerList/0", 4); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal(4, doc.SimpleObjectList.First().IntegerList.First()); - } - - [Fact] - public void ReplaceFullListFromEnumerable() - { - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace("IntegerList", new List() { 4, 5, 6 }); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal(new List() { 4, 5, 6 }, doc.IntegerList); - } - - [Fact] - public void ReplaceFullListWithCollection() - { - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace("IntegerList", new Collection() { 4, 5, 6 }); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal(new List() { 4, 5, 6 }, doc.IntegerList); - } - - [Fact] - public void ReplaceAtEndOfList() - { - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace("IntegerList/-", 5); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject(serialized); - deserialized.ApplyTo(doc); - - Assert.Equal(new List() { 1, 2, 5 }, doc.IntegerList); - } - } -} diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/SimpleObject.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/SimpleObject.cs deleted file mode 100644 index 01a890d7be..0000000000 --- a/test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/SimpleObject.cs +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; - -namespace Microsoft.AspNetCore.JsonPatch.Internal -{ - public class SimpleObject - { - public List SimpleObjectList { get; set; } - public List IntegerList { get; set; } - public int IntegerValue { get; set; } - public string StringProperty { get; set; } - public string AnotherStringProperty { get; set; } - public decimal DecimalValue { get; set; } - public double DoubleValue { get; set; } - public float FloatValue { get; set; } - public Guid GuidValue { get; set; } - } -} diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/SimpleObjectWithNestedObject.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/SimpleObjectWithNestedObject.cs deleted file mode 100644 index aa766b8438..0000000000 --- a/test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/SimpleObjectWithNestedObject.cs +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System.Collections.Generic; - -namespace Microsoft.AspNetCore.JsonPatch.Internal -{ - public class SimpleObjectWithNestedObject - { - public int IntegerValue { get; set; } - public NestedObject NestedObject { get; set; } - public SimpleObject SimpleObject { get; set; } - public List ListOfSimpleObject { get; set; } - - public SimpleObjectWithNestedObject() - { - NestedObject = new NestedObject(); - SimpleObject = new SimpleObject(); - ListOfSimpleObject = new List(); - } - } -} diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/IntegrationTests/AnonymousObjectIntegrationTest.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/IntegrationTests/AnonymousObjectIntegrationTest.cs new file mode 100644 index 0000000000..4f290aae2f --- /dev/null +++ b/test/Microsoft.AspNetCore.JsonPatch.Test/IntegrationTests/AnonymousObjectIntegrationTest.cs @@ -0,0 +1,190 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using Microsoft.AspNetCore.JsonPatch.Exceptions; +using Xunit; + +namespace Microsoft.AspNetCore.JsonPatch.IntegrationTests +{ + public class AnonymousObjectIntegrationTest + { + [Fact] + public void AddNewProperty_ShouldFail() + { + // Arrange + var targetObject = new { }; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Add("NewProperty", 4); + + // Act + var exception = Assert.Throws(() => + { + patchDocument.ApplyTo(targetObject); + }); + + // Assert + Assert.Equal("The target location specified by path segment 'NewProperty' was not found.", + exception.Message); + } + + [Fact] + public void AddNewProperty_ToNestedAnonymousObject_ShouldFail() + { + // Arrange + dynamic targetObject = new + { + Test = 1, + nested = new { } + }; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Add("Nested/NewInt", 1); + + // Act + var exception = Assert.Throws(() => + { + patchDocument.ApplyTo(targetObject); + }); + + // Assert + Assert.Equal("The target location specified by path segment 'NewInt' was not found.", + exception.Message); + } + + [Fact] + public void AddDoesNotReplace() + { + // Arrange + var targetObject = new + { + StringProperty = "A" + }; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Add("StringProperty", "B"); + + // Act + var exception = Assert.Throws(() => + { + patchDocument.ApplyTo(targetObject); + }); + + // Assert + Assert.Equal("The property at path 'StringProperty' could not be updated.", + exception.Message); + } + + [Fact] + public void RemoveProperty_ShouldFail() + { + // Arrange + dynamic targetObject = new + { + Test = 1 + }; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Remove("Test"); + + // Act + var exception = Assert.Throws(() => + { + patchDocument.ApplyTo(targetObject); + }); + + // Assert + Assert.Equal("The property at path 'Test' could not be updated.", + exception.Message); + } + + [Fact] + public void ReplaceProperty_ShouldFail() + { + // Arrange + var targetObject = new + { + StringProperty = "A", + AnotherStringProperty = "B" + }; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Replace("StringProperty", "AnotherStringProperty"); + + // Act + var exception = Assert.Throws(() => + { + patchDocument.ApplyTo(targetObject); + }); + + // Assert + Assert.Equal("The property at path 'StringProperty' could not be updated.", + exception.Message); + } + + [Fact] + public void MoveProperty_ShouldFail() + { + // Arrange + var targetObject = new + { + StringProperty = "A", + AnotherStringProperty = "B" + }; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Move("StringProperty", "AnotherStringProperty"); + + // Act + var exception = Assert.Throws(() => + { + patchDocument.ApplyTo(targetObject); + }); + + // Assert + Assert.Equal("The property at path 'StringProperty' could not be updated.", + exception.Message); + } + + [Fact] + public void TestStringProperty_IsSucessful() + { + // Arrange + var targetObject = new + { + StringProperty = "A", + AnotherStringProperty = "B" + }; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Test("StringProperty", "A"); + + // Act & Assert + patchDocument.ApplyTo(targetObject); + } + + [Fact] + public void TestStringProperty_Fails() + { + // Arrange + var targetObject = new + { + StringProperty = "A", + AnotherStringProperty = "B" + }; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Test("StringProperty", "B"); + + // Act + var exception = Assert.Throws(() => + { + patchDocument.ApplyTo(targetObject); + }); + + // Assert + Assert.Equal("The current value 'A' at path 'StringProperty' is not equal to the test value 'B'.", + exception.Message); + } + } +} diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/IntegrationTests/DictionaryIntegrationTest.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/IntegrationTests/DictionaryIntegrationTest.cs new file mode 100644 index 0000000000..da990e3e8c --- /dev/null +++ b/test/Microsoft.AspNetCore.JsonPatch.Test/IntegrationTests/DictionaryIntegrationTest.cs @@ -0,0 +1,319 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System.Collections.Generic; +using Microsoft.AspNetCore.JsonPatch.Exceptions; +using Xunit; + +namespace Microsoft.AspNetCore.JsonPatch.IntegrationTests +{ + public class DictionaryTest + { + [Fact] + public void TestIntegerValue_IsSuccessful() + { + // Arrange + var model = new IntDictionary(); + model.DictionaryOfStringToInteger["one"] = 1; + model.DictionaryOfStringToInteger["two"] = 2; + var patchDocument = new JsonPatchDocument(); + patchDocument.Test("/DictionaryOfStringToInteger/two", 2); + + // Act & Assert + patchDocument.ApplyTo(model); + } + + [Fact] + public void AddIntegerValue_Succeeds() + { + // Arrange + var model = new IntDictionary(); + model.DictionaryOfStringToInteger["one"] = 1; + model.DictionaryOfStringToInteger["two"] = 2; + var patchDocument = new JsonPatchDocument(); + patchDocument.Add("/DictionaryOfStringToInteger/three", 3); + + // Act + patchDocument.ApplyTo(model); + + // Assert + Assert.Equal(3, model.DictionaryOfStringToInteger.Count); + Assert.Equal(1, model.DictionaryOfStringToInteger["one"]); + Assert.Equal(2, model.DictionaryOfStringToInteger["two"]); + Assert.Equal(3, model.DictionaryOfStringToInteger["three"]); + } + + [Fact] + public void RemoveIntegerValue_Succeeds() + { + // Arrange + var model = new IntDictionary(); + model.DictionaryOfStringToInteger["one"] = 1; + model.DictionaryOfStringToInteger["two"] = 2; + var patchDocument = new JsonPatchDocument(); + patchDocument.Remove("/DictionaryOfStringToInteger/two"); + + // Act + patchDocument.ApplyTo(model); + + // Assert + Assert.Equal(1, model.DictionaryOfStringToInteger.Count); + Assert.Equal(1, model.DictionaryOfStringToInteger["one"]); + } + + [Fact] + public void MoveIntegerValue_Succeeds() + { + // Arrange + var model = new IntDictionary(); + model.DictionaryOfStringToInteger["one"] = 1; + model.DictionaryOfStringToInteger["two"] = 2; + var patchDocument = new JsonPatchDocument(); + patchDocument.Move("/DictionaryOfStringToInteger/one", "/DictionaryOfStringToInteger/two"); + + // Act + patchDocument.ApplyTo(model); + + // Assert + Assert.Equal(1, model.DictionaryOfStringToInteger.Count); + Assert.Equal(1, model.DictionaryOfStringToInteger["two"]); + } + + [Fact] + public void ReplaceIntegerValue_Succeeds() + { + // Arrange + var model = new IntDictionary(); + model.DictionaryOfStringToInteger["one"] = 1; + model.DictionaryOfStringToInteger["two"] = 2; + var patchDocument = new JsonPatchDocument(); + patchDocument.Replace("/DictionaryOfStringToInteger/two", 20); + + // Act + patchDocument.ApplyTo(model); + + // Assert + Assert.Equal(2, model.DictionaryOfStringToInteger.Count); + Assert.Equal(1, model.DictionaryOfStringToInteger["one"]); + Assert.Equal(20, model.DictionaryOfStringToInteger["two"]); + } + + [Fact] + public void CopyIntegerValue_Succeeds() + { + // Arrange + var model = new IntDictionary(); + model.DictionaryOfStringToInteger["one"] = 1; + model.DictionaryOfStringToInteger["two"] = 2; + var patchDocument = new JsonPatchDocument(); + patchDocument.Copy("/DictionaryOfStringToInteger/one", "/DictionaryOfStringToInteger/two"); + + // Act + patchDocument.ApplyTo(model); + + // Assert + Assert.Equal(2, model.DictionaryOfStringToInteger.Count); + Assert.Equal(1, model.DictionaryOfStringToInteger["one"]); + Assert.Equal(1, model.DictionaryOfStringToInteger["two"]); + } + + private class Customer + { + public string Name { get; set; } + public Address Address { get; set; } + } + + private class Address + { + public string City { get; set; } + } + + private class IntDictionary + { + public IDictionary DictionaryOfStringToInteger { get; } = new Dictionary(); + } + + private class CustomerDictionary + { + public IDictionary DictionaryOfStringToCustomer { get; } = new Dictionary(); + } + + [Fact] + public void TestPocoObject_Succeeds() + { + // Arrange + var key1 = 100; + var value1 = new Customer() { Name = "James" }; + var model = new CustomerDictionary(); + model.DictionaryOfStringToCustomer[key1] = value1; + var patchDocument = new JsonPatchDocument(); + patchDocument.Test($"/DictionaryOfStringToCustomer/{key1}/Name", "James"); + + // Act & Assert + patchDocument.ApplyTo(model); + } + + [Fact] + public void TestPocoObject_FailsWhenTestValueIsNotEqualToObjectValue() + { + // Arrange + var key1 = 100; + var value1 = new Customer() { Name = "James" }; + var model = new CustomerDictionary(); + model.DictionaryOfStringToCustomer[key1] = value1; + var patchDocument = new JsonPatchDocument(); + patchDocument.Test($"/DictionaryOfStringToCustomer/{key1}/Name", "Mike"); + + // Act + var exception = Assert.Throws(() => + { + patchDocument.ApplyTo(model); + }); + + // Assert + Assert.Equal("The current value 'James' at path 'Name' is not equal to the test value 'Mike'.", exception.Message); + } + + [Fact] + public void AddReplacesPocoObject_Succeeds() + { + // Arrange + var key1 = 100; + var value1 = new Customer() { Name = "Jamesss" }; + var key2 = 200; + var value2 = new Customer() { Name = "Mike" }; + var model = new CustomerDictionary(); + model.DictionaryOfStringToCustomer[key1] = value1; + model.DictionaryOfStringToCustomer[key2] = value2; + var patchDocument = new JsonPatchDocument(); + patchDocument.Add($"/DictionaryOfStringToCustomer/{key1}/Name", "James"); + + // Act + patchDocument.ApplyTo(model); + + // Assert + Assert.Equal(2, model.DictionaryOfStringToCustomer.Count); + var actualValue1 = model.DictionaryOfStringToCustomer[key1]; + Assert.NotNull(actualValue1); + Assert.Equal("James", actualValue1.Name); + } + + [Fact] + public void RemovePocoObject_Succeeds() + { + // Arrange + var key1 = 100; + var value1 = new Customer() { Name = "Jamesss" }; + var key2 = 200; + var value2 = new Customer() { Name = "Mike" }; + var model = new CustomerDictionary(); + model.DictionaryOfStringToCustomer[key1] = value1; + model.DictionaryOfStringToCustomer[key2] = value2; + var patchDocument = new JsonPatchDocument(); + patchDocument.Remove($"/DictionaryOfStringToCustomer/{key1}/Name"); + + // Act + patchDocument.ApplyTo(model); + + // Assert + var actualValue1 = model.DictionaryOfStringToCustomer[key1]; + Assert.Null(actualValue1.Name); + } + + [Fact] + public void MovePocoObject_Succeeds() + { + // Arrange + var key1 = 100; + var value1 = new Customer() { Name = "James" }; + var key2 = 200; + var value2 = new Customer() { Name = "Mike" }; + var model = new CustomerDictionary(); + model.DictionaryOfStringToCustomer[key1] = value1; + model.DictionaryOfStringToCustomer[key2] = value2; + var patchDocument = new JsonPatchDocument(); + patchDocument.Move($"/DictionaryOfStringToCustomer/{key1}/Name", $"/DictionaryOfStringToCustomer/{key2}/Name"); + + // Act + patchDocument.ApplyTo(model); + + // Assert + var actualValue2 = model.DictionaryOfStringToCustomer[key2]; + Assert.NotNull(actualValue2); + Assert.Equal("James", actualValue2.Name); + } + + [Fact] + public void CopyPocoObject_Succeeds() + { + // Arrange + var key1 = 100; + var value1 = new Customer() { Name = "James" }; + var key2 = 200; + var value2 = new Customer() { Name = "Mike" }; + var model = new CustomerDictionary(); + model.DictionaryOfStringToCustomer[key1] = value1; + model.DictionaryOfStringToCustomer[key2] = value2; + var patchDocument = new JsonPatchDocument(); + patchDocument.Copy($"/DictionaryOfStringToCustomer/{key1}/Name", $"/DictionaryOfStringToCustomer/{key2}/Name"); + + // Act + patchDocument.ApplyTo(model); + + // Assert + Assert.Equal(2, model.DictionaryOfStringToCustomer.Count); + var actualValue2 = model.DictionaryOfStringToCustomer[key2]; + Assert.NotNull(actualValue2); + Assert.Equal("James", actualValue2.Name); + } + + [Fact] + public void ReplacePocoObject_Succeeds() + { + // Arrange + var key1 = 100; + var value1 = new Customer() { Name = "Jamesss" }; + var key2 = 200; + var value2 = new Customer() { Name = "Mike" }; + var model = new CustomerDictionary(); + model.DictionaryOfStringToCustomer[key1] = value1; + model.DictionaryOfStringToCustomer[key2] = value2; + var patchDocument = new JsonPatchDocument(); + patchDocument.Replace($"/DictionaryOfStringToCustomer/{key1}/Name", "James"); + + // Act + patchDocument.ApplyTo(model); + + // Assert + Assert.Equal(2, model.DictionaryOfStringToCustomer.Count); + var actualValue1 = model.DictionaryOfStringToCustomer[key1]; + Assert.NotNull(actualValue1); + Assert.Equal("James", actualValue1.Name); + } + + [Fact] + public void ReplacePocoObject_WithEscaping_Succeeds() + { + // Arrange + var key1 = "Foo/Name"; + var value1 = 100; + var key2 = "Foo"; + var value2 = 200; + var model = new IntDictionary(); + model.DictionaryOfStringToInteger[key1] = value1; + model.DictionaryOfStringToInteger[key2] = value2; + var patchDocument = new JsonPatchDocument(); + patchDocument.Replace($"/DictionaryOfStringToInteger/Foo~1Name", 300); + + // Act + patchDocument.ApplyTo(model); + + // Assert + Assert.Equal(2, model.DictionaryOfStringToInteger.Count); + var actualValue1 = model.DictionaryOfStringToInteger[key1]; + var actualValue2 = model.DictionaryOfStringToInteger[key2]; + Assert.Equal(300, actualValue1); + Assert.Equal(200, actualValue2); + } + } +} diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/IntegrationTests/DynamicObjectTest.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/IntegrationTests/DynamicObjectIntegrationTest.cs similarity index 58% rename from test/Microsoft.AspNetCore.JsonPatch.Test/IntegrationTests/DynamicObjectTest.cs rename to test/Microsoft.AspNetCore.JsonPatch.Test/IntegrationTests/DynamicObjectIntegrationTest.cs index be7738419f..ec15951a85 100644 --- a/test/Microsoft.AspNetCore.JsonPatch.Test/IntegrationTests/DynamicObjectTest.cs +++ b/test/Microsoft.AspNetCore.JsonPatch.Test/IntegrationTests/DynamicObjectIntegrationTest.cs @@ -5,9 +5,9 @@ using System.Collections.Generic; using Microsoft.AspNetCore.JsonPatch.Exceptions; using Xunit; -namespace Microsoft.AspNetCore.JsonPatch.Internal +namespace Microsoft.AspNetCore.JsonPatch.IntegrationTests { - public class DynamicObjectTest + public class DynamicObjectIntegrationTest { [Fact] public void AddResults_ShouldReplaceExistingPropertyValue_InNestedDynamicObject() @@ -20,11 +20,11 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal 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"); + var patchDocument = new JsonPatchDocument(); + patchDocument.Add("/Nested/DynamicProperty/InBetweenFirst/InBetweenSecond/StringProperty", "B"); // Act - patchDoc.ApplyTo(dynamicTestObject); + patchDocument.ApplyTo(dynamicTestObject); // Assert Assert.Equal("B", dynamicTestObject.Nested.DynamicProperty.InBetweenFirst.InBetweenSecond.StringProperty); @@ -53,21 +53,17 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal DynamicProperty = new DynamicTestObject() }; - var patchDoc = new JsonPatchDocument(); - patchDoc.Add("DynamicProperty/OtherProperty/IntProperty", 1); + var patchDocument = new JsonPatchDocument(); + patchDocument.Add("DynamicProperty/OtherProperty/IntProperty", 1); // Act var exception = Assert.Throws(() => { - patchDoc.ApplyTo(nestedObject); + patchDocument.ApplyTo(nestedObject); }); // Assert - Assert.Equal( - string.Format( - "The target location specified by path segment '{0}' was not found.", - "OtherProperty"), - exception.Message); + Assert.Equal("The target location specified by path segment 'OtherProperty' was not found.", exception.Message); } [Fact] @@ -79,11 +75,11 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal dynamicTestObject.NestedDynamicObject.StringProperty = "A"; dynamicTestObject.NestedDynamicObject.AnotherStringProperty = "B"; - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy("NestedDynamicObject/StringProperty", "NestedDynamicObject/AnotherStringProperty"); + var patchDocument = new JsonPatchDocument(); + patchDocument.Copy("NestedDynamicObject/StringProperty", "NestedDynamicObject/AnotherStringProperty"); // Act - patchDoc.ApplyTo(dynamicTestObject); + patchDocument.ApplyTo(dynamicTestObject); // Assert Assert.Equal("A", dynamicTestObject.NestedDynamicObject.AnotherStringProperty); @@ -97,11 +93,11 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal dynamic dynamicTestObject = new DynamicTestObject(); dynamicTestObject.StringProperty = "A"; - var patchDoc = new JsonPatchDocument(); - patchDoc.Move("StringProperty", "AnotherStringProperty"); + var patchDocument = new JsonPatchDocument(); + patchDocument.Move("StringProperty", "AnotherStringProperty"); // Act - patchDoc.ApplyTo(dynamicTestObject); + patchDocument.ApplyTo(dynamicTestObject); dynamicTestObject.TryGetValue("StringProperty", out object valueFromDictionary); // Assert @@ -117,11 +113,11 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal dynamicTestObject.StringProperty = "A"; dynamicTestObject.SimpleObject = new SimpleObject() { AnotherStringProperty = "B" }; - var patchDoc = new JsonPatchDocument(); - patchDoc.Move("StringProperty", "SimpleObject/AnotherStringProperty"); + var patchDocument = new JsonPatchDocument(); + patchDocument.Move("StringProperty", "SimpleObject/AnotherStringProperty"); // Act - patchDoc.ApplyTo(dynamicTestObject); + patchDocument.ApplyTo(dynamicTestObject); dynamicTestObject.TryGetValue("StringProperty", out object valueFromDictionary); // Assert @@ -129,27 +125,6 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal Assert.Null(valueFromDictionary); } - [Fact] - public void MovePropertyValue_FromListToNonList_InNestedTypedObject_InDynamicObject() - { - // Arrange - dynamic dynamicTestObject = new DynamicTestObject(); - dynamicTestObject.Nested = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - var patchDoc = new JsonPatchDocument(); - patchDoc.Move("Nested/IntegerList/0", "Nested/IntegerValue"); - - // Act - patchDoc.ApplyTo(dynamicTestObject); - - // Assert - Assert.Equal(new List() { 2, 3 }, dynamicTestObject.Nested.IntegerList); - Assert.Equal(1, dynamicTestObject.Nested.IntegerValue); - } - [Fact] public void RemoveNestedProperty_FromDynamicObject() { @@ -158,11 +133,11 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal dynamicTestObject.Test = new DynamicTestObject(); dynamicTestObject.Test.AnotherTest = "A"; - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove("Test"); + var patchDocument = new JsonPatchDocument(); + patchDocument.Remove("Test"); // Act - patchDoc.ApplyTo(dynamicTestObject); + patchDocument.ApplyTo(dynamicTestObject); dynamicTestObject.TryGetValue("Test", out object valueFromDictionary); // Assert @@ -179,41 +154,19 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal StringProperty = "A" }; - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove("Simpleobject/stringProperty"); + var patchDocument = new JsonPatchDocument(); + patchDocument.Remove("Simpleobject/stringProperty"); // Act var exception = Assert.Throws(() => { - patchDoc.ApplyTo(dynamicTestObject); + patchDocument.ApplyTo(dynamicTestObject); }); // Assert - Assert.Equal( - string.Format("The target location specified by path segment '{0}' was not found.", - "Simpleobject"), - exception.Message); + Assert.Equal("The target location specified by path segment 'Simpleobject' was not found.", exception.Message); } - [Fact] - public void RemoveFromList_NestedInDynamicObject() - { - // Arrange - dynamic dynamicTestObject = new DynamicTestObject(); - dynamicTestObject.SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove("SimpleObject/IntegerList/2"); - - // Act - patchDoc.ApplyTo(dynamicTestObject); - - // Assert - Assert.Equal(new List() { 1, 2 }, dynamicTestObject.SimpleObject.IntegerList); - } [Fact] public void ReplaceNestedTypedObject_InDynamicObject() @@ -231,11 +184,11 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal DoubleValue = 1 }; - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace("SimpleObject", newObject); + var patchDocument = new JsonPatchDocument(); + patchDocument.Replace("SimpleObject", newObject); // Act - patchDoc.ApplyTo(dynamicTestObject); + patchDocument.ApplyTo(dynamicTestObject); // Assert Assert.Equal(1, dynamicTestObject.SimpleObject.DoubleValue); @@ -244,41 +197,21 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal } [Fact] - public void ReplaceFullList_InDynamicObject() + public void TestStringPropertyValue_IsSuccessful() { // Arrange dynamic dynamicTestObject = new DynamicTestObject(); - dynamicTestObject.IntegerList = new List() { 1, 2, 3 }; + dynamicTestObject.Property = "A"; - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace("IntegerList", new List() { 4, 5, 6 }); - - // Act - patchDoc.ApplyTo(dynamicTestObject); - - // Assert - Assert.Equal(new List() { 4, 5, 6 }, dynamicTestObject.IntegerList); - } - - [Fact] - public void TestPropertyValue_FromListToNonList_InNestedTypedObject_InDynamicObject() - { - // Arrange - dynamic dynamicTestObject = new DynamicTestObject(); - dynamicTestObject.Nested = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - var patchDoc = new JsonPatchDocument(); - patchDoc.Test("Nested/IntegerList/1", 2); + var patchDocument = new JsonPatchDocument(); + patchDocument.Test("Property", "A"); // Act & Assert - patchDoc.ApplyTo(dynamicTestObject); + patchDocument.ApplyTo(dynamicTestObject); } [Fact] - public void TestPropertyValue_FromListToNonList_InNestedTypedObject_InDynamicObject_ThrowsJsonPatchException_IfTestFails() + public void TestIntegerPropertyValue_ThrowsJsonPatchException_IfTestFails() { // Arrange dynamic dynamicTestObject = new DynamicTestObject(); @@ -287,13 +220,13 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal IntegerList = new List() { 1, 2, 3 } }; - var patchDoc = new JsonPatchDocument(); - patchDoc.Test("Nested/IntegerList/0", 2); + var patchDocument = new JsonPatchDocument(); + patchDocument.Test("Nested/IntegerList/0", 2); // Act var exception = Assert.Throws(() => { - patchDoc.ApplyTo(dynamicTestObject); + patchDocument.ApplyTo(dynamicTestObject); }); // Assert diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/IntegrationTests/ExpandoObjectIntegrationTest.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/IntegrationTests/ExpandoObjectIntegrationTest.cs new file mode 100644 index 0000000000..b1b58556f0 --- /dev/null +++ b/test/Microsoft.AspNetCore.JsonPatch.Test/IntegrationTests/ExpandoObjectIntegrationTest.cs @@ -0,0 +1,327 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Collections.Generic; +using System.Dynamic; +using Microsoft.AspNetCore.JsonPatch.Exceptions; +using Xunit; + +namespace Microsoft.AspNetCore.JsonPatch.IntegrationTests +{ + public class ExpandoObjectIntegrationTest + { + [Fact] + public void AddNewIntProperty() + { + // Arrange + dynamic targetObject = new ExpandoObject(); + targetObject.Test = 1; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Add("NewInt", 1); + + // Act + patchDocument.ApplyTo(targetObject); + + // Assert + Assert.Equal(1, targetObject.NewInt); + Assert.Equal(1, targetObject.Test); + } + + [Fact] + public void AddNewProperty_ToTypedObject_InExpandoObject() + { + // Arrange + dynamic dynamicProperty = new ExpandoObject(); + dynamicProperty.StringProperty = "A"; + + var targetObject = new NestedObject() + { + DynamicProperty = dynamicProperty + }; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Add("DynamicProperty/StringProperty", "B"); + + // Act + patchDocument.ApplyTo(targetObject); + + // Assert + Assert.Equal("B", targetObject.DynamicProperty.StringProperty); + } + + [Fact] + public void AddReplaces_ExistingProperty() + { + // Arrange + dynamic targetObject = new ExpandoObject(); + targetObject.StringProperty = "A"; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Add("StringProperty", "B"); + + // Act + patchDocument.ApplyTo(targetObject); + + // Assert + Assert.Equal("B", targetObject.StringProperty); + } + + [Fact] + public void AddReplaces_ExistingProperty_InNestedExpandoObject() + { + // Arrange + dynamic targetObject = new ExpandoObject(); + targetObject.InBetweenFirst = new ExpandoObject(); + targetObject.InBetweenFirst.InBetweenSecond = new ExpandoObject(); + targetObject.InBetweenFirst.InBetweenSecond.StringProperty = "A"; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Add("/InBetweenFirst/InBetweenSecond/StringProperty", "B"); + + // Act + patchDocument.ApplyTo(targetObject); + + // Assert + Assert.Equal("B", targetObject.InBetweenFirst.InBetweenSecond.StringProperty); + } + + [Fact] + public void ShouldNotReplaceProperty_WithDifferentCase() + { + // Arrange + dynamic targetObject = new ExpandoObject(); + targetObject.StringProperty = "A"; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Add("stringproperty", "B"); + + // Act + patchDocument.ApplyTo(targetObject); + + // Assert + Assert.Equal("A", targetObject.StringProperty); + Assert.Equal("B", targetObject.stringproperty); + } + + [Fact] + public void TestIntegerProperty_IsSucessful() + { + // Arrange + dynamic targetObject = new ExpandoObject(); + targetObject.Test = 1; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Test("Test", 1); + + // Act & Assert + patchDocument.ApplyTo(targetObject); + } + + [Fact] + public void TestStringProperty_ThrowsJsonPatchException_IfTestFails() + { + // Arrange + dynamic targetObject = new ExpandoObject(); + targetObject.Test = "Value"; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Test("Test", "TestValue"); + + // Act + var exception = Assert.Throws(() => + { + patchDocument.ApplyTo(targetObject); + }); + + // Assert + Assert.Equal("The current value 'Value' at path 'Test' is not equal to the test value 'TestValue'.", + exception.Message); + } + + [Fact] + public void CopyStringProperty_ToAnotherStringProperty() + { + // Arrange + dynamic targetObject = new ExpandoObject(); + + targetObject.StringProperty = "A"; + targetObject.AnotherStringProperty = "B"; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Copy("StringProperty", "AnotherStringProperty"); + + // Act + patchDocument.ApplyTo(targetObject); + + // Assert + Assert.Equal("A", targetObject.AnotherStringProperty); + } + + [Fact] + public void MoveIntegerValue_ToAnotherIntegerProperty() + { + // Arrange + dynamic targetObject = new ExpandoObject(); + targetObject.IntegerValue = 100; + targetObject.AnotherIntegerValue = 200; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Move("IntegerValue", "AnotherIntegerValue"); + + // Act + patchDocument.ApplyTo(targetObject); + + Assert.Equal(100, targetObject.AnotherIntegerValue); + + var cont = targetObject as IDictionary; + cont.TryGetValue("IntegerValue", out object valueFromDictionary); + + // Assert + Assert.Null(valueFromDictionary); + } + + [Fact] + public void Move_ToNonExistingProperty() + { + // Arrange + dynamic targetObject = new ExpandoObject(); + targetObject.StringProperty = "A"; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Move("StringProperty", "AnotherStringProperty"); + + // Act + patchDocument.ApplyTo(targetObject); + + Assert.Equal("A", targetObject.AnotherStringProperty); + + var cont = targetObject as IDictionary; + cont.TryGetValue("StringProperty", out var valueFromDictionary); + + // Assert + Assert.Null(valueFromDictionary); + } + + [Fact] + public void RemoveProperty_ShouldFail_IfItDoesntExist() + { + // Arrange + dynamic targetObject = new ExpandoObject(); + targetObject.Test = 1; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Remove("NonExisting"); + + // Act + var exception = Assert.Throws(() => + { + patchDocument.ApplyTo(targetObject); + }); + + // Assert + Assert.Equal("The target location specified by path segment 'NonExisting' was not found.", exception.Message); + } + + [Fact] + public void RemoveStringProperty() + { + // Arrange + dynamic targetObject = new ExpandoObject(); + targetObject.Test = 1; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Remove("Test"); + + // Act + patchDocument.ApplyTo(targetObject); + + var cont = targetObject as IDictionary; + cont.TryGetValue("Test", out object valueFromDictionary); + + // Assert + Assert.Null(valueFromDictionary); + } + + [Fact] + public void RemoveProperty_MixedCase_ThrowsPathNotFoundException() + { + // Arrange + dynamic targetObject = new ExpandoObject(); + targetObject.Test = 1; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Remove("test"); + + // Act + var exception = Assert.Throws(() => + { + patchDocument.ApplyTo(targetObject); + }); + + // Assert + Assert.Equal("The target location specified by path segment 'test' was not found.", exception.Message); + } + + [Fact] + public void RemoveNestedProperty() + { + // Arrange + dynamic targetObject = new ExpandoObject(); + targetObject.Test = new ExpandoObject(); + targetObject.Test.AnotherTest = "A"; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Remove("Test"); + + // Act + patchDocument.ApplyTo(targetObject); + + var cont = targetObject as IDictionary; + cont.TryGetValue("Test", out object valueFromDictionary); + + // Assert + Assert.Null(valueFromDictionary); + } + + [Fact] + public void RemoveNestedProperty_MixedCase_ThrowsPathNotFoundException() + { + // Arrange + dynamic targetObject = new ExpandoObject(); + targetObject.Test = new ExpandoObject(); + targetObject.Test.AnotherTest = "A"; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Remove("test"); + + // Act + var exception = Assert.Throws(() => + { + patchDocument.ApplyTo(targetObject); + }); + + // Assert + Assert.Equal("The target location specified by path segment 'test' was not found.", exception.Message); + } + + [Fact] + public void ReplaceGuid() + { + // Arrange + dynamic targetObject = new ExpandoObject(); + targetObject.GuidValue = Guid.NewGuid(); + + var newGuid = Guid.NewGuid(); + var patchDocument = new JsonPatchDocument(); + patchDocument.Replace("GuidValue", newGuid); + + // Act + patchDocument.ApplyTo(targetObject); + + // Assert + Assert.Equal(newGuid, targetObject.GuidValue); + } + } +} \ No newline at end of file diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/IntegrationTests/ListIntegrationTest.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/IntegrationTests/ListIntegrationTest.cs new file mode 100644 index 0000000000..5e261ea08b --- /dev/null +++ b/test/Microsoft.AspNetCore.JsonPatch.Test/IntegrationTests/ListIntegrationTest.cs @@ -0,0 +1,366 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System.Collections.Generic; +using System.Collections.ObjectModel; +using Microsoft.AspNetCore.JsonPatch.Exceptions; +using Xunit; + +namespace Microsoft.AspNetCore.JsonPatch.IntegrationTests +{ + public class ListIntegrationTest + { + [Fact] + public void TestInList_IsSuccessful() + { + // Arrange + var targetObject = new SimpleObjectWithNestedObject() + { + SimpleObject = new SimpleObject() + { + IntegerList = new List() { 1, 2, 3 } + } + }; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Test(o => o.SimpleObject.IntegerList, 3, 2); + + // Act & Assert + patchDocument.ApplyTo(targetObject); + } + + [Fact] + public void TestInList_InvalidPosition() + { + // Arrange + var targetObject = new SimpleObjectWithNestedObject() + { + SimpleObject = new SimpleObject() + { + IntegerList = new List() { 1, 2, 3 } + } + }; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Test(o => o.SimpleObject.IntegerList, 4, -1); + + // Act & Assert + var exception = Assert.Throws(() => { patchDocument.ApplyTo(targetObject); }); + Assert.Equal("The index value provided by path segment '-1' is out of bounds of the array size.", + exception.Message); + } + + [Fact] + public void AddToIntegerIList() + { + // Arrange + var targetObject = new SimpleObjectWithNestedObject() + { + SimpleObject = new SimpleObject() + { + IntegerIList = new List() { 1, 2, 3 } + } + }; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Add(o => (List)o.SimpleObject.IntegerIList, 4, 0); + + // Act + patchDocument.ApplyTo(targetObject); + + // Assert + Assert.Equal(new List() { 4, 1, 2, 3 }, targetObject.SimpleObject.IntegerIList); + } + + [Fact] + public void AddToComplextTypeList_SpecifyIndex() + { + // Arrange + var targetObject = new SimpleObjectWithNestedObject() + { + SimpleObjectList = new List() + { + new SimpleObject + { + StringProperty = "String1" + }, + new SimpleObject + { + StringProperty = "String2" + } + } + }; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Add(o => o.SimpleObjectList[0].StringProperty, "ChangedString1"); + + // Act + patchDocument.ApplyTo(targetObject); + + // Assert + Assert.Equal("ChangedString1", targetObject.SimpleObjectList[0].StringProperty); + } + + [Fact] + public void AddToListAppend() + { + // Arrange + var targetObject = new SimpleObjectWithNestedObject() + { + SimpleObject = new SimpleObject() + { + IntegerList = new List() { 1, 2, 3 } + } + }; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Add(o => o.SimpleObject.IntegerList, 4); + + // Act + patchDocument.ApplyTo(targetObject); + + // Assert + Assert.Equal(new List() { 1, 2, 3, 4 }, targetObject.SimpleObject.IntegerList); + } + + [Fact] + public void RemoveFromList() + { + // Arrange + var targetObject = new SimpleObject() + { + IntegerList = new List() { 1, 2, 3 } + }; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Remove("IntegerList/2"); + + // Act + patchDocument.ApplyTo(targetObject); + + // Assert + Assert.Equal(new List() { 1, 2 }, targetObject.IntegerList); + } + + [Theory] + [InlineData("3")] + [InlineData("-1")] + public void RemoveFromList_InvalidPosition(string position) + { + // Arrange + var targetObject = new SimpleObject() + { + IntegerList = new List() { 1, 2, 3 } + }; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Remove("IntegerList/" + position); + + // Act + var exception = Assert.Throws(() => + { + patchDocument.ApplyTo(targetObject); + }); + + // Assert + Assert.Equal($"The index value provided by path segment '{position}' is out of bounds of the array size.", exception.Message); + } + + [Fact] + public void Remove_FromEndOfList() + { + // Arrange + var targetObject = new SimpleObjectWithNestedObject() + { + SimpleObject = new SimpleObject() + { + IntegerList = new List() { 1, 2, 3 } + } + }; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Remove(o => o.SimpleObject.IntegerList); + + // Act + patchDocument.ApplyTo(targetObject); + + // Assert + Assert.Equal(new List() { 1, 2 }, targetObject.SimpleObject.IntegerList); + } + + [Fact] + public void ReplaceFullList_WithCollection() + { + // Arrange + var targetObject = new SimpleObject() + { + IntegerList = new List() { 1, 2, 3 } + }; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Replace("IntegerList", new Collection() { 4, 5, 6 }); + + // Act + patchDocument.ApplyTo(targetObject); + + // Assert + Assert.Equal(new List() { 4, 5, 6 }, targetObject.IntegerList); + } + + [Fact] + public void Replace_AtEndOfList() + { + // Arrange + var targetObject = new SimpleObjectWithNestedObject() + { + SimpleObject = new SimpleObject() + { + IntegerList = new List() { 1, 2, 3 } + } + }; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Replace(o => o.SimpleObject.IntegerList, 5); + + // Act + patchDocument.ApplyTo(targetObject); + + // Assert + Assert.Equal(new List() { 1, 2, 5 }, targetObject.SimpleObject.IntegerList); + } + + [Fact] + public void Replace_InList_InvalidPosition() + { + // Arrange + var targetObject = new SimpleObjectWithNestedObject() + { + SimpleObject = new SimpleObject() + { + IntegerList = new List() { 1, 2, 3 } + } + }; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Replace(o => o.SimpleObject.IntegerList, 5, -1); + + // Act + var exception = Assert.Throws(() => { patchDocument.ApplyTo(targetObject); }); + + // Assert + Assert.Equal("The index value provided by path segment '-1' is out of bounds of the array size.", exception.Message); + } + + [Fact] + public void CopyFromListToEndOfList() + { + // Arrange + var targetObject = new SimpleObject() + { + IntegerList = new List() { 1, 2, 3 } + }; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Copy("IntegerList/0", "IntegerList/-"); + + // Act + patchDocument.ApplyTo(targetObject); + + // Assert + Assert.Equal(new List() { 1, 2, 3, 1 }, targetObject.IntegerList); + } + + [Fact] + public void CopyFromListToNonList() + { + // Arrange + var targetObject = new SimpleObject() + { + IntegerList = new List() { 1, 2, 3 } + }; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Copy("IntegerList/0", "IntegerValue"); + + // Act + patchDocument.ApplyTo(targetObject); + + // Assert + Assert.Equal(1, targetObject.IntegerValue); + } + + [Fact] + public void MoveToEndOfList() + { + // Arrange + var targetObject = new SimpleObject() + { + IntegerValue = 5, + IntegerList = new List() { 1, 2, 3 } + }; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Move("IntegerValue", "IntegerList/-"); + + // Act + patchDocument.ApplyTo(targetObject); + + // Assert + Assert.Equal(0, targetObject.IntegerValue); + Assert.Equal(new List() { 1, 2, 3, 5 }, targetObject.IntegerList); + } + + [Fact] + public void Move_KeepsObjectReferenceInList() + { + // Arrange + var simpleObject1 = new SimpleObject() { IntegerValue = 1 }; + var simpleObject2 = new SimpleObject() { IntegerValue = 2 }; + var simpleObject3 = new SimpleObject() { IntegerValue = 3 }; + var targetObject = new SimpleObjectWithNestedObject() + { + SimpleObjectList = new List() { + simpleObject1, + simpleObject2, + simpleObject3 + } + }; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Move(o => o.SimpleObjectList, 0, o => o.SimpleObjectList, 1); + + // Act + patchDocument.ApplyTo(targetObject); + + // Assert + Assert.Equal(new List() { simpleObject2, simpleObject1, simpleObject3 }, targetObject.SimpleObjectList); + Assert.Equal(2, targetObject.SimpleObjectList[0].IntegerValue); + Assert.Equal(1, targetObject.SimpleObjectList[1].IntegerValue); + Assert.Same(simpleObject2, targetObject.SimpleObjectList[0]); + Assert.Same(simpleObject1, targetObject.SimpleObjectList[1]); + } + + [Fact] + public void MoveFromList_ToNonList_BetweenHierarchy() + { + // Arrange + var targetObject = new SimpleObjectWithNestedObject() + { + SimpleObject = new SimpleObject() + { + IntegerList = new List() { 1, 2, 3 } + } + }; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Move(o => o.SimpleObject.IntegerList, 0, o => o.IntegerValue); + + // Act + patchDocument.ApplyTo(targetObject); + + // Assert + Assert.Equal(new List() { 2, 3 }, targetObject.SimpleObject.IntegerList); + Assert.Equal(1, targetObject.IntegerValue); + } + } +} diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/IntegrationTests/NestedObjectIntegrationTest.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/IntegrationTests/NestedObjectIntegrationTest.cs new file mode 100644 index 0000000000..92c0e7fb2d --- /dev/null +++ b/test/Microsoft.AspNetCore.JsonPatch.Test/IntegrationTests/NestedObjectIntegrationTest.cs @@ -0,0 +1,342 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Dynamic; +using Newtonsoft.Json; +using Xunit; + +namespace Microsoft.AspNetCore.JsonPatch.IntegrationTests +{ + public class NestedObjectIntegrationTest + { + [Fact] + public void Replace_DTOWithNullCheck() + { + // Arrange + var targetObject = new SimpleObjectWithNestedObjectWithNullCheck() + { + SimpleObjectWithNullCheck = new SimpleObjectWithNullCheck() + { + StringProperty = "A" + } + }; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Replace(o => o.SimpleObjectWithNullCheck.StringProperty, "B"); + + // Act + patchDocument.ApplyTo(targetObject); + + // Assert + Assert.Equal("B", targetObject.SimpleObjectWithNullCheck.StringProperty); + } + + [Fact] + public void ReplaceNestedObject_WithSerialization() + { + // Arrange + var targetObject = new SimpleObjectWithNestedObject() + { + IntegerValue = 1 + }; + + var newNested = new NestedObject() { StringProperty = "B" }; + var patchDocument = new JsonPatchDocument(); + patchDocument.Replace(o => o.NestedObject, newNested); + + var serialized = JsonConvert.SerializeObject(patchDocument); + var deserialized = JsonConvert.DeserializeObject>(serialized); + + // Act + deserialized.ApplyTo(targetObject); + + // Assert + Assert.Equal("B", targetObject.NestedObject.StringProperty); + } + + [Fact] + public void TestStringProperty_InNestedObject() + { + // Arrange + var targetObject = new SimpleObjectWithNestedObject() + { + NestedObject = new NestedObject() { StringProperty = "A"} + }; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Test(o => o.StringProperty, "A"); + + // Act + patchDocument.ApplyTo(targetObject.NestedObject); + + // Assert + Assert.Equal("A", targetObject.NestedObject.StringProperty); + } + + [Fact] + public void TestNestedObject() + { + // Arrange + var targetObject = new SimpleObjectWithNestedObject() + { + NestedObject = new NestedObject() { StringProperty = "B"} + }; + + var testNested = new NestedObject() { StringProperty = "B" }; + var patchDocument = new JsonPatchDocument(); + patchDocument.Test(o => o.NestedObject, testNested); + + // Act + patchDocument.ApplyTo(targetObject); + + // Assert + Assert.Equal("B", targetObject.NestedObject.StringProperty); + } + + [Fact] + public void AddReplaces_ExistingStringProperty() + { + // Arrange + var targetObject = new SimpleObjectWithNestedObject() + { + SimpleObject = new SimpleObject() + { + StringProperty = "A" + } + }; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Add(o => o.SimpleObject.StringProperty, "B"); + + // Act + patchDocument.ApplyTo(targetObject); + + // Assert + Assert.Equal("B", targetObject.SimpleObject.StringProperty); + } + + [Fact] + public void AddNewProperty_ToExpandoOject_InTypedObject() + { + var targetObject = new NestedObject() + { + DynamicProperty = new ExpandoObject() + }; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Add("DynamicProperty/NewInt", 1); + + patchDocument.ApplyTo(targetObject); + + Assert.Equal(1, targetObject.DynamicProperty.NewInt); + } + + [Fact] + public void RemoveStringProperty() + { + // Arrange + var targetObject = new SimpleObjectWithNestedObject() + { + SimpleObject = new SimpleObject() + { + StringProperty = "A" + } + }; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Remove(o => o.SimpleObject.StringProperty); + + // Act + patchDocument.ApplyTo(targetObject); + + // Assert + Assert.Null(targetObject.SimpleObject.StringProperty); + } + + [Fact] + public void CopyStringProperty_ToAnotherStringProperty() + { + // Arrange + var targetObject = new SimpleObjectWithNestedObject() + { + SimpleObject = new SimpleObject() + { + StringProperty = "A", + AnotherStringProperty = "B" + } + }; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Copy(o => o.SimpleObject.StringProperty, o => o.SimpleObject.AnotherStringProperty); + + // Act + patchDocument.ApplyTo(targetObject); + + // Assert + Assert.Equal("A", targetObject.SimpleObject.AnotherStringProperty); + } + + [Fact] + public void Copy_DeepClonesObject() + { + // Arrange + var targetObject = new SimpleObjectWithNestedObject() + { + SimpleObject = new SimpleObject() + { + StringProperty = "A", + AnotherStringProperty = "B" + }, + InheritedObject = new InheritedObject() + { + StringProperty = "C", + AnotherStringProperty = "D" + } + }; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Copy(o => o.InheritedObject, o => o.SimpleObject); + + // Act + patchDocument.ApplyTo(targetObject); + + // Assert + Assert.Equal("C", targetObject.SimpleObject.StringProperty); + Assert.Equal("D", targetObject.SimpleObject.AnotherStringProperty); + Assert.Equal("C", targetObject.InheritedObject.StringProperty); + Assert.Equal("D", targetObject.InheritedObject.AnotherStringProperty); + Assert.NotSame(targetObject.SimpleObject.StringProperty, targetObject.InheritedObject.StringProperty); + } + + [Fact] + public void Copy_KeepsObjectType() + { + // Arrange + var targetObject = new SimpleObjectWithNestedObject() + { + SimpleObject = new SimpleObject(), + InheritedObject = new InheritedObject() + }; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Copy(o => o.InheritedObject, o => o.SimpleObject); + + // Act + patchDocument.ApplyTo(targetObject); + + // Assert + Assert.Equal(typeof(InheritedObject), targetObject.SimpleObject.GetType()); + } + + [Fact] + public void Copy_BreaksObjectReference() + { + // Arrange + var targetObject = new SimpleObjectWithNestedObject() + { + SimpleObject = new SimpleObject(), + InheritedObject = new InheritedObject() + }; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Copy(o => o.InheritedObject, o => o.SimpleObject); + + // Act + patchDocument.ApplyTo(targetObject); + + // Assert + Assert.NotSame(targetObject.SimpleObject, targetObject.InheritedObject); + } + + [Fact] + public void MoveIntegerValue_ToAnotherIntegerProperty() + { + // Arrange + var targetObject = new SimpleObjectWithNestedObject() + { + SimpleObject = new SimpleObject() + { + IntegerValue = 2, + AnotherIntegerValue = 3 + } + }; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Move(o => o.SimpleObject.IntegerValue, o => o.SimpleObject.AnotherIntegerValue); + + // Act + patchDocument.ApplyTo(targetObject); + + // Assert + Assert.Equal(2, targetObject.SimpleObject.AnotherIntegerValue); + Assert.Equal(0, targetObject.SimpleObject.IntegerValue); + } + + [Fact] + public void Move_KeepsObjectReference() + { + // Arrange + var sDto = new SimpleObject() + { + StringProperty = "A", + AnotherStringProperty = "B" + }; + var iDto = new InheritedObject() + { + StringProperty = "C", + AnotherStringProperty = "D" + }; + var targetObject = new SimpleObjectWithNestedObject() + { + SimpleObject = sDto, + InheritedObject = iDto + }; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Move(o => o.InheritedObject, o => o.SimpleObject); + + // Act + patchDocument.ApplyTo(targetObject); + + // Assert + Assert.Equal("C", targetObject.SimpleObject.StringProperty); + Assert.Equal("D", targetObject.SimpleObject.AnotherStringProperty); + Assert.Same(iDto, targetObject.SimpleObject); + Assert.Null(targetObject.InheritedObject); + } + + private class SimpleObjectWithNullCheck + { + private string stringProperty; + + public string StringProperty + { + get + { + return stringProperty; + } + + set + { + if (value == null) + { + throw new ArgumentNullException(); + } + + stringProperty = value; + } + } + } + + private class SimpleObjectWithNestedObjectWithNullCheck + { + public SimpleObjectWithNullCheck SimpleObjectWithNullCheck { get; set; } + + public SimpleObjectWithNestedObjectWithNullCheck() + { + SimpleObjectWithNullCheck = new SimpleObjectWithNullCheck(); + } + } + } +} diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/IntegrationTests/SimpleObjectIntegrationTest.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/IntegrationTests/SimpleObjectIntegrationTest.cs new file mode 100644 index 0000000000..669c6d7af4 --- /dev/null +++ b/test/Microsoft.AspNetCore.JsonPatch.Test/IntegrationTests/SimpleObjectIntegrationTest.cs @@ -0,0 +1,128 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using Xunit; + +namespace Microsoft.AspNetCore.JsonPatch.IntegrationTests +{ + public class SimpleObjectIntegrationTest + { + [Fact] + public void TestDoubleValueProperty() + { + // Arrange + var targetObject = new SimpleObject() + { + DoubleValue = 9.8 + }; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Test("DoubleValue", 9.8); + + // Act & Assert + patchDocument.ApplyTo(targetObject); + } + + [Fact] + public void CopyStringProperty_ToAnotherStringProperty() + { + // Arrange + var targetObject = new SimpleObject() + { + StringProperty = "A", + AnotherStringProperty = "B" + }; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Copy("StringProperty", "AnotherStringProperty"); + + // Act + patchDocument.ApplyTo(targetObject); + + // Assert + Assert.Equal("A", targetObject.AnotherStringProperty); + } + + [Fact] + public void MoveIntegerProperty_ToAnotherIntegerProperty() + { + // Arrange + var targetObject = new SimpleObject() + { + IntegerValue = 2, + AnotherIntegerValue = 3 + }; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Move("IntegerValue", "AnotherIntegerValue"); + + // Act + patchDocument.ApplyTo(targetObject); + + // Assert + Assert.Equal(2, targetObject.AnotherIntegerValue); + Assert.Equal(0, targetObject.IntegerValue); + } + + [Fact] + public void RemoveDecimalPropertyValue() + { + // Arrange + var targetObject = new SimpleObject() + { + DecimalValue = 9.8M + }; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Remove("DecimalValue"); + + // Act + patchDocument.ApplyTo(targetObject); + + // Assert + Assert.Equal(0, targetObject.DecimalValue); + } + + [Fact] + public void ReplaceGuid() + { + // Arrange + var targetObject = new SimpleObject() + { + GuidValue = Guid.NewGuid() + }; + + var newGuid = Guid.NewGuid(); + var patchDocument = new JsonPatchDocument(); + patchDocument.Replace("GuidValue", newGuid); + + // Act + patchDocument.ApplyTo(targetObject); + + // Assert + Assert.Equal(newGuid, targetObject.GuidValue); + } + + [Fact] + public void AddReplacesGuid() + { + // Arrange + var targetObject = new SimpleObject() + { + GuidValue = Guid.NewGuid() + }; + + var newGuid = Guid.NewGuid(); + var patchDocument = new JsonPatchDocument(); + patchDocument.Add("GuidValue", newGuid); + + // Act + patchDocument.ApplyTo(targetObject); + + // Assert + Assert.Equal(newGuid, targetObject.GuidValue); + } + + } +} diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/Adapters/DictionaryAdapterTest.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/Internal/DictionaryAdapterTest.cs similarity index 91% rename from test/Microsoft.AspNetCore.JsonPatch.Test/Adapters/DictionaryAdapterTest.cs rename to test/Microsoft.AspNetCore.JsonPatch.Test/Internal/DictionaryAdapterTest.cs index d1d2216c2c..a0dcd82ab0 100644 --- a/test/Microsoft.AspNetCore.JsonPatch.Test/Adapters/DictionaryAdapterTest.cs +++ b/test/Microsoft.AspNetCore.JsonPatch.Test/Internal/DictionaryAdapterTest.cs @@ -74,9 +74,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal // Assert Assert.False(getStatus); - Assert.Equal( - string.Format("The provided path segment '{0}' cannot be converted to the target type.", guidKey.ToString()), - message); + Assert.Equal($"The provided path segment '{guidKey.ToString()}' cannot be converted to the target type.", message); Assert.Null(outValue); } @@ -103,9 +101,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal // Assert Assert.False(getStatus); - Assert.Equal( - string.Format("The target location specified by path segment '{0}' was not found.", nameKey.ToUpper()), - message); + Assert.Equal("The target location specified by path segment 'NAME' was not found.", message); Assert.Null(outValue); } @@ -191,9 +187,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal // Assert Assert.False(replaceStatus); - Assert.Equal( - string.Format("The value '{0}' is invalid for target location.", "test"), - message); + Assert.Equal("The value 'test' is invalid for target location.", message); Assert.Equal(5, dictionary[guidKey]); } @@ -211,9 +205,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal // Assert Assert.False(replaceStatus); - Assert.Equal( - string.Format("The target location specified by path segment '{0}' was not found.", nameKey), - message); + Assert.Equal("The target location specified by path segment 'Name' was not found.", message); Assert.Empty(dictionary); } @@ -231,9 +223,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal // Assert Assert.False(removeStatus); - Assert.Equal( - string.Format("The target location specified by path segment '{0}' was not found.", nameKey), - message); + Assert.Equal("The target location specified by path segment 'Name' was not found.", message); Assert.Empty(dictionary); } @@ -280,13 +270,19 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal { // Arrange var key = "Name"; - var dictionary = new Dictionary(); - dictionary[key] = "James"; - var dictionaryAdapter = new DictionaryAdapter(); + var dictionary = new Dictionary>(); + var value = new List() + { + "James", + 2, + new Customer("James", 25) + }; + dictionary[key] = value; + var dictionaryAdapter = new DictionaryAdapter>(); var resolver = new DefaultContractResolver(); // Act - var testStatus = dictionaryAdapter.TryTest(dictionary, key, resolver, "James", out var message); + var testStatus = dictionaryAdapter.TryTest(dictionary, key, resolver, value, out var message); //Assert Assert.True(testStatus); diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/Adapters/DynamicObjectAdapterTest.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/Internal/DynamicObjectAdapterTest.cs similarity index 97% rename from test/Microsoft.AspNetCore.JsonPatch.Test/Adapters/DynamicObjectAdapterTest.cs rename to test/Microsoft.AspNetCore.JsonPatch.Test/Internal/DynamicObjectAdapterTest.cs index 9ed9a51ed0..96b1aee935 100644 --- a/test/Microsoft.AspNetCore.JsonPatch.Test/Adapters/DynamicObjectAdapterTest.cs +++ b/test/Microsoft.AspNetCore.JsonPatch.Test/Internal/DynamicObjectAdapterTest.cs @@ -233,15 +233,21 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal { var adapter = new DynamicObjectAdapter(); dynamic target = new DynamicTestObject(); - target.NewProperty = "Joana"; + var value = new List() + { + "Joana", + 2, + new Customer("Joana", 25) + }; + target.NewProperty = value; var segment = "NewProperty"; var resolver = new DefaultContractResolver(); // Act - var testStatus = adapter.TryTest(target, segment, resolver, "Joana", out string errorMessage); + var testStatus = adapter.TryTest(target, segment, resolver, value, out string errorMessage); // Assert - Assert.Equal("Joana", target.NewProperty); + Assert.Equal(value, target.NewProperty); Assert.True(testStatus); Assert.True(string.IsNullOrEmpty(errorMessage), "Expected no error message"); } diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/Adapters/ListAdapterTest.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/Internal/ListAdapterTest.cs similarity index 91% rename from test/Microsoft.AspNetCore.JsonPatch.Test/Adapters/ListAdapterTest.cs rename to test/Microsoft.AspNetCore.JsonPatch.Test/Internal/ListAdapterTest.cs index 25d8ae387b..f31e57541b 100644 --- a/test/Microsoft.AspNetCore.JsonPatch.Test/Adapters/ListAdapterTest.cs +++ b/test/Microsoft.AspNetCore.JsonPatch.Test/Internal/ListAdapterTest.cs @@ -24,11 +24,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal // Assert Assert.False(addStatus); - Assert.Equal( - string.Format( - "The type '{0}' which is an array is not supported for json patch operations as it has a fixed size.", - targetObject.GetType().FullName), - message); + Assert.Equal($"The type '{targetObject.GetType().FullName}' which is an array is not supported for json patch operations as it has a fixed size.", message); } [Fact] @@ -46,11 +42,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal // Assert Assert.False(addStatus); - Assert.Equal( - string.Format( - "The type '{0}' which is a non generic list is not supported for json patch operations. Only generic list types are supported.", - targetObject.GetType().FullName), - message); + Assert.Equal($"The type '{targetObject.GetType().FullName}' which is a non generic list is not supported for json patch operations. Only generic list types are supported.", message); } [Fact] @@ -88,9 +80,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal // Assert Assert.False(addStatus); - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", position), - message); + Assert.Equal($"The index value provided by path segment '{position}' is out of bounds of the array size.", message); } [Theory] @@ -108,9 +98,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal // Assert Assert.False(addStatus); - Assert.Equal( - string.Format("The path segment '{0}' is invalid for an array index.", position), - message); + Assert.Equal($"The path segment '{position}' is invalid for an array index.", message); } public static TheoryData, List> AppendAtEndOfListData @@ -200,7 +188,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal // Assert Assert.False(addStatus); - Assert.Equal(string.Format("The value '{0}' is invalid for target location.", "James"), message); + Assert.Equal("The value 'James' is invalid for target location.", message); } public static TheoryData AddingDifferentComplexTypeWorksData @@ -255,8 +243,10 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal Assert.Equal(expected, targetObject); } - public static TheoryData AddingKeepsObjectReferenceData { - get { + public static TheoryData AddingKeepsObjectReferenceData + { + get + { var sDto1 = new SimpleObject(); var sDto2 = new SimpleObject(); var sDto3 = new SimpleObject(); @@ -324,9 +314,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal // Assert Assert.False(getStatus); - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", position), - message); + Assert.Equal($"The index value provided by path segment '{position}' is out of bounds of the array size.", message); } [Theory] @@ -365,9 +353,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal // Assert Assert.False(removeStatus); - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", position), - message); + Assert.Equal($"The index value provided by path segment '{position}' is out of bounds of the array size.", message); } [Theory] @@ -402,9 +388,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal // Assert Assert.False(replaceStatus); - Assert.Equal( - string.Format("The value '{0}' is invalid for target location.", "James"), - message); + Assert.Equal("The value 'James' is invalid for target location.", message); } [Fact] diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/ObjectVisitorTest.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/Internal/ObjectVisitorTest.cs similarity index 95% rename from test/Microsoft.AspNetCore.JsonPatch.Test/ObjectVisitorTest.cs rename to test/Microsoft.AspNetCore.JsonPatch.Test/Internal/ObjectVisitorTest.cs index 3384986c28..cb299cf0dc 100644 --- a/test/Microsoft.AspNetCore.JsonPatch.Test/ObjectVisitorTest.cs +++ b/test/Microsoft.AspNetCore.JsonPatch.Test/Internal/ObjectVisitorTest.cs @@ -1,7 +1,6 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -using System; using System.Collections.Generic; using System.Dynamic; using Newtonsoft.Json.Serialization; @@ -168,9 +167,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal // Assert Assert.False(visitStatus); - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", position), - message); + Assert.Equal($"The index value provided by path segment '{position}' is out of bounds of the array size.", message); } [Theory] @@ -188,12 +185,9 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal // Assert Assert.False(visitStatus); - Assert.Equal(string.Format( - "The path segment '{0}' is invalid for an array index.", position), - message); + Assert.Equal($"The path segment '{position}' is invalid for an array index.", message); } - // The adapter takes care of the responsibility of validating the final segment [Fact] public void Visit_DoesNotValidate_FinalPathSegment() { diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/ParsedPathTests.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/Internal/ParsedPathTests.cs similarity index 91% rename from test/Microsoft.AspNetCore.JsonPatch.Test/ParsedPathTests.cs rename to test/Microsoft.AspNetCore.JsonPatch.Test/Internal/ParsedPathTests.cs index 6b7c2e69cb..c23abcdb5e 100644 --- a/test/Microsoft.AspNetCore.JsonPatch.Test/ParsedPathTests.cs +++ b/test/Microsoft.AspNetCore.JsonPatch.Test/Internal/ParsedPathTests.cs @@ -2,10 +2,9 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using Microsoft.AspNetCore.JsonPatch.Exceptions; -using Microsoft.AspNetCore.JsonPatch.Internal; using Xunit; -namespace Microsoft.AspNetCore.JsonPatch.Test +namespace Microsoft.AspNetCore.JsonPatch.Internal { public class ParsedPathTests { @@ -19,7 +18,10 @@ namespace Microsoft.AspNetCore.JsonPatch.Test [InlineData("~0~1foo", new string[] { "~/foo" })] public void ParsingValidPathShouldSucceed(string path, string[] expected) { + // Arrange & Act var parsedPath = new ParsedPath(path); + + // Assert Assert.Equal(expected, parsedPath.Segments); } @@ -30,6 +32,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test [InlineData("foo~3bar")] public void PathWithInvalidEscapeSequenceShouldFail(string path) { + // Arrange, Act & Assert Assert.Throws(() => { var parsedPath = new ParsedPath(path); diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/Adapters/PocoAdapterTest.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/Internal/PocoAdapterTest.cs similarity index 100% rename from test/Microsoft.AspNetCore.JsonPatch.Test/Adapters/PocoAdapterTest.cs rename to test/Microsoft.AspNetCore.JsonPatch.Test/Internal/PocoAdapterTest.cs diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/JsonPatchDocumentGetPathTest.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/JsonPatchDocumentGetPathTest.cs index 7d3331231c..266202c7a5 100644 --- a/test/Microsoft.AspNetCore.JsonPatch.Test/JsonPatchDocumentGetPathTest.cs +++ b/test/Microsoft.AspNetCore.JsonPatch.Test/JsonPatchDocumentGetPathTest.cs @@ -100,9 +100,7 @@ namespace Microsoft.AspNetCore.JsonPatch }); // Assert - Assert.Equal( - string.Format("The expression '(p.IntegerValue >= 4)' is not supported. Supported expressions include member access and indexer expressions."), - exception.Message); + Assert.Equal("The expression '(p.IntegerValue >= 4)' is not supported. Supported expressions include member access and indexer expressions.", exception.Message); } } diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/JsonPatchDocumentJsonPropertyAttributeTest.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/JsonPatchDocumentJsonPropertyAttributeTest.cs index 882e09ffe7..f5d6377989 100644 --- a/test/Microsoft.AspNetCore.JsonPatch.Test/JsonPatchDocumentJsonPropertyAttributeTest.cs +++ b/test/Microsoft.AspNetCore.JsonPatch.Test/JsonPatchDocumentJsonPropertyAttributeTest.cs @@ -1,9 +1,7 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -using System.Collections.Generic; using System.Linq; -using System.Reflection; using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using Xunit; @@ -13,263 +11,39 @@ namespace Microsoft.AspNetCore.JsonPatch public class JsonPatchDocumentJsonPropertyAttributeTest { [Fact] - public void Add_ToRoot_OfListOfObjects_AtEndOfList() + public void Add_RespectsJsonPropertyAttribute() { - var patchDoc = new JsonPatchDocument>(); - patchDoc.Add(p => p, new JsonPropertyObject()); + // Arrange + var patchDocument = new JsonPatchDocument(); - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = - JsonConvert.DeserializeObject>(serialized); + // Act + patchDocument.Add(p => p.Name, "John"); - // get path - var pathToCheck = deserialized.Operations.First().path; - Assert.Equal("/-", pathToCheck); - } - - [Fact] - public void Add_ToRoot_OfListOfObjects_AtGivenPosition() - { - var patchDoc = new JsonPatchDocument>(); - patchDoc.Add(p => p[3], new JsonPropertyObject()); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = - JsonConvert.DeserializeObject>(serialized); - - // get path - var pathToCheck = deserialized.Operations.First().path; - Assert.Equal("/3", pathToCheck); - } - - [Fact] - public void Add_WithExpression_RespectsJsonPropertyName_ForModelProperty() - { - var patchDoc = new JsonPatchDocument(); - patchDoc.Add(p => p.Name, "John"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - // serialized value should have "AnotherName" as path - // deserialize to a JsonPatchDocument to check - var deserialized = - JsonConvert.DeserializeObject>(serialized); - - // get path - var pathToCheck = deserialized.Operations.First().path; + // Assert + var pathToCheck = patchDocument.Operations.First().path; Assert.Equal("/AnotherName", pathToCheck); } [Fact] - public void Add_WithExpressionOnStringProperty_FallsbackToPropertyName_WhenJsonPropertyName_IsEmpty() + public void Move_FallsbackToPropertyName_WhenJsonPropertyAttributeName_IsEmpty() { // Arrange - var patchDoc = new JsonPatchDocument(); - patchDoc.Add(m => m.StringProperty, "Test"); - var serialized = JsonConvert.SerializeObject(patchDoc); + var patchDocument = new JsonPatchDocument(); // Act - var deserialized = - JsonConvert.DeserializeObject>(serialized); + patchDocument.Move(m => m.StringProperty, m => m.StringProperty2); // Assert - var pathToCheck = deserialized.Operations.First().path; - Assert.Equal("/StringProperty", pathToCheck); - } - - [Fact] - public void Add_WithExpressionOnArrayProperty_FallsbackToPropertyName_WhenJsonPropertyName_IsEmpty() - { - // Arrange - var patchDoc = new JsonPatchDocument(); - patchDoc.Add(m => m.ArrayProperty, "James"); - var serialized = JsonConvert.SerializeObject(patchDoc); - - // Act - var deserialized = - JsonConvert.DeserializeObject>(serialized); - - // Assert - var pathToCheck = deserialized.Operations.First().path; - Assert.Equal("/ArrayProperty/-", pathToCheck); - } - - [Fact] - public void Add_WithExpression_RespectsJsonPropertyName_WhenApplyingToDifferentlyTypedClassWithPropertyMatchingJsonPropertyName() - { - var patchDocToSerialize = new JsonPatchDocument(); - patchDocToSerialize.Add(p => p.Name, "John"); - - // the patchdoc will deserialize to "anothername". We should thus be able to apply - // it to a class that HAS that other property name. - var doc = new JsonPropertyWithAnotherNameObject() - { - AnotherName = "InitialValue" - }; - - var serialized = JsonConvert.SerializeObject(patchDocToSerialize); - var deserialized = - JsonConvert.DeserializeObject> - (serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal("John", doc.AnotherName); - } - - [Fact] - public void Add_WithExpression_RespectsJsonPropertyName_WhenApplyingToSameTypedClassWithMatchingJsonPropertyName() - { - var patchDocToSerialize = new JsonPatchDocument(); - patchDocToSerialize.Add(p => p.Name, "John"); - - // the patchdoc will deserialize to "anothername". As JsonPropertyDTO has - // a JsonProperty signifying that "Name" should be deseriallized from "AnotherName", - // we should be able to apply the patchDoc. - - var doc = new JsonPropertyObject() - { - Name = "InitialValue" - }; - - var serialized = JsonConvert.SerializeObject(patchDocToSerialize); - var deserialized = - JsonConvert.DeserializeObject> - (serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal("John", doc.Name); - } - - [Fact] - public void Add_OnApplyFromJson_RespectsJsonPropertyNameOnJsonDocument() - { - var doc = new JsonPropertyObject() - { - Name = "InitialValue" - }; - - // serialization should serialize to "AnotherName" - var serialized = "[{\"value\":\"Kevin\",\"path\":\"/AnotherName\",\"op\":\"add\"}]"; - var deserialized = - JsonConvert.DeserializeObject>(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal("Kevin", doc.Name); - } - - [Fact] - public void Remove_OnApplyFromJson_RespectsJsonPropertyNameOnJsonDocument() - { - var doc = new JsonPropertyObject() - { - Name = "InitialValue" - }; - - // serialization should serialize to "AnotherName" - var serialized = "[{\"path\":\"/AnotherName\",\"op\":\"remove\"}]"; - var deserialized = - JsonConvert.DeserializeObject>(serialized); - - deserialized.ApplyTo(doc); - - Assert.Null(doc.Name); - } - - [Fact] - public void Add_OnApplyFromJson_RespectsInheritedJsonPropertyNameOnJsonDocument() - { - var doc = new JsonPropertyWithInheritanceObject() - { - Name = "InitialName" - }; - - // serialization should serialize to "AnotherName" - var serialized = "[{\"value\":\"Kevin\",\"path\":\"/AnotherName\",\"op\":\"add\"}]"; - var deserialized = - JsonConvert.DeserializeObject>(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal("Kevin", doc.Name); - } - - [Fact] - public void Add_WithExpression_RespectsJsonPropertyName_ForInheritedModelProperty() - { - var patchDoc = new JsonPatchDocument(); - patchDoc.Add(p => p.Name, "John"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - // serialized value should have "AnotherName" as path - // deserialize to a JsonPatchDocument to check - var deserialized = - JsonConvert.DeserializeObject>(serialized); - - // get path - var pathToCheck = deserialized.Operations.First().path; - Assert.Equal("/AnotherName", pathToCheck); - } - - [Fact] - public void Add_OnApplyFromJson_EscapingHandledOnComplexJsonPropertyNameOnJsonDocument() - { - var doc = new JsonPropertyComplexNameObject() - { - FooSlashBars = "InitialName", - FooSlashTilde = new SimpleObject - { - StringProperty = "Initial Value" - } - }; - - // serialization should serialize to "AnotherName" - var serialized = "[{\"value\":\"Kevin\",\"path\":\"/foo~1bar~0\",\"op\":\"add\"},{\"value\":\"Final Value\",\"path\":\"/foo~1~0/StringProperty\",\"op\":\"replace\"}]"; - var deserialized = - JsonConvert.DeserializeObject>(serialized); - - deserialized.ApplyTo(doc); - - Assert.Equal("Kevin", doc.FooSlashBars); - Assert.Equal("Final Value", doc.FooSlashTilde.StringProperty); - } - - [Fact] - public void Move_WithExpression_FallsbackToPropertyName_WhenJsonPropertyName_IsEmpty() - { - // Arrange - var patchDoc = new JsonPatchDocument(); - patchDoc.Move(m => m.StringProperty, m => m.StringProperty2); - var serialized = JsonConvert.SerializeObject(patchDoc); - - // Act - var deserialized = - JsonConvert.DeserializeObject>(serialized); - - // Assert - var fromPath = deserialized.Operations.First().from; + var fromPath = patchDocument.Operations.First().from; Assert.Equal("/StringProperty", fromPath); - var toPath = deserialized.Operations.First().path; + var toPath = patchDocument.Operations.First().path; Assert.Equal("/StringProperty2", toPath); } - [Fact] - public void Add_WithExpression_AndCustomContractResolver_UsesPropertyName_SetByContractResolver() + private class JsonPropertyObject { - // Arrange - var patchDoc = new JsonPatchDocument(); - patchDoc.ContractResolver = new CustomContractResolver(); - patchDoc.Add(m => m.SSN, "123-45-6789"); - var serialized = JsonConvert.SerializeObject(patchDoc); - - // Act - var deserialized = - JsonConvert.DeserializeObject>(serialized); - - // Assert - var path = deserialized.Operations.First().path; - Assert.Equal("/SocialSecurityNumber", path); + [JsonProperty("AnotherName")] + public string Name { get; set; } } private class JsonPropertyWithNoPropertyName @@ -286,20 +60,5 @@ namespace Microsoft.AspNetCore.JsonPatch [JsonProperty] public string SSN { get; set; } } - - private class CustomContractResolver : DefaultContractResolver - { - protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization) - { - var jsonProperty = base.CreateProperty(member, memberSerialization); - - if (jsonProperty.PropertyName == "SSN") - { - jsonProperty.PropertyName = "SocialSecurityNumber"; - } - - return jsonProperty; - } - } } } diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/JsonPatchDocumentTest.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/JsonPatchDocumentTest.cs index d754e8da5b..6b44b6696c 100644 --- a/test/Microsoft.AspNetCore.JsonPatch.Test/JsonPatchDocumentTest.cs +++ b/test/Microsoft.AspNetCore.JsonPatch.Test/JsonPatchDocumentTest.cs @@ -1,55 +1,180 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using Microsoft.AspNetCore.JsonPatch.Exceptions; using Newtonsoft.Json; using Xunit; -namespace Microsoft.AspNetCore.JsonPatch.Test +namespace Microsoft.AspNetCore.JsonPatch { public class JsonPatchDocumentTest { [Fact] - public void InvalidOperation_ThrowsException_CallsIntoLogErrorAction() + public void InvalidPathAtBeginningShouldThrowException() { // Arrange - var operationName = "foo"; - var serialized = "[{\"value\":\"John\",\"path\":\"/Name\",\"op\":\"" + operationName + "\"}]"; - var jsonPatchDocument = JsonConvert.DeserializeObject>(serialized); - var model = new Customer(); - var expectedErrorMessage = $"Invalid JsonPatch operation '{operationName}'."; - string actualErrorMessage = null; + var patchDocument = new JsonPatchDocument(); // Act - jsonPatchDocument.ApplyTo(model, (jsonPatchError) => + var exception = Assert.Throws(() => { - actualErrorMessage = jsonPatchError.ErrorMessage; + patchDocument.Add("//NewInt", 1); }); // Assert - Assert.Equal(expectedErrorMessage, actualErrorMessage); + Assert.Equal( + "The provided string '//NewInt' is an invalid path.", + exception.Message); } [Fact] - public void InvalidOperation_NoLogErrorAction_ThrowsJsonPatchException() + public void InvalidPathAtEndShouldThrowException() { // Arrange - var operationName = "foo"; - var serialized = "[{\"value\":\"John\",\"path\":\"/Name\",\"op\":\"" + operationName + "\"}]"; - var jsonPatchDocument = JsonConvert.DeserializeObject>(serialized); - var model = new Customer(); - var expectedErrorMessage = $"Invalid JsonPatch operation '{operationName}'."; + var patchDocument = new JsonPatchDocument(); // Act - var jsonPatchException = Assert.Throws(() => jsonPatchDocument.ApplyTo(model)); + var exception = Assert.Throws(() => + { + patchDocument.Add("NewInt//", 1); + }); // Assert - Assert.Equal(expectedErrorMessage, jsonPatchException.Message); + Assert.Equal( + "The provided string 'NewInt//' is an invalid path.", + exception.Message); } - private class Customer + [Fact] + public void InvalidPathWithDotShouldThrowException() { - public string Name { get; set; } + // Arrange + var patchDocument = new JsonPatchDocument(); + + // Act + var exception = Assert.Throws(() => + { + patchDocument.Add("NewInt.Test", 1); + }); + + // Assert + Assert.Equal( + "The provided string 'NewInt.Test' is an invalid path.", + exception.Message); + } + + [Fact] + public void NonGenericPatchDocToGenericMustSerialize() + { + // Arrange + var targetObject = new SimpleObject() + { + StringProperty = "A", + AnotherStringProperty = "B" + }; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Copy("StringProperty", "AnotherStringProperty"); + + var serialized = JsonConvert.SerializeObject(patchDocument); + var deserialized = JsonConvert.DeserializeObject>(serialized); + + // Act + deserialized.ApplyTo(targetObject); + + // Assert + Assert.Equal("A", targetObject.AnotherStringProperty); + } + + [Fact] + public void GenericPatchDocToNonGenericMustSerialize() + { + // Arrange + var targetObject = new SimpleObject() + { + StringProperty = "A", + AnotherStringProperty = "B" + }; + + var patchDocTyped = new JsonPatchDocument(); + patchDocTyped.Copy(o => o.StringProperty, o => o.AnotherStringProperty); + + var patchDocUntyped = new JsonPatchDocument(); + patchDocUntyped.Copy("StringProperty", "AnotherStringProperty"); + + var serializedTyped = JsonConvert.SerializeObject(patchDocTyped); + var serializedUntyped = JsonConvert.SerializeObject(patchDocUntyped); + var deserialized = JsonConvert.DeserializeObject(serializedTyped); + + // Act + deserialized.ApplyTo(targetObject); + + // Assert + Assert.Equal("A", targetObject.AnotherStringProperty); + } + + [Fact] + public void Deserialization_Successful_ForValidJsonPatchDocument() + { + // Arrange + var doc = new SimpleObject() + { + StringProperty = "A", + DecimalValue = 10, + DoubleValue = 10, + FloatValue = 10, + IntegerValue = 10 + }; + + var patchDocument = new JsonPatchDocument(); + patchDocument.Replace(o => o.StringProperty, "B"); + patchDocument.Replace(o => o.DecimalValue, 12); + patchDocument.Replace(o => o.DoubleValue, 12); + patchDocument.Replace(o => o.FloatValue, 12); + patchDocument.Replace(o => o.IntegerValue, 12); + + // default: no envelope + var serialized = JsonConvert.SerializeObject(patchDocument); + + // Act + var deserialized = JsonConvert.DeserializeObject>(serialized); + + // Assert + Assert.IsType>(deserialized); + } + + [Fact] + public void Deserialization_Fails_ForInvalidJsonPatchDocument() + { + // Arrange + var serialized = "{\"Operations\": [{ \"op\": \"replace\", \"path\": \"/title\", \"value\": \"New Title\"}]}"; + + // Act + var exception = Assert.Throws(() => + { + var deserialized + = JsonConvert.DeserializeObject(serialized); + }); + + // Assert + Assert.Equal("The JSON patch document was malformed and could not be parsed.", exception.Message); + } + + [Fact] + public void Deserialization_Fails_ForInvalidTypedJsonPatchDocument() + { + // Arrange + var serialized = "{\"Operations\": [{ \"op\": \"replace\", \"path\": \"/title\", \"value\": \"New Title\"}]}"; + + // Act + var exception = Assert.Throws(() => + { + var deserialized + = JsonConvert.DeserializeObject>(serialized); + }); + + // Assert + Assert.Equal("The JSON patch document was malformed and could not be parsed.", exception.Message); } } -} +} \ No newline at end of file diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/JsonPropertyComplexNameObject.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/JsonPropertyComplexNameObject.cs deleted file mode 100644 index a39aa8e30c..0000000000 --- a/test/Microsoft.AspNetCore.JsonPatch.Test/JsonPropertyComplexNameObject.cs +++ /dev/null @@ -1,16 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using Newtonsoft.Json; - -namespace Microsoft.AspNetCore.JsonPatch -{ - public class JsonPropertyComplexNameObject - { - [JsonProperty("foo/bar~")] - public string FooSlashBars { get; set; } - - [JsonProperty("foo/~")] - public SimpleObject FooSlashTilde { get; set; } - } -} diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/JsonPropertyObject.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/JsonPropertyObject.cs deleted file mode 100644 index 245ae6bb65..0000000000 --- a/test/Microsoft.AspNetCore.JsonPatch.Test/JsonPropertyObject.cs +++ /dev/null @@ -1,13 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using Newtonsoft.Json; - -namespace Microsoft.AspNetCore.JsonPatch -{ - public class JsonPropertyObject - { - [JsonProperty("AnotherName")] - public string Name { get; set; } - } -} diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/JsonPropertyWithAnotherNameObject.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/JsonPropertyWithAnotherNameObject.cs deleted file mode 100644 index 4581a4826d..0000000000 --- a/test/Microsoft.AspNetCore.JsonPatch.Test/JsonPropertyWithAnotherNameObject.cs +++ /dev/null @@ -1,10 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -namespace Microsoft.AspNetCore.JsonPatch -{ - public class JsonPropertyWithAnotherNameObject - { - public string AnotherName { get; set; } - } -} diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/JsonPropertyWithInheritanceObject.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/JsonPropertyWithInheritanceObject.cs deleted file mode 100644 index 935ef275ad..0000000000 --- a/test/Microsoft.AspNetCore.JsonPatch.Test/JsonPropertyWithInheritanceObject.cs +++ /dev/null @@ -1,15 +0,0 @@ -using Newtonsoft.Json; - -namespace Microsoft.AspNetCore.JsonPatch -{ - public class JsonPropertyWithInheritanceObject : JsonPropertyWithInheritanceBaseObject - { - public override string Name { get; set; } - } - - public abstract class JsonPropertyWithInheritanceBaseObject - { - [JsonProperty("AnotherName")] - public abstract string Name { get; set; } - } -} diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/NestedObject.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/NestedObject.cs deleted file mode 100644 index 8f0927297e..0000000000 --- a/test/Microsoft.AspNetCore.JsonPatch.Test/NestedObject.cs +++ /dev/null @@ -1,10 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -namespace Microsoft.AspNetCore.JsonPatch -{ - public class NestedObject - { - public string StringProperty { get; set; } - } -} \ No newline at end of file diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/NestedObjectTests.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/NestedObjectTests.cs deleted file mode 100644 index a0b5bd7e3f..0000000000 --- a/test/Microsoft.AspNetCore.JsonPatch.Test/NestedObjectTests.cs +++ /dev/null @@ -1,2272 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System.Collections.Generic; -using System.Collections.ObjectModel; -using Microsoft.AspNetCore.JsonPatch.Exceptions; -using Newtonsoft.Json; -using Xunit; - -namespace Microsoft.AspNetCore.JsonPatch -{ - public class NestedObjectTests - { - [Fact] - public void ReplacePropertyInNestedObject() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - IntegerValue = 1 - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace(o => o.NestedObject.StringProperty, "B"); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal("B", doc.NestedObject.StringProperty); - } - - [Fact] - public void ReplacePropertyInNestedObjectWithSerialization() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - IntegerValue = 1 - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace(o => o.NestedObject.StringProperty, "B"); - - // serialize & deserialize - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal("B", doc.NestedObject.StringProperty); - } - - [Fact] - public void ReplaceNestedObject() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - IntegerValue = 1 - }; - - var newNested = new NestedObject() { StringProperty = "B" }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace(o => o.NestedObject, newNested); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal("B", doc.NestedObject.StringProperty); - } - - [Fact] - public void ReplaceNestedObjectWithSerialization() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - IntegerValue = 1 - }; - - var newNested = new NestedObject() { StringProperty = "B" }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace(o => o.NestedObject, newNested); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal("B", doc.NestedObject.StringProperty); - } - - [Fact] - public void AddResultsInReplace() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - StringProperty = "A" - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add(o => o.SimpleObject.StringProperty, "B"); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal("B", doc.SimpleObject.StringProperty); - } - - [Fact] - public void AddResultsInReplaceWithSerialization() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - StringProperty = "A" - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add(o => o.SimpleObject.StringProperty, "B"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal("B", doc.SimpleObject.StringProperty); - } - - [Fact] - public void AddToList() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add(o => o.SimpleObject.IntegerList, 4, 0); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 4, 1, 2, 3 }, doc.SimpleObject.IntegerList); - } - - [Fact] - public void AddToListWithSerialization() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add(o => o.SimpleObject.IntegerList, 4, 0); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 4, 1, 2, 3 }, doc.SimpleObject.IntegerList); - } - - [Fact] - public void AddToIntegerIList() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerIList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add(o => (List)o.SimpleObject.IntegerIList, 4, 0); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 4, 1, 2, 3 }, doc.SimpleObject.IntegerIList); - } - - [Fact] - public void AddToIntegerIListWithSerialization() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerIList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add(o => (List)o.SimpleObject.IntegerIList, 4, 0); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 4, 1, 2, 3 }, doc.SimpleObject.IntegerIList); - } - - [Fact] - public void AddToNestedIntegerIList() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObjectIList = new List - { - new SimpleObject - { - IntegerIList = new List() { 1, 2, 3 } - } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add(o => (List)o.SimpleObjectIList[0].IntegerIList, 4, 0); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 4, 1, 2, 3 }, doc.SimpleObjectIList[0].IntegerIList); - } - - [Fact] - public void AddToNestedIntegerIListWithSerialization() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObjectIList = new List - { - new SimpleObject - { - IntegerIList = new List() { 1, 2, 3 } - } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add(o => (List)o.SimpleObjectIList[0].IntegerIList, 4, 0); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 4, 1, 2, 3 }, doc.SimpleObjectIList[0].IntegerIList); - } - - [Fact] - public void AddToComplextTypeListSpecifyIndex() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObjectList = new List() - { - new SimpleObject - { - StringProperty = "String1" - }, - new SimpleObject - { - StringProperty = "String2" - } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add(o => o.SimpleObjectList[0].StringProperty, "ChangedString1"); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal("ChangedString1", doc.SimpleObjectList[0].StringProperty); - } - - [Fact] - public void AddToComplextTypeListSpecifyIndexWithSerialization() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObjectList = new List() - { - new SimpleObject - { - StringProperty = "String1" - }, - new SimpleObject - { - StringProperty = "String2" - } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add(o => o.SimpleObjectList[0].StringProperty, "ChangedString1"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal("ChangedString1", doc.SimpleObjectList[0].StringProperty); - } - - [Fact] - public void AddToListInvalidPositionTooLarge() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add(o => o.SimpleObject.IntegerList, 4, 4); - - // Act & Assert - var exception = Assert.Throws(() => { patchDoc.ApplyTo(doc); }); - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "4"), - exception.Message); - - } - - [Fact] - public void AddToListInvalidPositionTooLargeWithSerialization() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add(o => o.SimpleObject.IntegerList, 4, 4); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act & Assert - var exception = Assert.Throws(() => - { - deserialized.ApplyTo(doc); - }); - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "4"), - exception.Message); - } - - [Fact] - public void AddToListInvalidPositionTooLarge_LogsError() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add(o => o.SimpleObject.IntegerList, 4, 4); - - var logger = new TestErrorLogger(); - - patchDoc.ApplyTo(doc, logger.LogErrorMessage); - - - //Assert - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "4"), - logger.ErrorMessage); - - } - - [Fact] - public void AddToListInvalidPositionTooSmall() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add(o => o.SimpleObject.IntegerList, 4, -1); - - // Act & Assert - var exception = Assert.Throws(() => { patchDoc.ApplyTo(doc); }); - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "-1"), - exception.Message); - } - - [Fact] - public void AddToListInvalidPositionTooSmallWithSerialization() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add(o => o.SimpleObject.IntegerList, 4, -1); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act & Assert - var exception = Assert.Throws(() => - { - deserialized.ApplyTo(doc); - }); - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "-1"), - exception.Message); - } - - [Fact] - public void AddToListInvalidPositionTooSmall_LogsError() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add(o => o.SimpleObject.IntegerList, 4, -1); - - var logger = new TestErrorLogger(); - - - patchDoc.ApplyTo(doc, logger.LogErrorMessage); - - - //Assert - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "-1"), - logger.ErrorMessage); - } - - [Fact] - public void AddToListAppend() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add(o => o.SimpleObject.IntegerList, 4); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 1, 2, 3, 4 }, doc.SimpleObject.IntegerList); - } - - [Fact] - public void AddToListAppendWithSerialization() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add(o => o.SimpleObject.IntegerList, 4); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 1, 2, 3, 4 }, doc.SimpleObject.IntegerList); - } - - [Fact] - public void Remove() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - StringProperty = "A" - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove(o => o.SimpleObject.StringProperty); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Null(doc.SimpleObject.StringProperty); - } - - [Fact] - public void RemoveWithSerialization() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - StringProperty = "A" - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove(o => o.SimpleObject.StringProperty); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Null(doc.SimpleObject.StringProperty); - } - - [Fact] - public void RemoveFromList() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove(o => o.SimpleObject.IntegerList, 2); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 1, 2 }, doc.SimpleObject.IntegerList); - } - - [Fact] - public void RemoveFromListWithSerialization() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove(o => o.SimpleObject.IntegerList, 2); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 1, 2 }, doc.SimpleObject.IntegerList); - } - - [Fact] - public void RemoveFromListInvalidPositionTooLarge() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove(o => o.SimpleObject.IntegerList, 3); - - // Act & Assert - var exception = Assert.Throws(() => { patchDoc.ApplyTo(doc); }); - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "3"), - exception.Message); - } - - [Fact] - public void RemoveFromListInvalidPositionTooLargeWithSerialization() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove(o => o.SimpleObject.IntegerList, 3); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act & Assert - var exception = Assert.Throws(() => - { - deserialized.ApplyTo(doc); - }); - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "3"), - exception.Message); - } - - [Fact] - public void RemoveFromListInvalidPositionTooLarge_LogsError() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove(o => o.SimpleObject.IntegerList, 3); - - var logger = new TestErrorLogger(); - - patchDoc.ApplyTo(doc, logger.LogErrorMessage); - - // Assert - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "3"), - logger.ErrorMessage); - } - - [Fact] - public void RemoveFromListInvalidPositionTooSmall() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove(o => o.SimpleObject.IntegerList, -1); - - // Act & Assert - var exception = Assert.Throws(() => { patchDoc.ApplyTo(doc); }); - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "-1"), - exception.Message); - } - - [Fact] - public void RemoveFromListInvalidPositionTooSmallWithSerialization() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove(o => o.SimpleObject.IntegerList, -1); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act & Assert - var exception = Assert.Throws(() => - { - deserialized.ApplyTo(doc); - }); - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "-1"), - exception.Message); - } - - [Fact] - public void RemoveFromListInvalidPositionTooSmall_LogsError() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove(o => o.SimpleObject.IntegerList, -1); - - var logger = new TestErrorLogger(); - - - patchDoc.ApplyTo(doc, logger.LogErrorMessage); - - // Assert - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "-1"), - logger.ErrorMessage); - } - - [Fact] - public void RemoveFromEndOfList() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove(o => o.SimpleObject.IntegerList); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 1, 2 }, doc.SimpleObject.IntegerList); - } - - [Fact] - public void RemoveFromEndOfListWithSerialization() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove(o => o.SimpleObject.IntegerList); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 1, 2 }, doc.SimpleObject.IntegerList); - } - - [Fact] - public void Replace() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - StringProperty = "A", - DecimalValue = 10 - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace(o => o.SimpleObject.StringProperty, "B"); - patchDoc.Replace(o => o.SimpleObject.DecimalValue, 12); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal("B", doc.SimpleObject.StringProperty); - Assert.Equal(12, doc.SimpleObject.DecimalValue); - } - - [Fact] - public void Replace_DTOWithNullCheck() - { - // Arrange - var doc = new SimpleObjectWithNestedObjectWithNullCheck() - { - SimpleObjectWithNullCheck = new SimpleObjectWithNullCheck() - { - StringProperty = "A" - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace(o => o.SimpleObjectWithNullCheck.StringProperty, "B"); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal("B", doc.SimpleObjectWithNullCheck.StringProperty); - } - - [Fact] - public void ReplaceWithSerialization() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - StringProperty = "A", - DecimalValue = 10 - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace(o => o.SimpleObject.StringProperty, "B"); - patchDoc.Replace(o => o.SimpleObject.DecimalValue, 12); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal("B", doc.SimpleObject.StringProperty); - Assert.Equal(12, doc.SimpleObject.DecimalValue); - } - - [Fact] - public void SerializationTests() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - StringProperty = "A", - DecimalValue = 10, - DoubleValue = 10, - FloatValue = 10, - IntegerValue = 10 - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace(o => o.SimpleObject.StringProperty, "B"); - patchDoc.Replace(o => o.SimpleObject.DecimalValue, 12); - patchDoc.Replace(o => o.SimpleObject.DoubleValue, 12); - patchDoc.Replace(o => o.SimpleObject.FloatValue, 12); - patchDoc.Replace(o => o.SimpleObject.IntegerValue, 12); - - // serialize & deserialize - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserizalized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserizalized.ApplyTo(doc); - - // Assert - Assert.Equal("B", doc.SimpleObject.StringProperty); - Assert.Equal(12, doc.SimpleObject.DecimalValue); - Assert.Equal(12, doc.SimpleObject.DoubleValue); - Assert.Equal(12, doc.SimpleObject.FloatValue); - Assert.Equal(12, doc.SimpleObject.IntegerValue); - } - - [Fact] - public void ReplaceInList() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace(o => o.SimpleObject.IntegerList, 5, 0); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 5, 2, 3 }, doc.SimpleObject.IntegerList); - } - - [Fact] - public void ReplaceInListWithSerialization() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace(o => o.SimpleObject.IntegerList, 5, 0); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 5, 2, 3 }, doc.SimpleObject.IntegerList); - } - - [Fact] - public void ReplaceFullList() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace(o => o.SimpleObject.IntegerList, new List() { 4, 5, 6 }); - - // Act - patchDoc.ApplyTo(doc); - - // Arrange - Assert.Equal(new List() { 4, 5, 6 }, doc.SimpleObject.IntegerList); - } - - [Fact] - public void ReplaceFullListWithSerialiation() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace(o => o.SimpleObject.IntegerList, new List() { 4, 5, 6 }); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 4, 5, 6 }, doc.SimpleObject.IntegerList); - } - - [Fact] - public void ReplaceFullListFromEnumerable() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace>(o => o.SimpleObject.IntegerList, new List() { 4, 5, 6 }); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 4, 5, 6 }, doc.SimpleObject.IntegerList); - } - - [Fact] - public void ReplaceFullListFromEnumerableWithSerialization() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace>(o => o.SimpleObject.IntegerList, new List() { 4, 5, 6 }); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 4, 5, 6 }, doc.SimpleObject.IntegerList); - } - - [Fact] - public void ReplaceFullListWithCollection() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace>(o => o.SimpleObject.IntegerList, new Collection() { 4, 5, 6 }); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 4, 5, 6 }, doc.SimpleObject.IntegerList); - } - - [Fact] - public void ReplaceFullListWithCollectionWithSerialization() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace>(o => o.SimpleObject.IntegerList, new Collection() { 4, 5, 6 }); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 4, 5, 6 }, doc.SimpleObject.IntegerList); - } - - [Fact] - public void ReplaceAtEndOfList() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace(o => o.SimpleObject.IntegerList, 5); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 1, 2, 5 }, doc.SimpleObject.IntegerList); - } - - [Fact] - public void ReplaceAtEndOfListWithSerialization() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace(o => o.SimpleObject.IntegerList, 5); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 1, 2, 5 }, doc.SimpleObject.IntegerList); - } - - [Fact] - public void ReplaceInListInvalidInvalidPositionTooLarge() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace(o => o.SimpleObject.IntegerList, 5, 3); - - // Act & Assert - var exception = Assert.Throws(() => { patchDoc.ApplyTo(doc); }); - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "3"), - exception.Message); - } - - [Fact] - public void ReplaceInListInvalidInvalidPositionTooLargeWithSerialization() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace(o => o.SimpleObject.IntegerList, 5, 3); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act & Assert - var exception = Assert.Throws(() => - { - deserialized.ApplyTo(doc); - }); - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "3"), - exception.Message); - } - - [Fact] - public void ReplaceInListInvalid_PositionTooLarge_LogsError() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace(o => o.SimpleObject.IntegerList, 5, 3); - - var logger = new TestErrorLogger(); - - - patchDoc.ApplyTo(doc, logger.LogErrorMessage); - - - // Assert - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "3"), - logger.ErrorMessage); - } - - [Fact] - public void ReplaceInListInvalidPositionTooSmall() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace(o => o.SimpleObject.IntegerList, 5, -1); - - // Act & Assert - var exception = Assert.Throws(() => { patchDoc.ApplyTo(doc); }); - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "-1"), - exception.Message); - } - - [Fact] - public void ReplaceInListInvalidPositionTooSmallWithSerialization() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace(o => o.SimpleObject.IntegerList, 5, -1); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act & Assert - var exception = Assert.Throws(() => { deserialized.ApplyTo(doc); }); - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "-1"), - exception.Message); - } - - [Fact] - public void ReplaceInListInvalidPositionTooSmall_LogsError() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace(o => o.SimpleObject.IntegerList, 5, -1); - - var logger = new TestErrorLogger(); - - - patchDoc.ApplyTo(doc, logger.LogErrorMessage); - - - // Assert - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "-1"), - logger.ErrorMessage); - } - - [Fact] - public void Copy() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - StringProperty = "A", - AnotherStringProperty = "B" - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy(o => o.SimpleObject.StringProperty, o => o.SimpleObject.AnotherStringProperty); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal("A", doc.SimpleObject.AnotherStringProperty); - } - - [Fact] - public void CopyWithSerialization() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - StringProperty = "A", - AnotherStringProperty = "B" - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy(o => o.SimpleObject.StringProperty, o => o.SimpleObject.AnotherStringProperty); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal("A", doc.SimpleObject.AnotherStringProperty); - } - - [Fact] - public void CopyInList() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy(o => o.SimpleObject.IntegerList, 0, o => o.SimpleObject.IntegerList, 1); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 1, 1, 2, 3 }, doc.SimpleObject.IntegerList); - } - - [Fact] - public void CopyInListWithSerialization() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy(o => o.SimpleObject.IntegerList, 0, o => o.SimpleObject.IntegerList, 1); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 1, 1, 2, 3 }, doc.SimpleObject.IntegerList); - } - - [Fact] - public void CopyFromListToEndOfList() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy(o => o.SimpleObject.IntegerList, 0, o => o.SimpleObject.IntegerList); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 1, 2, 3, 1 }, doc.SimpleObject.IntegerList); - } - - [Fact] - public void CopyFromListToEndOfListWithSerialization() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy(o => o.SimpleObject.IntegerList, 0, o => o.SimpleObject.IntegerList); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 1, 2, 3, 1 }, doc.SimpleObject.IntegerList); - } - - [Fact] - public void CopyFromListToNonList() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy(o => o.SimpleObject.IntegerList, 0, o => o.SimpleObject.IntegerValue); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal(1, doc.SimpleObject.IntegerValue); - } - - [Fact] - public void CopyFromListToNonListWithSerialization() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy(o => o.SimpleObject.IntegerList, 0, o => o.SimpleObject.IntegerValue); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal(1, doc.SimpleObject.IntegerValue); - } - - [Fact] - public void CopyFromNonListToList() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerValue = 5, - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy(o => o.SimpleObject.IntegerValue, o => o.SimpleObject.IntegerList, 0); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 5, 1, 2, 3 }, doc.SimpleObject.IntegerList); - } - - [Fact] - public void CopyFromNonListToListWithSerialization() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerValue = 5, - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy(o => o.SimpleObject.IntegerValue, o => o.SimpleObject.IntegerList, 0); - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 5, 1, 2, 3 }, doc.SimpleObject.IntegerList); - } - - [Fact] - public void CopyToEndOfList() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerValue = 5, - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy(o => o.SimpleObject.IntegerValue, o => o.SimpleObject.IntegerList); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 1, 2, 3, 5 }, doc.SimpleObject.IntegerList); - } - - [Fact] - public void CopyToEndOfListWithSerialization() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerValue = 5, - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy(o => o.SimpleObject.IntegerValue, o => o.SimpleObject.IntegerList); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 1, 2, 3, 5 }, doc.SimpleObject.IntegerList); - } - - [Fact] - public void Copy_DeepClonesObject() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - StringProperty = "A", - AnotherStringProperty = "B" - }, - InheritedObject = new InheritedObject() - { - StringProperty = "C", - AnotherStringProperty = "D" - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy(o => o.InheritedObject, o => o.SimpleObject); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal("C", doc.SimpleObject.StringProperty); - Assert.Equal("D", doc.SimpleObject.AnotherStringProperty); - Assert.Equal("C", doc.InheritedObject.StringProperty); - Assert.Equal("D", doc.InheritedObject.AnotherStringProperty); - Assert.NotSame(doc.SimpleObject.StringProperty, doc.InheritedObject.StringProperty); - } - - [Fact] - public void Copy_KeepsObjectType() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject(), - InheritedObject = new InheritedObject() - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy(o => o.InheritedObject, o => o.SimpleObject); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal(typeof(InheritedObject), doc.SimpleObject.GetType()); - } - - [Fact] - public void Copy_BreaksObjectReference() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject(), - InheritedObject = new InheritedObject() - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy(o => o.InheritedObject, o => o.SimpleObject); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.NotSame(doc.SimpleObject, doc.InheritedObject); - } - - [Fact] - public void Move() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - StringProperty = "A", - AnotherStringProperty = "B" - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move(o => o.SimpleObject.StringProperty, o => o.SimpleObject.AnotherStringProperty); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal("A", doc.SimpleObject.AnotherStringProperty); - Assert.Null(doc.SimpleObject.StringProperty); - } - - [Fact] - public void MoveWithSerialization() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - StringProperty = "A", - AnotherStringProperty = "B" - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move(o => o.SimpleObject.StringProperty, o => o.SimpleObject.AnotherStringProperty); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal("A", doc.SimpleObject.AnotherStringProperty); - Assert.Null(doc.SimpleObject.StringProperty); - } - - [Fact] - public void Move_KeepsObjectReference() - { - // Arrange - var sDto = new SimpleObject() - { - StringProperty = "A", - AnotherStringProperty = "B" - }; - var iDto = new InheritedObject() - { - StringProperty = "C", - AnotherStringProperty = "D" - }; - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = sDto, - InheritedObject = iDto - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move(o => o.InheritedObject, o => o.SimpleObject); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal("C", doc.SimpleObject.StringProperty); - Assert.Equal("D", doc.SimpleObject.AnotherStringProperty); - Assert.Same(iDto, doc.SimpleObject); - Assert.Null(doc.InheritedObject); - } - - [Fact] - public void Move_KeepsObjectReferenceWithSerialization() - { - // Arrange - var sDto = new SimpleObject() - { - StringProperty = "A", - AnotherStringProperty = "B" - }; - var iDto = new InheritedObject() - { - StringProperty = "C", - AnotherStringProperty = "D" - }; - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = sDto, - InheritedObject = iDto - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move(o => o.InheritedObject, o => o.SimpleObject); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal("C", doc.SimpleObject.StringProperty); - Assert.Equal("D", doc.SimpleObject.AnotherStringProperty); - Assert.Same(iDto, doc.SimpleObject); - Assert.Null(doc.InheritedObject); - } - - [Fact] - public void MoveInList() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move(o => o.SimpleObject.IntegerList, 0, o => o.SimpleObject.IntegerList, 1); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 2, 1, 3 }, doc.SimpleObject.IntegerList); - } - - [Fact] - public void MoveInListWithSerialization() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move(o => o.SimpleObject.IntegerList, 0, o => o.SimpleObject.IntegerList, 1); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 2, 1, 3 }, doc.SimpleObject.IntegerList); - } - - [Fact] - public void Move_KeepsObjectReferenceInList() - { - // Arrange - var sDto1 = new SimpleObject() { IntegerValue = 1 }; - var sDto2 = new SimpleObject() { IntegerValue = 2 }; - var sDto3 = new SimpleObject() { IntegerValue = 3 }; - var doc = new SimpleObjectWithNestedObject() - { - SimpleObjectList = new List() { - sDto1, - sDto2, - sDto3 - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move(o => o.SimpleObjectList, 0, o => o.SimpleObjectList, 1); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { sDto2, sDto1, sDto3 }, doc.SimpleObjectList); - Assert.Equal(2, doc.SimpleObjectList[0].IntegerValue); - Assert.Equal(1, doc.SimpleObjectList[1].IntegerValue); - Assert.Same(sDto2, doc.SimpleObjectList[0]); - Assert.Same(sDto1, doc.SimpleObjectList[1]); - } - - [Fact] - public void Move_KeepsObjectReferenceInListWithSerialization() - { - // Arrange - var sDto1 = new SimpleObject() { IntegerValue = 1 }; - var sDto2 = new SimpleObject() { IntegerValue = 2 }; - var sDto3 = new SimpleObject() { IntegerValue = 3 }; - var doc = new SimpleObjectWithNestedObject() - { - SimpleObjectList = new List() { - sDto1, - sDto2, - sDto3 - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move(o => o.SimpleObjectList, 0, o => o.SimpleObjectList, 1); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { sDto2, sDto1, sDto3 }, doc.SimpleObjectList); - Assert.Equal(2, doc.SimpleObjectList[0].IntegerValue); - Assert.Equal(1, doc.SimpleObjectList[1].IntegerValue); - Assert.Same(sDto2, doc.SimpleObjectList[0]); - Assert.Same(sDto1, doc.SimpleObjectList[1]); - } - - [Fact] - public void MoveFromListToEndOfList() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move(o => o.SimpleObject.IntegerList, 0, o => o.SimpleObject.IntegerList); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 2, 3, 1 }, doc.SimpleObject.IntegerList); - } - - [Fact] - public void MoveFromListToEndOfListWithSerialization() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move(o => o.SimpleObject.IntegerList, 0, o => o.SimpleObject.IntegerList); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 2, 3, 1 }, doc.SimpleObject.IntegerList); - } - - [Fact] - public void MoveFomListToNonList() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move(o => o.SimpleObject.IntegerList, 0, o => o.SimpleObject.IntegerValue); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 2, 3 }, doc.SimpleObject.IntegerList); - Assert.Equal(1, doc.SimpleObject.IntegerValue); - } - - [Fact] - public void MoveFomListToNonListWithSerialization() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move(o => o.SimpleObject.IntegerList, 0, o => o.SimpleObject.IntegerValue); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 2, 3 }, doc.SimpleObject.IntegerList); - Assert.Equal(1, doc.SimpleObject.IntegerValue); - } - - [Fact] - public void MoveFomListToNonListBetweenHierarchy() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move(o => o.SimpleObject.IntegerList, 0, o => o.IntegerValue); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 2, 3 }, doc.SimpleObject.IntegerList); - Assert.Equal(1, doc.IntegerValue); - } - - [Fact] - public void MoveFomListToNonListBetweenHierarchyWithSerialization() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move(o => o.SimpleObject.IntegerList, 0, o => o.IntegerValue); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 2, 3 }, doc.SimpleObject.IntegerList); - Assert.Equal(1, doc.IntegerValue); - } - - [Fact] - public void MoveFromNonListToList() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerValue = 5, - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move(o => o.SimpleObject.IntegerValue, o => o.SimpleObject.IntegerList, 0); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal(0, doc.IntegerValue); - Assert.Equal(new List() { 5, 1, 2, 3 }, doc.SimpleObject.IntegerList); - } - - [Fact] - public void MoveFromNonListToListWithSerialization() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerValue = 5, - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move(o => o.SimpleObject.IntegerValue, o => o.SimpleObject.IntegerList, 0); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal(0, doc.IntegerValue); - Assert.Equal(new List() { 5, 1, 2, 3 }, doc.SimpleObject.IntegerList); - } - - [Fact] - public void MoveToEndOfList() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerValue = 5, - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move(o => o.SimpleObject.IntegerValue, o => o.SimpleObject.IntegerList); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal(0, doc.IntegerValue); - Assert.Equal(new List() { 1, 2, 3, 5 }, doc.SimpleObject.IntegerList); - } - - [Fact] - public void MoveToEndOfListWithSerialization() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerValue = 5, - IntegerList = new List() { 1, 2, 3 } - } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move(o => o.SimpleObject.IntegerValue, o => o.SimpleObject.IntegerList); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal(0, doc.IntegerValue); - Assert.Equal(new List() { 1, 2, 3, 5 }, doc.SimpleObject.IntegerList); - } - } -} diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/ObjectAdapterTests.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/ObjectAdapterTests.cs deleted file mode 100644 index c1d005e897..0000000000 --- a/test/Microsoft.AspNetCore.JsonPatch.Test/ObjectAdapterTests.cs +++ /dev/null @@ -1,2331 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using System.Collections.ObjectModel; -using Microsoft.AspNetCore.JsonPatch.Exceptions; -using Newtonsoft.Json; -using Xunit; - -namespace Microsoft.AspNetCore.JsonPatch.Adapters -{ - public class ObjectAdapterTests - { - [Fact] - public void AddResultsShouldReplace() - { - // Arrange - var doc = new SimpleObject() - { - StringProperty = "A" - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add(o => o.StringProperty, "B"); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal("B", doc.StringProperty); - } - - [Fact] - public void AddResultsShouldReplaceWithSerialization() - { - // Arrange - var doc = new SimpleObject() - { - StringProperty = "A" - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add(o => o.StringProperty, "B"); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal("B", doc.StringProperty); - } - - [Fact] - public void AddToList() - { - // Arrange - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add(o => o.IntegerList, 4, 0); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 4, 1, 2, 3 }, doc.IntegerList); - } - - [Fact] - public void AddToListWithSerialization() - { - // Arrange - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add(o => o.IntegerList, 4, 0); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 4, 1, 2, 3 }, doc.IntegerList); - } - - [Fact] - public void AddToIntegerIList() - { - // Arrange - var doc = new SimpleObject() - { - IntegerIList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add(o => o.IntegerIList, 4, 0); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 4, 1, 2, 3 }, doc.IntegerIList); - } - - [Fact] - public void AddToIntegerIListWithSerialization() - { - // Arrange - var doc = new SimpleObject() - { - IntegerIList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add(o => o.IntegerIList, 4, 0); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 4, 1, 2, 3 }, doc.IntegerIList); - } - - [Fact] - public void AddToListInvalidPositionTooLarge() - { - // Arrange - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add(o => o.IntegerList, 4, 4); - - // Act & Assert - var exception = Assert.Throws(() => { patchDoc.ApplyTo(doc); }); - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "4"), - exception.Message); - } - - [Fact] - public void AddToListInvalidPositionTooLargeWithSerialization() - { - // Arrange - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add(o => o.IntegerList, 4, 4); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act & Assert - var exception = Assert.Throws(() => { deserialized.ApplyTo(doc); }); - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "4"), - exception.Message); - } - - [Fact] - public void AddToListInvalidPositionTooLarge_LogsError() - { - // Arrange - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add(o => o.IntegerList, 4, 4); - - var logger = new TestErrorLogger(); - - patchDoc.ApplyTo(doc, logger.LogErrorMessage); - - - // Assert - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "4"), - logger.ErrorMessage); - } - - [Fact] - public void AddToListAtBeginning() - { - // Arrange - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add(o => o.IntegerList, 4, 0); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 4, 1, 2, 3 }, doc.IntegerList); - } - - [Fact] - public void AddToListAtBeginningWithSerialization() - { - // Arrange - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add(o => o.IntegerList, 4, 0); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 4, 1, 2, 3 }, doc.IntegerList); - } - - [Fact] - public void AddToListInvalidPositionTooSmall() - { - // Arrange - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add(o => o.IntegerList, 4, -1); - - // Act & Assert - var exception = Assert.Throws(() => { patchDoc.ApplyTo(doc); }); - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "-1"), - exception.Message); - } - - [Fact] - public void AddToListInvalidPositionTooSmallWithSerialization() - { - // Arrange - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add(o => o.IntegerList, 4, -1); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act & Assert - var exception = Assert.Throws(() => { deserialized.ApplyTo(doc); }); - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "-1"), - exception.Message); - } - - [Fact] - public void AddToListInvalidPositionTooSmall_LogsError() - { - // Arrange - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add(o => o.IntegerList, 4, -1); - - var logger = new TestErrorLogger(); - - patchDoc.ApplyTo(doc, logger.LogErrorMessage); - - // Assert - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "-1"), - logger.ErrorMessage); - } - - [Fact] - public void AddToListAppend() - { - // Arrange - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add(o => o.IntegerList, 4); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 1, 2, 3, 4 }, doc.IntegerList); - } - - [Fact] - public void AddToListAppendWithSerialization() - { - // Arrange - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add(o => o.IntegerList, 4); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 1, 2, 3, 4 }, doc.IntegerList); - } - - [Fact] - public void Remove() - { - // Arrange - var doc = new SimpleObject() - { - StringProperty = "A" - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove(o => o.StringProperty); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Null(doc.StringProperty); - } - - [Fact] - public void RemoveWithSerialization() - { - // Arrange - var doc = new SimpleObject() - { - StringProperty = "A" - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove(o => o.StringProperty); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Null(doc.StringProperty); - } - - [Fact] - public void RemoveFromList() - { - // Arrange - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove(o => o.IntegerList, 2); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 1, 2 }, doc.IntegerList); - } - - [Fact] - public void RemoveFromListWithSerialization() - { - // Arrange - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove(o => o.IntegerList, 2); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 1, 2 }, doc.IntegerList); - } - - [Fact] - public void RemoveFromListInvalidPositionTooLarge() - { - // Arrange - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove(o => o.IntegerList, 3); - - // Act & Assert - var exception = Assert.Throws(() => { patchDoc.ApplyTo(doc); }); - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "3"), - exception.Message); - } - - [Fact] - public void RemoveFromListInvalidPositionTooLargeWithSerialization() - { - // Arrange - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove(o => o.IntegerList, 3); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act & Assert - var exception = Assert.Throws(() => { deserialized.ApplyTo(doc); }); - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "3"), - exception.Message); - } - - [Fact] - public void RemoveFromListInvalidPositionTooLarge_LogsError() - { - // Arrange - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove(o => o.IntegerList, 3); - - var logger = new TestErrorLogger(); - - patchDoc.ApplyTo(doc, logger.LogErrorMessage); - - - // Assert - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "3"), - logger.ErrorMessage); - } - - [Fact] - public void RemoveFromListInvalidPositionTooSmall() - { - // Arrange - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove(o => o.IntegerList, -1); - - // Act & Assert - var exception = Assert.Throws(() => { patchDoc.ApplyTo(doc); }); - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "-1"), - exception.Message); - } - - [Fact] - public void RemoveFromListInvalidPositionTooSmallWithSerialization() - { - // Arrange - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove(o => o.IntegerList, -1); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act & Assert - var exception = Assert.Throws(() => { deserialized.ApplyTo(doc); }); - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "-1"), - exception.Message); - } - - [Fact] - public void RemoveFromListInvalidPositionTooSmall_LogsError() - { - // Arrange - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove(o => o.IntegerList, -1); - - var logger = new TestErrorLogger(); - - - patchDoc.ApplyTo(doc, logger.LogErrorMessage); - - - // Assert - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "-1"), - logger.ErrorMessage); - } - - [Fact] - public void RemoveFromEndOfList() - { - // Arrange - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove(o => o.IntegerList); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 1, 2 }, doc.IntegerList); - } - - [Fact] - public void RemoveFromEndOfListWithSerialization() - { - // Arrange - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove(o => o.IntegerList); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 1, 2 }, doc.IntegerList); - } - - [Fact] - public void Replace() - { - // Arrange - var doc = new SimpleObject() - { - StringProperty = "A", - DecimalValue = 10 - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace(o => o.StringProperty, "B"); - - patchDoc.Replace(o => o.DecimalValue, 12); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal("B", doc.StringProperty); - Assert.Equal(12, doc.DecimalValue); - } - - [Fact] - public void Replace_DTOWithNullCheck() - { - // Arrange - var doc = new SimpleObjectWithNullCheck() - { - StringProperty = "A", - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace(o => o.StringProperty, "B"); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal("B", doc.StringProperty); - } - - [Fact] - public void ReplaceWithSerialization() - { - // Arrange - var doc = new SimpleObject() - { - StringProperty = "A", - DecimalValue = 10 - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace(o => o.StringProperty, "B"); - - patchDoc.Replace(o => o.DecimalValue, 12); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal("B", doc.StringProperty); - Assert.Equal(12, doc.DecimalValue); - } - - [Fact] - public void SerializationMustNotIncudeEnvelope() - { - // Arrange - var doc = new SimpleObject() - { - StringProperty = "A", - DecimalValue = 10, - DoubleValue = 10, - FloatValue = 10, - IntegerValue = 10 - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace(o => o.StringProperty, "B"); - patchDoc.Replace(o => o.DecimalValue, 12); - patchDoc.Replace(o => o.DoubleValue, 12); - patchDoc.Replace(o => o.FloatValue, 12); - patchDoc.Replace(o => o.IntegerValue, 12); - - // Act - var serialized = JsonConvert.SerializeObject(patchDoc); - - // Assert - Assert.DoesNotContain("operations", serialized); - Assert.DoesNotContain("Operations", serialized); - } - - [Fact] - public void Deserialization_Successful_ForValidJsonPatchDocument() - { - // Arrange - var doc = new SimpleObject() - { - StringProperty = "A", - DecimalValue = 10, - DoubleValue = 10, - FloatValue = 10, - IntegerValue = 10 - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace(o => o.StringProperty, "B"); - patchDoc.Replace(o => o.DecimalValue, 12); - patchDoc.Replace(o => o.DoubleValue, 12); - patchDoc.Replace(o => o.FloatValue, 12); - patchDoc.Replace(o => o.IntegerValue, 12); - - // default: no envelope - var serialized = JsonConvert.SerializeObject(patchDoc); - - // Act - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Assert - Assert.IsType>(deserialized); - } - - [Fact] - public void Deserialization_Fails_ForInvalidJsonPatchDocument() - { - // Arrange - var serialized = "{\"Operations\": [{ \"op\": \"replace\", \"path\": \"/title\", \"value\": \"New Title\"}]}"; - - // Act & Assert - var exception = Assert.Throws(() => - { - var deserialized - = JsonConvert.DeserializeObject(serialized); - }); - - Assert.Equal("The JSON patch document was malformed and could not be parsed.", exception.Message); - } - - [Fact] - public void Deserialization_Fails_ForInvalidTypedJsonPatchDocument() - { - // Arrange - var serialized = "{\"Operations\": [{ \"op\": \"replace\", \"path\": \"/title\", \"value\": \"New Title\"}]}"; - - // Act & Assert - var exception = Assert.Throws(() => - { - var deserialized - = JsonConvert.DeserializeObject>(serialized); - }); - - Assert.Equal("The JSON patch document was malformed and could not be parsed.", exception.Message); - } - - [Fact] - public void SerializationTests() - { - // Arrange - var doc = new SimpleObject() - { - StringProperty = "A", - DecimalValue = 10, - DoubleValue = 10, - FloatValue = 10, - IntegerValue = 10 - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace(o => o.StringProperty, "B"); - patchDoc.Replace(o => o.DecimalValue, 12); - patchDoc.Replace(o => o.DoubleValue, 12); - patchDoc.Replace(o => o.FloatValue, 12); - patchDoc.Replace(o => o.IntegerValue, 12); - - // serialize & deserialize - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserizalized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserizalized.ApplyTo(doc); - - // Assert - Assert.Equal("B", doc.StringProperty); - Assert.Equal(12, doc.DecimalValue); - Assert.Equal(12, doc.DoubleValue); - Assert.Equal(12, doc.FloatValue); - Assert.Equal(12, doc.IntegerValue); - } - - [Fact] - public void SerializeAndReplaceGuidTest() - { - // Arrange - var doc = new SimpleObject() - { - GuidValue = Guid.NewGuid() - }; - - var newGuid = Guid.NewGuid(); - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace(o => o.GuidValue, newGuid); - - // serialize & deserialize - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserizalized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserizalized.ApplyTo(doc); - - // Assert - Assert.Equal(newGuid, doc.GuidValue); - } - - [Fact] - public void SerializeAndReplaceNestedObjectTest() - { - // Arrange - var doc = new SimpleObjectWithNestedObject() - { - SimpleObject = new SimpleObject() - { - IntegerValue = 5, - IntegerList = new List() { 1, 2, 3 } - } - }; - - var newDTO = new SimpleObject() - { - DoubleValue = 1 - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace(o => o.SimpleObject, newDTO); - - // serialize & deserialize - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal(1, doc.SimpleObject.DoubleValue); - Assert.Equal(0, doc.SimpleObject.IntegerValue); - Assert.Null(doc.SimpleObject.IntegerList); - } - - [Fact] - public void ReplaceInList() - { - // Arrange - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace(o => o.IntegerList, 5, 0); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 5, 2, 3 }, doc.IntegerList); - } - - [Fact] - public void ReplaceInListWithSerialization() - { - // Arrange - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace(o => o.IntegerList, 5, 0); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 5, 2, 3 }, doc.IntegerList); - } - - [Fact] - public void ReplaceFullList() - { - // Arrange - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace>(o => o.IntegerList, new List() { 4, 5, 6 }); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 4, 5, 6 }, doc.IntegerList); - } - - [Fact] - public void ReplaceFullListWithSerialization() - { - // Arrange - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace>(o => o.IntegerList, new List() { 4, 5, 6 }); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 4, 5, 6 }, doc.IntegerList); - } - - [Fact] - public void ReplaceFullListFromEnumerable() - { - // Arrange - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace>(o => o.IntegerList, new List() { 4, 5, 6 }); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 4, 5, 6 }, doc.IntegerList); - } - - [Fact] - public void ReplaceFullListFromEnumerableWithSerialization() - { - // Arrange - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace>(o => o.IntegerList, new List() { 4, 5, 6 }); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 4, 5, 6 }, doc.IntegerList); - } - - [Fact] - public void ReplaceFullListWithCollection() - { - // Arrange - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace>(o => o.IntegerList, new Collection() { 4, 5, 6 }); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 4, 5, 6 }, doc.IntegerList); - } - - [Fact] - public void ReplaceFullListWithCollectionWithSerialization() - { - // Arrange - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace>(o => o.IntegerList, new Collection() { 4, 5, 6 }); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 4, 5, 6 }, doc.IntegerList); - } - - [Fact] - public void ReplaceAtEndOfList() - { - // Arrange - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace(o => o.IntegerList, 5); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 1, 2, 5 }, doc.IntegerList); - } - - [Fact] - public void ReplaceAtEndOfListWithSerialization() - { - // Arrange - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace(o => o.IntegerList, 5); - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 1, 2, 5 }, doc.IntegerList); - } - - [Fact] - public void ReplaceInListInvalidInvalidPositionTooLarge() - { - // Arrange - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace(o => o.IntegerList, 5, 3); - - // Act & Assert - var exception = Assert.Throws(() => - { - patchDoc.ApplyTo(doc); - }); - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "3"), - exception.Message); - } - - [Fact] - public void ReplaceInListInvalidInvalidPositionTooLargeWithSerialization() - { - // Arrange - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace(o => o.IntegerList, 5, 3); - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act & Assert - var exception = Assert.Throws(() => - { - deserialized.ApplyTo(doc); - }); - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "3"), - exception.Message); - } - - [Fact] - public void ReplaceInListInvalidPositionTooSmall() - { - // Arrange - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace(o => o.IntegerList, 5, -1); - - // Act & Assert - var exception = Assert.Throws(() => - { - patchDoc.ApplyTo(doc); - }); - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "-1"), - exception.Message); - } - - [Fact] - public void ReplaceInListInvalidPositionTooSmallWithSerialization() - { - // Arrange - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Replace(o => o.IntegerList, 5, -1); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act & Assert - var exception = Assert.Throws(() => - { - deserialized.ApplyTo(doc); - }); - Assert.Equal( - string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "-1"), - exception.Message); - } - - [Fact] - public void Copy() - { - // Arrange - var doc = new SimpleObject() - { - StringProperty = "A", - AnotherStringProperty = "B" - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy(o => o.StringProperty, o => o.AnotherStringProperty); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal("A", doc.AnotherStringProperty); - } - - [Fact] - public void CopyWithSerialization() - { - // Arrange - var doc = new SimpleObject() - { - StringProperty = "A", - AnotherStringProperty = "B" - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy(o => o.StringProperty, o => o.AnotherStringProperty); - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal("A", doc.AnotherStringProperty); - } - - [Fact] - public void CopyInList() - { - // Arrange - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy(o => o.IntegerList, 0, o => o.IntegerList, 1); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 1, 1, 2, 3 }, doc.IntegerList); - } - - [Fact] - public void CopyInListWithSerialization() - { - // Arrange - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy(o => o.IntegerList, 0, o => o.IntegerList, 1); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 1, 1, 2, 3 }, doc.IntegerList); - } - - [Fact] - public void CopyFromListToEndOfList() - { - // Arrange - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy(o => o.IntegerList, 0, o => o.IntegerList); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 1, 2, 3, 1 }, doc.IntegerList); - } - - [Fact] - public void CopyFromListToEndOfListWithSerialization() - { - // Arrange - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy(o => o.IntegerList, 0, o => o.IntegerList); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 1, 2, 3, 1 }, doc.IntegerList); - } - - [Fact] - public void CopyFromListToNonList() - { - // Arrange - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy(o => o.IntegerList, 0, o => o.IntegerValue); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal(1, doc.IntegerValue); - } - - [Fact] - public void CopyFromListToNonListWithSerialization() - { - // Arrange - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy(o => o.IntegerList, 0, o => o.IntegerValue); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal(1, doc.IntegerValue); - } - - [Fact] - public void CopyFromNonListToList() - { - // Arrange - var doc = new SimpleObject() - { - IntegerValue = 5, - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy(o => o.IntegerValue, o => o.IntegerList, 0); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 5, 1, 2, 3 }, doc.IntegerList); - } - - [Fact] - public void CopyFromNonListToListWithSerialization() - { - // Arrange - var doc = new SimpleObject() - { - IntegerValue = 5, - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy(o => o.IntegerValue, o => o.IntegerList, 0); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 5, 1, 2, 3 }, doc.IntegerList); - } - - [Fact] - public void CopyToEndOfList() - { - // Arrange - var doc = new SimpleObject() - { - IntegerValue = 5, - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy(o => o.IntegerValue, o => o.IntegerList); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 1, 2, 3, 5 }, doc.IntegerList); - } - - [Fact] - public void CopyToEndOfListWithSerialization() - { - // Arrange - var doc = new SimpleObject() - { - IntegerValue = 5, - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Copy(o => o.IntegerValue, o => o.IntegerList); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 1, 2, 3, 5 }, doc.IntegerList); - } - - [Fact] - public void Move() - { - // Arrange - var doc = new SimpleObject() - { - StringProperty = "A", - AnotherStringProperty = "B" - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move(o => o.StringProperty, o => o.AnotherStringProperty); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal("A", doc.AnotherStringProperty); - Assert.Null(doc.StringProperty); - } - - [Fact] - public void MoveWithSerialization() - { - // Arrange - var doc = new SimpleObject() - { - StringProperty = "A", - AnotherStringProperty = "B" - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move(o => o.StringProperty, o => o.AnotherStringProperty); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal("A", doc.AnotherStringProperty); - Assert.Null(doc.StringProperty); - } - - [Fact] - public void MoveInList() - { - // Arrange - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move(o => o.IntegerList, 0, o => o.IntegerList, 1); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 2, 1, 3 }, doc.IntegerList); - } - - [Fact] - public void MoveInListWithSerialization() - { - // Arrange - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move(o => o.IntegerList, 0, o => o.IntegerList, 1); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 2, 1, 3 }, doc.IntegerList); - } - - [Fact] - public void MoveFromListToEndOfList() - { - // Arrange - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move(o => o.IntegerList, 0, o => o.IntegerList); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 2, 3, 1 }, doc.IntegerList); - } - - [Fact] - public void MoveFromListToEndOfListWithSerialization() - { - // Arrange - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move(o => o.IntegerList, 0, o => o.IntegerList); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 2, 3, 1 }, doc.IntegerList); - } - - [Fact] - public void MoveFomListToNonList() - { - // Arrange - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move(o => o.IntegerList, 0, o => o.IntegerValue); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 2, 3 }, doc.IntegerList); - Assert.Equal(1, doc.IntegerValue); - } - - [Fact] - public void MoveFomListToNonListWithSerialization() - { - // Arrange - var doc = new SimpleObject() - { - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move(o => o.IntegerList, 0, o => o.IntegerValue); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 2, 3 }, doc.IntegerList); - Assert.Equal(1, doc.IntegerValue); - } - - [Fact] - public void MoveFromNonListToList() - { - // Arrange - var doc = new SimpleObject() - { - IntegerValue = 5, - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move(o => o.IntegerValue, o => o.IntegerList, 0); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal(0, doc.IntegerValue); - Assert.Equal(new List() { 5, 1, 2, 3 }, doc.IntegerList); - } - - [Fact] - public void MoveFromNonListToListWithSerialization() - { - // Arrange - var doc = new SimpleObject() - { - IntegerValue = 5, - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move(o => o.IntegerValue, o => o.IntegerList, 0); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal(0, doc.IntegerValue); - Assert.Equal(new List() { 5, 1, 2, 3 }, doc.IntegerList); - } - - [Fact] - public void MoveToEndOfList() - { - // Arrange - var doc = new SimpleObject() - { - IntegerValue = 5, - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move(o => o.IntegerValue, o => o.IntegerList); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal(0, doc.IntegerValue); - Assert.Equal(new List() { 1, 2, 3, 5 }, doc.IntegerList); - } - - [Fact] - public void MoveToEndOfListWithSerialization() - { - // Arrange - var doc = new SimpleObject() - { - IntegerValue = 5, - IntegerList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Move(o => o.IntegerValue, o => o.IntegerList); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Equal(0, doc.IntegerValue); - Assert.Equal(new List() { 1, 2, 3, 5 }, doc.IntegerList); - } - - private class Class6 - { - public IDictionary DictionaryOfStringToInteger { get; } = new Dictionary(); - } - - [Fact] - public void Add_WhenDictionary_ValueIsNonObject_Succeeds() - { - // Arrange - var model = new Class6(); - model.DictionaryOfStringToInteger["one"] = 1; - model.DictionaryOfStringToInteger["two"] = 2; - var patchDocument = new JsonPatchDocument(); - patchDocument.Add("/DictionaryOfStringToInteger/three", 3); - - // Act - patchDocument.ApplyTo(model); - - // Assert - Assert.Equal(3, model.DictionaryOfStringToInteger.Count); - Assert.Equal(1, model.DictionaryOfStringToInteger["one"]); - Assert.Equal(2, model.DictionaryOfStringToInteger["two"]); - Assert.Equal(3, model.DictionaryOfStringToInteger["three"]); - } - - [Fact] - public void Remove_WhenDictionary_ValueIsNonObject_Succeeds() - { - // Arrange - var model = new Class6(); - model.DictionaryOfStringToInteger["one"] = 1; - model.DictionaryOfStringToInteger["two"] = 2; - var patchDocument = new JsonPatchDocument(); - patchDocument.Remove("/DictionaryOfStringToInteger/two"); - - // Act - patchDocument.ApplyTo(model); - - // Assert - Assert.Equal(1, model.DictionaryOfStringToInteger.Count); - Assert.Equal(1, model.DictionaryOfStringToInteger["one"]); - } - - [Fact] - public void Replace_WhenDictionary_ValueIsNonObject_Succeeds() - { - // Arrange - var model = new Class6(); - model.DictionaryOfStringToInteger["one"] = 1; - model.DictionaryOfStringToInteger["two"] = 2; - var patchDocument = new JsonPatchDocument(); - patchDocument.Replace("/DictionaryOfStringToInteger/two", 20); - - // Act - patchDocument.ApplyTo(model); - - // Assert - Assert.Equal(2, model.DictionaryOfStringToInteger.Count); - Assert.Equal(1, model.DictionaryOfStringToInteger["one"]); - Assert.Equal(20, model.DictionaryOfStringToInteger["two"]); - } - - private class Customer - { - public string Name { get; set; } - public Address Address { get; set; } - } - - private class Address - { - public string City { get; set; } - } - - private class Class8 - { - public IDictionary DictionaryOfStringToCustomer { get; } = new Dictionary(); - } - - [Fact] - public void Replace_WhenDictionary_ValueAPocoType_Succeeds() - { - // Arrange - var key1 = "key1"; - var value1 = new Customer() { Name = "Jamesss" }; - var key2 = "key2"; - var value2 = new Customer() { Name = "Mike" }; - var model = new Class8(); - model.DictionaryOfStringToCustomer[key1] = value1; - model.DictionaryOfStringToCustomer[key2] = value2; - var patchDocument = new JsonPatchDocument(); - patchDocument.Replace($"/DictionaryOfStringToCustomer/{key1}/Name", "James"); - - // Act - patchDocument.ApplyTo(model); - - // Assert - Assert.Equal(2, model.DictionaryOfStringToCustomer.Count); - var actualValue1 = model.DictionaryOfStringToCustomer[key1]; - Assert.NotNull(actualValue1); - Assert.Equal("James", actualValue1.Name); - } - - [Fact] - public void Replace_WhenDictionary_ValueAPocoType_Succeeds_WithSerialization() - { - // Arrange - var key1 = "key1"; - var value1 = new Customer() { Name = "Jamesss" }; - var key2 = "key2"; - var value2 = new Customer() { Name = "Mike" }; - var model = new Class8(); - model.DictionaryOfStringToCustomer[key1] = value1; - model.DictionaryOfStringToCustomer[key2] = value2; - var patchDocument = new JsonPatchDocument(); - patchDocument.Replace($"/DictionaryOfStringToCustomer/{key1}/Name", "James"); - var serialized = JsonConvert.SerializeObject(patchDocument); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - patchDocument.ApplyTo(model); - - // Assert - Assert.Equal(2, model.DictionaryOfStringToCustomer.Count); - var actualValue1 = model.DictionaryOfStringToCustomer[key1]; - Assert.NotNull(actualValue1); - Assert.Equal("James", actualValue1.Name); - } - - [Fact] - public void Replace_WhenDictionary_ValueAPocoType_WithEscaping_Succeeds() - { - // Arrange - var key1 = "Foo/Name"; - var value1 = new Customer() { Name = "Jamesss" }; - var key2 = "Foo"; - var value2 = new Customer() { Name = "Mike" }; - var model = new Class8(); - model.DictionaryOfStringToCustomer[key1] = value1; - model.DictionaryOfStringToCustomer[key2] = value2; - var patchDocument = new JsonPatchDocument(); - patchDocument.Replace($"/DictionaryOfStringToCustomer/Foo~1Name/Name", "James"); - - // Act - patchDocument.ApplyTo(model); - - // Assert - Assert.Equal(2, model.DictionaryOfStringToCustomer.Count); - var actualValue1 = model.DictionaryOfStringToCustomer[key1]; - var actualValue2 = model.DictionaryOfStringToCustomer[key2]; - Assert.NotNull(actualValue1); - Assert.Equal("James", actualValue1.Name); - Assert.Equal("Mike", actualValue2.Name); - - } - - [Fact] - public void Replace_DeepNested_DictionaryValue_Succeeds() - { - // Arrange - var key1 = "key1"; - var value1 = new Customer() { Name = "Jamesss" }; - var key2 = "key2"; - var value2 = new Customer() { Name = "Mike" }; - var model = new Class8(); - model.DictionaryOfStringToCustomer[key1] = value1; - model.DictionaryOfStringToCustomer[key2] = value2; - var patchDocument = new JsonPatchDocument(); - patchDocument.Replace($"/DictionaryOfStringToCustomer/{key1}/Name", "James"); - - // Act - patchDocument.ApplyTo(model); - - // Assert - Assert.Equal(2, model.DictionaryOfStringToCustomer.Count); - var actualValue1 = model.DictionaryOfStringToCustomer[key1]; - Assert.NotNull(actualValue1); - Assert.Equal("James", actualValue1.Name); - } - - [Fact] - public void Replace_DeepNested_DictionaryValue_Succeeds_WithSerialization() - { - // Arrange - var key1 = "key1"; - var value1 = new Customer() { Name = "James", Address = new Address { City = "Redmond" } }; - var key2 = "key2"; - var value2 = new Customer() { Name = "Mike", Address = new Address { City = "Seattle" } }; - var model = new Class8(); - model.DictionaryOfStringToCustomer[key1] = value1; - model.DictionaryOfStringToCustomer[key2] = value2; - var patchDocument = new JsonPatchDocument(); - patchDocument.Replace($"/DictionaryOfStringToCustomer/{key1}/Address/City", "Bellevue"); - var serialized = JsonConvert.SerializeObject(patchDocument); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - patchDocument.ApplyTo(model); - - // Assert - Assert.Equal(2, model.DictionaryOfStringToCustomer.Count); - var actualValue1 = model.DictionaryOfStringToCustomer[key1]; - Assert.NotNull(actualValue1); - Assert.Equal("James", actualValue1.Name); - var address = actualValue1.Address; - Assert.NotNull(address); - Assert.Equal("Bellevue", address.City); - } - - class Class9 - { - public List StringList { get; set; } = new List(); - } - - [Fact] - public void AddToNonIntegerListAtEnd() - { - // Arrange - var model = new Class9() - { - StringList = new List() - }; - model.StringList.Add("string1"); - model.StringList.Add("string2"); - var patchDoc = new JsonPatchDocument(); - patchDoc.Add("/StringList/0", "string3"); - - // Act - patchDoc.ApplyTo(model); - - // Assert - Assert.Equal(new List() { "string3", "string1", "string2" }, model.StringList); - } - - [Fact] - public void AddMember_OnPOCO_WithNullPropertyValue_ShouldAddPropertyValue() - { - // Arrange - var doc = new SimpleObject() - { - StringProperty = null - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add(o => o.StringProperty, "B"); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal("B", doc.StringProperty); - } - - private class Class1 - { - public IDictionary USStates { get; set; } = new Dictionary(); - } - - [Fact] - public void AddMember_OnDictionaryProperty_ShouldAddKeyValueMember() - { - // Arrange - var expected = "Washington"; - var model = new Class1(); - var patchDoc = new JsonPatchDocument(); - patchDoc.Add("/USStates/WA", expected); - - // Act - patchDoc.ApplyTo(model); - - // Assert - Assert.Equal(1, model.USStates.Count); - Assert.Equal(expected, model.USStates["WA"]); - } - - [Fact] - public void AddMember_OnDictionaryProperty_ShouldAddKeyValueMember_WithSerialization() - { - // Arrange - var expected = "Washington"; - var model = new Class1(); - var patchDoc = new JsonPatchDocument(); - patchDoc.Add("/USStates/WA", expected); - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(model); - - // Assert - Assert.Equal(1, model.USStates.Count); - Assert.Equal(expected, model.USStates["WA"]); - } - - private class Class2 - { - public Class1 Class1Property { get; set; } = new Class1(); - } - - [Fact] - public void AddMember_OnDictionaryPropertyDeeplyNested_ShouldAddKeyValueMember() - { - // Arrange - var expected = "Washington"; - var model = new Class2(); - var patchDoc = new JsonPatchDocument(); - patchDoc.Add("/Class1Property/USStates/WA", expected); - - // Act - patchDoc.ApplyTo(model); - - // Assert - Assert.Equal(1, model.Class1Property.USStates.Count); - Assert.Equal(expected, model.Class1Property.USStates["WA"]); - } - - [Fact] - public void AddMember_OnDictionaryPropertyDeeplyNested_ShouldAddKeyValueMember_WithSerialization() - { - // Arrange - var expected = "Washington"; - var model = new Class2(); - var patchDoc = new JsonPatchDocument(); - patchDoc.Add("/Class1Property/USStates/WA", expected); - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(model); - - // Assert - Assert.Equal(1, model.Class1Property.USStates.Count); - Assert.Equal(expected, model.Class1Property.USStates["WA"]); - } - - [Fact] - public void AddMember_OnDictionaryObjectDirectly_ShouldAddKeyValueMember() - { - // Arrange - var expected = "Washington"; - var model = new Dictionary(); - var patchDoc = new JsonPatchDocument(); - patchDoc.Add("/WA", expected); - - // Act - patchDoc.ApplyTo(model); - - // Assert - Assert.Single(model); - Assert.Equal(expected, model["WA"]); - } - - [Fact] - public void AddMember_OnDictionaryObjectDirectly_ShouldAddKeyValueMember_WithSerialization() - { - // Arrange - var expected = "Washington"; - var model = new Dictionary(); - var patchDoc = new JsonPatchDocument(); - patchDoc.Add("/WA", expected); - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>>(serialized); - - // Act - deserialized.ApplyTo(model); - - // Assert - Assert.Single(model); - Assert.Equal(expected, model["WA"]); - } - - [Fact] - public void AddElement_ToListDirectly_ShouldAppendValue() - { - // Arrange - var model = new List() { 1, 2, 3 }; - var patchDoc = new JsonPatchDocument(); - patchDoc.Add("/-", value: 4); - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>>(serialized); - - // Act - deserialized.ApplyTo(model); - - // Assert - Assert.Equal(new List() { 1, 2, 3, 4 }, model); - } - - [Fact] - public void AddElement_ToListDirectly_ShouldAppendValue_WithSerialization() - { - // Arrange - var model = new List() { 1, 2, 3 }; - var patchDoc = new JsonPatchDocument(); - patchDoc.Add("/-", value: 4); - - // Act - patchDoc.ApplyTo(model); - - // Assert - Assert.Equal(new List() { 1, 2, 3, 4 }, model); - } - - [Fact] - public void AddElement_ToListDirectly_ShouldAddValue_AtSuppliedPosition() - { - // Arrange - var model = new List() { 1, 2, 3 }; - var patchDoc = new JsonPatchDocument(); - patchDoc.Add("/0", value: 4); - - // Act - patchDoc.ApplyTo(model); - - // Assert - Assert.Equal(new List() { 4, 1, 2, 3 }, model); - } - - [Fact] - public void AddElement_ToListDirectly_ShouldAddValue_AtSuppliedPosition_WithSerialization() - { - // Arrange - var model = new List() { 1, 2, 3 }; - var patchDoc = new JsonPatchDocument(); - patchDoc.Add("/0", value: 4); - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>>(serialized); - - // Act - deserialized.ApplyTo(model); - - // Assert - Assert.Equal(new List() { 4, 1, 2, 3 }, model); - } - - class ListOnDictionary - { - public IDictionary> NamesAndBadgeIds { get; set; } = new Dictionary>(); - } - - [Fact] - public void AddElement_ToList_OnDictionary_ShouldAddValue_AtSuppliedPosition() - { - // Arrange - var model = new ListOnDictionary(); - model.NamesAndBadgeIds["James"] = new List(); - var patchDoc = new JsonPatchDocument(); - patchDoc.Add("/NamesAndBadgeIds/James/-", 200); - - // Act - patchDoc.ApplyTo(model); - - // Assert - var list = model.NamesAndBadgeIds["James"]; - Assert.NotNull(list); - Assert.Equal(new List() { 200 }, list); - } - - [Fact] - public void AddElement_ToList_OnPOCO_ShouldAddValue_AtSuppliedPosition() - { - // Arrange - var doc = new SimpleObject() - { - IntegerIList = new List() { 1, 2, 3 } - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add(o => o.IntegerIList, 4, 0); - - // Act - patchDoc.ApplyTo(doc); - - // Assert - Assert.Equal(new List() { 4, 1, 2, 3 }, doc.IntegerIList); - } - - class Class3 - { - public SimpleObject SimpleObjectProperty { get; set; } = new SimpleObject(); - } - - [Fact] - public void AddElement_ToDeeplyNestedListProperty_OnPOCO_ShouldAddValue_AtSuppliedPosition() - { - // Arrange - var model = new Class3() - { - SimpleObjectProperty = new SimpleObject() - { - IntegerIList = new List() { 1, 2, 3 } - } - }; - var patchDoc = new JsonPatchDocument(); - patchDoc.Add(o => o.SimpleObjectProperty.IntegerIList, value: 4, position: 0); - - // Act - patchDoc.ApplyTo(model); - - // Assert - Assert.Equal(new List() { 4, 1, 2, 3 }, model.SimpleObjectProperty.IntegerIList); - } - - [Fact] - public void AddElement_ToDeeplyNestedListProperty_OnPOCO_ShouldAddValue_AtSuppliedPosition_WithSerialization() - { - // Arrange - var model = new Class3() - { - SimpleObjectProperty = new SimpleObject() - { - IntegerIList = new List() { 1, 2, 3 } - } - }; - var patchDoc = new JsonPatchDocument(); - patchDoc.Add(o => o.SimpleObjectProperty.IntegerIList, value: 4, position: 0); - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(model); - - // Assert - Assert.Equal(new List() { 4, 1, 2, 3 }, model.SimpleObjectProperty.IntegerIList); - } - - class Class4 - { - public int IntegerProperty { get; set; } - } - - [Fact] - public void Remove_OnNonReferenceType_POCOProperty_ShouldSetDefaultValue() - { - // Arrange - var model = new Class4() - { - IntegerProperty = 10 - }; - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove(o => o.IntegerProperty); - - // Act - patchDoc.ApplyTo(model); - - // Assert - Assert.Equal(0, model.IntegerProperty); - } - - [Fact] - public void Remove_OnNonReferenceType_POCOProperty_ShouldSetDefaultValue_WithSerialization() - { - // Arrange - var doc = new SimpleObject() - { - StringProperty = "A" - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Remove(o => o.StringProperty); - - var serialized = JsonConvert.SerializeObject(patchDoc); - var deserialized = JsonConvert.DeserializeObject>(serialized); - - // Act - deserialized.ApplyTo(doc); - - // Assert - Assert.Null(doc.StringProperty); - } - - class ClassWithPrivateProperties - { - public string Name { get; set; } - private int Age { get; set; } = 45; - } - - [Fact] - public void Add_OnPrivateProperties_FailesWithException() - { - // Arrange - var doc = new ClassWithPrivateProperties() - { - Name = "James" - }; - - // create patch - var patchDoc = new JsonPatchDocument(); - patchDoc.Add("/Age", 30); - - // Act & Assert - var exception = Assert.Throws(() => patchDoc.ApplyTo(doc)); - Assert.Equal( - string.Format("The target location specified by path segment '{0}' was not found.", "Age"), - exception.Message); - } - } -} diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/SimpleObjectWithNestedObjectWithNullCheck.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/SimpleObjectWithNestedObjectWithNullCheck.cs deleted file mode 100644 index f2423700d8..0000000000 --- a/test/Microsoft.AspNetCore.JsonPatch.Test/SimpleObjectWithNestedObjectWithNullCheck.cs +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -namespace Microsoft.AspNetCore.JsonPatch -{ - public class SimpleObjectWithNestedObjectWithNullCheck - { - public SimpleObjectWithNullCheck SimpleObjectWithNullCheck { get; set; } - - public SimpleObjectWithNestedObjectWithNullCheck() - { - SimpleObjectWithNullCheck = new SimpleObjectWithNullCheck(); - } - } -} diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/SimpleObjectWithNullCheck.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/SimpleObjectWithNullCheck.cs deleted file mode 100644 index 83338c5a23..0000000000 --- a/test/Microsoft.AspNetCore.JsonPatch.Test/SimpleObjectWithNullCheck.cs +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; - -namespace Microsoft.AspNetCore.JsonPatch -{ - public class SimpleObjectWithNullCheck - { - private string stringProperty; - - public string StringProperty - { - get - { - return stringProperty; - } - - set - { - if (value == null) - { - throw new ArgumentNullException(); - } - - stringProperty = value; - } - } - } -} diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/TestObjectModels/Customer.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/TestObjectModels/Customer.cs new file mode 100644 index 0000000000..c8a5aa22b1 --- /dev/null +++ b/test/Microsoft.AspNetCore.JsonPatch.Test/TestObjectModels/Customer.cs @@ -0,0 +1,17 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +namespace Microsoft.AspNetCore.JsonPatch.Internal +{ + internal class Customer + { + private string _name; + private int _age; + + public Customer(string name, int age) + { + _name = name; + _age = age; + } + } +} diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/DynamicTestObject.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/TestObjectModels/DynamicTestObject.cs similarity index 98% rename from test/Microsoft.AspNetCore.JsonPatch.Test/DynamicTestObject.cs rename to test/Microsoft.AspNetCore.JsonPatch.Test/TestObjectModels/DynamicTestObject.cs index 4268d5526c..94ecf1685d 100644 --- a/test/Microsoft.AspNetCore.JsonPatch.Test/DynamicTestObject.cs +++ b/test/Microsoft.AspNetCore.JsonPatch.Test/TestObjectModels/DynamicTestObject.cs @@ -4,7 +4,7 @@ using System.Collections.Generic; using System.Dynamic; -namespace Microsoft.AspNetCore.JsonPatch.Internal +namespace Microsoft.AspNetCore.JsonPatch { public class DynamicTestObject : DynamicObject { diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/InheritedObject.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/TestObjectModels/InheritedObject.cs similarity index 100% rename from test/Microsoft.AspNetCore.JsonPatch.Test/InheritedObject.cs rename to test/Microsoft.AspNetCore.JsonPatch.Test/TestObjectModels/InheritedObject.cs diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/NestedObject.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/TestObjectModels/NestedObject.cs similarity index 85% rename from test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/NestedObject.cs rename to test/Microsoft.AspNetCore.JsonPatch.Test/TestObjectModels/NestedObject.cs index 050ad3a3b4..1b42d0d7ef 100644 --- a/test/Microsoft.AspNetCore.JsonPatch.Test/Dynamic/NestedObject.cs +++ b/test/Microsoft.AspNetCore.JsonPatch.Test/TestObjectModels/NestedObject.cs @@ -1,11 +1,11 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -namespace Microsoft.AspNetCore.JsonPatch.Internal +namespace Microsoft.AspNetCore.JsonPatch { public class NestedObject { public string StringProperty { get; set; } public dynamic DynamicProperty { get; set; } } -} +} \ No newline at end of file diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/SimpleObject.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/TestObjectModels/SimpleObject.cs similarity index 86% rename from test/Microsoft.AspNetCore.JsonPatch.Test/SimpleObject.cs rename to test/Microsoft.AspNetCore.JsonPatch.Test/TestObjectModels/SimpleObject.cs index 28a9570c2b..651a91bdcf 100644 --- a/test/Microsoft.AspNetCore.JsonPatch.Test/SimpleObject.cs +++ b/test/Microsoft.AspNetCore.JsonPatch.Test/TestObjectModels/SimpleObject.cs @@ -8,9 +8,11 @@ namespace Microsoft.AspNetCore.JsonPatch { public class SimpleObject { + public List SimpleObjectList { get; set; } public List IntegerList { get; set; } public IList IntegerIList { get; set; } public int IntegerValue { get; set; } + public int AnotherIntegerValue { get; set; } public string StringProperty { get; set; } public string AnotherStringProperty { get; set; } public decimal DecimalValue { get; set; } diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/SimpleObjectWithNestedObject.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/TestObjectModels/SimpleObjectWithNestedObject.cs similarity index 100% rename from test/Microsoft.AspNetCore.JsonPatch.Test/SimpleObjectWithNestedObject.cs rename to test/Microsoft.AspNetCore.JsonPatch.Test/TestObjectModels/SimpleObjectWithNestedObject.cs