counter: 1.2.9 - Reconfigured tests to receive data as domain-instances
[demos/kafka/wordcount] / src / test / java / de / juplo / kafka / wordcount / counter / CounterApplicationIT.java
index 559b171..158c6ae 100644 (file)
@@ -2,6 +2,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.state.KeyValueBytesStoreSupplier;
@@ -14,9 +15,9 @@ 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.core.DefaultKafkaProducerFactory;
-import org.springframework.kafka.core.KafkaTemplate;
-import org.springframework.kafka.core.ProducerFactory;
+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.test.context.EmbeddedKafka;
 
@@ -77,7 +78,7 @@ public class CounterApplicationIT
                private final List<Message> received = new LinkedList<>();
 
                @KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
-               public synchronized void receive(ConsumerRecord<String, String> record)
+               public synchronized void receive(ConsumerRecord<Word, WordCount> record)
                {
                        log.debug("Received message: {}", record);
                        received.add(Message.of(record.key(),record.value()));
@@ -114,6 +115,37 @@ public class CounterApplicationIT
                        return new DefaultKafkaProducerFactory<>(propertyMap);
                }
 
+               @Bean
+               ConcurrentKafkaListenerContainerFactory<?, ?> kafkaListenerContainerFactory(
+                               Properties streamProcessorProperties)
+               {
+                       Map<String, Object> 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<? super Object, ? super Object> consumerFactory =
+                                       new DefaultKafkaConsumerFactory<>(propertyMap);
+
+                       ConcurrentKafkaListenerContainerFactory<Object, Object> kafkaListenerContainerFactory =
+                                       new ConcurrentKafkaListenerContainerFactory<>();
+
+                       kafkaListenerContainerFactory.setConsumerFactory(consumerFactory);
+
+                       return kafkaListenerContainerFactory;
+               }
+
                @Bean
                Consumer consumer()
                {