@Override
    protected void encode(ChannelHandlerContext ctx, Object msg, List out) throws Exception {
        if (!(msg instanceof List)) {
            ByteBuf byteBuf = Unpooled.buffer();
            ((LispMessage) msg).writeTo(byteBuf);
            out.add(new DatagramPacket(byteBuf, ((LispMessage) msg).getSender()));
            return;
        }

        List<LispMessage> msgList = (List<LispMessage>) msg;

        for (LispMessage message : msgList) {
            if (message != null) {
                ByteBuf byteBuf = Unpooled.buffer();
                message.writeTo(byteBuf);
                out.add(new DatagramPacket(byteBuf, message.getSender()));
            }
        }
    }
@Test
    public void testEncode() throws Exception {
        LispMessageEncoder encoder = new LispMessageEncoder();
        MockLispMessage request = new MockLispMessage(LispType.LISP_MAP_REQUEST);
        MockLispMessage reply = new MockLispMessage(LispType.LISP_MAP_REPLY);
        MockLispMessage register = new MockLispMessage(LispType.LISP_MAP_REGISTER);
        MockLispMessage notify = new MockLispMessage(LispType.LISP_MAP_NOTIFY);

        ByteBuf buff = Unpooled.buffer();
        List<DatagramPacket> list = Lists.newArrayList();
        List<MockLispMessage> messages = ImmutableList.of(request, reply, register, notify);
        encoder.encode(null, messages, list);

        list.forEach(p -> {
            byte[] tmp = new byte[p.content().writerIndex()];
            p.content().readBytes(tmp);
            buff.writeBytes(tmp);
        });

        assertThat(buff, notNullValue());

        StringBuilder expBuilder = new StringBuilder();
        expBuilder.append("LISP message [LISP_MAP_REQUEST] ");
        expBuilder.append("LISP message [LISP_MAP_REPLY] ");
        expBuilder.append("LISP message [LISP_MAP_REGISTER] ");
        expBuilder.append("LISP message [LISP_MAP_NOTIFY] ");

        String expected = expBuilder.toString();
        String returned = new String(buff.array(),
                        StandardCharsets.UTF_8).substring(0, expected.length());
        assertThat(returned, is(expected));
    }