|
23 | 23 | import java.lang.reflect.Modifier; |
24 | 24 | import java.net.URL; |
25 | 25 | import java.util.ArrayList; |
| 26 | +import java.util.Arrays; |
| 27 | +import java.util.Collection; |
26 | 28 | import java.util.List; |
27 | 29 | import java.util.Optional; |
| 30 | +import java.util.Properties; |
28 | 31 |
|
29 | 32 | import org.junit.jupiter.api.extension.BeforeAllCallback; |
30 | 33 | import org.junit.jupiter.api.extension.ExtensionConfigurationException; |
|
36 | 39 | import org.microshed.testing.jaxrs.RestClientBuilder; |
37 | 40 | import org.microshed.testing.jwt.JwtBuilder; |
38 | 41 | import org.microshed.testing.jwt.JwtConfig; |
| 42 | +import org.microshed.testing.kafka.KafkaConsumerConfig; |
| 43 | +import org.microshed.testing.kafka.KafkaProducerConfig; |
39 | 44 | import org.slf4j.Logger; |
40 | 45 | import org.slf4j.LoggerFactory; |
41 | 46 |
|
@@ -63,6 +68,7 @@ public void beforeAll(ExtensionContext context) throws Exception { |
63 | 68 | config.start(); |
64 | 69 | configureRestAssured(config); |
65 | 70 | injectRestClients(testClass); |
| 71 | + injectKafkaClients(testClass); |
66 | 72 | } |
67 | 73 |
|
68 | 74 | private static void injectRestClients(Class<?> clazz) { |
@@ -90,7 +96,96 @@ private static void injectRestClients(Class<?> clazz) { |
90 | 96 | restClientField.set(null, restClient); |
91 | 97 | LOG.debug("Injected rest client for " + restClientField); |
92 | 98 | } catch (Exception e) { |
93 | | - throw new ExtensionConfigurationException("Unable to set field " + restClientField, e); |
| 99 | + throw new ExtensionConfigurationException("Unable to inject field " + restClientField, e); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private void injectKafkaClients(Class<?> clazz) { |
| 105 | + // Verify kafka-client and testcontainers-kafka is on classpath |
| 106 | + Class<?> KafkaProducer = tryLoad("org.apache.kafka.clients.producer.KafkaProducer"); |
| 107 | + Class<?> KafkaConsumer = tryLoad("org.apache.kafka.clients.consumer.KafkaConsumer"); |
| 108 | + if (KafkaProducer == null || KafkaConsumer == null) |
| 109 | + return; |
| 110 | + String globalBootstrapServers = System.getProperty("org.microshed.kafka.bootstrap.servers"); |
| 111 | + |
| 112 | + List<Field> kafkaProducerFields = AnnotationSupport.findAnnotatedFields(clazz, KafkaProducerConfig.class); |
| 113 | + for (Field producerField : kafkaProducerFields) { |
| 114 | + if (!KafkaProducer.isAssignableFrom(producerField.getType())) { |
| 115 | + throw new ExtensionConfigurationException("Fields annotated with @KafkaProducerConfig must be of the type " + KafkaProducer.getName()); |
| 116 | + } |
| 117 | + if (!Modifier.isPublic(producerField.getModifiers()) || |
| 118 | + !Modifier.isStatic(producerField.getModifiers()) || |
| 119 | + Modifier.isFinal(producerField.getModifiers())) { |
| 120 | + throw new ExtensionConfigurationException("The KafkaProducer field annotated with @KafkaProducerConfig " + |
| 121 | + "must be public, static, and non-final: " + producerField); |
| 122 | + } |
| 123 | + |
| 124 | + KafkaProducerConfig producerConfig = producerField.getAnnotation(KafkaProducerConfig.class); |
| 125 | + Properties properties = new Properties(); |
| 126 | + String bootstrapServers = producerConfig.bootstrapServers().isEmpty() ? globalBootstrapServers : producerConfig.bootstrapServers(); |
| 127 | + if (bootstrapServers.isEmpty()) |
| 128 | + throw new ExtensionConfigurationException("To use @KafkaProducerConfig on a KafkaProducer a bootstrap server must be " + |
| 129 | + "defined in the @KafkaProducerConfig annotation or using the " + |
| 130 | + "'org.microshed.kafka.bootstrap.servers' system property"); |
| 131 | + properties.put("bootstrap.servers", bootstrapServers); |
| 132 | + properties.put("key.serializer", producerConfig.keySerializer().getName()); |
| 133 | + properties.put("value.serializer", producerConfig.valueSerializer().getName()); |
| 134 | + for (String prop : producerConfig.properties()) { |
| 135 | + int split = prop.indexOf("="); |
| 136 | + if (split < 2) |
| 137 | + throw new ExtensionConfigurationException("The property '" + prop + "' for field " + producerField + " must be in the format 'key=value'"); |
| 138 | + properties.put(prop.substring(0, split), prop.substring(split + 1)); |
| 139 | + } |
| 140 | + try { |
| 141 | + Object producer = KafkaProducer.getConstructor(Properties.class).newInstance(properties); |
| 142 | + producerField.set(null, producer); |
| 143 | + LOG.debug("Injected kafka producer for " + producerField + " with config " + producerConfig); |
| 144 | + } catch (Exception e) { |
| 145 | + throw new ExtensionConfigurationException("Unable to inject field " + producerField, e); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + List<Field> kafkaConsumerFields = AnnotationSupport.findAnnotatedFields(clazz, KafkaConsumerConfig.class); |
| 150 | + for (Field consumerField : kafkaConsumerFields) { |
| 151 | + if (!KafkaConsumer.isAssignableFrom(consumerField.getType())) { |
| 152 | + throw new ExtensionConfigurationException("Fields annotated with @KafkaConsumerConfig must be of the type " + KafkaConsumer.getName()); |
| 153 | + } |
| 154 | + if (!Modifier.isPublic(consumerField.getModifiers()) || |
| 155 | + !Modifier.isStatic(consumerField.getModifiers()) || |
| 156 | + Modifier.isFinal(consumerField.getModifiers())) { |
| 157 | + throw new ExtensionConfigurationException("The KafkaProducer field annotated with @KafkaConsumerConfig " + |
| 158 | + "must be public, static, and non-final: " + consumerField); |
| 159 | + } |
| 160 | + |
| 161 | + KafkaConsumerConfig consumerConfig = consumerField.getAnnotation(KafkaConsumerConfig.class); |
| 162 | + Properties properties = new Properties(); |
| 163 | + String bootstrapServers = consumerConfig.bootstrapServers().isEmpty() ? globalBootstrapServers : consumerConfig.bootstrapServers(); |
| 164 | + if (bootstrapServers.isEmpty()) |
| 165 | + throw new ExtensionConfigurationException("To use @KafkaConsumerConfig on a KafkaConsumer a bootstrap server must be " + |
| 166 | + "defined in the @KafkaConsumerConfig annotation or using the " + |
| 167 | + "'org.microshed.kafka.bootstrap.servers' system property"); |
| 168 | + properties.put("bootstrap.servers", bootstrapServers); |
| 169 | + properties.put("group.id", consumerConfig.groupId()); |
| 170 | + properties.put("key.deserializer", consumerConfig.keyDeserializer().getName()); |
| 171 | + properties.put("value.deserializer", consumerConfig.valueDeserializer().getName()); |
| 172 | + for (String prop : consumerConfig.properties()) { |
| 173 | + int split = prop.indexOf("="); |
| 174 | + if (split < 2) |
| 175 | + throw new ExtensionConfigurationException("The property '" + prop + "' for field " + consumerField + " must be in the format 'key=value'"); |
| 176 | + properties.put(prop.substring(0, split), prop.substring(split + 1)); |
| 177 | + } |
| 178 | + try { |
| 179 | + Object consumer = KafkaConsumer.getConstructor(Properties.class).newInstance(properties); |
| 180 | + consumerField.set(null, consumer); |
| 181 | + LOG.debug("Injected kafka consumer for " + consumerField + " with config " + consumerConfig); |
| 182 | + if (consumerConfig.topics().length > 0) { |
| 183 | + Collection<String> topics = Arrays.asList(consumerConfig.topics()); |
| 184 | + KafkaConsumer.getMethod("subscribe", Collection.class).invoke(consumer, topics); |
| 185 | + LOG.debug("Subscribed kafka consumer for " + consumerField + " to topics " + topics); |
| 186 | + } |
| 187 | + } catch (Exception e) { |
| 188 | + throw new ExtensionConfigurationException("Unable to inject field " + consumerField, e); |
94 | 189 | } |
95 | 190 | } |
96 | 191 | } |
|
0 commit comments