Search in sources :

Example 6 with Message

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

the class CodecsTest method codecs.

@ParameterizedTest
@MethodSource("codecsCouples")
void codecs(CodecCouple codecCouple) {
    Codec serializer = codecCouple.serializer;
    Codec deserializer = codecCouple.deserializer;
    Stream<MessageTestConfiguration> messageOperations = Stream.of(test(builder -> builder.properties().messageId(42).messageBuilder(), message -> assertThat(message.getProperties().getMessageIdAsLong()).isEqualTo(42)), test(builder -> builder.properties().messageId("foo").messageBuilder(), message -> assertThat(message.getProperties().getMessageIdAsString()).isEqualTo("foo")), test(builder -> builder.properties().messageId("bar".getBytes(CHARSET)).messageBuilder(), message -> assertThat(message.getProperties().getMessageIdAsBinary()).isEqualTo("bar".getBytes(CHARSET))), test(builder -> builder.properties().messageId(TEST_UUID).messageBuilder(), message -> assertThat(message.getProperties().getMessageIdAsUuid()).isEqualTo(TEST_UUID)), test(builder -> builder.properties().correlationId(42 + 10).messageBuilder(), message -> assertThat(message.getProperties().getCorrelationIdAsLong()).isEqualTo(42 + 10)), test(builder -> builder.properties().correlationId("correlation foo").messageBuilder(), message -> assertThat(message.getProperties().getCorrelationIdAsString()).isEqualTo("correlation foo")), test(builder -> builder.properties().correlationId("correlation bar".getBytes(CHARSET)).messageBuilder(), message -> assertThat(message.getProperties().getCorrelationIdAsBinary()).isEqualTo("correlation bar".getBytes(CHARSET))), test(builder -> builder.properties().correlationId(TEST_UUID).messageBuilder(), message -> assertThat(message.getProperties().getCorrelationIdAsUuid()).isEqualTo(TEST_UUID)), test(builder -> builder, message -> assertThat(message.getProperties().getGroupSequence()).isEqualTo(-1)), test(builder -> builder.properties().groupSequence(10).messageBuilder(), message -> assertThat(message.getProperties().getGroupSequence()).isEqualTo(10)), test(builder -> builder.properties().groupSequence((long) Integer.MAX_VALUE + 10).messageBuilder(), message -> assertThat(message.getProperties().getGroupSequence()).isEqualTo((long) Integer.MAX_VALUE + 10)));
    String body = "hello";
    String userId = "yoda";
    String to = "the to address";
    String subject = "the subject";
    String replyTo = "the reply to";
    String contentType = "text/plain";
    String contentEncoding = "gzip";
    String groupId = "the group ID";
    String replyToGroupId = "the reply to group ID";
    long now = new Date().getTime();
    UUID uuid = UUID.randomUUID();
    byte[] binary = "the binary".getBytes(CHARSET);
    String string = "a string";
    String symbol = "a symbol";
    messageOperations.forEach(messageTestConfiguration -> {
        Function<MessageBuilder, MessageBuilder> messageOperation = messageTestConfiguration.messageOperation;
        Consumer<Message> messageExpectation = messageTestConfiguration.messageExpectation;
        MessageBuilder messageBuilder = codecCouple.messageBuilderSupplier.get();
        Message outboundMessage = messageOperation.apply(messageBuilder).addData(body.getBytes(CHARSET)).properties().userId(userId.getBytes(CHARSET)).to(to).subject(subject).replyTo(replyTo).contentType(contentType).contentEncoding(contentEncoding).absoluteExpiryTime(now + 1000).creationTime(now).groupId(groupId).replyToGroupId(replyToGroupId).messageBuilder().applicationProperties().entry("boolean", Boolean.FALSE).entry("byte", (byte) 1).entry("short", (short) 2).entry("int", 3).entry("long", 4l).entryUnsigned("ubyte", (byte) 1).entryUnsigned("ushort", (short) 2).entryUnsigned("uint", 3).entryUnsigned("ulong", 4l).entryUnsigned("large.ubyte", (byte) (Byte.MAX_VALUE + 10)).entryUnsigned("large.ushort", (short) (Short.MAX_VALUE + 10)).entryUnsigned("large.uint", Integer.MAX_VALUE + 10).entryUnsigned("large.ulong", Long.MAX_VALUE + 10).entry("float", 3.14f).entry("double", 6.28).entry("char", 'c').entryTimestamp("timestamp", now).entry("uuid", uuid).entry("binary", binary).entry("string", string).entrySymbol("symbol", symbol).messageBuilder().messageAnnotations().entry("annotations.boolean", Boolean.FALSE).entry("annotations.byte", (byte) 1).entry("annotations.short", (short) 2).entry("annotations.int", 3).entry("annotations.long", 4l).entryUnsigned("annotations.ubyte", (byte) 1).entryUnsigned("annotations.ushort", (short) 2).entryUnsigned("annotations.uint", 3).entryUnsigned("annotations.ulong", 4l).entryUnsigned("annotations.large.ubyte", (byte) (Byte.MAX_VALUE + 10)).entryUnsigned("annotations.large.ushort", (short) (Short.MAX_VALUE + 10)).entryUnsigned("annotations.large.uint", Integer.MAX_VALUE + 10).entryUnsigned("annotations.large.ulong", Long.MAX_VALUE + 10).entry("annotations.float", 3.14f).entry("annotations.double", 6.28).entry("annotations.char", 'c').entryTimestamp("annotations.timestamp", now).entry("annotations.uuid", uuid).entry("annotations.binary", binary).entry("annotations.string", string).entrySymbol("annotations.symbol", symbol).messageBuilder().build();
        Codec.EncodedMessage encoded = serializer.encode(outboundMessage);
        byte[] encodedData = new byte[encoded.getSize()];
        System.arraycopy(encoded.getData(), 0, encodedData, 0, encoded.getSize());
        Message inboundMessage = deserializer.decode(encodedData);
        messageExpectation.accept(inboundMessage);
        assertThat(new String(inboundMessage.getBodyAsBinary())).isEqualTo(body);
        assertThat(inboundMessage.getProperties().getUserId()).isEqualTo(userId.getBytes(CHARSET));
        assertThat(inboundMessage.getProperties().getTo()).isEqualTo(to);
        assertThat(inboundMessage.getProperties().getSubject()).isEqualTo(subject);
        assertThat(inboundMessage.getProperties().getReplyTo()).isEqualTo(replyTo);
        assertThat(inboundMessage.getProperties().getContentType()).isEqualTo(contentType);
        assertThat(inboundMessage.getProperties().getContentEncoding()).isEqualTo(contentEncoding);
        assertThat(inboundMessage.getProperties().getAbsoluteExpiryTime()).isEqualTo(now + 1000);
        assertThat(inboundMessage.getProperties().getCreationTime()).isEqualTo(now);
        assertThat(inboundMessage.getProperties().getGroupId()).isEqualTo(groupId);
        assertThat(inboundMessage.getProperties().getReplyToGroupId()).isEqualTo(replyToGroupId);
        // application properties
        assertThat(inboundMessage.getApplicationProperties().get("boolean")).isNotNull().isInstanceOf(Boolean.class).isEqualTo(Boolean.FALSE);
        assertThat(inboundMessage.getApplicationProperties().get("byte")).isNotNull().isInstanceOf(Byte.class).isEqualTo(Byte.valueOf((byte) 1));
        assertThat(inboundMessage.getApplicationProperties().get("short")).isNotNull().isInstanceOf(Short.class).isEqualTo(Short.valueOf((short) 2));
        assertThat(inboundMessage.getApplicationProperties().get("int")).isNotNull().isInstanceOf(Integer.class).isEqualTo(Integer.valueOf(3));
        assertThat(inboundMessage.getApplicationProperties().get("long")).isNotNull().isInstanceOf(Long.class).isEqualTo(Long.valueOf(4));
        assertThat(inboundMessage.getApplicationProperties().get("ubyte")).isNotNull().isInstanceOf(UnsignedByte.class).isEqualTo(UnsignedByte.valueOf((byte) 1));
        assertThat(inboundMessage.getApplicationProperties().get("ushort")).isNotNull().isInstanceOf(UnsignedShort.class).isEqualTo(UnsignedShort.valueOf((short) 2));
        assertThat(inboundMessage.getApplicationProperties().get("uint")).isNotNull().isInstanceOf(UnsignedInteger.class).isEqualTo(UnsignedInteger.valueOf(3));
        assertThat(inboundMessage.getApplicationProperties().get("ulong")).isNotNull().isInstanceOf(UnsignedLong.class).isEqualTo(UnsignedLong.valueOf(4));
        assertThat(inboundMessage.getApplicationProperties().get("large.ubyte")).isNotNull().isInstanceOf(UnsignedByte.class).asInstanceOf(InstanceOfAssertFactories.type(UnsignedByte.class)).extracting(v -> v.intValue()).isEqualTo(Byte.MAX_VALUE + 10);
        assertThat(inboundMessage.getApplicationProperties().get("large.ushort")).isNotNull().isInstanceOf(UnsignedShort.class).asInstanceOf(InstanceOfAssertFactories.type(UnsignedShort.class)).extracting(v -> v.intValue()).isEqualTo(Short.MAX_VALUE + 10);
        assertThat(inboundMessage.getApplicationProperties().get("large.uint")).isNotNull().isInstanceOf(UnsignedInteger.class).asInstanceOf(InstanceOfAssertFactories.type(UnsignedInteger.class)).extracting(v -> v.toString()).isEqualTo(BigInteger.valueOf((long) Integer.MAX_VALUE + 10L).toString());
        assertThat(inboundMessage.getApplicationProperties().get("large.ulong")).isNotNull().isInstanceOf(UnsignedLong.class).asInstanceOf(InstanceOfAssertFactories.type(UnsignedLong.class)).extracting(v -> v.toString()).isEqualTo(BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.TEN).toString());
        assertThat(inboundMessage.getApplicationProperties().get("float")).isNotNull().isInstanceOf(Float.class).isEqualTo(Float.valueOf(3.14f));
        assertThat(inboundMessage.getApplicationProperties().get("double")).isNotNull().isInstanceOf(Double.class).isEqualTo(Double.valueOf(6.28));
        assertThat(inboundMessage.getApplicationProperties().get("char")).isNotNull().isInstanceOf(Character.class).isEqualTo('c');
        assertThat(inboundMessage.getApplicationProperties().get("timestamp")).isNotNull().isInstanceOf(Long.class).isEqualTo(now);
        assertThat(inboundMessage.getApplicationProperties().get("uuid")).isNotNull().isInstanceOf(UUID.class).isEqualTo(uuid);
        assertThat(inboundMessage.getApplicationProperties().get("binary")).isNotNull().isInstanceOf(byte[].class).isEqualTo(binary);
        assertThat(inboundMessage.getApplicationProperties().get("string")).isNotNull().isInstanceOf(String.class).isEqualTo(string);
        assertThat(inboundMessage.getApplicationProperties().get("symbol")).isNotNull().isInstanceOf(String.class).isEqualTo(symbol);
        // message annotations
        assertThat(inboundMessage.getMessageAnnotations().get("annotations.boolean")).isNotNull().isInstanceOf(Boolean.class).isEqualTo(Boolean.FALSE);
        assertThat(inboundMessage.getMessageAnnotations().get("annotations.byte")).isNotNull().isInstanceOf(Byte.class).isEqualTo(Byte.valueOf((byte) 1));
        assertThat(inboundMessage.getMessageAnnotations().get("annotations.short")).isNotNull().isInstanceOf(Short.class).isEqualTo(Short.valueOf((short) 2));
        assertThat(inboundMessage.getMessageAnnotations().get("annotations.int")).isNotNull().isInstanceOf(Integer.class).isEqualTo(Integer.valueOf(3));
        assertThat(inboundMessage.getMessageAnnotations().get("annotations.long")).isNotNull().isInstanceOf(Long.class).isEqualTo(Long.valueOf(4));
        assertThat(inboundMessage.getMessageAnnotations().get("annotations.ubyte")).isNotNull().isInstanceOf(UnsignedByte.class).isEqualTo(UnsignedByte.valueOf((byte) 1));
        assertThat(inboundMessage.getMessageAnnotations().get("annotations.ushort")).isNotNull().isInstanceOf(UnsignedShort.class).isEqualTo(UnsignedShort.valueOf((short) 2));
        assertThat(inboundMessage.getMessageAnnotations().get("annotations.uint")).isNotNull().isInstanceOf(UnsignedInteger.class).isEqualTo(UnsignedInteger.valueOf(3));
        assertThat(inboundMessage.getMessageAnnotations().get("annotations.ulong")).isNotNull().isInstanceOf(UnsignedLong.class).isEqualTo(UnsignedLong.valueOf(4));
        assertThat(inboundMessage.getMessageAnnotations().get("annotations.large.ubyte")).isNotNull().isInstanceOf(UnsignedByte.class).asInstanceOf(InstanceOfAssertFactories.type(UnsignedByte.class)).extracting(v -> v.intValue()).isEqualTo(Byte.MAX_VALUE + 10);
        assertThat(inboundMessage.getMessageAnnotations().get("annotations.large.ushort")).isNotNull().isInstanceOf(UnsignedShort.class).asInstanceOf(InstanceOfAssertFactories.type(UnsignedShort.class)).extracting(v -> v.intValue()).isEqualTo(Short.MAX_VALUE + 10);
        assertThat(inboundMessage.getMessageAnnotations().get("annotations.large.uint")).isNotNull().isInstanceOf(UnsignedInteger.class).asInstanceOf(InstanceOfAssertFactories.type(UnsignedInteger.class)).extracting(v -> v.toString()).isEqualTo(BigInteger.valueOf((long) Integer.MAX_VALUE + 10L).toString());
        assertThat(inboundMessage.getMessageAnnotations().get("annotations.large.ulong")).isNotNull().isInstanceOf(UnsignedLong.class).asInstanceOf(InstanceOfAssertFactories.type(UnsignedLong.class)).extracting(v -> v.toString()).isEqualTo(BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.TEN).toString());
        assertThat(inboundMessage.getMessageAnnotations().get("annotations.float")).isNotNull().isInstanceOf(Float.class).isEqualTo(Float.valueOf(3.14f));
        assertThat(inboundMessage.getMessageAnnotations().get("annotations.double")).isNotNull().isInstanceOf(Double.class).isEqualTo(Double.valueOf(6.28));
        assertThat(inboundMessage.getMessageAnnotations().get("annotations.char")).isNotNull().isInstanceOf(Character.class).isEqualTo('c');
        assertThat(inboundMessage.getMessageAnnotations().get("annotations.timestamp")).isNotNull().isInstanceOf(Long.class).isEqualTo(now);
        assertThat(inboundMessage.getMessageAnnotations().get("annotations.uuid")).isNotNull().isInstanceOf(UUID.class).isEqualTo(uuid);
        assertThat(inboundMessage.getMessageAnnotations().get("annotations.binary")).isNotNull().isInstanceOf(byte[].class).isEqualTo(binary);
        assertThat(inboundMessage.getMessageAnnotations().get("annotations.string")).isNotNull().isInstanceOf(String.class).isEqualTo(string);
        assertThat(inboundMessage.getMessageAnnotations().get("annotations.symbol")).isNotNull().isInstanceOf(String.class).isEqualTo(symbol);
    });
}
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) Message(com.rabbitmq.stream.Message) Codec(com.rabbitmq.stream.Codec) MessageBuilder(com.rabbitmq.stream.MessageBuilder) UUID(java.util.UUID) UnsignedShort(com.rabbitmq.stream.amqp.UnsignedShort) UnsignedShort(com.rabbitmq.stream.amqp.UnsignedShort) UnsignedLong(com.rabbitmq.stream.amqp.UnsignedLong) UnsignedByte(com.rabbitmq.stream.amqp.UnsignedByte) Date(java.util.Date) UnsignedInteger(com.rabbitmq.stream.amqp.UnsignedInteger) BigInteger(java.math.BigInteger) UnsignedByte(com.rabbitmq.stream.amqp.UnsignedByte) UnsignedLong(com.rabbitmq.stream.amqp.UnsignedLong) UnsignedInteger(com.rabbitmq.stream.amqp.UnsignedInteger) EncodedMessage(com.rabbitmq.stream.Codec.EncodedMessage) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 7 with Message

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

