Added support for .. in the path

when one provides .. in the path it's actually a collection. So if regex is there now we support it. If it's size check - exception will be thrown

fixes #217
This commit is contained in:
Marcin Grzejszczak
2017-02-09 16:36:37 +01:00
parent dc1c230b3d
commit 576330652f
2 changed files with 7 additions and 3 deletions

View File

@@ -316,7 +316,7 @@ abstract class MethodBodyBuilder {
String path = quotedAndEscaped(it.path())
Object retrievedValue = value(copiedBody, it)
String valueAsParam = retrievedValue instanceof String ? quotedAndEscaped(retrievedValue.toString()) : retrievedValue.toString()
if (path.contains("[*]") && MatchingType.regexRelated(it.matchingType())) {
if (arrayRelated(path) && MatchingType.regexRelated(it.matchingType())) {
buildCustomMatchingConditionForEachElement(bb, path, valueAsParam)
} else {
String comparisonMethod = it.matchingType() == MatchingType.EQUALITY ? "isEqualTo" : "matches"
@@ -328,7 +328,7 @@ abstract class MethodBodyBuilder {
} else {
Object elementFromBody = value(copiedBody, it)
if (it.minTypeOccurrence() != null || it.maxTypeOccurrence() != null) {
if (it.path().contains("[*]")) {
if (arrayRelated(it.path())) {
throw new UnsupportedOperationException("Version 1.0.x doesn't support checking sizes when JSON Path contains [*]. " +
"For more information check out https://github.com/spring-cloud/spring-cloud-contract/issues/217 . " +
"Please upgrade to the latest version of Spring Cloud Contract for this feature.")
@@ -346,6 +346,10 @@ abstract class MethodBodyBuilder {
processBodyElement(bb, "", convertedResponseBody)
}
protected boolean arrayRelated(String path) {
return path.contains("[*]") || path.contains("..")
}
protected void buildCustomMatchingConditionForEachElement(BlockBuilder bb, String path, String valueAsParam) {
String method = "assertThat(parsedJson.read(${path}, java.util.Collection.class)).as(\"All elements match regex\").are("
String newCondition = "new org.assertj.core.api.Condition<Object>() {"

View File

@@ -225,7 +225,7 @@ class MockMvcMethodBodyBuilderWithMatchersSpec extends Specification implements
test.contains('new org.assertj.core.api.Condition<Object>() {')
test.contains('@Override public boolean matches(Object o) {')
test.contains('return ((String)o).matches("^[0-9]{3} [0-9]{3}-[0-9]{4}' + rootElement + '")')
test.contains('assertThat(parsedJson.read("' + rootElement + '..number", String.class)).matches("^[0-9]{3} [0-9]{3}-[0-9]{4}' + rootElement + '")')
test.contains('assertThat(parsedJson.read("' + rootElement + '..number", java.util.Collection.class)).as("All elements match regex").are(')
!test.contains('cursor')
and:
try {