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;
import java.util.Map;
import static de.juplo.kafka.wordcount.top10.Top10ApplicationConfiguration.serializationConfig;
+import static de.juplo.kafka.wordcount.top10.Top10StreamProcessor.STORE_NAME;
@Slf4j
{
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,
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)
.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(