@@ -21,6 +21,7 @@ import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import org.springframework.cloud.contract.verifier.dsl.wiremock.WireMock2_1_7_StubMapping;
|
||||
import org.springframework.util.StreamUtils;
|
||||
|
||||
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
|
||||
@@ -39,7 +40,7 @@ class WiremockMappingDescriptor {
|
||||
|
||||
public StubMapping getMapping() {
|
||||
try {
|
||||
return StubMapping.buildFrom(StreamUtils.copyToString(
|
||||
return WireMock2_1_7_StubMapping.buildFrom(StreamUtils.copyToString(
|
||||
new FileInputStream(this.descriptor), Charset.forName("UTF-8")));
|
||||
}
|
||||
catch (IOException e) {
|
||||
|
||||
@@ -21,6 +21,7 @@ import com.github.tomakehurst.wiremock.stubbing.StubMapping
|
||||
import org.junit.Rule
|
||||
import org.junit.rules.TemporaryFolder
|
||||
import org.skyscreamer.jsonassert.JSONAssert
|
||||
import org.springframework.cloud.contract.verifier.dsl.wiremock.WireMock2_1_7_StubMapping
|
||||
import org.springframework.cloud.contract.verifier.file.ContractMetadata
|
||||
import spock.lang.Issue
|
||||
import spock.lang.Specification
|
||||
@@ -578,7 +579,7 @@ class DslToWireMockClientConverterSpec extends Specification {
|
||||
}
|
||||
|
||||
void stubMappingIsValidWireMockStub(String mappingDefinition) {
|
||||
StubMapping stubMapping = StubMapping.buildFrom(mappingDefinition)
|
||||
StubMapping stubMapping = WireMock2_1_7_StubMapping.buildFrom(mappingDefinition)
|
||||
stubMapping.request.bodyPatterns.findAll { it.isPresent() && it instanceof RegexPattern }.every {
|
||||
Pattern.compile(it.getValue())
|
||||
}
|
||||
|
||||
@@ -16,9 +16,9 @@
|
||||
|
||||
package org.springframework.cloud.contract.verifier.wiremock
|
||||
|
||||
import com.github.tomakehurst.wiremock.stubbing.StubMapping
|
||||
import org.springframework.cloud.contract.verifier.util.ContractVerifierDslConverter
|
||||
import org.springframework.cloud.contract.spec.Contract
|
||||
import org.springframework.cloud.contract.verifier.dsl.wiremock.WireMock2_1_7_StubMapping
|
||||
import org.springframework.cloud.contract.verifier.util.ContractVerifierDslConverter
|
||||
import spock.lang.Specification
|
||||
|
||||
class WireMockToDslConverterSpec extends Specification {
|
||||
@@ -554,6 +554,6 @@ class WireMockToDslConverterSpec extends Specification {
|
||||
}
|
||||
|
||||
void stubMappingIsValidWireMockStub(String mappingDefinition) {
|
||||
StubMapping.buildFrom(mappingDefinition)
|
||||
WireMock2_1_7_StubMapping.buildFrom(mappingDefinition)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package org.springframework.cloud.contract.verifier.dsl.wiremock;
|
||||
|
||||
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
|
||||
import com.jayway.jsonpath.DocumentContext;
|
||||
import com.jayway.jsonpath.JsonPath;
|
||||
|
||||
/**
|
||||
* @author Marcin Grzejszczak
|
||||
*/
|
||||
public class WireMock2_1_7_StubMapping {
|
||||
public static StubMapping buildFrom(String mappingDefinition) {
|
||||
DocumentContext context = JsonPath.parse(mappingDefinition);
|
||||
context.delete("$.id");
|
||||
context.delete("$.uuid");
|
||||
return StubMapping.buildFrom(context.jsonString());
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,7 @@ package org.springframework.cloud.contract.verifier.dsl
|
||||
|
||||
import com.github.tomakehurst.wiremock.matching.RegexPattern
|
||||
import com.github.tomakehurst.wiremock.stubbing.StubMapping
|
||||
import org.springframework.cloud.contract.verifier.dsl.wiremock.WireMock2_1_7_StubMapping
|
||||
import org.springframework.cloud.contract.verifier.dsl.wiremock.WireMockStubStrategy
|
||||
import org.springframework.cloud.contract.verifier.file.ContractMetadata
|
||||
|
||||
@@ -26,7 +27,7 @@ import java.util.regex.Pattern
|
||||
trait WireMockStubVerifier {
|
||||
|
||||
void stubMappingIsValidWireMockStub(String mappingDefinition) {
|
||||
StubMapping stubMapping = StubMapping.buildFrom(mappingDefinition)
|
||||
StubMapping stubMapping = WireMock2_1_7_StubMapping.buildFrom(mappingDefinition)
|
||||
stubMapping.request.bodyPatterns.findAll { it.isPresent() && it instanceof RegexPattern }.every {
|
||||
Pattern.compile(it.getValue())
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
package org.springframework.cloud.contract.verifier.dsl.wiremock
|
||||
|
||||
import com.github.tomakehurst.wiremock.stubbing.StubMapping
|
||||
import org.skyscreamer.jsonassert.JSONAssert
|
||||
import spock.lang.Specification
|
||||
|
||||
/**
|
||||
* @author Marcin Grzejszczak
|
||||
*/
|
||||
class WireMock2_1_7_StubMappingSpec extends Specification {
|
||||
private static final String stub_2_1_7 = """
|
||||
{
|
||||
"request" : {
|
||||
"method" : "GET"
|
||||
},
|
||||
"response" : {
|
||||
"status" : 200
|
||||
}
|
||||
}
|
||||
"""
|
||||
private static final String stub_2_5_1 = """
|
||||
{
|
||||
"id" : "77514bd4-a102-4478-a3c0-0fda8b905591",
|
||||
"request" : {
|
||||
"method" : "GET"
|
||||
},
|
||||
"response" : {
|
||||
"status" : 200
|
||||
},
|
||||
"uuid" : "77514bd4-a102-4478-a3c0-0fda8b905591"
|
||||
}
|
||||
"""
|
||||
|
||||
def "should successfully parse a WireMock 2.1.7 stub"() {
|
||||
when:
|
||||
StubMapping mapping = WireMock2_1_7_StubMapping.buildFrom(stub_2_1_7)
|
||||
then:
|
||||
JSONAssert.assertEquals(stub_2_1_7, mapping.toString(), false)
|
||||
}
|
||||
|
||||
def "should successfully parse a WireMock 2.5.1 stub"() {
|
||||
when:
|
||||
StubMapping mapping = WireMock2_1_7_StubMapping.buildFrom(stub_2_5_1)
|
||||
then:
|
||||
JSONAssert.assertEquals(stub_2_1_7, mapping.toString(), false)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package org.springframework.cloud.contract.wiremock;
|
||||
|
||||
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
|
||||
import com.jayway.jsonpath.DocumentContext;
|
||||
import com.jayway.jsonpath.JsonPath;
|
||||
|
||||
/**
|
||||
* @author Marcin Grzejszczak
|
||||
*/
|
||||
public class WireMock2_1_7_StubMapping {
|
||||
public static StubMapping buildFrom(String mappingDefinition) {
|
||||
DocumentContext context = JsonPath.parse(mappingDefinition);
|
||||
context.delete("$.id");
|
||||
context.delete("$.uuid");
|
||||
return StubMapping.buildFrom(context.jsonString());
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,6 @@ import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -39,7 +38,6 @@ import org.springframework.util.StringUtils;
|
||||
import com.github.tomakehurst.wiremock.WireMockServer;
|
||||
import com.github.tomakehurst.wiremock.client.WireMock;
|
||||
import com.github.tomakehurst.wiremock.core.Options;
|
||||
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
|
||||
|
||||
/**
|
||||
* Configuration and lifecycle for a Spring Application context that wants to run a
|
||||
@@ -105,7 +103,7 @@ public class WireMockConfiguration implements SmartLifecycle {
|
||||
pattern = pattern + "**/*.json";
|
||||
}
|
||||
for (Resource resource : resolver.getResources(pattern)) {
|
||||
this.server.addStubMapping(StubMapping
|
||||
this.server.addStubMapping(WireMock2_1_7_StubMapping
|
||||
.buildFrom(StreamUtils.copyToString(resource.getInputStream(), Charset.forName("UTF-8"))));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package org.springframework.cloud.contract.wiremock;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.skyscreamer.jsonassert.JSONAssert;
|
||||
|
||||
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
|
||||
|
||||
/**
|
||||
* @author Marcin Grzejszczak
|
||||
*/
|
||||
public class WireMock2_1_7_StubMappingTest {
|
||||
private static final String stub_2_1_7 = "{\"request\" : { \"method\" : \"GET\" }, \"response\" : { \"status\" : 200 }}";
|
||||
private static final String stub_2_5_1 = "{\"id\" : \"77514bd4-a102-4478-a3c0-0fda8b905591\", \"request\" : { \"method\" : \"GET\" }, \"response\" : { \"status\" : 200 }, \"uuid\" : \"77514bd4-a102-4478-a3c0-0fda8b905591\"}";
|
||||
|
||||
@Test
|
||||
public void should_successfully_parse_a_WireMock_2_1_7_stub() {
|
||||
// when:
|
||||
StubMapping mapping = WireMock2_1_7_StubMapping.buildFrom(stub_2_1_7);
|
||||
// then:
|
||||
JSONAssert.assertEquals(stub_2_1_7, mapping.toString(), false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_successfully_parse_a_WireMock_2_5_1_stub() {
|
||||
// when:
|
||||
StubMapping mapping = WireMock2_1_7_StubMapping.buildFrom(stub_2_5_1);
|
||||
// then:
|
||||
JSONAssert.assertEquals(stub_2_1_7, mapping.toString(), false);
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,7 @@ import org.junit.runner.RunWith;
|
||||
import org.mockito.Answers;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.springframework.cloud.contract.wiremock.WireMock2_1_7_StubMapping;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
@@ -48,12 +49,6 @@ public class WireMockSnippetTests {
|
||||
@Before
|
||||
public void setup() throws IOException {
|
||||
this.outputFolder = this.tmp.newFolder();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_maintain_the_response_status_when_generating_stub()
|
||||
throws Exception {
|
||||
WireMockSnippet snippet = new WireMockSnippet();
|
||||
RestDocumentationContext context = new RestDocumentationContext(this.getClass(),
|
||||
"method", this.outputFolder);
|
||||
given(this.operation.getName()).willReturn("foo");
|
||||
@@ -62,12 +57,18 @@ public class WireMockSnippetTests {
|
||||
.get(RestDocumentationContext.class.getName())).willReturn(context);
|
||||
given(this.operation.getRequest()).willReturn(request());
|
||||
given(this.operation.getResponse()).willReturn(response());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_maintain_the_response_status_when_generating_stub()
|
||||
throws Exception {
|
||||
WireMockSnippet snippet = new WireMockSnippet();
|
||||
|
||||
snippet.document(this.operation);
|
||||
|
||||
File stub = new File(this.outputFolder, "stubs/foo.json");
|
||||
assertThat(stub).exists();
|
||||
StubMapping stubMapping = StubMapping
|
||||
StubMapping stubMapping = WireMock2_1_7_StubMapping
|
||||
.buildFrom(new String(Files.readAllBytes(stub.toPath())));
|
||||
assertThat(stubMapping.getResponse().getStatus())
|
||||
.isEqualTo(HttpStatus.ACCEPTED.value());
|
||||
@@ -77,20 +78,13 @@ public class WireMockSnippetTests {
|
||||
public void should_use_equal_to_json_pattern_for_body_when_request_content_type_is_json_when_generating_stub()
|
||||
throws Exception {
|
||||
WireMockSnippet snippet = new WireMockSnippet();
|
||||
RestDocumentationContext context = new RestDocumentationContext(this.getClass(),
|
||||
"method", this.outputFolder);
|
||||
given(this.operation.getName()).willReturn("foo");
|
||||
given(this.operation.getAttributes().get(anyString())).willReturn(null);
|
||||
given(this.operation.getAttributes()
|
||||
.get(RestDocumentationContext.class.getName())).willReturn(context);
|
||||
given(this.operation.getRequest()).willReturn(requestPostWithJsonContentType());
|
||||
given(this.operation.getResponse()).willReturn(response());
|
||||
|
||||
snippet.document(this.operation);
|
||||
|
||||
File stub = new File(this.outputFolder, "stubs/foo.json");
|
||||
assertThat(stub).exists();
|
||||
StubMapping stubMapping = StubMapping
|
||||
StubMapping stubMapping = WireMock2_1_7_StubMapping
|
||||
.buildFrom(new String(Files.readAllBytes(stub.toPath())));
|
||||
assertThat(stubMapping.getRequest().getBodyPatterns().get(0))
|
||||
.isInstanceOf(EqualToJsonPattern.class);
|
||||
|
||||
Reference in New Issue
Block a user