use of org.springframework.cloud.contract.verifier.messaging.internal.ContractVerifierMessageMetadata in project spring-cloud-contract by spring-cloud.
the class SpringAmqpStubMessages method mergeMessagePropertiesFromMetadata.
public void mergeMessagePropertiesFromMetadata(YamlContract contract, Message message) {
if (contract != null && contract.metadata.containsKey(AmqpMetadata.METADATA_KEY)) {
AmqpMetadata amqpMetadata = AmqpMetadata.fromMetadata(contract.metadata);
ContractVerifierMessageMetadata messageMetadata = ContractVerifierMessageMetadata.fromMetadata(contract.metadata);
boolean isInput = isInputMessage(messageMetadata);
MessageProperties fromMetadata = isInput ? amqpMetadata.getInput().getMessageProperties() : amqpMetadata.getOutputMessage().getMessageProperties();
MetadataUtil.merge(message.getMessageProperties(), fromMetadata);
}
}
use of org.springframework.cloud.contract.verifier.messaging.internal.ContractVerifierMessageMetadata in project spring-cloud-contract by spring-cloud.
the class CamelStubMessages method receive.
@Override
public Message receive(String destination, long timeout, TimeUnit timeUnit, YamlContract contract) {
try {
StandaloneMetadata standaloneMetadata = StandaloneMetadata.fromMetadata(contract != null ? contract.metadata : null);
ContractVerifierMessageMetadata verifierMessageMetadata = ContractVerifierMessageMetadata.fromMetadata(contract != null ? contract.metadata : null);
String finalDestination = finalDestination(destination, additionalOptions(verifierMessageMetadata, standaloneMetadata), verifierMessageMetadata);
log.info("Will receive a message from URI [" + finalDestination + "]");
Exchange exchange = this.consumerTemplate.receive(finalDestination, timeUnit.toMillis(timeout));
return exchange != null ? exchange.getIn() : null;
} catch (Exception e) {
log.error("Exception occurred while trying to read a message from " + " a channel with name [" + destination + "]", e);
throw new IllegalStateException(e);
}
}
use of org.springframework.cloud.contract.verifier.messaging.internal.ContractVerifierMessageMetadata in project spring-cloud-contract by spring-cloud.
the class ContractVerifierCamelHelper method manualMessageVerifier.
@Bean
MessageVerifier<Message> manualMessageVerifier(ConsumerTemplate consumerTemplate) {
return new MessageVerifier<Message>() {
private final Logger log = LoggerFactory.getLogger(MessageVerifier.class);
@Override
public Message receive(String destination, long timeout, TimeUnit timeUnit, YamlContract yamlContract) {
String uri = messagingType() + "://" + destination + additionalOptions(yamlContract);
log.info("Camel URI [{}]", uri);
Exchange exchange = consumerTemplate.receive(uri, timeUnit.toMillis(timeout));
if (exchange == null) {
return null;
}
return exchange.getMessage();
}
private String messagingType() {
if (messagingType.equalsIgnoreCase("kafka")) {
return "kafka";
}
return "rabbitmq";
}
private String additionalOptions(YamlContract contract) {
if (contract == null) {
return "";
}
if (messagingType.equalsIgnoreCase("kafka")) {
return setKafkaOpts(contract);
}
return setRabbitOpts(contract);
}
private String setKafkaOpts(YamlContract contract) {
String opts = defaultOpts(contract);
KafkaMetadata metadata = KafkaMetadata.fromMetadata(contract.metadata);
ContractVerifierMessageMetadata messageMetadata = ContractVerifierMessageMetadata.fromMetadata(contract.metadata);
if (inputMessage(messageMetadata) && StringUtils.hasText(metadata.getInput().getConnectToBroker().getAdditionalOptions())) {
return opts + "&" + metadata.getInput().getConnectToBroker().getAdditionalOptions();
} else if (StringUtils.hasText(metadata.getOutputMessage().getConnectToBroker().getAdditionalOptions())) {
return opts + "&" + metadata.getOutputMessage().getConnectToBroker().getAdditionalOptions();
}
return opts;
}
private String defaultOpts(YamlContract contract) {
String consumerGroup = sameConsumerGroupForSameContract(contract);
return "?brokers=" + getRequiredProperty("SPRING_KAFKA_BOOTSTRAP_SERVERS", springKafkaBootstrapServers) + "&autoOffsetReset=latest&groupId=" + consumerGroup + "&shutdownTimeout=5";
}
private String sameConsumerGroupForSameContract(YamlContract contract) {
return contract.input.hashCode() + "_" + contract.outputMessage.hashCode();
}
private String setRabbitOpts(YamlContract contract) {
String opts = "?addresses=" + getRequiredProperty("SPRING_RABBITMQ_ADDRESSES", springRabbitmqAddresses);
AmqpMetadata metadata = AmqpMetadata.fromMetadata(contract.metadata);
ContractVerifierMessageMetadata messageMetadata = ContractVerifierMessageMetadata.fromMetadata(contract.metadata);
if (inputMessage(messageMetadata) && StringUtils.hasText(metadata.getInput().getConnectToBroker().getAdditionalOptions())) {
return opts + "&" + metadata.getInput().getConnectToBroker().getAdditionalOptions();
} else if (StringUtils.hasText(metadata.getOutputMessage().getConnectToBroker().getAdditionalOptions())) {
return opts + "&" + metadata.getOutputMessage().getConnectToBroker().getAdditionalOptions();
}
return defaultOpts(opts, metadata, messageMetadata);
}
private String getRequiredProperty(String name, String value) {
if (!StringUtils.hasText(value)) {
throw new IllegalStateException("The property [" + name + "] must not be empty!");
}
return value;
}
private boolean inputMessage(ContractVerifierMessageMetadata messageMetadata) {
return messageMetadata.getMessageType() == ContractVerifierMessageMetadata.MessageType.INPUT;
}
private String defaultOpts(String opts, AmqpMetadata amqpMetadata, ContractVerifierMessageMetadata messageMetadata) {
AmqpMetadata.ConnectToBroker connectToBroker = inputMessage(messageMetadata) ? amqpMetadata.getInput().getConnectToBroker() : amqpMetadata.getOutputMessage().getConnectToBroker();
MessageProperties messageProperties = inputMessage(messageMetadata) ? amqpMetadata.getInput().getMessageProperties() : amqpMetadata.getOutputMessage().getMessageProperties();
if (StringUtils.hasText(connectToBroker.getDeclareQueueWithName())) {
opts = opts + "&queue=" + connectToBroker.getDeclareQueueWithName();
}
if (messageProperties != null && StringUtils.hasText(messageProperties.getReceivedRoutingKey())) {
opts = opts + "&routingKey=" + messageProperties.getReceivedRoutingKey();
}
return opts;
}
@Override
public Message receive(String destination, YamlContract yamlContract) {
return receive(destination, 5, TimeUnit.SECONDS, yamlContract);
}
@Override
public void send(Message message, String destination, YamlContract yamlContract) {
throw new UnsupportedOperationException("Currently supports only receiving");
}
@Override
public void send(Object payload, Map headers, String destination, YamlContract yamlContract) {
throw new UnsupportedOperationException("Currently supports only receiving");
}
};
}
use of org.springframework.cloud.contract.verifier.messaging.internal.ContractVerifierMessageMetadata in project spring-cloud-contract by spring-cloud.
the class CamelStubMessages method send.
@Override
public void send(Message message, String destination, YamlContract contract) {
try {
Exchange exchange = new DefaultExchange(this.context);
exchange.setIn(message);
StandaloneMetadata standaloneMetadata = StandaloneMetadata.fromMetadata(contract != null ? contract.metadata : null);
ContractVerifierMessageMetadata verifierMessageMetadata = ContractVerifierMessageMetadata.fromMetadata(contract != null ? contract.metadata : null);
String finalDestination = finalDestination(destination, additionalOptions(verifierMessageMetadata, standaloneMetadata), verifierMessageMetadata);
log.info("Will send a message to URI [" + finalDestination + "]");
this.producerTemplate.send(finalDestination, exchange);
} catch (Exception e) {
log.error("Exception occurred while trying to send a message [" + message + "] " + "to a channel with name [" + destination + "]", e);
throw e;
}
}
Aggregations