Fixed invalid multiline json reading
This commit is contained in:
@@ -33,7 +33,7 @@ import java.nio.charset.Charset
|
||||
class FromFileProperty implements Serializable {
|
||||
|
||||
final File file
|
||||
final Charset charset
|
||||
final String charset
|
||||
final Class type
|
||||
|
||||
FromFileProperty(File file, Class type) {
|
||||
@@ -43,7 +43,7 @@ class FromFileProperty implements Serializable {
|
||||
FromFileProperty(File file, Class type, Charset charset) {
|
||||
this.file = file
|
||||
this.type = type
|
||||
this.charset = charset
|
||||
this.charset = charset.toString()
|
||||
}
|
||||
|
||||
boolean isString() {
|
||||
|
||||
@@ -91,7 +91,8 @@ class JUnitMessagingMethodBodyBuilder extends MessagingMethodBodyBuilder {
|
||||
return "assertThat(response.getPayloadAsByteArray()).isEqualTo(" +
|
||||
readBytesFromFileString(value, CommunicationType.RESPONSE) + ")"
|
||||
}
|
||||
return getResponseBodyPropertyComparisonString(property, value.asString())
|
||||
return "assertThat(response.getPayload()).isEqualTo(" +
|
||||
readStringFromFileString(value, CommunicationType.RESPONSE) +")"
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -126,7 +126,7 @@ abstract class JUnitMethodBodyBuilder extends RequestProcessingMethodBodyBuilder
|
||||
return "assertThat(response.getBody().asByteArray()).isEqualTo(" +
|
||||
readBytesFromFileString(value, CommunicationType.RESPONSE) + ")"
|
||||
}
|
||||
return getResponseBodyPropertyComparisonString(property, value.asString())
|
||||
return "assertThat(response.getBody()).isEqualTo(" + readStringFromFileString(value, CommunicationType.RESPONSE) + ")"
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -164,6 +164,15 @@ class JaxRsClientJUnitMethodBodyBuilder extends JUnitMethodBodyBuilder {
|
||||
bb.addLine("assertThat(response.getStatus()).isEqualTo($response.status.serverValue);")
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getResponseBodyPropertyComparisonString(String property, FromFileProperty value) {
|
||||
if (value.isByte()) {
|
||||
return "assertThat(response.readEntity(byte[].class)).isEqualTo(" +
|
||||
readBytesFromFileString(value, CommunicationType.RESPONSE) + ")"
|
||||
}
|
||||
return "assertThat(response.readEntity(String.class)).isEqualTo(" + readStringFromFileString(value, CommunicationType.RESPONSE) + ")"
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void validateResponseHeadersBlock(BlockBuilder bb) {
|
||||
response.headers?.executeForEachHeader { Header header ->
|
||||
|
||||
@@ -77,6 +77,15 @@ class JaxRsClientSpockMethodRequestProcessingBodyBuilder extends SpockMethodRequ
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getResponseBodyPropertyComparisonString(String property, FromFileProperty value) {
|
||||
if (value.isByte()) {
|
||||
return "response.readEntity(byte[]) == " +
|
||||
readBytesFromFileString(value, CommunicationType.RESPONSE)
|
||||
}
|
||||
return "response.readEntity(String) == " + readStringFromFileString(value, CommunicationType.RESPONSE)
|
||||
}
|
||||
|
||||
protected void appendRequestWithRequiredResponseContentType(BlockBuilder bb) {
|
||||
String acceptHeader = getHeader("Accept")
|
||||
if (acceptHeader) {
|
||||
|
||||
@@ -169,7 +169,8 @@ class SpockMessagingMethodBodyBuilder extends MessagingMethodBodyBuilder {
|
||||
return "response.payloadAsByteArray == " +
|
||||
readBytesFromFileString(value, CommunicationType.RESPONSE)
|
||||
}
|
||||
return getResponseBodyPropertyComparisonString(property, value.asString())
|
||||
return "response.payload == " +
|
||||
readStringFromFileString(value, CommunicationType.RESPONSE)
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -113,7 +113,7 @@ abstract class SpockMethodRequestProcessingBodyBuilder extends RequestProcessing
|
||||
return "response.body.asByteArray() == " +
|
||||
readBytesFromFileString(value, CommunicationType.RESPONSE)
|
||||
}
|
||||
return getResponseBodyPropertyComparisonString(property, value.asString())
|
||||
return "response.body == " + readStringFromFileString(value, CommunicationType.RESPONSE)
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -90,7 +90,9 @@ class WireMockResponseStubStrategy extends BaseWireMockStubStrategy {
|
||||
if (contentType == ContentType.UNKNOWN) {
|
||||
contentType = recognizeContentTypeFromContent(body)
|
||||
}
|
||||
if (body instanceof FromFileProperty && body.isByte()) {
|
||||
if (body instanceof byte[]) {
|
||||
builder.withBody(body)
|
||||
} else if (body instanceof FromFileProperty && body.isByte()) {
|
||||
builder.withBody(body.asBytes())
|
||||
} else {
|
||||
builder.withBody(parseBody(body, contentType))
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.cloud.contract.verifier.util
|
||||
|
||||
import groovy.json.JsonSlurper
|
||||
import org.springframework.cloud.contract.spec.internal.DslProperty
|
||||
import org.springframework.cloud.contract.spec.internal.FromFileProperty
|
||||
import org.springframework.cloud.contract.verifier.template.HandlebarsTemplateProcessor
|
||||
import org.springframework.cloud.contract.verifier.template.TemplateProcessor
|
||||
/**
|
||||
@@ -130,6 +131,8 @@ class MapConverter {
|
||||
}
|
||||
return it
|
||||
})
|
||||
} else if (it instanceof FromFileProperty) {
|
||||
return it.isByte() ? it.asBytes() : it.asString()
|
||||
}
|
||||
return it
|
||||
}
|
||||
|
||||
@@ -785,6 +785,43 @@ DocumentContext parsedJson = JsonPath.parse(json);
|
||||
WebTestClientJUnitMethodBodyBuilder.simpleName | { Contract dsl -> new WebTestClientJUnitMethodBodyBuilder(dsl, properties, classDataForMethod) }
|
||||
}
|
||||
|
||||
def 'should work with files that have new lines [#methodBuilderName]'() {
|
||||
given:
|
||||
Contract contractDsl = Contract.make {
|
||||
request {
|
||||
method('PUT')
|
||||
headers {
|
||||
contentType(applicationJson())
|
||||
}
|
||||
body(file("classpath/request.json"))
|
||||
url("/1")
|
||||
}
|
||||
response {
|
||||
status OK()
|
||||
body(file("classpath/response.json"))
|
||||
headers {
|
||||
contentType(textPlain())
|
||||
}
|
||||
}
|
||||
}
|
||||
MethodBodyBuilder builder = methodBuilder(contractDsl)
|
||||
BlockBuilder blockBuilder = new BlockBuilder(' ')
|
||||
when:
|
||||
builder.appendTo(blockBuilder)
|
||||
then:
|
||||
String test = blockBuilder.toString()
|
||||
SyntaxChecker.tryToCompileWithoutCompileStatic(methodBuilderName, test)
|
||||
and:
|
||||
stubMappingIsValidWireMockStub(contractDsl)
|
||||
where:
|
||||
methodBuilderName | methodBuilder
|
||||
HttpSpockMethodRequestProcessingBodyBuilder.simpleName | { Contract dsl -> new HttpSpockMethodRequestProcessingBodyBuilder(dsl, properties, classDataForMethod) }
|
||||
MockMvcJUnitMethodBodyBuilder.simpleName | { Contract dsl -> new MockMvcJUnitMethodBodyBuilder(dsl, properties, classDataForMethod) }
|
||||
JaxRsClientSpockMethodRequestProcessingBodyBuilder.simpleName | { Contract dsl -> new JaxRsClientSpockMethodRequestProcessingBodyBuilder(dsl, properties, classDataForMethod) }
|
||||
JaxRsClientJUnitMethodBodyBuilder.simpleName | { Contract dsl -> new JaxRsClientJUnitMethodBodyBuilder(dsl, properties, classDataForMethod) }
|
||||
WebTestClientJUnitMethodBodyBuilder.simpleName | { Contract dsl -> new WebTestClientJUnitMethodBodyBuilder(dsl, properties, classDataForMethod) }
|
||||
}
|
||||
|
||||
@Issue('#509')
|
||||
def 'classToCheck() should return class of object'() {
|
||||
given:
|
||||
@@ -898,6 +935,7 @@ DocumentContext parsedJson = JsonPath.parse(json);
|
||||
String test = blockBuilder.toString()
|
||||
requestMatcher(test)
|
||||
responseMatcher(test)
|
||||
SyntaxChecker.tryToCompile(methodBuilderName, test)
|
||||
where:
|
||||
methodBuilderName | methodBuilder | requestMatcher | responseMatcher
|
||||
HttpSpockMethodRequestProcessingBodyBuilder.simpleName | { Contract dsl -> new HttpSpockMethodRequestProcessingBodyBuilder(dsl, properties, classDataForMethod) } | { String string ->
|
||||
@@ -913,12 +951,12 @@ DocumentContext parsedJson = JsonPath.parse(json);
|
||||
JaxRsClientSpockMethodRequestProcessingBodyBuilder.simpleName | { Contract dsl -> new JaxRsClientSpockMethodRequestProcessingBodyBuilder(dsl, properties, classDataForMethod) } | { String string ->
|
||||
string.contains('entity(fileToBytes(this, "some_method_request_request.pdf")')
|
||||
} | { String string ->
|
||||
string.contains('response.body.asByteArray() == fileToBytes(this, "some_method_response_response.pdf")')
|
||||
string.contains('response.readEntity(byte[]) == fileToBytes(this, "some_method_response_response.pdf")')
|
||||
}
|
||||
JaxRsClientJUnitMethodBodyBuilder.simpleName | { Contract dsl -> new JaxRsClientJUnitMethodBodyBuilder(dsl, properties, classDataForMethod) } | { String string ->
|
||||
string.contains('entity(fileToBytes(this, "some_method_request_request.pdf")')
|
||||
} | { String string ->
|
||||
string.contains('assertThat(response.getBody().asByteArray()).isEqualTo(fileToBytes(this, "some_method_response_response.pdf"));')
|
||||
string.contains('assertThat(response.readEntity(byte[].class)).isEqualTo(fileToBytes(this, "some_method_response_response.pdf"));')
|
||||
}
|
||||
WebTestClientJUnitMethodBodyBuilder.simpleName | { Contract dsl -> new WebTestClientJUnitMethodBodyBuilder(dsl, properties, classDataForMethod) } | { String string ->
|
||||
string.contains('.body(fileToBytes(this, "some_method_request_request.pdf"));')
|
||||
|
||||
@@ -614,7 +614,7 @@ class SingleTestGeneratorSpec extends Specification {
|
||||
and:
|
||||
String test = new File(temp, "a/b/ContractVerifier${getTestName(testFramework)}").text
|
||||
test.contains('readFromFile_request_request.json')
|
||||
test.contains('RESPONSE')
|
||||
test.contains('readFromFile_response_response.json')
|
||||
where:
|
||||
testFramework << [JUNIT, JUNIT5, SPOCK]
|
||||
}
|
||||
@@ -639,7 +639,7 @@ class SingleTestGeneratorSpec extends Specification {
|
||||
and:
|
||||
String test = new File(temp, "a/b/ContractVerifier${getTestName(testFramework)}").text
|
||||
test.contains('readFromFile_request_request.json')
|
||||
test.contains('RESPONSE')
|
||||
test.contains('readFromFile_response_response.json')
|
||||
where:
|
||||
testFramework << [JUNIT, JUNIT5, SPOCK]
|
||||
}
|
||||
@@ -664,7 +664,7 @@ class SingleTestGeneratorSpec extends Specification {
|
||||
and:
|
||||
String test = new File(temp, "a/b/ContractVerifier${getTestName(testFramework)}").text
|
||||
test.contains('readFromFile_request_request.json')
|
||||
test.contains('RESPONSE')
|
||||
test.contains('readFromFile_response_response.json')
|
||||
where:
|
||||
testFramework << [JUNIT, JUNIT5, SPOCK]
|
||||
}
|
||||
@@ -689,7 +689,7 @@ class SingleTestGeneratorSpec extends Specification {
|
||||
and:
|
||||
String test = new File(temp, "a/b/ContractVerifier${getTestName(testFramework)}").text
|
||||
test.contains('readFromFile_request_request.json')
|
||||
test.contains('RESPONSE')
|
||||
test.contains('readFromFile_response_response.json')
|
||||
where:
|
||||
testFramework << [JUNIT, JUNIT5, SPOCK]
|
||||
}
|
||||
@@ -713,7 +713,7 @@ class SingleTestGeneratorSpec extends Specification {
|
||||
and:
|
||||
String test = new File(temp, "org/springframework/cloud/contract/verifier/tests/ContractVerifier${getTestName(testFramework)}").text
|
||||
test.contains('readFromFile_request_request.json')
|
||||
test.contains('RESPONSE')
|
||||
test.contains('readFromFile_response_response.json')
|
||||
where:
|
||||
testFramework << [JUNIT, JUNIT5, SPOCK]
|
||||
}
|
||||
|
||||
@@ -67,6 +67,7 @@ class SyntaxChecker {
|
||||
"${RestAssuredMockMvc.name}.when",
|
||||
"${RestAssured.name}.*",
|
||||
"${Entity.name}.*",
|
||||
"${ContractVerifierUtil.name}.fileToBytes",
|
||||
"${ContractVerifierMessagingUtil.name}.headers",
|
||||
"${JsonAssertion.name}.assertThatJson",
|
||||
"${SpringCloudContractAssertions.name}.assertThat"
|
||||
@@ -75,6 +76,7 @@ class SyntaxChecker {
|
||||
private static final String WEB_TEST_CLIENT_STATIC_IMPORTS = [
|
||||
"${RestAssuredWebTestClient.name}.*",
|
||||
"${Entity.name}.*",
|
||||
"${ContractVerifierUtil.name}.fileToBytes",
|
||||
"${ContractVerifierMessagingUtil.name}.headers",
|
||||
"${JsonAssertion.name}.assertThatJson",
|
||||
"${SpringCloudContractAssertions.name}.assertThat"
|
||||
|
||||
@@ -1 +1,3 @@
|
||||
{ "status" : "REQUEST" }
|
||||
{
|
||||
"status" : "REQUEST"
|
||||
}
|
||||
@@ -1 +1,3 @@
|
||||
{ "status" : "RESPONSE" }
|
||||
{
|
||||
"status" : "RESPONSE"
|
||||
}
|
||||
Reference in New Issue
Block a user