splitter: 1.2.0 - Refined creation of input in `SplitterApplicationIT`
authorKai Moritz <kai@juplo.de>
Sun, 2 Jun 2024 11:44:28 +0000 (13:44 +0200)
committerKai Moritz <kai@juplo.de>
Mon, 3 Jun 2024 07:34:40 +0000 (09:34 +0200)
src/test/java/de/juplo/kafka/wordcount/splitter/SplitterApplicationIT.java
src/test/java/de/juplo/kafka/wordcount/splitter/TestData.java

index a702e1d..743c06e 100644 (file)
@@ -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<String, TestRecording> kafkaTemplate;
        @Autowired
        Consumer consumer;
 
-
-       @BeforeEach
-       public void clear()
+       @BeforeAll
+       public static void testSendMessage(
+                       @Autowired KafkaTemplate<String, TestRecording> kafkaTemplate)
        {
-               consumer.received.clear();
+               Stream
+                               .of(TestData.INPUT_MESSAGES)
+                               .forEach(kv ->
+                               {
+                                       try
+                                       {
+                                               SendResult<String, TestRecording> 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(() ->
index e2bf9cc..8c343d5 100644 (file)
@@ -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<String, TestRecording> consumer)
-       {
-               Stream
-                               .of(INPUT_MESSAGES)
-                               .forEach(kv -> consumer.accept(kv.key, kv.value));
-       }
 
        static final KeyValue<String, TestRecording>[] INPUT_MESSAGES = new KeyValue[]
        {