package de.juplo.kafka.wordcount.splitter;
+import de.juplo.kafka.wordcount.counter.TestOutputUser;
import de.juplo.kafka.wordcount.counter.TestOutputWord;
import de.juplo.kafka.wordcount.recorder.TestInputRecording;
+import de.juplo.kafka.wordcount.recorder.TestInputUser;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
@SpringBootTest(
properties = {
+ "spring.kafka.producer.key-serializer=org.springframework.kafka.support.serializer.JsonSerializer",
"spring.kafka.producer.value-serializer=org.springframework.kafka.support.serializer.JsonSerializer",
"spring.kafka.producer.properties.spring.json.add.type.headers=false",
"spring.kafka.consumer.auto-offset-reset=earliest",
+ "spring.kafka.consumer.key-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer",
"spring.kafka.consumer.value-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer",
- "spring.kafka.consumer.properties.spring.json.value.default.type=de.juplo.kafka.wordcount.counter.TestWord",
- "spring.kafka.consumer.properties.spring.json.trusted.packages=de.juplo.kafka.wordcount.splitter",
+ "spring.kafka.consumer.properties.spring.json.key.default.type=de.juplo.kafka.wordcount.counter.TestOutputUser",
+ "spring.kafka.consumer.properties.spring.json.value.default.type=de.juplo.kafka.wordcount.counter.TestOutputWord",
"logging.level.root=WARN",
"logging.level.de.juplo=DEBUG",
"juplo.wordcount.splitter.bootstrap-server=${spring.embedded.kafka.brokers}",
@BeforeAll
public static void testSendMessage(
- @Autowired KafkaTemplate<String, TestInputRecording> kafkaTemplate)
+ @Autowired KafkaTemplate<TestInputUser, TestInputRecording> kafkaTemplate)
{
TestData
.getInputMessages()
{
try
{
- SendResult<String, TestInputRecording> result = kafkaTemplate.send(TOPIC_IN, kv.key, kv.value).get();
+ SendResult<TestInputUser, TestInputRecording> result = kafkaTemplate.send(TOPIC_IN, kv.key, kv.value).get();
log.info(
"Sent: {}={}, partition={}, offset={}",
result.getProducerRecord().key(),
@Test
void testSendMessage() throws Exception
{
- await("Expexted converted data")
+ await("Expected converted data")
.atMost(Duration.ofSeconds(5))
.untilAsserted(() ->
TestData.assertExpectedMessages(consumer.getReceivedMessages()));
static class Consumer
{
- private final MultiValueMap<String, TestOutputWord> received = new LinkedMultiValueMap<>();
+ private final MultiValueMap<TestOutputUser, TestOutputWord> received = new LinkedMultiValueMap<>();
@KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
public synchronized void receive(
- @Header(KafkaHeaders.RECEIVED_KEY) String key,
+ @Header(KafkaHeaders.RECEIVED_KEY) TestOutputUser key,
@Payload TestOutputWord value)
{
log.debug("Received message: {}={}", key, value);
received.add(key, value);
}
- synchronized MultiValueMap<String, TestOutputWord> getReceivedMessages()
+ synchronized MultiValueMap<TestOutputUser, TestOutputWord> getReceivedMessages()
{
return received;
}
package de.juplo.kafka.wordcount.splitter;
+import de.juplo.kafka.wordcount.counter.TestOutputUser;
import de.juplo.kafka.wordcount.counter.TestOutputWord;
import de.juplo.kafka.wordcount.recorder.TestInputRecording;
+import de.juplo.kafka.wordcount.recorder.TestInputUser;
import org.apache.kafka.streams.KeyValue;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
public class TestData
{
- static final String PETER = "peter";
- static final String KLAUS = "klaus";
+ static final TestInputUser PETER = TestInputUser.of("peter");
+ static final TestInputUser KLAUS = TestInputUser.of("klaus");
- static final Stream<KeyValue<String, TestInputRecording>> getInputMessages()
+ static final Stream<KeyValue<TestInputUser, TestInputRecording>> getInputMessages()
{
return Stream.of(INPUT_MESSAGES);
}
- private static final KeyValue<String, TestInputRecording>[] INPUT_MESSAGES = new KeyValue[]
+ private static final KeyValue<TestInputUser, TestInputRecording>[] INPUT_MESSAGES = new KeyValue[]
{
new KeyValue<>(
PETER,
- TestInputRecording.of(PETER, "Hallo Welt!")),
+ TestInputRecording.of(PETER.getUser(), "Hallo Welt!")),
new KeyValue<>(
KLAUS,
- TestInputRecording.of(KLAUS, "Müsch gäb's auch!")),
+ TestInputRecording.of(KLAUS.getUser(), "Müsch gäb's auch!")),
new KeyValue<>(
PETER,
- TestInputRecording.of(PETER, "Boäh, echt! ß mal nä Nümmäh!")),
+ TestInputRecording.of(PETER.getUser(), "Boäh, echt! ß mal nä Nümmäh!")),
};
- static void assertExpectedMessages(MultiValueMap<String, TestOutputWord> receivedMessages)
+ static void assertExpectedMessages(MultiValueMap<TestOutputUser, TestOutputWord> receivedMessages)
{
await("Received expected messages")
.atMost(Duration.ofSeconds(5))
assertThat(receivedMessages.get(user)).containsExactlyElementsOf(word)));
}
- private static final KeyValue<String, TestOutputWord>[] EXPECTED_MESSAGES = new KeyValue[]
+ private static final KeyValue<TestOutputUser, TestOutputWord>[] EXPECTED_MESSAGES = new KeyValue[]
{
KeyValue.pair(
- "peter",
- TestOutputWord.of("peter", "Hallo")),
+ TestOutputUser.of(PETER.getUser()),
+ TestOutputWord.of(PETER.getUser(), "Hallo")),
KeyValue.pair(
- "peter",
- TestOutputWord.of("peter", "Welt")),
+ TestOutputUser.of(PETER.getUser()),
+ TestOutputWord.of(PETER.getUser(), "Welt")),
KeyValue.pair(
- "klaus",
- TestOutputWord.of("klaus", "Müsch")),
+ TestOutputUser.of(KLAUS.getUser()),
+ TestOutputWord.of(KLAUS.getUser(), "Müsch")),
KeyValue.pair(
- "klaus",
- TestOutputWord.of("klaus", "gäb")),
+ TestOutputUser.of(KLAUS.getUser()),
+ TestOutputWord.of(KLAUS.getUser(), "gäb")),
KeyValue.pair(
- "klaus",
- TestOutputWord.of("klaus", "s")),
+ TestOutputUser.of(KLAUS.getUser()),
+ TestOutputWord.of(KLAUS.getUser(), "s")),
KeyValue.pair(
- "klaus",
- TestOutputWord.of("klaus", "auch")),
+ TestOutputUser.of(KLAUS.getUser()),
+ TestOutputWord.of(KLAUS.getUser(), "auch")),
KeyValue.pair(
- "peter",
- TestOutputWord.of("peter", "Boäh")),
+ TestOutputUser.of(PETER.getUser()),
+ TestOutputWord.of(PETER.getUser(), "Boäh")),
KeyValue.pair(
- "peter",
- TestOutputWord.of("peter", "echt")),
+ TestOutputUser.of(PETER.getUser()),
+ TestOutputWord.of(PETER.getUser(), "echt")),
KeyValue.pair(
- "peter",
- TestOutputWord.of("peter", "ß")),
+ TestOutputUser.of(PETER.getUser()),
+ TestOutputWord.of(PETER.getUser(), "ß")),
KeyValue.pair(
- "peter",
- TestOutputWord.of("peter", "mal")),
+ TestOutputUser.of(PETER.getUser()),
+ TestOutputWord.of(PETER.getUser(), "mal")),
KeyValue.pair(
- "peter",
- TestOutputWord.of("peter", "nä")),
+ TestOutputUser.of(PETER.getUser()),
+ TestOutputWord.of(PETER.getUser(), "nä")),
KeyValue.pair(
- "peter",
- TestOutputWord.of("peter", "Nümmäh")),
+ TestOutputUser.of(PETER.getUser()),
+ TestOutputWord.of(PETER.getUser(), "Nümmäh")),
};
- static MultiValueMap<String, TestOutputWord> expectedMessages()
+ static MultiValueMap<TestOutputUser, TestOutputWord> expectedMessages()
{
- MultiValueMap<String, TestOutputWord> expectedMessages = new LinkedMultiValueMap<>();
+ MultiValueMap<TestOutputUser, TestOutputWord> expectedMessages = new LinkedMultiValueMap<>();
Stream
.of(EXPECTED_MESSAGES)
.forEach(keyValue -> expectedMessages.add(keyValue.key, keyValue.value));