Search in sources :

Example 46 with MapBasedConfig

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

the class TracingAmqpToAppWithParentTest method testFromAmqpToAppWithParentSpan.

@Test
public void testFromAmqpToAppWithParentSpan() {
    weld.addBeanClass(MyAppReceivingData.class);
    new MapBasedConfig().put("mp.messaging.incoming.stuff.connector", AmqpConnector.CONNECTOR_NAME).put("mp.messaging.incoming.stuff.host", host).put("mp.messaging.incoming.stuff.port", port).put("amqp-username", username).put("amqp-password", password).write();
    container = weld.initialize();
    MyAppReceivingData bean = container.getBeanManager().createInstance().select(MyAppReceivingData.class).get();
    await().until(() -> isAmqpConnectorReady(container));
    AtomicInteger count = new AtomicInteger();
    List<SpanContext> producedSpanContexts = new CopyOnWriteArrayList<>();
    usage.produce("stuff", 10, () -> AmqpMessage.create().durable(false).ttl(10000).withIntegerAsBody(count.getAndIncrement()).applicationProperties(createTracingSpan(producedSpanContexts, "stuff")).build());
    await().until(() -> bean.list().size() >= 10);
    assertThat(bean.list()).containsExactly(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
    List<String> producedTraceIds = producedSpanContexts.stream().map(SpanContext::getTraceId).collect(Collectors.toList());
    assertThat(producedTraceIds).hasSize(10);
    assertThat(bean.tracing()).hasSizeGreaterThanOrEqualTo(10);
    assertThat(bean.tracing()).doesNotContainNull().doesNotHaveDuplicates();
    List<String> receivedTraceIds = bean.tracing().stream().map(tracingMetadata -> Span.fromContext(tracingMetadata.getCurrentContext()).getSpanContext().getTraceId()).collect(Collectors.toList());
    assertThat(receivedTraceIds).doesNotContainNull().doesNotHaveDuplicates().hasSize(10);
    assertThat(receivedTraceIds).containsExactlyInAnyOrderElementsOf(producedTraceIds);
    List<String> spanIds = new ArrayList<>();
    for (TracingMetadata tracing : bean.tracing()) {
        spanIds.add(Span.fromContext(tracing.getCurrentContext()).getSpanContext().getSpanId());
        assertThat(tracing.getPreviousContext()).isNotNull();
        Span previousSpan = Span.fromContextOrNull(tracing.getPreviousContext());
        assertThat(previousSpan).isNotNull();
        assertThat(previousSpan.getSpanContext().getTraceId()).isEqualTo(Span.fromContext(tracing.getCurrentContext()).getSpanContext().getTraceId());
        assertThat(previousSpan.getSpanContext().getSpanId()).isNotEqualTo(Span.fromContext(tracing.getCurrentContext()).getSpanContext().getSpanId());
    }
    assertThat(spanIds).doesNotContainNull().doesNotHaveDuplicates().hasSizeGreaterThanOrEqualTo(10);
    List<String> parentIds = bean.tracing().stream().map(tracingMetadata -> Span.fromContextOrNull(tracingMetadata.getPreviousContext()).getSpanContext().getSpanId()).collect(Collectors.toList());
    assertThat(producedSpanContexts.stream().map(SpanContext::getSpanId)).containsExactlyElementsOf(parentIds);
    for (SpanData data : testExporter.getFinishedSpanItems()) {
        assertThat(data.getSpanId()).isIn(spanIds);
        assertThat(data.getSpanId()).isNotEqualTo(data.getParentSpanId());
        assertThat(data.getKind()).isEqualByComparingTo(SpanKind.CONSUMER);
        assertThat(data.getParentSpanId()).isNotNull();
        assertThat(data.getParentSpanId()).isIn(parentIds);
    }
}
Also used : Sampler(io.opentelemetry.sdk.trace.samplers.Sampler) BeforeEach(org.junit.jupiter.api.BeforeEach) W3CTraceContextPropagator(io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator) OpenTelemetrySdk(io.opentelemetry.sdk.OpenTelemetrySdk) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig) ArrayList(java.util.ArrayList) AfterAll(org.junit.jupiter.api.AfterAll) SimpleSpanProcessor(io.opentelemetry.sdk.trace.export.SimpleSpanProcessor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContextPropagators(io.opentelemetry.context.propagation.ContextPropagators) SmallRyeConfigProviderResolver(io.smallrye.config.SmallRyeConfigProviderResolver) JsonObject(io.vertx.core.json.JsonObject) Weld(org.jboss.weld.environment.se.Weld) AmqpMessage(io.vertx.mutiny.amqp.AmqpMessage) Context(io.opentelemetry.context.Context) Awaitility.await(org.awaitility.Awaitility.await) Properties(java.util.Properties) Span(io.opentelemetry.api.trace.Span) SpanContext(io.opentelemetry.api.trace.SpanContext) TracingMetadata(io.smallrye.reactive.messaging.TracingMetadata) WeldContainer(org.jboss.weld.environment.se.WeldContainer) Collectors(java.util.stream.Collectors) SpanKind(io.opentelemetry.api.trace.SpanKind) StandardCharsets(java.nio.charset.StandardCharsets) SdkTracerProvider(io.opentelemetry.sdk.trace.SdkTracerProvider) GlobalOpenTelemetry(io.opentelemetry.api.GlobalOpenTelemetry) Test(org.junit.jupiter.api.Test) List(java.util.List) Message(org.eclipse.microprofile.reactive.messaging.Message) AfterEach(org.junit.jupiter.api.AfterEach) CompletionStage(java.util.concurrent.CompletionStage) InMemorySpanExporter(io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter) ConfigProvider(org.eclipse.microprofile.config.ConfigProvider) ApplicationScoped(javax.enterprise.context.ApplicationScoped) SpanData(io.opentelemetry.sdk.trace.data.SpanData) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Incoming(org.eclipse.microprofile.reactive.messaging.Incoming) SpanProcessor(io.opentelemetry.sdk.trace.SpanProcessor) SpanContext(io.opentelemetry.api.trace.SpanContext) SpanData(io.opentelemetry.sdk.trace.data.SpanData) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Span(io.opentelemetry.api.trace.Span) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TracingMetadata(io.smallrye.reactive.messaging.TracingMetadata) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.jupiter.api.Test)

