Search in sources :

Example 1 with MessageVerifier

use of org.springframework.cloud.contract.verifier.messaging.MessageVerifier in project spring-cloud-contract-samples by spring-cloud-samples.

the class TestConfig method standaloneMessageVerifier.

@Bean
MessageVerifier<Message> standaloneMessageVerifier(JmsTemplate jmsTemplate) {
    return new MessageVerifier<>() {

        @Override
        public Message receive(String destination, long timeout, TimeUnit timeUnit, @Nullable YamlContract contract) {
            return null;
        }

        @Override
        public Message receive(String destination, YamlContract contract) {
            return null;
        }

        @Override
        public void send(Message message, String destination, @Nullable YamlContract contract) {
        }

        @Override
        public <T> void send(T payload, Map<String, Object> headers, String destination, @Nullable YamlContract contract) {
            jmsTemplate.send(destination, session -> {
                Message message = session.createTextMessage(payload.toString());
                headers.forEach((s, o) -> {
                    try {
                        message.setStringProperty(s, o.toString());
                    } catch (JMSException e) {
                        throw new IllegalStateException(e);
                    }
                });
                return message;
            });
        }
    };
}
Also used : YamlContract(org.springframework.cloud.contract.verifier.converter.YamlContract) Message(javax.jms.Message) TimeUnit(java.util.concurrent.TimeUnit) MessageVerifier(org.springframework.cloud.contract.verifier.messaging.MessageVerifier) JMSException(javax.jms.JMSException) Map(java.util.Map) Nullable(javax.annotation.Nullable) Bean(org.springframework.context.annotation.Bean)

Example 2 with MessageVerifier

use of org.springframework.cloud.contract.verifier.messaging.MessageVerifier in project spring-cloud-contract-samples by spring-cloud-samples.

the class TestConfig method testMessageVerifier.

@Bean
MessageVerifier<Message> testMessageVerifier(RabbitTemplate rabbitTemplate) {
    return new MessageVerifier<>() {

        @Override
        public Message receive(String destination, long timeout, TimeUnit timeUnit, @Nullable YamlContract contract) {
            return null;
        }

        @Override
        public Message receive(String destination, YamlContract contract) {
            return null;
        }

        @Override
        public void send(Message message, String destination, @Nullable YamlContract contract) {
            rabbitTemplate.send(destination, message);
        }

        @Override
        public <T> void send(T payload, Map<String, Object> headers, String destination, @Nullable YamlContract contract) {
            Map<String, Object> newHeaders = headers != null ? new HashMap<>(headers) : new HashMap<>();
            MessageProperties messageProperties = new MessageProperties();
            newHeaders.forEach(messageProperties::setHeader);
            log.info("Sending a message to destination [{}] with routing key", destination);
            if (payload instanceof String) {
                String json = (String) payload;
                Message message = MessageBuilder.withBody(json.getBytes(StandardCharsets.UTF_8)).andProperties(messageProperties).build();
                send(message, destination, contract);
            } else {
                throw new IllegalStateException("Payload is not a String");
            }
        }
    };
}
Also used : YamlContract(org.springframework.cloud.contract.verifier.converter.YamlContract) Message(org.springframework.amqp.core.Message) MessageVerifier(org.springframework.cloud.contract.verifier.messaging.MessageVerifier) MessageProperties(org.springframework.amqp.core.MessageProperties) TimeUnit(java.util.concurrent.TimeUnit) HashMap(java.util.HashMap) Map(java.util.Map) Nullable(javax.annotation.Nullable) Bean(org.springframework.context.annotation.Bean)

Example 3 with MessageVerifier

use of org.springframework.cloud.contract.verifier.messaging.MessageVerifier 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");
        }
    };
}
Also used : YamlContract(org.springframework.cloud.contract.verifier.converter.YamlContract) AmqpMetadata(org.springframework.cloud.contract.verifier.messaging.amqp.AmqpMetadata) ContractVerifierMessage(org.springframework.cloud.contract.verifier.messaging.internal.ContractVerifierMessage) Message(org.apache.camel.Message) ContractVerifierMessageMetadata(org.springframework.cloud.contract.verifier.messaging.internal.ContractVerifierMessageMetadata) MessageVerifier(org.springframework.cloud.contract.verifier.messaging.MessageVerifier) Logger(org.slf4j.Logger) KafkaMetadata(org.springframework.cloud.contract.verifier.messaging.kafka.KafkaMetadata) Exchange(org.apache.camel.Exchange) MessageProperties(org.springframework.amqp.core.MessageProperties) TimeUnit(java.util.concurrent.TimeUnit) Map(java.util.Map) Bean(org.springframework.context.annotation.Bean)

Aggregations

Map (java.util.Map)3 TimeUnit (java.util.concurrent.TimeUnit)3 YamlContract (org.springframework.cloud.contract.verifier.converter.YamlContract)3 MessageVerifier (org.springframework.cloud.contract.verifier.messaging.MessageVerifier)3 Bean (org.springframework.context.annotation.Bean)3 Nullable (javax.annotation.Nullable)2 MessageProperties (org.springframework.amqp.core.MessageProperties)2 HashMap (java.util.HashMap)1 JMSException (javax.jms.JMSException)1 Message (javax.jms.Message)1 Exchange (org.apache.camel.Exchange)1 Message (org.apache.camel.Message)1 Logger (org.slf4j.Logger)1 Message (org.springframework.amqp.core.Message)1 AmqpMetadata (org.springframework.cloud.contract.verifier.messaging.amqp.AmqpMetadata)1 ContractVerifierMessage (org.springframework.cloud.contract.verifier.messaging.internal.ContractVerifierMessage)1 ContractVerifierMessageMetadata (org.springframework.cloud.contract.verifier.messaging.internal.ContractVerifierMessageMetadata)1 KafkaMetadata (org.springframework.cloud.contract.verifier.messaging.kafka.KafkaMetadata)1