From 91e3e9c0e9fd925a65816505bb6c83ae689119e2 Mon Sep 17 00:00:00 2001 From: Gleb Osokin Date: Mon, 16 Nov 2020 12:05:07 +0100 Subject: [PATCH 01/10] support Json partial match with JsonPartialMatcher --- src/WireMock.Net/Matchers/JsonMatcher.cs | 37 ++- .../Matchers/JsonPartialMatcher.cs | 86 +++++ .../Matchers/JsonMatcherTests.cs | 137 +++++--- .../Matchers/JsonPartialMatcherTests.cs | 299 ++++++++++++++++++ 4 files changed, 503 insertions(+), 56 deletions(-) create mode 100644 src/WireMock.Net/Matchers/JsonPartialMatcher.cs create mode 100644 test/WireMock.Net.Tests/Matchers/JsonPartialMatcherTests.cs diff --git a/src/WireMock.Net/Matchers/JsonMatcher.cs b/src/WireMock.Net/Matchers/JsonMatcher.cs index 85cfe6c0e..2aceb3501 100644 --- a/src/WireMock.Net/Matchers/JsonMatcher.cs +++ b/src/WireMock.Net/Matchers/JsonMatcher.cs @@ -1,4 +1,5 @@ -using System.Collections; +using System; +using System.Collections; using System.Linq; using JetBrains.Annotations; using Newtonsoft.Json; @@ -17,7 +18,7 @@ public class JsonMatcher : IValueMatcher, IIgnoreCaseMatcher public object Value { get; } /// - public string Name => "JsonMatcher"; + public virtual string Name => "JsonMatcher"; /// public MatchBehaviour MatchBehaviour { get; } @@ -29,6 +30,7 @@ public class JsonMatcher : IValueMatcher, IIgnoreCaseMatcher public bool ThrowException { get; } private readonly JToken _valueAsJToken; + private readonly Func _jTokenConverter; /// /// Initializes a new instance of the class. @@ -67,6 +69,9 @@ public JsonMatcher(MatchBehaviour matchBehaviour, [NotNull] object value, bool i Value = value; _valueAsJToken = ConvertValueToJToken(value); + _jTokenConverter = ignoreCase + ? (Func)Rename + : jToken => jToken; } /// @@ -81,7 +86,9 @@ public double IsMatch(object input) { var inputAsJToken = ConvertValueToJToken(input); - match = DeepEquals(_valueAsJToken, inputAsJToken); + match = AreEqual( + _jTokenConverter(_valueAsJToken), + _jTokenConverter(inputAsJToken)); } catch (JsonException) { @@ -95,6 +102,17 @@ public double IsMatch(object input) return MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.ToScore(match)); } + /// + /// Compares the input against the matcher value + /// + /// Matcher value + /// Input value + /// + protected virtual bool AreEqual(JToken value, JToken input) + { + return JToken.DeepEquals(value, input); + } + private static JToken ConvertValueToJToken(object value) { // Check if JToken, string, IEnumerable or object @@ -114,19 +132,6 @@ private static JToken ConvertValueToJToken(object value) } } - private bool DeepEquals(JToken value, JToken input) - { - if (!IgnoreCase) - { - return JToken.DeepEquals(value, input); - } - - JToken renamedValue = Rename(value); - JToken renamedInput = Rename(input); - - return JToken.DeepEquals(renamedValue, renamedInput); - } - private static string ToUpper(string input) { return input?.ToUpperInvariant(); diff --git a/src/WireMock.Net/Matchers/JsonPartialMatcher.cs b/src/WireMock.Net/Matchers/JsonPartialMatcher.cs new file mode 100644 index 000000000..718271373 --- /dev/null +++ b/src/WireMock.Net/Matchers/JsonPartialMatcher.cs @@ -0,0 +1,86 @@ +using System.Collections.Generic; +using System.Linq; +using JetBrains.Annotations; +using Newtonsoft.Json.Linq; + +namespace WireMock.Matchers +{ + /// + /// JsonPartialMatcher + /// + public class JsonPartialMatcher : JsonMatcher + { + /// + public override string Name => "JsonPartialMatcher"; + + /// + /// Initializes a new instance of the class. + /// + /// The string value to check for equality. + /// Ignore the case from the PropertyName and PropertyValue (string only). + /// Throw an exception when the internal matching fails because of invalid input. + public JsonPartialMatcher([NotNull] string value, bool ignoreCase = false, bool throwException = false) + : base(value, ignoreCase, throwException) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The object value to check for equality. + /// Ignore the case from the PropertyName and PropertyValue (string only). + /// Throw an exception when the internal matching fails because of invalid input. + public JsonPartialMatcher([NotNull] object value, bool ignoreCase = false, bool throwException = false) + : base(value, ignoreCase, throwException) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The match behaviour. + /// The value to check for equality. + /// Ignore the case from the PropertyName and PropertyValue (string only). + /// Throw an exception when the internal matching fails because of invalid input. + public JsonPartialMatcher(MatchBehaviour matchBehaviour, [NotNull] object value, bool ignoreCase = false, bool throwException = false) + : base(matchBehaviour, value, ignoreCase, throwException) + { + } + + /// + protected override bool AreEqual(JToken value, JToken input) + { + return IsMatch(value, input); + } + + private static bool IsMatch(JToken value, JToken token) + { + if (value == null || value == token) + return true; + + if (token == null) + return false; + + switch (value.Type) + { + case JTokenType.Object: + var nestedValues = value.ToObject>(); + return nestedValues?.Any() != true || + nestedValues.All(pair => IsMatch(pair.Value, token.SelectToken(pair.Key))); + + case JTokenType.Array: + var filtersArray = value.ToObject(); + var tokenArray = token.ToObject(); + + if (filtersArray?.Any() != true) + return true; + + return tokenArray?.Any() == true && + filtersArray.All(subFilter => tokenArray.Any(subToken => IsMatch(subFilter, subToken))); + + default: + return value.ToString() == token.ToString(); + } + } + } +} diff --git a/test/WireMock.Net.Tests/Matchers/JsonMatcherTests.cs b/test/WireMock.Net.Tests/Matchers/JsonMatcherTests.cs index 639994fa9..eb31f74ae 100644 --- a/test/WireMock.Net.Tests/Matchers/JsonMatcherTests.cs +++ b/test/WireMock.Net.Tests/Matchers/JsonMatcherTests.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.IO; using FluentAssertions; using Newtonsoft.Json; @@ -9,26 +10,26 @@ namespace WireMock.Net.Tests.Matchers { - public class JsonMatcherTests + public class JsonPartialMatcherTests { [Fact] - public void JsonMatcher_GetName() + public void JsonPartialMatcher_GetName() { // Assign - var matcher = new JsonMatcher("{}"); + var matcher = new JsonPartialMatcher("{}"); // Act string name = matcher.Name; // Assert - Check.That(name).Equals("JsonMatcher"); + Check.That(name).Equals("JsonPartialMatcher"); } [Fact] - public void JsonMatcher_GetValue() + public void JsonPartialMatcher_GetValue() { // Assign - var matcher = new JsonMatcher("{}"); + var matcher = new JsonPartialMatcher("{}"); // Act object value = matcher.Value; @@ -38,30 +39,30 @@ public void JsonMatcher_GetValue() } [Fact] - public void JsonMatcher_WithInvalidStringValue_Should_ThrowException() + public void JsonPartialMatcher_WithInvalidStringValue_Should_ThrowException() { // Act - Action action = () => new JsonMatcher(MatchBehaviour.AcceptOnMatch, "{ \"Id\""); + Action action = () => new JsonPartialMatcher(MatchBehaviour.AcceptOnMatch, "{ \"Id\""); // Assert action.Should().Throw(); } [Fact] - public void JsonMatcher_WithInvalidObjectValue_Should_ThrowException() + public void JsonPartialMatcher_WithInvalidObjectValue_Should_ThrowException() { // Act - Action action = () => new JsonMatcher(MatchBehaviour.AcceptOnMatch, new MemoryStream()); + Action action = () => new JsonPartialMatcher(MatchBehaviour.AcceptOnMatch, new MemoryStream()); // Assert action.Should().Throw(); } [Fact] - public void JsonMatcher_IsMatch_WithInvalidValue_And_ThrowExceptionIsFalse_Should_ReturnMismatch() + public void JsonPartialMatcher_IsMatch_WithInvalidValue_And_ThrowExceptionIsFalse_Should_ReturnMismatch() { // Assign - var matcher = new JsonMatcher(""); + var matcher = new JsonPartialMatcher(""); // Act double match = matcher.IsMatch(new MemoryStream()); @@ -71,10 +72,10 @@ public void JsonMatcher_IsMatch_WithInvalidValue_And_ThrowExceptionIsFalse_Shoul } [Fact] - public void JsonMatcher_IsMatch_WithInvalidValue_And_ThrowExceptionIsTrue_Should_ReturnMismatch() + public void JsonPartialMatcher_IsMatch_WithInvalidValue_And_ThrowExceptionIsTrue_Should_ReturnMismatch() { // Assign - var matcher = new JsonMatcher("", false, true); + var matcher = new JsonPartialMatcher("", false, true); // Act Action action = () => matcher.IsMatch(new MemoryStream()); @@ -84,11 +85,11 @@ public void JsonMatcher_IsMatch_WithInvalidValue_And_ThrowExceptionIsTrue_Should } [Fact] - public void JsonMatcher_IsMatch_ByteArray() + public void JsonPartialMatcher_IsMatch_ByteArray() { // Assign var bytes = new byte[0]; - var matcher = new JsonMatcher(""); + var matcher = new JsonPartialMatcher(""); // Act double match = matcher.IsMatch(bytes); @@ -98,11 +99,11 @@ public void JsonMatcher_IsMatch_ByteArray() } [Fact] - public void JsonMatcher_IsMatch_NullString() + public void JsonPartialMatcher_IsMatch_NullString() { // Assign string s = null; - var matcher = new JsonMatcher(""); + var matcher = new JsonPartialMatcher(""); // Act double match = matcher.IsMatch(s); @@ -112,11 +113,11 @@ public void JsonMatcher_IsMatch_NullString() } [Fact] - public void JsonMatcher_IsMatch_NullObject() + public void JsonPartialMatcher_IsMatch_NullObject() { // Assign object o = null; - var matcher = new JsonMatcher(""); + var matcher = new JsonPartialMatcher(""); // Act double match = matcher.IsMatch(o); @@ -126,10 +127,10 @@ public void JsonMatcher_IsMatch_NullObject() } [Fact] - public void JsonMatcher_IsMatch_JArray() + public void JsonPartialMatcher_IsMatch_JArray() { // Assign - var matcher = new JsonMatcher(new[] { "x", "y" }); + var matcher = new JsonPartialMatcher(new[] { "x", "y" }); // Act var jArray = new JArray @@ -144,10 +145,10 @@ public void JsonMatcher_IsMatch_JArray() } [Fact] - public void JsonMatcher_IsMatch_JObject() + public void JsonPartialMatcher_IsMatch_JObject() { // Assign - var matcher = new JsonMatcher(new { Id = 1, Name = "Test" }); + var matcher = new JsonPartialMatcher(new { Id = 1, Name = "Test" }); // Act var jobject = new JObject @@ -162,10 +163,10 @@ public void JsonMatcher_IsMatch_JObject() } [Fact] - public void JsonMatcher_IsMatch_WithIgnoreCaseTrue_JObject() + public void JsonPartialMatcher_IsMatch_WithIgnoreCaseTrue_JObject() { // Assign - var matcher = new JsonMatcher(new { id = 1, Name = "test" }, true); + var matcher = new JsonPartialMatcher(new { id = 1, Name = "test" }, true); // Act var jobject = new JObject @@ -180,10 +181,10 @@ public void JsonMatcher_IsMatch_WithIgnoreCaseTrue_JObject() } [Fact] - public void JsonMatcher_IsMatch_JObjectParsed() + public void JsonPartialMatcher_IsMatch_JObjectParsed() { // Assign - var matcher = new JsonMatcher(new { Id = 1, Name = "Test" }); + var matcher = new JsonPartialMatcher(new { Id = 1, Name = "Test" }); // Act var jobject = JObject.Parse("{ \"Id\" : 1, \"Name\" : \"Test\" }"); @@ -194,10 +195,10 @@ public void JsonMatcher_IsMatch_JObjectParsed() } [Fact] - public void JsonMatcher_IsMatch_WithIgnoreCaseTrue_JObjectParsed() + public void JsonPartialMatcher_IsMatch_WithIgnoreCaseTrue_JObjectParsed() { // Assign - var matcher = new JsonMatcher(new { Id = 1, Name = "TESt" }, true); + var matcher = new JsonPartialMatcher(new { Id = 1, Name = "TESt" }, true); // Act var jobject = JObject.Parse("{ \"Id\" : 1, \"Name\" : \"Test\" }"); @@ -208,10 +209,10 @@ public void JsonMatcher_IsMatch_WithIgnoreCaseTrue_JObjectParsed() } [Fact] - public void JsonMatcher_IsMatch_JArrayAsString() + public void JsonPartialMatcher_IsMatch_JArrayAsString() { // Assign - var matcher = new JsonMatcher("[ \"x\", \"y\" ]"); + var matcher = new JsonPartialMatcher("[ \"x\", \"y\" ]"); // Act var jArray = new JArray @@ -226,10 +227,10 @@ public void JsonMatcher_IsMatch_JArrayAsString() } [Fact] - public void JsonMatcher_IsMatch_JObjectAsString() + public void JsonPartialMatcher_IsMatch_JObjectAsString() { // Assign - var matcher = new JsonMatcher("{ \"Id\" : 1, \"Name\" : \"Test\" }"); + var matcher = new JsonPartialMatcher("{ \"Id\" : 1, \"Name\" : \"Test\" }"); // Act var jobject = new JObject @@ -244,10 +245,10 @@ public void JsonMatcher_IsMatch_JObjectAsString() } [Fact] - public void JsonMatcher_IsMatch_WithIgnoreCaseTrue_JObjectAsString() + public void JsonPartialMatcher_IsMatch_WithIgnoreCaseTrue_JObjectAsString() { // Assign - var matcher = new JsonMatcher("{ \"Id\" : 1, \"Name\" : \"test\" }", true); + var matcher = new JsonPartialMatcher("{ \"Id\" : 1, \"Name\" : \"test\" }", true); // Act var jobject = new JObject @@ -262,10 +263,10 @@ public void JsonMatcher_IsMatch_WithIgnoreCaseTrue_JObjectAsString() } [Fact] - public void JsonMatcher_IsMatch_JObjectAsString_RejectOnMatch() + public void JsonPartialMatcher_IsMatch_JObjectAsString_RejectOnMatch() { // Assign - var matcher = new JsonMatcher(MatchBehaviour.RejectOnMatch, "{ \"Id\" : 1, \"Name\" : \"Test\" }"); + var matcher = new JsonPartialMatcher(MatchBehaviour.RejectOnMatch, "{ \"Id\" : 1, \"Name\" : \"Test\" }"); // Act var jobject = new JObject @@ -280,10 +281,10 @@ public void JsonMatcher_IsMatch_JObjectAsString_RejectOnMatch() } [Fact] - public void JsonMatcher_IsMatch_JObjectWithDateTimeOffsetAsString() + public void JsonPartialMatcher_IsMatch_JObjectWithDateTimeOffsetAsString() { // Assign - var matcher = new JsonMatcher("{ \"preferredAt\" : \"2019-11-21T10:32:53.2210009+00:00\" }"); + var matcher = new JsonPartialMatcher("{ \"preferredAt\" : \"2019-11-21T10:32:53.2210009+00:00\" }"); // Act var jobject = new JObject @@ -295,5 +296,61 @@ public void JsonMatcher_IsMatch_JObjectWithDateTimeOffsetAsString() // Assert Assert.Equal(1.0, match); } + + [Theory] + [MemberData(nameof(ValidMatches))] + public void JsonPartialMatcher_IsMatch_StringInputValidMatch(string value, string input) + { + // Assign + var matcher = new JsonPartialMatcher(value); + + // Act + double match = matcher.IsMatch(input); + + // Assert + Assert.Equal(1.0, match); + } + + [Theory] + [MemberData(nameof(InvalidMatches))] + public void JsonPartialMatcher_IsMatch_StringInputWithInvalidMatch(string value, string input) + { + // Assign + var matcher = new JsonPartialMatcher(value); + + // Act + double match = matcher.IsMatch(input); + + // Assert + Assert.Equal(0.0, match); + } + + public static IEnumerable ValidMatches() + { + yield return new object[] { "{}", "\"test\"" }; + yield return new object[] { "\"test\"", "\"test\"" }; + yield return new object[] { "123", "123" }; + yield return new object[] { "[\"test\"]", "[\"test\"]" }; + yield return new object[] { "[\"test\"]", "[\"test\", \"test1\"]" }; + yield return new object[] { "[123]", "[123]" }; + yield return new object[] { "{ \"test\":\"value\" }", "{\"test\":\"value\"}" }; + yield return new object[] { "{ \"test.test1\":\"value\" }", "{\"test\":{\"test1\":\"value\"}}" }; + yield return new object[] { "{\"test\":{\"test1\":\"value\"}}", "{\"test\":{\"test1\":\"value\"}}" }; + yield return new object[] { "[{ \"test.test1\":\"value\" }]", "[{\"test\":{\"test1\":\"value\"}}]" }; + } + + public static IEnumerable InvalidMatches() + { + yield return new object[] { "\"test\"", null }; + yield return new object[] { "\"test1\"", "\"test2\"" }; + yield return new object[] { "123", "1234" }; + yield return new object[] { "[\"test\"]", "[\"test1\"]" }; + yield return new object[] { "[\"test\"]", "[\"test1\", \"test2\"]" }; + yield return new object[] { "[123]", "[1234]" }; + yield return new object[] { "{ \"test\":\"value\" }", "{\"test\":\"value2\"}" }; + yield return new object[] { "{ \"test.test1\":\"value\" }", "{\"test\":{\"test1\":\"value1\"}}" }; + yield return new object[] { "{\"test\":{\"test1\":\"value\"}}", "{\"test\":{\"test1\":\"value1\"}}" }; + yield return new object[] { "[{ \"test.test1\":\"value\" }]", "[{\"test\":{\"test1\":\"value1\"}}]" }; + } } } \ No newline at end of file diff --git a/test/WireMock.Net.Tests/Matchers/JsonPartialMatcherTests.cs b/test/WireMock.Net.Tests/Matchers/JsonPartialMatcherTests.cs new file mode 100644 index 000000000..639994fa9 --- /dev/null +++ b/test/WireMock.Net.Tests/Matchers/JsonPartialMatcherTests.cs @@ -0,0 +1,299 @@ +using System; +using System.IO; +using FluentAssertions; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using NFluent; +using WireMock.Matchers; +using Xunit; + +namespace WireMock.Net.Tests.Matchers +{ + public class JsonMatcherTests + { + [Fact] + public void JsonMatcher_GetName() + { + // Assign + var matcher = new JsonMatcher("{}"); + + // Act + string name = matcher.Name; + + // Assert + Check.That(name).Equals("JsonMatcher"); + } + + [Fact] + public void JsonMatcher_GetValue() + { + // Assign + var matcher = new JsonMatcher("{}"); + + // Act + object value = matcher.Value; + + // Assert + Check.That(value).Equals("{}"); + } + + [Fact] + public void JsonMatcher_WithInvalidStringValue_Should_ThrowException() + { + // Act + Action action = () => new JsonMatcher(MatchBehaviour.AcceptOnMatch, "{ \"Id\""); + + // Assert + action.Should().Throw(); + } + + [Fact] + public void JsonMatcher_WithInvalidObjectValue_Should_ThrowException() + { + // Act + Action action = () => new JsonMatcher(MatchBehaviour.AcceptOnMatch, new MemoryStream()); + + // Assert + action.Should().Throw(); + } + + [Fact] + public void JsonMatcher_IsMatch_WithInvalidValue_And_ThrowExceptionIsFalse_Should_ReturnMismatch() + { + // Assign + var matcher = new JsonMatcher(""); + + // Act + double match = matcher.IsMatch(new MemoryStream()); + + // Assert + Check.That(match).IsEqualTo(0); + } + + [Fact] + public void JsonMatcher_IsMatch_WithInvalidValue_And_ThrowExceptionIsTrue_Should_ReturnMismatch() + { + // Assign + var matcher = new JsonMatcher("", false, true); + + // Act + Action action = () => matcher.IsMatch(new MemoryStream()); + + // Assert + action.Should().Throw(); + } + + [Fact] + public void JsonMatcher_IsMatch_ByteArray() + { + // Assign + var bytes = new byte[0]; + var matcher = new JsonMatcher(""); + + // Act + double match = matcher.IsMatch(bytes); + + // Assert + Check.That(match).IsEqualTo(0); + } + + [Fact] + public void JsonMatcher_IsMatch_NullString() + { + // Assign + string s = null; + var matcher = new JsonMatcher(""); + + // Act + double match = matcher.IsMatch(s); + + // Assert + Check.That(match).IsEqualTo(0); + } + + [Fact] + public void JsonMatcher_IsMatch_NullObject() + { + // Assign + object o = null; + var matcher = new JsonMatcher(""); + + // Act + double match = matcher.IsMatch(o); + + // Assert + Check.That(match).IsEqualTo(0); + } + + [Fact] + public void JsonMatcher_IsMatch_JArray() + { + // Assign + var matcher = new JsonMatcher(new[] { "x", "y" }); + + // Act + var jArray = new JArray + { + "x", + "y" + }; + double match = matcher.IsMatch(jArray); + + // Assert + Assert.Equal(1.0, match); + } + + [Fact] + public void JsonMatcher_IsMatch_JObject() + { + // Assign + var matcher = new JsonMatcher(new { Id = 1, Name = "Test" }); + + // Act + var jobject = new JObject + { + { "Id", new JValue(1) }, + { "Name", new JValue("Test") } + }; + double match = matcher.IsMatch(jobject); + + // Assert + Assert.Equal(1.0, match); + } + + [Fact] + public void JsonMatcher_IsMatch_WithIgnoreCaseTrue_JObject() + { + // Assign + var matcher = new JsonMatcher(new { id = 1, Name = "test" }, true); + + // Act + var jobject = new JObject + { + { "Id", new JValue(1) }, + { "NaMe", new JValue("Test") } + }; + double match = matcher.IsMatch(jobject); + + // Assert + Assert.Equal(1.0, match); + } + + [Fact] + public void JsonMatcher_IsMatch_JObjectParsed() + { + // Assign + var matcher = new JsonMatcher(new { Id = 1, Name = "Test" }); + + // Act + var jobject = JObject.Parse("{ \"Id\" : 1, \"Name\" : \"Test\" }"); + double match = matcher.IsMatch(jobject); + + // Assert + Assert.Equal(1.0, match); + } + + [Fact] + public void JsonMatcher_IsMatch_WithIgnoreCaseTrue_JObjectParsed() + { + // Assign + var matcher = new JsonMatcher(new { Id = 1, Name = "TESt" }, true); + + // Act + var jobject = JObject.Parse("{ \"Id\" : 1, \"Name\" : \"Test\" }"); + double match = matcher.IsMatch(jobject); + + // Assert + Assert.Equal(1.0, match); + } + + [Fact] + public void JsonMatcher_IsMatch_JArrayAsString() + { + // Assign + var matcher = new JsonMatcher("[ \"x\", \"y\" ]"); + + // Act + var jArray = new JArray + { + "x", + "y" + }; + double match = matcher.IsMatch(jArray); + + // Assert + Assert.Equal(1.0, match); + } + + [Fact] + public void JsonMatcher_IsMatch_JObjectAsString() + { + // Assign + var matcher = new JsonMatcher("{ \"Id\" : 1, \"Name\" : \"Test\" }"); + + // Act + var jobject = new JObject + { + { "Id", new JValue(1) }, + { "Name", new JValue("Test") } + }; + double match = matcher.IsMatch(jobject); + + // Assert + Assert.Equal(1.0, match); + } + + [Fact] + public void JsonMatcher_IsMatch_WithIgnoreCaseTrue_JObjectAsString() + { + // Assign + var matcher = new JsonMatcher("{ \"Id\" : 1, \"Name\" : \"test\" }", true); + + // Act + var jobject = new JObject + { + { "Id", new JValue(1) }, + { "Name", new JValue("Test") } + }; + double match = matcher.IsMatch(jobject); + + // Assert + Assert.Equal(1.0, match); + } + + [Fact] + public void JsonMatcher_IsMatch_JObjectAsString_RejectOnMatch() + { + // Assign + var matcher = new JsonMatcher(MatchBehaviour.RejectOnMatch, "{ \"Id\" : 1, \"Name\" : \"Test\" }"); + + // Act + var jobject = new JObject + { + { "Id", new JValue(1) }, + { "Name", new JValue("Test") } + }; + double match = matcher.IsMatch(jobject); + + // Assert + Assert.Equal(0.0, match); + } + + [Fact] + public void JsonMatcher_IsMatch_JObjectWithDateTimeOffsetAsString() + { + // Assign + var matcher = new JsonMatcher("{ \"preferredAt\" : \"2019-11-21T10:32:53.2210009+00:00\" }"); + + // Act + var jobject = new JObject + { + { "preferredAt", new JValue("2019-11-21T10:32:53.2210009+00:00") } + }; + double match = matcher.IsMatch(jobject); + + // Assert + Assert.Equal(1.0, match); + } + } +} \ No newline at end of file From f7edfa01ddf371af2417b42746bd0e03f71082c2 Mon Sep 17 00:00:00 2001 From: Gleb Osokin Date: Mon, 16 Nov 2020 12:09:55 +0100 Subject: [PATCH 02/10] fix erroneous filenames --- .../Matchers/JsonMatcherTests.cs | 137 +++++------------- .../Matchers/JsonPartialMatcherTests.cs | 137 +++++++++++++----- 2 files changed, 137 insertions(+), 137 deletions(-) diff --git a/test/WireMock.Net.Tests/Matchers/JsonMatcherTests.cs b/test/WireMock.Net.Tests/Matchers/JsonMatcherTests.cs index eb31f74ae..639994fa9 100644 --- a/test/WireMock.Net.Tests/Matchers/JsonMatcherTests.cs +++ b/test/WireMock.Net.Tests/Matchers/JsonMatcherTests.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.IO; using FluentAssertions; using Newtonsoft.Json; @@ -10,26 +9,26 @@ namespace WireMock.Net.Tests.Matchers { - public class JsonPartialMatcherTests + public class JsonMatcherTests { [Fact] - public void JsonPartialMatcher_GetName() + public void JsonMatcher_GetName() { // Assign - var matcher = new JsonPartialMatcher("{}"); + var matcher = new JsonMatcher("{}"); // Act string name = matcher.Name; // Assert - Check.That(name).Equals("JsonPartialMatcher"); + Check.That(name).Equals("JsonMatcher"); } [Fact] - public void JsonPartialMatcher_GetValue() + public void JsonMatcher_GetValue() { // Assign - var matcher = new JsonPartialMatcher("{}"); + var matcher = new JsonMatcher("{}"); // Act object value = matcher.Value; @@ -39,30 +38,30 @@ public void JsonPartialMatcher_GetValue() } [Fact] - public void JsonPartialMatcher_WithInvalidStringValue_Should_ThrowException() + public void JsonMatcher_WithInvalidStringValue_Should_ThrowException() { // Act - Action action = () => new JsonPartialMatcher(MatchBehaviour.AcceptOnMatch, "{ \"Id\""); + Action action = () => new JsonMatcher(MatchBehaviour.AcceptOnMatch, "{ \"Id\""); // Assert action.Should().Throw(); } [Fact] - public void JsonPartialMatcher_WithInvalidObjectValue_Should_ThrowException() + public void JsonMatcher_WithInvalidObjectValue_Should_ThrowException() { // Act - Action action = () => new JsonPartialMatcher(MatchBehaviour.AcceptOnMatch, new MemoryStream()); + Action action = () => new JsonMatcher(MatchBehaviour.AcceptOnMatch, new MemoryStream()); // Assert action.Should().Throw(); } [Fact] - public void JsonPartialMatcher_IsMatch_WithInvalidValue_And_ThrowExceptionIsFalse_Should_ReturnMismatch() + public void JsonMatcher_IsMatch_WithInvalidValue_And_ThrowExceptionIsFalse_Should_ReturnMismatch() { // Assign - var matcher = new JsonPartialMatcher(""); + var matcher = new JsonMatcher(""); // Act double match = matcher.IsMatch(new MemoryStream()); @@ -72,10 +71,10 @@ public void JsonPartialMatcher_IsMatch_WithInvalidValue_And_ThrowExceptionIsFals } [Fact] - public void JsonPartialMatcher_IsMatch_WithInvalidValue_And_ThrowExceptionIsTrue_Should_ReturnMismatch() + public void JsonMatcher_IsMatch_WithInvalidValue_And_ThrowExceptionIsTrue_Should_ReturnMismatch() { // Assign - var matcher = new JsonPartialMatcher("", false, true); + var matcher = new JsonMatcher("", false, true); // Act Action action = () => matcher.IsMatch(new MemoryStream()); @@ -85,11 +84,11 @@ public void JsonPartialMatcher_IsMatch_WithInvalidValue_And_ThrowExceptionIsTrue } [Fact] - public void JsonPartialMatcher_IsMatch_ByteArray() + public void JsonMatcher_IsMatch_ByteArray() { // Assign var bytes = new byte[0]; - var matcher = new JsonPartialMatcher(""); + var matcher = new JsonMatcher(""); // Act double match = matcher.IsMatch(bytes); @@ -99,11 +98,11 @@ public void JsonPartialMatcher_IsMatch_ByteArray() } [Fact] - public void JsonPartialMatcher_IsMatch_NullString() + public void JsonMatcher_IsMatch_NullString() { // Assign string s = null; - var matcher = new JsonPartialMatcher(""); + var matcher = new JsonMatcher(""); // Act double match = matcher.IsMatch(s); @@ -113,11 +112,11 @@ public void JsonPartialMatcher_IsMatch_NullString() } [Fact] - public void JsonPartialMatcher_IsMatch_NullObject() + public void JsonMatcher_IsMatch_NullObject() { // Assign object o = null; - var matcher = new JsonPartialMatcher(""); + var matcher = new JsonMatcher(""); // Act double match = matcher.IsMatch(o); @@ -127,10 +126,10 @@ public void JsonPartialMatcher_IsMatch_NullObject() } [Fact] - public void JsonPartialMatcher_IsMatch_JArray() + public void JsonMatcher_IsMatch_JArray() { // Assign - var matcher = new JsonPartialMatcher(new[] { "x", "y" }); + var matcher = new JsonMatcher(new[] { "x", "y" }); // Act var jArray = new JArray @@ -145,10 +144,10 @@ public void JsonPartialMatcher_IsMatch_JArray() } [Fact] - public void JsonPartialMatcher_IsMatch_JObject() + public void JsonMatcher_IsMatch_JObject() { // Assign - var matcher = new JsonPartialMatcher(new { Id = 1, Name = "Test" }); + var matcher = new JsonMatcher(new { Id = 1, Name = "Test" }); // Act var jobject = new JObject @@ -163,10 +162,10 @@ public void JsonPartialMatcher_IsMatch_JObject() } [Fact] - public void JsonPartialMatcher_IsMatch_WithIgnoreCaseTrue_JObject() + public void JsonMatcher_IsMatch_WithIgnoreCaseTrue_JObject() { // Assign - var matcher = new JsonPartialMatcher(new { id = 1, Name = "test" }, true); + var matcher = new JsonMatcher(new { id = 1, Name = "test" }, true); // Act var jobject = new JObject @@ -181,10 +180,10 @@ public void JsonPartialMatcher_IsMatch_WithIgnoreCaseTrue_JObject() } [Fact] - public void JsonPartialMatcher_IsMatch_JObjectParsed() + public void JsonMatcher_IsMatch_JObjectParsed() { // Assign - var matcher = new JsonPartialMatcher(new { Id = 1, Name = "Test" }); + var matcher = new JsonMatcher(new { Id = 1, Name = "Test" }); // Act var jobject = JObject.Parse("{ \"Id\" : 1, \"Name\" : \"Test\" }"); @@ -195,10 +194,10 @@ public void JsonPartialMatcher_IsMatch_JObjectParsed() } [Fact] - public void JsonPartialMatcher_IsMatch_WithIgnoreCaseTrue_JObjectParsed() + public void JsonMatcher_IsMatch_WithIgnoreCaseTrue_JObjectParsed() { // Assign - var matcher = new JsonPartialMatcher(new { Id = 1, Name = "TESt" }, true); + var matcher = new JsonMatcher(new { Id = 1, Name = "TESt" }, true); // Act var jobject = JObject.Parse("{ \"Id\" : 1, \"Name\" : \"Test\" }"); @@ -209,10 +208,10 @@ public void JsonPartialMatcher_IsMatch_WithIgnoreCaseTrue_JObjectParsed() } [Fact] - public void JsonPartialMatcher_IsMatch_JArrayAsString() + public void JsonMatcher_IsMatch_JArrayAsString() { // Assign - var matcher = new JsonPartialMatcher("[ \"x\", \"y\" ]"); + var matcher = new JsonMatcher("[ \"x\", \"y\" ]"); // Act var jArray = new JArray @@ -227,10 +226,10 @@ public void JsonPartialMatcher_IsMatch_JArrayAsString() } [Fact] - public void JsonPartialMatcher_IsMatch_JObjectAsString() + public void JsonMatcher_IsMatch_JObjectAsString() { // Assign - var matcher = new JsonPartialMatcher("{ \"Id\" : 1, \"Name\" : \"Test\" }"); + var matcher = new JsonMatcher("{ \"Id\" : 1, \"Name\" : \"Test\" }"); // Act var jobject = new JObject @@ -245,10 +244,10 @@ public void JsonPartialMatcher_IsMatch_JObjectAsString() } [Fact] - public void JsonPartialMatcher_IsMatch_WithIgnoreCaseTrue_JObjectAsString() + public void JsonMatcher_IsMatch_WithIgnoreCaseTrue_JObjectAsString() { // Assign - var matcher = new JsonPartialMatcher("{ \"Id\" : 1, \"Name\" : \"test\" }", true); + var matcher = new JsonMatcher("{ \"Id\" : 1, \"Name\" : \"test\" }", true); // Act var jobject = new JObject @@ -263,10 +262,10 @@ public void JsonPartialMatcher_IsMatch_WithIgnoreCaseTrue_JObjectAsString() } [Fact] - public void JsonPartialMatcher_IsMatch_JObjectAsString_RejectOnMatch() + public void JsonMatcher_IsMatch_JObjectAsString_RejectOnMatch() { // Assign - var matcher = new JsonPartialMatcher(MatchBehaviour.RejectOnMatch, "{ \"Id\" : 1, \"Name\" : \"Test\" }"); + var matcher = new JsonMatcher(MatchBehaviour.RejectOnMatch, "{ \"Id\" : 1, \"Name\" : \"Test\" }"); // Act var jobject = new JObject @@ -281,10 +280,10 @@ public void JsonPartialMatcher_IsMatch_JObjectAsString_RejectOnMatch() } [Fact] - public void JsonPartialMatcher_IsMatch_JObjectWithDateTimeOffsetAsString() + public void JsonMatcher_IsMatch_JObjectWithDateTimeOffsetAsString() { // Assign - var matcher = new JsonPartialMatcher("{ \"preferredAt\" : \"2019-11-21T10:32:53.2210009+00:00\" }"); + var matcher = new JsonMatcher("{ \"preferredAt\" : \"2019-11-21T10:32:53.2210009+00:00\" }"); // Act var jobject = new JObject @@ -296,61 +295,5 @@ public void JsonPartialMatcher_IsMatch_JObjectWithDateTimeOffsetAsString() // Assert Assert.Equal(1.0, match); } - - [Theory] - [MemberData(nameof(ValidMatches))] - public void JsonPartialMatcher_IsMatch_StringInputValidMatch(string value, string input) - { - // Assign - var matcher = new JsonPartialMatcher(value); - - // Act - double match = matcher.IsMatch(input); - - // Assert - Assert.Equal(1.0, match); - } - - [Theory] - [MemberData(nameof(InvalidMatches))] - public void JsonPartialMatcher_IsMatch_StringInputWithInvalidMatch(string value, string input) - { - // Assign - var matcher = new JsonPartialMatcher(value); - - // Act - double match = matcher.IsMatch(input); - - // Assert - Assert.Equal(0.0, match); - } - - public static IEnumerable ValidMatches() - { - yield return new object[] { "{}", "\"test\"" }; - yield return new object[] { "\"test\"", "\"test\"" }; - yield return new object[] { "123", "123" }; - yield return new object[] { "[\"test\"]", "[\"test\"]" }; - yield return new object[] { "[\"test\"]", "[\"test\", \"test1\"]" }; - yield return new object[] { "[123]", "[123]" }; - yield return new object[] { "{ \"test\":\"value\" }", "{\"test\":\"value\"}" }; - yield return new object[] { "{ \"test.test1\":\"value\" }", "{\"test\":{\"test1\":\"value\"}}" }; - yield return new object[] { "{\"test\":{\"test1\":\"value\"}}", "{\"test\":{\"test1\":\"value\"}}" }; - yield return new object[] { "[{ \"test.test1\":\"value\" }]", "[{\"test\":{\"test1\":\"value\"}}]" }; - } - - public static IEnumerable InvalidMatches() - { - yield return new object[] { "\"test\"", null }; - yield return new object[] { "\"test1\"", "\"test2\"" }; - yield return new object[] { "123", "1234" }; - yield return new object[] { "[\"test\"]", "[\"test1\"]" }; - yield return new object[] { "[\"test\"]", "[\"test1\", \"test2\"]" }; - yield return new object[] { "[123]", "[1234]" }; - yield return new object[] { "{ \"test\":\"value\" }", "{\"test\":\"value2\"}" }; - yield return new object[] { "{ \"test.test1\":\"value\" }", "{\"test\":{\"test1\":\"value1\"}}" }; - yield return new object[] { "{\"test\":{\"test1\":\"value\"}}", "{\"test\":{\"test1\":\"value1\"}}" }; - yield return new object[] { "[{ \"test.test1\":\"value\" }]", "[{\"test\":{\"test1\":\"value1\"}}]" }; - } } } \ No newline at end of file diff --git a/test/WireMock.Net.Tests/Matchers/JsonPartialMatcherTests.cs b/test/WireMock.Net.Tests/Matchers/JsonPartialMatcherTests.cs index 639994fa9..eb31f74ae 100644 --- a/test/WireMock.Net.Tests/Matchers/JsonPartialMatcherTests.cs +++ b/test/WireMock.Net.Tests/Matchers/JsonPartialMatcherTests.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.IO; using FluentAssertions; using Newtonsoft.Json; @@ -9,26 +10,26 @@ namespace WireMock.Net.Tests.Matchers { - public class JsonMatcherTests + public class JsonPartialMatcherTests { [Fact] - public void JsonMatcher_GetName() + public void JsonPartialMatcher_GetName() { // Assign - var matcher = new JsonMatcher("{}"); + var matcher = new JsonPartialMatcher("{}"); // Act string name = matcher.Name; // Assert - Check.That(name).Equals("JsonMatcher"); + Check.That(name).Equals("JsonPartialMatcher"); } [Fact] - public void JsonMatcher_GetValue() + public void JsonPartialMatcher_GetValue() { // Assign - var matcher = new JsonMatcher("{}"); + var matcher = new JsonPartialMatcher("{}"); // Act object value = matcher.Value; @@ -38,30 +39,30 @@ public void JsonMatcher_GetValue() } [Fact] - public void JsonMatcher_WithInvalidStringValue_Should_ThrowException() + public void JsonPartialMatcher_WithInvalidStringValue_Should_ThrowException() { // Act - Action action = () => new JsonMatcher(MatchBehaviour.AcceptOnMatch, "{ \"Id\""); + Action action = () => new JsonPartialMatcher(MatchBehaviour.AcceptOnMatch, "{ \"Id\""); // Assert action.Should().Throw(); } [Fact] - public void JsonMatcher_WithInvalidObjectValue_Should_ThrowException() + public void JsonPartialMatcher_WithInvalidObjectValue_Should_ThrowException() { // Act - Action action = () => new JsonMatcher(MatchBehaviour.AcceptOnMatch, new MemoryStream()); + Action action = () => new JsonPartialMatcher(MatchBehaviour.AcceptOnMatch, new MemoryStream()); // Assert action.Should().Throw(); } [Fact] - public void JsonMatcher_IsMatch_WithInvalidValue_And_ThrowExceptionIsFalse_Should_ReturnMismatch() + public void JsonPartialMatcher_IsMatch_WithInvalidValue_And_ThrowExceptionIsFalse_Should_ReturnMismatch() { // Assign - var matcher = new JsonMatcher(""); + var matcher = new JsonPartialMatcher(""); // Act double match = matcher.IsMatch(new MemoryStream()); @@ -71,10 +72,10 @@ public void JsonMatcher_IsMatch_WithInvalidValue_And_ThrowExceptionIsFalse_Shoul } [Fact] - public void JsonMatcher_IsMatch_WithInvalidValue_And_ThrowExceptionIsTrue_Should_ReturnMismatch() + public void JsonPartialMatcher_IsMatch_WithInvalidValue_And_ThrowExceptionIsTrue_Should_ReturnMismatch() { // Assign - var matcher = new JsonMatcher("", false, true); + var matcher = new JsonPartialMatcher("", false, true); // Act Action action = () => matcher.IsMatch(new MemoryStream()); @@ -84,11 +85,11 @@ public void JsonMatcher_IsMatch_WithInvalidValue_And_ThrowExceptionIsTrue_Should } [Fact] - public void JsonMatcher_IsMatch_ByteArray() + public void JsonPartialMatcher_IsMatch_ByteArray() { // Assign var bytes = new byte[0]; - var matcher = new JsonMatcher(""); + var matcher = new JsonPartialMatcher(""); // Act double match = matcher.IsMatch(bytes); @@ -98,11 +99,11 @@ public void JsonMatcher_IsMatch_ByteArray() } [Fact] - public void JsonMatcher_IsMatch_NullString() + public void JsonPartialMatcher_IsMatch_NullString() { // Assign string s = null; - var matcher = new JsonMatcher(""); + var matcher = new JsonPartialMatcher(""); // Act double match = matcher.IsMatch(s); @@ -112,11 +113,11 @@ public void JsonMatcher_IsMatch_NullString() } [Fact] - public void JsonMatcher_IsMatch_NullObject() + public void JsonPartialMatcher_IsMatch_NullObject() { // Assign object o = null; - var matcher = new JsonMatcher(""); + var matcher = new JsonPartialMatcher(""); // Act double match = matcher.IsMatch(o); @@ -126,10 +127,10 @@ public void JsonMatcher_IsMatch_NullObject() } [Fact] - public void JsonMatcher_IsMatch_JArray() + public void JsonPartialMatcher_IsMatch_JArray() { // Assign - var matcher = new JsonMatcher(new[] { "x", "y" }); + var matcher = new JsonPartialMatcher(new[] { "x", "y" }); // Act var jArray = new JArray @@ -144,10 +145,10 @@ public void JsonMatcher_IsMatch_JArray() } [Fact] - public void JsonMatcher_IsMatch_JObject() + public void JsonPartialMatcher_IsMatch_JObject() { // Assign - var matcher = new JsonMatcher(new { Id = 1, Name = "Test" }); + var matcher = new JsonPartialMatcher(new { Id = 1, Name = "Test" }); // Act var jobject = new JObject @@ -162,10 +163,10 @@ public void JsonMatcher_IsMatch_JObject() } [Fact] - public void JsonMatcher_IsMatch_WithIgnoreCaseTrue_JObject() + public void JsonPartialMatcher_IsMatch_WithIgnoreCaseTrue_JObject() { // Assign - var matcher = new JsonMatcher(new { id = 1, Name = "test" }, true); + var matcher = new JsonPartialMatcher(new { id = 1, Name = "test" }, true); // Act var jobject = new JObject @@ -180,10 +181,10 @@ public void JsonMatcher_IsMatch_WithIgnoreCaseTrue_JObject() } [Fact] - public void JsonMatcher_IsMatch_JObjectParsed() + public void JsonPartialMatcher_IsMatch_JObjectParsed() { // Assign - var matcher = new JsonMatcher(new { Id = 1, Name = "Test" }); + var matcher = new JsonPartialMatcher(new { Id = 1, Name = "Test" }); // Act var jobject = JObject.Parse("{ \"Id\" : 1, \"Name\" : \"Test\" }"); @@ -194,10 +195,10 @@ public void JsonMatcher_IsMatch_JObjectParsed() } [Fact] - public void JsonMatcher_IsMatch_WithIgnoreCaseTrue_JObjectParsed() + public void JsonPartialMatcher_IsMatch_WithIgnoreCaseTrue_JObjectParsed() { // Assign - var matcher = new JsonMatcher(new { Id = 1, Name = "TESt" }, true); + var matcher = new JsonPartialMatcher(new { Id = 1, Name = "TESt" }, true); // Act var jobject = JObject.Parse("{ \"Id\" : 1, \"Name\" : \"Test\" }"); @@ -208,10 +209,10 @@ public void JsonMatcher_IsMatch_WithIgnoreCaseTrue_JObjectParsed() } [Fact] - public void JsonMatcher_IsMatch_JArrayAsString() + public void JsonPartialMatcher_IsMatch_JArrayAsString() { // Assign - var matcher = new JsonMatcher("[ \"x\", \"y\" ]"); + var matcher = new JsonPartialMatcher("[ \"x\", \"y\" ]"); // Act var jArray = new JArray @@ -226,10 +227,10 @@ public void JsonMatcher_IsMatch_JArrayAsString() } [Fact] - public void JsonMatcher_IsMatch_JObjectAsString() + public void JsonPartialMatcher_IsMatch_JObjectAsString() { // Assign - var matcher = new JsonMatcher("{ \"Id\" : 1, \"Name\" : \"Test\" }"); + var matcher = new JsonPartialMatcher("{ \"Id\" : 1, \"Name\" : \"Test\" }"); // Act var jobject = new JObject @@ -244,10 +245,10 @@ public void JsonMatcher_IsMatch_JObjectAsString() } [Fact] - public void JsonMatcher_IsMatch_WithIgnoreCaseTrue_JObjectAsString() + public void JsonPartialMatcher_IsMatch_WithIgnoreCaseTrue_JObjectAsString() { // Assign - var matcher = new JsonMatcher("{ \"Id\" : 1, \"Name\" : \"test\" }", true); + var matcher = new JsonPartialMatcher("{ \"Id\" : 1, \"Name\" : \"test\" }", true); // Act var jobject = new JObject @@ -262,10 +263,10 @@ public void JsonMatcher_IsMatch_WithIgnoreCaseTrue_JObjectAsString() } [Fact] - public void JsonMatcher_IsMatch_JObjectAsString_RejectOnMatch() + public void JsonPartialMatcher_IsMatch_JObjectAsString_RejectOnMatch() { // Assign - var matcher = new JsonMatcher(MatchBehaviour.RejectOnMatch, "{ \"Id\" : 1, \"Name\" : \"Test\" }"); + var matcher = new JsonPartialMatcher(MatchBehaviour.RejectOnMatch, "{ \"Id\" : 1, \"Name\" : \"Test\" }"); // Act var jobject = new JObject @@ -280,10 +281,10 @@ public void JsonMatcher_IsMatch_JObjectAsString_RejectOnMatch() } [Fact] - public void JsonMatcher_IsMatch_JObjectWithDateTimeOffsetAsString() + public void JsonPartialMatcher_IsMatch_JObjectWithDateTimeOffsetAsString() { // Assign - var matcher = new JsonMatcher("{ \"preferredAt\" : \"2019-11-21T10:32:53.2210009+00:00\" }"); + var matcher = new JsonPartialMatcher("{ \"preferredAt\" : \"2019-11-21T10:32:53.2210009+00:00\" }"); // Act var jobject = new JObject @@ -295,5 +296,61 @@ public void JsonMatcher_IsMatch_JObjectWithDateTimeOffsetAsString() // Assert Assert.Equal(1.0, match); } + + [Theory] + [MemberData(nameof(ValidMatches))] + public void JsonPartialMatcher_IsMatch_StringInputValidMatch(string value, string input) + { + // Assign + var matcher = new JsonPartialMatcher(value); + + // Act + double match = matcher.IsMatch(input); + + // Assert + Assert.Equal(1.0, match); + } + + [Theory] + [MemberData(nameof(InvalidMatches))] + public void JsonPartialMatcher_IsMatch_StringInputWithInvalidMatch(string value, string input) + { + // Assign + var matcher = new JsonPartialMatcher(value); + + // Act + double match = matcher.IsMatch(input); + + // Assert + Assert.Equal(0.0, match); + } + + public static IEnumerable ValidMatches() + { + yield return new object[] { "{}", "\"test\"" }; + yield return new object[] { "\"test\"", "\"test\"" }; + yield return new object[] { "123", "123" }; + yield return new object[] { "[\"test\"]", "[\"test\"]" }; + yield return new object[] { "[\"test\"]", "[\"test\", \"test1\"]" }; + yield return new object[] { "[123]", "[123]" }; + yield return new object[] { "{ \"test\":\"value\" }", "{\"test\":\"value\"}" }; + yield return new object[] { "{ \"test.test1\":\"value\" }", "{\"test\":{\"test1\":\"value\"}}" }; + yield return new object[] { "{\"test\":{\"test1\":\"value\"}}", "{\"test\":{\"test1\":\"value\"}}" }; + yield return new object[] { "[{ \"test.test1\":\"value\" }]", "[{\"test\":{\"test1\":\"value\"}}]" }; + } + + public static IEnumerable InvalidMatches() + { + yield return new object[] { "\"test\"", null }; + yield return new object[] { "\"test1\"", "\"test2\"" }; + yield return new object[] { "123", "1234" }; + yield return new object[] { "[\"test\"]", "[\"test1\"]" }; + yield return new object[] { "[\"test\"]", "[\"test1\", \"test2\"]" }; + yield return new object[] { "[123]", "[1234]" }; + yield return new object[] { "{ \"test\":\"value\" }", "{\"test\":\"value2\"}" }; + yield return new object[] { "{ \"test.test1\":\"value\" }", "{\"test\":{\"test1\":\"value1\"}}" }; + yield return new object[] { "{\"test\":{\"test1\":\"value\"}}", "{\"test\":{\"test1\":\"value1\"}}" }; + yield return new object[] { "[{ \"test.test1\":\"value\" }]", "[{\"test\":{\"test1\":\"value1\"}}]" }; + } } } \ No newline at end of file From d3838d66099d8f6c4e5115814c55313a59177aeb Mon Sep 17 00:00:00 2001 From: Gleb Osokin Date: Mon, 16 Nov 2020 12:10:59 +0100 Subject: [PATCH 03/10] add newline --- test/WireMock.Net.Tests/Matchers/JsonPathMatcherTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/WireMock.Net.Tests/Matchers/JsonPathMatcherTests.cs b/test/WireMock.Net.Tests/Matchers/JsonPathMatcherTests.cs index 2eca285bf..e1cb1b0c1 100644 --- a/test/WireMock.Net.Tests/Matchers/JsonPathMatcherTests.cs +++ b/test/WireMock.Net.Tests/Matchers/JsonPathMatcherTests.cs @@ -159,4 +159,4 @@ public void JsonPathMatcher_IsMatch_RejectOnMatch() Check.That(match).IsEqualTo(0.0); } } -} \ No newline at end of file +} From b0efe080fa86582c6716d5e91f12f30ac93e0869 Mon Sep 17 00:00:00 2001 From: Gleb Osokin Date: Mon, 16 Nov 2020 13:02:44 +0100 Subject: [PATCH 04/10] newlines fix --- test/WireMock.Net.Tests/Matchers/JsonPartialMatcherTests.cs | 2 +- test/WireMock.Net.Tests/Matchers/JsonPathMatcherTests.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/WireMock.Net.Tests/Matchers/JsonPartialMatcherTests.cs b/test/WireMock.Net.Tests/Matchers/JsonPartialMatcherTests.cs index eb31f74ae..8b13bae45 100644 --- a/test/WireMock.Net.Tests/Matchers/JsonPartialMatcherTests.cs +++ b/test/WireMock.Net.Tests/Matchers/JsonPartialMatcherTests.cs @@ -353,4 +353,4 @@ public static IEnumerable InvalidMatches() yield return new object[] { "[{ \"test.test1\":\"value\" }]", "[{\"test\":{\"test1\":\"value1\"}}]" }; } } -} \ No newline at end of file +} diff --git a/test/WireMock.Net.Tests/Matchers/JsonPathMatcherTests.cs b/test/WireMock.Net.Tests/Matchers/JsonPathMatcherTests.cs index e1cb1b0c1..2eca285bf 100644 --- a/test/WireMock.Net.Tests/Matchers/JsonPathMatcherTests.cs +++ b/test/WireMock.Net.Tests/Matchers/JsonPathMatcherTests.cs @@ -159,4 +159,4 @@ public void JsonPathMatcher_IsMatch_RejectOnMatch() Check.That(match).IsEqualTo(0.0); } } -} +} \ No newline at end of file From 4af601e17b48af387f1ad47f12dd7e359e28e947 Mon Sep 17 00:00:00 2001 From: Gleb Osokin Date: Mon, 16 Nov 2020 16:43:43 +0100 Subject: [PATCH 05/10] add JsonPartialMatcher to mapper --- .../Serialization/MatcherMapper.cs | 4 + .../Serialization/MatcherMapperTests.cs | 82 ++++++++++++++++++- 2 files changed, 85 insertions(+), 1 deletion(-) diff --git a/src/WireMock.Net/Serialization/MatcherMapper.cs b/src/WireMock.Net/Serialization/MatcherMapper.cs index 9a0c9bcbb..3021401cb 100644 --- a/src/WireMock.Net/Serialization/MatcherMapper.cs +++ b/src/WireMock.Net/Serialization/MatcherMapper.cs @@ -67,6 +67,10 @@ public IMatcher Map([CanBeNull] MatcherModel matcher) object value = matcher.Pattern ?? matcher.Patterns; return new JsonMatcher(matchBehaviour, value, ignoreCase, throwExceptionWhenMatcherFails); + case "JsonPartialMatcher": + object matcherValue = matcher.Pattern ?? matcher.Patterns; + return new JsonPartialMatcher(matchBehaviour, matcherValue, ignoreCase, throwExceptionWhenMatcherFails); + case "JsonPathMatcher": return new JsonPathMatcher(matchBehaviour, throwExceptionWhenMatcherFails, stringPatterns); diff --git a/test/WireMock.Net.Tests/Serialization/MatcherMapperTests.cs b/test/WireMock.Net.Tests/Serialization/MatcherMapperTests.cs index 14dd05544..d1d9f0490 100644 --- a/test/WireMock.Net.Tests/Serialization/MatcherMapperTests.cs +++ b/test/WireMock.Net.Tests/Serialization/MatcherMapperTests.cs @@ -221,5 +221,85 @@ public void MatcherMapper_Map_MatcherModel_JsonMatcher_Patterns_As_Object() matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch); matcher.Value.Should().BeEquivalentTo(patterns); } + + [Fact] + public void MatcherMapper_Map_MatcherModel_JsonPartialMatcher_Pattern_As_String() + { + // Assign + var pattern = "{ \"AccountIds\": [ 1, 2, 3 ] }"; + var model = new MatcherModel + { + Name = "JsonPartialMatcher", + Pattern = pattern + }; + + // Act + var matcher = (JsonPartialMatcher)_sut.Map(model); + + // Assert + matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch); + matcher.Value.Should().BeEquivalentTo(pattern); + } + + [Fact] + public void MatcherMapper_Map_MatcherModel_JsonPartialMatcher_Patterns_As_String() + { + // Assign + var pattern1 = "{ \"AccountIds\": [ 1, 2, 3 ] }"; + var pattern2 = "{ \"X\": \"x\" }"; + var patterns = new[] { pattern1, pattern2 }; + var model = new MatcherModel + { + Name = "JsonPartialMatcher", + Pattern = patterns + }; + + // Act + var matcher = (JsonPartialMatcher)_sut.Map(model); + + // Assert + matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch); + matcher.Value.Should().BeEquivalentTo(patterns); + } + + [Fact] + public void MatcherMapper_Map_MatcherModel_JsonPartialMatcher_Pattern_As_Object() + { + // Assign + var pattern = new { AccountIds = new[] { 1, 2, 3 } }; + var model = new MatcherModel + { + Name = "JsonPartialMatcher", + Pattern = pattern + }; + + // Act + var matcher = (JsonPartialMatcher)_sut.Map(model); + + // Assert + matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch); + matcher.Value.Should().BeEquivalentTo(pattern); + } + + [Fact] + public void MatcherMapper_Map_MatcherModel_JsonPartialMatcher_Patterns_As_Object() + { + // Assign + object pattern1 = new { AccountIds = new[] { 1, 2, 3 } }; + object pattern2 = new { X = "x" }; + var patterns = new[] { pattern1, pattern2 }; + var model = new MatcherModel + { + Name = "JsonPartialMatcher", + Patterns = patterns + }; + + // Act + var matcher = (JsonMatcher)_sut.Map(model); + + // Assert + matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch); + matcher.Value.Should().BeEquivalentTo(patterns); + } } -} \ No newline at end of file +} From 9378d4d8a5113b78650551bcfe9d24f13ca633b1 Mon Sep 17 00:00:00 2001 From: Gleb Osokin Date: Tue, 17 Nov 2020 08:35:19 +0100 Subject: [PATCH 06/10] curly braces for ifs --- src/WireMock.Net/Matchers/JsonPartialMatcher.cs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/WireMock.Net/Matchers/JsonPartialMatcher.cs b/src/WireMock.Net/Matchers/JsonPartialMatcher.cs index 718271373..5722675a8 100644 --- a/src/WireMock.Net/Matchers/JsonPartialMatcher.cs +++ b/src/WireMock.Net/Matchers/JsonPartialMatcher.cs @@ -56,10 +56,14 @@ protected override bool AreEqual(JToken value, JToken input) private static bool IsMatch(JToken value, JToken token) { if (value == null || value == token) + { return true; + } if (token == null) + { return false; + } switch (value.Type) { @@ -69,14 +73,16 @@ private static bool IsMatch(JToken value, JToken token) nestedValues.All(pair => IsMatch(pair.Value, token.SelectToken(pair.Key))); case JTokenType.Array: - var filtersArray = value.ToObject(); + var valuesArray = value.ToObject(); var tokenArray = token.ToObject(); - if (filtersArray?.Any() != true) + if (valuesArray?.Any() != true) + { return true; + } return tokenArray?.Any() == true && - filtersArray.All(subFilter => tokenArray.Any(subToken => IsMatch(subFilter, subToken))); + valuesArray.All(subFilter => tokenArray.Any(subToken => IsMatch(subFilter, subToken))); default: return value.ToString() == token.ToString(); From 1c109bd0e0b56eaf86645e9d354949440cdb0e59 Mon Sep 17 00:00:00 2001 From: Gleb Osokin Date: Tue, 17 Nov 2020 09:12:16 +0100 Subject: [PATCH 07/10] fix JToken type comparison --- src/WireMock.Net/Matchers/JsonPartialMatcher.cs | 2 +- test/WireMock.Net.Tests/Matchers/JsonPartialMatcherTests.cs | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/WireMock.Net/Matchers/JsonPartialMatcher.cs b/src/WireMock.Net/Matchers/JsonPartialMatcher.cs index 5722675a8..0666b79f6 100644 --- a/src/WireMock.Net/Matchers/JsonPartialMatcher.cs +++ b/src/WireMock.Net/Matchers/JsonPartialMatcher.cs @@ -60,7 +60,7 @@ private static bool IsMatch(JToken value, JToken token) return true; } - if (token == null) + if (token == null || value.Type != token.Type) { return false; } diff --git a/test/WireMock.Net.Tests/Matchers/JsonPartialMatcherTests.cs b/test/WireMock.Net.Tests/Matchers/JsonPartialMatcherTests.cs index 8b13bae45..15ba6b57e 100644 --- a/test/WireMock.Net.Tests/Matchers/JsonPartialMatcherTests.cs +++ b/test/WireMock.Net.Tests/Matchers/JsonPartialMatcherTests.cs @@ -327,12 +327,12 @@ public void JsonPartialMatcher_IsMatch_StringInputWithInvalidMatch(string value, public static IEnumerable ValidMatches() { - yield return new object[] { "{}", "\"test\"" }; yield return new object[] { "\"test\"", "\"test\"" }; yield return new object[] { "123", "123" }; yield return new object[] { "[\"test\"]", "[\"test\"]" }; yield return new object[] { "[\"test\"]", "[\"test\", \"test1\"]" }; yield return new object[] { "[123]", "[123]" }; + yield return new object[] { "{ \"test\":\"value\" }", "{\"test\":\"value\",\"test1\":123}" }; yield return new object[] { "{ \"test\":\"value\" }", "{\"test\":\"value\"}" }; yield return new object[] { "{ \"test.test1\":\"value\" }", "{\"test\":{\"test1\":\"value\"}}" }; yield return new object[] { "{\"test\":{\"test1\":\"value\"}}", "{\"test\":{\"test1\":\"value\"}}" }; @@ -347,6 +347,7 @@ public static IEnumerable InvalidMatches() yield return new object[] { "[\"test\"]", "[\"test1\"]" }; yield return new object[] { "[\"test\"]", "[\"test1\", \"test2\"]" }; yield return new object[] { "[123]", "[1234]" }; + yield return new object[] { "{}", "\"test\"" }; yield return new object[] { "{ \"test\":\"value\" }", "{\"test\":\"value2\"}" }; yield return new object[] { "{ \"test.test1\":\"value\" }", "{\"test\":{\"test1\":\"value1\"}}" }; yield return new object[] { "{\"test\":{\"test1\":\"value\"}}", "{\"test\":{\"test1\":\"value1\"}}" }; From 099368e0f9db2e9a8a03dba565e4c49e52707ab0 Mon Sep 17 00:00:00 2001 From: Gleb Osokin Date: Tue, 17 Nov 2020 11:42:35 +0100 Subject: [PATCH 08/10] more test cases --- .../Matchers/JsonPartialMatcherTests.cs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/test/WireMock.Net.Tests/Matchers/JsonPartialMatcherTests.cs b/test/WireMock.Net.Tests/Matchers/JsonPartialMatcherTests.cs index 15ba6b57e..3ab0ef417 100644 --- a/test/WireMock.Net.Tests/Matchers/JsonPartialMatcherTests.cs +++ b/test/WireMock.Net.Tests/Matchers/JsonPartialMatcherTests.cs @@ -327,16 +327,19 @@ public void JsonPartialMatcher_IsMatch_StringInputWithInvalidMatch(string value, public static IEnumerable ValidMatches() { + yield return new object[] { "{\"test\":\"abc\"}", "{\"test\":\"abc\",\"other\":\"xyz\"}" }; yield return new object[] { "\"test\"", "\"test\"" }; yield return new object[] { "123", "123" }; yield return new object[] { "[\"test\"]", "[\"test\"]" }; - yield return new object[] { "[\"test\"]", "[\"test\", \"test1\"]" }; + yield return new object[] { "[\"test\"]", "[\"test\", \"other\"]" }; yield return new object[] { "[123]", "[123]" }; - yield return new object[] { "{ \"test\":\"value\" }", "{\"test\":\"value\",\"test1\":123}" }; + yield return new object[] { "{ \"test\":\"value\" }", "{\"test\":\"value\",\"other\":123}" }; yield return new object[] { "{ \"test\":\"value\" }", "{\"test\":\"value\"}" }; - yield return new object[] { "{ \"test.test1\":\"value\" }", "{\"test\":{\"test1\":\"value\"}}" }; - yield return new object[] { "{\"test\":{\"test1\":\"value\"}}", "{\"test\":{\"test1\":\"value\"}}" }; - yield return new object[] { "[{ \"test.test1\":\"value\" }]", "[{\"test\":{\"test1\":\"value\"}}]" }; + yield return new object[] { "{ \"test.nested\":\"value\" }", "{\"test\":{\"nested\":\"value\"}}" }; + yield return new object[] { "{ \"['name.with.dot']\":\"value\" }", "{\"name.with.dot\":\"value\"}" }; + yield return new object[] { "{\"test\":{\"nested\":\"value\"}}", "{\"test\":{\"nested\":\"value\"}}" }; + yield return new object[] { "[{ \"test.nested\":\"value\" }]", "[{\"test\":{\"nested\":\"value\"}}]" }; + yield return new object[] { "[{ \"['name.with.dot']\":\"value\" }]", "[{\"name.with.dot\":\"value\"}]" }; } public static IEnumerable InvalidMatches() @@ -349,9 +352,9 @@ public static IEnumerable InvalidMatches() yield return new object[] { "[123]", "[1234]" }; yield return new object[] { "{}", "\"test\"" }; yield return new object[] { "{ \"test\":\"value\" }", "{\"test\":\"value2\"}" }; - yield return new object[] { "{ \"test.test1\":\"value\" }", "{\"test\":{\"test1\":\"value1\"}}" }; + yield return new object[] { "{ \"test.nested\":\"value\" }", "{\"test\":{\"nested\":\"value1\"}}" }; yield return new object[] { "{\"test\":{\"test1\":\"value\"}}", "{\"test\":{\"test1\":\"value1\"}}" }; - yield return new object[] { "[{ \"test.test1\":\"value\" }]", "[{\"test\":{\"test1\":\"value1\"}}]" }; + yield return new object[] { "[{ \"test.nested\":\"value\" }]", "[{\"test\":{\"nested\":\"value1\"}}]" }; } } } From a08318bc57977a546d6d015457c144180727d926 Mon Sep 17 00:00:00 2001 From: Gleb Osokin Date: Tue, 17 Nov 2020 13:25:03 +0100 Subject: [PATCH 09/10] rename AreEqual -> IsMatch + more test cases --- src/WireMock.Net/Matchers/JsonMatcher.cs | 4 ++-- src/WireMock.Net/Matchers/JsonPartialMatcher.cs | 17 ++++++----------- .../Matchers/JsonPartialMatcherTests.cs | 1 + 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/src/WireMock.Net/Matchers/JsonMatcher.cs b/src/WireMock.Net/Matchers/JsonMatcher.cs index 2aceb3501..e37d7fbc1 100644 --- a/src/WireMock.Net/Matchers/JsonMatcher.cs +++ b/src/WireMock.Net/Matchers/JsonMatcher.cs @@ -86,7 +86,7 @@ public double IsMatch(object input) { var inputAsJToken = ConvertValueToJToken(input); - match = AreEqual( + match = IsMatch( _jTokenConverter(_valueAsJToken), _jTokenConverter(inputAsJToken)); } @@ -108,7 +108,7 @@ public double IsMatch(object input) /// Matcher value /// Input value /// - protected virtual bool AreEqual(JToken value, JToken input) + protected virtual bool IsMatch(JToken value, JToken input) { return JToken.DeepEquals(value, input); } diff --git a/src/WireMock.Net/Matchers/JsonPartialMatcher.cs b/src/WireMock.Net/Matchers/JsonPartialMatcher.cs index 0666b79f6..54b04cb5b 100644 --- a/src/WireMock.Net/Matchers/JsonPartialMatcher.cs +++ b/src/WireMock.Net/Matchers/JsonPartialMatcher.cs @@ -48,19 +48,14 @@ public JsonPartialMatcher(MatchBehaviour matchBehaviour, [NotNull] object value, } /// - protected override bool AreEqual(JToken value, JToken input) + protected override bool IsMatch(JToken value, JToken input) { - return IsMatch(value, input); - } - - private static bool IsMatch(JToken value, JToken token) - { - if (value == null || value == token) + if (value == null || value == input) { return true; } - if (token == null || value.Type != token.Type) + if (input == null || value.Type != input.Type) { return false; } @@ -70,11 +65,11 @@ private static bool IsMatch(JToken value, JToken token) case JTokenType.Object: var nestedValues = value.ToObject>(); return nestedValues?.Any() != true || - nestedValues.All(pair => IsMatch(pair.Value, token.SelectToken(pair.Key))); + nestedValues.All(pair => IsMatch(pair.Value, input.SelectToken(pair.Key))); case JTokenType.Array: var valuesArray = value.ToObject(); - var tokenArray = token.ToObject(); + var tokenArray = input.ToObject(); if (valuesArray?.Any() != true) { @@ -85,7 +80,7 @@ private static bool IsMatch(JToken value, JToken token) valuesArray.All(subFilter => tokenArray.Any(subToken => IsMatch(subFilter, subToken))); default: - return value.ToString() == token.ToString(); + return value.ToString() == input.ToString(); } } } diff --git a/test/WireMock.Net.Tests/Matchers/JsonPartialMatcherTests.cs b/test/WireMock.Net.Tests/Matchers/JsonPartialMatcherTests.cs index 3ab0ef417..e35d1e1a3 100644 --- a/test/WireMock.Net.Tests/Matchers/JsonPartialMatcherTests.cs +++ b/test/WireMock.Net.Tests/Matchers/JsonPartialMatcherTests.cs @@ -333,6 +333,7 @@ public static IEnumerable ValidMatches() yield return new object[] { "[\"test\"]", "[\"test\"]" }; yield return new object[] { "[\"test\"]", "[\"test\", \"other\"]" }; yield return new object[] { "[123]", "[123]" }; + yield return new object[] { "[123]", "[123, 456]" }; yield return new object[] { "{ \"test\":\"value\" }", "{\"test\":\"value\",\"other\":123}" }; yield return new object[] { "{ \"test\":\"value\" }", "{\"test\":\"value\"}" }; yield return new object[] { "{ \"test.nested\":\"value\" }", "{\"test\":{\"nested\":\"value\"}}" }; From 4a2adb9a9998366c55b41922d225efdc57c9044d Mon Sep 17 00:00:00 2001 From: Gleb Osokin Date: Tue, 17 Nov 2020 14:37:44 +0100 Subject: [PATCH 10/10] separate tests for JPath matcher values --- .../Matchers/JsonPartialMatcherTests.cs | 82 ++++++++++++------- 1 file changed, 53 insertions(+), 29 deletions(-) diff --git a/test/WireMock.Net.Tests/Matchers/JsonPartialMatcherTests.cs b/test/WireMock.Net.Tests/Matchers/JsonPartialMatcherTests.cs index e35d1e1a3..0e274035d 100644 --- a/test/WireMock.Net.Tests/Matchers/JsonPartialMatcherTests.cs +++ b/test/WireMock.Net.Tests/Matchers/JsonPartialMatcherTests.cs @@ -298,7 +298,16 @@ public void JsonPartialMatcher_IsMatch_JObjectWithDateTimeOffsetAsString() } [Theory] - [MemberData(nameof(ValidMatches))] + [InlineData("{\"test\":\"abc\"}", "{\"test\":\"abc\",\"other\":\"xyz\"}")] + [InlineData("\"test\"", "\"test\"")] + [InlineData("123", "123")] + [InlineData("[\"test\"]", "[\"test\"]")] + [InlineData("[\"test\"]", "[\"test\", \"other\"]")] + [InlineData("[123]", "[123]")] + [InlineData("[123]", "[123, 456]")] + [InlineData("{ \"test\":\"value\" }", "{\"test\":\"value\",\"other\":123}")] + [InlineData("{ \"test\":\"value\" }", "{\"test\":\"value\"}")] + [InlineData("{\"test\":{\"nested\":\"value\"}}", "{\"test\":{\"nested\":\"value\"}}")] public void JsonPartialMatcher_IsMatch_StringInputValidMatch(string value, string input) { // Assign @@ -312,7 +321,17 @@ public void JsonPartialMatcher_IsMatch_StringInputValidMatch(string value, strin } [Theory] - [MemberData(nameof(InvalidMatches))] + [InlineData("\"test\"", null)] + [InlineData("\"test1\"", "\"test2\"")] + [InlineData("123", "1234")] + [InlineData("[\"test\"]", "[\"test1\"]")] + [InlineData("[\"test\"]", "[\"test1\", \"test2\"]")] + [InlineData("[123]", "[1234]")] + [InlineData("{}", "\"test\"")] + [InlineData("{ \"test\":\"value\" }", "{\"test\":\"value2\"}")] + [InlineData("{ \"test.nested\":\"value\" }", "{\"test\":{\"nested\":\"value1\"}}")] + [InlineData("{\"test\":{\"test1\":\"value\"}}", "{\"test\":{\"test1\":\"value1\"}}")] + [InlineData("[{ \"test.nested\":\"value\" }]", "[{\"test\":{\"nested\":\"value1\"}}]")] public void JsonPartialMatcher_IsMatch_StringInputWithInvalidMatch(string value, string input) { // Assign @@ -325,37 +344,42 @@ public void JsonPartialMatcher_IsMatch_StringInputWithInvalidMatch(string value, Assert.Equal(0.0, match); } - public static IEnumerable ValidMatches() + [Theory] + [InlineData("{ \"test.nested\":123 }", "{\"test\":{\"nested\":123}}")] + [InlineData("{ \"test.nested\":[123, 456] }", "{\"test\":{\"nested\":[123, 456]}}")] + [InlineData("{ \"test.nested\":\"value\" }", "{\"test\":{\"nested\":\"value\"}}")] + [InlineData("{ \"['name.with.dot']\":\"value\" }", "{\"name.with.dot\":\"value\"}")] + [InlineData("[{ \"test.nested\":\"value\" }]", "[{\"test\":{\"nested\":\"value\"}}]")] + [InlineData("[{ \"['name.with.dot']\":\"value\" }]", "[{\"name.with.dot\":\"value\"}]")] + public void JsonPartialMatcher_IsMatch_ValueAsJPathValidMatch(string value, string input) { - yield return new object[] { "{\"test\":\"abc\"}", "{\"test\":\"abc\",\"other\":\"xyz\"}" }; - yield return new object[] { "\"test\"", "\"test\"" }; - yield return new object[] { "123", "123" }; - yield return new object[] { "[\"test\"]", "[\"test\"]" }; - yield return new object[] { "[\"test\"]", "[\"test\", \"other\"]" }; - yield return new object[] { "[123]", "[123]" }; - yield return new object[] { "[123]", "[123, 456]" }; - yield return new object[] { "{ \"test\":\"value\" }", "{\"test\":\"value\",\"other\":123}" }; - yield return new object[] { "{ \"test\":\"value\" }", "{\"test\":\"value\"}" }; - yield return new object[] { "{ \"test.nested\":\"value\" }", "{\"test\":{\"nested\":\"value\"}}" }; - yield return new object[] { "{ \"['name.with.dot']\":\"value\" }", "{\"name.with.dot\":\"value\"}" }; - yield return new object[] { "{\"test\":{\"nested\":\"value\"}}", "{\"test\":{\"nested\":\"value\"}}" }; - yield return new object[] { "[{ \"test.nested\":\"value\" }]", "[{\"test\":{\"nested\":\"value\"}}]" }; - yield return new object[] { "[{ \"['name.with.dot']\":\"value\" }]", "[{\"name.with.dot\":\"value\"}]" }; + // Assign + var matcher = new JsonPartialMatcher(value); + + // Act + double match = matcher.IsMatch(input); + + // Assert + Assert.Equal(1.0, match); } - public static IEnumerable InvalidMatches() + [Theory] + [InlineData("{ \"test.nested\":123 }", "{\"test\":{\"nested\":456}}")] + [InlineData("{ \"test.nested\":[123, 456] }", "{\"test\":{\"nested\":[1, 2]}}")] + [InlineData("{ \"test.nested\":\"value\" }", "{\"test\":{\"nested\":\"value1\"}}")] + [InlineData("{ \"['name.with.dot']\":\"value\" }", "{\"name.with.dot\":\"value1\"}")] + [InlineData("[{ \"test.nested\":\"value\" }]", "[{\"test\":{\"nested\":\"value1\"}}]")] + [InlineData("[{ \"['name.with.dot']\":\"value\" }]", "[{\"name.with.dot\":\"value1\"}]")] + public void JsonPartialMatcher_IsMatch_ValueAsJPathInvalidMatch(string value, string input) { - yield return new object[] { "\"test\"", null }; - yield return new object[] { "\"test1\"", "\"test2\"" }; - yield return new object[] { "123", "1234" }; - yield return new object[] { "[\"test\"]", "[\"test1\"]" }; - yield return new object[] { "[\"test\"]", "[\"test1\", \"test2\"]" }; - yield return new object[] { "[123]", "[1234]" }; - yield return new object[] { "{}", "\"test\"" }; - yield return new object[] { "{ \"test\":\"value\" }", "{\"test\":\"value2\"}" }; - yield return new object[] { "{ \"test.nested\":\"value\" }", "{\"test\":{\"nested\":\"value1\"}}" }; - yield return new object[] { "{\"test\":{\"test1\":\"value\"}}", "{\"test\":{\"test1\":\"value1\"}}" }; - yield return new object[] { "[{ \"test.nested\":\"value\" }]", "[{\"test\":{\"nested\":\"value1\"}}]" }; + // Assign + var matcher = new JsonPartialMatcher(value); + + // Act + double match = matcher.IsMatch(input); + + // Assert + Assert.Equal(0.0, match); } } }