From 0795ec1d9afdbac6c85990f7beb414bf7241fb3e Mon Sep 17 00:00:00 2001 From: Jass Bagga Date: Thu, 26 Jan 2017 11:28:35 -0800 Subject: [PATCH] Add tests for Replace operation with null checks for property (#56) See #32 --- .../NestedObjectTests.cs | 23 ++++++++++++++ .../ObjectAdapterTests.cs | 20 +++++++++++++ .../SimpleDTOWithNestedDTOWithNullCheck.cs | 15 ++++++++++ .../SimpleDTOWithNullCheck.cs | 30 +++++++++++++++++++ 4 files changed, 88 insertions(+) create mode 100644 test/Microsoft.AspNetCore.JsonPatch.Test/SimpleDTOWithNestedDTOWithNullCheck.cs create mode 100644 test/Microsoft.AspNetCore.JsonPatch.Test/SimpleDTOWithNullCheck.cs diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/NestedObjectTests.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/NestedObjectTests.cs index 345ffe2201..cc2e990997 100644 --- a/test/Microsoft.AspNetCore.JsonPatch.Test/NestedObjectTests.cs +++ b/test/Microsoft.AspNetCore.JsonPatch.Test/NestedObjectTests.cs @@ -907,6 +907,29 @@ namespace Microsoft.AspNetCore.JsonPatch Assert.Equal(12, doc.SimpleDTO.DecimalValue); } + [Fact] + public void Replace_DTOWithNullCheck() + { + // Arrange + var doc = new SimpleDTOWithNestedDTOWithNullCheck() + { + SimpleDTOWithNullCheck = new SimpleDTOWithNullCheck() + { + StringProperty = "A" + } + }; + + // create patch + var patchDoc = new JsonPatchDocument(); + patchDoc.Replace(o => o.SimpleDTOWithNullCheck.StringProperty, "B"); + + // Act + patchDoc.ApplyTo(doc); + + // Assert + Assert.Equal("B", doc.SimpleDTOWithNullCheck.StringProperty); + } + [Fact] public void ReplaceWithSerialization() { diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/ObjectAdapterTests.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/ObjectAdapterTests.cs index 5898311044..d14daadbff 100644 --- a/test/Microsoft.AspNetCore.JsonPatch.Test/ObjectAdapterTests.cs +++ b/test/Microsoft.AspNetCore.JsonPatch.Test/ObjectAdapterTests.cs @@ -648,6 +648,26 @@ namespace Microsoft.AspNetCore.JsonPatch.Adapters Assert.Equal(12, doc.DecimalValue); } + [Fact] + public void Replace_DTOWithNullCheck() + { + // Arrange + var doc = new SimpleDTOWithNullCheck() + { + 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() { diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/SimpleDTOWithNestedDTOWithNullCheck.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/SimpleDTOWithNestedDTOWithNullCheck.cs new file mode 100644 index 0000000000..308f23b470 --- /dev/null +++ b/test/Microsoft.AspNetCore.JsonPatch.Test/SimpleDTOWithNestedDTOWithNullCheck.cs @@ -0,0 +1,15 @@ +// 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 SimpleDTOWithNestedDTOWithNullCheck + { + public SimpleDTOWithNullCheck SimpleDTOWithNullCheck { get; set; } + + public SimpleDTOWithNestedDTOWithNullCheck() + { + SimpleDTOWithNullCheck = new SimpleDTOWithNullCheck(); + } + } +} diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/SimpleDTOWithNullCheck.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/SimpleDTOWithNullCheck.cs new file mode 100644 index 0000000000..d2a5fe51a4 --- /dev/null +++ b/test/Microsoft.AspNetCore.JsonPatch.Test/SimpleDTOWithNullCheck.cs @@ -0,0 +1,30 @@ +// 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 SimpleDTOWithNullCheck + { + private string stringProperty; + + public string StringProperty + { + get + { + return stringProperty; + } + + set + { + if (value == null) + { + throw new ArgumentNullException(); + } + + stringProperty = value; + } + } + } +}