1 package de.juplo.kafka.wordcount.counter;
3 import lombok.RequiredArgsConstructor;
4 import lombok.extern.slf4j.Slf4j;
5 import org.apache.kafka.clients.consumer.ConsumerRecord;
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.*;
24 import static org.awaitility.Awaitility.*;
29 "spring.kafka.bootstrap-servers=${spring.embedded.kafka.brokers}",
30 "juplo.wordcount.counter.bootstrap-server=${spring.embedded.kafka.brokers}",
31 "juplo.wordcount.counter.commit-interval=0",
32 "juplo.wordcount.counter.cacheMaxBytes=0",
33 "juplo.wordcount.counter.input-topic=" + TOPIC_IN,
34 "juplo.wordcount.counter.output-topic=" + TOPIC_OUT })
35 @EmbeddedKafka(topics = { TOPIC_IN, TOPIC_OUT }, partitions = PARTITIONS)
37 public class CounterApplicationIT
39 public final static String TOPIC_IN = "in";
40 public final static String TOPIC_OUT = "out";
41 static final int PARTITIONS = 2;
44 KafkaTemplate<String, String> kafkaTemplate;
52 consumer.received.clear();
57 void testSendMessage()
59 TestData.writeInputData((key, value) -> kafkaTemplate.send(TOPIC_IN, key, value));
61 await("Expexted converted data")
62 .atMost(Duration.ofSeconds(10))
63 .untilAsserted(() -> TestData.assertExpectedResult(consumer.getReceivedMessages()));
67 @RequiredArgsConstructor
70 private final List<Message> received = new LinkedList<>();
72 @KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
73 public synchronized void receive(ConsumerRecord<String, String> record)
75 log.debug("Received message: {}", record);
76 received.add(Message.of(record.key(),record.value()));
79 synchronized List<Message> getReceivedMessages()
86 static class Configuration
91 return new Consumer();
96 KeyValueBytesStoreSupplier inMemoryStoreSupplier()
98 return Stores.inMemoryKeyValueStore("TEST-STORE");