Generating values if only one regex is passed

with this change if in the DSL the regex is not in the valid place - an exception will be thrown
if the regex is passed properly but there is no other value (client / server) - it will get generated

fixes #32
This commit is contained in:
Marcin Grzejszczak
2016-07-15 00:19:51 +02:00
parent 8752cf2324
commit 66bc04a6da
12 changed files with 149 additions and 19 deletions

View File

@@ -17,6 +17,11 @@
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
</dependency>
<dependency>
<groupId>dk.brics.automaton</groupId>
<artifactId>automaton</artifactId>
<version>1.11-8</version>
</dependency>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>

View File

@@ -81,15 +81,19 @@ class Common {
return new DslProperty(client.clientValue, server.serverValue)
}
DslProperty value(Object value) {
return new DslProperty(value)
}
DslProperty value(ServerDslProperty server, ClientDslProperty client) {
assertThatSidesMatch(client.clientValue, server.serverValue)
return new DslProperty(client.clientValue, server.serverValue)
}
DslProperty value(DslProperty value) {
return value
}
DslProperty value(Object value) {
return new DslProperty(value)
}
DslProperty $(ClientDslProperty client, ServerDslProperty server) {
return value(client, server)
}

View File

@@ -20,7 +20,9 @@ import groovy.transform.CompileStatic
import groovy.transform.EqualsAndHashCode
import groovy.transform.ToString
import groovy.transform.TypeChecked
import repackaged.nl.flotsam.xeger.Xeger
import java.util.regex.Pattern
/**
* Represents the request side of the HTTP communication
*
@@ -172,6 +174,30 @@ class Request extends Common {
throw new IllegalStateException("Optional can be used only for the stub side of the request!")
}
DslProperty value(ClientDslProperty client) {
Object clientValue = client.clientValue
if (client.clientValue instanceof Pattern) {
clientValue = new Xeger(((Pattern)client.clientValue).pattern()).generate()
}
return new DslProperty(client.clientValue, clientValue)
}
@Override
DslProperty value(ClientDslProperty client, ServerDslProperty server) {
if (server.clientValue instanceof Pattern) {
throw new IllegalStateException("You can't have a regular expression for the request on the server side")
}
return super.value(client, server)
}
@Override
DslProperty value(ServerDslProperty server, ClientDslProperty client) {
if (server.clientValue instanceof Pattern) {
throw new IllegalStateException("You can't have a regular expression for the request on the server side")
}
return super.value(server, client)
}
}
@CompileStatic

View File

@@ -20,6 +20,9 @@ import groovy.transform.CompileStatic
import groovy.transform.EqualsAndHashCode
import groovy.transform.ToString
import groovy.transform.TypeChecked
import repackaged.nl.flotsam.xeger.Xeger
import java.util.regex.Pattern
/**
* Represents the response side of the HTTP communication
@@ -83,6 +86,30 @@ class Response extends Common {
void assertThatSidesMatch(OptionalProperty stubSide, Object testSide) {
throw new IllegalStateException("Optional can be used only in the test side of the response!")
}
DslProperty value(ServerDslProperty server) {
Object value = server.clientValue
if (server.clientValue instanceof Pattern) {
value = new Xeger(((Pattern)server.clientValue).pattern()).generate()
}
return new DslProperty(server.clientValue, value)
}
@Override
DslProperty value(ClientDslProperty client, ServerDslProperty server) {
if (client.clientValue instanceof Pattern) {
throw new IllegalStateException("You can't have a regular expression for the response on the client side")
}
return super.value(client, server)
}
@Override
DslProperty value(ServerDslProperty server, ClientDslProperty client) {
if (client.clientValue instanceof Pattern) {
throw new IllegalStateException("You can't have a regular expression for the response on the client side")
}
return super.value(server, client)
}
}
@CompileStatic

View File

@@ -19,16 +19,16 @@
* released to any central repository.
*
*/
package nl.flotsam.xeger;
package repackaged.nl.flotsam.xeger;
import java.util.List;
import java.util.Random;
import dk.brics.automaton.Automaton;
import dk.brics.automaton.RegExp;
import dk.brics.automaton.State;
import dk.brics.automaton.Transition;
import java.util.List;
import java.util.Random;
/**
* An object that will generate text from a regular expression. In a way, it's the opposite of a regular expression
* matcher: an instance of this class will produce text that is guaranteed to match the regular expression passed in.
@@ -53,7 +53,7 @@ public class Xeger {
}
/**
* As {@link nl.flotsam.xeger.Xeger#Xeger(String, java.util.Random)}, creating a {@link java.util.Random} instance
* As {@link Xeger#Xeger(String, java.util.Random)}, creating a {@link java.util.Random} instance
* implicityly.
*
* @param regex as string

View File

@@ -0,0 +1,37 @@
package org.springframework.cloud.contract.spec.internal
import spock.lang.Specification
/**
* @author Marcin Grzejszczak
*/
class RequestSpec extends Specification {
def 'should throw exception when on request side a value contains regex for server'() {
given:
Request request = new Request()
when:
request.with {
value(client("foo"), server(regex("foo")))
}
then:
thrown(IllegalStateException)
when:
request.with {
value(server(regex("foo")), client("foo"))
}
then:
thrown(IllegalStateException)
}
def 'should generate a value if only regex is passed for client'() {
given:
Request request = new Request()
DslProperty property
when:
request.with {
property = value(client(regex("[0-9]{5}")))
}
then:
(property.serverValue as String).matches(/[0-9]{5}/)
}
}

View File

@@ -0,0 +1,37 @@
package org.springframework.cloud.contract.spec.internal
import spock.lang.Specification
/**
* @author Marcin Grzejszczak
*/
class ResponseSpec extends Specification {
def 'should throw exception when on response side a value contains regex for client'() {
given:
Response response = new Response()
when:
response.with {
value(server("foo"), client(regex("foo")))
}
then:
thrown(IllegalStateException)
when:
response.with {
value(client(regex("foo")), server("foo"))
}
then:
thrown(IllegalStateException)
}
def 'should generate a value if only regex is passed for server'() {
given:
Response request = new Response()
DslProperty property
when:
request.with {
property = value(server(regex("[0-9]{5}")))
}
then:
(property.serverValue as String).matches(/[0-9]{5}/)
}
}

View File

@@ -15,7 +15,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.flotsam.xeger;
package repackaged.nl.flotsam.xeger;
import org.junit.Test;

View File

@@ -15,7 +15,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.flotsam.xeger;
package repackaged.nl.flotsam.xeger;
import org.hamcrest.Matchers;
import org.junit.Test;

View File

@@ -30,11 +30,6 @@
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock</artifactId>
</dependency>
<dependency>
<groupId>dk.brics.automaton</groupId>
<artifactId>automaton</artifactId>
<version>1.11-8</version>
</dependency>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>

View File

@@ -23,7 +23,7 @@ import groovy.json.JsonSlurper
import groovy.transform.CompileDynamic
import groovy.xml.XmlUtil
import org.springframework.cloud.contract.spec.Contract
import nl.flotsam.xeger.Xeger
import repackaged.nl.flotsam.xeger.Xeger
import java.nio.charset.StandardCharsets

View File

@@ -865,8 +865,7 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie
}
}
then:
def e = thrown(IllegalStateException)
e.message.contains "Query parameter 'age' can't be a pattern for the server side"
thrown(IllegalStateException)
}
def "should not allow query parameter unresolvable for a server value"() {