Parse ExecutionProperty in queryParameters (#1646)

Fixes gh-854
This commit is contained in:
mariuszdb
2021-04-26 09:04:25 +02:00
committed by GitHub
parent c323e3e105
commit e73d6526bf
3 changed files with 70 additions and 5 deletions

View File

@@ -75,8 +75,8 @@ class JaxRsUrlPathWhen implements When, JaxRsAcceptor, QueryParamsResolver {
.iterator();
while (iterator.hasNext()) {
QueryParameter param = iterator.next();
String text = ".queryParam(\"" + param.getName() + "\", "
+ this.bodyParser.quotedShortText(resolveParamValue(param)) + ")";
String queryParamValue = getQueryParamValue(param);
String text = ".queryParam(\"" + param.getName() + "\", " + queryParamValue + ")";
if (iterator.hasNext()) {
this.blockBuilder.addLine(text);
}
@@ -86,6 +86,14 @@ class JaxRsUrlPathWhen implements When, JaxRsAcceptor, QueryParamsResolver {
}
}
private String getQueryParamValue(QueryParameter param) {
Object serverValue = param.getServerValue();
if (serverValue instanceof ExecutionProperty) {
return ((ExecutionProperty) serverValue).getExecutionCommand();
}
return this.bodyParser.quotedShortText(resolveParamValue(param));
}
/**
* @return {@code true} if the query parameter is allowed
*/

View File

@@ -20,6 +20,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.cloud.contract.spec.internal.ExecutionProperty;
import org.springframework.cloud.contract.spec.internal.MatchingStrategy;
import org.springframework.cloud.contract.spec.internal.QueryParameter;
import org.springframework.cloud.contract.spec.internal.Request;
@@ -85,10 +86,17 @@ class MockMvcQueryParamsWhen implements When, MockMvcAcceptor, QueryParamsResolv
}
private String addQueryParameter(QueryParameter queryParam) {
String queryParamValue = getQueryParamValue(queryParam);
return "." + QUERY_PARAM_METHOD + "(" + this.bodyParser.quotedLongText(queryParam.getName()) + ","
+ this.bodyParser
.quotedLongText(resolveParamValue(MapConverter.getTestSideValuesForNonBody(queryParam)))
+ ")";
+ queryParamValue + ")";
}
private String getQueryParamValue(QueryParameter queryParam) {
Object serverValue = queryParam.getServerValue();
if (serverValue instanceof ExecutionProperty) {
return ((ExecutionProperty) serverValue).getExecutionCommand();
}
return this.bodyParser.quotedLongText(resolveParamValue(MapConverter.getTestSideValuesForNonBody(queryParam)));
}
@Override

View File

@@ -1857,6 +1857,55 @@ response:
}
}
@Issue("#854")
def "should call execute in queryParameters [#methodBuilderName]"() {
given:
Contract contractDsl = Contract.make {
request {
method 'POST'
urlPath("/rest/something") {
queryParameters {
parameter('someHashCode': $(
consumer(regex(anInteger())),
producer(execute("hashCode()")))
)
}
}
}
response {
status OK()
}
}
methodBuilder()
when:
String test = singleTestGenerator(contractDsl)
then:
SyntaxChecker.tryToCompileWithoutCompileStatic(methodBuilderName, test)
test.contains('''.queryParam("someHashCode",hashCode())''') | test.contains('''.queryParam("someHashCode", hashCode())''')
and:
stubMappingIsValidWireMockStub(contractDsl)
where:
methodBuilderName | methodBuilder
"spock" | {
properties.testFramework = TestFramework.SPOCK
}
"mockmvc" | {
properties.testMode = TestMode.MOCKMVC
}
"jaxrs-spock" | {
properties.testFramework = TestFramework.SPOCK; properties.testMode = TestMode.JAXRSCLIENT
}
"jaxrs" | {
properties.testFramework = TestFramework.JUNIT; properties.testMode = TestMode.JAXRSCLIENT
}
"webclient" | {
properties.testMode = TestMode.WEBTESTCLIENT
}
"testNG" | {
properties.testFramework = TestFramework.TESTNG
}
}
@Issue("#1262")
def "should work with the timeout flag for groovy [#methodBuilderName]"() {
given: