1 package de.juplo.kafka.wordcount.splitter;
3 import de.juplo.kafka.wordcount.counter.TestWord;
4 import lombok.extern.slf4j.Slf4j;
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.support.KafkaHeaders;
14 import org.springframework.kafka.test.context.EmbeddedKafka;
15 import org.springframework.messaging.handler.annotation.Header;
16 import org.springframework.messaging.handler.annotation.Payload;
17 import org.springframework.util.LinkedMultiValueMap;
18 import org.springframework.util.MultiValueMap;
20 import java.time.Duration;
22 import static de.juplo.kafka.wordcount.splitter.SplitterApplicationIT.*;
23 import static org.awaitility.Awaitility.await;
28 "spring.kafka.producer.value-serializer=org.springframework.kafka.support.serializer.JsonSerializer",
29 "spring.kafka.producer.properties.spring.json.add.type.headers=false",
30 "spring.kafka.consumer.auto-offset-reset=earliest",
31 "spring.kafka.consumer.value-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer",
32 "spring.kafka.consumer.properties.spring.json.value.default.type=de.juplo.kafka.wordcount.counter.TestWord",
33 "spring.kafka.consumer.properties.spring.json.trusted.packages=de.juplo.kafka.wordcount.splitter",
34 "logging.level.root=WARN",
35 "logging.level.de.juplo=DEBUG",
36 "juplo.wordcount.splitter.bootstrap-server=${spring.embedded.kafka.brokers}",
37 "juplo.wordcount.splitter.input-topic=" + TOPIC_IN,
38 "juplo.wordcount.splitter.output-topic=" + TOPIC_OUT })
39 @EmbeddedKafka(topics = { TOPIC_IN, TOPIC_OUT })
41 public class SplitterApplicationIT
43 public final static String TOPIC_IN = "in";
44 public final static String TOPIC_OUT = "out";
47 KafkaTemplate<String, Recording> kafkaTemplate;
55 consumer.received.clear();
59 void testSendMessage() throws Exception
61 TestData.writeInputData((user, recording) -> kafkaTemplate.send(TOPIC_IN, user, recording));
63 await("Expexted converted data")
64 .atMost(Duration.ofSeconds(5))
66 TestData.assertExpectedMessages(consumer.getReceivedMessages()));
72 private final MultiValueMap<String, TestWord> received = new LinkedMultiValueMap<>();
74 @KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
75 public synchronized void receive(
76 @Header(KafkaHeaders.RECEIVED_KEY) String key,
77 @Payload TestWord value)
79 log.debug("Received message: {}={}", key, value);
80 received.add(key, value);
83 synchronized MultiValueMap<String, TestWord> getReceivedMessages()
91 static class Configuration
96 return new Consumer();