Search in sources :

Example 51 with MapBasedConfig

use of io.smallrye.reactive.messaging.test.common.config.MapBasedConfig in project smallrye-reactive-messaging by smallrye.

the class CloudEventConsumptionTest method testWithBeanReceivingBinaryAndStructuredCloudEvents.

@Test
public void testWithBeanReceivingBinaryAndStructuredCloudEvents() {
    String address = UUID.randomUUID().toString();
    new MapBasedConfig().with("mp.messaging.incoming.source.connector", AmqpConnector.CONNECTOR_NAME).with("mp.messaging.incoming.source.address", address).with("mp.messaging.incoming.source.host", host).with("mp.messaging.incoming.source.port", port).with("mp.messaging.incoming.source.tracing-enabled", false).with("amqp-username", username).with("amqp-password", password).write();
    weld.addBeanClass(MyConsumptionBean.class);
    container = weld.initialize();
    await().until(() -> isAmqpConnectorReady(container));
    MyConsumptionBean bean = container.getBeanManager().createInstance().select(MyConsumptionBean.class).get();
    usage.produce(address, 1, () -> {
        JsonObject json = new JsonObject().put(AmqpCloudEventHelper.AMQP_HEADER_FOR_SPEC_VERSION, CloudEventMetadata.CE_VERSION_1_0).put(AmqpCloudEventHelper.AMQP_HEADER_FOR_TYPE, "type").put(AmqpCloudEventHelper.AMQP_HEADER_FOR_ID, "id").put(AmqpCloudEventHelper.AMQP_HEADER_FOR_SOURCE, "test://test").put(AmqpCloudEventHelper.AMQP_HEADER_FOR_SUBJECT, "foo").put(AmqpCloudEventHelper.AMQP_HEADER_FOR_SCHEMA, "http://schema.io").put(AmqpCloudEventHelper.AMQP_HEADER_FOR_TIME, "2020-07-23T09:12:34Z");
        JsonObject payload = new JsonObject();
        payload.put("name", "neo");
        return AmqpMessage.create().applicationProperties(json).withJsonObjectAsBody(payload).build();
    });
    await().atMost(2, TimeUnit.MINUTES).until(() -> bean.getList().size() == 1);
    Message<JsonObject> message = bean.getList().get(0);
    IncomingCloudEventMetadata<JsonObject> metadata = message.getMetadata(IncomingCloudEventMetadata.class).orElse(null);
    assertThat(metadata).isNotNull();
    assertThat(metadata.getSpecVersion()).isEqualTo(CloudEventMetadata.CE_VERSION_1_0);
    assertThat(metadata.getType()).isEqualTo("type");
    assertThat(metadata.getId()).isEqualTo("id");
    assertThat(metadata.getSource()).isEqualTo(URI.create("test://test"));
    assertThat(metadata.getSubject()).hasValue("foo");
    assertThat(metadata.getDataContentType()).hasValue("application/json");
    assertThat(metadata.getDataSchema()).hasValue(URI.create("http://schema.io"));
    assertThat(metadata.getTimeStamp()).isNotEmpty();
    assertThat(metadata.getData().getString("name")).isEqualTo("neo");
    assertThat(message.getPayload()).isInstanceOf(JsonObject.class);
    assertThat(message.getPayload().getString("name")).isEqualTo("neo");
    usage.produce(address, 1, () -> {
        JsonObject json = new JsonObject().put("specversion", CloudEventMetadata.CE_VERSION_1_0).put("type", "type").put("id", "id").put("source", "test://test").put("subject", "foo").put("datacontenttype", "application/json").put("dataschema", "http://schema.io").put("time", "2020-07-23T09:12:34Z").put("data", new JsonObject().put("name", "neo-2"));
        return AmqpMessage.create().withBufferAsBody(Buffer.buffer(json.toBuffer().getBytes())).contentType(AmqpCloudEventHelper.STRUCTURED_CONTENT_TYPE).build();
    });
    await().atMost(2, TimeUnit.MINUTES).until(() -> bean.getList().size() == 2);
    message = bean.getList().get(1);
    metadata = message.getMetadata(IncomingCloudEventMetadata.class).orElse(null);
    assertThat(metadata).isNotNull();
    assertThat(metadata.getSpecVersion()).isEqualTo(CloudEventMetadata.CE_VERSION_1_0);
    assertThat(metadata.getType()).isEqualTo("type");
    assertThat(metadata.getId()).isEqualTo("id");
    assertThat(metadata.getSource()).isEqualTo(URI.create("test://test"));
    assertThat(metadata.getSubject()).hasValue("foo");
    assertThat(metadata.getDataContentType()).hasValue("application/json");
    assertThat(metadata.getDataSchema()).hasValue(URI.create("http://schema.io"));
    assertThat(metadata.getTimeStamp()).isNotEmpty();
    assertThat(metadata.getData().getString("name")).isEqualTo("neo-2");
    assertThat(message.getPayload()).isInstanceOf(JsonObject.class);
    assertThat(message.getPayload().getString("name")).isEqualTo("neo-2");
}
Also used : JsonObject(io.vertx.core.json.JsonObject) IncomingCloudEventMetadata(io.smallrye.reactive.messaging.ce.IncomingCloudEventMetadata) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig) Test(org.junit.jupiter.api.Test)

