From: Kai Moritz Date: Sat, 11 Feb 2023 06:09:45 +0000 (+0100) Subject: counter: Implemented an integration test for the old message-format X-Git-Tag: counter-tests-instances~9 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=9b66fb28d8b140694b9bdbd245e89bffb9f00726;p=demos%2Fkafka%2Fwordcount counter: Implemented an integration test for the old message-format --- diff --git a/pom.xml b/pom.xml index 83f96cd..c7b09d7 100644 --- a/pom.xml +++ b/pom.xml @@ -29,6 +29,10 @@ org.apache.kafka kafka-streams + + org.springframework.kafka + spring-kafka + org.springframework.boot @@ -46,11 +50,27 @@ lombok true + org.springframework.boot spring-boot-starter-test test + + org.springframework.kafka + spring-kafka-test + test + + + org.awaitility + awaitility + test + + + org.assertj + assertj-core + test + diff --git a/src/main/java/de/juplo/kafka/wordcount/counter/CounterApplication.java b/src/main/java/de/juplo/kafka/wordcount/counter/CounterApplication.java index 1ee65ca..6a6a78c 100644 --- a/src/main/java/de/juplo/kafka/wordcount/counter/CounterApplication.java +++ b/src/main/java/de/juplo/kafka/wordcount/counter/CounterApplication.java @@ -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 index 0000000..3e47ebe --- /dev/null +++ b/src/test/java/de/juplo/kafka/wordcount/counter/CounterApplicationIT.java @@ -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 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 received = new LinkedMultiValueMap<>(); + private final ObjectMapper mapper; + + @KafkaListener(groupId = "TEST", topics = TOPIC_OUT) + public void receive(ConsumerRecord 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 index 0000000..171bf63 --- /dev/null +++ b/src/test/resources/logback-test.xml @@ -0,0 +1,5 @@ + + + + +