Search in sources :

Example 1 with KafkaConnectorOutgoingConfiguration

use of io.smallrye.reactive.messaging.kafka.KafkaConnectorOutgoingConfiguration in project smallrye-reactive-messaging by smallrye.

the class KafkaSinkWithCloudEventsTest method testSendingStructuredCloudEventsWithConfiguredTypeAndSourceAndNoCloudEventMetadata.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testSendingStructuredCloudEventsWithConfiguredTypeAndSourceAndNoCloudEventMetadata() {
    KafkaMapBasedConfig config = newCommonConfig();
    config.put("topic", topic);
    config.put("value.serializer", StringSerializer.class.getName());
    config.put("channel-name", topic);
    config.put("cloud-events-mode", "structured");
    config.put("cloud-events-type", "my type");
    config.put("cloud-events-source", "http://acme.org");
    KafkaConnectorOutgoingConfiguration oc = new KafkaConnectorOutgoingConfiguration(config);
    sink = new KafkaSink(oc, CountKafkaCdiEvents.noCdiEvents, UnsatisfiedInstance.instance());
    ConsumerTask<String, String> records = companion.consumeStrings().fromTopics(topic);
    Message<?> message = Message.of("hello!");
    Multi.createFrom().<Message<?>>item(message).subscribe().withSubscriber((Subscriber) sink.getSink().build());
    await().until(() -> records.getRecords().size() == 1);
    ConsumerRecord<String, String> record = records.getRecords().get(0);
    assertThat(record.topic()).isEqualTo(topic);
    assertThat(record.key()).isNull();
    assertThat(record.headers()).contains(new RecordHeader("content-type", "application/cloudevents+json; charset=UTF-8".getBytes()));
    JsonObject json = new JsonObject(record.value());
    assertThat(json.getString("specversion")).isEqualTo("1.0");
    assertThat(json.getString("type")).isEqualTo("my type");
    assertThat(json.getString("source")).isEqualTo("http://acme.org");
    assertThat(json.getString("id")).isNotNull();
    assertThat(json.getString("data")).isEqualTo("hello!");
}
Also used : Message(org.eclipse.microprofile.reactive.messaging.Message) KafkaSink(io.smallrye.reactive.messaging.kafka.impl.KafkaSink) JsonObject(io.vertx.core.json.JsonObject) KafkaConnectorOutgoingConfiguration(io.smallrye.reactive.messaging.kafka.KafkaConnectorOutgoingConfiguration) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) RecordHeader(org.apache.kafka.common.header.internals.RecordHeader) KafkaMapBasedConfig(io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig) Test(org.junit.jupiter.api.Test)

Example 2 with KafkaConnectorOutgoingConfiguration

use of io.smallrye.reactive.messaging.kafka.KafkaConnectorOutgoingConfiguration in project smallrye-reactive-messaging by smallrye.

the class KafkaSinkWithCloudEventsTest method testSendingBinaryCloudEventsWithConfiguredTypeAndSource.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testSendingBinaryCloudEventsWithConfiguredTypeAndSource() {
    KafkaMapBasedConfig config = newCommonConfig();
    config.put("topic", topic);
    config.put("value.serializer", StringSerializer.class.getName());
    config.put("channel-name", topic);
    config.put("key", "my-key");
    config.put("cloud-events-type", "my type");
    config.put("cloud-events-source", "http://acme.org");
    KafkaConnectorOutgoingConfiguration oc = new KafkaConnectorOutgoingConfiguration(config);
    sink = new KafkaSink(oc, CountKafkaCdiEvents.noCdiEvents, UnsatisfiedInstance.instance());
    ConsumerTask<String, String> records = companion.consumeStrings().fromTopics(topic);
    Message<?> message = Message.of("hello!").addMetadata(OutgoingCloudEventMetadata.builder().withId("some id").build());
    Multi.createFrom().<Message<?>>item(message).subscribe().withSubscriber((Subscriber) sink.getSink().build());
    await().until(() -> records.getRecords().size() == 1);
    ConsumerRecord<String, String> record = records.getRecords().get(0);
    assertThat(record.topic()).isEqualTo(topic);
    assertThat(record.key()).isEqualTo("my-key");
    assertThat(record.headers()).contains(new RecordHeader("ce_specversion", "1.0".getBytes()), new RecordHeader("ce_type", "my type".getBytes()), new RecordHeader("ce_source", "http://acme.org".getBytes()), new RecordHeader("ce_id", "some id".getBytes()));
    assertThat(record.value()).isEqualTo("hello!");
}
Also used : Message(org.eclipse.microprofile.reactive.messaging.Message) KafkaSink(io.smallrye.reactive.messaging.kafka.impl.KafkaSink) KafkaConnectorOutgoingConfiguration(io.smallrye.reactive.messaging.kafka.KafkaConnectorOutgoingConfiguration) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) RecordHeader(org.apache.kafka.common.header.internals.RecordHeader) KafkaMapBasedConfig(io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig) Test(org.junit.jupiter.api.Test)

