Ensures that non-transactional channel is an option for AmqpStubMessages; fixes gh-859
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.cloud.contract.verifier.messaging.amqp
|
||||
|
||||
import com.rabbitmq.client.Channel
|
||||
import org.mockito.exceptions.verification.WantedButNotInvoked
|
||||
import spock.lang.Specification
|
||||
import wiremock.com.google.common.collect.ImmutableMap
|
||||
@@ -29,6 +30,7 @@ import org.springframework.amqp.core.Queue
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate
|
||||
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer
|
||||
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter
|
||||
import org.springframework.boot.autoconfigure.amqp.RabbitProperties
|
||||
|
||||
import static org.mockito.Mockito.mock
|
||||
import static org.springframework.amqp.core.MessageProperties.CONTENT_TYPE_JSON
|
||||
@@ -42,6 +44,7 @@ class SpringAmqpStubMessagesSpec extends Specification {
|
||||
RabbitTemplate rabbitTemplate = mock(RabbitTemplate.class)
|
||||
SimpleMessageListenerContainer listenerContainer = new SimpleMessageListenerContainer()
|
||||
MessageListenerAdapter messageListenerAdapter = Mock(MessageListenerAdapter.class)
|
||||
RabbitProperties rabbitProperties = new RabbitProperties()
|
||||
Message message = Mock(Message.class)
|
||||
|
||||
String queueName = "test.queue"
|
||||
@@ -55,7 +58,7 @@ class SpringAmqpStubMessagesSpec extends Specification {
|
||||
listenerContainer.setQueueNames(queueName)
|
||||
Binding binding = BindingBuilder.bind(new Queue(queueName)).to(new DirectExchange(exchange)).with(routingKey)
|
||||
MessageListenerAccessor messageListenerAccessor = new MessageListenerAccessor(null, [listenerContainer], [binding])
|
||||
SpringAmqpStubMessages messageVerifier = new SpringAmqpStubMessages(rabbitTemplate, messageListenerAccessor)
|
||||
SpringAmqpStubMessages messageVerifier = new SpringAmqpStubMessages(rabbitTemplate, messageListenerAccessor, rabbitProperties)
|
||||
|
||||
when:
|
||||
messageVerifier.send(payload,
|
||||
@@ -73,13 +76,85 @@ class SpringAmqpStubMessagesSpec extends Specification {
|
||||
})
|
||||
}
|
||||
|
||||
def "should send amqp message for non transactional channel"() {
|
||||
given:
|
||||
rabbitProperties.setPublisherConfirms(true)
|
||||
listenerContainer.setMessageListener(messageListenerAdapter)
|
||||
listenerContainer.setQueueNames(queueName)
|
||||
Binding binding = BindingBuilder.bind(new Queue(queueName)).to(new DirectExchange(exchange)).with(routingKey)
|
||||
MessageListenerAccessor messageListenerAccessor = new MessageListenerAccessor(null, [listenerContainer], [binding])
|
||||
boolean createChannelCalled = false
|
||||
boolean transactionalChannel = false
|
||||
SpringAmqpStubMessages messageVerifier = new SpringAmqpStubMessages(rabbitTemplate, messageListenerAccessor, rabbitProperties) {
|
||||
@Override
|
||||
boolean isChannelAwareListener(SimpleMessageListenerContainer listenerContainer, Object messageListener) {
|
||||
return true
|
||||
}
|
||||
|
||||
@Override
|
||||
Channel createChannel(SimpleMessageListenerContainer listenerContainer, boolean transactional) {
|
||||
createChannelCalled = true
|
||||
transactionalChannel = transactional
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
when:
|
||||
messageVerifier.send(payload,
|
||||
ImmutableMap.builder()
|
||||
.put(DEFAULT_CLASSID_FIELD_NAME, "org.example.Some")
|
||||
.put("amqp_receivedRoutingKey", routingKey)
|
||||
.put("contentType", CONTENT_TYPE_JSON)
|
||||
.build(),
|
||||
exchange)
|
||||
then:
|
||||
createChannelCalled
|
||||
!transactionalChannel
|
||||
}
|
||||
|
||||
def "should send amqp message for transactional channel"() {
|
||||
given:
|
||||
rabbitProperties.setPublisherConfirms(false)
|
||||
listenerContainer.setMessageListener(messageListenerAdapter)
|
||||
listenerContainer.setQueueNames(queueName)
|
||||
Binding binding = BindingBuilder.bind(new Queue(queueName)).to(new DirectExchange(exchange)).with(routingKey)
|
||||
MessageListenerAccessor messageListenerAccessor = new MessageListenerAccessor(null, [listenerContainer], [binding])
|
||||
boolean createChannelCalled = false
|
||||
boolean transactionalChannel = false
|
||||
SpringAmqpStubMessages messageVerifier = new SpringAmqpStubMessages(rabbitTemplate, messageListenerAccessor, rabbitProperties) {
|
||||
@Override
|
||||
boolean isChannelAwareListener(SimpleMessageListenerContainer listenerContainer, Object messageListener) {
|
||||
return true
|
||||
}
|
||||
|
||||
@Override
|
||||
Channel createChannel(SimpleMessageListenerContainer listenerContainer, boolean transactional) {
|
||||
createChannelCalled = true
|
||||
transactionalChannel = transactional
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
when:
|
||||
messageVerifier.send(payload,
|
||||
ImmutableMap.builder()
|
||||
.put(DEFAULT_CLASSID_FIELD_NAME, "org.example.Some")
|
||||
.put("amqp_receivedRoutingKey", routingKey)
|
||||
.put("contentType", CONTENT_TYPE_JSON)
|
||||
.build(),
|
||||
exchange)
|
||||
then:
|
||||
createChannelCalled
|
||||
transactionalChannel
|
||||
}
|
||||
|
||||
def "should fail to receive a message if rabbit template wasn't called"() {
|
||||
given:
|
||||
listenerContainer.setMessageListener(messageListenerAdapter)
|
||||
listenerContainer.setQueueNames(queueName)
|
||||
Binding binding = BindingBuilder.bind(new Queue(queueName)).to(new DirectExchange(exchange)).with(routingKey)
|
||||
MessageListenerAccessor messageListenerAccessor = new MessageListenerAccessor(null, [listenerContainer], [binding])
|
||||
SpringAmqpStubMessages messageVerifier = new SpringAmqpStubMessages(rabbitTemplate, messageListenerAccessor)
|
||||
SpringAmqpStubMessages messageVerifier = new SpringAmqpStubMessages(rabbitTemplate, messageListenerAccessor, rabbitProperties)
|
||||
when:
|
||||
messageVerifier.receive("foo")
|
||||
then:
|
||||
@@ -92,7 +167,7 @@ class SpringAmqpStubMessagesSpec extends Specification {
|
||||
listenerContainer.setQueueNames(queueName)
|
||||
Binding binding = BindingBuilder.bind(new Queue(queueName)).to(new DirectExchange(exchange)).with(routingKey)
|
||||
MessageListenerAccessor messageListenerAccessor = new MessageListenerAccessor(null, [listenerContainer], [binding])
|
||||
SpringAmqpStubMessages messageVerifier = new SpringAmqpStubMessages(rabbitTemplate, messageListenerAccessor)
|
||||
SpringAmqpStubMessages messageVerifier = new SpringAmqpStubMessages(rabbitTemplate, messageListenerAccessor, rabbitProperties)
|
||||
|
||||
and:
|
||||
rabbitTemplate.send("foo", "bar", null, null)
|
||||
@@ -106,7 +181,7 @@ class SpringAmqpStubMessagesSpec extends Specification {
|
||||
listenerContainer.setQueueNames(queueName)
|
||||
Binding binding = BindingBuilder.bind(new Queue(queueName)).to(new DirectExchange(exchange)).with(routingKey)
|
||||
MessageListenerAccessor messageListenerAccessor = new MessageListenerAccessor(null, [listenerContainer], [binding])
|
||||
SpringAmqpStubMessages messageVerifier = new SpringAmqpStubMessages(rabbitTemplate, messageListenerAccessor)
|
||||
SpringAmqpStubMessages messageVerifier = new SpringAmqpStubMessages(rabbitTemplate, messageListenerAccessor, rabbitProperties)
|
||||
message.getMessageProperties() >> new MessageProperties()
|
||||
and:
|
||||
rabbitTemplate.send("foo", "bar", message, null)
|
||||
|
||||
Reference in New Issue
Block a user