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));
}
Aggregations