1 package de.juplo.kafka.wordcount.splitter;
3 import de.juplo.kafka.wordcount.counter.TestWord;
4 import de.juplo.kafka.wordcount.recorder.TestRecording;
5 import lombok.extern.slf4j.Slf4j;
6 import org.junit.jupiter.api.BeforeEach;
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.test.context.EmbeddedKafka;
16 import org.springframework.messaging.handler.annotation.Header;
17 import org.springframework.messaging.handler.annotation.Payload;
18 import org.springframework.util.LinkedMultiValueMap;
19 import org.springframework.util.MultiValueMap;
21 import java.time.Duration;
23 import static de.juplo.kafka.wordcount.splitter.SplitterApplicationIT.*;
24 import static org.awaitility.Awaitility.await;
29 "spring.kafka.producer.value-serializer=org.springframework.kafka.support.serializer.JsonSerializer",
30 "spring.kafka.producer.properties.spring.json.add.type.headers=false",
31 "spring.kafka.consumer.auto-offset-reset=earliest",
32 "spring.kafka.consumer.value-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer",
33 "spring.kafka.consumer.properties.spring.json.value.default.type=de.juplo.kafka.wordcount.counter.TestWord",
34 "spring.kafka.consumer.properties.spring.json.trusted.packages=de.juplo.kafka.wordcount.splitter",
35 "logging.level.root=WARN",
36 "logging.level.de.juplo=DEBUG",
37 "juplo.wordcount.splitter.bootstrap-server=${spring.embedded.kafka.brokers}",
38 "juplo.wordcount.splitter.input-topic=" + TOPIC_IN,
39 "juplo.wordcount.splitter.output-topic=" + TOPIC_OUT })
40 @EmbeddedKafka(topics = { TOPIC_IN, TOPIC_OUT })
42 public class SplitterApplicationIT
44 public final static String TOPIC_IN = "in";
45 public final static String TOPIC_OUT = "out";
48 KafkaTemplate<String, TestRecording> kafkaTemplate;
56 consumer.received.clear();
60 void testSendMessage() throws Exception
62 TestData.writeInputData((user, recording) -> kafkaTemplate.send(TOPIC_IN, user, recording));
64 await("Expexted converted data")
65 .atMost(Duration.ofSeconds(5))
67 TestData.assertExpectedMessages(consumer.getReceivedMessages()));
73 private final MultiValueMap<String, TestWord> received = new LinkedMultiValueMap<>();
75 @KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
76 public synchronized void receive(
77 @Header(KafkaHeaders.RECEIVED_KEY) String key,
78 @Payload TestWord value)
80 log.debug("Received message: {}={}", key, value);
81 received.add(key, value);
84 synchronized MultiValueMap<String, TestWord> getReceivedMessages()
92 static class Configuration
97 return new Consumer();