@@ -14,7 +14,7 @@
|
||||
<name>spring-cloud-contract-dependencies</name>
|
||||
<description>Spring Cloud Contract Dependencies</description>
|
||||
<properties>
|
||||
<wiremock.version>2.1.7</wiremock.version>
|
||||
<wiremock.version>2.5.1</wiremock.version>
|
||||
<jsonassert.version>0.4.8</jsonassert.version>
|
||||
<aether.version>1.0.2.v20150114</aether.version>
|
||||
</properties>
|
||||
|
||||
@@ -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.WireMockStubMapping;
|
||||
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 WireMockStubMapping.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.WireMockStubMapping
|
||||
import org.springframework.cloud.contract.spec.Contract
|
||||
import org.springframework.cloud.contract.verifier.file.ContractMetadata
|
||||
import spock.lang.Issue
|
||||
@@ -632,7 +633,7 @@ class DslToWireMockClientConverterSpec extends Specification {
|
||||
}
|
||||
|
||||
void stubMappingIsValidWireMockStub(String mappingDefinition) {
|
||||
StubMapping stubMapping = StubMapping.buildFrom(mappingDefinition)
|
||||
StubMapping stubMapping = WireMockStubMapping.buildFrom(mappingDefinition)
|
||||
stubMapping.request.bodyPatterns.findAll { it.isPresent() && it instanceof RegexPattern }.every {
|
||||
Pattern.compile(it.getValue())
|
||||
}
|
||||
|
||||
@@ -16,8 +16,8 @@
|
||||
|
||||
package org.springframework.cloud.contract.verifier.wiremock
|
||||
|
||||
import com.github.tomakehurst.wiremock.stubbing.StubMapping
|
||||
import org.springframework.cloud.contract.spec.Contract
|
||||
import org.springframework.cloud.contract.verifier.dsl.wiremock.WireMockStubMapping
|
||||
import org.springframework.cloud.contract.verifier.util.ContractVerifierDslConverter
|
||||
import spock.lang.Specification
|
||||
|
||||
@@ -564,6 +564,6 @@ class WireMockToDslConverterSpec extends Specification {
|
||||
}
|
||||
|
||||
void stubMappingIsValidWireMockStub(String mappingDefinition) {
|
||||
StubMapping.buildFrom(mappingDefinition)
|
||||
WireMockStubMapping.buildFrom(mappingDefinition)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,9 +34,7 @@ import org.gradle.jvm.tasks.Jar
|
||||
* Also adds the necessary {@code testCompile} dependencies
|
||||
*
|
||||
* <ul>
|
||||
* <li>WireMock</li>
|
||||
* <li>JSON Assert</li>
|
||||
* <li>AssertJ</li>
|
||||
* </ul>
|
||||
*
|
||||
* @author Jakub Kubrynski, codearte.io
|
||||
@@ -72,6 +70,7 @@ class SpringCloudContractVerifierGradlePlugin implements Plugin<Project> {
|
||||
createGenerateTestsTask(extension, copyContracts)
|
||||
Task clientTask = createAndConfigureGenerateClientStubsFromDslTask(extension, copyContracts)
|
||||
createAndConfigureGenerateWireMockClientStubsFromDslTask(extension, clientTask)
|
||||
addProjectDependencies(project)
|
||||
addIdeaTestSources(project, extension)
|
||||
}
|
||||
|
||||
@@ -89,6 +88,10 @@ class SpringCloudContractVerifierGradlePlugin implements Plugin<Project> {
|
||||
}
|
||||
}
|
||||
|
||||
private void addProjectDependencies(Project project) {
|
||||
project.dependencies.add("testCompile", "com.toomuchcoding.jsonassert:jsonassert:0.4.7")
|
||||
}
|
||||
|
||||
private void setConfigurationDefaults(ContractVerifierExtension extension) {
|
||||
extension.with {
|
||||
generatedTestSourcesDir = generatedTestSourcesDir ?: project.file("${project.buildDir}/generated-test-sources/contracts")
|
||||
|
||||
@@ -160,12 +160,12 @@ abstract class ContractVerifierIntegrationSpec extends Specification {
|
||||
private static class CopyFileVisitor extends SimpleFileVisitor<Path> {
|
||||
private final Path targetPath;
|
||||
private Path sourcePath = null
|
||||
public CopyFileVisitor(Path targetPath) {
|
||||
CopyFileVisitor(Path targetPath) {
|
||||
this.targetPath = targetPath
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileVisitResult preVisitDirectory(final Path dir,
|
||||
FileVisitResult preVisitDirectory(final Path dir,
|
||||
final BasicFileAttributes attrs) throws IOException {
|
||||
if (sourcePath == null) {
|
||||
sourcePath = dir
|
||||
@@ -177,7 +177,7 @@ abstract class ContractVerifierIntegrationSpec extends Specification {
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileVisitResult visitFile(final Path file,
|
||||
FileVisitResult visitFile(final Path file,
|
||||
final BasicFileAttributes attrs) throws IOException {
|
||||
Path target = targetPath.resolve(sourcePath.relativize(file))
|
||||
if (!target.toFile().exists()) {
|
||||
|
||||
@@ -109,6 +109,16 @@ class ContractVerifierSpec extends Specification {
|
||||
publications.findByName("stubs") != null
|
||||
}
|
||||
|
||||
def "should add jsonassert as a testCompile dependency"() {
|
||||
given:
|
||||
project.plugins.apply(SpringCloudContractVerifierGradlePlugin)
|
||||
|
||||
expect:
|
||||
project.configurations.testCompile.dependencies.find {
|
||||
it.group == "com.toomuchcoding.jsonassert" && it.name == "jsonassert"
|
||||
} != null
|
||||
}
|
||||
|
||||
def "should compile"() {
|
||||
given:
|
||||
ContractVerifierExtension extension = new ContractVerifierExtension()
|
||||
|
||||
@@ -2,48 +2,35 @@ buildscript {
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
dependencies {
|
||||
classpath "org.springframework.boot:spring-boot-gradle-plugin:1.4.0.RELEASE"
|
||||
}
|
||||
}
|
||||
|
||||
apply plugin: 'groovy'
|
||||
apply plugin: 'spring-cloud-contract'
|
||||
apply plugin: 'maven-publish'
|
||||
apply plugin: 'spring-boot'
|
||||
|
||||
group = 'org.springframework.cloud.testprojects'
|
||||
|
||||
ext {
|
||||
contractsDir = file("repository/mappings")
|
||||
stubsOutputDirRoot = file("${project.buildDir}/production/${project.name}-stubs/repository/")
|
||||
BOM_VERSION = "Dalston.BUILD-SNAPSHOT"
|
||||
}
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
mavenLocal()
|
||||
maven { url "http://repo.spring.io/snapshot" }
|
||||
maven { url "http://repo.spring.io/milestone" }
|
||||
maven { url "http://repo.spring.io/release" }
|
||||
}
|
||||
|
||||
dependencyManagement {
|
||||
imports {
|
||||
mavenBom "org.springframework.cloud:spring-cloud-dependencies:$BOM_VERSION"
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compile "org.springframework:spring-web"
|
||||
compile "org.springframework:spring-web:$springVersion"
|
||||
compile "org.springframework:spring-context-support:$springVersion"
|
||||
compile "org.codehaus.groovy:groovy-all"
|
||||
compile 'com.jayway.jsonpath:json-path-assert'
|
||||
compile "org.codehaus.groovy:groovy-all:2.4.5"
|
||||
compile 'com.jayway.jsonpath:json-path-assert:2.2.0'
|
||||
|
||||
testCompile "com.github.tomakehurst:wiremock"
|
||||
testCompile "com.github.tomakehurst:wiremock:2.5.1"
|
||||
testCompile "org.spockframework:spock-spring:1.0-groovy-2.4"
|
||||
testCompile "com.jayway.restassured:rest-assured:$restAssuredVersion"
|
||||
testCompile "com.jayway.restassured:spring-mock-mvc:$restAssuredVersion"
|
||||
testCompile "ch.qos.logback:logback-classic:1.1.2"
|
||||
testCompile("org.springframework.cloud:spring-cloud-starter-contract-verifier")
|
||||
}
|
||||
|
||||
contracts {
|
||||
|
||||
@@ -29,40 +29,31 @@ allprojects {
|
||||
}
|
||||
|
||||
ext {
|
||||
restAssuredVersion = '2.5.0'
|
||||
spockVersion = '1.0-groovy-2.4'
|
||||
BOM_VERSION = "Dalston.BUILD-SNAPSHOT"
|
||||
wiremockVersion = '2.5.1'
|
||||
|
||||
contractVerifierStubsBaseDirectory = 'src/test/resources/stubs'
|
||||
}
|
||||
|
||||
subprojects {
|
||||
apply plugin: 'groovy'
|
||||
apply plugin: 'spring-boot'
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
mavenLocal()
|
||||
maven { url "http://repo.spring.io/snapshot" }
|
||||
maven { url "http://repo.spring.io/milestone" }
|
||||
maven { url "http://repo.spring.io/release" }
|
||||
}
|
||||
|
||||
dependencyManagement {
|
||||
imports {
|
||||
mavenBom "org.springframework.cloud:spring-cloud-dependencies:$BOM_VERSION"
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
testCompile 'org.codehaus.groovy:groovy-all'
|
||||
testCompile 'org.codehaus.groovy:groovy-all:2.4.5'
|
||||
testCompile "org.spockframework:spock-core:$spockVersion"
|
||||
testCompile 'junit:junit'
|
||||
testCompile "com.github.tomakehurst:wiremock"
|
||||
testCompile("org.springframework.cloud:spring-cloud-starter-contract-verifier")
|
||||
testCompile 'junit:junit:4.12'
|
||||
testCompile "com.github.tomakehurst:wiremock:$wiremockVersion"
|
||||
}
|
||||
}
|
||||
|
||||
configure([project(':fraudDetectionService'), project(':loanApplicationService')]) {
|
||||
apply plugin: 'spring-boot'
|
||||
apply plugin: 'maven-publish'
|
||||
|
||||
ext['jetty.version'] = '9.2.17.v20160517'
|
||||
@@ -90,7 +81,7 @@ configure([project(':fraudDetectionService'), project(':loanApplicationService')
|
||||
testCompile "org.mockito:mockito-core"
|
||||
testCompile "org.springframework:spring-test"
|
||||
testCompile "org.springframework.boot:spring-boot-test"
|
||||
testCompile("com.github.tomakehurst:wiremock") {
|
||||
testCompile("com.github.tomakehurst:wiremock:2.5.1") {
|
||||
exclude group: 'org.eclipse.jetty'
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ buildscript {
|
||||
ext {
|
||||
restAssuredVersion = '2.5.0'
|
||||
spockVersion = '1.0-groovy-2.4'
|
||||
BOM_VERSION = "Dalston.BUILD-SNAPSHOT"
|
||||
wiremockVersion = '2.5.1'
|
||||
|
||||
contractVerifierStubsBaseDirectory = 'src/test/resources/stubs'
|
||||
}
|
||||
@@ -36,28 +36,17 @@ group = 'org.springframework.cloud.testprojects'
|
||||
|
||||
subprojects {
|
||||
apply plugin: 'groovy'
|
||||
apply plugin: 'spring-boot'
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
mavenLocal()
|
||||
maven { url "http://repo.spring.io/snapshot" }
|
||||
maven { url "http://repo.spring.io/milestone" }
|
||||
maven { url "http://repo.spring.io/release" }
|
||||
}
|
||||
|
||||
dependencyManagement {
|
||||
imports {
|
||||
mavenBom "org.springframework.cloud:spring-cloud-dependencies:$BOM_VERSION"
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
testCompile "org.codehaus.groovy:groovy-all:2.4.5"
|
||||
testCompile "org.spockframework:spock-core:$spockVersion"
|
||||
testCompile "junit:junit"
|
||||
testCompile "com.github.tomakehurst:wiremock"
|
||||
testCompile("org.springframework.cloud:spring-cloud-starter-contract-verifier")
|
||||
testCompile("junit:junit:4.12")
|
||||
testCompile "com.github.tomakehurst:wiremock:$wiremockVersion"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,8 +86,6 @@ configure([project(':fraudDetectionService'), project(':loanApplicationService')
|
||||
testCompile "org.springframework.boot:spring-boot-test"
|
||||
testCompile "com.jayway.restassured:rest-assured:$restAssuredVersion"
|
||||
testCompile "com.jayway.restassured:spring-mock-mvc:$restAssuredVersion"
|
||||
testCompile "com.jayway.restassured:spring-mock-mvc:$restAssuredVersion"
|
||||
testCompile "com.toomuchcoding.jsonassert:jsonassert:${jsonAssertVersion}"
|
||||
}
|
||||
|
||||
task cleanup(type: Delete) {
|
||||
|
||||
@@ -27,7 +27,7 @@ buildscript {
|
||||
ext {
|
||||
restAssuredVersion = '2.5.0'
|
||||
spockVersion = '1.0-groovy-2.4'
|
||||
BOM_VERSION = "Dalston.BUILD-SNAPSHOT"
|
||||
wiremockVersion = '2.5.1'
|
||||
|
||||
contractVerifierStubsBaseDirectory = 'src/test/resources/stubs'
|
||||
}
|
||||
@@ -37,27 +37,17 @@ group = 'org.springframework.cloud.testprojects'
|
||||
subprojects {
|
||||
apply plugin: 'groovy'
|
||||
apply plugin: 'maven-publish'
|
||||
apply plugin: 'spring-boot'
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
mavenLocal()
|
||||
maven { url "http://repo.spring.io/snapshot" }
|
||||
maven { url "http://repo.spring.io/milestone" }
|
||||
maven { url "http://repo.spring.io/release" }
|
||||
}
|
||||
|
||||
dependencyManagement {
|
||||
imports {
|
||||
mavenBom "org.springframework.cloud:spring-cloud-dependencies:$BOM_VERSION"
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
testCompile "org.codehaus.groovy:groovy-all"
|
||||
testCompile "org.codehaus.groovy:groovy-all:2.4.5"
|
||||
testCompile "org.spockframework:spock-core:$spockVersion"
|
||||
testCompile("junit:junit:4.12")
|
||||
testCompile("org.springframework.cloud:spring-cloud-starter-contract-verifier")
|
||||
testCompile "com.github.tomakehurst:wiremock:$wiremockVersion"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,6 +107,8 @@ configure([project(':fraudDetectionService'), project(':loanApplicationService')
|
||||
testCompile "org.mockito:mockito-core"
|
||||
testCompile "org.springframework:spring-test"
|
||||
testCompile "org.springframework.boot:spring-boot-test"
|
||||
testCompile "com.jayway.restassured:rest-assured:$restAssuredVersion"
|
||||
testCompile "com.jayway.restassured:spring-mock-mvc:$restAssuredVersion"
|
||||
}
|
||||
|
||||
task cleanup(type: Delete) {
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package org.springframework.cloud.contract.verifier.dsl.wiremock;
|
||||
|
||||
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
|
||||
|
||||
/**
|
||||
* @author Marcin Grzejszczak
|
||||
*/
|
||||
public class WireMockStubMapping {
|
||||
public static StubMapping buildFrom(String mappingDefinition) {
|
||||
return StubMapping.buildFrom(mappingDefinition);
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,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.spec.Contract
|
||||
import org.springframework.cloud.contract.verifier.dsl.wiremock.WireMockStubMapping
|
||||
import org.springframework.cloud.contract.verifier.dsl.wiremock.WireMockStubStrategy
|
||||
import org.springframework.cloud.contract.verifier.file.ContractMetadata
|
||||
|
||||
@@ -27,7 +28,7 @@ import java.util.regex.Pattern
|
||||
trait WireMockStubVerifier {
|
||||
|
||||
void stubMappingIsValidWireMockStub(String mappingDefinition) {
|
||||
StubMapping stubMapping = StubMapping.buildFrom(mappingDefinition)
|
||||
StubMapping stubMapping = WireMockStubMapping.buildFrom(mappingDefinition)
|
||||
stubMapping.request.bodyPatterns.findAll { it.isPresent() && it instanceof RegexPattern }.every {
|
||||
Pattern.compile(it.getValue())
|
||||
}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package org.springframework.cloud.contract.verifier.dsl.wiremock
|
||||
|
||||
import spock.lang.Specification
|
||||
|
||||
/**
|
||||
* @author Marcin Grzejszczak
|
||||
*/
|
||||
class WireMockStubMappingSpec 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"() {
|
||||
expect:
|
||||
WireMockStubMapping.buildFrom(stub_2_1_7)
|
||||
}
|
||||
|
||||
def "should successfully parse a WireMock 2.5.1 stub"() {
|
||||
expect:
|
||||
WireMockStubMapping.buildFrom(stub_2_5_1)
|
||||
}
|
||||
}
|
||||
@@ -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(WireMockStubMapping
|
||||
.buildFrom(StreamUtils.copyToString(resource.getInputStream(), Charset.forName("UTF-8"))));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package org.springframework.cloud.contract.wiremock;
|
||||
|
||||
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
|
||||
|
||||
/**
|
||||
* @author Marcin Grzejszczak
|
||||
*/
|
||||
public class WireMockStubMapping {
|
||||
public static StubMapping buildFrom(String mappingDefinition) {
|
||||
return StubMapping.buildFrom(mappingDefinition);
|
||||
}
|
||||
}
|
||||
@@ -74,6 +74,19 @@ public class ResourcesFileSource implements FileSource {
|
||||
throw new IllegalStateException("Cannot create file for " + name);
|
||||
}
|
||||
|
||||
@Override public TextFile getTextFileNamed(String name) {
|
||||
for (FileSource resource : this.sources) {
|
||||
TextFile file = resource.getTextFileNamed(name);
|
||||
try {
|
||||
file.readContentsAsString();
|
||||
return file;
|
||||
} catch (RuntimeException e) {
|
||||
// Ignore
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createIfNecessary() {
|
||||
throw new UnsupportedOperationException("Resource file sources are read-only");
|
||||
@@ -156,4 +169,8 @@ public class ResourcesFileSource implements FileSource {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override public void deleteFile(String name) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,151 @@
|
||||
package org.springframework.cloud.contract.wiremock.restdocs;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import com.github.tomakehurst.wiremock.client.BasicCredentials;
|
||||
import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
|
||||
import com.github.tomakehurst.wiremock.client.ScenarioMappingBuilder;
|
||||
import com.github.tomakehurst.wiremock.extension.Parameters;
|
||||
import com.github.tomakehurst.wiremock.http.Request;
|
||||
import com.github.tomakehurst.wiremock.http.RequestMethod;
|
||||
import com.github.tomakehurst.wiremock.http.ResponseDefinition;
|
||||
import com.github.tomakehurst.wiremock.matching.RequestPattern;
|
||||
import com.github.tomakehurst.wiremock.matching.RequestPatternBuilder;
|
||||
import com.github.tomakehurst.wiremock.matching.StringValuePattern;
|
||||
import com.github.tomakehurst.wiremock.matching.UrlPattern;
|
||||
import com.github.tomakehurst.wiremock.matching.ValueMatcher;
|
||||
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
|
||||
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
|
||||
|
||||
class BasicMappingBuilder implements ScenarioMappingBuilder {
|
||||
|
||||
private RequestPatternBuilder requestPatternBuilder;
|
||||
private ResponseDefinitionBuilder responseDefBuilder;
|
||||
private Integer priority;
|
||||
private String scenarioName;
|
||||
private String requiredScenarioState;
|
||||
private String newScenarioState;
|
||||
private UUID id = UUID.randomUUID();
|
||||
private String name;
|
||||
private boolean isPersistent = false;
|
||||
private Map<String, Parameters> postServeActions = new LinkedHashMap<>();
|
||||
|
||||
BasicMappingBuilder(RequestMethod method, UrlPattern urlPattern) {
|
||||
this.requestPatternBuilder = new RequestPatternBuilder(method, urlPattern);
|
||||
}
|
||||
|
||||
BasicMappingBuilder(ValueMatcher<Request> requestMatcher) {
|
||||
this.requestPatternBuilder = new RequestPatternBuilder(requestMatcher);
|
||||
}
|
||||
|
||||
BasicMappingBuilder(String customRequestMatcherName, Parameters parameters) {
|
||||
this.requestPatternBuilder = new RequestPatternBuilder(customRequestMatcherName,
|
||||
parameters);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BasicMappingBuilder willReturn(ResponseDefinitionBuilder responseDefBuilder) {
|
||||
this.responseDefBuilder = responseDefBuilder;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override public BasicMappingBuilder atPriority(Integer priority) {
|
||||
this.priority = priority;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BasicMappingBuilder withHeader(String key, StringValuePattern headerPattern) {
|
||||
this.requestPatternBuilder.withHeader(key, headerPattern);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override public BasicMappingBuilder withCookie(String name,
|
||||
StringValuePattern cookieValuePattern) {
|
||||
this.requestPatternBuilder.withCookie(name, cookieValuePattern);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override public BasicMappingBuilder withQueryParam(String key,
|
||||
StringValuePattern queryParamPattern) {
|
||||
this.requestPatternBuilder.withQueryParam(key, queryParamPattern);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override public BasicMappingBuilder withRequestBody(StringValuePattern bodyPattern) {
|
||||
this.requestPatternBuilder.withRequestBody(bodyPattern);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override public BasicMappingBuilder inScenario(String scenarioName) {
|
||||
this.scenarioName = scenarioName;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override public BasicMappingBuilder whenScenarioStateIs(String stateName) {
|
||||
this.requiredScenarioState = stateName;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override public BasicMappingBuilder willSetStateTo(String stateName) {
|
||||
this.newScenarioState = stateName;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override public BasicMappingBuilder withId(UUID id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override public BasicMappingBuilder withName(String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override public ScenarioMappingBuilder persistent() {
|
||||
this.isPersistent = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override public BasicMappingBuilder withBasicAuth(String username, String password) {
|
||||
this.requestPatternBuilder
|
||||
.withBasicAuth(new BasicCredentials(username, password));
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override public <P> BasicMappingBuilder withPostServeAction(String extensionName,
|
||||
P parameters) {
|
||||
Parameters params = parameters instanceof Parameters ?
|
||||
(Parameters) parameters :
|
||||
Parameters.of(parameters);
|
||||
this.postServeActions.put(extensionName, params);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override public StubMapping build() {
|
||||
if (this.scenarioName == null && (this.requiredScenarioState != null
|
||||
|| this.newScenarioState != null)) {
|
||||
throw new IllegalStateException(
|
||||
"Scenario name must be specified to require or set a new scenario state");
|
||||
}
|
||||
RequestPattern requestPattern = this.requestPatternBuilder.build();
|
||||
ResponseDefinition response = (this.responseDefBuilder != null ?
|
||||
this.responseDefBuilder :
|
||||
aResponse()).build();
|
||||
StubMapping mapping = new StubMapping(requestPattern, response);
|
||||
mapping.setPriority(this.priority);
|
||||
mapping.setScenarioName(this.scenarioName);
|
||||
mapping.setRequiredScenarioState(this.requiredScenarioState);
|
||||
mapping.setNewScenarioState(this.newScenarioState);
|
||||
mapping.setUuid(this.id);
|
||||
mapping.setName(this.name);
|
||||
mapping.setPersistent(this.isPersistent);
|
||||
mapping.setPostServeActions(
|
||||
this.postServeActions.isEmpty() ? null : this.postServeActions);
|
||||
return mapping;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -32,7 +32,7 @@ import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.StreamUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import com.github.tomakehurst.wiremock.client.RemoteMappingBuilder;
|
||||
import com.github.tomakehurst.wiremock.client.MappingBuilder;
|
||||
import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
|
||||
import com.github.tomakehurst.wiremock.matching.MatchResult;
|
||||
import com.github.tomakehurst.wiremock.servlet.WireMockHttpServletRequestAdapter;
|
||||
@@ -49,7 +49,7 @@ public class ContractRequestHandler implements ResultHandler {
|
||||
private MediaType contentType;
|
||||
private String name;
|
||||
|
||||
private RemoteMappingBuilder<?, ?> builder;
|
||||
private MappingBuilder builder;
|
||||
|
||||
public ContractRequestHandler() {
|
||||
}
|
||||
@@ -117,7 +117,7 @@ public class ContractRequestHandler implements ResultHandler {
|
||||
return map;
|
||||
}
|
||||
|
||||
public ContractRequestHandler wiremock(RemoteMappingBuilder<?, ?> builder) {
|
||||
public ContractRequestHandler wiremock(MappingBuilder builder) {
|
||||
this.builder = builder;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ import org.springframework.restdocs.RestDocumentationContext;
|
||||
import org.springframework.restdocs.operation.Operation;
|
||||
import org.springframework.restdocs.snippet.Snippet;
|
||||
|
||||
import com.github.tomakehurst.wiremock.client.RemoteMappingBuilder;
|
||||
import com.github.tomakehurst.wiremock.client.MappingBuilder;
|
||||
import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
|
||||
import com.github.tomakehurst.wiremock.common.Json;
|
||||
import com.github.tomakehurst.wiremock.http.HttpHeader;
|
||||
@@ -112,11 +112,11 @@ public class WireMockSnippet implements Snippet {
|
||||
.withStatus(operation.getResponse().getStatus().value());
|
||||
}
|
||||
|
||||
private RemoteMappingBuilder<?, ?> request(Operation operation) {
|
||||
private MappingBuilder request(Operation operation) {
|
||||
return requestHeaders(requestBuilder(operation), operation);
|
||||
}
|
||||
|
||||
private RemoteMappingBuilder<?, ?> requestHeaders(RemoteMappingBuilder<?, ?> request,
|
||||
private MappingBuilder requestHeaders(MappingBuilder request,
|
||||
Operation operation) {
|
||||
org.springframework.http.HttpHeaders headers = operation.getRequest()
|
||||
.getHeaders();
|
||||
@@ -136,7 +136,7 @@ public class WireMockSnippet implements Snippet {
|
||||
return request;
|
||||
}
|
||||
|
||||
private RemoteMappingBuilder<?, ?> requestBuilder(Operation operation) {
|
||||
private MappingBuilder requestBuilder(Operation operation) {
|
||||
switch (operation.getRequest().getMethod()) {
|
||||
case DELETE:
|
||||
return delete(requestPattern(operation));
|
||||
@@ -151,7 +151,7 @@ public class WireMockSnippet implements Snippet {
|
||||
}
|
||||
}
|
||||
|
||||
private RemoteMappingBuilder<?, ?> bodyPattern(RemoteMappingBuilder<?, ?> builder,
|
||||
private MappingBuilder bodyPattern(MappingBuilder builder,
|
||||
String content) {
|
||||
if (this.jsonPaths != null) {
|
||||
for (String jsonPath : this.jsonPaths) {
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package org.springframework.cloud.contract.wiremock;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author Marcin Grzejszczak
|
||||
*/
|
||||
public class WireMockStubMappingTest {
|
||||
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() throws JSONException {
|
||||
// when:
|
||||
WireMockStubMapping.buildFrom(stub_2_1_7);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_successfully_parse_a_WireMock_2_5_1_stub() throws JSONException {
|
||||
// when:
|
||||
WireMockStubMapping.buildFrom(stub_2_5_1);
|
||||
}
|
||||
}
|
||||
@@ -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.WireMockStubMapping;
|
||||
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 = WireMockStubMapping
|
||||
.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 = WireMockStubMapping
|
||||
.buildFrom(new String(Files.readAllBytes(stub.toPath())));
|
||||
assertThat(stubMapping.getRequest().getBodyPatterns().get(0))
|
||||
.isInstanceOf(EqualToJsonPattern.class);
|
||||
|
||||
Reference in New Issue
Block a user