counter: 1.2.15 - Removed logging of type-headers in tests
[demos/kafka/wordcount] / src / test / java / de / juplo / kafka / wordcount / counter / CounterStreamProcessorTopologyTest.java
index 16cf5d2..955d7a0 100644 (file)
@@ -1,25 +1,24 @@
 package de.juplo.kafka.wordcount.counter;
 
+import de.juplo.kafka.wordcount.splitter.TestInputWord;
+import de.juplo.kafka.wordcount.top10.TestOutputWord;
+import de.juplo.kafka.wordcount.top10.TestOutputWordCounter;
 import lombok.extern.slf4j.Slf4j;
+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 org.apache.kafka.streams.state.Stores;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
 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 org.springframework.util.LinkedMultiValueMap;
 import org.springframework.util.MultiValueMap;
 
-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;
+import static de.juplo.kafka.wordcount.counter.CounterApplicationConfiguriation.serializationConfig;
 
 
 @Slf4j
@@ -28,53 +27,56 @@ public class CounterStreamProcessorTopologyTest
   public final static String IN = "TEST-IN";
   public final static String OUT = "TEST-OUT";
 
-  @Test
-  public void test()
+
+  TopologyTestDriver testDriver;
+  TestInputTopic<String, TestInputWord> in;
+  TestOutputTopic<TestOutputWord, TestOutputWordCounter> out;
+
+
+  @BeforeEach
+  public void setUpTestDriver()
   {
     Topology topology = CounterStreamProcessor.buildTopology(
         IN,
         OUT,
         Stores.inMemoryKeyValueStore("TOPOLOGY-TEST"));
 
-    CounterApplicationConfiguriation applicationConfiguriation =
-        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);
+    testDriver = new TopologyTestDriver(topology, serializationConfig());
 
-    TestInputTopic<String, Word> in = testDriver.createInputTopic(
+    in = testDriver.createInputTopic(
         IN,
-        (JsonSerializer<String>)keySerde.serializer(),
-        (JsonSerializer<Word>)valueSerde.serializer());
+        new StringSerializer(),
+        new JsonSerializer().noTypeInfo());
 
-    TestOutputTopic<Word, WordCounter> out = testDriver.createOutputTopic(
+    out = testDriver.createOutputTopic(
         OUT,
-        (JsonDeserializer<Word>)keySerde.deserializer(),
-        (JsonDeserializer<WordCounter>)valueSerde.deserializer());
+        new JsonDeserializer()
+            .copyWithType(TestOutputWord.class)
+            .ignoreTypeHeaders(),
+        new JsonDeserializer()
+            .copyWithType(TestOutputWordCounter.class)
+            .ignoreTypeHeaders());
+  }
+
 
-    TestData.injectInputMessages((key, value) -> in.pipeInput(key, value));
+  @Test
+  public void test()
+  {
+    TestData
+        .getInputMessages()
+        .forEach(word -> in.pipeInput(word.getUser(), word));
 
-    MultiValueMap<Word, WordCounter> receivedMessages = new LinkedMultiValueMap<>();
+    MultiValueMap<TestOutputWord, TestOutputWordCounter> receivedMessages = new LinkedMultiValueMap<>();
     out
         .readRecordsToList()
-        .forEach(record ->
-        {
-          log.debug(
-              "OUT: {} -> {}, {}, {}",
-              record.key(),
-              record.value(),
-              parseHeader(record.headers(), KEY_DEFAULT_CLASSID_FIELD_NAME),
-              parseHeader(record.headers(), DEFAULT_CLASSID_FIELD_NAME));
-          receivedMessages.add(record.key(), record.value());
-        });
+        .forEach(record -> receivedMessages.add(record.key(), record.value()));
 
     TestData.assertExpectedMessages(receivedMessages);
   }
+
+  @AfterEach
+  public void tearDown()
+  {
+    testDriver.close();
+  }
 }