Introduce empty & non-empty JsonPath matchers

This commit introduces new `isEmpty()` and `isNotEmpty()` methods in
`JsonPathResultMatchers` and `JsonPathRequestMatchers` which delegate
to the new `assertValueIsEmpty()` and `assertValueIsNotEmpty()` methods
in `JsonPathExpectationsHelper`, respectively.

Issue: SPR-13352
This commit is contained in:
Sam Brannen
2015-08-15 19:28:09 +02:00
parent 4799668a98
commit 7a17abe448
6 changed files with 369 additions and 38 deletions

View File

@@ -25,7 +25,9 @@ import java.util.Map;
import org.hamcrest.Matcher;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
import com.jayway.jsonpath.InvalidPathException;
import com.jayway.jsonpath.JsonPath;
@@ -135,8 +137,7 @@ public class JsonPathExpectationsHelper {
*/
public void assertValueIsString(String content) throws ParseException {
Object value = assertExistsAndReturn(content);
String reason = "Expected a string at JSON path \"" + this.expression + "\" but found: " + value;
assertThat(reason, value, instanceOf(String.class));
assertThat(failureReason("a string", value), value, instanceOf(String.class));
}
/**
@@ -147,8 +148,7 @@ public class JsonPathExpectationsHelper {
*/
public void assertValueIsBoolean(String content) throws ParseException {
Object value = assertExistsAndReturn(content);
String reason = "Expected a boolean at JSON path \"" + this.expression + "\" but found: " + value;
assertThat(reason, value, instanceOf(Boolean.class));
assertThat(failureReason("a boolean", value), value, instanceOf(Boolean.class));
}
/**
@@ -159,8 +159,7 @@ public class JsonPathExpectationsHelper {
*/
public void assertValueIsNumber(String content) throws ParseException {
Object value = assertExistsAndReturn(content);
String reason = "Expected a number at JSON path \"" + this.expression + "\" but found: " + value;
assertThat(reason, value, instanceOf(Number.class));
assertThat(failureReason("a number", value), value, instanceOf(Number.class));
}
/**
@@ -170,8 +169,7 @@ public class JsonPathExpectationsHelper {
*/
public void assertValueIsArray(String content) throws ParseException {
Object value = assertExistsAndReturn(content);
String reason = "Expected an array at JSON path \"" + this.expression + "\" but found: " + value;
assertThat(reason, value, instanceOf(List.class));
assertThat(failureReason("an array", value), value, instanceOf(List.class));
}
/**
@@ -182,8 +180,7 @@ public class JsonPathExpectationsHelper {
*/
public void assertValueIsMap(String content) throws ParseException {
Object value = assertExistsAndReturn(content);
String reason = "Expected a map at JSON path \"" + this.expression + "\" but found: " + value;
assertThat(reason, value, instanceOf(Map.class));
assertThat(failureReason("a map", value), value, instanceOf(Map.class));
}
/**
@@ -214,7 +211,7 @@ public class JsonPathExpectationsHelper {
catch (AssertionError ex) {
return;
}
String reason = "Expected no value at JSON path \"" + this.expression + "\" but found: " + value;
String reason = failureReason("no value", value);
if (pathIsIndefinite() && value instanceof List) {
assertTrue(reason, ((List<?>) value).isEmpty());
}
@@ -223,6 +220,35 @@ public class JsonPathExpectationsHelper {
}
}
/**
* Evaluate the JSON path expression against the supplied {@code content}
* and assert that an empty value exists at the given path.
* <p>For the semantics of <em>empty</em>, consult the Javadoc for
* {@link ObjectUtils#isEmpty(Object)}.
* @param content the JSON content
*/
public void assertValueIsEmpty(String content) throws ParseException {
Object value = evaluateJsonPath(content);
assertTrue(failureReason("an empty value", value), ObjectUtils.isEmpty(value));
}
/**
* Evaluate the JSON path expression against the supplied {@code content}
* and assert that a non-empty value exists at the given path.
* <p>For the semantics of <em>empty</em>, consult the Javadoc for
* {@link ObjectUtils#isEmpty(Object)}.
* @param content the JSON content
*/
public void assertValueIsNotEmpty(String content) throws ParseException {
Object value = evaluateJsonPath(content);
assertTrue(failureReason("a non-empty value", value), !ObjectUtils.isEmpty(value));
}
private String failureReason(String expectedDescription, Object value) {
return String.format("Expected %s at JSON path \"%s\" but found: %s", expectedDescription, this.expression,
ObjectUtils.nullSafeToString(StringUtils.quoteIfString(value)));
}
private Object evaluateJsonPath(String content) throws ParseException {
String message = "No value at JSON path \"" + this.expression + "\", exception: ";
try {

View File

@@ -115,6 +115,44 @@ public class JsonPathRequestMatchers {
};
}
/**
* Evaluate the JSON path expression against the request content and
* assert that an empty value exists at the given path.
* <p>For the semantics of <em>empty</em>, consult the Javadoc for
* {@link org.springframework.util.ObjectUtils#isEmpty(Object)}.
* @since 4.2.1
* @see #isNotEmpty()
* @see #exists()
* @see #doesNotExist()
*/
public RequestMatcher isEmpty() {
return new AbstractJsonPathRequestMatcher() {
@Override
public void matchInternal(MockClientHttpRequest request) throws IOException, ParseException {
JsonPathRequestMatchers.this.jsonPathHelper.assertValueIsEmpty(request.getBodyAsString());
}
};
}
/**
* Evaluate the JSON path expression against the request content and
* assert that a non-empty value exists at the given path.
* <p>For the semantics of <em>empty</em>, consult the Javadoc for
* {@link org.springframework.util.ObjectUtils#isEmpty(Object)}.
* @since 4.2.1
* @see #isEmpty()
* @see #exists()
* @see #doesNotExist()
*/
public RequestMatcher isNotEmpty() {
return new AbstractJsonPathRequestMatcher() {
@Override
public void matchInternal(MockClientHttpRequest request) throws IOException, ParseException {
JsonPathRequestMatchers.this.jsonPathHelper.assertValueIsNotEmpty(request.getBodyAsString());
}
};
}
/**
* Evaluate the JSON path expression against the request content and
* assert that the result is a {@link String}.

View File

@@ -116,6 +116,46 @@ public class JsonPathResultMatchers {
};
}
/**
* Evaluate the JSON path expression against the response content and
* assert that an empty value exists at the given path.
* <p>For the semantics of <em>empty</em>, consult the Javadoc for
* {@link org.springframework.util.ObjectUtils#isEmpty(Object)}.
* @since 4.2.1
* @see #isNotEmpty()
* @see #exists()
* @see #doesNotExist()
*/
public ResultMatcher isEmpty() {
return new ResultMatcher() {
@Override
public void match(MvcResult result) throws Exception {
String content = result.getResponse().getContentAsString();
JsonPathResultMatchers.this.jsonPathHelper.assertValueIsEmpty(content);
}
};
}
/**
* Evaluate the JSON path expression against the response content and
* assert that a non-empty value exists at the given path.
* <p>For the semantics of <em>empty</em>, consult the Javadoc for
* {@link org.springframework.util.ObjectUtils#isEmpty(Object)}.
* @since 4.2.1
* @see #isEmpty()
* @see #exists()
* @see #doesNotExist()
*/
public ResultMatcher isNotEmpty() {
return new ResultMatcher() {
@Override
public void match(MvcResult result) throws Exception {
String content = result.getResponse().getContentAsString();
JsonPathResultMatchers.this.jsonPathHelper.assertValueIsNotEmpty(content);
}
};
}
/**
* Evaluate the JSON path expression against the response content and
* assert that the result is a {@link String}.