Search in sources :

Example 36 with KafkaMapBasedConfig

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

the class ConsumerRecordConverterTest method testChannelBeanUsingConverter.

@Test
public void testChannelBeanUsingConverter() {
    KafkaMapBasedConfig builder = kafkaConfig("mp.messaging.incoming.data");
    builder.put("value.deserializer", StringDeserializer.class.getName());
    builder.put("auto.offset.reset", "earliest");
    builder.put("topic", topic);
    addBeans(ConsumerRecordConverter.class, RecordConverter.class);
    MyChannelBean bean = runApplication(builder, MyChannelBean.class);
    bean.consume();
    companion.produceStrings().usingGenerator(i -> new ProducerRecord<>(topic, i % 2 == 0 ? "key" : "k", "v-" + i), 10);
    await().until(() -> bean.list().size() == 10);
    assertThat(bean.list()).hasSize(10).allSatisfy(r -> {
        assertThat(r.value()).startsWith("v-");
        assertThat(r.key()).startsWith("k");
        if (!r.key().equalsIgnoreCase("key")) {
            assertThat(r.key()).isEqualTo("k");
        }
    });
}
Also used : StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) KafkaMapBasedConfig(io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig) Test(org.junit.jupiter.api.Test)

Example 37 with KafkaMapBasedConfig

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

the class ConsumerRecordsConverterTest method testBeanUsingConverter.

@Test
public void testBeanUsingConverter() {
    KafkaMapBasedConfig builder = kafkaConfig("mp.messaging.incoming.data");
    builder.put("value.deserializer", StringDeserializer.class.getName());
    builder.put("auto.offset.reset", "earliest");
    builder.put("topic", topic);
    builder.put("batch", true);
    addBeans(ConsumerRecordsConverter.class);
    MyBean bean = runApplication(builder, MyBean.class);
    companion.produceStrings().usingGenerator(i -> new ProducerRecord<>(topic, i % 2 == 0 ? "key" : "k", "v-" + i), 10);
    await().until(() -> bean.list().stream().mapToInt(ConsumerRecords::count).sum() == 10);
    List<ConsumerRecord<String, String>> consumerRecords = bean.list().stream().flatMap(r -> StreamSupport.stream(r.records(topic).spliterator(), false)).collect(Collectors.toList());
    assertThat(consumerRecords).hasSize(10).allSatisfy(r -> {
        assertThat(r.value()).startsWith("v-");
        assertThat(r.key()).startsWith("k");
        if (!r.key().equalsIgnoreCase("key")) {
            assertThat(r.key()).isEqualTo("k");
        }
    });
}
Also used : Awaitility.await(org.awaitility.Awaitility.await) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) KafkaCompanionTestBase(io.smallrye.reactive.messaging.kafka.base.KafkaCompanionTestBase) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) KafkaMapBasedConfig(io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig) ConsumerRecords(org.apache.kafka.clients.consumer.ConsumerRecords) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) List(java.util.List) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) StreamSupport(java.util.stream.StreamSupport) ApplicationScoped(javax.enterprise.context.ApplicationScoped) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Incoming(org.eclipse.microprofile.reactive.messaging.Incoming) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) KafkaMapBasedConfig(io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig) Test(org.junit.jupiter.api.Test)

Example 38 with KafkaMapBasedConfig

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

the class CustomConverterTest method testBeanUsingCustomConverter.

@Test
public void testBeanUsingCustomConverter() {
    KafkaMapBasedConfig builder = kafkaConfig("mp.messaging.incoming.data");
    builder.put("value.deserializer", StringDeserializer.class.getName());
    builder.put("auto.offset.reset", "earliest");
    builder.put("topic", topic);
    addBeans(ConsumerRecordConverter.class, RecordConverter.class, MyConverter.class);
    MyBean bean = runApplication(builder, MyBean.class);
    companion.produceStrings().usingGenerator(i -> new ProducerRecord<>(topic, i % 2 == 0 ? "key" : "k", "v-" + i), 10);
    await().until(() -> bean.list().size() == 10);
    assertThat(bean.list()).hasSize(10).allSatisfy(r -> {
        assertThat(r.getItem1()).isEqualTo(topic);
        assertThat(r.getItem3()).startsWith("v-");
        assertThat(r.getItem2()).startsWith("k");
        if (!r.getItem2().equalsIgnoreCase("key")) {
            assertThat(r.getItem2()).isEqualTo("k");
        }
    });
}
Also used : StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) KafkaMapBasedConfig(io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig) Test(org.junit.jupiter.api.Test)

Example 39 with KafkaMapBasedConfig

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

the class KafkaSourceWithCloudEventsTest method testReceivingStructuredCloudEventsWithByteArrayDeserializer.

