Search in sources :

Example 1 with SpringCloudContractRestDocs.dslContract

use of org.springframework.cloud.contract.wiremock.restdocs.SpringCloudContractRestDocs.dslContract in project spring-cloud-contract by spring-cloud.

the class ContractDslSnippetTests method should_create_contract_template_and_doc.

@Test
public void should_create_contract_template_and_doc() throws Exception {
    // tag::contract_snippet[]
    this.mockMvc.perform(post("/foo").accept(MediaType.APPLICATION_PDF).accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON).content("{\"foo\": 23, \"bar\" : \"baz\" }")).andExpect(status().isOk()).andExpect(content().string("bar")).andDo(WireMockRestDocs.verify().jsonPath("$[?(@.foo >= 20)]").jsonPath("$[?(@.bar in ['baz','bazz','bazzz'])]").contentType(MediaType.valueOf("application/json"))).andDo(document("index", SpringCloudContractRestDocs.dslContract()));
    // end::contract_snippet[]
    then(file("/contracts/index.groovy")).exists();
    then(file("/stubs/index.json")).exists();
    then(file("/index/dsl-contract.adoc")).exists();
    Collection<Contract> parsedContracts = ContractVerifierDslConverter.convertAsCollection(new File("/"), file("/contracts/index.groovy"));
    Contract parsedContract = parsedContracts.iterator().next();
    then(parsedContract.getRequest().getHeaders().getEntries()).isNotNull();
    then(headerNames(parsedContract.getRequest().getHeaders().getEntries())).doesNotContain(HttpHeaders.HOST, HttpHeaders.CONTENT_LENGTH);
    then(headerNames(parsedContract.getResponse().getHeaders().getEntries())).doesNotContain(HttpHeaders.HOST, HttpHeaders.CONTENT_LENGTH);
    then(parsedContract.getRequest().getMethod().getClientValue()).isNotNull();
    then(parsedContract.getRequest().getUrlPath().getClientValue()).isNotNull();
    then(parsedContract.getRequest().getUrlPath().getClientValue().toString()).startsWith("/");
    then(parsedContract.getRequest().getBody().getClientValue()).isNotNull();
    then(parsedContract.getRequest().getBodyMatchers().hasMatchers()).isTrue();
    then(parsedContract.getResponse().getStatus().getClientValue()).isNotNull();
    then(parsedContract.getResponse().getHeaders().getEntries()).isNotEmpty();
    then(parsedContract.getResponse().getBody().getClientValue()).isNotNull();
}
Also used : Contract(org.springframework.cloud.contract.spec.Contract) SpringCloudContractRestDocs.dslContract(org.springframework.cloud.contract.wiremock.restdocs.SpringCloudContractRestDocs.dslContract) File(java.io.File) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with SpringCloudContractRestDocs.dslContract

use of org.springframework.cloud.contract.wiremock.restdocs.SpringCloudContractRestDocs.dslContract in project spring-cloud-contract by spring-cloud.

the class ContractDslSnippetTests method should_create_contract_template_and_doc_with_placeholder_names.

@Test
public void should_create_contract_template_and_doc_with_placeholder_names() throws Exception {
    this.mockMvc.perform(post("/foo").accept(MediaType.APPLICATION_PDF).accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON).content("{\"foo\": 23, \"bar\" : \"baz\" }")).andExpect(status().isOk()).andExpect(content().string("bar")).andDo(WireMockRestDocs.verify().jsonPath("$[?(@.foo >= 20)]").jsonPath("$[?(@.bar in ['baz','bazz','bazzz'])]").contentType(MediaType.valueOf("application/json"))).andDo(document("{methodName}", SpringCloudContractRestDocs.dslContract()));
    then(file("/contracts/should_create_contract_template_and_doc_with_placeholder_names.groovy")).exists();
    then(file("/stubs/should_create_contract_template_and_doc_with_placeholder_names.json")).exists();
    then(file("/should_create_contract_template_and_doc_with_placeholder_names/dsl-contract.adoc")).exists();
    Collection<Contract> parsedContracts = ContractVerifierDslConverter.convertAsCollection(new File("/"), file("/contracts/should_create_contract_template_and_doc_with_placeholder_names.groovy"));
    Contract parsedContract = parsedContracts.iterator().next();
    then(parsedContract.getRequest().getHeaders().getEntries()).isNotNull();
    then(headerNames(parsedContract.getRequest().getHeaders().getEntries())).doesNotContain(HttpHeaders.HOST, HttpHeaders.CONTENT_LENGTH);
    then(headerNames(parsedContract.getResponse().getHeaders().getEntries())).doesNotContain(HttpHeaders.HOST, HttpHeaders.CONTENT_LENGTH);
    then(parsedContract.getRequest().getMethod().getClientValue()).isNotNull();
    then(parsedContract.getRequest().getUrlPath().getClientValue()).isNotNull();
    then(parsedContract.getRequest().getUrlPath().getClientValue().toString()).startsWith("/");
    then(parsedContract.getRequest().getBody().getClientValue()).isNotNull();
    then(parsedContract.getRequest().getBodyMatchers().hasMatchers()).isTrue();
    then(parsedContract.getResponse().getStatus().getClientValue()).isNotNull();
    then(parsedContract.getResponse().getHeaders().getEntries()).isNotEmpty();
    then(parsedContract.getResponse().getBody().getClientValue()).isNotNull();
}
Also used : Contract(org.springframework.cloud.contract.spec.Contract) SpringCloudContractRestDocs.dslContract(org.springframework.cloud.contract.wiremock.restdocs.SpringCloudContractRestDocs.dslContract) File(java.io.File) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

File (java.io.File)2 Test (org.junit.Test)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 Contract (org.springframework.cloud.contract.spec.Contract)2 SpringCloudContractRestDocs.dslContract (org.springframework.cloud.contract.wiremock.restdocs.SpringCloudContractRestDocs.dslContract)2