Example 3 with KafkaConnectorOutgoingConfiguration

use of io.smallrye.reactive.messaging.kafka.KafkaConnectorOutgoingConfiguration in project smallrye-reactive-messaging by smallrye.

the class KafkaSinkWithCloudEventsTest method testSendingBinaryCloudEventsWithConfiguredTypeAndSourceButNoMetadata.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testSendingBinaryCloudEventsWithConfiguredTypeAndSourceButNoMetadata() {
    KafkaMapBasedConfig config = newCommonConfig();
    config.put("topic", topic);
    config.put("value.serializer", StringSerializer.class.getName());
    config.put("channel-name", topic);
    config.put("key", "my-key");
    config.put("cloud-events-type", "my type");
    config.put("cloud-events-source", "http://acme.org");
    KafkaConnectorOutgoingConfiguration oc = new KafkaConnectorOutgoingConfiguration(config);
    sink = new KafkaSink(oc, CountKafkaCdiEvents.noCdiEvents, UnsatisfiedInstance.instance());
    ConsumerTask<String, String> records = companion.consumeStrings().fromTopics(topic);
    Message<?> message = Message.of("hello!");
    Multi.createFrom().<Message<?>>item(message).subscribe().withSubscriber((Subscriber) sink.getSink().build());
    await().until(() -> records.getRecords().size() == 1);
    ConsumerRecord<String, String> record = records.getRecords().get(0);
    assertThat(record.topic()).isEqualTo(topic);
    assertThat(record.key()).isEqualTo("my-key");
    assertThat(record.headers()).contains(new RecordHeader("ce_specversion", "1.0".getBytes()), new RecordHeader("ce_type", "my type".getBytes()), new RecordHeader("ce_source", "http://acme.org".getBytes()));
    assertThat(record.headers().lastHeader("ce_id")).isNotNull();
    assertThat(record.value()).isEqualTo("hello!");
}
Also used : Message(org.eclipse.microprofile.reactive.messaging.Message) KafkaSink(io.smallrye.reactive.messaging.kafka.impl.KafkaSink) KafkaConnectorOutgoingConfiguration(io.smallrye.reactive.messaging.kafka.KafkaConnectorOutgoingConfiguration) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) RecordHeader(org.apache.kafka.common.header.internals.RecordHeader) KafkaMapBasedConfig(io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig) Test(org.junit.jupiter.api.Test)

Example 4 with KafkaConnectorOutgoingConfiguration

use of io.smallrye.reactive.messaging.kafka.KafkaConnectorOutgoingConfiguration in project smallrye-reactive-messaging by smallrye.

the class KafkaSinkWithCloudEventsTest method testSendingBinaryCloudEventsMissingMandatoryAttribute.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testSendingBinaryCloudEventsMissingMandatoryAttribute() {
    KafkaMapBasedConfig config = newCommonConfig();
    config.put("topic", topic);
    config.put("value.serializer", StringSerializer.class.getName());
    config.put("channel-name", topic);
    KafkaConnectorOutgoingConfiguration oc = new KafkaConnectorOutgoingConfiguration(config);
    sink = new KafkaSink(oc, CountKafkaCdiEvents.noCdiEvents, UnsatisfiedInstance.instance());
    Message<?> message = Message.of("hello").addMetadata(OutgoingCloudEventMetadata.builder().withSource(URI.create("test://test")).withId("some id").build());
    await().until(() -> {
        HealthReport.HealthReportBuilder builder = HealthReport.builder();
        sink.isAlive(builder);
        return builder.build().isOk();
    });
    Multi.createFrom().<Message<?>>item(message).subscribe().withSubscriber((Subscriber) sink.getSink().build());
    await().until(() -> {
        HealthReport.HealthReportBuilder builder = HealthReport.builder();
        sink.isAlive(builder);
        return !builder.build().isOk();
    });
}
Also used : Message(org.eclipse.microprofile.reactive.messaging.Message) HealthReport(io.smallrye.reactive.messaging.health.HealthReport) KafkaSink(io.smallrye.reactive.messaging.kafka.impl.KafkaSink) KafkaConnectorOutgoingConfiguration(io.smallrye.reactive.messaging.kafka.KafkaConnectorOutgoingConfiguration) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) KafkaMapBasedConfig(io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig) Test(org.junit.jupiter.api.Test)

