@Test public void testInt2773WithOverrideToDefaultAmqpTemplateExchangeAndRoutingLey() throws IOException { ConnectionFactory connectionFactory = context.getBean(ConnectionFactory.class); Connection mockConnection = mock(Connection.class); Channel mockChannel = mock(Channel.class); when(connectionFactory.createConnection()).thenReturn(mockConnection); PublisherCallbackChannelImpl publisherCallbackChannel = new PublisherCallbackChannelImpl(mockChannel, mock(ExecutorService.class)); when(mockConnection.createChannel(false)).thenReturn(publisherCallbackChannel); MessageChannel requestChannel = context.getBean("overrideTemplateAttributesToEmpty", MessageChannel.class); requestChannel.send(MessageBuilder.withPayload("test").build()); Mockito.verify(mockChannel, Mockito.times(1)).basicPublish(Mockito.eq(""), Mockito.eq(""), Mockito.anyBoolean(), Mockito.any(BasicProperties.class), Mockito.any(byte[].class)); }
@Test public void testInt2773UseDefaultAmqpTemplateExchangeAndRoutingLey() throws IOException { ConnectionFactory connectionFactory = context.getBean(ConnectionFactory.class); Connection mockConnection = mock(Connection.class); Channel mockChannel = mock(Channel.class); when(connectionFactory.createConnection()).thenReturn(mockConnection); PublisherCallbackChannelImpl publisherCallbackChannel = new PublisherCallbackChannelImpl(mockChannel, mock(ExecutorService.class)); when(mockConnection.createChannel(false)).thenReturn(publisherCallbackChannel); MessageChannel requestChannel = context.getBean("toRabbitOnlyWithTemplateChannel", MessageChannel.class); requestChannel.send(MessageBuilder.withPayload("test").build()); Mockito.verify(mockChannel, Mockito.times(1)).basicPublish(Mockito.eq("default.test.exchange"), Mockito.eq("default.routing.key"), Mockito.anyBoolean(), Mockito.any(BasicProperties.class), Mockito.any(byte[].class)); }
@Test public void testInt2773WithDefaultAmqpTemplateExchangeAndRoutingKey() throws IOException { ConnectionFactory connectionFactory = context.getBean(ConnectionFactory.class); Connection mockConnection = mock(Connection.class); Channel mockChannel = mock(Channel.class); when(connectionFactory.createConnection()).thenReturn(mockConnection); PublisherCallbackChannelImpl publisherCallbackChannel = new PublisherCallbackChannelImpl(mockChannel, mock(ExecutorService.class)); when(mockConnection.createChannel(false)).thenReturn(publisherCallbackChannel); MessageChannel requestChannel = context.getBean("withDefaultAmqpTemplateExchangeAndRoutingKey", MessageChannel.class); requestChannel.send(MessageBuilder.withPayload("test").build()); Mockito.verify(mockChannel, Mockito.times(1)).basicPublish(Mockito.eq(""), Mockito.eq(""), Mockito.anyBoolean(), Mockito.any(BasicProperties.class), Mockito.any(byte[].class)); }
@Test public void testPubSub() { final Channel channel = mock(Channel.class); 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); PublishSubscribeAmqpChannel amqpChannel = new PublishSubscribeAmqpChannel("noSubscribersChannel", container, amqpTemplate); amqpChannel.setBeanName("noSubscribersChannel"); amqpChannel.setBeanFactory(mock(BeanFactory.class)); amqpChannel.afterPropertiesSet(); List<String> logList = insertMockLoggerInListener(amqpChannel); MessageListener listener = (MessageListener) container.getMessageListener(); listener.onMessage(new Message("Hello world!".getBytes(), null)); verifyLogReceived(logList); }
@Override public Channel createChannel(Connection con) throws IOException { return con.createChannel(this.synchedLocalTransactionAllowed); }
@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'.")); } }
@Test public void testInt2809JavaTypePropertiesFromAmqp() throws Exception { Connection connection = mock(Connection.class); doAnswer(invocation -> mock(Channel.class)).when(connection).createChannel(anyBoolean()); ConnectionFactory connectionFactory = mock(ConnectionFactory.class); when(connectionFactory.createConnection()).thenReturn(connection); SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); container.setConnectionFactory(connectionFactory); AmqpInboundChannelAdapter adapter = new AmqpInboundChannelAdapter(container); PollableChannel channel = new QueueChannel(); adapter.setOutputChannel(channel); adapter.setBeanFactory(mock(BeanFactory.class)); adapter.afterPropertiesSet(); Object payload = new Foo("bar1"); MessageProperties amqpMessageProperties = new MessageProperties(); org.springframework.amqp.core.Message amqpMessage = new Jackson2JsonMessageConverter().toMessage(payload, amqpMessageProperties); ChannelAwareMessageListener listener = (ChannelAwareMessageListener) container.getMessageListener(); listener.onMessage(amqpMessage, null); Message<?> receive = channel.receive(1000); Message<?> result = new JsonToObjectTransformer().transform(receive); assertEquals(payload, result.getPayload()); }
@Test public void testAdapterConversionError() throws Exception { Connection connection = mock(Connection.class); doAnswer(invocation -> mock(Channel.class)).when(connection).createChannel(anyBoolean()); ConnectionFactory connectionFactory = mock(ConnectionFactory.class); when(connectionFactory.createConnection()).thenReturn(connection); SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); container.setConnectionFactory(connectionFactory); AmqpInboundChannelAdapter adapter = new AmqpInboundChannelAdapter(container); QueueChannel outputChannel = new QueueChannel(); adapter.setOutputChannel(outputChannel); QueueChannel errorChannel = new QueueChannel(); adapter.setErrorChannel(errorChannel); adapter.setMessageConverter(new SimpleMessageConverter() { @Override public Object fromMessage(org.springframework.amqp.core.Message message) throws MessageConversionException { throw new MessageConversionException("intended"); } }); adapter.afterPropertiesSet(); ((ChannelAwareMessageListener) container.getMessageListener()) .onMessage(mock(org.springframework.amqp.core.Message.class), null); assertNull(outputChannel.receive(0)); Message<?> received = errorChannel.receive(0); assertNotNull(received); assertNotNull(received.getHeaders().get(AmqpMessageHeaderErrorMessageStrategy.AMQP_RAW_MESSAGE)); }
@Test public void testGatewayConversionError() throws Exception { Connection connection = mock(Connection.class); doAnswer(invocation -> mock(Channel.class)).when(connection).createChannel(anyBoolean()); ConnectionFactory connectionFactory = mock(ConnectionFactory.class); when(connectionFactory.createConnection()).thenReturn(connection);
@Test public void testMessageConverterJsonHeadersHavePrecedenceOverMessageHeaders() throws Exception { Connection connection = mock(Connection.class); doAnswer(invocation -> mock(Channel.class)).when(connection).createChannel(anyBoolean()); ConnectionFactory connectionFactory = mock(ConnectionFactory.class); when(connectionFactory.createConnection()).thenReturn(connection);
@Test public void testInt2809JavaTypePropertiesToAmqp() throws Exception { Connection connection = mock(Connection.class); doAnswer(invocation -> mock(Channel.class)).when(connection).createChannel(anyBoolean()); ConnectionFactory connectionFactory = mock(ConnectionFactory.class); when(connectionFactory.createConnection()).thenReturn(connection);
Channel notCached = conn.createChannel(false); // should not have been "closed" verify(connection, times(2)).createChannel(); StaticMessageHeaderAccessor.getAcknowledgmentCallback(received) .acknowledge(Status.ACCEPT); verify(channel).basicAck(123L, false); Channel cached = conn.createChannel(false); // should have been "closed" verify(connection, times(2)).createChannel(); notCached.close();
@Override protected AbstractIntegrationMessageBuilder<Object> doReceive() { Connection connection = this.connectionFactory.createConnection(); // NOSONAR - RabbitUtils Channel channel = connection.createChannel(this.transacted); try { GetResponse resp = channel.basicGet(this.queue, false);
@Bean public ConnectionFactory rabbitConnectionFactory() { ConnectionFactory mockCF = mock(ConnectionFactory.class); Connection mockConn = mock(Connection.class); when(mockCF.createConnection()).thenReturn(mockConn); Channel mockChannel = mock(Channel.class); when(mockChannel.isOpen()).thenReturn(true); when(mockConn.createChannel(anyBoolean())).thenReturn(mockChannel); return mockCF; }
@Bean public ConnectionFactory rabbitConnectionFactory() { ConnectionFactory mockCF = mock(ConnectionFactory.class); Connection mockConn = mock(Connection.class); when(mockCF.createConnection()).thenReturn(mockConn); Channel mockChannel = mock(Channel.class); when(mockChannel.isOpen()).thenReturn(true); when(mockConn.createChannel(anyBoolean())).thenReturn(mockChannel); return mockCF; }
public int getMessageCount(final String queue) throws IOException { Connection connection = connectionFactory.createConnection(); final Channel channel = connection.createChannel(false); final AMQP.Queue.DeclareOk declareOk = channel.queueDeclarePassive(queue); return declareOk.getMessageCount(); } }
@Bean public ConnectionFactory cf2() throws IOException { ConnectionFactory connectionFactory = mock(ConnectionFactory.class); when(connectionFactory.createConnection()).thenReturn(conn2); when(conn2.createChannel(false)).thenReturn(channel2); when(channel2.queueDeclare("foo", true, false, false, null)) .thenReturn(new AMQImpl.Queue.DeclareOk("foo", 0, 0)); doAnswer(invocation -> { listener2 = invocation.getArgument(0); return null; }).when(connectionFactory).addConnectionListener(any(ConnectionListener.class)); return connectionFactory; }
@Test(expected = AmqpResourceNotAvailableException.class) public void testChannelMax() { this.connectionFactory.getRabbitConnectionFactory().setRequestedChannelMax(1); Connection connection = this.connectionFactory.createConnection(); connection.createChannel(true); connection.createChannel(false); }
@Test public void testConnectionCloseLog() { Log logger = spy(TestUtils.getPropertyValue(this.connectionFactory, "logger", Log.class)); new DirectFieldAccessor(this.connectionFactory).setPropertyValue("logger", logger); Connection conn = this.connectionFactory.createConnection(); conn.createChannel(false); this.connectionFactory.destroy(); verify(logger, never()).error(anyString()); }
@Bean public ConnectionFactory connectionFactory() throws IOException { ConnectionFactory factory = mock(ConnectionFactory.class); Connection connection = mock(Connection.class); Channel channel = mock(Channel.class); willReturn(connection).given(factory).createConnection(); willReturn(channel).given(connection).createChannel(anyBoolean()); given(channel.isOpen()).willReturn(true); return factory; }