counter: 1.2.13 - The tests print out the type-mapping headers
[demos/kafka/wordcount] / src / test / java / de / juplo / kafka / wordcount / counter / CounterStreamProcessorTopologyTest.java
index 902e93f..ca2664e 100644 (file)
@@ -1,19 +1,24 @@
 package de.juplo.kafka.wordcount.counter;
 
-import org.apache.kafka.common.serialization.StringDeserializer;
-import org.apache.kafka.common.serialization.StringSerializer;
-import org.apache.kafka.streams.TestInputTopic;
-import org.apache.kafka.streams.TestOutputTopic;
-import org.apache.kafka.streams.Topology;
-import org.apache.kafka.streams.TopologyTestDriver;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.kafka.streams.*;
 import org.apache.kafka.streams.state.Stores;
 import org.junit.jupiter.api.Test;
+import org.springframework.kafka.support.serializer.JsonDeserializer;
+import org.springframework.kafka.support.serializer.JsonSerde;
 import org.springframework.kafka.support.serializer.JsonSerializer;
 
 import java.util.List;
+import java.util.Map;
 import java.util.Properties;
 
+import static de.juplo.kafka.wordcount.counter.TestData.convertToMap;
+import static de.juplo.kafka.wordcount.counter.TestData.parseHeader;
+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;
 
+
+@Slf4j
 public class CounterStreamProcessorTopologyTest
 {
   public final static String IN = "TEST-IN";
@@ -31,25 +36,40 @@ public class CounterStreamProcessorTopologyTest
         new CounterApplicationConfiguriation();
     Properties streamProcessorProperties =
         applicationConfiguriation.streamProcessorProperties(new CounterApplicationProperties());
+    Map<String, Object> propertyMap = convertToMap(streamProcessorProperties);
+
+    JsonSerde<?> keySerde = new JsonSerde<>();
+    keySerde.configure(propertyMap, true);
+    JsonSerde<?> valueSerde = new JsonSerde<>();
+    valueSerde.configure(propertyMap, false);
 
     TopologyTestDriver testDriver = new TopologyTestDriver(topology, streamProcessorProperties);
 
     TestInputTopic<String, Word> in = testDriver.createInputTopic(
         IN,
-        new JsonSerializer<>(),
-        new JsonSerializer<>());
+        (JsonSerializer<String>)keySerde.serializer(),
+        (JsonSerializer<Word>)valueSerde.serializer());
 
-    TestOutputTopic<String, String> out = testDriver.createOutputTopic(
+    TestOutputTopic<Word, WordCounter> out = testDriver.createOutputTopic(
         OUT,
-        new StringDeserializer(),
-        new StringDeserializer());
+        (JsonDeserializer<Word>)keySerde.deserializer(),
+        (JsonDeserializer<WordCounter>)valueSerde.deserializer());
 
     TestData.writeInputData((key, value) -> in.pipeInput(key, value));
 
-    List<Message> receivedMessages = out
+    List<KeyValue<Word, WordCounter>> receivedMessages = out
         .readRecordsToList()
         .stream()
-        .map(record -> Message.of(record.key(), record.value()))
+        .map(record ->
+        {
+          log.debug(
+              "OUT: {} -> {}, {}, {}",
+              record.key(),
+              record.value(),
+              parseHeader(record.headers(), KEY_DEFAULT_CLASSID_FIELD_NAME),
+              parseHeader(record.headers(), DEFAULT_CLASSID_FIELD_NAME));
+          return KeyValue.pair(record.key(), record.value());
+        })
         .toList();
 
     TestData.assertExpectedResult(receivedMessages);