From: Kai Moritz Date: Sat, 22 Jun 2024 15:53:34 +0000 (+0200) Subject: top10: 1.2.1 - Refined `Top10StreamProcessorTopologyTest` (splitted tests) X-Git-Tag: top10-1.3.0~1 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=066ac7e345947764b28084b9961b1633264d63d0;p=demos%2Fkafka%2Fwordcount top10: 1.2.1 - Refined `Top10StreamProcessorTopologyTest` (splitted tests) --- diff --git a/src/test/java/de/juplo/kafka/wordcount/top10/Top10StreamProcessorTopologyTest.java b/src/test/java/de/juplo/kafka/wordcount/top10/Top10StreamProcessorTopologyTest.java index 90d8e4c..cca9a3a 100644 --- a/src/test/java/de/juplo/kafka/wordcount/top10/Top10StreamProcessorTopologyTest.java +++ b/src/test/java/de/juplo/kafka/wordcount/top10/Top10StreamProcessorTopologyTest.java @@ -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 receivedMessages = new LinkedMultiValueMap<>(); - TopologyTestDriver testDriver; - TestInputTopic in; - TestOutputTopic 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 in = testDriver.createInputTopic( IN, jsonSerializer(TestWord.class, true), jsonSerializer(TestCounter.class,false)); - out = testDriver.createOutputTopic( + TestOutputTopic 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 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 store = testDriver.getKeyValueStore(STORE_NAME); TestData.assertExpectedState(store); } - @AfterEach - public void tearDown() + @AfterAll + public static void tearDown() { testDriver.close(); } - private JsonSerializer jsonSerializer(Class type, boolean isKey) + private static JsonSerializer jsonSerializer(Class type, boolean isKey) { JsonSerializer jsonSerializer = new JsonSerializer<>(); jsonSerializer.configure(