X-Git-Url: https://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fde%2Fjuplo%2Fkafka%2Fwordcount%2Fcounter%2FCounterApplicationIT.java;h=992164c3a040418838800b0c06a0c84bdbbed282;hb=a5146f975d5383dd2ec046478f20937d821dfa51;hp=fea89aba9b308f823571d798e2b798b29ec9c29c;hpb=4e10b6e106409b2884fcef67f7a5ded0581bc35f;p=demos%2Fkafka%2Fwordcount diff --git a/src/test/java/de/juplo/kafka/wordcount/counter/CounterApplicationIT.java b/src/test/java/de/juplo/kafka/wordcount/counter/CounterApplicationIT.java index fea89ab..992164c 100644 --- a/src/test/java/de/juplo/kafka/wordcount/counter/CounterApplicationIT.java +++ b/src/test/java/de/juplo/kafka/wordcount/counter/CounterApplicationIT.java @@ -1,10 +1,7 @@ package de.juplo.kafka.wordcount.counter; -import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.apache.kafka.clients.consumer.ConsumerConfig; import org.apache.kafka.clients.consumer.ConsumerRecord; -import org.apache.kafka.clients.producer.ProducerConfig; import org.apache.kafka.streams.KeyValue; import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier; import org.apache.kafka.streams.state.Stores; @@ -16,38 +13,44 @@ import org.springframework.boot.test.context.TestConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Primary; import org.springframework.kafka.annotation.KafkaListener; -import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory; -import org.springframework.kafka.core.*; -import org.springframework.kafka.support.serializer.JsonDeserializer; -import org.springframework.kafka.support.serializer.JsonSerializer; +import org.springframework.kafka.core.KafkaTemplate; import org.springframework.kafka.test.context.EmbeddedKafka; import java.time.Duration; import java.util.LinkedList; import java.util.List; -import java.util.Map; -import java.util.Properties; -import java.util.stream.Collectors; -import static de.juplo.kafka.wordcount.counter.CounterApplicationIT.*; -import static org.awaitility.Awaitility.*; +import static de.juplo.kafka.wordcount.counter.CounterApplicationIT.TOPIC_IN; +import static de.juplo.kafka.wordcount.counter.CounterApplicationIT.TOPIC_OUT; +import static de.juplo.kafka.wordcount.counter.TestData.parseHeader; +import static org.awaitility.Awaitility.await; +import static org.springframework.kafka.support.mapping.AbstractJavaTypeMapper.DEFAULT_CLASSID_FIELD_NAME; +import static org.springframework.kafka.support.mapping.AbstractJavaTypeMapper.KEY_DEFAULT_CLASSID_FIELD_NAME; @SpringBootTest( properties = { - "spring.kafka.bootstrap-servers=${spring.embedded.kafka.brokers}", + "spring.kafka.producer.value-serializer=org.springframework.kafka.support.serializer.JsonSerializer", + "spring.kafka.producer.properties.spring.json.add.type.headers=false", + "spring.kafka.consumer.auto-offset-reset=earliest", + "spring.kafka.consumer.key-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer", + "spring.kafka.consumer.value-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer", + "spring.kafka.consumer.properties.spring.json.use.type.headers=false", + "spring.kafka.consumer.properties.spring.json.key.default.type=de.juplo.kafka.wordcount.counter.Word", + "spring.kafka.consumer.properties.spring.json.value.default.type=de.juplo.kafka.wordcount.counter.WordCounter", + "logging.level.root=WARN", + "logging.level.de.juplo=DEBUG", "juplo.wordcount.counter.bootstrap-server=${spring.embedded.kafka.brokers}", "juplo.wordcount.counter.commit-interval=0", "juplo.wordcount.counter.cacheMaxBytes=0", "juplo.wordcount.counter.input-topic=" + TOPIC_IN, "juplo.wordcount.counter.output-topic=" + TOPIC_OUT }) -@EmbeddedKafka(topics = { TOPIC_IN, TOPIC_OUT }, partitions = PARTITIONS) +@EmbeddedKafka(topics = { TOPIC_IN, TOPIC_OUT }) @Slf4j public class CounterApplicationIT { - public final static String TOPIC_IN = "in"; - public final static String TOPIC_OUT = "out"; - static final int PARTITIONS = 2; + public static final String TOPIC_IN = "in"; + public static final String TOPIC_OUT = "out"; @Autowired KafkaTemplate kafkaTemplate; @@ -73,19 +76,23 @@ public class CounterApplicationIT } - @RequiredArgsConstructor static class Consumer { - private final List> received = new LinkedList<>(); + private final List> received = new LinkedList<>(); @KafkaListener(groupId = "TEST", topics = TOPIC_OUT) - public synchronized void receive(ConsumerRecord record) + public synchronized void receive(ConsumerRecord record) { - log.debug("Received message: {}", record); + log.debug( + "Received message: {} -> {}, key: {}, value: {}", + record.key(), + record.value(), + parseHeader(record.headers(), KEY_DEFAULT_CLASSID_FIELD_NAME), + parseHeader(record.headers(), DEFAULT_CLASSID_FIELD_NAME)); received.add(KeyValue.pair(record.key(),record.value())); } - synchronized List> getReceivedMessages() + synchronized List> getReceivedMessages() { return received; } @@ -94,59 +101,6 @@ public class CounterApplicationIT @TestConfiguration static class Configuration { - @Bean - ProducerFactory producerFactory(Properties streamProcessorProperties) - { - Map propertyMap = streamProcessorProperties - .entrySet() - .stream() - .collect( - Collectors.toMap( - entry -> (String)entry.getKey(), - entry -> entry.getValue() - )); - - propertyMap.put( - ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, - JsonSerializer.class.getName()); - propertyMap.put( - ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, - JsonSerializer.class.getName()); - - return new DefaultKafkaProducerFactory<>(propertyMap); - } - - @Bean - ConcurrentKafkaListenerContainerFactory kafkaListenerContainerFactory( - Properties streamProcessorProperties) - { - Map propertyMap = streamProcessorProperties - .entrySet() - .stream() - .collect( - Collectors.toMap( - entry -> (String)entry.getKey(), - entry -> entry.getValue() - )); - - propertyMap.put( - ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, - JsonDeserializer.class.getName()); - propertyMap.put( - ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, - JsonDeserializer.class.getName()); - - ConsumerFactory consumerFactory = - new DefaultKafkaConsumerFactory<>(propertyMap); - - ConcurrentKafkaListenerContainerFactory kafkaListenerContainerFactory = - new ConcurrentKafkaListenerContainerFactory<>(); - - kafkaListenerContainerFactory.setConsumerFactory(consumerFactory); - - return kafkaListenerContainerFactory; - } - @Bean Consumer consumer() {