Skip to content

Add Kotlin test case #663

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
buildscript {
ext.kotlinVersion = '1.2.41'
repositories {
maven { url 'https://repo.spring.io/plugins-release' }
}
dependencies {
classpath 'io.spring.gradle:docbook-reference-plugin:0.3.1'
classpath 'org.asciidoctor:asciidoctor-gradle-plugin:1.5.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlinVersion"
}
}

Expand Down Expand Up @@ -49,14 +52,23 @@ subprojects { subproject ->
apply plugin: 'idea'
apply plugin: 'jacoco'
apply plugin: 'checkstyle'
apply plugin: 'kotlin'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's consider to add kotlin-spring as well?
spring-projects/spring-integration#2432

apply plugin: 'kotlin-spring'

compileJava {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}

compileTestKotlin {
kotlinOptions {
jvmTarget = '1.8'
}
}

ext {
assertjVersion = '3.9.1'
assertkVersion = '0.10'
hamcrestVersion = '1.3'
jacksonVersion = '2.9.5'
jaywayJsonPathVersion = '2.4.0'
Expand Down Expand Up @@ -94,6 +106,13 @@ subprojects { subproject ->
testCompileOnly 'org.apiguardian:apiguardian-api:1.0.0'

testRuntime "org.apache.logging.log4j:log4j-slf4j-impl:$log4jVersion"

testCompile("com.willowtreeapps.assertk:assertk:$assertkVersion") {
exclude group: 'org.jetbrains.kotlin', module: 'kotlin-reflect'
}

testCompile "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
testRuntime "org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion"
}

// enable all compiler warnings; individual projects may customize further
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Copyright 2016-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import org.apache.kafka.clients.consumer.ConsumerConfig
import org.apache.kafka.clients.producer.ProducerConfig
import org.apache.kafka.common.serialization.StringDeserializer
import org.apache.kafka.common.serialization.StringSerializer
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.kafka.annotation.EnableKafka
import org.springframework.kafka.annotation.KafkaListener
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory
import org.springframework.kafka.core.*
import org.springframework.kafka.test.context.EmbeddedKafka
import org.springframework.kafka.test.rule.KafkaEmbedded
import org.springframework.test.annotation.DirtiesContext
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit


/**
* @author Gary Russell
* @since 2.2
*/

@SpringJUnitConfig
@DirtiesContext
@EmbeddedKafka(topics = ["kotlinTestTopic"])
class EnableKafkaKotlinTests {

@Autowired
private lateinit var config: Config

@Autowired
private lateinit var template: KafkaTemplate<String, String>

@Test
fun `test listener`() {
this.template.send("kotlinTestTopic", "foo")
assertThat(this.config.latch.await(10, TimeUnit.SECONDS)).isTrue()
assertThat(this.config.received).isEqualTo("foo")
}

@Configuration
@EnableKafka
class Config {

lateinit var received: String

val latch = CountDownLatch(1)

@Value("\${" + KafkaEmbedded.SPRING_EMBEDDED_KAFKA_BROKERS + "}")
private lateinit var brokerAddresses: String

@Bean
fun kpf(): ProducerFactory<String, String> {
val configs = HashMap<String, Any>()
configs[ProducerConfig.BOOTSTRAP_SERVERS_CONFIG] = this.brokerAddresses
configs[ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG] = StringSerializer::class.java
configs[ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG] = StringSerializer::class.java
return DefaultKafkaProducerFactory(configs)
}

@Bean
fun kcf(): ConsumerFactory<String, String> {
val configs = HashMap<String, Any>()
configs["foo"] = "bar"
configs[ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG] = this.brokerAddresses
configs[ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG] = StringDeserializer::class.java
configs[ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG] = StringDeserializer::class.java
configs[ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG] = false
configs[ConsumerConfig.AUTO_OFFSET_RESET_CONFIG] = "earliest"
return DefaultKafkaConsumerFactory(configs)
}

@Bean
fun kt(): KafkaTemplate<String, String> {
return KafkaTemplate(kpf())
}

@Bean
fun kafkaListenerContainerFactory(): ConcurrentKafkaListenerContainerFactory<String, String> {
val factory: ConcurrentKafkaListenerContainerFactory<String, String>
= ConcurrentKafkaListenerContainerFactory()
factory.consumerFactory = kcf()
return factory
}

@KafkaListener(id = "kotlin", topics = ["kotlinTestTopic"])
fun listen(value: String) {
this.received = value
this.latch.countDown()
}

}

}