1 package de.juplo.kafka.wordcount.counter;
3 import lombok.extern.slf4j.Slf4j;
4 import org.apache.kafka.streams.*;
5 import org.apache.kafka.streams.state.Stores;
6 import org.junit.jupiter.api.Test;
7 import org.springframework.kafka.support.serializer.JsonDeserializer;
8 import org.springframework.kafka.support.serializer.JsonSerde;
9 import org.springframework.kafka.support.serializer.JsonSerializer;
11 import java.util.List;
13 import java.util.Properties;
15 import static de.juplo.kafka.wordcount.counter.TestData.convertToMap;
16 import static de.juplo.kafka.wordcount.counter.TestData.parseHeader;
17 import static org.springframework.kafka.support.mapping.AbstractJavaTypeMapper.DEFAULT_CLASSID_FIELD_NAME;
18 import static org.springframework.kafka.support.mapping.AbstractJavaTypeMapper.KEY_DEFAULT_CLASSID_FIELD_NAME;
22 public class CounterStreamProcessorTopologyTest
24 public final static String IN = "TEST-IN";
25 public final static String OUT = "TEST-OUT";
30 Topology topology = CounterStreamProcessor.buildTopology(
33 Stores.inMemoryKeyValueStore("TOPOLOGY-TEST"));
35 CounterApplicationConfiguriation applicationConfiguriation =
36 new CounterApplicationConfiguriation();
37 Properties streamProcessorProperties =
38 applicationConfiguriation.streamProcessorProperties(new CounterApplicationProperties());
39 Map<String, Object> propertyMap = convertToMap(streamProcessorProperties);
41 JsonSerde<?> keySerde = new JsonSerde<>();
42 keySerde.configure(propertyMap, true);
43 JsonSerde<?> valueSerde = new JsonSerde<>();
44 valueSerde.configure(propertyMap, false);
46 TopologyTestDriver testDriver = new TopologyTestDriver(topology, streamProcessorProperties);
48 TestInputTopic<String, Word> in = testDriver.createInputTopic(
50 (JsonSerializer<String>)keySerde.serializer(),
51 (JsonSerializer<Word>)valueSerde.serializer());
53 TestOutputTopic<Word, WordCounter> out = testDriver.createOutputTopic(
55 (JsonDeserializer<Word>)keySerde.deserializer(),
56 (JsonDeserializer<WordCounter>)valueSerde.deserializer());
58 TestData.writeInputData((key, value) -> in.pipeInput(key, value));
60 List<KeyValue<Word, WordCounter>> receivedMessages = out
66 "OUT: {} -> {}, {}, {}",
69 parseHeader(record.headers(), KEY_DEFAULT_CLASSID_FIELD_NAME),
70 parseHeader(record.headers(), DEFAULT_CLASSID_FIELD_NAME));
71 return KeyValue.pair(record.key(), record.value());
75 TestData.assertExpectedResult(receivedMessages);