Search in sources :

Example 21 with StreamMessage

use of io.scalecube.streams.StreamMessage in project scalecube by scalecube.

the class ByteBufCodecTest method testEncodeWithDataFieldMessage.

@Test
public void testEncodeWithDataFieldMessage() {
    ByteBuf dataBuf = copiedBuffer("{\"greeting\":\"yadayada\"}", UTF_8);
    StreamMessage message = StreamMessage.builder().qualifier("q").data(copiedBuffer(dataBuf)).build();
    ByteBuf targetBuf = Unpooled.buffer();
    ImmutableList<String> get = ImmutableList.of("q");
    ImmutableList<String> match = ImmutableList.of("data");
    ByteBufCodec.encode(targetBuf, get, match, headerName -> {
        switch(headerName) {
            case QUALIFIER_NAME:
                return message.qualifier();
            case DATA_NAME:
                return message.data();
            default:
                return null;
        }
    });
    StreamMessage.Builder messageBuilder = StreamMessage.builder();
    ByteBufCodec.decode(copiedBuffer(targetBuf), get, match, (headerName, obj) -> {
        switch(headerName) {
            case QUALIFIER_NAME:
                messageBuilder.qualifier((String) obj);
                break;
            case DATA_NAME:
                messageBuilder.data(obj);
                break;
            default:
        }
    });
    assertEquals(dataBuf.toString(UTF_8), ((ByteBuf) messageBuilder.build().data()).toString(UTF_8));
}
Also used : StreamMessage(io.scalecube.streams.StreamMessage) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Aggregations

StreamMessage (io.scalecube.streams.StreamMessage)21 Test (org.junit.Test)21 ByteBuf (io.netty.buffer.ByteBuf)11