Der Test verwendet die `@Bean` von `EndlessConsumer`
[demos/kafka/training] / src / test / java / de / juplo / kafka / ApplicationTests.java
1 package de.juplo.kafka;
2
3 import org.apache.kafka.clients.producer.ProducerRecord;
4 import org.apache.kafka.common.serialization.LongSerializer;
5 import org.apache.kafka.common.serialization.StringSerializer;
6 import org.apache.kafka.common.utils.Bytes;
7 import org.springframework.boot.test.context.TestConfiguration;
8 import org.springframework.context.annotation.Bean;
9 import org.springframework.test.context.ContextConfiguration;
10
11 import java.util.function.Consumer;
12
13
14 @ContextConfiguration(classes = ApplicationTests.Configuration.class)
15 public class ApplicationTests extends GenericApplicationTests<String, Long>
16 {
17   public ApplicationTests()
18   {
19     super(
20         new RecordGenerator()
21         {
22           final StringSerializer stringSerializer = new StringSerializer();
23           final LongSerializer longSerializer = new LongSerializer();
24
25
26           @Override
27           public int generate(
28               boolean poisonPills,
29               boolean logicErrors,
30               Consumer<ProducerRecord<Bytes, Bytes>> messageSender)
31           {
32             int i = 0;
33
34             for (int partition = 0; partition < 10; partition++)
35             {
36               for (int key = 0; key < 10000; key++)
37               {
38                 i++;
39
40                 Bytes value = new Bytes(longSerializer.serialize(TOPIC, (long)i));
41                 if (i == 99977)
42                 {
43                   if (logicErrors)
44                   {
45                     value = new Bytes(longSerializer.serialize(TOPIC, Long.MIN_VALUE));
46                   }
47                   if (poisonPills)
48                   {
49                     value = new Bytes(stringSerializer.serialize(TOPIC, "BOOM (Poison-Pill)!"));
50                   }
51                 }
52
53                 ProducerRecord<Bytes, Bytes> record =
54                     new ProducerRecord<>(
55                         TOPIC,
56                         partition,
57                         new Bytes(stringSerializer.serialize(TOPIC,Integer.toString(partition*10+key%2))),
58                         value);
59
60                 messageSender.accept(record);
61               }
62             }
63
64             return i;
65           }
66         });
67   }
68
69
70   @TestConfiguration
71   public static class Configuration
72   {
73     @Bean
74     public RecordHandler<String, Long> applicationRecordHandler()
75     {
76       return (record) ->
77       {
78         if (record.value() == Long.MIN_VALUE)
79           throw new RuntimeException("BOOM (Logic-Error)!");
80       };
81     }
82   }
83 }