1 package de.juplo.kafka.wordcount.popular;
3 import de.juplo.kafka.wordcount.splitter.InputUser;
4 import de.juplo.kafka.wordcount.splitter.InputWord;
5 import de.juplo.kafka.wordcount.stats.OutputWindowedWord;
6 import de.juplo.kafka.wordcount.stats.OutputWordCounter;
7 import lombok.extern.slf4j.Slf4j;
8 import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier;
9 import org.apache.kafka.streams.state.Stores;
10 import org.junit.jupiter.api.BeforeAll;
11 import org.junit.jupiter.api.DisplayName;
12 import org.junit.jupiter.api.Test;
13 import org.springframework.beans.factory.annotation.Autowired;
14 import org.springframework.boot.test.context.SpringBootTest;
15 import org.springframework.boot.test.context.TestConfiguration;
16 import org.springframework.context.annotation.Bean;
17 import org.springframework.kafka.annotation.KafkaListener;
18 import org.springframework.kafka.core.KafkaTemplate;
19 import org.springframework.kafka.support.KafkaHeaders;
20 import org.springframework.kafka.support.SendResult;
21 import org.springframework.kafka.test.context.EmbeddedKafka;
22 import org.springframework.messaging.handler.annotation.Header;
23 import org.springframework.messaging.handler.annotation.Payload;
24 import org.springframework.util.LinkedMultiValueMap;
25 import org.springframework.util.MultiValueMap;
27 import java.time.Duration;
29 import static de.juplo.kafka.wordcount.popular.PopularApplicationIT.TOPIC_IN;
30 import static de.juplo.kafka.wordcount.popular.PopularApplicationIT.TOPIC_OUT;
31 import static de.juplo.kafka.wordcount.popular.PopularStreamProcessor.STORE_NAME;
32 import static org.awaitility.Awaitility.await;
37 "spring.main.allow-bean-definition-overriding=true",
38 "spring.kafka.producer.key-serializer=org.springframework.kafka.support.serializer.JsonSerializer",
39 "spring.kafka.producer.value-serializer=org.springframework.kafka.support.serializer.JsonSerializer",
40 "spring.kafka.producer.properties.spring.json.add.type.headers=false",
41 "spring.kafka.consumer.auto-offset-reset=earliest",
42 "spring.kafka.consumer.key-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer",
43 "spring.kafka.consumer.value-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer",
44 "spring.kafka.consumer.properties.spring.json.type.mapping=word:de.juplo.kafka.wordcount.stats.OutputWindowedWord,counter:de.juplo.kafka.wordcount.stats.OutputWordCounter",
45 "logging.level.root=WARN",
46 "logging.level.de.juplo=DEBUG",
47 "juplo.wordcount.popular.bootstrap-server=${spring.embedded.kafka.brokers}",
48 "juplo.wordcount.popular.commit-interval=100",
49 "juplo.wordcount.popular.cache-max-bytes=0",
50 "juplo.wordcount.popular.input-topic=" + TOPIC_IN,
51 "juplo.wordcount.popular.output-topic=" + TOPIC_OUT })
52 @EmbeddedKafka(topics = { TOPIC_IN, TOPIC_OUT })
54 public class PopularApplicationIT
56 public static final String TOPIC_IN = "in";
57 public static final String TOPIC_OUT = "out";
62 PopularStreamProcessor streamProcessor;
66 public static void testSendMessage(
67 @Autowired KafkaTemplate<InputUser, InputWord> kafkaTemplate)
75 SendResult<InputUser, InputWord> result = kafkaTemplate.send(TOPIC_IN, kv.key, kv.value).get();
77 "Sent: {}={}, partition={}, offset={}",
78 result.getProducerRecord().key(),
79 result.getProducerRecord().value(),
80 result.getRecordMetadata().partition(),
81 result.getRecordMetadata().offset());
85 throw new RuntimeException(e);
90 @DisplayName("Await the expected number of messages")
92 public void testAwaitExpectedNumberOfMessagesForUsers()
94 await("Expected number of messages")
95 .atMost(Duration.ofSeconds(5))
96 .untilAsserted(() -> consumer.enforceAssertion(
97 receivedMessages -> TestData.assertExpectedNumberOfMessagesForWord(receivedMessages)));
100 @DisplayName("Await the expected output messages")
102 void testSendMessage()
104 await("Expected messages")
105 .atMost(Duration.ofSeconds(10))
106 .untilAsserted(() -> consumer.enforceAssertion(
107 receivedMessages -> TestData.assertExpectedMessages(receivedMessages)));
110 @DisplayName("Await the expected final output messages")
112 public void testAwaitExpectedLastMessagesForUsers()
114 await("Expected final output messages")
115 .atMost(Duration.ofSeconds(5))
116 .untilAsserted(() -> consumer.enforceAssertion(
117 receivedMessages -> TestData.assertExpectedLastMessagesForWord(receivedMessages)));
120 @DisplayName("Await the expected state in the state-store")
122 public void testAwaitExpectedState()
124 await("Expected state")
125 .atMost(Duration.ofSeconds(5))
126 .untilAsserted(() -> TestData.assertExpectedState(streamProcessor.getStore()));
130 static class Consumer
132 private final MultiValueMap<OutputWindowedWord, OutputWordCounter> received = new LinkedMultiValueMap<>();
134 @KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
135 public synchronized void receive(
136 @Header(KafkaHeaders.RECEIVED_KEY) OutputWindowedWord word,
137 @Payload OutputWordCounter counter)
139 log.debug("Received message: {} -> {}", word, counter);
140 received.add(word, counter);
143 synchronized void enforceAssertion(
144 java.util.function.Consumer<MultiValueMap<OutputWindowedWord, OutputWordCounter>> assertion)
146 assertion.accept(received);
151 static class Configuration
156 return new Consumer();
160 KeyValueBytesStoreSupplier storeSupplier()
162 return Stores.inMemoryKeyValueStore(STORE_NAME);