Search in sources :

Example 1 with Message

use of org.eclipse.microprofile.reactive.messaging.Message in project sandbox by 5733d9e2be6485d52ffa08870cabdee0.

the class KafkaTopicInvokerTest method onEvent.

@Test
public void onEvent() {
    ArgumentCaptor<Message<String>> captor = ArgumentCaptor.forClass(Message.class);
    Emitter<String> emitter = mock(Emitter.class);
    String event = "{\"key\": \"value\"}";
    String topic = "myTestTopic";
    ProcessorDTO processor = createProcessor();
    KafkaTopicInvoker invoker = new KafkaTopicInvoker(emitter, processor, topic);
    invoker.onEvent(event);
    verify(emitter).send(captor.capture());
    Message<String> sent = captor.getValue();
    assertThat(sent.getPayload()).isEqualTo(event);
    Metadata metadata = sent.getMetadata();
    OutgoingKafkaRecordMetadata recordMetadata = metadata.get(OutgoingKafkaRecordMetadata.class).get();
    assertThat(recordMetadata.getTopic()).isEqualTo(topic);
}
Also used : Message(org.eclipse.microprofile.reactive.messaging.Message) ProcessorDTO(com.redhat.service.bridge.infra.models.dto.ProcessorDTO) OutgoingKafkaRecordMetadata(io.smallrye.reactive.messaging.kafka.api.OutgoingKafkaRecordMetadata) Metadata(org.eclipse.microprofile.reactive.messaging.Metadata) OutgoingKafkaRecordMetadata(io.smallrye.reactive.messaging.kafka.api.OutgoingKafkaRecordMetadata) Test(org.junit.jupiter.api.Test)

Example 2 with Message

use of org.eclipse.microprofile.reactive.messaging.Message in project smallrye-reactive-messaging by smallrye.

the class KafkaSourceTest method testSource.

@SuppressWarnings("unchecked")
@Test
public void testSource() {
    MapBasedConfig config = newCommonConfigForSource().with("value.deserializer", IntegerDeserializer.class.getName());
    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.produceIntegers().usingGenerator(i -> new ProducerRecord<>(topic, "hello", i), 10);
    await().atMost(2, TimeUnit.MINUTES).until(() -> messages.size() >= 10);
    assertThat(messages.stream().map(m -> ((KafkaRecord<String, Integer>) m).getPayload()).collect(Collectors.toList())).containsExactly(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
}
Also used : IntegerDeserializer(org.apache.kafka.common.serialization.IntegerDeserializer) IntStream(java.util.stream.IntStream) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) KafkaCompanionTestBase(io.smallrye.reactive.messaging.kafka.base.KafkaCompanionTestBase) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) UnsatisfiedResolutionException(javax.enterprise.inject.UnsatisfiedResolutionException) HealthReport(io.smallrye.reactive.messaging.health.HealthReport) Random(java.util.Random) KafkaMetadataUtil(io.smallrye.reactive.messaging.kafka.api.KafkaMetadataUtil) Multi(io.smallrye.mutiny.Multi) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Assert(org.testng.Assert) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) KafkaBrokerExtension(io.smallrye.reactive.messaging.kafka.companion.test.KafkaBrokerExtension) Tag(org.junit.jupiter.api.Tag) ExecutorService(java.util.concurrent.ExecutorService) DeploymentException(org.jboss.weld.exceptions.DeploymentException) TopicPartition(org.apache.kafka.common.TopicPartition) ExecutionHolder(io.smallrye.reactive.messaging.providers.connectors.ExecutionHolder) Awaitility.await(org.awaitility.Awaitility.await) StrimziKafkaContainer(io.strimzi.test.container.StrimziKafkaContainer) UUID(java.util.UUID) KafkaMapBasedConfig(io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig) UnsatisfiedInstance(io.smallrye.reactive.messaging.kafka.base.UnsatisfiedInstance) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) Channel(org.eclipse.microprofile.reactive.messaging.Channel) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) List(java.util.List) Message(org.eclipse.microprofile.reactive.messaging.Message) AfterEach(org.junit.jupiter.api.AfterEach) KafkaCompanion(io.smallrye.reactive.messaging.kafka.companion.KafkaCompanion) IntegerDeserializer(org.apache.kafka.common.serialization.IntegerDeserializer) ApplicationScoped(javax.enterprise.context.ApplicationScoped) KafkaSource(io.smallrye.reactive.messaging.kafka.impl.KafkaSource) PublisherBuilder(org.eclipse.microprofile.reactive.streams.operators.PublisherBuilder) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Identifier(io.smallrye.common.annotation.Identifier) Message(org.eclipse.microprofile.reactive.messaging.Message) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig) KafkaMapBasedConfig(io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig) Test(org.junit.jupiter.api.Test)

