1 package de.juplo.kafka.wordcount.counter;
3 import de.juplo.kafka.wordcount.splitter.TestInputUser;
4 import de.juplo.kafka.wordcount.splitter.TestInputWord;
5 import de.juplo.kafka.wordcount.top10.TestOutputWord;
6 import de.juplo.kafka.wordcount.top10.TestOutputWordCounter;
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.context.annotation.Primary;
18 import org.springframework.kafka.annotation.KafkaListener;
19 import org.springframework.kafka.core.KafkaTemplate;
20 import org.springframework.kafka.support.KafkaHeaders;
21 import org.springframework.kafka.support.SendResult;
22 import org.springframework.kafka.test.context.EmbeddedKafka;
23 import org.springframework.messaging.handler.annotation.Header;
24 import org.springframework.messaging.handler.annotation.Payload;
25 import org.springframework.util.LinkedMultiValueMap;
26 import org.springframework.util.MultiValueMap;
28 import java.time.Duration;
30 import static de.juplo.kafka.wordcount.counter.CounterApplicationIT.TOPIC_IN;
31 import static de.juplo.kafka.wordcount.counter.CounterApplicationIT.TOPIC_OUT;
32 import static de.juplo.kafka.wordcount.counter.CounterStreamProcessor.STORE_NAME;
33 import static org.awaitility.Awaitility.await;
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.top10.TestOutputWord,counter:de.juplo.kafka.wordcount.top10.TestOutputWordCounter",
45 "logging.level.root=WARN",
46 "logging.level.de.juplo=DEBUG",
47 "juplo.wordcount.counter.bootstrap-server=${spring.embedded.kafka.brokers}",
48 "juplo.wordcount.counter.commit-interval=0",
49 "juplo.wordcount.counter.input-topic=" + TOPIC_IN,
50 "juplo.wordcount.counter.output-topic=" + TOPIC_OUT })
51 @EmbeddedKafka(topics = { TOPIC_IN, TOPIC_OUT })
53 public class CounterApplicationIT
55 public static final String TOPIC_IN = "in";
56 public static final String TOPIC_OUT = "out";
61 CounterStreamProcessor streamProcessor;
65 public static void testSendMessage(
66 @Autowired KafkaTemplate<TestInputUser, TestInputWord> kafkaTemplate)
74 SendResult<TestInputUser, TestInputWord> result = kafkaTemplate.send(TOPIC_IN, kv.key, kv.value).get();
76 "Sent: {}={}, partition={}, offset={}",
77 result.getProducerRecord().key(),
78 result.getProducerRecord().value(),
79 result.getRecordMetadata().partition(),
80 result.getRecordMetadata().offset());
84 throw new RuntimeException(e);
89 @DisplayName("Await the expected number of messages")
91 public void testAwaitExpectedNumberOfMessagesForUsers()
93 await("Expected number of messages")
94 .atMost(Duration.ofSeconds(5))
95 .untilAsserted(() -> consumer.enforceAssertion(
96 receivedMessages -> TestData.assertExpectedNumberOfMessagesForWord(receivedMessages)));
99 @DisplayName("Await the expected output messages")
101 void testSendMessage()
103 await("Expected messages")
104 .atMost(Duration.ofSeconds(10))
105 .untilAsserted(() -> consumer.enforceAssertion(
106 receivedMessages -> TestData.assertExpectedMessages(receivedMessages)));
109 @DisplayName("Await the expected final output messages")
111 public void testAwaitExpectedLastMessagesForUsers()
113 await("Expected final output messages")
114 .atMost(Duration.ofSeconds(5))
115 .untilAsserted(() -> consumer.enforceAssertion(
116 receivedMessages -> TestData.assertExpectedLastMessagesForWord(receivedMessages)));
119 @DisplayName("Await the expected state in the state-store")
121 public void testAwaitExpectedState()
123 await("Expected state")
124 .atMost(Duration.ofSeconds(5))
125 .untilAsserted(() -> TestData.assertExpectedState(streamProcessor.getStore()));
129 static class Consumer
131 private final MultiValueMap<TestOutputWord, TestOutputWordCounter> received = new LinkedMultiValueMap<>();
133 @KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
134 public synchronized void receive(
135 @Header(KafkaHeaders.RECEIVED_KEY) TestOutputWord word,
136 @Payload TestOutputWordCounter counter)
138 log.debug("Received message: {} -> {}", word, counter);
139 received.add(word, counter);
142 synchronized void enforceAssertion(
143 java.util.function.Consumer<MultiValueMap<TestOutputWord, TestOutputWordCounter>> assertion)
145 assertion.accept(received);
150 static class Configuration
155 return new Consumer();
160 KeyValueBytesStoreSupplier inMemoryStoreSupplier()
162 return Stores.inMemoryKeyValueStore(STORE_NAME);