Example 5 with KafkaConnectorOutgoingConfiguration

use of io.smallrye.reactive.messaging.kafka.KafkaConnectorOutgoingConfiguration in project smallrye-reactive-messaging by smallrye.

the class KafkaSinkWithCloudEventsTest method testSendingStructuredCloudEventsWithTimestampAndSubject.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testSendingStructuredCloudEventsWithTimestampAndSubject() {
    KafkaMapBasedConfig config = newCommonConfig();
    config.put("topic", topic);
    config.put("value.serializer", StringSerializer.class.getName());
    config.put("channel-name", topic);
    config.put("cloud-events-mode", "structured");
    KafkaConnectorOutgoingConfiguration oc = new KafkaConnectorOutgoingConfiguration(config);
    sink = new KafkaSink(oc, CountKafkaCdiEvents.noCdiEvents, UnsatisfiedInstance.instance());
    ConsumerTask<String, String> records = companion.consumeStrings().fromTopics(topic);
    ZonedDateTime time = ZonedDateTime.now();
    Message<?> message = Message.of("").addMetadata(OutgoingCloudEventMetadata.builder().withSource(URI.create("test://test")).withType("type").withId("some id").withSubject("subject").withTimestamp(time).build());
    Multi.createFrom().<Message<?>>item(message).subscribe().withSubscriber((Subscriber) sink.getSink().build());
    await().until(() -> records.count() == 1);
    ConsumerRecord<String, String> record = records.getRecords().get(0);
    assertThat(record.topic()).isEqualTo(topic);
    assertThat(record.key()).isNull();
    assertThat(record.headers()).contains(new RecordHeader("content-type", "application/cloudevents+json; charset=UTF-8".getBytes()));
    JsonObject json = new JsonObject(record.value());
    assertThat(json.getString("specversion")).isEqualTo("1.0");
    assertThat(json.getString("type")).isEqualTo("type");
    assertThat(json.getString("source")).isEqualTo("test://test");
    assertThat(json.getString("id")).isEqualTo("some id");
    assertThat(json.getString("subject")).isEqualTo("subject");
    assertThat(json.getInstant("time")).isNotNull();
    assertThat(json.getInstant("time").getEpochSecond()).isEqualTo(time.toEpochSecond());
}
Also used : Message(org.eclipse.microprofile.reactive.messaging.Message) ZonedDateTime(java.time.ZonedDateTime) KafkaSink(io.smallrye.reactive.messaging.kafka.impl.KafkaSink) JsonObject(io.vertx.core.json.JsonObject) KafkaConnectorOutgoingConfiguration(io.smallrye.reactive.messaging.kafka.KafkaConnectorOutgoingConfiguration) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) RecordHeader(org.apache.kafka.common.header.internals.RecordHeader) KafkaMapBasedConfig(io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig) Test(org.junit.jupiter.api.Test)

Aggregations

KafkaConnectorOutgoingConfiguration (io.smallrye.reactive.messaging.kafka.KafkaConnectorOutgoingConfiguration)18 KafkaSink (io.smallrye.reactive.messaging.kafka.impl.KafkaSink)18 KafkaMapBasedConfig (io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig)17 Test (org.junit.jupiter.api.Test)17 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)16 Message (org.eclipse.microprofile.reactive.messaging.Message)16 RecordHeader (org.apache.kafka.common.header.internals.RecordHeader)13 JsonObject (io.vertx.core.json.JsonObject)7 HealthReport (io.smallrye.reactive.messaging.health.HealthReport)2 MapBasedConfig (io.smallrye.reactive.messaging.test.common.config.MapBasedConfig)1 ZonedDateTime (java.time.ZonedDateTime)1 DoubleSerializer (org.apache.kafka.common.serialization.DoubleSerializer)1