Example 3 with Message

use of org.eclipse.microprofile.reactive.messaging.Message in project smallrye-reactive-messaging by smallrye.

the class KafkaSourceWithLegacyMetadataTest method testInvalidIncomingType.

@SuppressWarnings("unchecked")
@Test
public void testInvalidIncomingType() {
    MapBasedConfig config = newCommonConfigForSource().with("value.deserializer", IntegerDeserializer.class.getName());
    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.produceIntegers().usingGenerator(i -> new ProducerRecord<>(topic, i), 2);
    await().atMost(2, TimeUnit.MINUTES).until(() -> messages.size() >= 2);
    assertThat(messages.stream().map(m -> ((KafkaRecord<String, Integer>) m).getPayload()).collect(Collectors.toList())).containsExactly(0, 1);
    companion.produceStrings().fromRecords(new ProducerRecord<>(topic, "hello"));
    companion.produceIntegers().usingGenerator(i -> new ProducerRecord<>(topic, i), 2);
    // no other message received
    assertThat(messages.stream().map(m -> ((KafkaRecord<String, Integer>) m).getPayload()).collect(Collectors.toList())).containsExactly(0, 1);
}
Also used : IntegerDeserializer(org.apache.kafka.common.serialization.IntegerDeserializer) IntStream(java.util.stream.IntStream) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) KafkaCompanionTestBase(io.smallrye.reactive.messaging.kafka.base.KafkaCompanionTestBase) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) UnsatisfiedResolutionException(javax.enterprise.inject.UnsatisfiedResolutionException) HealthReport(io.smallrye.reactive.messaging.health.HealthReport) Random(java.util.Random) KafkaMetadataUtil(io.smallrye.reactive.messaging.kafka.api.KafkaMetadataUtil) Multi(io.smallrye.mutiny.Multi) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Duration(java.time.Duration) KafkaBrokerExtension(io.smallrye.reactive.messaging.kafka.companion.test.KafkaBrokerExtension) Tag(org.junit.jupiter.api.Tag) ExecutorService(java.util.concurrent.ExecutorService) DeploymentException(org.jboss.weld.exceptions.DeploymentException) TopicPartition(org.apache.kafka.common.TopicPartition) ExecutionHolder(io.smallrye.reactive.messaging.providers.connectors.ExecutionHolder) Awaitility.await(org.awaitility.Awaitility.await) StrimziKafkaContainer(io.strimzi.test.container.StrimziKafkaContainer) UUID(java.util.UUID) KafkaMapBasedConfig(io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig) UnsatisfiedInstance(io.smallrye.reactive.messaging.kafka.base.UnsatisfiedInstance) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) Channel(org.eclipse.microprofile.reactive.messaging.Channel) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) List(java.util.List) Message(org.eclipse.microprofile.reactive.messaging.Message) AfterEach(org.junit.jupiter.api.AfterEach) KafkaCompanion(io.smallrye.reactive.messaging.kafka.companion.KafkaCompanion) IntegerDeserializer(org.apache.kafka.common.serialization.IntegerDeserializer) ApplicationScoped(javax.enterprise.context.ApplicationScoped) KafkaSource(io.smallrye.reactive.messaging.kafka.impl.KafkaSource) KafkaBrokerExtension.restart(io.smallrye.reactive.messaging.kafka.companion.test.KafkaBrokerExtension.restart) PublisherBuilder(org.eclipse.microprofile.reactive.streams.operators.PublisherBuilder) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Identifier(io.smallrye.common.annotation.Identifier) Message(org.eclipse.microprofile.reactive.messaging.Message) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig) KafkaMapBasedConfig(io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig) Test(org.junit.jupiter.api.Test)

