1 package de.juplo.kafka.wordcount.counter;
3 import de.juplo.kafka.wordcount.splitter.TestInputWord;
4 import de.juplo.kafka.wordcount.top10.TestOutputWord;
5 import de.juplo.kafka.wordcount.top10.TestOutputWordCounter;
6 import lombok.extern.slf4j.Slf4j;
7 import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier;
8 import org.apache.kafka.streams.state.Stores;
9 import org.junit.jupiter.api.BeforeEach;
10 import org.junit.jupiter.api.Test;
11 import org.springframework.beans.factory.annotation.Autowired;
12 import org.springframework.boot.test.context.SpringBootTest;
13 import org.springframework.boot.test.context.TestConfiguration;
14 import org.springframework.context.annotation.Bean;
15 import org.springframework.context.annotation.Primary;
16 import org.springframework.kafka.annotation.KafkaListener;
17 import org.springframework.kafka.core.KafkaTemplate;
18 import org.springframework.kafka.support.KafkaHeaders;
19 import org.springframework.kafka.test.context.EmbeddedKafka;
20 import org.springframework.messaging.handler.annotation.Header;
21 import org.springframework.messaging.handler.annotation.Payload;
22 import org.springframework.util.LinkedMultiValueMap;
23 import org.springframework.util.MultiValueMap;
25 import java.time.Duration;
27 import static de.juplo.kafka.wordcount.counter.CounterApplicationIT.TOPIC_IN;
28 import static de.juplo.kafka.wordcount.counter.CounterApplicationIT.TOPIC_OUT;
29 import static org.awaitility.Awaitility.await;
34 "spring.kafka.producer.value-serializer=org.springframework.kafka.support.serializer.JsonSerializer",
35 "spring.kafka.producer.properties.spring.json.add.type.headers=false",
36 "spring.kafka.consumer.auto-offset-reset=earliest",
37 "spring.kafka.consumer.key-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer",
38 "spring.kafka.consumer.value-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer",
39 "spring.kafka.consumer.properties.spring.json.type.mapping=word:de.juplo.kafka.wordcount.top10.TestOutputWord,counter:de.juplo.kafka.wordcount.top10.TestOutputWordCounter",
40 "logging.level.root=WARN",
41 "logging.level.de.juplo=DEBUG",
42 "juplo.wordcount.counter.bootstrap-server=${spring.embedded.kafka.brokers}",
43 "juplo.wordcount.counter.commit-interval=0",
44 "juplo.wordcount.counter.cacheMaxBytes=0",
45 "juplo.wordcount.counter.input-topic=" + TOPIC_IN,
46 "juplo.wordcount.counter.output-topic=" + TOPIC_OUT })
47 @EmbeddedKafka(topics = { TOPIC_IN, TOPIC_OUT })
49 public class CounterApplicationIT
51 public static final String TOPIC_IN = "in";
52 public static final String TOPIC_OUT = "out";
55 KafkaTemplate<String, TestInputWord> kafkaTemplate;
63 consumer.received.clear();
68 void testSendMessage()
72 .forEach(kv -> kafkaTemplate.send(TOPIC_IN, kv.key, kv.value));
74 await("Expected messages")
75 .atMost(Duration.ofSeconds(10))
76 .untilAsserted(() -> consumer.enforceAssertion(TestData.expectedMessagesAssertion()));
82 private final MultiValueMap<TestOutputWord, TestOutputWordCounter> received = new LinkedMultiValueMap<>();
84 @KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
85 public synchronized void receive(
86 @Header(KafkaHeaders.RECEIVED_KEY) TestOutputWord word,
87 @Payload TestOutputWordCounter counter)
89 log.debug("Received message: {} -> {}", word, counter);
90 received.add(word, counter);
93 synchronized void enforceAssertion(
94 java.util.function.Consumer<MultiValueMap<TestOutputWord, TestOutputWordCounter>> assertion)
96 assertion.accept(received);
101 static class Configuration
106 return new Consumer();
111 KeyValueBytesStoreSupplier inMemoryStoreSupplier()
113 return Stores.inMemoryKeyValueStore("TEST-STORE");