@SuppressWarnings("unchecked")
@Test
public void testReceivingStructuredCloudEventsWithByteArrayDeserializer() {
    KafkaMapBasedConfig config = newCommonConfig();
    config.put("topic", topic);
    config.put("value.deserializer", ByteArrayDeserializer.class.getName());
    config.put("channel-name", topic);
    KafkaConnectorIncomingConfiguration ic = new KafkaConnectorIncomingConfiguration(config);
    source = new KafkaSource<>(vertx, UUID.randomUUID().toString(), ic, UnsatisfiedInstance.instance(), CountKafkaCdiEvents.noCdiEvents, UnsatisfiedInstance.instance(), -1);
    List<Message<?>> messages = new ArrayList<>();
    source.getStream().subscribe().with(messages::add);
    companion.produce(String.class, JsonObject.class).fromRecords(new ProducerRecord<>(topic, null, null, "key", new JsonObject().put("specversion", CloudEventMetadata.CE_VERSION_1_0).put("type", "type").put("id", "id").put("source", "test://test").put("subject", "foo").put("data", new JsonObject().put("name", "neo")), Collections.singletonList(new RecordHeader("content-type", "application/cloudevents+json; charset=utf-8".getBytes()))));
    await().atMost(2, TimeUnit.MINUTES).until(() -> messages.size() >= 1);
    Message<?> message = messages.get(0);
    IncomingKafkaCloudEventMetadata<String, JsonObject> metadata = message.getMetadata(IncomingKafkaCloudEventMetadata.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.getData().getString("name")).isEqualTo("neo");
    // Extensions
    assertThat(metadata.getKey()).isEqualTo("key");
    // Rule 3.1 - partitionkey attribute
    assertThat(metadata.<String>getExtension("partitionkey")).hasValue("key");
    assertThat(metadata.getTopic()).isEqualTo(topic);
    assertThat(message.getPayload()).isInstanceOf(JsonObject.class);
    assertThat(((JsonObject) message.getPayload()).getString("name")).isEqualTo("neo");
}
Also used : Message(org.eclipse.microprofile.reactive.messaging.Message) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) JsonObject(io.vertx.core.json.JsonObject) IncomingKafkaCloudEventMetadata(io.smallrye.reactive.messaging.kafka.IncomingKafkaCloudEventMetadata) KafkaConnectorIncomingConfiguration(io.smallrye.reactive.messaging.kafka.KafkaConnectorIncomingConfiguration) ByteArrayDeserializer(org.apache.kafka.common.serialization.ByteArrayDeserializer) RecordHeader(org.apache.kafka.common.header.internals.RecordHeader) KafkaMapBasedConfig(io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig) Test(org.junit.jupiter.api.Test)

Example 40 with KafkaMapBasedConfig

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

the class KafkaSourceWithCloudEventsTest method getConfig.

private KafkaMapBasedConfig getConfig(String topic) {
    KafkaMapBasedConfig config = kafkaConfig("mp.messaging.incoming.data");
    config.put("value.deserializer", StringDeserializer.class.getName());
    config.put("enable.auto.commit", "false");
    config.put("auto.offset.reset", "earliest");
    config.put("topic", topic);
    return config;
}
Also used : StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) KafkaMapBasedConfig(io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig)

Aggregations

KafkaMapBasedConfig (io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig)86 Test (org.junit.jupiter.api.Test)56 Message (org.eclipse.microprofile.reactive.messaging.Message)34 RecordHeader (org.apache.kafka.common.header.internals.RecordHeader)30 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)25 StringDeserializer (org.apache.kafka.common.serialization.StringDeserializer)22 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)20 JsonObject (io.vertx.core.json.JsonObject)19 ArrayList (java.util.ArrayList)18 KafkaConnectorIncomingConfiguration (io.smallrye.reactive.messaging.kafka.KafkaConnectorIncomingConfiguration)17 KafkaConnectorOutgoingConfiguration (io.smallrye.reactive.messaging.kafka.KafkaConnectorOutgoingConfiguration)17 KafkaSink (io.smallrye.reactive.messaging.kafka.impl.KafkaSink)17 IncomingKafkaCloudEventMetadata (io.smallrye.reactive.messaging.kafka.IncomingKafkaCloudEventMetadata)14 IntegerDeserializer (org.apache.kafka.common.serialization.IntegerDeserializer)14 IntegerSerializer (org.apache.kafka.common.serialization.IntegerSerializer)13 JsonObjectDeserializer (io.vertx.kafka.client.serialization.JsonObjectDeserializer)5 Header (org.apache.kafka.common.header.Header)5 HealthReport (io.smallrye.reactive.messaging.health.HealthReport)4 KafkaCompanionTestBase (io.smallrye.reactive.messaging.kafka.base.KafkaCompanionTestBase)3 List (java.util.List)3