PostgreSQLPacketCodecEngine implements DatabasePacketCodecEngine<PostgreSQLPacket> { @Override public void encode(final ChannelHandlerContext context, final PostgreSQLPacket message, final ByteBuf out) { try (PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(context.alloc().buffer())) { message.write(payload); if (!(message instanceof PostgreSQLSSLNegativePacket)) { out.writeByte(message.getMessageType()); out.writeInt(payload.getByteBuf().readableBytes() + PostgreSQLPacket.PAYLOAD_LENGTH); } out.writeBytes(payload.getByteBuf()); } }  @Override boolean isValidHeader(final int readableBytes); @Override void decode(final ChannelHandlerContext context, final ByteBuf in, final List<Object> out, final int readableBytes); @Override void encode(final ChannelHandlerContext context, final PostgreSQLPacket message, final ByteBuf out); @Override PostgreSQLPacketPayload createPacketPayload(final ByteBuf message);  }
@Test public void assertEncode() { ByteBufAllocator byteBufAllocator = mock(ByteBufAllocator.class); when(context.alloc()).thenReturn(byteBufAllocator); ByteBuf payloadByteBuf = mock(ByteBuf.class); when(byteBufAllocator.buffer()).thenReturn(payloadByteBuf); when(payloadByteBuf.readableBytes()).thenReturn(50); PostgreSQLPacket actualMessage = mock(PostgreSQLPacket.class); when(actualMessage.getMessageType()).thenReturn(PostgreSQLCommandPacketType.AUTHENTICATION_OK.getValue()); new PostgreSQLPacketCodecEngine().encode(context, actualMessage, byteBuf); verify(actualMessage).write(ArgumentMatchers.any()); verify(byteBuf).writeByte(PostgreSQLCommandPacketType.AUTHENTICATION_OK.getValue()); verify(byteBuf).writeInt(50 + PostgreSQLPacket.PAYLOAD_LENGTH); verify(byteBuf).writeBytes(payloadByteBuf); }