Add hasJsonPath/doesNotHaveJsonPath assertion options

Issue: SPR-16339
This commit is contained in:
Rossen Stoyanchev
2018-01-03 20:27:09 -05:00
parent a49123a72b
commit 0f1f95e090
5 changed files with 193 additions and 9 deletions

View File

@@ -173,10 +173,11 @@ public class JsonPathExpectationsHelper {
/**
* Evaluate the JSON path expression against the supplied {@code content}
* and assert that a non-null value exists at the given path.
* <p>If the JSON path expression is not
* and assert that a non-null value, possibly an empty array or map, exists
* at the given path.
* <p>Note that if the JSON path expression is not
* {@linkplain JsonPath#isDefinite() definite}, this method asserts
* that the value at the given path is not <em>empty</em>.
* that the list of values at the given path is not <em>empty</em>.
* @param content the JSON content
*/
public void exists(String content) {
@@ -185,10 +186,10 @@ public class JsonPathExpectationsHelper {
/**
* Evaluate the JSON path expression against the supplied {@code content}
* and assert that a value does not exist at the given path.
* <p>If the JSON path expression is not
* and assert that a non-null value does not exist at the given path.
* <p>Note that if the JSON path expression is not
* {@linkplain JsonPath#isDefinite() definite}, this method asserts
* that the value at the given path is <em>empty</em>.
* that the list of values at the given path is <em>empty</em>.
* @param content the JSON content
*/
public void doesNotExist(String content) {
@@ -232,6 +233,48 @@ public class JsonPathExpectationsHelper {
assertTrue(failureReason("a non-empty value", value), !ObjectUtils.isEmpty(value));
}
/**
* Evaluate the JSON path expression against the supplied {@code content}
* and assert that a value, possibly {@code null}, exists.
* <p>If the JSON path expression is not
* {@linkplain JsonPath#isDefinite() definite}, this method asserts
* that the list of values at the given path is not <em>empty</em>.
* @param content the JSON content
* @since 5.0.3
*/
public void hasJsonPath(String content) {
Object value = evaluateJsonPath(content);
if (pathIsIndefinite() && value instanceof List) {
assertTrue("No values for JSON path \"" + this.expression + "\"", !((List<?>) value).isEmpty());
}
}
/**
* Evaluate the JSON path expression against the supplied {@code content}
* and assert that a value, including {@code null} values, does not exist
* at the given path.
* <p>If the JSON path expression is not
* {@linkplain JsonPath#isDefinite() definite}, this method asserts
* that the list of values at the given path is <em>empty</em>.
* @param content the JSON content
* @since 5.0.3
*/
public void doesNotHaveJsonPath(String content) {
Object value;
try {
value = evaluateJsonPath(content);
}
catch (AssertionError ex) {
return;
}
if (pathIsIndefinite() && value instanceof List) {
assertTrue(failureReason("no values", value), ((List<?>) value).isEmpty());
}
else {
fail(failureReason("no value", value));
}
}
private String failureReason(String expectedDescription, @Nullable Object value) {
return String.format("Expected %s at JSON path \"%s\" but found: %s", expectedDescription, this.expression,
ObjectUtils.nullSafeToString(StringUtils.quoteIfString(value)));

View File

@@ -131,6 +131,45 @@ public class JsonPathRequestMatchers {
};
}
/**
* Evaluate the JSON path expression against the response content
* and assert that a value, possibly {@code null}, exists.
* <p>If the JSON path expression is not
* {@linkplain JsonPath#isDefinite() definite}, this method asserts
* that the list of values at the given path is not <em>empty</em>.
* @since 5.0.3
* @see #exists()
* @see #isNotEmpty()
*/
public RequestMatcher hasJsonPath() {
return new AbstractJsonPathRequestMatcher() {
@Override
protected void matchInternal(MockClientHttpRequest request) {
JsonPathRequestMatchers.this.jsonPathHelper.hasJsonPath(request.getBodyAsString());
}
};
}
/**
* Evaluate the JSON path expression against the supplied {@code content}
* and assert that a value, including {@code null} values, does not exist
* at the given path.
* <p>If the JSON path expression is not
* {@linkplain JsonPath#isDefinite() definite}, this method asserts
* that the list of values at the given path is <em>empty</em>.
* @since 5.0.3
* @see #doesNotExist()
* @see #isEmpty()
*/
public RequestMatcher doesNotHaveJsonPath() {
return new AbstractJsonPathRequestMatcher() {
@Override
protected void matchInternal(MockClientHttpRequest request) {
JsonPathRequestMatchers.this.jsonPathHelper.doesNotHaveJsonPath(request.getBodyAsString());
}
};
}
/**
* Evaluate the JSON path expression against the request content and
* assert that an empty value exists at the given path.

View File

@@ -82,6 +82,24 @@ public class JsonPathAssertions {
return this.bodySpec;
}
/**
* Applies {@link JsonPathExpectationsHelper#hasJsonPath}.
* @since 5.0.3
*/
public WebTestClient.BodyContentSpec hasJsonPath() {
this.pathHelper.hasJsonPath(this.content);
return this.bodySpec;
}
/**
* Applies {@link JsonPathExpectationsHelper#doesNotHaveJsonPath}.
* @since 5.0.3
*/
public WebTestClient.BodyContentSpec doesNotHaveJsonPath() {
this.pathHelper.doesNotHaveJsonPath(this.content);
return this.bodySpec;
}
/**
* Applies {@link JsonPathExpectationsHelper#assertValueIsBoolean(String)}.
*/

View File

@@ -98,7 +98,8 @@ public class JsonPathResultMatchers {
/**
* Evaluate the JSON path expression against the response content and
* assert that a non-null value exists at the given path.
* assert that a non-null value, possibly an empty array or map, exists at
* the given path.
* <p>If the JSON path expression is not {@linkplain JsonPath#isDefinite
* definite}, this method asserts that the value at the given path is not
* <em>empty</em>.
@@ -112,7 +113,7 @@ public class JsonPathResultMatchers {
/**
* Evaluate the JSON path expression against the response content and
* assert that a value does not exist at the given path.
* assert that a non-null value does not exist at the given path.
* <p>If the JSON path expression is not {@linkplain JsonPath#isDefinite
* definite}, this method asserts that the value at the given path is
* <em>empty</em>.
@@ -158,6 +159,41 @@ public class JsonPathResultMatchers {
};
}
/**
* Evaluate the JSON path expression against the response content
* and assert that a value, possibly {@code null}, exists.
* <p>If the JSON path expression is not
* {@linkplain JsonPath#isDefinite() definite}, this method asserts
* that the list of values at the given path is not <em>empty</em>.
* @since 5.0.3
* @see #exists()
* @see #isNotEmpty()
*/
public ResultMatcher hasJsonPath() {
return result -> {
String content = getContent(result);
jsonPathHelper.hasJsonPath(content);
};
}
/**
* Evaluate the JSON path expression against the supplied {@code content}
* and assert that a value, including {@code null} values, does not exist
* at the given path.
* <p>If the JSON path expression is not
* {@linkplain JsonPath#isDefinite() definite}, this method asserts
* that the list of values at the given path is <em>empty</em>.
* @since 5.0.3
* @see #doesNotExist()
* @see #isEmpty()
*/
public ResultMatcher doesNotHaveJsonPath() {
return result -> {
String content = getContent(result);
jsonPathHelper.doesNotHaveJsonPath(content);
};
}
/**
* Evaluate the JSON path expression against the response content and
* assert that the result is a {@link String}.