use method queryParam provided by MockMvc instead of appending Url
This commit is contained in:
committed by
Marcin Grzejszczak
parent
2ce55a9ac0
commit
a646ca5be7
@@ -1,17 +1,17 @@
|
||||
/*
|
||||
*Copyright 2013-2016 the original author or authors.
|
||||
* Copyright 2013-2016 the original author or authors.
|
||||
*
|
||||
*Licensed under the Apache License, Version 2.0 (the "License");
|
||||
*you may not use this file except in compliance with the License.
|
||||
*You may obtain a copy of the License at
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
*Unless required by applicable law or agreed to in writing, software
|
||||
*distributed under the License is distributed on an "AS IS" BASIS,
|
||||
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
*See the License for the specific language governing permissions and
|
||||
*limitations under the License.
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.contract.verifier.builder
|
||||
@@ -26,7 +26,6 @@ import org.springframework.cloud.contract.spec.internal.Header
|
||||
import org.springframework.cloud.contract.spec.internal.MatchingStrategy
|
||||
import org.springframework.cloud.contract.spec.internal.NamedProperty
|
||||
import org.springframework.cloud.contract.spec.internal.QueryParameter
|
||||
import org.springframework.cloud.contract.spec.internal.QueryParameters
|
||||
import org.springframework.cloud.contract.spec.internal.Response
|
||||
import org.springframework.cloud.contract.spec.internal.Url
|
||||
import org.springframework.cloud.contract.verifier.config.ContractVerifierConfigProperties
|
||||
@@ -52,6 +51,7 @@ abstract class RequestProcessingMethodBodyBuilder extends MethodBodyBuilder {
|
||||
protected final Request request
|
||||
protected final Response response
|
||||
private static final String DOUBLE_QUOTE = '"'
|
||||
private static final String QUERY_PARAM_METHOD = 'queryParam'
|
||||
|
||||
RequestProcessingMethodBodyBuilder(Contract stubDefinition, ContractVerifierConfigProperties configProperties) {
|
||||
super(configProperties)
|
||||
@@ -109,47 +109,31 @@ abstract class RequestProcessingMethodBodyBuilder extends MethodBodyBuilder {
|
||||
bb.indent()
|
||||
|
||||
Url url = getUrl(request)
|
||||
|
||||
addUrlParameters(url, bb)
|
||||
addQueryParameters(url, bb)
|
||||
addUrl(url, bb)
|
||||
addColonIfRequired(bb)
|
||||
|
||||
bb.unindent()
|
||||
}
|
||||
|
||||
@TypeChecked(TypeCheckingMode.SKIP)
|
||||
private addUrlParameters(Url buildUrl, BlockBuilder bb) {
|
||||
protected addQueryParameters(Url buildUrl, BlockBuilder bb) {
|
||||
if(hasQueryParams(buildUrl)){
|
||||
List<QueryParameter> queryParameters = buildUrl.queryParameters.parameters.findAll(this.&allowedQueryParameter)
|
||||
for (queryParam in queryParameters) {
|
||||
addQueryParameter(queryParam, bb)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@TypeChecked(TypeCheckingMode.SKIP)
|
||||
protected addQueryParameter(QueryParameter queryParam, BlockBuilder bb) {
|
||||
bb.addLine(/.${QUERY_PARAM_METHOD}(${DOUBLE_QUOTE}${queryParam.name}${DOUBLE_QUOTE},${DOUBLE_QUOTE}${resolveParamValue(queryParam).toString()}${DOUBLE_QUOTE})/)
|
||||
}
|
||||
|
||||
protected addUrl(Url buildUrl, BlockBuilder bb){
|
||||
String url =MapConverter.getTestSideValues(buildUrl)
|
||||
String method = request.method.serverValue.toString().toLowerCase()
|
||||
|
||||
if(hasQueryParams(buildUrl)){
|
||||
List<QueryParameter> queryParameters = buildUrl.queryParameters.parameters.findAll(this.&allowedQueryParameter)
|
||||
bb.addLine(/.${method}(${DOUBLE_QUOTE}${url}?${buildQueryParameterTemplates(queryParameters)}${DOUBLE_QUOTE},${buildQueryParameterValues(queryParameters)})/)
|
||||
}
|
||||
else
|
||||
bb.addLine(/.${method}(${DOUBLE_QUOTE}${url}${DOUBLE_QUOTE})/)
|
||||
}
|
||||
|
||||
|
||||
@TypeChecked(TypeCheckingMode.SKIP)
|
||||
protected String buildQueryParameterTemplates(List<QueryParameters> queryParameters){
|
||||
final String open = '{'
|
||||
final String close ='}'
|
||||
String params = queryParameters
|
||||
.inject([] as List<String>) { List<String> result, QueryParameter param ->
|
||||
result << "${param.name}=${open}${param.name}${close}"
|
||||
}
|
||||
.join('&')
|
||||
return "$params"
|
||||
}
|
||||
|
||||
@TypeChecked(TypeCheckingMode.SKIP)
|
||||
protected String buildQueryParameterValues(List<QueryParameters> queryParameters){
|
||||
String params = queryParameters
|
||||
.inject([] as List<String>) { List<String> result, QueryParameter param ->
|
||||
result << "${DOUBLE_QUOTE}${resolveParamValue(param).toString()}${DOUBLE_QUOTE}"
|
||||
}
|
||||
.join(',')
|
||||
return "$params"
|
||||
bb.addLine(/.${method}(${DOUBLE_QUOTE}${url}${DOUBLE_QUOTE})/)
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -559,7 +559,15 @@ class MockMvcMethodBodyBuilderSpec extends Specification implements WireMockStub
|
||||
builder.appendTo(blockBuilder)
|
||||
def test = blockBuilder.toString()
|
||||
then:
|
||||
test.contains('get("/users?limit={limit}&offset={offset}&filter={filter}&sort={sort}&search={search}&age={age}&name={name}&email={email}","10","20","email","name","55","99","Denis.Stepanov","bob@email.com")')
|
||||
test.contains('''.queryParam("limit","10")''')
|
||||
test.contains('''.queryParam("offset","20")''')
|
||||
test.contains('''.queryParam("filter","email")''')
|
||||
test.contains('''.queryParam("sort","name")''')
|
||||
test.contains('''.queryParam("search","55")''')
|
||||
test.contains('''.queryParam("age","99")''')
|
||||
test.contains('''.queryParam("name","Denis.Stepanov")''')
|
||||
test.contains('''.queryParam("email","bob@email.com")''')
|
||||
test.contains('''.get("/users")''')
|
||||
test.contains('assertThatJson(parsedJson).field("property1").isEqualTo("a")')
|
||||
test.contains('assertThatJson(parsedJson).field("property2").isEqualTo("b")')
|
||||
and:
|
||||
@@ -607,7 +615,15 @@ class MockMvcMethodBodyBuilderSpec extends Specification implements WireMockStub
|
||||
builder.appendTo(blockBuilder)
|
||||
def test = blockBuilder.toString()
|
||||
then:
|
||||
test.contains('get("/foo/123456?limit={limit}&offset={offset}&filter={filter}&sort={sort}&search={search}&age={age}&name={name}&email={email}","10","20","email","name","55","99","Denis.Stepanov","bob@email.com")')
|
||||
test.contains('''.queryParam("limit","10")''')
|
||||
test.contains('''.queryParam("offset","20")''')
|
||||
test.contains('''.queryParam("filter","email")''')
|
||||
test.contains('''.queryParam("sort","name")''')
|
||||
test.contains('''.queryParam("search","55")''')
|
||||
test.contains('''.queryParam("age","99")''')
|
||||
test.contains('''.queryParam("name","Denis.Stepanov")''')
|
||||
test.contains('''.queryParam("email","bob@email.com")''')
|
||||
test.contains('''.get("/foo/123456")''')
|
||||
test.contains('assertThatJson(parsedJson).field("property1").isEqualTo("a")')
|
||||
test.contains('assertThatJson(parsedJson).field("property2").isEqualTo("b")')
|
||||
and:
|
||||
|
||||
Reference in New Issue
Block a user