Implemented queryParameters matching for urlPattern and urlPath

This commit is contained in:
Denis Stepanov
2015-06-05 12:49:55 +02:00
parent 078ba57f23
commit 194d4a034a
8 changed files with 337 additions and 9 deletions

View File

@@ -2,6 +2,9 @@ package io.codearte.accurest.dsl
import groovy.transform.PackageScope
import groovy.transform.TypeChecked
import io.codearte.accurest.dsl.internal.ClientRequest
import io.codearte.accurest.dsl.internal.MatchingStrategy
import io.codearte.accurest.dsl.internal.QueryParameter
import io.codearte.accurest.dsl.internal.QueryParameters
import io.codearte.accurest.dsl.internal.Request
import java.util.regex.Pattern
@@ -27,12 +30,47 @@ class WiremockRequestStubStrategy extends BaseWiremockStubStrategy {
private Map<String, Object> buildRequestContent(ClientRequest request) {
return ([method : request?.method?.clientValue,
headers : buildClientRequestHeadersSection(request.headers)
] << appendUrl(request) << appendBody(request)).findAll { it.value }
] << appendUrl(request) << appendQueryParameters(request) << appendBody(request)).findAll { it.value }
}
private Map<String, Object> appendUrl(ClientRequest clientRequest) {
Object url = clientRequest?.url?.clientValue
return url instanceof Pattern ? [urlPattern: ((Pattern)url).pattern()] : [url: url]
def urlPath = clientRequest?.urlPath?.clientValue
if (urlPath) {
return [urlPath: urlPath]
}
def url = clientRequest?.url?.clientValue
return url instanceof Pattern ? [urlPattern: url.pattern()] : [url: url]
}
private Map<String, Object> appendQueryParameters(ClientRequest clientRequest) {
def queryParameters = clientRequest?.urlPath?.queryParameters ?: clientRequest?.url?.queryParameters
return queryParameters && !queryParameters.parameters.isEmpty() ?
[queryParameters: buildUrlPathQueryParameters(queryParameters)] : [:]
}
private Map buildUrlPathQueryParameters(QueryParameters queryParameters) {
return queryParameters.parameters.collectEntries { QueryParameter param ->
parseQueryParameter(param.name, param.clientValue)
}
}
protected Map parseQueryParameter(String name, MatchingStrategy matchingStrategy) {
return buildQueryParameter(name, matchingStrategy.clientValue, matchingStrategy.type)
}
protected Map parseQueryParameter(String name, Object value) {
return buildQueryParameter(name, value, MatchingStrategy.Type.EQUAL_TO)
}
protected Map parseQueryParameter(String name, Pattern pattern) {
return buildQueryParameter(name, pattern.pattern(), MatchingStrategy.Type.MATCHING)
}
private Map buildQueryParameter(String name, Object value, MatchingStrategy.Type type) {
if (value instanceof Pattern) {
value = value.pattern()
}
return [(name): [(type.name) : value]]
}
private Map<String, Object> appendBody(ClientRequest clientRequest) {

View File

@@ -0,0 +1,34 @@
package io.codearte.accurest.dsl.internal
import groovy.transform.CompileStatic
import groovy.transform.EqualsAndHashCode
import groovy.transform.ToString;
@EqualsAndHashCode(includeFields = true)
@ToString(includePackage = false, includeFields = true, ignoreNulls = true, includeNames = true)
@CompileStatic
public class MatchingStrategy extends DslProperty {
Type type
MatchingStrategy(Object value, Type type) {
super(value)
this.type = type
}
MatchingStrategy(DslProperty value, Type type) {
super(value.clientValue, value.serverValue)
this.type = type
}
enum Type {
EQUAL_TO("equalTo"), CONTAINS("contains"), MATCHING("matches"), NOT_MATCHING("doesNotMatch")
final String name
Type(name) {
this.name = name
}
}
}

View File

@@ -0,0 +1,29 @@
package io.codearte.accurest.dsl.internal;
import groovy.transform.CompileStatic;
import groovy.transform.EqualsAndHashCode;
import groovy.transform.ToString;
@EqualsAndHashCode(includeFields = true)
@ToString(includePackage = false, includeFields = true, ignoreNulls = true, includeNames = true)
@CompileStatic
public class QueryParameter extends DslProperty {
String name
QueryParameter(String name, DslProperty dslProperty) {
super(dslProperty.clientValue, dslProperty.serverValue)
this.name = name
}
QueryParameter(String name, MatchingStrategy matchingStrategy) {
super(matchingStrategy)
this.name = name
}
QueryParameter(String name, Object value) {
super(value)
this.name = name
}
}

View File

@@ -0,0 +1,45 @@
package io.codearte.accurest.dsl.internal
import groovy.transform.EqualsAndHashCode
import groovy.transform.ToString
import groovy.transform.TypeChecked
@EqualsAndHashCode(includeFields = true)
@ToString(includePackage = false, includeFields = true, ignoreNulls = true, includeNames = true)
@TypeChecked
public class QueryParameters {
List<QueryParameter> parameters = []
public void parameter(Map<String, Object> singleParameter) {
Map.Entry<String, Object> first = singleParameter.entrySet().first()
parameters << new QueryParameter(first?.key, first?.value)
}
public void parameter(String parameterName, Object parameterValue) {
parameters << new QueryParameter(parameterName, parameterValue)
}
def equalTo(Object value) {
return new MatchingStrategy(value, MatchingStrategy.Type.EQUAL_TO)
}
def containing(Object value) {
return new MatchingStrategy(value, MatchingStrategy.Type.CONTAINS)
}
def matching(Object value) {
return new MatchingStrategy(value, MatchingStrategy.Type.MATCHING)
}
def notMatching(Object value) {
return new MatchingStrategy(value, MatchingStrategy.Type.NOT_MATCHING)
}
void collect(Closure closure) {
parameters?.each {
parameter -> closure(parameter)
}
}
}

View File

@@ -11,6 +11,7 @@ class Request extends Common {
DslProperty method
Url url
UrlPath urlPath
Headers headers
Body body
@@ -20,6 +21,7 @@ class Request extends Common {
Request(Request request) {
this.method = request.method
this.url = request.url
this.urlPath = request.urlPath
this.headers = request.headers
this.body = request.body
}
@@ -32,7 +34,7 @@ class Request extends Common {
this.method = toDslProperty(method)
}
void url(String url) {
void url(Object url) {
this.url = new Url(url)
}
@@ -40,6 +42,38 @@ class Request extends Common {
this.url = new Url(url)
}
void url(Object url, @DelegatesTo(UrlPath) Closure closure) {
this.url = new Url(url)
closure.delegate = this.url
closure()
}
void url(DslProperty url, @DelegatesTo(UrlPath) Closure closure) {
this.url = new Url(url)
closure.delegate = this.url
closure()
}
void urlPath(String path) {
this.urlPath = new UrlPath(path)
}
void urlPath(DslProperty path) {
this.urlPath = new UrlPath(path)
}
void urlPath(String path, @DelegatesTo(UrlPath) Closure closure) {
this.urlPath = new UrlPath(path)
closure.delegate = urlPath
closure()
}
void urlPath(DslProperty path, @DelegatesTo(UrlPath) Closure closure) {
this.urlPath = new UrlPath(path)
closure.delegate = urlPath
closure()
}
void headers(@DelegatesTo(Headers) Closure closure) {
this.headers = new Headers()
closure.delegate = headers

View File

@@ -1,18 +1,28 @@
package io.codearte.accurest.dsl.internal
import groovy.transform.CompileStatic
import groovy.transform.EqualsAndHashCode
import groovy.transform.ToString
@ToString(includePackage = false, includeFields = true, includeNames = true)
@EqualsAndHashCode(includeFields = true)
@CompileStatic
class Url extends DslProperty {
Url(DslProperty bodyAsValue) {
super(bodyAsValue.clientValue, bodyAsValue.serverValue)
QueryParameters queryParameters
Url(DslProperty prop) {
super(prop.clientValue, prop.serverValue)
}
Url(String bodyAsValue) {
super(bodyAsValue)
Url(Object url) {
super(url)
}
void queryParameters(@DelegatesTo(QueryParameters) Closure closure) {
this.queryParameters = new QueryParameters()
closure.delegate = queryParameters
closure()
}
}

View File

@@ -0,0 +1,20 @@
package io.codearte.accurest.dsl.internal
import groovy.transform.CompileStatic;
import groovy.transform.EqualsAndHashCode
import groovy.transform.ToString;
@ToString(includePackage = false, includeFields = true, includeNames = true)
@EqualsAndHashCode(includeFields = true)
@CompileStatic
class UrlPath extends Url {
UrlPath(String path) {
super(path)
}
UrlPath(DslProperty path) {
super(path)
}
}

View File

@@ -1,4 +1,6 @@
package io.codearte.accurest.dsl
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
class WiremockGroovyDslSpec extends WiremockSpec {
@@ -313,6 +315,110 @@ class WiremockGroovyDslSpec extends WiremockSpec {
''')
}
def "should generate request with urlPath and queryParameters for client side"() {
given:
GroovyDsl groovyDsl = GroovyDsl.make {
request {
method 'GET'
urlPath($(client("users"), server("items"))) {
queryParameters {
parameter 'limit': $(client(equalTo("20")), server(containing("10")))
parameter 'offset': containing("10")
parameter 'filter': "email"
parameter 'sort': ~/^[0-9]{10}$/
parameter 'search': $(client(notMatching(~/^\/[0-9]{2}$/)), server(containing("10")))
parameter 'age': notMatching("^\\w*\$")
parameter 'name': matching("Denis.*")
}
}
}
response {
status 200
}
}
when:
def json = toWiremockClientJsonStub(groovyDsl)
then:
parseJson(json) == parseJson('''
{
"request": {
"method": "GET",
"urlPath":"users",
"queryParameters": {
"offset": {
"contains": "10"
},
"limit": {
"equalTo": "20"
},
"filter": {
"equalTo": "email"
},
"sort": {
"matches": "^[0-9]{10}$"
},
"search": {
"doesNotMatch": "^/[0-9]{2}$"
},
"age": {
"doesNotMatch": "^\\\\w*$"
},
"name": {
"matches": "Denis.*"
}
}
},
"response": {
"status": 200,
}
}
''')
and:
stubMappingIsValidWiremockStub(json)
}
def "should generate request with url and queryParameters for client side"() {
given:
GroovyDsl groovyDsl = GroovyDsl.make {
request {
method 'GET'
url(regex(/users\/[0-9]*/)) {
queryParameters {
parameter 'age': notMatching("^\\w*\$")
parameter 'name': matching("Denis.*")
}
}
}
response {
status 200
}
}
when:
def json = toWiremockClientJsonStub(groovyDsl)
then:
parseJson(json) == parseJson('''
{
"request": {
"method": "GET",
"urlPattern": "users/[0-9]*",
"queryParameters": {
"age": {
"doesNotMatch": "^\\\\w*$"
},
"name": {
"matches": "Denis.*"
}
}
},
"response": {
"status": 200,
}
}
''')
and:
stubMappingIsValidWiremockStub(json)
}
def "should generate stub with some headers section for client side"() {
given:
GroovyDsl groovyDsl = GroovyDsl.make {
@@ -347,4 +453,16 @@ class WiremockGroovyDslSpec extends WiremockSpec {
}
''')
}
def toJsonString(value) {
new JsonBuilder(value).toPrettyString()
}
def parseJson(json) {
new JsonSlurper().parseText(json)
}
def toWiremockClientJsonStub(groovyDsl) {
new WiremockStubStrategy(groovyDsl).toWiremockClientStub()
}
}