import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
-import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.test.context.EmbeddedKafka;
-import org.springframework.util.LinkedMultiValueMap;
-import org.springframework.util.MultiValueMap;
import java.time.Duration;
+import java.util.LinkedList;
+import java.util.List;
import static de.juplo.kafka.wordcount.counter.CounterApplicationIT.*;
import static org.assertj.core.api.Assertions.assertThat;
@Test
void testSendMessage() throws Exception
{
- kafkaTemplate.send(TOPIC_IN, "peter", "Hallo");
- kafkaTemplate.send(TOPIC_IN, "klaus", "Müsch");
- kafkaTemplate.send(TOPIC_IN, "peter", "Welt");
- kafkaTemplate.send(TOPIC_IN, "klaus", "Müsch");
- kafkaTemplate.send(TOPIC_IN, "klaus", "s");
- kafkaTemplate.send(TOPIC_IN, "peter", "Boäh");
- kafkaTemplate.send(TOPIC_IN, "peter", "Welt");
- kafkaTemplate.send(TOPIC_IN, "peter", "Boäh");
- kafkaTemplate.send(TOPIC_IN, "klaus", "s");
- kafkaTemplate.send(TOPIC_IN, "peter", "Boäh");
- kafkaTemplate.send(TOPIC_IN, "klaus", "s");
-
- Message peter1 = Message.of(
- Key.of("peter", "Hallo"),
- "1");
- Message peter2 = Message.of(
- Key.of("peter", "Welt"),
- "1");
- Message peter3 = Message.of(
- Key.of("peter", "Boäh"),
- "1");
- Message peter4 = Message.of(
- Key.of("peter", "Welt"),
- "2");
- Message peter5 = Message.of(
- Key.of("peter", "Boäh"),
- "2");
- Message peter6 = Message.of(
- Key.of("peter", "Boäh"),
- "3");
-
- Message klaus1 = Message.of(
- Key.of("klaus", "Müsch"),
- "1");
- Message klaus2 = Message.of(
- Key.of("klaus", "Müsch"),
- "2");
- Message klaus3 = Message.of(
- Key.of("klaus", "s"),
- "1");
- Message klaus4 = Message.of(
- Key.of("klaus", "s"),
- "2");
- Message klaus5 = Message.of(
- Key.of("klaus", "s"),
- "3");
+ TestData.writeInputData((key, value) -> kafkaTemplate.send(TOPIC_IN, key, value));
await("Expexted converted data")
.atMost(Duration.ofSeconds(10))
- .untilAsserted(() ->
- {
- assertThat(consumer.received).hasSize(2);
- assertThat(consumer.received.get("klaus")).containsExactly(klaus1, klaus2, klaus3, klaus4, klaus5);
- assertThat(consumer.received.get("peter")).containsExactly(peter1, peter2, peter3, peter4, peter5, peter6);
- });
+ .untilAsserted(() -> TestData.assertExpectedResult(consumer.received));
}
@RequiredArgsConstructor
static class Consumer
{
- private final MultiValueMap<String, Message> received = new LinkedMultiValueMap<>();
+ private final List<Message> received = new LinkedList<>();
private final ObjectMapper mapper;
@KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
{
log.debug("Received message: {}", record);
Key key = mapper.readValue(record.key(), Key.class);
- received.add(key.getUsername(), Message.of(key,record.value()));
+ received.add(Message.of(key,record.value()));
}
}
- @Value(staticConstructor = "of")
- static class Message
- {
- Key key;
- String value;
- }
-
@TestConfiguration
static class Configuration
{
--- /dev/null
+package de.juplo.kafka.wordcount.counter;
+
+import org.springframework.util.LinkedMultiValueMap;
+import org.springframework.util.MultiValueMap;
+
+import java.util.List;
+import java.util.function.BiConsumer;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+
+class TestData
+{
+ static void writeInputData(BiConsumer<String, String> consumer)
+ {
+ consumer.accept("peter", "Hallo");
+ consumer.accept("klaus", "Müsch");
+ consumer.accept("peter", "Welt");
+ consumer.accept("klaus", "Müsch");
+ consumer.accept("klaus", "s");
+ consumer.accept("peter", "Boäh");
+ consumer.accept("peter", "Welt");
+ consumer.accept("peter", "Boäh");
+ consumer.accept("klaus", "s");
+ consumer.accept("peter", "Boäh");
+ consumer.accept("klaus", "s");
+ }
+
+ static void assertExpectedResult(List<Message> receivedMessages)
+ {
+ MultiValueMap<String, Message> received = new LinkedMultiValueMap<>();
+
+ receivedMessages.forEach(message -> received.add(message.getKey().getUsername(), message));
+
+ assertThat(received).hasSize(2);
+ assertThat(received.get("klaus")).containsExactly(klaus1, klaus2, klaus3, klaus4, klaus5);
+ assertThat(received.get("peter")).containsExactly(peter1, peter2, peter3, peter4, peter5, peter6);
+ }
+
+ static Message peter1 = Message.of(
+ Key.of("peter", "Hallo"),
+ "1");
+ static Message peter2 = Message.of(
+ Key.of("peter", "Welt"),
+ "1");
+ static Message peter3 = Message.of(
+ Key.of("peter", "Boäh"),
+ "1");
+ static Message peter4 = Message.of(
+ Key.of("peter", "Welt"),
+ "2");
+ static Message peter5 = Message.of(
+ Key.of("peter", "Boäh"),
+ "2");
+ static Message peter6 = Message.of(
+ Key.of("peter", "Boäh"),
+ "3");
+
+ static Message klaus1 = Message.of(
+ Key.of("klaus", "Müsch"),
+ "1");
+ static Message klaus2 = Message.of(
+ Key.of("klaus", "Müsch"),
+ "2");
+ static Message klaus3 = Message.of(
+ Key.of("klaus", "s"),
+ "1");
+ static Message klaus4 = Message.of(
+ Key.of("klaus", "s"),
+ "2");
+ static Message klaus5 = Message.of(
+ Key.of("klaus", "s"),
+ "3");
+}