use of org.springframework.amqp.core.Message in project spring-integration by spring-projects.
the class DispatcherHasNoSubscribersTests method testPtP.
@SuppressWarnings("unchecked")
@Test
public void testPtP() throws Exception {
final Channel channel = mock(Channel.class);
DeclareOk declareOk = mock(DeclareOk.class);
when(declareOk.getQueue()).thenReturn("noSubscribersChannel");
when(channel.queueDeclare(anyString(), anyBoolean(), anyBoolean(), anyBoolean(), isNull())).thenReturn(declareOk);
Connection connection = mock(Connection.class);
doAnswer(invocation -> channel).when(connection).createChannel(anyBoolean());
ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
when(connectionFactory.createConnection()).thenReturn(connection);
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
AmqpTemplate amqpTemplate = mock(AmqpTemplate.class);
PointToPointSubscribableAmqpChannel amqpChannel = new PointToPointSubscribableAmqpChannel("noSubscribersChannel", container, amqpTemplate);
amqpChannel.setBeanName("noSubscribersChannel");
amqpChannel.setBeanFactory(mock(BeanFactory.class));
amqpChannel.afterPropertiesSet();
MessageListener listener = (MessageListener) container.getMessageListener();
try {
listener.onMessage(new Message("Hello world!".getBytes(), null));
fail("Exception expected");
} catch (MessageDeliveryException e) {
assertThat(e.getMessage(), containsString("Dispatcher has no subscribers for amqp-channel 'noSubscribersChannel'."));
}
}
use of org.springframework.amqp.core.Message in project spring-integration by spring-projects.
the class AmqpInboundChannelAdapterParserTests method withHeaderMapperStandardAndCustomHeaders.
@Test
public void withHeaderMapperStandardAndCustomHeaders() throws Exception {
AmqpInboundChannelAdapter adapter = context.getBean("withHeaderMapperStandardAndCustomHeaders", AmqpInboundChannelAdapter.class);
AbstractMessageListenerContainer mlc = TestUtils.getPropertyValue(adapter, "messageListenerContainer", AbstractMessageListenerContainer.class);
ChannelAwareMessageListener listener = TestUtils.getPropertyValue(mlc, "messageListener", ChannelAwareMessageListener.class);
MessageProperties amqpProperties = new MessageProperties();
amqpProperties.setAppId("test.appId");
amqpProperties.setClusterId("test.clusterId");
amqpProperties.setContentEncoding("test.contentEncoding");
amqpProperties.setContentLength(99L);
amqpProperties.setContentType("test.contentType");
amqpProperties.setHeader("foo", "foo");
amqpProperties.setHeader("bar", "bar");
Message amqpMessage = new Message("hello".getBytes(), amqpProperties);
listener.onMessage(amqpMessage, null);
QueueChannel requestChannel = context.getBean("requestChannel", QueueChannel.class);
org.springframework.messaging.Message<?> siMessage = requestChannel.receive(0);
assertEquals("foo", siMessage.getHeaders().get("foo"));
assertNull(siMessage.getHeaders().get("bar"));
assertNotNull(siMessage.getHeaders().get(AmqpHeaders.CONTENT_ENCODING));
assertNotNull(siMessage.getHeaders().get(AmqpHeaders.CLUSTER_ID));
assertNotNull(siMessage.getHeaders().get(AmqpHeaders.APP_ID));
assertNotNull(siMessage.getHeaders().get(AmqpHeaders.CONTENT_TYPE));
}
use of org.springframework.amqp.core.Message in project spring-integration by spring-projects.
the class AmqpInboundChannelAdapterParserTests method withHeaderMapperNothingToMap.
@Test
public void withHeaderMapperNothingToMap() throws Exception {
AmqpInboundChannelAdapter adapter = context.getBean("withHeaderMapperNothingToMap", AmqpInboundChannelAdapter.class);
AbstractMessageListenerContainer mlc = TestUtils.getPropertyValue(adapter, "messageListenerContainer", AbstractMessageListenerContainer.class);
ChannelAwareMessageListener listener = TestUtils.getPropertyValue(mlc, "messageListener", ChannelAwareMessageListener.class);
MessageProperties amqpProperties = new MessageProperties();
amqpProperties.setAppId("test.appId");
amqpProperties.setClusterId("test.clusterId");
amqpProperties.setContentEncoding("test.contentEncoding");
amqpProperties.setContentLength(99L);
amqpProperties.setContentType("test.contentType");
amqpProperties.setHeader("foo", "foo");
amqpProperties.setHeader("bar", "bar");
Message amqpMessage = new Message("hello".getBytes(), amqpProperties);
listener.onMessage(amqpMessage, null);
QueueChannel requestChannel = context.getBean("requestChannel", QueueChannel.class);
org.springframework.messaging.Message<?> siMessage = requestChannel.receive(0);
assertNull(siMessage.getHeaders().get("foo"));
assertNull(siMessage.getHeaders().get("bar"));
assertNull(siMessage.getHeaders().get(AmqpHeaders.CONTENT_ENCODING));
assertNull(siMessage.getHeaders().get(AmqpHeaders.CLUSTER_ID));
assertNull(siMessage.getHeaders().get(AmqpHeaders.APP_ID));
assertNull(siMessage.getHeaders().get(AmqpHeaders.CONTENT_TYPE));
}
use of org.springframework.amqp.core.Message in project spring-integration by spring-projects.
the class AmqpInboundChannelAdapterParserTests method withHeaderMapperDefaultMapping.
@Test
public void withHeaderMapperDefaultMapping() throws Exception {
AmqpInboundChannelAdapter adapter = context.getBean("withHeaderMapperDefaultMapping", AmqpInboundChannelAdapter.class);
AbstractMessageListenerContainer mlc = TestUtils.getPropertyValue(adapter, "messageListenerContainer", AbstractMessageListenerContainer.class);
ChannelAwareMessageListener listener = TestUtils.getPropertyValue(mlc, "messageListener", ChannelAwareMessageListener.class);
MessageProperties amqpProperties = new MessageProperties();
amqpProperties.setAppId("test.appId");
amqpProperties.setClusterId("test.clusterId");
amqpProperties.setContentEncoding("test.contentEncoding");
amqpProperties.setContentLength(99L);
amqpProperties.setContentType("test.contentType");
amqpProperties.setHeader("foo", "foo");
amqpProperties.setHeader("bar", "bar");
Message amqpMessage = new Message("hello".getBytes(), amqpProperties);
listener.onMessage(amqpMessage, null);
QueueChannel requestChannel = context.getBean("requestChannel", QueueChannel.class);
org.springframework.messaging.Message<?> siMessage = requestChannel.receive(0);
assertNotNull(siMessage.getHeaders().get("bar"));
assertNotNull(siMessage.getHeaders().get("foo"));
assertNotNull(siMessage.getHeaders().get(AmqpHeaders.CONTENT_ENCODING));
assertNotNull(siMessage.getHeaders().get(AmqpHeaders.CLUSTER_ID));
assertNotNull(siMessage.getHeaders().get(AmqpHeaders.APP_ID));
assertNotNull(siMessage.getHeaders().get(AmqpHeaders.CONTENT_TYPE));
}
use of org.springframework.amqp.core.Message in project spring-integration by spring-projects.
the class AmqpInboundGatewayParserTests method verifyUsageWithHeaderMapper.
@Test
public void verifyUsageWithHeaderMapper() throws Exception {
DirectChannel requestChannel = context.getBean("requestChannel", DirectChannel.class);
requestChannel.subscribe(siMessage -> {
org.springframework.messaging.Message<?> replyMessage = MessageBuilder.fromMessage(siMessage).setHeader("bar", "bar").build();
MessageChannel replyChannel = (MessageChannel) siMessage.getHeaders().getReplyChannel();
replyChannel.send(replyMessage);
});
final AmqpInboundGateway gateway = context.getBean("withHeaderMapper", AmqpInboundGateway.class);
Field amqpTemplateField = ReflectionUtils.findField(AmqpInboundGateway.class, "amqpTemplate");
amqpTemplateField.setAccessible(true);
RabbitTemplate amqpTemplate = TestUtils.getPropertyValue(gateway, "amqpTemplate", RabbitTemplate.class);
amqpTemplate = Mockito.spy(amqpTemplate);
Mockito.doAnswer(invocation -> {
Object[] args = invocation.getArguments();
Message amqpReplyMessage = (Message) args[2];
MessageProperties properties = amqpReplyMessage.getMessageProperties();
assertEquals("bar", properties.getHeaders().get("bar"));
return null;
}).when(amqpTemplate).send(Mockito.any(String.class), Mockito.any(String.class), Mockito.any(Message.class), isNull());
ReflectionUtils.setField(amqpTemplateField, gateway, amqpTemplate);
AbstractMessageListenerContainer mlc = TestUtils.getPropertyValue(gateway, "messageListenerContainer", AbstractMessageListenerContainer.class);
ChannelAwareMessageListener listener = TestUtils.getPropertyValue(mlc, "messageListener", ChannelAwareMessageListener.class);
MessageProperties amqpProperties = new MessageProperties();
amqpProperties.setAppId("test.appId");
amqpProperties.setClusterId("test.clusterId");
amqpProperties.setContentEncoding("test.contentEncoding");
amqpProperties.setContentLength(99L);
amqpProperties.setReplyTo("oleg");
amqpProperties.setContentType("test.contentType");
amqpProperties.setHeader("foo", "foo");
amqpProperties.setHeader("bar", "bar");
Message amqpMessage = new Message("hello".getBytes(), amqpProperties);
listener.onMessage(amqpMessage, null);
Mockito.verify(amqpTemplate, Mockito.times(1)).send(Mockito.any(String.class), Mockito.any(String.class), Mockito.any(Message.class), isNull());
}
Aggregations