Merge remote-tracking branch 'origin/2.2.x'

This commit is contained in:
Marcin Grzejszczak
2020-11-10 22:19:23 +01:00
7 changed files with 20 additions and 25 deletions

View File

@@ -31,6 +31,7 @@ import org.springframework.cloud.contract.spec.Contract;
import org.springframework.cloud.contract.verifier.converter.StubGenerator;
import org.springframework.cloud.contract.verifier.converter.StubGeneratorProvider;
import org.springframework.cloud.contract.verifier.file.ContractMetadata;
import org.springframework.cloud.contract.verifier.wiremock.DslToWireMockClientConverter;
import org.springframework.util.StringUtils;
final class MappingGenerator {
@@ -43,7 +44,7 @@ final class MappingGenerator {
static Collection<Path> toMappings(File contractFile, Collection<Contract> contracts, File mappingsFolder) {
StubGeneratorProvider provider = new StubGeneratorProvider();
Collection<StubGenerator> stubGenerators = provider.converterForName(contractFile);
Collection<StubGenerator> stubGenerators = provider.allOrDefault(new DslToWireMockClientConverter());
if (log.isDebugEnabled()) {
log.debug("Found following matching stub generators " + stubGenerators);
}

View File

@@ -37,6 +37,7 @@ import org.springframework.cloud.contract.verifier.converter.RecursiveFilesConve
import org.springframework.cloud.contract.verifier.converter.StubGenerator;
import org.springframework.cloud.contract.verifier.converter.StubGeneratorProvider;
import org.springframework.cloud.contract.verifier.messaging.MessageVerifier;
import org.springframework.cloud.contract.verifier.wiremock.DslToWireMockClientConverter;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.SpringFactoriesLoader;
@@ -129,9 +130,10 @@ class StubRunnerFactory {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
File fileToConvert = file.toFile();
Collection<StubGenerator> stubGenerators = this.provider.converterForName(fileToConvert);
if (!stubGenerators.isEmpty() || this.wireMockHttpServerStub.isAccepted(fileToConvert)) {
File potentialStubMapping = file.toFile();
Collection<StubGenerator> stubGenerators = this.provider
.allOrDefault(new DslToWireMockClientConverter());
if (stubGenerators.stream().anyMatch(s -> s.canReadStubMapping(potentialStubMapping))) {
if (log.isDebugEnabled()) {
log.debug("Deleting file [" + file.toString() + "] since it contains a valid mapping.");
}

View File

@@ -119,8 +119,7 @@ public class RecursiveFilesConverter {
log.debug("Will create a stub for contract [" + contract + "]");
}
File sourceFile = contract.getPath().toFile();
Collection<StubGenerator> stubGenerators = contract.getConvertedContract() != null
? holder.allOrDefault(new DslToWireMockClientConverter()) : holder.converterForName(sourceFile);
Collection<StubGenerator> stubGenerators = holder.allOrDefault(new DslToWireMockClientConverter());
try {
String path = sourceFile.getPath();
if (excludeBuildFolders && (matchesPath(path, "target") || matchesPath(path, "build"))) {

View File

@@ -33,12 +33,11 @@ import org.springframework.cloud.contract.verifier.file.ContractMetadata;
public interface StubGenerator<T> {
/**
* @param file - file
* @return {@code true} if the converter can handle the file to convert it into a
* stub.
* @param mapping - potential stub mapping mapping
* @return {@code true} if this converter could have generated this mapping stub.
*/
default boolean canHandleFileName(File file) {
return file.getName().endsWith(fileExtension());
default boolean canReadStubMapping(File mapping) {
return mapping.getName().endsWith(fileExtension());
}
/**
@@ -92,8 +91,9 @@ public interface StubGenerator<T> {
String generateOutputFileNameForInput(String inputFileName);
/**
* Describes the file extension that this stub generator will generate.
* @return string describing the file extension starting with a dot
* Describes the file extension of the generated mapping that this stub generator can
* handle.
* @return string describing the file extension
*/
default String fileExtension() {
return ".json";

View File

@@ -16,12 +16,10 @@
package org.springframework.cloud.contract.verifier.converter;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.core.io.support.SpringFactoriesLoader;
@@ -43,11 +41,6 @@ public class StubGeneratorProvider {
this.converters.addAll(converters);
}
public Collection<StubGenerator> converterForName(final File file) {
return this.converters.stream().filter(stubGenerator -> stubGenerator.canHandleFileName(file))
.collect(Collectors.toList());
}
public Collection<StubGenerator> allOrDefault(StubGenerator defaultStubGenerator) {
return this.converters.isEmpty() ? Collections.singletonList(defaultStubGenerator) : this.converters;
}

View File

@@ -53,11 +53,11 @@ public abstract class DslToWireMockConverter implements StubGenerator<StubMappin
}
@Override
public boolean canHandleFileName(File file) {
if (!file.getName().endsWith(fileExtension())) {
public boolean canReadStubMapping(File mapping) {
if (!mapping.getName().endsWith(".json")) {
return false;
}
try (InputStream stream = Files.newInputStream(file.toPath())) {
try (InputStream stream = Files.newInputStream(mapping.toPath())) {
StubMapping.buildFrom(StreamUtils.copyToString(stream, Charset.forName("UTF-8")));
return true;
}

View File

@@ -110,7 +110,7 @@ org.springframework.cloud.contract.spec.Contract.make {
}"""
and:
StubGenerator stubGenerator = Stub(StubGenerator)
stubGenerator.canHandleFileName(_) >> { true }
stubGenerator.canReadStubMapping(_) >> { true }
stubGenerator.convertContents(_, _) >> {
throw new NullPointerException("Test conversion error")
}
@@ -195,7 +195,7 @@ org.springframework.cloud.contract.spec.Contract.make {
return new StubGenerator() {
@Override
boolean canHandleFileName(File fileName) {
boolean canReadStubMapping(File mapping) {
return true
}