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;
public static final String OUT = "TEST-OUT";
- TopologyTestDriver testDriver;
- TestInputTopic<TestInputUser, TestInputWord> in;
- TestOutputTopic<TestOutputWord, TestOutputWordCounter> out;
+ static TopologyTestDriver testDriver;
+ static MultiValueMap<TestOutputWord, TestOutputWordCounter> receivedMessages = new LinkedMultiValueMap<>();
- @BeforeEach
- public void setUpTestDriver()
+ @BeforeAll
+ public static void setUpTestDriver()
{
Topology topology = CounterStreamProcessor.buildTopology(
IN,
testDriver = new TopologyTestDriver(topology, serializationConfig());
- in = testDriver.createInputTopic(IN, serializer(), serializer());
- out = testDriver.createOutputTopic(OUT, keyDeserializer(), valueDeserializer());
- }
-
+ TestInputTopic<TestInputUser, TestInputWord> in =
+ testDriver.createInputTopic(IN, serializer(), serializer());
+ TestOutputTopic<TestOutputWord, TestOutputWordCounter> out =
+ testDriver.createOutputTopic(OUT, keyDeserializer(), valueDeserializer());
- @Test
- public void test()
- {
TestData
.getInputMessages()
.forEach(kv -> in.pipeInput(kv.key, kv.value));
- MultiValueMap<TestOutputWord, TestOutputWordCounter> receivedMessages = new LinkedMultiValueMap<>();
+ 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 testExpectedNumberOfMessagesForWord()
+ {
TestData.assertExpectedNumberOfMessagesForWord(receivedMessages);
+ }
+
+ @DisplayName("Await the expected final output messages")
+ @Test
+ public void testExpectedLastMessagesForWord()
+ {
TestData.assertExpectedLastMessagesForWord(receivedMessages);
+ }
+ @DisplayName("Assert the expected state in the state-store")
+ @Test
+ public void testExpectedState()
+ {
KeyValueStore<Word, Long> store = testDriver.getKeyValueStore(STORE_NAME);
TestData.assertExpectedState(store);
}
- @AfterEach
- public void tearDown()
+ @AfterAll
+ public static void tearDown()
{
testDriver.close();
}
return new JsonSerializer().noTypeInfo();
}
- private JsonDeserializer<TestOutputWord> keyDeserializer()
+ private static JsonDeserializer<TestOutputWord> keyDeserializer()
{
return deserializer(true);
}