--- /dev/null
+package de.juplo.kafka.wordcount.counter;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import lombok.RequiredArgsConstructor;
+import lombok.Value;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.kafka.clients.consumer.ConsumerRecord;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.test.context.TestConfiguration;
+import org.springframework.context.annotation.Bean;
+import org.springframework.kafka.annotation.KafkaListener;
+import org.springframework.kafka.core.KafkaTemplate;
+import org.springframework.kafka.test.context.EmbeddedKafka;
+import org.springframework.util.LinkedMultiValueMap;
+import org.springframework.util.MultiValueMap;
+
+import java.time.Duration;
+
+import static de.juplo.kafka.wordcount.counter.CounterApplicationIT.*;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.awaitility.Awaitility.*;
+
+
+@SpringBootTest(
+ properties = {
+ "spring.kafka.bootstrap-servers=${spring.embedded.kafka.brokers}",
+ "juplo.wordcount.counter.bootstrap-server=${spring.embedded.kafka.brokers}",
+ "juplo.wordcount.counter.input-topic=" + TOPIC_IN,
+ "juplo.wordcount.counter.output-topic=" + TOPIC_OUT })
+@EmbeddedKafka(topics = { TOPIC_IN, TOPIC_OUT }, partitions = PARTITIONS)
+@Slf4j
+public class CounterApplicationIT
+{
+ public final static String TOPIC_IN = "in";
+ public final static String TOPIC_OUT = "out";
+ static final int PARTITIONS = 2;
+
+ @Autowired
+ KafkaTemplate<String, String> kafkaTemplate;
+ @Autowired
+ ObjectMapper mapper;
+ @Autowired
+ Consumer consumer;
+
+
+ @BeforeEach
+ public void clear()
+ {
+ consumer.received.clear();
+ }
+
+
+ @Test
+ void testSendMessage() throws Exception
+ {
+ kafkaTemplate.send(TOPIC_IN, "peter", "Hallo");
+ kafkaTemplate.send(TOPIC_IN, "klaus", "Müsch");
+ kafkaTemplate.send(TOPIC_IN, "peter", "Welt");
+ kafkaTemplate.send(TOPIC_IN, "klaus", "Müsch");
+ kafkaTemplate.send(TOPIC_IN, "klaus", "s");
+ kafkaTemplate.send(TOPIC_IN, "peter", "Boäh");
+ kafkaTemplate.send(TOPIC_IN, "peter", "Welt");
+ kafkaTemplate.send(TOPIC_IN, "peter", "Boäh");
+ kafkaTemplate.send(TOPIC_IN, "klaus", "s");
+ kafkaTemplate.send(TOPIC_IN, "peter", "Boäh");
+ kafkaTemplate.send(TOPIC_IN, "klaus", "s");
+
+ Message peter1 = Message.of(
+ Key.of("peter", "Hallo"),
+ "1");
+ Message peter2 = Message.of(
+ Key.of("peter", "Welt"),
+ "1");
+ Message peter3 = Message.of(
+ Key.of("peter", "Boäh"),
+ "1");
+ Message peter4 = Message.of(
+ Key.of("peter", "Welt"),
+ "2");
+ Message peter5 = Message.of(
+ Key.of("peter", "Boäh"),
+ "2");
+ Message peter6 = Message.of(
+ Key.of("peter", "Boäh"),
+ "3");
+
+ Message klaus1 = Message.of(
+ Key.of("klaus", "Müsch"),
+ "1");
+ Message klaus2 = Message.of(
+ Key.of("klaus", "Müsch"),
+ "2");
+ Message klaus3 = Message.of(
+ Key.of("klaus", "s"),
+ "1");
+ Message klaus4 = Message.of(
+ Key.of("klaus", "s"),
+ "2");
+ Message klaus5 = Message.of(
+ Key.of("klaus", "s"),
+ "3");
+
+ await("Expexted converted data")
+ .atMost(Duration.ofSeconds(10))
+ .untilAsserted(() ->
+ {
+ assertThat(consumer.received).hasSize(2);
+ assertThat(consumer.received.get("klaus")).containsExactly(klaus1, klaus2, klaus3, klaus4, klaus5);
+ assertThat(consumer.received.get("peter")).containsExactly(peter1, peter2, peter3, peter4, peter5, peter6);
+ });
+ }
+
+
+ @RequiredArgsConstructor
+ static class Consumer
+ {
+ private final MultiValueMap<String, Message> received = new LinkedMultiValueMap<>();
+ private final ObjectMapper mapper;
+
+ @KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
+ public void receive(ConsumerRecord<String, String> record) throws JsonProcessingException
+ {
+ log.debug("Received message: {}", record);
+ Key key = mapper.readValue(record.key(), Key.class);
+ received.add(key.getUsername(), Message.of(key,record.value()));
+ }
+ }
+
+ @Value(staticConstructor = "of")
+ static class Message
+ {
+ Key key;
+ String value;
+ }
+
+ @TestConfiguration
+ static class Configuration
+ {
+ @Bean
+ Consumer consumer(ObjectMapper mapper)
+ {
+ return new Consumer(mapper);
+ }
+ }
+}