1 package de.juplo.kafka.wordcount.counter;
3 import lombok.extern.slf4j.Slf4j;
4 import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier;
5 import org.apache.kafka.streams.state.Stores;
6 import org.junit.jupiter.api.BeforeEach;
7 import org.junit.jupiter.api.Test;
8 import org.springframework.beans.factory.annotation.Autowired;
9 import org.springframework.boot.test.context.SpringBootTest;
10 import org.springframework.boot.test.context.TestConfiguration;
11 import org.springframework.context.annotation.Bean;
12 import org.springframework.context.annotation.Primary;
13 import org.springframework.kafka.annotation.KafkaListener;
14 import org.springframework.kafka.core.KafkaTemplate;
15 import org.springframework.kafka.support.KafkaHeaders;
16 import org.springframework.kafka.test.context.EmbeddedKafka;
17 import org.springframework.messaging.handler.annotation.Header;
18 import org.springframework.messaging.handler.annotation.Payload;
19 import org.springframework.util.LinkedMultiValueMap;
20 import org.springframework.util.MultiValueMap;
22 import java.time.Duration;
24 import static de.juplo.kafka.wordcount.counter.CounterApplicationIT.TOPIC_IN;
25 import static de.juplo.kafka.wordcount.counter.CounterApplicationIT.TOPIC_OUT;
26 import static org.awaitility.Awaitility.await;
31 "spring.kafka.producer.value-serializer=org.springframework.kafka.support.serializer.JsonSerializer",
32 "spring.kafka.producer.properties.spring.json.add.type.headers=false",
33 "spring.kafka.consumer.auto-offset-reset=earliest",
34 "spring.kafka.consumer.key-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer",
35 "spring.kafka.consumer.value-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer",
36 "spring.kafka.consumer.properties.spring.json.use.type.headers=false",
37 "spring.kafka.consumer.properties.spring.json.key.default.type=de.juplo.kafka.wordcount.counter.Word",
38 "spring.kafka.consumer.properties.spring.json.value.default.type=de.juplo.kafka.wordcount.counter.WordCounter",
39 "logging.level.root=WARN",
40 "logging.level.de.juplo=DEBUG",
41 "juplo.wordcount.counter.bootstrap-server=${spring.embedded.kafka.brokers}",
42 "juplo.wordcount.counter.commit-interval=0",
43 "juplo.wordcount.counter.cacheMaxBytes=0",
44 "juplo.wordcount.counter.input-topic=" + TOPIC_IN,
45 "juplo.wordcount.counter.output-topic=" + TOPIC_OUT })
46 @EmbeddedKafka(topics = { TOPIC_IN, TOPIC_OUT })
48 public class CounterApplicationIT
50 public static final String TOPIC_IN = "in";
51 public static final String TOPIC_OUT = "out";
54 KafkaTemplate<String, Word> kafkaTemplate;
62 consumer.received.clear();
67 void testSendMessage()
69 TestData.injectInputMessages((key, value) -> kafkaTemplate.send(TOPIC_IN, key, value));
71 await("Expected messages")
72 .atMost(Duration.ofSeconds(10))
73 .untilAsserted(() -> TestData.assertExpectedMessages(consumer.getReceivedMessages()));
79 private final MultiValueMap<Word, WordCounter> received = new LinkedMultiValueMap<>();
81 @KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
82 public synchronized void receive(
83 @Header(KafkaHeaders.RECEIVED_KEY) Word word,
84 @Payload WordCounter counter)
86 log.debug("Received message: {} -> {}", word, counter);
87 received.add(word, counter);
90 synchronized MultiValueMap<Word, WordCounter> getReceivedMessages()
97 static class Configuration
102 return new Consumer();
107 KeyValueBytesStoreSupplier inMemoryStoreSupplier()
109 return Stores.inMemoryKeyValueStore("TEST-STORE");