Skip to content

Commit

Permalink
[plugin-rest-api] Fix detection of empty JSON array diff (#626)
Browse files Browse the repository at this point in the history
  • Loading branch information
uarlouski committed May 30, 2020
1 parent 23a977a commit 5caaecf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.jayway.jsonpath.PathNotFoundException;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.jbehave.core.annotations.Then;
import org.jbehave.core.annotations.When;
import org.vividus.bdd.context.IBddVariableContext;
Expand All @@ -61,9 +62,10 @@ public class JsonResponseValidationSteps
{
private static final Set<String> ASSERTION_BOUNDS = Set.of(
"Different (?:value|keys) found",
"Array \"[^\"]+\" has different"
"Array \"[^\"]*\" has different"
);
private static final String PIPE = "|";
private static final char LF = '\n';
private static final Pattern DIFFERENCES_PATTERN = Pattern.compile(
"(?=(?:" + join(PIPE, ASSERTION_BOUNDS) + ")).+?(?=(?:" + join(PIPE, ASSERTION_BOUNDS) + "|$))",
Pattern.DOTALL);
Expand Down Expand Up @@ -121,12 +123,18 @@ private Function<String, Boolean> match(String jsonPath, String expectedData, Op
actualData, jsonMatcher);
}

StringBuilder matched = new StringBuilder("JSON documents are different:").append(LF);
Matcher matcher = DIFFERENCES_PATTERN.matcher(differences);
while (matcher.find())
{
String assertion = matcher.group().strip();
matched.append(assertion).append(LF);
softAssert.recordFailedAssertion(assertion);
}
String matchedDiff = matched.toString();
Validate.isTrue(matchedDiff.equals(differences),
"Unable to match all JSON diff entries from the diff text."
+ "%nExpected diff to match:%n%s%nActual matched diff:%n%s%n", differences, matchedDiff);
return false;
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,22 @@ void testIsDataByJsonPathEqualIgnoringArrayOrder()
new Options(Option.IGNORING_ARRAY_ORDER));
}

@Test
void testIsDataByJsonPathFromJsonEqualCheckEmptyArrayMissmatch()
{
String json = "{ \"arrayKey\": [ { \"idKey\": \"q4jn0f8\", \"randValueKey\": \"i4t8ivC\"} ] }";
String expected = "[ { \"idKey\": \"b54Y8id\", \"randValueKey\": \"i4t8ivC\"} ]";

jsonResponseValidationSteps.isDataByJsonPathFromJsonEqual(json, "$.arrayKey.[?(@.idKey==\"b54Y8id\")]",
expected, Options.empty());

verify(softAssert).recordFailedAssertion("Array \"\" has different length, expected: <1> but was: <0>.");
verify(softAssert).recordFailedAssertion("Array \"\" has different content. Missing values: "
+ "[{\"idKey\":\"b54Y8id\",\"randValueKey\":\"i4t8ivC\"}], expected: <[{\"idKey\":\"b54Y8id\","
+ "\"randValueKey\":\"i4t8ivC\"}]> but was: <[]>");
verifyNoMoreInteractions(softAssert);
}

@Test
void testIsDataByJsonPathEqualIgnoringArrayOrderAndExtraArrayItems()
{
Expand Down

0 comments on commit 5caaecf

Please sign in to comment.