public static Message buildResponse(UUID id) { return new Message(id, Op.Response); } }
public static Message buildResponse(UUID id, Op op) { return new Message(id, op); }
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) { // uuid byte[] uuidBytes = new byte[36]; in.readBytes(uuidBytes); UUID id = UUID.fromString(new String(uuidBytes)); // op byte[] opBytes = new byte[4]; in.readBytes(opBytes); Message.Op op = Message.Op.fromBytes(opBytes); // payloadSize byte[] payloadSizeBytes = new byte[4]; in.readBytes(payloadSizeBytes); int payloadSize = Ints.fromByteArray(payloadSizeBytes); if (in.readableBytes() < payloadSize) { ctx.fireExceptionCaught(new DecoderException("Not enough bytes available to decode payload")); } out.add(in.readRetainedSlice(payloadSize)); out.add(new Message(id, op)); } }
@Test public void testErrorNoPayload() { thrown.expect(EncoderException.class); thrown.expectMessage("Encoder received Message without anything to encode"); Integer payload = new Integer(1); Request request = new Request(UUID.randomUUID(), SettableFuture.create()); Message message = new Message(request, payload); channel.writeOutbound(message); channel.runPendingTasks(); } }
@Test public void testWriteSucceeds() throws Exception { Integer payload = new Integer(1); Request request = new Request( UUID.fromString("f6fb3dbf-43fa-4ebf-8f45-b38e7444beae"), SettableFuture.create()); Message message = new Message(request, payload); ByteBuf buf = Unpooled.buffer(); buf.writeBytes(Ints.toByteArray(payload)); channel.writeOutbound(buf); channel.writeOutbound(message); channel.runPendingTasks(); ByteBuf encoded = (ByteBuf) channel.outboundMessages().poll(); String expectedEncoded = new StringBuilder() .append(" +-------------------------------------------------+\n") .append(" | 0 1 2 3 4 5 6 7 8 9 a b c d e f |\n") .append( "+--------+-------------------------------------------------+----------------+\n") .append( "|00000000| 66 36 66 62 33 64 62 66 2d 34 33 66 61 2d 34 65 |f6fb3dbf-43fa-4e|\n") .append( "|00000010| 62 66 2d 38 66 34 35 2d 62 33 38 65 37 34 34 34 |bf-8f45-b38e7444|\n") .append( "|00000020| 62 65 61 65 00 00 00 00 00 00 00 04 00 00 00 01 |beae............|\n") .append("+--------+-------------------------------------------------+----------------+") .toString(); assertEquals( "Expected:\n" + expectedEncoded, expectedEncoded, ByteBufUtil.prettyHexDump(encoded)); }
@Test public void testErrorInvalidPayload() { thrown.expect(EncoderException.class); thrown.expectMessage("Can only encode Message or ByteBuf"); Integer payload = new Integer(1); Request request = new Request(UUID.randomUUID(), SettableFuture.create()); Message message = new Message(request, payload); channel.writeOutbound(payload); channel.writeOutbound(message); channel.runPendingTasks(); }
new Request( UUID.fromString("6566a1c5-b0ec-47a8-8e77-b5173b78873a"), SettableFuture.create()); Message message = new Message(request, payload);
@Test public void testRequestNoResponse() { Integer payload = new Integer(1); Request request = new Request(UUID.randomUUID(), SettableFuture.create()); Message message = new Message(request, payload); channel.writeOutbound(message); channel.runPendingTasks(); // assert request not in map assertNull(codec.getRequest(codec.getMapping(channel), message)); // assert two objects written in the correct order assertEquals(payload, channel.outboundMessages().poll()); assertEquals(message, channel.outboundMessages().poll()); }
@Test public void testRequestExpectedResponse() { Integer payload = new Integer(1); Request request = new Request(UUID.randomUUID(), SettableFuture.create(), SettableFuture.create()); Message message = new Message(request, payload); channel.writeOutbound(message); channel.runPendingTasks(); // assert request in map assertEquals(request, codec.getRequest(codec.getMapping(channel), message)); // assert two objects written in the correct order assertEquals(payload, channel.outboundMessages().poll()); assertEquals(message, channel.outboundMessages().poll()); }
@Test public void testEncodeGet() { Request request = new Request( UUID.fromString("934bf16b-7d6f-4f8a-92ce-6d46affb933f"), SettableFuture.create(), SettableFuture.create()); HttpRequest payload = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/path"); Message message = new Message(request, payload); channel.writeOutbound(message); channel.runPendingTasks(); ByteBuf expectedLength = Unpooled.copiedBuffer(hexToBytes("0042")); ByteBuf expectedEncoded = Unpooled.copiedBuffer( hexToBytes( "39333462663136622d376436662d346638612d393263652d3664343661666662393333660000000100000016474554202f7061746820485454502f312e310d0a0d0a")); ByteBuf length = (ByteBuf) channel.outboundMessages().poll(); ByteBuf encoded = (ByteBuf) channel.outboundMessages().poll(); assertTrue( "Expected: " + ByteBufUtil.hexDump(expectedLength), ByteBufUtil.equals(expectedLength, length)); assertTrue( "Expected: " + ByteBufUtil.hexDump(expectedEncoded), ByteBufUtil.equals(expectedEncoded, encoded)); }
new DefaultHttpHeaders()); Message message = new Message(request, payload); channel.writeOutbound(message); channel.runPendingTasks();