Deserialisierung von Nachrichten unterschiedlichen Typs
[demos/kafka/training] / src / test / java / de / juplo / kafka / ApplicationTests.java
index 39ff0d7..9169de0 100644 (file)
@@ -15,17 +15,18 @@ import org.springframework.boot.test.context.ConfigDataApplicationContextInitial
 import org.springframework.boot.test.context.TestConfiguration;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Import;
+import org.springframework.kafka.support.serializer.JsonSerializer;
 import org.springframework.kafka.test.context.EmbeddedKafka;
 import org.springframework.test.context.TestPropertySource;
 import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
 
 import java.time.Duration;
+import java.time.LocalDateTime;
 import java.util.*;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
 import java.util.function.BiConsumer;
 import java.util.function.Consumer;
-import java.util.function.Function;
 import java.util.stream.Collectors;
 import java.util.stream.IntStream;
 
@@ -56,7 +57,7 @@ class ApplicationTests
        @Autowired
        KafkaProducer<String, Bytes> kafkaProducer;
        @Autowired
-       KafkaConsumer<String, Long> kafkaConsumer;
+       KafkaConsumer<String, ValidMessage> kafkaConsumer;
        @Autowired
        KafkaConsumer<Bytes, Bytes> offsetConsumer;
        @Autowired
@@ -64,11 +65,11 @@ class ApplicationTests
        @Autowired
        ExecutorService executor;
 
-       Consumer<ConsumerRecord<String, Long>> testHandler;
-       EndlessConsumer<String, Long> endlessConsumer;
+       Consumer<ConsumerRecord<String, ValidMessage>> testHandler;
+       EndlessConsumer<String, ValidMessage> endlessConsumer;
        Map<TopicPartition, Long> oldOffsets;
        Map<TopicPartition, Long> newOffsets;
-       Set<ConsumerRecord<String, Long>> receivedRecords;
+       Set<ConsumerRecord<String, ValidMessage>> receivedRecords;
 
 
        /** Tests methods */
@@ -79,8 +80,20 @@ class ApplicationTests
        {
                send100Messages((partition, key, counter) ->
                {
-                       Bytes value = new Bytes(valueSerializer.serialize(TOPIC, counter));
-                       return new ProducerRecord<>(TOPIC, partition, key, value);
+                       Bytes value;
+                       String type;
+
+                       if (counter%3 != 0)
+                       {
+                               value = serializeClientMessage(key, counter);
+                               type = "message";
+                       }
+                       else {
+                               value = serializeGreeting(key, counter);
+                               type = "greeting";
+                       }
+
+                       return toRecord(partition, key, value, type);
                });
 
                await("100 records received")
@@ -106,10 +119,28 @@ class ApplicationTests
        {
                send100Messages((partition, key, counter) ->
                {
-                       Bytes value = counter == 77
-                                       ? new Bytes(stringSerializer.serialize(TOPIC, "BOOM!"))
-                                       : new Bytes(valueSerializer.serialize(TOPIC, counter));
-                       return new ProducerRecord<>(TOPIC, partition, key, value);
+                       Bytes value;
+                       String type;
+
+                       if (counter == 77)
+                       {
+                               value = serializeFooMessage(key, counter);
+                               type = "foo";
+                       }
+                       else
+                       {
+                               if (counter%3 != 0)
+                               {
+                                       value = serializeClientMessage(key, counter);
+                                       type = "message";
+                               }
+                               else {
+                                       value = serializeGreeting(key, counter);
+                                       type = "greeting";
+                               }
+                       }
+
+                       return toRecord(partition, key, value, type);
                });
 
                await("Consumer failed")
@@ -206,7 +237,7 @@ class ApplicationTests
                        for (int key = 0; key < 10; key++)
                        {
                                ProducerRecord<String, Bytes> record =
-                                               recordGenerator.generate(partition, Integer.toString(key%2), ++i);
+                                               recordGenerator.generate(partition, Integer.toString(partition*10+key%2), ++i);
 
                                kafkaProducer.send(record, (metadata, e) ->
                                {
@@ -232,6 +263,31 @@ class ApplicationTests
                }
        }
 
+       ProducerRecord<String, Bytes> toRecord(int partition, String key, Bytes value, String type)
+       {
+               ProducerRecord<String, Bytes> record =
+                               new ProducerRecord<>(TOPIC, partition, key, value);
+               record.headers().add("__TypeId__", type.getBytes());
+               return record;
+       }
+
+       Bytes serializeClientMessage(String key, Long value)
+       {
+               TestClientMessage message = new TestClientMessage(key, value.toString());
+               return new Bytes(valueSerializer.serialize(TOPIC, message));
+       }
+
+       Bytes serializeGreeting(String key, Long value)
+       {
+               TestGreeting message = new TestGreeting(key, LocalDateTime.now());
+               return new Bytes(valueSerializer.serialize(TOPIC, message));
+       }
+
+       Bytes serializeFooMessage(String key, Long value)
+       {
+               TestFooMessage message = new TestFooMessage(key, value);
+               return new Bytes(valueSerializer.serialize(TOPIC, message));
+       }
 
        @BeforeEach
        public void init()
@@ -248,7 +304,7 @@ class ApplicationTests
                        newOffsets.put(tp, offset - 1);
                });
 
-               Consumer<ConsumerRecord<String, Long>> captureOffsetAndExecuteTestHandler =
+               Consumer<ConsumerRecord<String, ValidMessage>> captureOffsetAndExecuteTestHandler =
                                record ->
                                {
                                        newOffsets.put(
@@ -288,9 +344,9 @@ class ApplicationTests
        public static class Configuration
        {
                @Bean
-               Serializer<Long> serializer()
+               Serializer<ValidMessage> serializer()
                {
-                       return new LongSerializer();
+                       return new JsonSerializer<>();
                }
 
                @Bean