Fixed wrong conditional on missing beans for messaging; fixes gh-1513

This commit is contained in:
Marcin Grzejszczak
2020-09-21 11:21:48 +02:00
parent 6e951e60fd
commit b738eca9f4
11 changed files with 41 additions and 19 deletions

View File

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

View File

@@ -32,12 +32,12 @@ import org.springframework.cloud.contract.verifier.file.ContractMetadata;
public interface StubGenerator<T> {
/**
* @param fileName - file name
* @return {@code true} if the converter can handle the file to convert it into a
* stub.
* @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.
*/
default boolean canHandleFileName(String fileName) {
return fileName.endsWith(fileExtension());
return true;
}
/**
@@ -81,7 +81,8 @@ 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#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
@@ -90,8 +91,8 @@ public interface StubGenerator<T> {
String generateOutputFileNameForInput(String inputFileName);
/**
* Describes the file extension that this stub generator can handle.
* @return string describing the file extension
* Describes the file extension that this stub generator will generate.
* @return string describing the file extension starting with a dot
*/
default String fileExtension() {
return ".json";

View File

@@ -32,6 +32,8 @@ import org.springframework.core.io.support.SpringFactoriesLoader;
*/
public class StubGeneratorProvider {
private final List<StubGenerator> converters = new ArrayList<StubGenerator>();
public StubGeneratorProvider() {
this.converters.addAll(SpringFactoriesLoader.loadFactories(StubGenerator.class, null));
}
@@ -49,6 +51,4 @@ public class StubGeneratorProvider {
return this.converters.isEmpty() ? Collections.singletonList(defaultStubGenerator) : this.converters;
}
private final List<StubGenerator> converters = new ArrayList<StubGenerator>();
}

View File

@@ -16,6 +16,9 @@
package org.springframework.cloud.contract.verifier.wiremock;
import java.io.File;
import java.nio.file.Files;
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
import org.springframework.cloud.contract.verifier.converter.StubGenerator;
@@ -40,4 +43,18 @@ public abstract class DslToWireMockConverter implements StubGenerator<StubMappin
return "";
}
@Override
public boolean canHandleFileName(String fileName) {
if (!fileName.endsWith(fileExtension())) {
return false;
}
try {
StubMapping.buildFrom(new String(Files.readAllBytes(new File(fileName).toPath())));
return true;
}
catch (Exception e) {
return false;
}
}
}

View File

@@ -56,6 +56,7 @@ import static java.util.Collections.emptyList;
@ConditionalOnProperty(name = "stubrunner.amqp.enabled", havingValue = "true")
@AutoConfigureBefore(ContractVerifierIntegrationConfiguration.class)
@AutoConfigureAfter(ContractVerifierStreamAutoConfiguration.class)
@ConditionalOnMissingBean({ ContractVerifierMessaging.class, MessageVerifier.class })
public class ContractVerifierAmqpAutoConfiguration {
@SpyBean

View File

@@ -43,6 +43,7 @@ import org.springframework.context.annotation.Import;
@Import(CamelAutoConfiguration.class)
@ConditionalOnProperty(name = "stubrunner.camel.enabled", havingValue = "true", matchIfMissing = true)
@AutoConfigureBefore({ NoOpContractVerifierAutoConfiguration.class, ContractVerifierJmsConfiguration.class })
@ConditionalOnMissingBean({ ContractVerifierMessaging.class, MessageVerifier.class })
public class ContractVerifierCamelConfiguration {
@Bean

View File

@@ -37,6 +37,7 @@ import org.springframework.messaging.Message;
@ConditionalOnClass(Message.class)
@AutoConfigureBefore(NoOpContractVerifierAutoConfiguration.class)
@AutoConfigureAfter(ContractVerifierStreamAutoConfiguration.class)
@ConditionalOnMissingBean({ ContractVerifierMessaging.class, MessageVerifier.class })
public class ContractVerifierIntegrationConfiguration {
@Bean

View File

@@ -50,6 +50,7 @@ import org.springframework.jms.core.JmsTemplate;
@ConditionalOnClass(JmsTemplate.class)
@ConditionalOnProperty(name = "stubrunner.jms.enabled", havingValue = "true", matchIfMissing = true)
@AutoConfigureBefore({ ContractVerifierIntegrationConfiguration.class, NoOpContractVerifierAutoConfiguration.class })
@ConditionalOnMissingBean({ ContractVerifierMessaging.class, MessageVerifier.class })
public class ContractVerifierJmsConfiguration {
@Bean

View File

@@ -44,6 +44,7 @@ import org.springframework.messaging.Message;
@ConditionalOnProperty(name = "stubrunner.kafka.enabled", havingValue = "true", matchIfMissing = true)
@AutoConfigureBefore({ ContractVerifierIntegrationConfiguration.class, NoOpContractVerifierAutoConfiguration.class })
@ConditionalOnBean(EmbeddedKafkaBroker.class)
@ConditionalOnMissingBean({ ContractVerifierMessaging.class, MessageVerifier.class })
public class ContractVerifierKafkaConfiguration {
private static final Log log = LogFactory.getLog(ContractVerifierKafkaConfiguration.class);

View File

@@ -18,7 +18,7 @@ package org.springframework.cloud.contract.verifier.messaging.noop;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.contract.verifier.messaging.MessageVerifier;
@@ -37,26 +37,24 @@ import org.springframework.core.Ordered;
@AutoConfigureOrder(Ordered.LOWEST_PRECEDENCE)
public class NoOpContractVerifierAutoConfiguration {
@Autowired(required = false)
ObjectMapper objectMapper;
@Bean
@ConditionalOnMissingBean
@ConditionalOnMissingBean(MessageVerifier.class)
public MessageVerifier<?> contractVerifierMessageExchange() {
return new NoOpStubMessages();
}
@Bean
@ConditionalOnMissingBean
@ConditionalOnMissingBean(ContractVerifierMessaging.class)
public ContractVerifierMessaging<?> contractVerifierMessaging(MessageVerifier<Object> exchange) {
return new ContractVerifierMessaging<>(exchange);
}
@Bean
@ConditionalOnMissingBean
public ContractVerifierObjectMapper contractVerifierObjectMapper() {
if (this.objectMapper != null) {
return new ContractVerifierObjectMapper(this.objectMapper);
public ContractVerifierObjectMapper contractVerifierObjectMapper(ObjectProvider<ObjectMapper> objectMapper) {
ObjectMapper mapper = objectMapper.getIfAvailable();
if (mapper != null) {
return new ContractVerifierObjectMapper(mapper);
}
return new ContractVerifierObjectMapper();
}

View File

@@ -40,6 +40,7 @@ import org.springframework.util.Assert;
@ConditionalOnClass(EnableBinding.class)
@ConditionalOnProperty(name = "stubrunner.stream.enabled", havingValue = "true", matchIfMissing = true)
@AutoConfigureBefore(NoOpContractVerifierAutoConfiguration.class)
@ConditionalOnMissingBean({ ContractVerifierMessaging.class, MessageVerifier.class })
public class ContractVerifierStreamAutoConfiguration {
@Bean