1 package de.juplo.kafka.wordcount.counter;
3 import lombok.extern.slf4j.Slf4j;
4 import org.apache.kafka.clients.consumer.ConsumerRecord;
5 import org.apache.kafka.streams.KeyValue;
6 import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier;
7 import org.apache.kafka.streams.state.Stores;
8 import org.junit.jupiter.api.BeforeEach;
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.context.annotation.Primary;
15 import org.springframework.kafka.annotation.KafkaListener;
16 import org.springframework.kafka.core.KafkaTemplate;
17 import org.springframework.kafka.test.context.EmbeddedKafka;
19 import java.time.Duration;
20 import java.util.LinkedList;
21 import java.util.List;
23 import static de.juplo.kafka.wordcount.counter.CounterApplicationIT.TOPIC_IN;
24 import static de.juplo.kafka.wordcount.counter.CounterApplicationIT.TOPIC_OUT;
25 import static de.juplo.kafka.wordcount.counter.TestData.parseHeader;
26 import static org.awaitility.Awaitility.await;
27 import static org.springframework.kafka.support.mapping.AbstractJavaTypeMapper.DEFAULT_CLASSID_FIELD_NAME;
28 import static org.springframework.kafka.support.mapping.AbstractJavaTypeMapper.KEY_DEFAULT_CLASSID_FIELD_NAME;
33 "spring.kafka.producer.value-serializer=org.springframework.kafka.support.serializer.JsonSerializer",
34 "spring.kafka.producer.properties.spring.json.add.type.headers=false",
35 "spring.kafka.consumer.auto-offset-reset=earliest",
36 "spring.kafka.consumer.key-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer",
37 "spring.kafka.consumer.value-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer",
38 "spring.kafka.consumer.properties.spring.json.use.type.headers=false",
39 "spring.kafka.consumer.properties.spring.json.key.default.type=de.juplo.kafka.wordcount.counter.Word",
40 "spring.kafka.consumer.properties.spring.json.value.default.type=de.juplo.kafka.wordcount.counter.WordCounter",
41 "logging.level.root=WARN",
42 "logging.level.de.juplo=DEBUG",
43 "juplo.wordcount.counter.bootstrap-server=${spring.embedded.kafka.brokers}",
44 "juplo.wordcount.counter.commit-interval=0",
45 "juplo.wordcount.counter.cacheMaxBytes=0",
46 "juplo.wordcount.counter.input-topic=" + TOPIC_IN,
47 "juplo.wordcount.counter.output-topic=" + TOPIC_OUT })
48 @EmbeddedKafka(topics = { TOPIC_IN, TOPIC_OUT })
50 public class CounterApplicationIT
52 public static final String TOPIC_IN = "in";
53 public static final String TOPIC_OUT = "out";
56 KafkaTemplate<String, Word> kafkaTemplate;
64 consumer.received.clear();
69 void testSendMessage()
71 TestData.writeInputData((key, value) -> kafkaTemplate.send(TOPIC_IN, key, value));
73 await("Expexted converted data")
74 .atMost(Duration.ofSeconds(10))
75 .untilAsserted(() -> TestData.assertExpectedResult(consumer.getReceivedMessages()));
81 private final List<KeyValue<Word, WordCounter>> received = new LinkedList<>();
83 @KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
84 public synchronized void receive(ConsumerRecord<Word, WordCounter> record)
87 "Received message: {} -> {}, key: {}, value: {}",
90 parseHeader(record.headers(), KEY_DEFAULT_CLASSID_FIELD_NAME),
91 parseHeader(record.headers(), DEFAULT_CLASSID_FIELD_NAME));
92 received.add(KeyValue.pair(record.key(),record.value()));
95 synchronized List<KeyValue<Word, WordCounter>> getReceivedMessages()
102 static class Configuration
107 return new Consumer();
112 KeyValueBytesStoreSupplier inMemoryStoreSupplier()
114 return Stores.inMemoryKeyValueStore("TEST-STORE");