Search in sources :

Example 6 with Codec

use of com.rabbitmq.stream.Codec in project rabbitmq-stream-java-client by rabbitmq.

the class CodecsTest method supportAmqpValueBody.

@ParameterizedTest
@MethodSource("codecs")
void supportAmqpValueBody(Codec codec) {
    Function<Object, Message> encodeDecode = content -> {
        org.apache.qpid.proton.message.Message nativeMessage = org.apache.qpid.proton.message.Message.Factory.create();
        nativeMessage.setBody(new AmqpValue(content));
        QpidProtonAmqpMessageWrapper wrapper = new QpidProtonAmqpMessageWrapper(true, 1L, nativeMessage);
        EncodedMessage encoded = new QpidProtonCodec().encode(wrapper);
        byte[] encodedData = new byte[encoded.getSize()];
        System.arraycopy(encoded.getData(), 0, encodedData, 0, encoded.getSize());
        Message decodedMessage = codec.decode(encodedData);
        return decodedMessage;
    };
    Message m1 = encodeDecode.apply("hello".getBytes(StandardCharsets.UTF_8));
    assertThat(m1.getBodyAsBinary()).asString(StandardCharsets.UTF_8).isEqualTo("hello");
    Message m2 = encodeDecode.apply("a string is not an array of byte");
    assertThatThrownBy(() -> m2.getBodyAsBinary()).isInstanceOf(IllegalStateException.class);
}
Also used : Arrays(java.util.Arrays) EncodedMessage(com.rabbitmq.stream.Codec.EncodedMessage) UnsignedInteger(com.rabbitmq.stream.amqp.UnsignedInteger) Message(com.rabbitmq.stream.Message) Date(java.util.Date) InstanceOfAssertFactories(org.assertj.core.api.InstanceOfAssertFactories) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Codec(com.rabbitmq.stream.Codec) UnsignedByte(com.rabbitmq.stream.amqp.UnsignedByte) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) UnsignedLong(com.rabbitmq.stream.amqp.UnsignedLong) Charset(java.nio.charset.Charset) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue) BigInteger(java.math.BigInteger) MethodSource(org.junit.jupiter.params.provider.MethodSource) ThrowableAssert(org.assertj.core.api.ThrowableAssert) Mockito.when(org.mockito.Mockito.when) UUID(java.util.UUID) StandardCharsets(java.nio.charset.StandardCharsets) Consumer(java.util.function.Consumer) List(java.util.List) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Stream(java.util.stream.Stream) MessageBuilder(com.rabbitmq.stream.MessageBuilder) QpidProtonAmqpMessageWrapper(com.rabbitmq.stream.codec.QpidProtonCodec.QpidProtonAmqpMessageWrapper) UnsignedShort(com.rabbitmq.stream.amqp.UnsignedShort) Mockito.mock(org.mockito.Mockito.mock) EncodedMessage(com.rabbitmq.stream.Codec.EncodedMessage) EncodedMessage(com.rabbitmq.stream.Codec.EncodedMessage) Message(com.rabbitmq.stream.Message) QpidProtonAmqpMessageWrapper(com.rabbitmq.stream.codec.QpidProtonCodec.QpidProtonAmqpMessageWrapper) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

Codec (com.rabbitmq.stream.Codec)6 Charset (java.nio.charset.Charset)5 Supplier (java.util.function.Supplier)5 Message (com.rabbitmq.stream.Message)4 MessageBuilder (com.rabbitmq.stream.MessageBuilder)4 UnsignedByte (com.rabbitmq.stream.amqp.UnsignedByte)4 StandardCharsets (java.nio.charset.StandardCharsets)4 ArrayList (java.util.ArrayList)4 Consumer (java.util.function.Consumer)4 Stream (java.util.stream.Stream)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 MethodSource (org.junit.jupiter.params.provider.MethodSource)4 OffsetSpecification (com.rabbitmq.stream.OffsetSpecification)3 QpidProtonCodec (com.rabbitmq.stream.codec.QpidProtonCodec)3 Arrays (java.util.Arrays)3 List (java.util.List)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Function (java.util.function.Function)3 IntStream (java.util.stream.IntStream)3