Example 52 with MapBasedConfig

use of io.smallrye.reactive.messaging.test.common.config.MapBasedConfig in project smallrye-reactive-messaging by smallrye.

the class CloudEventConsumptionTest method testReceivingBinaryCloudEventsUsingStringPayload.

@SuppressWarnings("unchecked")
@Test
public void testReceivingBinaryCloudEventsUsingStringPayload() {
    String address = UUID.randomUUID().toString();
    new MapBasedConfig().with("mp.messaging.incoming.source.connector", AmqpConnector.CONNECTOR_NAME).with("mp.messaging.incoming.source.address", address).with("mp.messaging.incoming.source.host", host).with("mp.messaging.incoming.source.port", port).with("mp.messaging.incoming.source.tracing-enabled", false).with("amqp-username", username).with("amqp-password", password).write();
    weld.addBeanClass(MyStringConsumptionBean.class);
    container = weld.initialize();
    await().until(() -> isAmqpConnectorReady(container));
    MyStringConsumptionBean bean = container.getBeanManager().createInstance().select(MyStringConsumptionBean.class).get();
    usage.produce(address, 1, () -> {
        JsonObject json = new JsonObject().put(AmqpCloudEventHelper.AMQP_HEADER_FOR_SPEC_VERSION, CloudEventMetadata.CE_VERSION_1_0).put(AmqpCloudEventHelper.AMQP_HEADER_FOR_TYPE, "type").put(AmqpCloudEventHelper.AMQP_HEADER_FOR_ID, "id").put(AmqpCloudEventHelper.AMQP_HEADER_FOR_SOURCE, "test://test").put(AmqpCloudEventHelper.AMQP_HEADER_FOR_SUBJECT, "foo").put(AmqpCloudEventHelper.AMQP_HEADER_FOR_SCHEMA, "http://schema.io").put(AmqpCloudEventHelper.AMQP_HEADER_FOR_TIME, "2020-07-23T09:12:34Z").put(AmqpCloudEventHelper.CE_HEADER_PREFIX + "ext", "hello");
        return AmqpMessage.create().applicationProperties(json).contentType("text/plain").withBody("Hello World").build();
    });
    await().atMost(2, TimeUnit.MINUTES).until(() -> bean.getList().size() >= 1);
    Message<String> message = bean.getList().get(0);
    IncomingCloudEventMetadata<String> metadata = message.getMetadata(IncomingCloudEventMetadata.class).orElse(null);
    assertThat(metadata).isNotNull();
    assertThat(metadata.getSpecVersion()).isEqualTo(CloudEventMetadata.CE_VERSION_1_0);
    assertThat(metadata.getType()).isEqualTo("type");
    assertThat(metadata.getId()).isEqualTo("id");
    assertThat(metadata.getSource()).isEqualTo(URI.create("test://test"));
    assertThat(metadata.getSubject()).hasValue("foo");
    assertThat(metadata.getDataContentType()).hasValue("text/plain");
    assertThat(metadata.getDataSchema()).hasValue(URI.create("http://schema.io"));
    assertThat(metadata.getTimeStamp()).isNotEmpty();
    assertThat(metadata.getExtensions()).containsEntry("ext", "hello");
    assertThat(metadata.getData()).isEqualTo("Hello World");
    assertThat(message.getPayload()).isInstanceOf(String.class);
    assertThat(message.getPayload()).isEqualTo("Hello World");
}
Also used : JsonObject(io.vertx.core.json.JsonObject) IncomingCloudEventMetadata(io.smallrye.reactive.messaging.ce.IncomingCloudEventMetadata) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig) Test(org.junit.jupiter.api.Test)

