splitter: 1.2.0 - Renamed classes for test-input/-output -- ALIGN
[demos/kafka/wordcount] / src / test / java / de / juplo / kafka / wordcount / splitter / SplitterApplicationIT.java
index adf4dde..891a435 100644 (file)
@@ -1,8 +1,9 @@
 package de.juplo.kafka.wordcount.splitter;
 
-import de.juplo.kafka.wordcount.counter.TestWord;
+import de.juplo.kafka.wordcount.counter.TestOutputWord;
+import de.juplo.kafka.wordcount.recorder.TestInputRecording;
 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;
@@ -11,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,7 +21,8 @@ import org.springframework.util.MultiValueMap;
 
 import java.time.Duration;
 
-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;
 
 
@@ -43,23 +46,38 @@ public class SplitterApplicationIT
        public final static String TOPIC_IN = "in";
        public final static String TOPIC_OUT = "out";
 
-       @Autowired
-       KafkaTemplate<String, Recording> kafkaTemplate;
        @Autowired
        Consumer consumer;
 
-
-       @BeforeEach
-       public void clear()
+       @BeforeAll
+       public static void testSendMessage(
+                       @Autowired KafkaTemplate<String, TestInputRecording> kafkaTemplate)
        {
-               consumer.received.clear();
+               TestData
+                               .getInputMessages()
+                               .forEach(kv ->
+                               {
+                                       try
+                                       {
+                                               SendResult<String, TestInputRecording> 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(() ->
@@ -69,18 +87,18 @@ public class SplitterApplicationIT
 
        static class Consumer
        {
-               private final MultiValueMap<String, TestWord> received = new LinkedMultiValueMap<>();
+               private final MultiValueMap<String, TestOutputWord> received = new LinkedMultiValueMap<>();
 
                @KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
                public synchronized void receive(
                                @Header(KafkaHeaders.RECEIVED_KEY) String key,
-                               @Payload TestWord value)
+                               @Payload TestOutputWord value)
                {
                        log.debug("Received message: {}={}", key, value);
                        received.add(key, value);
                }
 
-               synchronized MultiValueMap<String, TestWord> getReceivedMessages()
+               synchronized MultiValueMap<String, TestOutputWord> getReceivedMessages()
                {
                        return received;
                }