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