Merge branch '2.0.x'

This commit is contained in:
Marcin Grzejszczak
2018-12-17 10:46:21 +01:00
2 changed files with 61 additions and 44 deletions

View File

@@ -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 {

View File

@@ -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,50 +32,32 @@ 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;
@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);
@@ -82,8 +71,10 @@ public class WireMockSnippetTests {
}
@Test
public void should_use_placeholders_in_stub_file_name() throws Exception {
given(this.operation.getName()).willReturn("{method-name}/{step}");
public void should_use_placeholders_in_stub_file_name()
throws Exception {
this.operation = operation("{method-name}/{step}",
request(), response(), this.context);
WireMockSnippet snippet = new WireMockSnippet();
snippet.document(this.operation);
@@ -99,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);
@@ -118,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);
@@ -136,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);
@@ -151,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<String, Object> map = new HashMap<>();
@Override
public Map<String, Object> 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() {
@@ -332,8 +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;
}
@@ -363,5 +385,4 @@ public class WireMockSnippetTests {
}
};
}
}