Allows passing of regex type (#832)

without this change if one does $(regex("[0-9]")) we have no knowledge of whether the result should be text or a number. What we do ATM is we always generate a String
with this change once can pass the type of regular expression and we will generate the concrete value of that given type

fixes gh-768
This commit is contained in:
Marcin Grzejszczak
2018-12-28 16:29:32 +01:00
committed by GitHub
parent 58bf533462
commit 56fbc11f62
40 changed files with 989 additions and 263 deletions

View File

@@ -16,12 +16,14 @@
package org.springframework.cloud.contract.verifier.wiremock
import org.springframework.cloud.contract.spec.Contract
import org.springframework.cloud.contract.verifier.dsl.wiremock.WireMockStubMapping
import org.springframework.cloud.contract.verifier.util.ContractVerifierDslConverter
import java.util.regex.Pattern
import spock.lang.Specification
import java.util.regex.Pattern
import org.springframework.cloud.contract.spec.Contract
import org.springframework.cloud.contract.spec.internal.RegexProperty
import org.springframework.cloud.contract.verifier.dsl.wiremock.WireMockStubMapping
import org.springframework.cloud.contract.verifier.util.ContractVerifierDslConverter
class WireMockToDslConverterSpec extends Specification {
@@ -148,7 +150,7 @@ class WireMockToDslConverterSpec extends Specification {
$groovyDsl
}""")
def b = expectedGroovyDsl
(a.first().request.url.clientValue as Pattern).pattern() == (b.request.url.clientValue as Pattern).pattern()
(a.first().request.url.clientValue as RegexProperty).pattern() == (b.request.url.clientValue as Pattern).pattern()
}
def 'should convert WireMock stub with response body containing integer'() {
@@ -401,7 +403,7 @@ class WireMockToDslConverterSpec extends Specification {
$groovyDsl
}""").first()
and:
(evaluatedGroovyDsl.request.body.clientValue as Pattern).pattern() == (expectedGroovyDsl.request.body.clientValue as Pattern).pattern()
(evaluatedGroovyDsl.request.body.clientValue as RegexProperty).pattern() == (expectedGroovyDsl.request.body.clientValue as Pattern).pattern()
}
def 'should convert WireMock stub with request body with equalToJson'() {
@@ -522,7 +524,7 @@ class WireMockToDslConverterSpec extends Specification {
$groovyDsl
}""").first()
and:
(evaluatedGroovyDsl.request.body.clientValue as Pattern).pattern() == (expectedGroovyDsl.request.body.clientValue as Pattern).pattern()
(evaluatedGroovyDsl.request.body.clientValue as RegexProperty).pattern() == (expectedGroovyDsl.request.body.clientValue as Pattern).pattern()
}
def 'should convert WireMock stub with priorities'() {

View File

@@ -15,6 +15,8 @@
*/
package org.springframework.cloud.contract.verifier.spec.pact
import java.util.regex.Pattern
import au.com.dius.pact.model.generators.Category
import au.com.dius.pact.model.generators.DateGenerator
import au.com.dius.pact.model.generators.DateTimeGenerator
@@ -30,13 +32,13 @@ import au.com.dius.pact.model.generators.TimeGenerator
import au.com.dius.pact.model.generators.UuidGenerator
import groovy.transform.CompileStatic
import groovy.transform.PackageScope
import org.springframework.cloud.contract.spec.internal.Body
import org.springframework.cloud.contract.spec.internal.DslProperty
import org.springframework.cloud.contract.spec.internal.OutputMessage
import org.springframework.cloud.contract.spec.internal.RegexProperty
import org.springframework.cloud.contract.verifier.util.ContentUtils
import java.util.regex.Pattern
/**
* @author Tim Ysewyn
* @Since 2.0.0
@@ -129,8 +131,9 @@ class ValueGeneratorConverter {
}
} else if (v instanceof DslProperty) {
traverse(v, dslPropertyValueProvider, path, generators, category)
} else if (v instanceof Pattern) {
switch (v.pattern()) {
} else if (v instanceof RegexProperty || v instanceof Pattern) {
RegexProperty regexProperty = new RegexProperty(v)
switch (regexProperty.pattern()) {
case INTEGER_PATTERN:
generators.addGenerator(category, path, new RandomIntGenerator(0, Integer.MAX_VALUE))
break
@@ -159,7 +162,7 @@ class ValueGeneratorConverter {
generators.addGenerator(category, path, RandomBooleanGenerator.INSTANCE)
break
default:
generators.addGenerator(category, path, new RegexGenerator(v.pattern()))
generators.addGenerator(category, path, new RegexGenerator(regexProperty.pattern()))
break
}
}