the class CodecsTest method publishingIdShouldNotBeSetOnMessageIfNotSetOnMessageBuilder.

@ParameterizedTest
@MethodSource("messageBuilders")
void publishingIdShouldNotBeSetOnMessageIfNotSetOnMessageBuilder(MessageBuilder builder) {
    Message message = builder.build();
    assertThat(message.hasPublishingId()).isFalse();
    assertThat(message.getPublishingId()).isEqualTo(0);
}
Also used : EncodedMessage(com.rabbitmq.stream.Codec.EncodedMessage) Message(com.rabbitmq.stream.Message) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 8 with Message

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

the class CodecsTest method readCreatedMessage.

@ParameterizedTest
@MethodSource
void readCreatedMessage(Codec codec) {
    // same conversion logic as for encoding/decoding, so not testing all types
    Message message = codec.messageBuilder().addData("hello".getBytes(CHARSET)).properties().messageId(42).messageBuilder().applicationProperties().entry("property1", "value1").messageBuilder().messageAnnotations().entry("annotation1", "value1").messageBuilder().build();
    assertThat(message.getBodyAsBinary()).isEqualTo("hello".getBytes(CHARSET));
    assertThat(message.getBody()).isNotNull();
    assertThat(message.getProperties().getMessageIdAsLong()).isEqualTo(42);
    assertThat(message.getApplicationProperties()).hasSize(1).containsKey("property1").containsValue("value1");
    assertThat(message.getMessageAnnotations()).hasSize(1).containsKey("annotation1").containsValue("value1");
}
Also used : EncodedMessage(com.rabbitmq.stream.Codec.EncodedMessage) Message(com.rabbitmq.stream.Message) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 9 with Message

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

