counter: 1.2.15 - Refined `TestData.assertExpectedResult(..)`
[demos/kafka/wordcount] / src / test / java / de / juplo / kafka / wordcount / counter / CounterApplicationIT.java
index 992164c..57ac365 100644 (file)
@@ -1,8 +1,6 @@
 package de.juplo.kafka.wordcount.counter;
 
 import lombok.extern.slf4j.Slf4j;
-import org.apache.kafka.clients.consumer.ConsumerRecord;
-import org.apache.kafka.streams.KeyValue;
 import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier;
 import org.apache.kafka.streams.state.Stores;
 import org.junit.jupiter.api.BeforeEach;
@@ -14,18 +12,18 @@ import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Primary;
 import org.springframework.kafka.annotation.KafkaListener;
 import org.springframework.kafka.core.KafkaTemplate;
+import org.springframework.kafka.support.KafkaHeaders;
 import org.springframework.kafka.test.context.EmbeddedKafka;
+import org.springframework.messaging.handler.annotation.Header;
+import org.springframework.messaging.handler.annotation.Payload;
+import org.springframework.util.LinkedMultiValueMap;
+import org.springframework.util.MultiValueMap;
 
 import java.time.Duration;
-import java.util.LinkedList;
-import java.util.List;
 
 import static de.juplo.kafka.wordcount.counter.CounterApplicationIT.TOPIC_IN;
 import static de.juplo.kafka.wordcount.counter.CounterApplicationIT.TOPIC_OUT;
-import static de.juplo.kafka.wordcount.counter.TestData.parseHeader;
 import static org.awaitility.Awaitility.await;
-import static org.springframework.kafka.support.mapping.AbstractJavaTypeMapper.DEFAULT_CLASSID_FIELD_NAME;
-import static org.springframework.kafka.support.mapping.AbstractJavaTypeMapper.KEY_DEFAULT_CLASSID_FIELD_NAME;
 
 
 @SpringBootTest(
@@ -78,21 +76,18 @@ public class CounterApplicationIT
 
        static class Consumer
        {
-               private final List<KeyValue<Word, WordCounter>> received = new LinkedList<>();
+               private final MultiValueMap<Word, WordCounter> received = new LinkedMultiValueMap<>();
 
                @KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
-               public synchronized void receive(ConsumerRecord<Word, WordCounter> record)
+               public synchronized void receive(
+                               @Header(KafkaHeaders.RECEIVED_KEY) Word word,
+                               @Payload WordCounter counter)
                {
-                       log.debug(
-                                       "Received message: {} -> {}, key: {}, value: {}",
-                                       record.key(),
-                                       record.value(),
-                                       parseHeader(record.headers(), KEY_DEFAULT_CLASSID_FIELD_NAME),
-                                       parseHeader(record.headers(), DEFAULT_CLASSID_FIELD_NAME));
-                       received.add(KeyValue.pair(record.key(),record.value()));
+                       log.debug("Received message: {} -> {}", word, counter);
+                       received.add(word, counter);
                }
 
-               synchronized List<KeyValue<Word, WordCounter>> getReceivedMessages()
+               synchronized MultiValueMap<Word, WordCounter> getReceivedMessages()
                {
                        return received;
                }