From d23e0c61bf0b8be34acba042b47a59111fe345fd Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Mon, 17 Dec 2018 10:43:22 +0100 Subject: [PATCH] The DSL generated from RESTDocs for String response is wrong; fixes gh-775 --- .../default-dsl-contract-only.snippet | 8 +- .../restdocs/WireMockSnippetTests.java | 95 ++++++++++++------- 2 files changed, 63 insertions(+), 40 deletions(-) diff --git a/spring-cloud-contract-wiremock/src/main/resources/org/springframework/restdocs/templates/default-dsl-contract-only.snippet b/spring-cloud-contract-wiremock/src/main/resources/org/springframework/restdocs/templates/default-dsl-contract-only.snippet index d22792c819..99a4b203e3 100644 --- a/spring-cloud-contract-wiremock/src/main/resources/org/springframework/restdocs/templates/default-dsl-contract-only.snippet +++ b/spring-cloud-contract-wiremock/src/main/resources/org/springframework/restdocs/templates/default-dsl-contract-only.snippet @@ -5,9 +5,7 @@ Contract.make { method '{{request_method}}' url '{{request_url}}' {{#request_body_present}} - body(''' - {{request_body}} - ''') + body('''{{request_body}}''') {{/request_body_present}} {{#request_headers_present}} headers { @@ -27,9 +25,7 @@ Contract.make { response { status {{response_status}} {{#response_body_present}} - body(''' - {{response_body}} - ''') + body('''{{response_body}}''') {{/response_body_present}} {{#response_headers_present}} headers { diff --git a/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/restdocs/WireMockSnippetTests.java b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/restdocs/WireMockSnippetTests.java index 76a8c01d23..5be8ce7c7d 100644 --- a/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/restdocs/WireMockSnippetTests.java +++ b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/restdocs/WireMockSnippetTests.java @@ -1,5 +1,15 @@ package org.springframework.cloud.contract.wiremock.restdocs; +import java.io.File; +import java.io.IOException; +import java.net.URI; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + import com.github.tomakehurst.wiremock.matching.EqualToJsonPattern; import com.github.tomakehurst.wiremock.matching.EqualToXmlPattern; import com.github.tomakehurst.wiremock.stubbing.StubMapping; @@ -7,10 +17,7 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; -import org.junit.runner.RunWith; -import org.mockito.Answers; -import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; + import org.springframework.cloud.contract.wiremock.WireMockStubMapping; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; @@ -25,26 +32,15 @@ import org.springframework.restdocs.operation.OperationResponse; import org.springframework.restdocs.operation.Parameters; import org.springframework.restdocs.operation.RequestCookie; -import java.io.File; -import java.io.IOException; -import java.net.URI; -import java.nio.charset.Charset; -import java.nio.file.Files; -import java.util.Collection; -import java.util.Collections; - import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.BDDMockito.given; /** * @author Marcin Grzejszczak */ -@RunWith(MockitoJUnitRunner.class) public class WireMockSnippetTests { - @Mock(answer = Answers.RETURNS_DEEP_STUBS) Operation operation; + RestDocumentationContext context; @Rule public TemporaryFolder tmp = new TemporaryFolder(); private File outputFolder; @@ -52,20 +48,16 @@ public class WireMockSnippetTests { @Before public void setup() throws IOException { this.outputFolder = this.tmp.newFolder(); - ManualRestDocumentation restDocumentation = new ManualRestDocumentation(this.outputFolder.getAbsolutePath()); + ManualRestDocumentation restDocumentation = new ManualRestDocumentation(this.outputFolder + .getAbsolutePath()); restDocumentation.beforeTest(this.getClass(), "method"); - RestDocumentationContext context = restDocumentation.beforeOperation(); - given(this.operation.getAttributes().get(anyString())).willReturn(null); - given(this.operation.getAttributes() - .get(RestDocumentationContext.class.getName())).willReturn(context); - given(this.operation.getRequest()).willReturn(request()); - given(this.operation.getResponse()).willReturn(response()); + this.context = restDocumentation.beforeOperation(); + this.operation = operation(request(), response(), this.context); } @Test public void should_maintain_the_response_status_when_generating_stub() throws Exception { - given(this.operation.getName()).willReturn("foo"); WireMockSnippet snippet = new WireMockSnippet(); snippet.document(this.operation); @@ -81,7 +73,8 @@ public class WireMockSnippetTests { @Test public void should_use_placeholders_in_stub_file_name() throws Exception { - given(this.operation.getName()).willReturn("{method-name}/{step}"); + this.operation = operation("{method-name}/{step}", + request(), response(), this.context); WireMockSnippet snippet = new WireMockSnippet(); snippet.document(this.operation); @@ -97,9 +90,8 @@ public class WireMockSnippetTests { @Test public void should_use_equal_to_json_pattern_for_body_when_request_content_type_is_json_when_generating_stub() throws Exception { - given(this.operation.getName()).willReturn("foo"); + this.operation = operation(requestPostWithJsonContentType(), response(), this.context); WireMockSnippet snippet = new WireMockSnippet(); - given(this.operation.getRequest()).willReturn(requestPostWithJsonContentType()); snippet.document(this.operation); @@ -116,9 +108,8 @@ public class WireMockSnippetTests { @Test public void should_use_equal_to_xml_pattern_for_body_when_request_content_type_is_xml_when_generating_stub() throws Exception { - given(this.operation.getName()).willReturn("foo"); + this.operation = operation(requestPostWithXmlContentType(), response(), this.context); WireMockSnippet snippet = new WireMockSnippet(); - given(this.operation.getRequest()).willReturn(requestPostWithXmlContentType()); snippet.document(this.operation); @@ -134,9 +125,8 @@ public class WireMockSnippetTests { @Test public void should_handle_empty_request_body() throws IOException { - given(this.operation.getName()).willReturn("foo"); + this.operation = operation(requestPostWithEmptyBody(), response(), this.context); WireMockSnippet snippet = new WireMockSnippet(); - given(this.operation.getRequest()).willReturn(requestPostWithEmptyBody()); snippet.document(this.operation); @@ -149,6 +139,40 @@ public class WireMockSnippetTests { .isEqualTo(HttpStatus.ACCEPTED.value()); } + private Operation operation(OperationRequest request, OperationResponse response, + RestDocumentationContext context) { + return operation("foo", request, response, context); + } + + private Operation operation(String name, OperationRequest request, OperationResponse response, + RestDocumentationContext context) { + return new Operation() { + + Map map = new HashMap<>(); + + @Override + public Map getAttributes() { + this.map.put(RestDocumentationContext.class.getName(), context); + return this.map; + } + + @Override + public String getName() { + return name; + } + + @Override + public OperationRequest getRequest() { + return request; + } + + @Override + public OperationResponse getResponse() { + return response; + } + }; + } + private OperationResponse response() { return new OperationResponse() { @@ -308,7 +332,8 @@ public class WireMockSnippetTests { return URI.create("http://foo/bar"); } - @Override public Collection getCookies() { + @Override + public Collection getCookies() { return Collections.emptySet(); } }; @@ -329,7 +354,8 @@ public class WireMockSnippetTests { @Override public HttpHeaders getHeaders() { HttpHeaders httpHeaders = new HttpHeaders(); - httpHeaders.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); + httpHeaders + .add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); return httpHeaders; } @@ -353,7 +379,8 @@ public class WireMockSnippetTests { return URI.create("http://foo/bar"); } - @Override public Collection getCookies() { + @Override + public Collection getCookies() { return Collections.emptySet(); } };