Search in sources :

Example 21 with KafkaSink

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

the class KafkaSinkWithCloudEventsTest method testWithCloudEventDisabled.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testWithCloudEventDisabled() {
    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", false);
    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().lastHeader("ce_specversion")).isNull();
    assertThat(record.headers().lastHeader("ce_id")).isNull();
    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) KafkaMapBasedConfig(io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig) Test(org.junit.jupiter.api.Test)

Example 22 with KafkaSink

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

the class KafkaSinkWithCloudEventsTest method testSendingStructuredCloudEvents.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testSendingStructuredCloudEvents() {
    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);
    Message<?> message = Message.of("hello").addMetadata(OutgoingCloudEventMetadata.builder().withSource(URI.create("test://test")).withType("type").withId("some id").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("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 23 with KafkaSink

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

the class KafkaSinkWithCloudEventsTest method testSendingBinaryCloudEvents.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testSendingBinaryCloudEvents() {
    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());
    ConsumerTask<String, String> records = companion.consumeStrings().fromTopics(topic);
    Message<?> message = Message.of("hello").addMetadata(OutgoingCloudEventMetadata.builder().withSource(URI.create("test://test")).withType("type").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()).isNull();
    assertThat(record.headers()).contains(new RecordHeader("ce_specversion", "1.0".getBytes()), new RecordHeader("ce_type", "type".getBytes()), new RecordHeader("ce_source", "test://test".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 24 with KafkaSink

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

the class KafkaSinkWithCloudEventsTest method testSendingStructuredCloudEventsMissingMandatoryAttribute.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testSendingStructuredCloudEventsMissingMandatoryAttribute() {
    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());
    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 25 with KafkaSink

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

the class KafkaSinkWithCloudEventsTest method testSendingStructuredCloudEventsWithKey.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testSendingStructuredCloudEventsWithKey() {
    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);
    Message<?> message = Message.of("hello").addMetadata(OutgoingCloudEventMetadata.builder().withSource(URI.create("test://test")).withType("type").withId("some id").withExtension("partitionkey", "my-key").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()).isEqualTo("my-key");
    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");
    // Rule 3.1 - partitionkey must be kept
    assertThat(json.getString("partitionkey")).isEqualTo("my-key");
    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)

Aggregations

KafkaSink (io.smallrye.reactive.messaging.kafka.impl.KafkaSink)34 Test (org.junit.jupiter.api.Test)32 KafkaMapBasedConfig (io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig)27 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)20 KafkaConnectorOutgoingConfiguration (io.smallrye.reactive.messaging.kafka.KafkaConnectorOutgoingConfiguration)18 Message (org.eclipse.microprofile.reactive.messaging.Message)18 MapBasedConfig (io.smallrye.reactive.messaging.test.common.config.MapBasedConfig)16 RecordHeader (org.apache.kafka.common.header.internals.RecordHeader)13 JsonObject (io.vertx.core.json.JsonObject)9 HealthReport (io.smallrye.reactive.messaging.health.HealthReport)4 IntegerSerializer (org.apache.kafka.common.serialization.IntegerSerializer)4 Subscriber (org.reactivestreams.Subscriber)4 Multi (io.smallrye.mutiny.Multi)2 Uni (io.smallrye.mutiny.Uni)2 KafkaCompanionTestBase (io.smallrye.reactive.messaging.kafka.base.KafkaCompanionTestBase)2 UnsatisfiedInstance (io.smallrye.reactive.messaging.kafka.base.UnsatisfiedInstance)2 ConsumerTask (io.smallrye.reactive.messaging.kafka.companion.ConsumerTask)2 KafkaCompanion (io.smallrye.reactive.messaging.kafka.companion.KafkaCompanion)2 JsonObjectSerializer (io.vertx.kafka.client.serialization.JsonObjectSerializer)2 Duration (java.time.Duration)2