package de.juplo.kafka.wordcount.top10;
+import de.juplo.kafka.wordcount.counter.TestCounter;
+import de.juplo.kafka.wordcount.counter.TestWord;
+import de.juplo.kafka.wordcount.query.TestRanking;
+import de.juplo.kafka.wordcount.query.TestStats;
import lombok.extern.slf4j.Slf4j;
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.KeyValueStore;
+import org.apache.kafka.streams.state.Stores;
+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.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 java.util.stream.Stream;
-import static de.juplo.kafka.wordcount.top10.TestData.convertToMap;
-import static de.juplo.kafka.wordcount.top10.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.top10.Top10ApplicationConfiguration.serializationConfig;
+import static de.juplo.kafka.wordcount.top10.Top10StreamProcessor.STORE_NAME;
@Slf4j
public class Top10StreamProcessorTopologyTest
{
- public final static String IN = "TEST-IN";
- public final static String OUT = "TEST-OUT";
+ public static final String IN = "TEST-IN";
+ public static final String OUT = "TEST-OUT";
- @Test
- public void test()
- {
- Topology topology = Top10StreamProcessor.buildTopology(IN, OUT);
+ static TopologyTestDriver testDriver;
+ static MultiValueMap<TestStats, TestRanking> receivedMessages = new LinkedMultiValueMap<>();
- Top10ApplicationConfiguration applicationConfiguriation =
- new Top10ApplicationConfiguration();
- Properties streamProcessorProperties =
- applicationConfiguriation.streamProcessorProperties(new Top10ApplicationProperties());
- Map<String, Object> propertyMap = convertToMap(streamProcessorProperties);
- JsonSerde<?> keySerde = new JsonSerde<>();
- keySerde.configure(propertyMap, true);
- JsonSerde<?> valueSerde = new JsonSerde<>();
- valueSerde.configure(propertyMap, false);
+ @BeforeAll
+ public static void setUp()
+ {
+ Topology topology = Top10StreamProcessor.buildTopology(
+ IN,
+ OUT,
+ Stores.inMemoryKeyValueStore(STORE_NAME));
- TopologyTestDriver testDriver = new TopologyTestDriver(topology, streamProcessorProperties);
+ testDriver = new TopologyTestDriver(topology, serializationConfig());
- TestInputTopic<Key, Entry> in = testDriver.createInputTopic(
+ TestInputTopic<TestWord, TestCounter> in = testDriver.createInputTopic(
IN,
- (JsonSerializer<Key>)keySerde.serializer(),
- (JsonSerializer<Entry>)valueSerde.serializer());
+ jsonSerializer(TestWord.class, true),
+ jsonSerializer(TestCounter.class,false));
- TestOutputTopic<String, Ranking> out = testDriver.createOutputTopic(
+ TestOutputTopic<TestStats, TestRanking> out = testDriver.createOutputTopic(
OUT,
- (JsonDeserializer<String>)keySerde.deserializer(),
- (JsonDeserializer<Ranking>)valueSerde.deserializer());
+ new JsonDeserializer()
+ .copyWithType(TestStats.class)
+ .ignoreTypeHeaders(),
+ new JsonDeserializer()
+ .copyWithType(TestRanking.class)
+ .ignoreTypeHeaders());
- Stream
- .of(TestData.INPUT_MESSAGES)
- .forEach(kv -> in.pipeInput(
- Key.of(kv.key.getUser(), kv.key.getWord()),
- Entry.of(kv.value.getWord(), kv.value.getCounter())));
+ TestData
+ .getInputMessages()
+ .forEach(kv -> in.pipeInput(kv.key, kv.value));
- MultiValueMap<String, Ranking> 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()));
+ }
+
+ @DisplayName("Assert the expected output messages")
+ @Test
+ public void testExpectedMessages()
+ {
TestData.assertExpectedMessages(receivedMessages);
+ }
+
+ @DisplayName("Assert the expected number of messages")
+ @Test
+ public void testExpectedNumberOfMessages()
+ {
+ TestData.assertExpectedNumberOfMessages(receivedMessages);
+ }
+ @DisplayName("Assert the expected final output messages")
+ @Test
+ public void testExpectedLastMessages()
+ {
+ TestData.assertExpectedLastMessages(receivedMessages);
+ }
+
+ @DisplayName("Assert the expected state in the state-store")
+ @Test
+ public void testExpectedState()
+ {
+ KeyValueStore<Stats, Ranking> store = testDriver.getKeyValueStore(STORE_NAME);
+ TestData.assertExpectedState(store);
+ }
+
+ @AfterAll
+ public static void tearDown()
+ {
testDriver.close();
}
+
+ private static <T> JsonSerializer<T> jsonSerializer(Class<T> type, boolean isKey)
+ {
+ JsonSerializer<T> jsonSerializer = new JsonSerializer<>();
+ jsonSerializer.configure(
+ Map.of(
+ JsonSerializer.TYPE_MAPPINGS,
+ "key:" + TestWord.class.getName() + "," +
+ "counter:" + TestCounter.class.getName()),
+ isKey);
+ return jsonSerializer;
+ }
}