counter: Implemented an integration test for the old message-format
authorKai Moritz <kai@juplo.de>
Sat, 11 Feb 2023 06:09:45 +0000 (07:09 +0100)
committerKai Moritz <kai@juplo.de>
Sat, 11 Feb 2023 12:59:34 +0000 (13:59 +0100)
pom.xml
src/main/java/de/juplo/kafka/wordcount/counter/CounterApplication.java
src/test/java/de/juplo/kafka/wordcount/counter/CounterApplicationIT.java [new file with mode: 0644]
src/test/resources/logback-test.xml [new file with mode: 0644]

diff --git a/pom.xml b/pom.xml
index 83f96cd..c7b09d7 100644 (file)
--- a/pom.xml
+++ b/pom.xml
                        <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>
index 1ee65ca..6a6a78c 100644 (file)
@@ -34,6 +34,9 @@ public class CounterApplication
                propertyMap.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, properties.getBootstrapServer());
                propertyMap.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.StringSerde.class.getName());
                propertyMap.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.StringSerde.class.getName());
+               propertyMap.put(StreamsConfig.STATE_DIR_CONFIG, "target");
+               propertyMap.put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG, 0);
+               propertyMap.put(StreamsConfig.CACHE_MAX_BYTES_BUFFERING_CONFIG, 0);
                propertyMap.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
 
                CounterStreamProcessor streamProcessor = new CounterStreamProcessor(
diff --git a/src/test/java/de/juplo/kafka/wordcount/counter/CounterApplicationIT.java b/src/test/java/de/juplo/kafka/wordcount/counter/CounterApplicationIT.java
new file mode 100644 (file)
index 0000000..3e47ebe
--- /dev/null
@@ -0,0 +1,149 @@
+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);
+               }
+       }
+}
diff --git a/src/test/resources/logback-test.xml b/src/test/resources/logback-test.xml
new file mode 100644 (file)
index 0000000..171bf63
--- /dev/null
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<configuration>
+    <include resource="org/springframework/boot/logging/logback/base.xml" />
+    <logger name="de.juplo.kafka.wordcount.counter" level="DEBUG" />
+</configuration>