1 package de.juplo.kafka.wordcount.splitter;
3 import de.juplo.kafka.wordcount.counter.TestOutputUser;
4 import de.juplo.kafka.wordcount.counter.TestOutputWord;
5 import de.juplo.kafka.wordcount.recorder.TestInputRecording;
6 import de.juplo.kafka.wordcount.recorder.TestInputUser;
7 import lombok.extern.slf4j.Slf4j;
8 import org.junit.jupiter.api.BeforeAll;
9 import org.junit.jupiter.api.Test;
10 import org.springframework.beans.factory.annotation.Autowired;
11 import org.springframework.boot.test.context.SpringBootTest;
12 import org.springframework.boot.test.context.TestConfiguration;
13 import org.springframework.context.annotation.Bean;
14 import org.springframework.kafka.annotation.KafkaListener;
15 import org.springframework.kafka.core.KafkaTemplate;
16 import org.springframework.kafka.support.KafkaHeaders;
17 import org.springframework.kafka.support.SendResult;
18 import org.springframework.kafka.test.context.EmbeddedKafka;
19 import org.springframework.messaging.handler.annotation.Header;
20 import org.springframework.messaging.handler.annotation.Payload;
21 import org.springframework.util.LinkedMultiValueMap;
22 import org.springframework.util.MultiValueMap;
24 import java.time.Duration;
26 import static de.juplo.kafka.wordcount.splitter.SplitterApplicationIT.TOPIC_IN;
27 import static de.juplo.kafka.wordcount.splitter.SplitterApplicationIT.TOPIC_OUT;
28 import static org.awaitility.Awaitility.await;
33 "spring.kafka.producer.key-serializer=org.springframework.kafka.support.serializer.JsonSerializer",
34 "spring.kafka.producer.value-serializer=org.springframework.kafka.support.serializer.JsonSerializer",
35 "spring.kafka.producer.properties.spring.json.add.type.headers=false",
36 "spring.kafka.consumer.auto-offset-reset=earliest",
37 "spring.kafka.consumer.key-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer",
38 "spring.kafka.consumer.value-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer",
39 "spring.kafka.consumer.properties.spring.json.key.default.type=de.juplo.kafka.wordcount.counter.TestOutputUser",
40 "spring.kafka.consumer.properties.spring.json.value.default.type=de.juplo.kafka.wordcount.counter.TestOutputWord",
41 "logging.level.root=WARN",
42 "logging.level.de.juplo=DEBUG",
43 "juplo.wordcount.splitter.bootstrap-server=${spring.embedded.kafka.brokers}",
44 "juplo.wordcount.splitter.input-topic=" + TOPIC_IN,
45 "juplo.wordcount.splitter.output-topic=" + TOPIC_OUT })
46 @EmbeddedKafka(topics = { TOPIC_IN, TOPIC_OUT })
48 public class SplitterApplicationIT
50 public final static String TOPIC_IN = "in";
51 public final static String TOPIC_OUT = "out";
57 public static void testSendMessage(
58 @Autowired KafkaTemplate<TestInputUser, TestInputRecording> kafkaTemplate)
66 SendResult<TestInputUser, TestInputRecording> result = kafkaTemplate.send(TOPIC_IN, kv.key, kv.value).get();
68 "Sent: {}={}, partition={}, offset={}",
69 result.getProducerRecord().key(),
70 result.getProducerRecord().value(),
71 result.getRecordMetadata().partition(),
72 result.getRecordMetadata().offset());
76 throw new RuntimeException(e);
83 void testSendMessage() throws Exception
85 await("Expected converted data")
86 .atMost(Duration.ofSeconds(5))
88 TestData.assertExpectedMessages(consumer.getReceivedMessages()));
94 private final MultiValueMap<TestOutputUser, TestOutputWord> received = new LinkedMultiValueMap<>();
96 @KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
97 public synchronized void receive(
98 @Header(KafkaHeaders.RECEIVED_KEY) TestOutputUser key,
99 @Payload TestOutputWord value)
101 log.debug("Received message: {}={}", key, value);
102 received.add(key, value);
105 synchronized MultiValueMap<TestOutputUser, TestOutputWord> getReceivedMessages()
113 static class Configuration
118 return new Consumer();