Fixed the way regex entries are generated for message bodies; fixes gh-858

This commit is contained in:
Marcin Grzejszczak
2019-01-22 09:14:08 +01:00
parent a2d6137340
commit 8ebc749d2f
5 changed files with 29 additions and 4 deletions

View File

@@ -20,6 +20,7 @@ import java.util.regex.Pattern
import groovy.transform.CompileStatic
import groovy.transform.ToString
import repackaged.nl.flotsam.xeger.Xeger
/**
* Represents a property that may or may not be there
@@ -28,7 +29,7 @@ import groovy.transform.ToString
*/
@CompileStatic
@ToString(includePackage = false, includeNames = true)
class OptionalProperty implements Serializable {
class OptionalProperty implements Serializable, CanBeDynamic {
final Object value
OptionalProperty(Object value) {
@@ -56,4 +57,9 @@ class OptionalProperty implements Serializable {
String toString() {
return optionalPattern()
}
@Override
Object generateConcreteValue() {
return new Xeger(optionalPattern()).generate()
}
}

View File

@@ -33,7 +33,7 @@ import repackaged.nl.flotsam.xeger.Xeger
@TypeChecked
@EqualsAndHashCode
@ToString(includePackage = false, includeNames = true)
class RegexProperty extends DslProperty {
class RegexProperty extends DslProperty implements CanBeDynamic {
final Pattern pattern
private final Class clazz
@@ -169,4 +169,9 @@ class RegexProperty extends DslProperty {
String toString() {
return this.pattern()
}
@Override
Object generateConcreteValue() {
return generate()
}
}

View File

@@ -160,8 +160,10 @@ class StubRunnerExecutorSpec extends Specification {
then:
triggered
1 * messageVerifier.send({ it ->
println "Body <${it}>"
!it.toString().contains("cursor")
}, { Map map ->
println "Headers <${map}>"
!map.values().any { it.toString().contains("cursor")}
}, _)
cleanup:

View File

@@ -21,7 +21,8 @@ org.springframework.cloud.contract.spec.Contract.make {
"firstName" : $(regex(nonEmpty())),
"middleName": $(optional(regex(nonEmpty()))),
"lastName" : $(regex(nonEmpty())),
"version" : $(producer(regex(number())), consumer(0l))
"version" : $(producer(regex(number())), consumer(0l)),
"uid" : $(producer(regex(uuid())))
])
}
}

View File

@@ -16,10 +16,15 @@
package org.springframework.cloud.contract.verifier.util
import java.util.regex.Pattern
import groovy.json.JsonOutput
import groovy.json.StringEscapeUtils
import groovy.transform.CompileStatic
import org.springframework.cloud.contract.spec.internal.CanBeDynamic
import org.springframework.cloud.contract.spec.internal.DslProperty
import org.springframework.cloud.contract.spec.internal.RegexProperty
import static ContentUtils.extractValue
/**
@@ -78,7 +83,13 @@ class BodyExtractor {
} else if (bodyValue instanceof DslProperty) {
return extractClientValueFromBody(bodyValue.clientValue)
} else {
return MapConverter.transformValues(bodyValue, { it instanceof DslProperty ? it.clientValue : it })
return MapConverter.transformValues(bodyValue, {
Object prop = it instanceof DslProperty ? it.clientValue : it
if (prop instanceof CanBeDynamic || prop instanceof Pattern) {
return new RegexProperty(prop).generateConcreteValue()
}
return prop
})
}
}
}