Example 47 with MapBasedConfig

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

the class CloudEventConsumptionTest method testReceivingStructuredCloudEventsWithStringPayload.

@SuppressWarnings("unchecked")
@Test
public void testReceivingStructuredCloudEventsWithStringPayload() {
    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").put("data", new JsonObject().put("name", "neo"));
        return AmqpMessage.create().withBody(json.encode()).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.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");
}
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 48 with MapBasedConfig

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

the class CloudEventConsumptionTest method testReceivingStructuredCloudEventsWithBinaryPayload.

@SuppressWarnings("unchecked")
@Test
public void testReceivingStructuredCloudEventsWithBinaryPayload() {
    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").put("data", new JsonObject().put("name", "neo"));
        return AmqpMessage.create().withBufferAsBody(Buffer.buffer(json.toBuffer().getBytes())).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.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");
}
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 49 with MapBasedConfig

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

the class CloudEventConsumptionTest method testReceivingStructuredCloudEventsWithoutMatchingContentTypeIsNotReadACloudEvent.

@SuppressWarnings("unchecked")
@Test
public void testReceivingStructuredCloudEventsWithoutMatchingContentTypeIsNotReadACloudEvent() {
    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("data", new JsonObject().put("name", "neo"));
        return AmqpMessage.create().withJsonObjectAsBody(// Set the content type to application/json
        json).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).isNull();
    assertThat(message.getPayload().getString("id")).isEqualTo("id");
}
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 50 with MapBasedConfig

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

the class CloudEventConsumptionTest method testReceivingStructuredCloudEventsWithoutSource.

@Test
public void testReceivingStructuredCloudEventsWithoutSource() {
    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("subject", "foo").put("data", new JsonObject().put("name", "neo"));
        return AmqpMessage.create().withJsonObjectAsBody(json).contentType(AmqpCloudEventHelper.STRUCTURED_CONTENT_TYPE).build();
    });
    await().pollDelay(Duration.ofSeconds(1)).atMost(2, TimeUnit.MINUTES).until(() -> bean.getList().size() == 0);
// Nothing has been received because the deserializer is not supported.
}
Also used : JsonObject(io.vertx.core.json.JsonObject) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig) Test(org.junit.jupiter.api.Test)

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