diff --git a/src/main/kotlin/com/github/kpavlov/jreactive8583/netty/pipeline/Iso8583ChannelInitializer.kt b/src/main/kotlin/com/github/kpavlov/jreactive8583/netty/pipeline/Iso8583ChannelInitializer.kt index d65ec0c..b88cd5e 100644 --- a/src/main/kotlin/com/github/kpavlov/jreactive8583/netty/pipeline/Iso8583ChannelInitializer.kt +++ b/src/main/kotlin/com/github/kpavlov/jreactive8583/netty/pipeline/Iso8583ChannelInitializer.kt @@ -61,8 +61,10 @@ internal constructor( if (configuration.replyOnError()) { pipeline.addLast(workerGroup, "replyOnError", parseExceptionHandler) } - pipeline.addLast("idleState", IdleStateHandler(0, 0, configuration.idleTimeout)) - pipeline.addLast("idleEventHandler", IdleEventHandler(isoMessageFactory)) + if (configuration.shouldAddEchoMessageListener()) { + pipeline.addLast("idleState", IdleStateHandler(0, 0, configuration.idleTimeout)) + pipeline.addLast("idleEventHandler", IdleEventHandler(isoMessageFactory)) + } pipeline.addLast(workerGroup, *customChannelHandlers) configurer?.configurePipeline(pipeline, configuration) } diff --git a/src/test/java/com/github/kpavlov/jreactive8583/it/EchoFromClientIT.java b/src/test/java/com/github/kpavlov/jreactive8583/it/EchoFromClientIT.java index d1a5410..a17b863 100644 --- a/src/test/java/com/github/kpavlov/jreactive8583/it/EchoFromClientIT.java +++ b/src/test/java/com/github/kpavlov/jreactive8583/it/EchoFromClientIT.java @@ -26,19 +26,26 @@ class EchoFromClientIT extends AbstractIT { @Override protected void configureServer(final Iso8583Server server) { - server.setConfigurer(new ConnectorConfigurer<>() { + server.setConfigurer(new ConnectorConfigurer<>() { - @Override - public void configurePipeline(final ChannelPipeline pipeline, final ServerConfiguration configuration) { - pipeline.addBefore("idleEventHandler", "connectListenerHandler", new ChannelInboundHandlerAdapter() { - @Override - public void channelActive(final ChannelHandlerContext ctx) throws Exception { - super.channelActive(ctx); - final var message = server.getIsoMessageFactory().newMessage(0x800); - ctx.writeAndFlush(message); - } - }); - } + @Override + public void configurePipeline(final ChannelPipeline pipeline, + final ServerConfiguration configuration) { + final var echoHandler = new ChannelInboundHandlerAdapter() { + @Override + public void channelActive(final ChannelHandlerContext ctx) throws Exception { + super.channelActive(ctx); + final var message = server.getIsoMessageFactory().newMessage(0x800); + ctx.writeAndFlush(message); + } + }; + + if (pipeline.get("idleEventHandler") != null) { + pipeline.addBefore("idleEventHandler", "connectListenerHandler", echoHandler); + } else { + pipeline.addLast("connectListenerHandler", echoHandler); + } + } }); } diff --git a/src/test/java/com/github/kpavlov/jreactive8583/netty/pipeline/Iso8583ChannelInitializerTest.java b/src/test/java/com/github/kpavlov/jreactive8583/netty/pipeline/Iso8583ChannelInitializerTest.java index 5c11263..b41c581 100644 --- a/src/test/java/com/github/kpavlov/jreactive8583/netty/pipeline/Iso8583ChannelInitializerTest.java +++ b/src/test/java/com/github/kpavlov/jreactive8583/netty/pipeline/Iso8583ChannelInitializerTest.java @@ -10,6 +10,7 @@ import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelPipeline; import io.netty.channel.EventLoopGroup; +import io.netty.handler.timeout.IdleStateHandler; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -19,7 +20,7 @@ import static org.mockito.Mockito.*; @ExtendWith(MockitoExtension.class) -public class Iso8583ChannelInitializerTest { +class Iso8583ChannelInitializerTest { @Mock private EventLoopGroup workerGroup; @@ -44,7 +45,7 @@ public void setUp() { } @Test - public void testInitChannelWithLogger() { + void testInitChannelWithLogger() { //given configurationBuilder.addLoggingHandler(true); final var channelInitializer = createChannelInitializer(configurer); @@ -57,7 +58,7 @@ public void testInitChannelWithLogger() { } @Test - public void testInitChannelWithoutLogger() { + void testInitChannelWithoutLogger() { //given configurationBuilder.addLoggingHandler(false); @@ -67,11 +68,12 @@ public void testInitChannelWithoutLogger() { channelInitializer.initChannel(channel); //then - verify(pipeline, never()).addLast(any(EventLoopGroup.class), anyString(), any(IsoMessageLoggingHandler.class)); + verify(pipeline, never()) + .addLast(any(EventLoopGroup.class), anyString(), any(IsoMessageLoggingHandler.class)); } @Test - public void testInitChannelWithDefaultLoggingSetting() { + void testInitChannelWithDefaultLoggingSetting() { //given final var channelInitializer = createChannelInitializer(configurer); @@ -83,6 +85,40 @@ public void testInitChannelWithDefaultLoggingSetting() { .addLast(any(EventLoopGroup.class), anyString(), any(IsoMessageLoggingHandler.class)); } + @Test + void shouldInitChannelWithEchoEnabled() { + //given + configurationBuilder.addEchoMessageListener(true); + final var channelInitializer = createChannelInitializer(configurer); + + //when + channelInitializer.initChannel(channel); + + //then + verify(pipeline, times(1)) + .addLast(eq("idleState"), any(IdleStateHandler.class)); + verify(pipeline, times(1)) + .addLast(eq("idleEventHandler"), any(IdleEventHandler.class)); + + } + + @Test + void shouldInitChannelWithEchoDisabled() { + //given + configurationBuilder.addEchoMessageListener(false); + + final var channelInitializer = createChannelInitializer(configurer); + + //when + channelInitializer.initChannel(channel); + + //then + verify(pipeline, never()) + .addLast(anyString(), any(IdleStateHandler.class)); + verify(pipeline, never()) + .addLast(anyString(), any(IdleEventHandler.class)); + } + private Iso8583ChannelInitializer, ConnectorConfiguration> createChannelInitializer(final ConnectorConfigurer> configurer) { return new Iso8583ChannelInitializer( configurationBuilder.build(),