Sebastian D'Agostino
Sebastian D'Agostino

Reputation: 1675

How to test a socket server in Citrus?

I would like to do some unit testing to a socket server built with Netty.

The Socket Server has the simple following code:

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class SocketServer implements Runnable {

    private int port;

    private EventLoopGroup bossGroup;
    private EventLoopGroup workerGroup;

    private ChannelFuture channelFuture;
    private ServerBootstrap bootstrap;

    public SocketServer(int port) {
        this.port = port;
        this.bossGroup = new NioEventLoopGroup();
        this.workerGroup = new NioEventLoopGroup();
    }

    public int getPort() {
        return port;
    }

    @Override
    public void run() {
        try {
            bootstrap = new ServerBootstrap();
            bootstrap
                    .group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        public void initChannel(SocketChannel ch)
                                throws Exception {
                            ch.pipeline()
                                    .addLast(new ReceiveMessageServerHandler())
                                    .addLast(new ParseMessageServerHandler());
                        }
                    }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true);

            // Bind and start to accept incoming connections.
            channelFuture = bootstrap.bind(port).sync();

            // Wait until the server socket is closed
            channelFuture.channel().closeFuture().sync();

        } catch (InterruptedException e) {
            e.printStackTrace();

        } finally {
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }
    }

    public void shutdown() throws InterruptedException {
        channelFuture.channel().close();
    }

}

On the MessageHandlers I do first receive text messages separated by '\n'. I pretty much need a telnet client.

I want to test that I can send different messages to the server and that I receive certain expected responses within a timeframe.

I tried using Citrus Framework, but I wasn't able to get any results because it does not provide a proper plain text protocol (I tried Rest, Soap, etc, but they are no good for me). I could not find the answer in Citrus Reference 2.4.

Citrus 2.4 Reference - HTML Version

Upvotes: 1

Views: 1313

Answers (2)

Christoph Deppisch
Christoph Deppisch

Reputation: 2216

You can also use the Citrus Apache Camel integration for that. You need citrus-camel module and camel-netty (http://camel.apache.org/netty.html) or camel-netty4 (http://camel.apache.org/netty4.html) dependency. Then you can directly use the Camel Netty component for sending the message:

@Test
@CitrusTest
public void sendNettyMessageTest() {
    status(Status.DRAFT);
    //sends data to server
    send("camel:netty4:tcp://localhost:9123").payload("Message 1");
    send("camel:netty4:tcp://localhost:9123").payload("Message 2");
}

This is making use of dynamic endpoints in Citrus where endpoint configuration is created at test runtime. So practically no additional configuration required here!

Upvotes: 2

Sebastian D&#39;Agostino
Sebastian D&#39;Agostino

Reputation: 1675

I was able to solve the problem before I could post the question (I wrote the question yesterday but I didn't post it until today... that I found the answer).

So I could finally solve this issue with Citrus Framework and Spring Integration.

After reading this with a colleague I was able to use a Spring Integration TCP adapter to work as a channel (SocketChannel from java.nio) for a Citrus endpoint.

Spring Integration Reference - IP

Here you may see the citrus-context.xml configuration I used:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:citrus="http://www.citrusframework.org/schema/config"
    xmlns:int="http://www.springframework.org/schema/integration"
    xmlns:int-ip="http://www.springframework.org/schema/integration/ip"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
        http://www.citrusframework.org/schema/config http://www.citrusframework.org/schema/config/citrus-config.xsd
        http://www.springframework.org/schema/integration  http://www.springframework.org/schema/integration/spring-integration.xsd
        http://www.springframework.org/schema/integration/ip  http://www.springframework.org/schema/integration/ip/spring-integration-ip.xsd">

    <citrus:channel-endpoint id="citrusServiceEndpoint"
        channel-name="input" />

    <int-ip:tcp-connection-factory id="client"
        type="client" host="localhost" port="9123" single-use="true"
        so-timeout="10000" using-nio="true" />

    <int:channel id="input" />

    <int-ip:tcp-outbound-channel-adapter
        id="outboundClient" channel="input" connection-factory="client" />

</beans>

On my Citrus Test I was able to send messages as I needed:

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import com.consol.citrus.TestCaseMetaInfo.Status;
import com.consol.citrus.annotations.CitrusTest;
import com.consol.citrus.dsl.junit.JUnit4CitrusTestDesigner;

public class MyFirstTest extends JUnit4CitrusTestDesigner {

    private static final int PORT = 9123;
    private static SocketServer socketServer;
    private static Thread socketThread;

    @BeforeClass
    public static void setUp() throws Exception {
        socketServer = new SocketServer(PORT);
        socketThread = new Thread(socketServer);
        socketThread.start();
    }

    @AfterClass
    public static void tearDown() throws Exception {
        socketServer.shutdown();
        socketThread.join();
    }

    @Test
    @CitrusTest(name = "sendSpringIntegrationMessageTest")
    public void sendSpringIntegrationMessageTest() {
        status(Status.DRAFT);
        send("citrusServiceEndpoint").payload("Message 1");
        send("citrusServiceEndpoint").payload("Message 2");
    }

}

I hope this helps anyone that may have the same problem as I did.

Upvotes: 1

Related Questions