use of com.rabbitmq.stream.MessageHandler.Context in project spring-amqp by spring-projects.
the class StreamListenerContainerTests method testAdviceChain.
@Test
void testAdviceChain() throws Exception {
Environment env = mock(Environment.class);
ConsumerBuilder builder = mock(ConsumerBuilder.class);
given(env.consumerBuilder()).willReturn(builder);
AtomicReference<MessageHandler> handler = new AtomicReference<>();
willAnswer(inv -> {
handler.set(inv.getArgument(0));
return null;
}).given(builder).messageHandler(any());
AtomicBoolean advised = new AtomicBoolean();
MethodInterceptor advice = (inv) -> {
advised.set(true);
return inv.proceed();
};
StreamListenerContainer container = new StreamListenerContainer(env);
container.setAdviceChain(advice);
AtomicBoolean called = new AtomicBoolean();
MessageListener ml = mock(MessageListener.class);
willAnswer(inv -> {
called.set(true);
return null;
}).given(ml).onMessage(any());
container.setupMessageListener(ml);
Message message = mock(Message.class);
given(message.getBodyAsBinary()).willReturn("foo".getBytes());
Context context = mock(Context.class);
handler.get().handle(context, message);
assertThat(advised.get()).isTrue();
assertThat(called.get()).isTrue();
advised.set(false);
called.set(false);
ChannelAwareMessageListener cal = mock(ChannelAwareMessageListener.class);
willAnswer(inv -> {
called.set(true);
return null;
}).given(cal).onMessage(any(), isNull());
container.setupMessageListener(cal);
handler.get().handle(context, message);
assertThat(advised.get()).isTrue();
assertThat(called.get()).isTrue();
called.set(false);
StreamMessageListener sml = mock(StreamMessageListener.class);
willAnswer(inv -> {
called.set(true);
return null;
}).given(sml).onStreamMessage(message, context);
container.setupMessageListener(sml);
handler.get().handle(context, message);
assertThat(advised.get()).isTrue();
assertThat(called.get()).isTrue();
}
Aggregations