Added better support for XML in RestDocs; fixes gh-647

This commit is contained in:
Marcin Grzejszczak
2018-05-08 20:38:00 +02:00
parent 72f5ba2722
commit 7341b570b8
24 changed files with 353 additions and 19 deletions

View File

@@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<version>1.5.12.RELEASE</version>
<relativePath />
</parent>

View File

@@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<version>1.5.12.RELEASE</version>
<relativePath />
</parent>

View File

@@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<version>1.5.12.RELEASE</version>
<relativePath />
</parent>

View File

@@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<version>1.5.12.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

View File

@@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<version>1.5.12.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

View File

@@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<version>1.5.12.RELEASE</version>
<relativePath />
</parent>

View File

@@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<version>1.5.12.RELEASE</version>
<relativePath />
</parent>

View File

@@ -36,7 +36,8 @@ dependencyManagement {
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-actuator")
compile("com.fasterxml.jackson.dataformat:jackson-dataformat-xml")
testCompile 'org.springframework.cloud:spring-cloud-contract-wiremock'
testCompile 'org.springframework.cloud:spring-cloud-starter-contract-stub-runner'
testCompile "org.springframework.boot:spring-boot-starter-test"

View File

@@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<version>1.5.12.RELEASE</version>
<relativePath />
</parent>
@@ -32,6 +32,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
<!-- tag::stub_runner[] -->
<dependency>

View File

@@ -0,0 +1,84 @@
package com.example.loan;
import java.net.URI;
import java.nio.charset.Charset;
import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
import org.assertj.core.api.BDDAssertions;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.StreamUtils;
import org.springframework.web.client.RestTemplate;
@RunWith(SpringRunner.class)
@SpringBootTest(properties="service.port=${wiremock.server.port}")
@AutoConfigureWireMock(port=0)
public class XmlServiceTests {
@Value("classpath:META-INF/com.example/http-server-restdocs/0.0.1-SNAPSHOT/mappings/should_return_empty_content.json")
private Resource empty;
@Value("classpath:META-INF/com.example/http-server-restdocs/0.0.1-SNAPSHOT/mappings/should_return_full_content.json")
private Resource full;
@Autowired
private WireMockServer server;
@Test
public void shouldSuccessfullyReturnFullResponse() throws Exception {
server.addStubMapping(StubMapping.buildFrom(StreamUtils.copyToString(
full.getInputStream(), Charset.forName("UTF-8"))));
ResponseEntity<XmlResponseBody> responseEntity = new RestTemplate().exchange(
RequestEntity.post(URI.create("http://localhost:" + server.port() + "/xmlfraud"))
.contentType(MediaType.valueOf("application/xml;charset=UTF-8"))
.body(new XmlRequestBody("foo")), XmlResponseBody.class);
BDDAssertions.then(responseEntity.getStatusCodeValue()).isEqualTo(200);
BDDAssertions.then(responseEntity.getBody().status).isEqualTo("FULL");
}
@Test
public void shouldSuccessfullyReturnEmptyResponse() throws Exception {
server.addStubMapping(StubMapping.buildFrom(StreamUtils.copyToString(
empty.getInputStream(), Charset.forName("UTF-8"))));
ResponseEntity<XmlResponseBody> responseEntity = new RestTemplate().exchange(
RequestEntity.post(URI.create("http://localhost:" + server.port() + "/xmlfraud"))
.contentType(MediaType.valueOf("application/xml;charset=UTF-8"))
.body(new XmlRequestBody("")), XmlResponseBody.class);
BDDAssertions.then(responseEntity.getStatusCodeValue()).isEqualTo(200);
BDDAssertions.then(responseEntity.getBody().status).isEqualTo("EMPTY");
}
}
class XmlRequestBody {
public String name;
public XmlRequestBody(String name) {
this.name = name;
}
}
class XmlResponseBody {
public String status;
public XmlResponseBody(String status) {
this.status = status;
}
public XmlResponseBody() {
}
}

View File

@@ -0,0 +1,57 @@
package com.example.loan;
import java.net.URI;
import java.nio.charset.Charset;
import com.example.loan.model.Client;
import com.example.loan.model.LoanApplication;
import com.example.loan.model.LoanApplicationResult;
import com.example.loan.model.LoanApplicationStatus;
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
import org.assertj.core.api.BDDAssertions;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.contract.stubrunner.spring.AutoConfigureStubRunner;
import org.springframework.http.MediaType;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.StreamUtils;
import org.springframework.web.client.RestTemplate;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureStubRunner(ids = "com.example:http-server-restdocs")
public class XmlServiceUsingStubRunnerTests {
@Value("${stubrunner.runningstubs.http-server-restdocs.port}") int port;
@Test
public void shouldSuccessfullyReturnFullResponse() throws Exception {
ResponseEntity<XmlResponseBody> responseEntity = new RestTemplate().exchange(
RequestEntity.post(URI.create("http://localhost:" + this.port + "/xmlfraud"))
.contentType(MediaType.valueOf("application/xml;charset=UTF-8"))
.body(new XmlRequestBody("foo")), XmlResponseBody.class);
BDDAssertions.then(responseEntity.getStatusCodeValue()).isEqualTo(200);
BDDAssertions.then(responseEntity.getBody().status).isEqualTo("FULL");
}
@Test
public void shouldSuccessfullyReturnEmptyResponse() throws Exception {
ResponseEntity<XmlResponseBody> responseEntity = new RestTemplate().exchange(
RequestEntity.post(URI.create("http://localhost:" + this.port + "/xmlfraud"))
.contentType(MediaType.valueOf("application/xml;charset=UTF-8"))
.body(new XmlRequestBody("")), XmlResponseBody.class);
BDDAssertions.then(responseEntity.getStatusCodeValue()).isEqualTo(200);
BDDAssertions.then(responseEntity.getBody().status).isEqualTo("EMPTY");
}
}

View File

@@ -40,6 +40,7 @@ dependencyManagement {
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-actuator")
compile("com.fasterxml.jackson.dataformat:jackson-dataformat-xml")
testCompile 'org.springframework.boot:spring-boot-starter-test'
testCompile 'org.springframework.restdocs:spring-restdocs-mockmvc'

View File

@@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<version>1.5.12.RELEASE</version>
<relativePath />
</parent>
@@ -32,6 +32,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>

View File

@@ -0,0 +1,47 @@
package com.example.fraud;
import java.math.BigDecimal;
import com.example.fraud.model.FraudCheck;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import static org.springframework.web.bind.annotation.RequestMethod.POST;
import static org.springframework.web.bind.annotation.RequestMethod.PUT;
@Controller
public class FraudDetectionXmlController {
@RequestMapping(
value = "/xmlfraud",
method = POST,
consumes = MediaType.APPLICATION_XML_VALUE,
produces = MediaType.APPLICATION_XML_VALUE)
@ResponseBody
public XmlResponseBody xmlResponseBody(@RequestBody XmlRequestBody xmlRequestBody) {
if (StringUtils.isEmpty(xmlRequestBody.name)) {
return new XmlResponseBody("EMPTY");
}
return new XmlResponseBody("FULL");
}
}
class XmlRequestBody {
public String name;
}
class XmlResponseBody {
public String status;
public XmlResponseBody(String status) {
this.status = status;
}
public XmlResponseBody() {
}
}

View File

@@ -0,0 +1,60 @@
package com.example.fraud;
import java.math.BigDecimal;
import com.example.fraud.model.FraudCheck;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.json.AutoConfigureJsonTesters;
import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.json.JacksonTester;
import org.springframework.http.MediaType;
import org.springframework.restdocs.mockmvc.MockMvcRestDocumentation;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import static org.springframework.cloud.contract.wiremock.restdocs.WireMockRestDocs.verify;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@AutoConfigureRestDocs(outputDir = "target/snippets")
@AutoConfigureMockMvc
@DirtiesContext
public class XmlStubGeneratorTests {
@Autowired
private MockMvc mockMvc;
@Test
public void should_return_full_content() throws Exception {
mockMvc.perform(post("/xmlfraud")
.contentType(MediaType.APPLICATION_XML)
.content("<XmlRequestBody><name>foo</name></XmlRequestBody>"))
.andExpect(status().is2xxSuccessful())
.andExpect(content().string("<XmlResponseBody><status>FULL</status></XmlResponseBody>"))
.andDo(MockMvcRestDocumentation.document("{methodName}"));
}
@Test
public void should_return_empty_content() throws Exception {
mockMvc.perform(post("/xmlfraud")
.contentType(MediaType.APPLICATION_XML)
.content("<XmlRequestBody><name></name></XmlRequestBody>"))
.andExpect(status().is2xxSuccessful())
.andExpect(content().string("<XmlResponseBody><status>EMPTY</status></XmlResponseBody>"))
.andDo(MockMvcRestDocumentation.document("{methodName}"));
}
}

View File

@@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<version>1.5.12.RELEASE</version>
<relativePath />
</parent>

View File

@@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<version>1.5.12.RELEASE</version>
<relativePath />
</parent>

View File

@@ -28,7 +28,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<version>1.5.12.RELEASE</version>
</parent>
<dependencies>

View File

@@ -28,7 +28,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<version>1.5.12.RELEASE</version>
</parent>
<dependencies>

View File

@@ -28,7 +28,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<version>1.5.12.RELEASE</version>
</parent>
<dependencies>

View File

@@ -28,7 +28,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<version>1.5.12.RELEASE</version>
</parent>
<dependencies>

View File

@@ -28,7 +28,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<version>1.5.12.RELEASE</version>
</parent>
<dependencies>

View File

@@ -46,6 +46,7 @@ import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.delete;
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static com.github.tomakehurst.wiremock.client.WireMock.equalToJson;
import static com.github.tomakehurst.wiremock.client.WireMock.equalToXml;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.head;
import static com.github.tomakehurst.wiremock.client.WireMock.matching;
@@ -71,6 +72,7 @@ public class WireMockSnippet implements Snippet {
private StubMapping stubMapping;
private boolean hasJsonBodyRequestToMatch = false;
private boolean hasXmlBodyRequestToMatch = false;
private final PropertyPlaceholderHelper propertyPlaceholderHelper = new PropertyPlaceholderHelper(
"{", "}");
@@ -121,13 +123,22 @@ public class WireMockSnippet implements Snippet {
.get("contract.contentType");
if (this.contentType == null) {
this.hasJsonBodyRequestToMatch = hasJsonContentType(operation);
this.hasXmlBodyRequestToMatch = hasXmlContentType(operation);
}
}
private boolean hasJsonContentType(Operation operation) {
return hasContentType(operation, MediaType.APPLICATION_JSON);
}
private boolean hasXmlContentType(Operation operation) {
return hasContentType(operation, MediaType.APPLICATION_XML);
}
private boolean hasContentType(Operation operation, MediaType mediaType) {
return operation.getRequest().getHeaders().getContentType() != null
&& (operation.getRequest().getHeaders().getContentType()
.isCompatibleWith(MediaType.APPLICATION_JSON));
.isCompatibleWith(mediaType));
}
private ResponseDefinitionBuilder response(Operation operation) {
@@ -196,6 +207,9 @@ public class WireMockSnippet implements Snippet {
if (this.hasJsonBodyRequestToMatch) {
builder.withRequestBody(equalToJson(content));
}
else if (this.hasXmlBodyRequestToMatch) {
builder.withRequestBody(equalToXml(content));
}
else {
builder.withRequestBody(equalTo(content));
}

View File

@@ -8,6 +8,7 @@ import java.nio.file.Files;
import java.util.Collection;
import com.github.tomakehurst.wiremock.matching.EqualToJsonPattern;
import com.github.tomakehurst.wiremock.matching.EqualToXmlPattern;
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
import org.junit.Before;
@@ -112,6 +113,25 @@ public class WireMockSnippetTests {
.isEqualTo("{\"name\": \"12\"}");
}
@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");
WireMockSnippet snippet = new WireMockSnippet();
given(this.operation.getRequest()).willReturn(requestPostWithXmlContentType());
snippet.document(this.operation);
File stub = new File(this.outputFolder, "stubs/foo.json");
assertThat(stub).exists();
StubMapping stubMapping = WireMockStubMapping
.buildFrom(new String(Files.readAllBytes(stub.toPath())));
assertThat(stubMapping.getRequest().getBodyPatterns().get(0))
.isInstanceOf(EqualToXmlPattern.class);
assertThat(stubMapping.getRequest().getBodyPatterns().get(0).getValue())
.isEqualTo("<name>foo</name>");
}
@Test
public void should_handle_empty_request_body() throws IOException {
given(this.operation.getName()).willReturn("foo");
@@ -127,7 +147,6 @@ public class WireMockSnippetTests {
assertThat(stubMapping.getRequest().getBodyPatterns()).isNullOrEmpty();
assertThat(stubMapping.getResponse().getStatus())
.isEqualTo(HttpStatus.ACCEPTED.value());
}
private OperationResponse response() {
@@ -238,6 +257,49 @@ public class WireMockSnippetTests {
};
}
private OperationRequest requestPostWithXmlContentType() {
return new OperationRequest() {
@Override
public byte[] getContent() {
String content = "<name>foo</name>";
return content.getBytes(Charset.forName("UTF-8"));
}
@Override
public String getContentAsString() {
return "<name>foo</name>";
}
@Override
public HttpHeaders getHeaders() {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Content-Type", MediaType.APPLICATION_XML_VALUE);
return httpHeaders;
}
@Override
public HttpMethod getMethod() {
return HttpMethod.POST;
}
@Override
public Parameters getParameters() {
return null;
}
@Override
public Collection<OperationRequestPart> getParts() {
return null;
}
@Override
public URI getUri() {
return URI.create("http://foo/bar");
}
};
}
private OperationRequest requestPostWithEmptyBody() {
return new OperationRequest() {
@Override