Example 53 with MapBasedConfig

use of io.smallrye.reactive.messaging.test.common.config.MapBasedConfig in project smallrye-reactive-messaging by smallrye.

the class CloudEventConsumptionTest method testReceivingStructuredCloudEventsNoData.

@Test
public void testReceivingStructuredCloudEventsNoData() {
    String address = UUID.randomUUID().toString();
    new MapBasedConfig().with("mp.messaging.incoming.source.connector", AmqpConnector.CONNECTOR_NAME).with("mp.messaging.incoming.source.address", address).with("mp.messaging.incoming.source.host", host).with("mp.messaging.incoming.source.port", port).with("mp.messaging.incoming.source.tracing-enabled", false).with("amqp-username", username).with("amqp-password", password).write();
    weld.addBeanClass(MyConsumptionBean.class);
    container = weld.initialize();
    await().until(() -> isAmqpConnectorReady(container));
    MyConsumptionBean bean = container.getBeanManager().createInstance().select(MyConsumptionBean.class).get();
    usage.produce(address, 1, () -> {
        JsonObject json = new JsonObject().put("specversion", CloudEventMetadata.CE_VERSION_1_0).put("type", "type").put("id", "id").put("source", "test://test").put("subject", "foo").put("datacontenttype", "application/json").put("dataschema", "http://schema.io").put("time", "2020-07-23T09:12:34Z");
        return AmqpMessage.create().withJsonObjectAsBody(json).contentType(AmqpCloudEventHelper.STRUCTURED_CONTENT_TYPE).build();
    });
    await().atMost(2, TimeUnit.MINUTES).until(() -> bean.getList().size() >= 1);
    Message<JsonObject> message = bean.getList().get(0);
    IncomingCloudEventMetadata<JsonObject> metadata = message.getMetadata(IncomingCloudEventMetadata.class).orElse(null);
    assertThat(metadata).isNotNull();
    assertThat(metadata.getSpecVersion()).isEqualTo(CloudEventMetadata.CE_VERSION_1_0);
    assertThat(metadata.getType()).isEqualTo("type");
    assertThat(metadata.getId()).isEqualTo("id");
    assertThat(metadata.getSource()).isEqualTo(URI.create("test://test"));
    assertThat(metadata.getData()).isNull();
    assertThat(message.getPayload()).isNull();
}
Also used : JsonObject(io.vertx.core.json.JsonObject) IncomingCloudEventMetadata(io.smallrye.reactive.messaging.ce.IncomingCloudEventMetadata) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig) Test(org.junit.jupiter.api.Test)

Example 54 with MapBasedConfig

use of io.smallrye.reactive.messaging.test.common.config.MapBasedConfig in project smallrye-reactive-messaging by smallrye.

the class AmqpSinkTest method testABeanProducingMessagesSentToAMQPWithOutboundMetadata.

