Browse Source

Add hasJsonPath/doesNotHaveJsonPath assertion options

Issue: SPR-16339
pull/1639/head
Rossen Stoyanchev 7 years ago
parent
commit
0f1f95e090
  1. 55
      spring-test/src/main/java/org/springframework/test/util/JsonPathExpectationsHelper.java
  2. 39
      spring-test/src/main/java/org/springframework/test/web/client/match/JsonPathRequestMatchers.java
  3. 18
      spring-test/src/main/java/org/springframework/test/web/reactive/server/JsonPathAssertions.java
  4. 40
      spring-test/src/main/java/org/springframework/test/web/servlet/result/JsonPathResultMatchers.java
  5. 50
      spring-test/src/test/java/org/springframework/test/util/JsonPathExpectationsHelperTests.java

55
spring-test/src/main/java/org/springframework/test/util/JsonPathExpectationsHelper.java

@ -173,10 +173,11 @@ public class JsonPathExpectationsHelper { @@ -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 { @@ -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 { @@ -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)));

39
spring-test/src/main/java/org/springframework/test/web/client/match/JsonPathRequestMatchers.java

@ -131,6 +131,45 @@ public class JsonPathRequestMatchers { @@ -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.

18
spring-test/src/main/java/org/springframework/test/web/reactive/server/JsonPathAssertions.java

@ -82,6 +82,24 @@ public class JsonPathAssertions { @@ -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)}.
*/

40
spring-test/src/main/java/org/springframework/test/web/servlet/result/JsonPathResultMatchers.java

@ -98,7 +98,8 @@ public class JsonPathResultMatchers { @@ -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 { @@ -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 { @@ -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}.

50
spring-test/src/test/java/org/springframework/test/util/JsonPathExpectationsHelperTests.java

@ -20,7 +20,7 @@ import org.junit.Rule; @@ -20,7 +20,7 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import static org.hamcrest.core.Is.*;
import static org.hamcrest.core.Is.is;
/**
* Unit tests for {@link JsonPathExpectationsHelper}.
@ -217,6 +217,54 @@ public class JsonPathExpectationsHelperTests { @@ -217,6 +217,54 @@ public class JsonPathExpectationsHelperTests {
new JsonPathExpectationsHelper(expression).assertValueIsNotEmpty(CONTENT);
}
@Test
public void hasJsonPath() {
new JsonPathExpectationsHelper("$.abc").hasJsonPath("{\"abc\": \"123\"}");
}
@Test
public void hasJsonPathWithNull() {
new JsonPathExpectationsHelper("$.abc").hasJsonPath("{\"abc\": null}");
}
@Test
public void hasJsonPathForIndefinatePathWithResults() {
new JsonPathExpectationsHelper("$.familyMembers[?(@.name == 'Bart')]").hasJsonPath(SIMPSONS);
}
@Test
public void hasJsonPathForIndefinatePathWithEmptyResults() {
String expression = "$.familyMembers[?(@.name == 'Dilbert')]";
exception.expect(AssertionError.class);
exception.expectMessage("No values for JSON path \"" + expression + "\"");
new JsonPathExpectationsHelper(expression).hasJsonPath(SIMPSONS);
}
@Test // SPR-16339
public void doesNotHaveJsonPath() {
new JsonPathExpectationsHelper("$.abc").doesNotHaveJsonPath("{}");
}
@Test // SPR-16339
public void doesNotHaveJsonPathWithNull() {
exception.expect(AssertionError.class);
new JsonPathExpectationsHelper("$.abc").doesNotHaveJsonPath("{\"abc\": null}");
}
@Test
public void doesNotHaveJsonPathForIndefinatePathWithEmptyResults() {
new JsonPathExpectationsHelper("$.familyMembers[?(@.name == 'Dilbert')]").doesNotHaveJsonPath(SIMPSONS);
}
@Test
public void doesNotHaveEmptyPathForIndefinatePathWithResults() {
String expression = "$.familyMembers[?(@.name == 'Bart')]";
exception.expect(AssertionError.class);
exception.expectMessage("Expected no values at JSON path \"" + expression + "\" " +
"but found: [{\"name\":\"Bart\"}]");
new JsonPathExpectationsHelper(expression).doesNotHaveJsonPath(SIMPSONS);
}
@Test
public void assertValue() throws Exception {
new JsonPathExpectationsHelper("$.num").assertValue(CONTENT, 5);

Loading…
Cancel
Save