diff --git a/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/util/ContractVerifierDslConverter.groovy b/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/util/ContractVerifierDslConverter.groovy index 01a81c2e75..b6ff28d480 100644 --- a/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/util/ContractVerifierDslConverter.groovy +++ b/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/util/ContractVerifierDslConverter.groovy @@ -44,7 +44,7 @@ class ContractVerifierDslConverter implements ContractConverter contracts = ContractVerifierDslConverter.convertAsCollection(contractFile.parentFile, contractFile) + Collection contracts = ContractVerifierDslConverter. + convertAsCollection(contractFile.parentFile, contractFile) then: Contract contract = contracts.first() contract.description == "Some description" @@ -79,7 +86,8 @@ class JavaContractConverterSpec extends Specification { contract.request.method.clientValue == "PUT" contract.request.headers.entries.find { it.name == "foo" && - ((RegexProperty) it.clientValue).pattern() == "bar" && it.serverValue == "bar" + ((RegexProperty) it.clientValue).pattern() == "bar" && + it.serverValue == "bar" } contract.request.headers.entries.find { it.name == "fooReq" && @@ -94,23 +102,27 @@ class JavaContractConverterSpec extends Specification { contract.response.delay.clientValue == 1000 contract.response.headers.entries.find { it.name == "foo2" && - ((RegexProperty) it.serverValue).pattern() == "bar" && it.clientValue == "bar" + ((RegexProperty) it.serverValue).pattern() == "bar" && + it.clientValue == "bar" } contract.response.headers.entries.find { it.name == "foo3" && - ((ExecutionProperty) it.serverValue).insertValue('foo') == "andMeToo(foo)" + ((ExecutionProperty) it.serverValue). + insertValue('foo') == "andMeToo(foo)" } contract.response.headers.entries.find { it.name == "fooRes" && it.clientValue == "baz" } - MapConverter.getStubSideValues(contract.response.body) == [foo2: "bar", foo3: "baz", nullValue: null] + MapConverter.getStubSideValues( + contract.response.body) == [foo2: "bar", foo3: "baz", nullValue: null] contract.response.bodyMatchers.matchers[0].path() == '$.foo2' contract.response.bodyMatchers.matchers[0].matchingType() == REGEX contract.response.bodyMatchers.matchers[0].value().pattern() == 'bar' contract.response.bodyMatchers.matchers[1].path() == '$.foo3' contract.response.bodyMatchers.matchers[1].matchingType() == COMMAND - contract.response.bodyMatchers.matchers[1].value() == new ExecutionProperty('executeMe($it)') + contract.response.bodyMatchers.matchers[1]. + value() == new ExecutionProperty('executeMe($it)') contract.response.bodyMatchers.matchers[2].path() == '$.nullValue' contract.response.bodyMatchers.matchers[2].matchingType() == NULL contract.response.bodyMatchers.matchers[2].value() == null @@ -120,7 +132,8 @@ class JavaContractConverterSpec extends Specification { def "should convert java with REST with body from file"() { when: - Collection contracts = ContractVerifierDslConverter.convertAsCollection(contractBodyFile.parentFile, contractBodyFile) + Collection contracts = ContractVerifierDslConverter. + convertAsCollection(contractBodyFile.parentFile, contractBodyFile) then: contracts.size() == 1 Contract contract = contracts.first() @@ -133,7 +146,9 @@ class JavaContractConverterSpec extends Specification { def "should convert java with REST with body as bytes"() { when: - Collection contracts = ContractVerifierDslConverter.convertAsCollection(contractBodyBytesFile.parentFile, contractBodyBytesFile) + Collection contracts = ContractVerifierDslConverter. + convertAsCollection( + contractBodyBytesFile.parentFile, contractBodyBytesFile) then: contracts.size() == 1 Contract contract = contracts.first() @@ -144,7 +159,36 @@ class JavaContractConverterSpec extends Specification { def "should convert java with REST for docs"() { when: - Collection contracts = ContractVerifierDslConverter.convertAsCollection(docsFile.parentFile, docsFile) + Collection contracts = ContractVerifierDslConverter. + convertAsCollection(docsFile.parentFile, docsFile) + then: + contracts.size() == 1 + } + + @Issue("1398") + def "should work when contract starts with 'package'"() { + given: + URL packageContract = JavaContractConverterSpec. + getResource("/contractsToCompile/package_contract.java") + File packageFile = new File(packageContract.toURI()) + when: + Collection contracts = ContractVerifierDslConverter. + convertAsCollection(packageFile.parentFile, packageFile) + + then: + contracts.size() == 1 + } + + @Issue("1398") + def "should work when 'contract starts with 'package' preceded by other text"() { + given: + URL contract = JavaContractConverterSpec. + getResource("/contractsToCompile/contract.java") + File contractFile = new File(contract.toURI()) + when: + Collection contracts = ContractVerifierDslConverter. + convertAsCollection(contractFile.parentFile, contractFile) + then: contracts.size() == 1 } diff --git a/spring-cloud-contract-verifier/src/test/resources/contractsToCompile/package_contract.java b/spring-cloud-contract-verifier/src/test/resources/contractsToCompile/package_contract.java new file mode 100644 index 0000000000..9fce68310d --- /dev/null +++ b/spring-cloud-contract-verifier/src/test/resources/contractsToCompile/package_contract.java @@ -0,0 +1,32 @@ +package example; + +import java.util.Collection; +import java.util.Collections; +import java.util.function.Supplier; + +import org.springframework.cloud.contract.spec.Contract; + +class package_contract implements Supplier> { + + @Override + public Collection get() { + return Collections.singletonList(Contract.make(c -> { + c.request(r -> { + r.method(r.PUT()); + r.headers(h -> { + h.contentType(h.applicationJson()); + }); + r.body(" { \"status\" : \"OK\" } "); + r.url("/1"); + }); + c.response(r -> { + r.status(r.OK()); + r.body(" { \"status\" : \"OK\" } "); + r.headers(h -> { + h.contentType(h.textPlain()); + }); + }); + })); + } + +}