Merge branch '2.1.x'

This commit is contained in:
Marcin Grzejszczak
2019-11-06 14:21:54 +01:00
2 changed files with 65 additions and 4 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.cloud.contract.verifier.builder;
import java.math.BigDecimal;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
@@ -178,7 +179,7 @@ class JsonBodyVerificationBuilder implements BodyMethodGeneration, ClassVerifier
else {
String comparisonMethod = bodyMatcher.matchingType() == MatchingType.EQUALITY
? "isEqualTo" : "matches";
String classToCastTo = retrievedValue.getClass().getSimpleName() + ".class";
String classToCastTo = className(retrievedValue) + ".class";
String method = "assertThat(parsedJson.read(" + path + ", " + classToCastTo
+ "))." + comparisonMethod + "(" + valueAsParam + ")";
bb.addLine(postProcessJsonPathCall.apply(method));
@@ -186,9 +187,20 @@ class JsonBodyVerificationBuilder implements BodyMethodGeneration, ClassVerifier
addColonIfRequired(lineSuffix, bb);
}
private String className(Object retrievedValue) {
return retrievedValue.getClass().getName().startsWith("java.lang") ?
retrievedValue.getClass().getSimpleName() : retrievedValue.getClass().getName();
}
private String objectToString(Object value) {
return value instanceof Long ? String.valueOf(value).concat("L")
: String.valueOf(value);
if (value instanceof Long) {
return String.valueOf(value).concat("L");
} else if (value instanceof Double) {
return String.valueOf(value).concat("D");
} else if (value instanceof BigDecimal) {
return quotedAndEscaped(value.toString());
}
return String.valueOf(value);
}
protected String processIfTemplateIsPresent(String method,

View File

@@ -32,7 +32,6 @@ import org.springframework.cloud.contract.verifier.config.TestMode
import org.springframework.cloud.contract.verifier.dsl.wiremock.WireMockStubVerifier
import org.springframework.cloud.contract.verifier.file.ContractMetadata
import org.springframework.cloud.contract.verifier.util.SyntaxChecker
/**
* @author Jakub Kubrynski, codearte.io
* @author Tim Ysewyn
@@ -2963,4 +2962,54 @@ DocumentContext parsedJson = JsonPath.parse(json);
}
"webclient" | { properties.testMode = TestMode.WEBTESTCLIENT }
}
@Issue('#1139')
def 'should have a big decimal import for [#methodBuilderName]'() {
given:
Contract contractDsl = Contract.make {
request {
method 'POST'
url '/crystals/create'
headers {
header 'Content-Type' : 'application/json'
}
body(
amount: 200
)
bodyMatchers {
jsonPath('$.amount', byRegex('^[0-9]{1,3}$'))
}
}
response {
status 201
body(
amount: fromRequest().body('$.amount'),
price: 10100.0
)
bodyMatchers {
jsonPath('$.amount', byRegex('^\\d*$'))
jsonPath('$.price', byEquality())
}
}
}
methodBuilder()
when:
String test = singleTestGenerator(contractDsl)
then:
SyntaxChecker.tryToCompile(methodBuilderName, test)
then:
test.contains('''$.price", java.math.BigDecimal.class)).isEqualTo("10100.0")''')
where:
methodBuilderName | methodBuilder
"spock" | { properties.testFramework = TestFramework.SPOCK }
"testng" | { properties.testFramework = TestFramework.TESTNG }
"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 }
}
}