1 package de.juplo.kafka.wordcount.splitter;
3 import de.juplo.kafka.wordcount.counter.TestOutputWord;
4 import de.juplo.kafka.wordcount.recorder.TestInputRecording;
5 import lombok.extern.slf4j.Slf4j;
6 import org.junit.jupiter.api.BeforeAll;
7 import org.junit.jupiter.api.Test;
8 import org.springframework.beans.factory.annotation.Autowired;
9 import org.springframework.boot.test.context.SpringBootTest;
10 import org.springframework.boot.test.context.TestConfiguration;
11 import org.springframework.context.annotation.Bean;
12 import org.springframework.kafka.annotation.KafkaListener;
13 import org.springframework.kafka.core.KafkaTemplate;
14 import org.springframework.kafka.support.KafkaHeaders;
15 import org.springframework.kafka.support.SendResult;
16 import org.springframework.kafka.test.context.EmbeddedKafka;
17 import org.springframework.messaging.handler.annotation.Header;
18 import org.springframework.messaging.handler.annotation.Payload;
19 import org.springframework.util.LinkedMultiValueMap;
20 import org.springframework.util.MultiValueMap;
22 import java.time.Duration;
24 import static de.juplo.kafka.wordcount.splitter.SplitterApplicationIT.TOPIC_IN;
25 import static de.juplo.kafka.wordcount.splitter.SplitterApplicationIT.TOPIC_OUT;
26 import static org.awaitility.Awaitility.await;
31 "spring.kafka.producer.value-serializer=org.springframework.kafka.support.serializer.JsonSerializer",
32 "spring.kafka.producer.properties.spring.json.add.type.headers=false",
33 "spring.kafka.consumer.auto-offset-reset=earliest",
34 "spring.kafka.consumer.value-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer",
35 "spring.kafka.consumer.properties.spring.json.value.default.type=de.juplo.kafka.wordcount.counter.TestWord",
36 "spring.kafka.consumer.properties.spring.json.trusted.packages=de.juplo.kafka.wordcount.splitter",
37 "logging.level.root=WARN",
38 "logging.level.de.juplo=DEBUG",
39 "juplo.wordcount.splitter.bootstrap-server=${spring.embedded.kafka.brokers}",
40 "juplo.wordcount.splitter.input-topic=" + TOPIC_IN,
41 "juplo.wordcount.splitter.output-topic=" + TOPIC_OUT })
42 @EmbeddedKafka(topics = { TOPIC_IN, TOPIC_OUT })
44 public class SplitterApplicationIT
46 public final static String TOPIC_IN = "in";
47 public final static String TOPIC_OUT = "out";
53 public static void testSendMessage(
54 @Autowired KafkaTemplate<String, TestInputRecording> kafkaTemplate)
62 SendResult<String, TestInputRecording> result = kafkaTemplate.send(TOPIC_IN, kv.key, kv.value).get();
64 "Sent: {}={}, partition={}, offset={}",
65 result.getProducerRecord().key(),
66 result.getProducerRecord().value(),
67 result.getRecordMetadata().partition(),
68 result.getRecordMetadata().offset());
72 throw new RuntimeException(e);
79 void testSendMessage() throws Exception
81 await("Expexted converted data")
82 .atMost(Duration.ofSeconds(5))
84 TestData.assertExpectedMessages(consumer.getReceivedMessages()));
90 private final MultiValueMap<String, TestOutputWord> received = new LinkedMultiValueMap<>();
92 @KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
93 public synchronized void receive(
94 @Header(KafkaHeaders.RECEIVED_KEY) String key,
95 @Payload TestOutputWord value)
97 log.debug("Received message: {}={}", key, value);
98 received.add(key, value);
101 synchronized MultiValueMap<String, TestOutputWord> getReceivedMessages()
109 static class Configuration
114 return new Consumer();