This commit is contained in:
Marcin Grzejszczak
2016-12-05 15:52:36 +01:00
parent 6602f98be6
commit e7619a1117
4 changed files with 17 additions and 9 deletions

View File

@@ -20,7 +20,7 @@ import groovy.transform.CompileStatic
import org.springframework.cloud.contract.verifier.file.ContractMetadata
/**
* Converts contracts into their stub representation
* Converts contracts into their stub representation.
*
* @since 1.0.0
*/
@@ -28,17 +28,17 @@ import org.springframework.cloud.contract.verifier.file.ContractMetadata
interface SingleFileConverter {
/**
* Returns {@code true} if the converter can handle the file.
* Returns {@code true} if the converter can handle the file to convert it into a stub.
*/
boolean canHandleFileName(String fileName)
/**
* Returns the content of the converted file
* Returns the content of the converted file. The content will be the stub.
*/
String convertContent(String rootName, ContractMetadata content)
/**
* Returns the name of the converted file
* Returns the name of the converted stub file.
*/
String generateOutputFileNameForInput(String inputFileName)
}

View File

@@ -21,6 +21,7 @@ import org.springframework.cloud.contract.verifier.dsl.wiremock.WireMockStubStra
import org.springframework.cloud.contract.verifier.file.ContractMetadata
import java.nio.charset.StandardCharsets
/**
* Converts DSLs to WireMock stubs
*
@@ -31,8 +32,8 @@ class DslToWireMockClientConverter extends DslToWireMockConverter {
@Override
String convertContent(String rootName, ContractMetadata contract) {
String dslContent = contract.path.getText(StandardCharsets.UTF_8.toString())
return new WireMockStubStrategy(rootName, contract,
createGroovyDSLFromStringContent(dslContent)).toWireMockClientStub()
contract.convertedContract ?: createGroovyDSLFromStringContent(contract.path.getText(StandardCharsets.UTF_8.toString()))
).toWireMockClientStub()
}
}

View File

@@ -36,7 +36,15 @@ abstract class DslToWireMockConverter implements SingleFileConverter {
@Override
String generateOutputFileNameForInput(String inputFileName) {
return inputFileName.replaceAll('.groovy', '.json')
return inputFileName.replaceAll(extension(inputFileName), 'json')
}
private String extension(String inputFileName) {
int i = inputFileName.lastIndexOf('.')
if (i > 0) {
return inputFileName.substring(i + 1)
}
return ""
}
protected Contract createGroovyDSLFromStringContent(String groovyDslAsString) {

View File

@@ -56,7 +56,6 @@ class RecursiveFilesConverter {
this.holder = holder ?: new SingleFileConvertersHolder()
}
@Deprecated
RecursiveFilesConverter(SingleFileConverter singleFileConverter, ContractVerifierConfigProperties properties) {
this.properties = properties
@@ -83,7 +82,7 @@ class RecursiveFilesConverter {
File sourceFile = contract.path.toFile()
SingleFileConverter singleFileConverter = holder.converterForName(sourceFile.name);
try {
if (!contract.convertedContract && !singleFileConverter.canHandleFileName(sourceFile.name)) {
if (!contract.convertedContract && !singleFileConverter) {
return
}
String convertedContent = singleFileConverter.convertContent(entry.key.last().toString(), contract)