@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 testNoResponsePayload() { thrown.expect(RuntimeException.class); thrown.expectMessage( "No response payload received for request id 'a3f3a6db-aefc-40d3-bd49-3d52ee474630'"); Request request = new Request( UUID.fromString("a3f3a6db-aefc-40d3-bd49-3d52ee474630"), SettableFuture.create(), SettableFuture.create()); Message message = Message.buildResponse(request.getId()); channel.writeInbound(message); }
@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(); }
@Test public void testRequestUnexpectedResponse() { thrown.expect(RuntimeException.class); thrown.expectMessage( "Unexpected response received for request id 'db7598b0-a153-4ab1-85c2-2120a729c2db'"); Request request = new Request( UUID.fromString("db7598b0-a153-4ab1-85c2-2120a729c2db"), SettableFuture.create(), SettableFuture.create()); Integer payload = new Integer(1); Message message = Message.buildResponse(request.getId()); channel.writeInbound(payload); channel.writeInbound(message); }
new Request( UUID.fromString("6566a1c5-b0ec-47a8-8e77-b5173b78873a"), SettableFuture.create()); Message message = new Message(request, payload);
@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 testMultipleResponsePayloads() { thrown.expect(RuntimeException.class); thrown.expectMessage( "Multiple response payloads received for request id '8f98f582-b726-408e-b974-040276b408dd'"); Request request = new Request( UUID.fromString("8f98f582-b726-408e-b974-040276b408dd"), SettableFuture.create(), SettableFuture.create()); Message message = Message.buildResponse(request.getId()); Integer payload1 = new Integer(1); Integer payload2 = new Integer(2); channel.writeInbound(payload1); channel.writeInbound(payload2); channel.writeInbound(message); } }
@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 testExpectedResponse() throws Exception { Request request = new Request(UUID.randomUUID(), SettableFuture.create(), SettableFuture.create()); codec.setRequest(codec.getMapping(channel), request); Integer payload = new Integer(1); Message message = Message.buildResponse(request.getId()); channel.writeInbound(payload); channel.writeInbound(message); channel.runPendingTasks(); Response response = request.getResponseFuture().get(1, TimeUnit.SECONDS); assertEquals(request.getId(), response.getInResponseTo()); assertEquals(payload, response.getPayload()); }
@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 Request( UUID.fromString("70b3e594-2387-412c-9050-f6f7f46b64d5"), SettableFuture.create(),