Merge remote-tracking branch 'origin/2.2.x'
This commit is contained in:
@@ -120,8 +120,7 @@ public class RecursiveFilesConverter {
|
||||
}
|
||||
File sourceFile = contract.getPath().toFile();
|
||||
Collection<StubGenerator> stubGenerators = contract.getConvertedContract() != null
|
||||
? holder.allOrDefault(new DslToWireMockClientConverter())
|
||||
: holder.converterForName(sourceFile.getAbsolutePath());
|
||||
? holder.allOrDefault(new DslToWireMockClientConverter()) : holder.converterForName(sourceFile);
|
||||
try {
|
||||
String path = sourceFile.getPath();
|
||||
if (excludeBuildFolders && (matchesPath(path, "target") || matchesPath(path, "build"))) {
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.cloud.contract.verifier.converter;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -32,12 +33,12 @@ import org.springframework.cloud.contract.verifier.file.ContractMetadata;
|
||||
public interface StubGenerator<T> {
|
||||
|
||||
/**
|
||||
* @param fileName - file name or absolute path of a contract
|
||||
* @return {@code true} if the converter can handle the contract file to convert it
|
||||
* into a stub.
|
||||
* @param file - file
|
||||
* @return {@code true} if the converter can handle the file to convert it into a
|
||||
* stub.
|
||||
*/
|
||||
default boolean canHandleFileName(String fileName) {
|
||||
return true;
|
||||
default boolean canHandleFileName(File file) {
|
||||
return file.getName().endsWith(fileExtension());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -81,7 +82,7 @@ public interface StubGenerator<T> {
|
||||
* @param inputFileName - name of the input file
|
||||
* @return the name of the converted stub file. If you have multiple contracts in a
|
||||
* single file then a prefix will be added to the generated file. If you provide the
|
||||
* {@link Contract#getName()} field then that field will override the generated file
|
||||
* {@link Contract#getName} field then that field will override the generated file
|
||||
* name.
|
||||
*
|
||||
* Example: name of file with 2 contracts is {@code foo.groovy}, it will be converted
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.cloud.contract.verifier.converter;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
@@ -32,7 +33,7 @@ import org.springframework.core.io.support.SpringFactoriesLoader;
|
||||
*/
|
||||
public class StubGeneratorProvider {
|
||||
|
||||
private final List<StubGenerator> converters = new ArrayList<StubGenerator>();
|
||||
private final List<StubGenerator> converters = new ArrayList<>();
|
||||
|
||||
public StubGeneratorProvider() {
|
||||
this.converters.addAll(SpringFactoriesLoader.loadFactories(StubGenerator.class, null));
|
||||
@@ -42,8 +43,8 @@ public class StubGeneratorProvider {
|
||||
this.converters.addAll(converters);
|
||||
}
|
||||
|
||||
public Collection<StubGenerator> converterForName(final String fileName) {
|
||||
return this.converters.stream().filter(stubGenerator -> stubGenerator.canHandleFileName(fileName))
|
||||
public Collection<StubGenerator> converterForName(final File file) {
|
||||
return this.converters.stream().filter(stubGenerator -> stubGenerator.canHandleFileName(file))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
|
||||
@@ -17,11 +17,18 @@
|
||||
package org.springframework.cloud.contract.verifier.wiremock;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.file.Files;
|
||||
|
||||
import com.github.tomakehurst.wiremock.common.JsonException;
|
||||
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.cloud.contract.verifier.converter.StubGenerator;
|
||||
import org.springframework.util.StreamUtils;
|
||||
|
||||
/**
|
||||
* WireMock implementation of the {@link StubGenerator}.
|
||||
@@ -30,6 +37,8 @@ import org.springframework.cloud.contract.verifier.converter.StubGenerator;
|
||||
*/
|
||||
public abstract class DslToWireMockConverter implements StubGenerator<StubMapping> {
|
||||
|
||||
private static final Log log = LogFactory.getLog(DslToWireMockConverter.class);
|
||||
|
||||
@Override
|
||||
public String generateOutputFileNameForInput(String inputFileName) {
|
||||
return inputFileName.replaceAll(extension(inputFileName), "json");
|
||||
@@ -44,15 +53,18 @@ public abstract class DslToWireMockConverter implements StubGenerator<StubMappin
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canHandleFileName(String fileName) {
|
||||
if (!fileName.endsWith(fileExtension())) {
|
||||
public boolean canHandleFileName(File file) {
|
||||
if (!file.getName().endsWith(fileExtension())) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
StubMapping.buildFrom(new String(Files.readAllBytes(new File(fileName).toPath())));
|
||||
try (InputStream stream = Files.newInputStream(file.toPath())) {
|
||||
StubMapping.buildFrom(StreamUtils.copyToString(stream, Charset.forName("UTF-8")));
|
||||
return true;
|
||||
}
|
||||
catch (Exception e) {
|
||||
catch (IOException | JsonException e) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Cannot read file", e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -193,8 +193,9 @@ org.springframework.cloud.contract.spec.Contract.make {
|
||||
|
||||
private StubGenerator stubGenerator(String stub) {
|
||||
return new StubGenerator() {
|
||||
|
||||
@Override
|
||||
boolean canHandleFileName(String fileName) {
|
||||
boolean canHandleFileName(File fileName) {
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user