top10: 1.2.1 - Refined `Top10StreamProcessorTopologyTest` (splitted tests)
authorKai Moritz <kai@juplo.de>
Sat, 22 Jun 2024 15:53:34 +0000 (17:53 +0200)
committerKai Moritz <kai@juplo.de>
Sat, 22 Jun 2024 16:10:53 +0000 (18:10 +0200)
src/test/java/de/juplo/kafka/wordcount/top10/Top10StreamProcessorTopologyTest.java

index 90d8e4c..cca9a3a 100644 (file)
@@ -11,8 +11,9 @@ import org.apache.kafka.streams.Topology;
 import org.apache.kafka.streams.TopologyTestDriver;
 import org.apache.kafka.streams.state.KeyValueStore;
 import org.apache.kafka.streams.state.Stores;
-import org.junit.jupiter.api.AfterEach;
-import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.DisplayName;
 import org.junit.jupiter.api.Test;
 import org.springframework.kafka.support.serializer.JsonDeserializer;
 import org.springframework.kafka.support.serializer.JsonSerializer;
@@ -22,6 +23,7 @@ import org.springframework.util.MultiValueMap;
 import java.util.Map;
 
 import static de.juplo.kafka.wordcount.top10.Top10ApplicationConfiguration.serializationConfig;
+import static de.juplo.kafka.wordcount.top10.Top10StreamProcessor.STORE_NAME;
 
 
 @Slf4j
@@ -29,16 +31,13 @@ public class Top10StreamProcessorTopologyTest
 {
   public static final String IN = "TEST-IN";
   public static final String OUT = "TEST-OUT";
-  public static final String STORE_NAME = "TOPOLOGY-TEST";
 
+  static TopologyTestDriver testDriver;
+  static MultiValueMap<TestUser, TestRanking> receivedMessages = new LinkedMultiValueMap<>();
 
-  TopologyTestDriver testDriver;
-  TestInputTopic<TestWord, TestCounter> in;
-  TestOutputTopic<TestUser, TestRanking> out;
 
-
-  @BeforeEach
-  public void setUp()
+  @BeforeAll
+  public static void setUp()
   {
     Topology topology = Top10StreamProcessor.buildTopology(
         IN,
@@ -47,12 +46,12 @@ public class Top10StreamProcessorTopologyTest
 
     testDriver = new TopologyTestDriver(topology, serializationConfig());
 
-    in = testDriver.createInputTopic(
+    TestInputTopic<TestWord, TestCounter> in = testDriver.createInputTopic(
         IN,
         jsonSerializer(TestWord.class, true),
         jsonSerializer(TestCounter.class,false));
 
-    out = testDriver.createOutputTopic(
+    TestOutputTopic<TestUser, TestRanking> out = testDriver.createOutputTopic(
         OUT,
         new JsonDeserializer()
             .copyWithType(TestUser.class)
@@ -61,37 +60,52 @@ public class Top10StreamProcessorTopologyTest
             .copyWithType(TestRanking.class)
             .ignoreTypeHeaders());
 
-  }
-
-
-  @Test
-  public void test()
-  {
     TestData
         .getInputMessages()
         .forEach(kv -> in.pipeInput(kv.key, kv.value));
 
-    MultiValueMap<TestUser, TestRanking> receivedMessages = new LinkedMultiValueMap<>();
     out
         .readRecordsToList()
         .forEach(record -> receivedMessages.add(record.key(), record.value()));
+  }
+
 
+  @DisplayName("Assert the expected output messages")
+  @Test
+  public void testExpectedMessages()
+  {
     TestData.assertExpectedMessages(receivedMessages);
+  }
 
+  @DisplayName("Assert the expected number of messages")
+  @Test
+  public void testExpectedNumberOfMessagesForUsers()
+  {
     TestData.assertExpectedNumberOfMessagesForUsers(receivedMessages);
+  }
+
+  @DisplayName("Assert the expected final output messages")
+  @Test
+  public void testExpectedLastMessagesForUSers()
+  {
     TestData.assertExpectedLastMessagesForUsers(receivedMessages);
+  }
 
+  @DisplayName("Assert the expected state in the state-store")
+  @Test
+  public void testExpectedState()
+  {
     KeyValueStore<User, Ranking> store = testDriver.getKeyValueStore(STORE_NAME);
     TestData.assertExpectedState(store);
   }
 
-  @AfterEach
-  public void tearDown()
+  @AfterAll
+  public static void tearDown()
   {
     testDriver.close();
   }
 
-  private <T> JsonSerializer<T> jsonSerializer(Class<T> type, boolean isKey)
+  private static <T> JsonSerializer<T> jsonSerializer(Class<T> type, boolean isKey)
   {
     JsonSerializer<T> jsonSerializer = new JsonSerializer<>();
     jsonSerializer.configure(