Example 4 with Message

use of org.eclipse.microprofile.reactive.messaging.Message in project smallrye-reactive-messaging by smallrye.

the class KafkaSourceWithLegacyMetadataTest method testSourceWithPartitions.

@SuppressWarnings("unchecked")
@Test
public void testSourceWithPartitions() {
    MapBasedConfig config = newCommonConfigForSource().with("value.deserializer", IntegerDeserializer.class.getName()).with("partitions", 4);
    companion.topics().createAndWait(topic, 3);
    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.produceIntegers().usingGenerator(i -> new ProducerRecord<>(topic, i), 1000);
    await().atMost(2, TimeUnit.MINUTES).until(() -> messages.size() >= 1000);
    List<Integer> expected = IntStream.range(0, 1000).boxed().collect(Collectors.toList());
    // Because of partitions we cannot enforce the order.
    assertThat(messages.stream().map(m -> ((KafkaRecord<String, Integer>) m).getPayload()).collect(Collectors.toList())).containsExactlyInAnyOrderElementsOf(expected);
}
Also used : IntStream(java.util.stream.IntStream) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) KafkaCompanionTestBase(io.smallrye.reactive.messaging.kafka.base.KafkaCompanionTestBase) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) UnsatisfiedResolutionException(javax.enterprise.inject.UnsatisfiedResolutionException) HealthReport(io.smallrye.reactive.messaging.health.HealthReport) Random(java.util.Random) KafkaMetadataUtil(io.smallrye.reactive.messaging.kafka.api.KafkaMetadataUtil) Multi(io.smallrye.mutiny.Multi) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Duration(java.time.Duration) KafkaBrokerExtension(io.smallrye.reactive.messaging.kafka.companion.test.KafkaBrokerExtension) Tag(org.junit.jupiter.api.Tag) ExecutorService(java.util.concurrent.ExecutorService) DeploymentException(org.jboss.weld.exceptions.DeploymentException) TopicPartition(org.apache.kafka.common.TopicPartition) ExecutionHolder(io.smallrye.reactive.messaging.providers.connectors.ExecutionHolder) Awaitility.await(org.awaitility.Awaitility.await) StrimziKafkaContainer(io.strimzi.test.container.StrimziKafkaContainer) UUID(java.util.UUID) KafkaMapBasedConfig(io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig) UnsatisfiedInstance(io.smallrye.reactive.messaging.kafka.base.UnsatisfiedInstance) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) Channel(org.eclipse.microprofile.reactive.messaging.Channel) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) List(java.util.List) Message(org.eclipse.microprofile.reactive.messaging.Message) AfterEach(org.junit.jupiter.api.AfterEach) KafkaCompanion(io.smallrye.reactive.messaging.kafka.companion.KafkaCompanion) IntegerDeserializer(org.apache.kafka.common.serialization.IntegerDeserializer) ApplicationScoped(javax.enterprise.context.ApplicationScoped) KafkaSource(io.smallrye.reactive.messaging.kafka.impl.KafkaSource) KafkaBrokerExtension.restart(io.smallrye.reactive.messaging.kafka.companion.test.KafkaBrokerExtension.restart) PublisherBuilder(org.eclipse.microprofile.reactive.streams.operators.PublisherBuilder) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Identifier(io.smallrye.common.annotation.Identifier) Message(org.eclipse.microprofile.reactive.messaging.Message) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig) KafkaMapBasedConfig(io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig) Test(org.junit.jupiter.api.Test)

Example 5 with Message

use of org.eclipse.microprofile.reactive.messaging.Message in project smallrye-reactive-messaging by smallrye.

the class KafkaSourceWithLegacyMetadataTest method testSource.

