Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
fslev committed Oct 4, 2022
1 parent f57df24 commit 663d0b5
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public List<String> match() {
try {
diffs.addAll(new JsonPathMatcher(jsonPathExpression.get(), expectedValue, actual, comparator, compareModes).match());
} catch (PathNotFoundException e) {
diffs.add(String.format("json path '%s' -> %s", jsonPathExpression.get(), e.getMessage()));
diffs.add(String.format("Json path '%s' -> %s", jsonPathExpression.get(), e.getMessage()));
}
}
break;
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/io/json/compare/matcher/JsonPathMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public List<String> match() {
List<String> diffs = new ArrayList<>();
JsonNode result = MAPPER.convertValue(PARSE_CONTEXT.parse(actual).read(jsonPath), JsonNode.class);
List<String> jsonPathDiffs = new JsonMatcher(expected, result, comparator, compareModes).match();
jsonPathDiffs.forEach(diff -> diffs.add(String.format("Json path ('%s') -> Expected json path result:" +
System.lineSeparator() + "%s" + System.lineSeparator() +
"But got:" + System.lineSeparator() + "%s" + System.lineSeparator() + System.lineSeparator() + "%s",
jsonPathDiffs.forEach(diff -> diffs.add(String.format("Json path '%s' -> Expected json path result:" +
System.lineSeparator() + "%s" + System.lineSeparator() + "But got:" +
System.lineSeparator() + "%s" + System.lineSeparator() + "________diffs________" + System.lineSeparator() + "%s",
jsonPath, expected, result, diff)));
return diffs;
}
Expand Down
12 changes: 6 additions & 6 deletions src/test/java/io/json/compare/matcher/JSONPathCompareTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ public void checkInvalidOrNotFoundJsonPathErrorMessage() {
JSONCompare.assertMatches(expected, actual);
} catch (AssertionError e) {
assertTrue(e.getMessage().matches("(?s).*FOUND 1 DIFFERENCE.*" +
"_________________________DIFF__________________________.*" +
"\\Qa -> a1 -> a11 -> json path '$.idontexist' -> No results for path: $['idontexist']\\E.*"));
"____DIFF_____.*" +
"a -> a1 -> a11 -> Json path \\Q'$.idontexist'\\E.*->.*No results for path.*idontexist.*"));
return;
}
fail("No error thrown");
Expand All @@ -218,10 +218,10 @@ public void checkJsonPathAssertionErrorMessage() {
try {
JSONCompare.assertMatches(expected, actual);
} catch (AssertionError e) {
assertTrue(e.getMessage().contains("a -> a1 -> a11 -> Json path ('$.a') -> Expected json path result:\n" +
"\"lorem1\"\n" +
"But got:\n" +
"\"lorem\""));
assertTrue(e.getMessage().matches("(?s).*a -> a1 -> a11 -> \\QJson path '$.a' -> Expected json path result\\E.*" +
"\"lorem1\".*" +
"But got:.*" +
"\"lorem\".*"));
return;
}
fail("No error thrown");
Expand Down
57 changes: 57 additions & 0 deletions src/test/java/io/json/compare/matcher/diffs/JsonPathDiffTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package io.json.compare.matcher.diffs;

import io.json.compare.JSONCompare;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class JsonPathDiffTests {

@Test
public void checkMultipleJsonPathDifferences() {
String expected = "{\"#($.a.length())\":3,\"b\":\"val1\",\"x\":{\"x1\":{\"y11\":{\"#($.www)\":\"lorem1\"}}},\"z\":[{\"#($.length())\":1}]," +
"\"u\":{\"#($.u1)\":{\"u11\":20209}}}";
String actual = "{\"z\":[2,3,4],\"x\":{\"x2\":290.11,\"x1\":{\"x11\":null,\"y11\":{\"a\":\"lorem2\"}}},\"b\":\"val2\",\"a\":[4,5]," +
"\"u\":{\"u1\":{\"u11\":20000}}}";
AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual));
assertTrue(error.getMessage().matches("(?s).*FOUND 5 DIFFERENCE.*" +
"\\QJson path '$.a.length()' -> Expected json path result\\E.*" +
"3.*But got.*2.*" +
"b ->.*Expected value: \"val1\" But got: \"val2\".*" +
"x -> x1 -> y11 -> \\QJson path '$.www'\\E -> No results for path.*" +
"z -> \\QJson path '$.length()' -> Expected json path result\\E.*" +
"1.*But got.*3.*" +
"u -> \\QJson path '$.u1' -> Expected json path result\\E.*" +
"u11 ->.*Expected value: 20209 But got: 20000.*"));
JSONCompare.assertNotMatches(expected, actual);

String expected1 = "{\"#($.a.length())\":2,\"b\":\"val2\",\"x\":{\"x1\":{\"y11\":{\"#($.a)\":\"lorem2\"}}}}";
String actual1 = "{\"x\":{\"x2\":290.11,\"x1\":{\"x11\":null,\"y11\":{\"a\":\"lorem2\"}}},\"b\":\"val2\",\"a\":[4,5]}";
JSONCompare.assertMatches(expected1, actual1);
}

@Test
public void checkMultipleJsonPathDifferencesFromArray() {
String expected = "[false, {\"#($.length())\":1}, \"b\",{\"x\":{\"#($.length())\":2}}]";
String actual = "[\"b\",false,{\"x\":[1,2,5]}, {\"w\":\"yyyy\"}]";
AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual));
assertTrue(error.getMessage().matches("(?s).*FOUND 2 DIFFERENCE.*" +
"\\QJson path '$.length()' -> Expected json path result\\E.*" +
"1.*But got.*4.*" +
"Expected element from position 4 was NOT FOUND:.*"));
JSONCompare.assertNotMatches(expected, actual);

String expected1 = "[false, {\"#($.length())\":1}, \"b\",{\"x\":{\"#($.length())\":3}}]";
String actual1 = "[\"b\",false,{\"x\":[1,2,5]}, {\"w\":\"yyyy\"}]";
AssertionError error1 = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected1, actual1));
assertTrue(error1.getMessage().matches("(?s).*FOUND 1 DIFFERENCE.*" +
"\\QJson path '$.length()' -> Expected json path result\\E.*" +
"1.*But got.*4.*"));
JSONCompare.assertNotMatches(expected1, actual1);

String expected2 = "[false, {\"#($.length())\":4}, \"b\",{\"x\":{\"#($.length())\":3}}]";
String actual2 = "[\"b\",false,{\"x\":[1,2,5]}, {\"w\":\"yyyy\"}]";
JSONCompare.assertMatches(expected2, actual2);
}
}

0 comments on commit 663d0b5

Please sign in to comment.