Introduced File for verifying if a stubgenerator should be used

This commit is contained in:
Marcin Grzejszczak
2020-11-10 14:50:26 +01:00
parent 8871838e69
commit 0cbf8e9ca7
9 changed files with 69 additions and 17 deletions

View File

@@ -45,7 +45,7 @@ final class MappingGenerator {
File mappingsFolder) {
StubGeneratorProvider provider = new StubGeneratorProvider();
Collection<StubGenerator> stubGenerators = provider
.converterForName(contractFile.getName());
.converterForName(contractFile);
if (log.isDebugEnabled()) {
log.debug("Found following matching stub generators " + stubGenerators);
}

View File

@@ -51,8 +51,8 @@ import org.springframework.cloud.contract.verifier.messaging.MessageVerifier;
* @author Olga Maciaszek-Sharma
* @since 2.1.0
*/
public class StubRunnerExtension implements BeforeAllCallback, AfterAllCallback, BeforeEachCallback, AfterEachCallback,
StubFinder, StubRunnerExtensionOptions {
public class StubRunnerExtension implements BeforeAllCallback, AfterAllCallback,
BeforeEachCallback, AfterEachCallback, StubFinder, StubRunnerExtensionOptions {
private static final String DELIMITER = ":";
@@ -84,7 +84,8 @@ public class StubRunnerExtension implements BeforeAllCallback, AfterAllCallback,
public void afterAll(ExtensionContext extensionContext) {
try {
after();
} finally {
}
finally {
this.afterAllCalled.set(true);
}
}
@@ -105,7 +106,8 @@ public class StubRunnerExtension implements BeforeAllCallback, AfterAllCallback,
public void beforeAll(ExtensionContext extensionContext) {
try {
before();
} finally {
}
finally {
this.beforeAllCalled.set(true);
}
}
@@ -133,7 +135,6 @@ public class StubRunnerExtension implements BeforeAllCallback, AfterAllCallback,
}
}
@Override
public URL findStubUrl(String groupId, String artifactId)
throws StubNotFoundException {

View File

@@ -99,8 +99,8 @@ public class StubRunnerCamelConfiguration {
@Override
public void process(Exchange exchange) {
if (log.isDebugEnabled()) {
log.debug("Got exchange [" + exchange + "]");
if (log.isTraceEnabled()) {
log.trace("Got exchange [" + exchange + "]");
}
}

View File

@@ -131,7 +131,7 @@ public class RecursiveFilesConverter {
Collection<StubGenerator> stubGenerators = contract
.getConvertedContract() != null
? holder.allOrDefault(new DslToWireMockClientConverter())
: holder.converterForName(sourceFile.getName());
: holder.converterForName(sourceFile);
try {
String path = sourceFile.getPath();
if (excludeBuildFolders && (matchesPath(path, "target")

View File

@@ -16,6 +16,7 @@
package org.springframework.cloud.contract.verifier.converter;
import java.io.File;
import java.util.Map;
import org.springframework.cloud.contract.spec.Contract;
@@ -32,11 +33,22 @@ public interface StubGenerator {
* @param fileName - file name
* @return {@code true} if the converter can handle the file to convert it into a
* stub.
* @deprecated use {@link StubGenerator#canHandleFileName(File)}
*/
@Deprecated
default boolean canHandleFileName(String fileName) {
return fileName.endsWith(fileExtension());
}
/**
* @param file - file
* @return {@code true} if the converter can handle the file to convert it into a
* stub.
*/
default boolean canHandleFileName(File file) {
return file.getName().endsWith(fileExtension());
}
/**
* @param rootName - root name of the contract
* @param content - metadata of the contract
@@ -49,7 +61,8 @@ public interface StubGenerator {
* @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#name} field then that field will override the generated file name.
* {@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
* by the implementation to {@code foo.json}. The recursive file converter will create

View File

@@ -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,6 +33,8 @@ import org.springframework.core.io.support.SpringFactoriesLoader;
*/
public class StubGeneratorProvider {
private final List<StubGenerator> converters = new ArrayList<>();
public StubGeneratorProvider() {
this.converters
.addAll(SpringFactoriesLoader.loadFactories(StubGenerator.class, null));
@@ -41,9 +44,9 @@ public class StubGeneratorProvider {
this.converters.addAll(converters);
}
public Collection<StubGenerator> converterForName(final String fileName) {
public Collection<StubGenerator> converterForName(final File file) {
return this.converters.stream()
.filter(stubGenerator -> stubGenerator.canHandleFileName(fileName))
.filter(stubGenerator -> stubGenerator.canHandleFileName(file))
.collect(Collectors.toList());
}
@@ -52,6 +55,4 @@ public class StubGeneratorProvider {
: this.converters;
}
private final List<StubGenerator> converters = new ArrayList<StubGenerator>();
}

View File

@@ -16,7 +16,19 @@
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}.
@@ -25,6 +37,8 @@ import org.springframework.cloud.contract.verifier.converter.StubGenerator;
*/
public abstract class DslToWireMockConverter implements StubGenerator {
private static final Log log = LogFactory.getLog(DslToWireMockConverter.class);
@Override
public String generateOutputFileNameForInput(String inputFileName) {
return inputFileName.replaceAll(extension(inputFileName), "json");
@@ -38,4 +52,19 @@ public abstract class DslToWireMockConverter implements StubGenerator {
return "";
}
@Override
public boolean canHandleFileName(File file) {
try (InputStream stream = Files.newInputStream(file.toPath())) {
StubMapping.buildFrom(
StreamUtils.copyToString(stream, Charset.forName("UTF-8")));
return true;
}
catch (IOException | JsonException e) {
if (log.isDebugEnabled()) {
log.debug("Cannot read file", e);
}
return false;
}
}
}

View File

@@ -198,6 +198,11 @@ org.springframework.cloud.contract.spec.Contract.make {
return true
}
@Override
boolean canHandleFileName(File fileName) {
return true
}
@Override
Map<Contract, String> convertContents(String rootName, ContractMetadata content) {
return [

View File

@@ -24,10 +24,13 @@ public class XmlBodyVerificationBuilderTest {
builder.addXmlResponseBodyCheck(blockBuilder, xml, matchers, xml, true);
// Then
String test = blockBuilder.toString();
assertThat(test).contains("DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();")
assertThat(test).contains(
"DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();")
.contains("builderFactory.setNamespaceAware(true);")
.contains("DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();")
.contains("Document parsedXml = documentBuilder.parse(new InputSource(new StringReader(")
.contains(
"DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();")
.contains(
"Document parsedXml = documentBuilder.parse(new InputSource(new StringReader(")
.contains(xml);
}