@SuppressWarnings("unchecked")
@Test
public void testSource() {
    MapBasedConfig config = newCommonConfigForSource().with("value.deserializer", IntegerDeserializer.class.getName());
    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.produceIntegers().usingGenerator(i -> new ProducerRecord<>(topic, "hello", i), 10);
    await().atMost(2, TimeUnit.MINUTES).until(() -> messages.size() >= 10);
    assertThat(messages.stream().map(m -> ((KafkaRecord<String, Integer>) m).getPayload()).collect(Collectors.toList())).containsExactly(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
}
Also used : IntegerDeserializer(org.apache.kafka.common.serialization.IntegerDeserializer) IntStream(java.util.stream.IntStream) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) KafkaCompanionTestBase(io.smallrye.reactive.messaging.kafka.base.KafkaCompanionTestBase) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) UnsatisfiedResolutionException(javax.enterprise.inject.UnsatisfiedResolutionException) HealthReport(io.smallrye.reactive.messaging.health.HealthReport) Random(java.util.Random) KafkaMetadataUtil(io.smallrye.reactive.messaging.kafka.api.KafkaMetadataUtil) Multi(io.smallrye.mutiny.Multi) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Duration(java.time.Duration) KafkaBrokerExtension(io.smallrye.reactive.messaging.kafka.companion.test.KafkaBrokerExtension) Tag(org.junit.jupiter.api.Tag) ExecutorService(java.util.concurrent.ExecutorService) DeploymentException(org.jboss.weld.exceptions.DeploymentException) TopicPartition(org.apache.kafka.common.TopicPartition) ExecutionHolder(io.smallrye.reactive.messaging.providers.connectors.ExecutionHolder) Awaitility.await(org.awaitility.Awaitility.await) StrimziKafkaContainer(io.strimzi.test.container.StrimziKafkaContainer) UUID(java.util.UUID) KafkaMapBasedConfig(io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig) UnsatisfiedInstance(io.smallrye.reactive.messaging.kafka.base.UnsatisfiedInstance) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) Channel(org.eclipse.microprofile.reactive.messaging.Channel) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) List(java.util.List) Message(org.eclipse.microprofile.reactive.messaging.Message) AfterEach(org.junit.jupiter.api.AfterEach) KafkaCompanion(io.smallrye.reactive.messaging.kafka.companion.KafkaCompanion) IntegerDeserializer(org.apache.kafka.common.serialization.IntegerDeserializer) ApplicationScoped(javax.enterprise.context.ApplicationScoped) KafkaSource(io.smallrye.reactive.messaging.kafka.impl.KafkaSource) KafkaBrokerExtension.restart(io.smallrye.reactive.messaging.kafka.companion.test.KafkaBrokerExtension.restart) PublisherBuilder(org.eclipse.microprofile.reactive.streams.operators.PublisherBuilder) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Identifier(io.smallrye.common.annotation.Identifier) Message(org.eclipse.microprofile.reactive.messaging.Message) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig) KafkaMapBasedConfig(io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig) Test(org.junit.jupiter.api.Test)

Aggregations

Message (org.eclipse.microprofile.reactive.messaging.Message)146 Test (org.junit.jupiter.api.Test)124 MapBasedConfig (io.smallrye.reactive.messaging.test.common.config.MapBasedConfig)72 JsonObject (io.vertx.core.json.JsonObject)59 ArrayList (java.util.ArrayList)49 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)44 KafkaMapBasedConfig (io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig)41 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)39 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)35 Awaitility.await (org.awaitility.Awaitility.await)33 AfterEach (org.junit.jupiter.api.AfterEach)33 HealthReport (io.smallrye.reactive.messaging.health.HealthReport)31 Collectors (java.util.stream.Collectors)31 RecordHeader (org.apache.kafka.common.header.internals.RecordHeader)31 TopicPartition (org.apache.kafka.common.TopicPartition)30 Multi (io.smallrye.mutiny.Multi)29 TimeUnit (java.util.concurrent.TimeUnit)29 KafkaConnectorIncomingConfiguration (io.smallrye.reactive.messaging.kafka.KafkaConnectorIncomingConfiguration)28 List (java.util.List)24 CompletableFuture (java.util.concurrent.CompletableFuture)21