</parent>
<groupId>de.juplo.kafka.wordcount</groupId>
<artifactId>counter</artifactId>
- <version>1.1.2</version>
+ <version>1.1.3</version>
<name>Wordcount-Counter</name>
<description>Word-counting stream-processor of the multi-user wordcount-example</description>
<properties>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-streams</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.springframework.kafka</groupId>
+ <artifactId>spring-kafka</artifactId>
+ </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
+
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.springframework.kafka</groupId>
+ <artifactId>spring-kafka-test</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.awaitility</groupId>
+ <artifactId>awaitility</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.assertj</groupId>
+ <artifactId>assertj-core</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>
<plugins>
+ <plugin>
+ <artifactId>maven-failsafe-plugin</artifactId>
+ </plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
--- /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(
+ "{\"username\":\"peter\",\"word\":\"Hallo\"}",
+ "1");
+ Message peter2 = Message.of(
+ "{\"username\":\"peter\",\"word\":\"Welt\"}",
+ "1");
+ Message peter3 = Message.of(
+ "{\"username\":\"peter\",\"word\":\"Boäh\"}",
+ "1");
+ Message peter4 = Message.of(
+ "{\"username\":\"peter\",\"word\":\"Welt\"}",
+ "2");
+ Message peter5 = Message.of(
+ "{\"username\":\"peter\",\"word\":\"Boäh\"}",
+ "2");
+ Message peter6 = Message.of(
+ "{\"username\":\"peter\",\"word\":\"Boäh\"}",
+ "3");
+
+ Message klaus1 = Message.of(
+ "{\"username\":\"klaus\",\"word\":\"Müsch\"}",
+ "1");
+ Message klaus2 = Message.of(
+ "{\"username\":\"klaus\",\"word\":\"Müsch\"}",
+ "2");
+ Message klaus3 = Message.of(
+ "{\"username\":\"klaus\",\"word\":\"s\"}",
+ "1");
+ Message klaus4 = Message.of(
+ "{\"username\":\"klaus\",\"word\":\"s\"}",
+ "2");
+ Message klaus5 = Message.of(
+ "{\"username\":\"klaus\",\"word\":\"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(record.key(),record.value()));
+ }
+ }
+
+ @Value(staticConstructor = "of")
+ static class Message
+ {
+ String key;
+ String value;
+ }
+
+ @TestConfiguration
+ static class Configuration
+ {
+ @Bean
+ Consumer consumer(ObjectMapper mapper)
+ {
+ return new Consumer(mapper);
+ }
+ }
+}