Fixed naming
This commit is contained in:
@@ -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;
|
||||
@@ -29,14 +30,22 @@ import org.springframework.cloud.contract.verifier.file.ContractMetadata;
|
||||
public interface StubGenerator {
|
||||
|
||||
/**
|
||||
* @param contractFileName - file name
|
||||
* @param fileName - file name
|
||||
* @return {@code true} if the converter can handle the file to convert it into a
|
||||
* stub.
|
||||
* @deprecated all present converters will be picked
|
||||
* @deprecated use {@link StubGenerator#canReadStubMapping(File)}
|
||||
*/
|
||||
@Deprecated
|
||||
default boolean canHandleFileName(String contractFileName) {
|
||||
return contractFileName.endsWith(fileExtension());
|
||||
default boolean canReadStubMapping(String fileName) {
|
||||
return fileName.endsWith(fileExtension());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mapping - potential stub mapping mapping
|
||||
* @return {@code true} if this converter could have generated this mapping stub.
|
||||
*/
|
||||
default boolean canReadStubMapping(File mapping) {
|
||||
return mapping.getName().endsWith(fileExtension());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -61,7 +70,8 @@ public interface StubGenerator {
|
||||
String generateOutputFileNameForInput(String inputFileName);
|
||||
|
||||
/**
|
||||
* Describes the file extension that this stub generator can handle.
|
||||
* Describes the file extension of the generated mapping that this stub generator can
|
||||
* handle.
|
||||
* @return string describing the file extension
|
||||
*/
|
||||
default String fileExtension() {
|
||||
|
||||
@@ -16,10 +16,12 @@
|
||||
|
||||
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;
|
||||
|
||||
@@ -42,6 +44,18 @@ public class StubGeneratorProvider {
|
||||
this.converters.addAll(converters);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param file - stub mapping file
|
||||
* @return collection of stub generators matching the stub mapping
|
||||
* @deprecated use {@link #allOrDefault(StubGenerator)}
|
||||
*/
|
||||
@Deprecated
|
||||
public Collection<StubGenerator> converterForName(final File file) {
|
||||
return this.converters.stream()
|
||||
.filter(stubGenerator -> stubGenerator.canReadStubMapping(file))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public Collection<StubGenerator> allOrDefault(StubGenerator defaultStubGenerator) {
|
||||
return this.converters.isEmpty() ? Collections.singletonList(defaultStubGenerator)
|
||||
: this.converters;
|
||||
|
||||
@@ -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,22 @@ public abstract class DslToWireMockConverter implements StubGenerator {
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canReadStubMapping(File mapping) {
|
||||
if (!mapping.getName().endsWith(".json")) {
|
||||
return false;
|
||||
}
|
||||
try (InputStream stream = Files.newInputStream(mapping.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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
# Stub converters
|
||||
org.springframework.cloud.contract.verifier.converter.StubGenerator=\
|
||||
org.springframework.cloud.contract.verifier.wiremock.DslToWireMockClientConverter
|
||||
@@ -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")
|
||||
}
|
||||
@@ -194,7 +194,12 @@ org.springframework.cloud.contract.spec.Contract.make {
|
||||
private StubGenerator stubGenerator(String stub) {
|
||||
return new StubGenerator() {
|
||||
@Override
|
||||
boolean canHandleFileName(String contractFileName) {
|
||||
boolean canReadStubMapping(String fileName) {
|
||||
return true
|
||||
}
|
||||
|
||||
@Override
|
||||
boolean canReadStubMapping(File mapping) {
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user