|
| 1 | +/* |
| 2 | + * Copyright 2016-2018 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import org.apache.kafka.clients.consumer.ConsumerConfig |
| 18 | +import org.apache.kafka.clients.producer.ProducerConfig |
| 19 | +import org.apache.kafka.common.serialization.StringDeserializer |
| 20 | +import org.apache.kafka.common.serialization.StringSerializer |
| 21 | +import org.assertj.core.api.Assertions.assertThat |
| 22 | +import org.junit.jupiter.api.Test |
| 23 | +import org.springframework.beans.factory.annotation.Autowired |
| 24 | +import org.springframework.beans.factory.annotation.Value |
| 25 | +import org.springframework.context.annotation.Bean |
| 26 | +import org.springframework.context.annotation.Configuration |
| 27 | +import org.springframework.kafka.annotation.EnableKafka |
| 28 | +import org.springframework.kafka.annotation.KafkaListener |
| 29 | +import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory |
| 30 | +import org.springframework.kafka.core.* |
| 31 | +import org.springframework.kafka.test.context.EmbeddedKafka |
| 32 | +import org.springframework.kafka.test.rule.KafkaEmbedded |
| 33 | +import org.springframework.test.annotation.DirtiesContext |
| 34 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig |
| 35 | +import java.util.concurrent.CountDownLatch |
| 36 | +import java.util.concurrent.TimeUnit |
| 37 | + |
| 38 | + |
| 39 | +/** |
| 40 | + * @author Gary Russell |
| 41 | + * @since 2.2 |
| 42 | + */ |
| 43 | + |
| 44 | +@SpringJUnitConfig |
| 45 | +@DirtiesContext |
| 46 | +@EmbeddedKafka(topics = ["kotlinTestTopic"]) |
| 47 | +class EnableKafkaKotlinTests { |
| 48 | + |
| 49 | + @Autowired |
| 50 | + private lateinit var config: Config |
| 51 | + |
| 52 | + @Autowired |
| 53 | + private lateinit var template: KafkaTemplate<String, String> |
| 54 | + |
| 55 | + @Test |
| 56 | + fun `test listener`() { |
| 57 | + this.template.send("kotlinTestTopic", "foo") |
| 58 | + assertThat(this.config.latch.await(10, TimeUnit.SECONDS)).isTrue() |
| 59 | + assertThat(this.config.received).isEqualTo("foo") |
| 60 | + } |
| 61 | + |
| 62 | + @Configuration |
| 63 | + @EnableKafka |
| 64 | + class Config { |
| 65 | + |
| 66 | + lateinit var received: String |
| 67 | + |
| 68 | + val latch = CountDownLatch(1) |
| 69 | + |
| 70 | + @Value("\${" + KafkaEmbedded.SPRING_EMBEDDED_KAFKA_BROKERS + "}") |
| 71 | + private lateinit var brokerAddresses: String |
| 72 | + |
| 73 | + @Bean |
| 74 | + fun kpf(): ProducerFactory<String, String> { |
| 75 | + val configs = HashMap<String, Any>() |
| 76 | + configs[ProducerConfig.BOOTSTRAP_SERVERS_CONFIG] = this.brokerAddresses |
| 77 | + configs[ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG] = StringSerializer::class.java |
| 78 | + configs[ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG] = StringSerializer::class.java |
| 79 | + return DefaultKafkaProducerFactory(configs) |
| 80 | + } |
| 81 | + |
| 82 | + @Bean |
| 83 | + fun kcf(): ConsumerFactory<String, String> { |
| 84 | + val configs = HashMap<String, Any>() |
| 85 | + configs["foo"] = "bar" |
| 86 | + configs[ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG] = this.brokerAddresses |
| 87 | + configs[ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG] = StringDeserializer::class.java |
| 88 | + configs[ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG] = StringDeserializer::class.java |
| 89 | + configs[ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG] = false |
| 90 | + configs[ConsumerConfig.AUTO_OFFSET_RESET_CONFIG] = "earliest" |
| 91 | + return DefaultKafkaConsumerFactory(configs) |
| 92 | + } |
| 93 | + |
| 94 | + @Bean |
| 95 | + fun kt(): KafkaTemplate<String, String> { |
| 96 | + return KafkaTemplate(kpf()) |
| 97 | + } |
| 98 | + |
| 99 | + @Bean |
| 100 | + fun kafkaListenerContainerFactory(): ConcurrentKafkaListenerContainerFactory<String, String> { |
| 101 | + val factory: ConcurrentKafkaListenerContainerFactory<String, String> |
| 102 | + = ConcurrentKafkaListenerContainerFactory() |
| 103 | + factory.consumerFactory = kcf() |
| 104 | + return factory |
| 105 | + } |
| 106 | + |
| 107 | + @KafkaListener(id = "kotlin", topics = ["kotlinTestTopic"]) |
| 108 | + fun listen(value: String) { |
| 109 | + this.received = value |
| 110 | + this.latch.countDown() |
| 111 | + } |
| 112 | + |
| 113 | + } |
| 114 | + |
| 115 | +} |
0 commit comments