the class ProducerUsage method producerWithNameQueryLastPublishingId.

void producerWithNameQueryLastPublishingId() {
    Environment environment = Environment.builder().build();
    // tag::producer-queries-last-publishing-id[]
    Producer producer = environment.producerBuilder().name(// <1>
    "my-app-producer").confirmTimeout(// <2>
    Duration.ZERO).stream("my-stream").build();
    // <3>
    long nextPublishingId = producer.getLastPublishingId() + 1;
    while (moreContent(nextPublishingId)) {
        // <4>
        byte[] content = getContent(nextPublishingId);
        Message message = producer.messageBuilder().publishingId(// <5>
        nextPublishingId).addData(content).build();
        producer.send(message, confirmationStatus -> {
        });
        nextPublishingId++;
    }
// end::producer-queries-last-publishing-id[]
}
Also used : Producer(com.rabbitmq.stream.Producer) Message(com.rabbitmq.stream.Message) Environment(com.rabbitmq.stream.Environment)

Example 10 with Message

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

the class ProducerUsage method producerComplexMessage.

void producerComplexMessage() {
    Environment environment = Environment.builder().build();
    Producer producer = environment.producerBuilder().stream("my-stream").build();
    // tag::producer-publish-complex-message[]
    Message message = // <1>
    producer.messageBuilder().properties().messageId(UUID.randomUUID()).correlationId(UUID.randomUUID()).contentType("text/plain").messageBuilder().addData(// <4>
    "hello".getBytes(StandardCharsets.UTF_8)).build();
    // <6>
    producer.send(message, confirmationStatus -> {
    });
// end::producer-publish-complex-message[]
}
Also used : Producer(com.rabbitmq.stream.Producer) Message(com.rabbitmq.stream.Message) Environment(com.rabbitmq.stream.Environment)

Aggregations

Message (com.rabbitmq.stream.Message)29 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)14 Environment (com.rabbitmq.stream.Environment)13 List (java.util.List)13 Producer (com.rabbitmq.stream.Producer)11 StandardCharsets (java.nio.charset.StandardCharsets)11 CountDownLatch (java.util.concurrent.CountDownLatch)11 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)11 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)11 Test (org.junit.jupiter.api.Test)10 EncodedMessage (com.rabbitmq.stream.Codec.EncodedMessage)9 ArrayList (java.util.ArrayList)9 UUID (java.util.UUID)9 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)9 SECONDS (java.util.concurrent.TimeUnit.SECONDS)9 IntStream (java.util.stream.IntStream)9 Codec (com.rabbitmq.stream.Codec)8 OffsetSpecification (com.rabbitmq.stream.OffsetSpecification)8 Collections (java.util.Collections)8 Consumer (com.rabbitmq.stream.Consumer)7