1 package de.juplo.kafka.wordcount.counter;
3 import com.fasterxml.jackson.databind.ObjectMapper;
4 import lombok.RequiredArgsConstructor;
5 import lombok.extern.slf4j.Slf4j;
6 import org.apache.kafka.clients.consumer.ConsumerRecord;
7 import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier;
8 import org.apache.kafka.streams.state.Stores;
9 import org.junit.jupiter.api.BeforeEach;
10 import org.junit.jupiter.api.Test;
11 import org.springframework.beans.factory.annotation.Autowired;
12 import org.springframework.boot.test.context.SpringBootTest;
13 import org.springframework.boot.test.context.TestConfiguration;
14 import org.springframework.context.annotation.Bean;
15 import org.springframework.context.annotation.Primary;
16 import org.springframework.kafka.annotation.KafkaListener;
17 import org.springframework.kafka.core.KafkaTemplate;
18 import org.springframework.kafka.test.context.EmbeddedKafka;
20 import java.time.Duration;
21 import java.util.LinkedList;
22 import java.util.List;
24 import static de.juplo.kafka.wordcount.counter.CounterApplicationIT.*;
25 import static org.awaitility.Awaitility.*;
30 "spring.kafka.bootstrap-servers=${spring.embedded.kafka.brokers}",
31 "juplo.wordcount.counter.bootstrap-server=${spring.embedded.kafka.brokers}",
32 "juplo.wordcount.counter.commit-interval=0",
33 "juplo.wordcount.counter.cacheMaxBytes=0",
34 "juplo.wordcount.counter.input-topic=" + TOPIC_IN,
35 "juplo.wordcount.counter.output-topic=" + TOPIC_OUT })
36 @EmbeddedKafka(topics = { TOPIC_IN, TOPIC_OUT }, partitions = PARTITIONS)
38 public class CounterApplicationIT
40 public final static String TOPIC_IN = "in";
41 public final static String TOPIC_OUT = "out";
42 static final int PARTITIONS = 2;
45 KafkaTemplate<String, String> kafkaTemplate;
55 consumer.received.clear();
60 void testSendMessage()
62 TestData.writeInputData((key, value) -> kafkaTemplate.send(TOPIC_IN, key, value));
64 await("Expexted converted data")
65 .atMost(Duration.ofSeconds(10))
66 .untilAsserted(() -> TestData.assertExpectedResult(consumer.received, mapper));
70 @RequiredArgsConstructor
73 private final List<Message> received = new LinkedList<>();
75 @KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
76 public void receive(ConsumerRecord<String, String> record)
78 log.debug("Received message: {}", record);
79 received.add(Message.of(record.key(),record.value()));
84 static class Configuration
89 return new Consumer();
94 KeyValueBytesStoreSupplier inMemoryStoreSupplier()
96 return Stores.inMemoryKeyValueStore("TEST-STORE");