1 package de.juplo.kafka.wordcount.splitter;
3 import org.apache.kafka.clients.consumer.ConsumerRecord;
4 import org.awaitility.Awaitility;
5 import org.junit.jupiter.api.BeforeEach;
6 import org.junit.jupiter.api.Test;
7 import org.springframework.beans.factory.annotation.Autowired;
8 import org.springframework.boot.test.context.SpringBootTest;
9 import org.springframework.boot.test.context.TestConfiguration;
10 import org.springframework.context.annotation.Bean;
11 import org.springframework.kafka.annotation.KafkaListener;
12 import org.springframework.kafka.core.KafkaTemplate;
13 import org.springframework.kafka.test.context.EmbeddedKafka;
15 import java.time.Duration;
16 import java.util.HashMap;
17 import java.util.LinkedList;
18 import java.util.List;
21 import static de.juplo.kafka.wordcount.splitter.ApplicationTests.TOPIC_IN;
22 import static de.juplo.kafka.wordcount.splitter.ApplicationTests.TOPIC_OUT;
23 import static org.assertj.core.api.Assertions.assertThat;
28 "juplo.wordcount.splitter.bootstrap-server=${spring.embedded.kafka.brokers}",
29 "juplo.wordcount.splitter.input-topic=" + TOPIC_IN,
30 "juplo.wordcount.splitter.output-topic=" + TOPIC_OUT,
31 "spring.kafka.bootstrap-servers=${spring.embedded.kafka.brokers}",
34 topics = { TOPIC_IN, TOPIC_OUT },
36 "transaction.state.log.replication.factor=1",
37 "transaction.state.log.min.isr=1",
39 class ApplicationTests
41 final static String TOPIC_IN = "in";
42 final static String TOPIC_OUT = "out";
45 KafkaTemplate<String, String> kafkaTemplate;
49 MessageSplitter messagSplitter;
66 kafkaTemplate.send(TOPIC_IN, "beate", LORE_IPSUM);
68 String[] words = messagSplitter.split(LORE_IPSUM);
71 .await("Receive all words")
72 .atMost(Duration.ofSeconds(5))
74 assertThat(listener.total)
75 .describedAs("Received all words")
76 .isEqualTo(words.length));
78 assertThat(listener.words.keySet())
79 .describedAs("Received unexpected key(s)")
80 .containsExactlyInAnyOrder("beate");
81 assertThat(listener.words.get("beate"))
82 .describedAs("Received unexpected words")
83 .containsExactly(words);
89 kafkaTemplate.send(TOPIC_IN, "beate", LORE_IPSUM);
90 kafkaTemplate.send(TOPIC_IN, "peter", LORE_IPSUM);
91 kafkaTemplate.send(TOPIC_IN, "klaus", LORE_IPSUM);
93 String[] words = messagSplitter.split(LORE_IPSUM);
96 .await("Receive all words")
97 .atMost(Duration.ofSeconds(5))
98 .untilAsserted(() -> {
99 assertThat(listener.total)
100 .describedAs("Received all words for beate")
101 .isEqualTo(words.length * 3);
104 assertThat(listener.words.keySet())
105 .describedAs("Received unexpected key(s)")
106 .containsExactlyInAnyOrder("beate", "peter", "klaus");
107 assertThat(listener.words.get("beate"))
108 .describedAs("Received unexpected words for beate")
109 .containsExactly(words);
110 assertThat(listener.words.get("peter"))
111 .describedAs("Received unexpected words for beate")
112 .containsExactly(words);
113 assertThat(listener.words.get("klaus"))
114 .describedAs("Received unexpected words for beate")
115 .containsExactly(words);
119 static class Listener
121 final Map<String, List<String>> words = new HashMap<>();
124 @KafkaListener(groupId = "peter", topics = TOPIC_OUT)
125 public void receive(ConsumerRecord<String, String> record)
127 List<String> words = this.words.get(record.key());
130 words = new LinkedList<>();
131 this.words.put(record.key(), words);
133 words.add(record.value());
145 static class Configuration
150 return new Listener();
155 final static String LORE_IPSUM =
156 "Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod " +
157 "tempor incidunt ut labore et dolore magna aliqua. Ut enim ad minim " +
158 "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquid ex " +
159 "ea commodi consequat. Quis aute iure reprehenderit in voluptate velit " +
160 "esse cillum dolore eu fugiat nulla pariatur. Excepteur sint obcaecat " +
161 "cupiditat non proident, sunt in culpa qui officia deserunt mollit anim " +