@Test
@Timeout(30)
public void testABeanProducingMessagesSentToAMQPWithOutboundMetadata() throws Exception {
    int msgCount = 10;
    CountDownLatch msgsReceived = new CountDownLatch(msgCount);
    List<org.apache.qpid.proton.message.Message> messagesReceived = Collections.synchronizedList(new ArrayList<>(msgCount));
    AtomicReference<String> attachAddress = new AtomicReference<>("non-null-initialisation-value");
    server = setupMockServerForTypeTest(messagesReceived, msgsReceived, attachAddress);
    Weld weld = new Weld();
    weld.addBeanClass(ProducingBeanUsingOutboundMetadata.class);
    new MapBasedConfig().with("mp.messaging.outgoing.sink.address", "not-used").with("mp.messaging.outgoing.sink.connector", AmqpConnector.CONNECTOR_NAME).with("mp.messaging.outgoing.sink.host", "localhost").with("mp.messaging.outgoing.sink.port", server.actualPort()).with("mp.messaging.outgoing.sink.tracing-enabled", false).write();
    container = weld.initialize();
    assertThat(msgsReceived.await(6, TimeUnit.SECONDS)).isTrue();
    List<Object> payloadsReceived = new ArrayList<>(msgCount);
    messagesReceived.forEach(msg -> {
        // Set in ProducingBeanUsingOutboundMetadata
        assertThat(msg.getAddress()).isEqualTo("metadata-address");
        assertThat(msg.getSubject()).isEqualTo("metadata-subject");
        Section body = msg.getBody();
        assertThat(body).isInstanceOf(AmqpValue.class);
        payloadsReceived.add(((AmqpValue) body).getValue());
    });
    assertThat(payloadsReceived).containsExactly(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    // Should have used an anonymous sender link, verify null target address
    assertThat(attachAddress.get()).isNull();
}
Also used : Message(org.eclipse.microprofile.reactive.messaging.Message) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Section(org.apache.qpid.proton.amqp.messaging.Section) Weld(org.jboss.weld.environment.se.Weld) JsonObject(io.vertx.core.json.JsonObject) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 55 with MapBasedConfig

use of io.smallrye.reactive.messaging.test.common.config.MapBasedConfig in project smallrye-reactive-messaging by smallrye.

the class AmqpSinkTest method testABeanProducingMessagesSentToAMQPWithOutboundMetadataUsingNonAnonymousSender.

@Test
@Timeout(30)
public void testABeanProducingMessagesSentToAMQPWithOutboundMetadataUsingNonAnonymousSender() throws Exception {
    int msgCount = 10;
    String address = "sink-foo";
    CountDownLatch msgsReceived = new CountDownLatch(msgCount);
    List<org.apache.qpid.proton.message.Message> messagesReceived = Collections.synchronizedList(new ArrayList<>(msgCount));
    AtomicReference<String> attachAddress = new AtomicReference<>("non-null-initialisation-value");
    server = setupMockServerForTypeTest(messagesReceived, msgsReceived, attachAddress);
    Weld weld = new Weld();
    weld.addBeanClass(ProducingBeanUsingOutboundMetadata.class);
    new MapBasedConfig().with("mp.messaging.outgoing.sink.address", address).with("mp.messaging.outgoing.sink.connector", AmqpConnector.CONNECTOR_NAME).with("mp.messaging.outgoing.sink.host", "localhost").with("mp.messaging.outgoing.sink.port", server.actualPort()).with("mp.messaging.outgoing.sink.use-anonymous-sender", false).with("mp.messaging.outgoing.sink.tracing-enabled", false).write();
    container = weld.initialize();
    assertThat(msgsReceived.await(6, TimeUnit.SECONDS)).isTrue();
    List<Object> payloadsReceived = new ArrayList<>(msgCount);
    messagesReceived.forEach(msg -> {
        assertThat(msg.getAddress()).isEqualTo(// Matches the one from the config, not metadata, due to not using an anonymous sender
        address);
        assertThat(msg.getSubject()).isEqualTo("metadata-subject");
        Section body = msg.getBody();
        assertThat(body).isInstanceOf(AmqpValue.class);
        payloadsReceived.add(((AmqpValue) body).getValue());
    });
    assertThat(payloadsReceived).containsExactly(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    // Should have used an fixed-address sender link, verify target address
    assertThat(attachAddress.get()).isEqualTo(address);
}
Also used : Message(org.eclipse.microprofile.reactive.messaging.Message) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Section(org.apache.qpid.proton.amqp.messaging.Section) Weld(org.jboss.weld.environment.se.Weld) JsonObject(io.vertx.core.json.JsonObject) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Aggregations

MapBasedConfig (io.smallrye.reactive.messaging.test.common.config.MapBasedConfig)272 Test (org.junit.jupiter.api.Test)223 Message (org.eclipse.microprofile.reactive.messaging.Message)69 JsonObject (io.vertx.core.json.JsonObject)63 ArrayList (java.util.ArrayList)61 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)57 TopicPartition (org.apache.kafka.common.TopicPartition)43 Awaitility.await (org.awaitility.Awaitility.await)38 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)37 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)34 Weld (org.jboss.weld.environment.se.Weld)32 KafkaMapBasedConfig (io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig)31 HashMap (java.util.HashMap)29 AfterEach (org.junit.jupiter.api.AfterEach)29 Collectors (java.util.stream.Collectors)28 HealthReport (io.smallrye.reactive.messaging.health.HealthReport)26 KafkaSource (io.smallrye.reactive.messaging.kafka.impl.KafkaSource)26 IntegerDeserializer (org.apache.kafka.common.serialization.IntegerDeserializer)25 ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)23 Duration (java.time.Duration)22