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.splitter.SplitterApplicationTests.*;
import static org.assertj.core.api.Assertions.assertThat;
Recording recording = new Recording();
recording.setUser("peter");
recording.setSentence("Hallo Welt!");
- kafkaTemplate.send(TOPIC_IN, mapper.writeValueAsString(recording));
-
- String word1 = mapper.writeValueAsString(Word.of("peter", "Hallo"));
- String word2 = mapper.writeValueAsString(Word.of("peter", "Welt"));
+ kafkaTemplate.send(TOPIC_IN, recording.getUser(), mapper.writeValueAsString(recording));
+ recording.setUser("klaus");
+ recording.setSentence("Müsch gäb's auch!");
+ kafkaTemplate.send(TOPIC_IN, recording.getUser(), mapper.writeValueAsString(recording));
+ recording.setUser("peter");
+ recording.setSentence("Boäh, echt! ß mal nä Nümmäh!");
+ kafkaTemplate.send(TOPIC_IN, recording.getUser(), mapper.writeValueAsString(recording));
+
+ String peter1 = mapper.writeValueAsString(Word.of("peter", "Hallo"));
+ String peter2 = mapper.writeValueAsString(Word.of("peter", "Welt"));
+ String peter3 = mapper.writeValueAsString(Word.of("peter", "Boäh"));
+ String peter4 = mapper.writeValueAsString(Word.of("peter", "echt"));
+ String peter5 = mapper.writeValueAsString(Word.of("peter", "ß"));
+ String peter6 = mapper.writeValueAsString(Word.of("peter", "mal"));
+ String peter7 = mapper.writeValueAsString(Word.of("peter", "nä"));
+ String peter8 = mapper.writeValueAsString(Word.of("peter", "Nümmäh"));
+
+ String klaus1 = mapper.writeValueAsString(Word.of("klaus","Müsch"));
+ String klaus2 = mapper.writeValueAsString(Word.of("klaus","gäb"));
+ String klaus3 = mapper.writeValueAsString(Word.of("klaus","s"));
+ String klaus4 = mapper.writeValueAsString(Word.of("klaus","auch"));
await("Expexted converted data")
.atMost(Duration.ofSeconds(5))
.untilAsserted(() ->
{
assertThat(consumer.received).hasSize(2);
- assertThat(consumer.received.get(0).value()).isEqualTo(word1);
- assertThat(consumer.received.get(1).value()).isEqualTo(word2);
+ assertThat(consumer.received.get("klaus")).containsExactly(klaus1, klaus2, klaus3, klaus4);
+ assertThat(consumer.received.get("peter")).containsExactly(peter1, peter2, peter3, peter4, peter5, peter6, peter7, peter8);
});
}
static class Consumer
{
- final List<ConsumerRecord<String, String>> received = new LinkedList<>();
+ final MultiValueMap<String, String> received = new LinkedMultiValueMap<>();
@KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
public void receive(ConsumerRecord<String, String> record)
{
log.debug("Received message: {}", record);
- received.add(record);
+ received.add(record.key(), record.value());
}
}