Re-organize contract generation into a new helper

This commit is contained in:
Dave Syer
2016-07-26 08:27:35 +01:00
parent 1a8c1214c4
commit 44eebcab0f
4 changed files with 51 additions and 15 deletions

View File

@@ -1,7 +1,5 @@
package com.example.fraud;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import java.math.BigDecimal;
import org.junit.Test;
@@ -12,6 +10,7 @@ import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDoc
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.cloud.contract.wiremock.RestDocsContracts;
import org.springframework.cloud.contract.wiremock.WireMockSnippet;
import org.springframework.http.MediaType;
import org.springframework.test.annotation.DirtiesContext;
@@ -32,7 +31,7 @@ public class StubGeneratorTests {
@Autowired
private MockMvc mockMvc;
@Autowired
private WireMockSnippet snippet;
@@ -50,9 +49,10 @@ public class StubGeneratorTests {
.value("FRAUD"))
.andExpect(MockMvcResultMatchers.jsonPath("$.rejectionReason")
.value("Amount too high"))
.andDo(snippet.content().jsonPath("$.clientId").jsonPath("$[?(@.loanAmount > 1000)]")
.contentType(MediaType.valueOf("application/vnd.fraud.v1+json")))
.andDo(document("markClientAsFraud"));
.andDo(RestDocsContracts.content(snippet).jsonPath("$.clientId")
.jsonPath("$[?(@.loanAmount > 1000)]")
.contentType(MediaType.valueOf("application/vnd.fraud.v1+json"))
.contract("markClientAsFraud"));
}
@Test
@@ -63,13 +63,14 @@ public class StubGeneratorTests {
mockMvc.perform(MockMvcRequestBuilders.put("/fraudcheck")
.contentType(MediaType.valueOf("application/vnd.fraud.v1+json"))
.content(json.write(fraudCheck).getJson()))
.andExpect(MockMvcResultMatchers.jsonPath("$.fraudCheckStatus")
.value("OK"))
.andExpect(
MockMvcResultMatchers.jsonPath("$.fraudCheckStatus").value("OK"))
.andExpect(MockMvcResultMatchers.jsonPath("$.rejectionReason")
.doesNotExist())
.andDo(snippet.content().jsonPath("$.clientId").jsonPath("$[?(@.loanAmount <= 1000)]")
.contentType(MediaType.valueOf("application/vnd.fraud.v1+json")))
.andDo(document("markClientAsNotFraud"));
.andDo(RestDocsContracts.content(snippet).jsonPath("$.clientId")
.jsonPath("$[?(@.loanAmount <= 1000)]")
.contentType(MediaType.valueOf("application/vnd.fraud.v1+json"))
.contract("markClientAsNotFraud"));
}
}

View File

@@ -24,6 +24,7 @@ import java.util.Map;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.restdocs.mockmvc.MockMvcRestDocumentation;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultHandler;
import org.springframework.util.ObjectUtils;
@@ -37,11 +38,19 @@ public class ContractRequestHandler implements ResultHandler {
private Map<String, JsonPath> jsonPaths = new LinkedHashMap<>();
private MediaType contentType;
private WireMockSnippet snippet;
private String name;
public ContractRequestHandler(WireMockSnippet snippet) {
this.snippet = snippet;
}
public ResultHandler contract(String name) {
this.name = name;
// TODO: try and get access to the internals of this so we don't need to store
// state in the snippet
return this;
}
@Override
public void handle(MvcResult result) throws Exception {
MockHttpServletRequest request = result.getRequest();
@@ -58,6 +67,7 @@ public class ContractRequestHandler implements ResultHandler {
assertThat(contentType.includes(MediaType.valueOf(resultType))).isTrue()
.as("content type did not match");
}
MockMvcRestDocumentation.document(this.name).handle(result);
}
public ContractRequestHandler jsonPath(String expression, Object... args) {

View File

@@ -0,0 +1,29 @@
/*
* Copyright 2012-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.contract.wiremock;
/**
* @author Dave Syer
*
*/
public class RestDocsContracts {
public static ContractRequestHandler content(WireMockSnippet snippet) {
return new ContractRequestHandler(snippet);
}
}

View File

@@ -149,10 +149,6 @@ public class WireMockSnippet implements Snippet {
return result;
}
public ContractRequestHandler content() {
return new ContractRequestHandler(this);
}
public void setJsonPaths(Collection<String> jsonPaths) {
this.jsonPaths = new LinkedHashSet<>(jsonPaths);
}