From: Kai Moritz Date: Sun, 2 Jun 2024 11:44:28 +0000 (+0200) Subject: splitter: 1.2.0 - Refined creation of input in `SplitterApplicationIT` X-Git-Tag: splitter-1.2.0~7 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=5283e1688c50764d828f83f7d58d4cf1c7e56cf9;p=demos%2Fkafka%2Fwordcount splitter: 1.2.0 - Refined creation of input in `SplitterApplicationIT` --- diff --git a/src/test/java/de/juplo/kafka/wordcount/splitter/SplitterApplicationIT.java b/src/test/java/de/juplo/kafka/wordcount/splitter/SplitterApplicationIT.java index a702e1d..743c06e 100644 --- a/src/test/java/de/juplo/kafka/wordcount/splitter/SplitterApplicationIT.java +++ b/src/test/java/de/juplo/kafka/wordcount/splitter/SplitterApplicationIT.java @@ -3,7 +3,7 @@ package de.juplo.kafka.wordcount.splitter; import de.juplo.kafka.wordcount.counter.TestWord; import de.juplo.kafka.wordcount.recorder.TestRecording; import lombok.extern.slf4j.Slf4j; -import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @@ -12,6 +12,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.kafka.annotation.KafkaListener; import org.springframework.kafka.core.KafkaTemplate; import org.springframework.kafka.support.KafkaHeaders; +import org.springframework.kafka.support.SendResult; import org.springframework.kafka.test.context.EmbeddedKafka; import org.springframework.messaging.handler.annotation.Header; import org.springframework.messaging.handler.annotation.Payload; @@ -19,8 +20,10 @@ import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import java.time.Duration; +import java.util.stream.Stream; -import static de.juplo.kafka.wordcount.splitter.SplitterApplicationIT.*; +import static de.juplo.kafka.wordcount.splitter.SplitterApplicationIT.TOPIC_IN; +import static de.juplo.kafka.wordcount.splitter.SplitterApplicationIT.TOPIC_OUT; import static org.awaitility.Awaitility.await; @@ -44,23 +47,38 @@ public class SplitterApplicationIT public final static String TOPIC_IN = "in"; public final static String TOPIC_OUT = "out"; - @Autowired - KafkaTemplate kafkaTemplate; @Autowired Consumer consumer; - - @BeforeEach - public void clear() + @BeforeAll + public static void testSendMessage( + @Autowired KafkaTemplate kafkaTemplate) { - consumer.received.clear(); + Stream + .of(TestData.INPUT_MESSAGES) + .forEach(kv -> + { + try + { + SendResult result = kafkaTemplate.send(TOPIC_IN, kv.key, kv.value).get(); + log.info( + "Sent: {}={}, partition={}, offset={}", + result.getProducerRecord().key(), + result.getProducerRecord().value(), + result.getRecordMetadata().partition(), + result.getRecordMetadata().offset()); + } + catch (Exception e) + { + throw new RuntimeException(e); + } + }); } + @Test void testSendMessage() throws Exception { - TestData.writeInputData((user, recording) -> kafkaTemplate.send(TOPIC_IN, user, recording)); - await("Expexted converted data") .atMost(Duration.ofSeconds(5)) .untilAsserted(() -> diff --git a/src/test/java/de/juplo/kafka/wordcount/splitter/TestData.java b/src/test/java/de/juplo/kafka/wordcount/splitter/TestData.java index e2bf9cc..8c343d5 100644 --- a/src/test/java/de/juplo/kafka/wordcount/splitter/TestData.java +++ b/src/test/java/de/juplo/kafka/wordcount/splitter/TestData.java @@ -7,7 +7,6 @@ import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import java.time.Duration; -import java.util.function.BiConsumer; import java.util.stream.Stream; import static org.assertj.core.api.Assertions.assertThat; @@ -19,12 +18,6 @@ public class TestData static final String PETER = "peter"; static final String KLAUS = "klaus"; - static void writeInputData(BiConsumer consumer) - { - Stream - .of(INPUT_MESSAGES) - .forEach(kv -> consumer.accept(kv.key, kv.value)); - } static final KeyValue[] INPUT_MESSAGES = new KeyValue[] {