This repository has been archived by the owner on Sep 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
plugins { | ||
id 'java-library' | ||
} | ||
|
||
apply from: "$rootDir/gradle/publish.gradle" | ||
|
||
dependencies { | ||
api project(':env-core') | ||
api("org.springframework.kafka:spring-kafka-test:2.7.1") | ||
testImplementation("org.jetbrains.kotlin:kotlin-test-junit") | ||
} |
57 changes: 57 additions & 0 deletions
57
...fka-embedded/src/main/java/io/github/adven27/env/mq/kafka/embedded/EmbeddedKafkaSystem.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package io.github.adven27.env.mq.kafka.embedded | ||
|
||
import io.github.adven27.env.core.Environment.Companion.setProperties | ||
import io.github.adven27.env.core.Environment.Prop | ||
import io.github.adven27.env.core.Environment.Prop.Companion.set | ||
import io.github.adven27.env.core.ExternalSystem | ||
import org.springframework.kafka.test.EmbeddedKafkaBroker | ||
|
||
open class EmbeddedKafkaSystem( | ||
topics: Array<String>, | ||
@Suppress("SpreadOperator") | ||
private val embeddedKafka: EmbeddedKafkaBroker = EmbeddedKafkaBroker( | ||
NUMBER_OF_BROKERS, | ||
CONTROLLED_SHUTDOWN, | ||
NUMBER_OF_PARTITIONS, | ||
*topics | ||
) | ||
) : ExternalSystem { | ||
private var config: Config = Config() | ||
private var isRunning = false | ||
|
||
override fun running() = isRunning | ||
|
||
@Suppress("unused") | ||
fun config(): Config = config | ||
|
||
override fun start() { | ||
embeddedKafka.afterPropertiesSet() | ||
config = Config(PROP_BOOTSTRAPSERVERS set embeddedKafka.brokersAsString) | ||
isRunning = true | ||
} | ||
|
||
override fun stop() { | ||
embeddedKafka.destroy() | ||
isRunning = false | ||
} | ||
|
||
override fun describe() = super.describe() + "\n\t" + config.asMap().entries.joinToString("\n\t") { it.toString() } | ||
|
||
data class Config(val bootstrapServers: Prop = PROP_BOOTSTRAPSERVERS set "PLAINTEXT://localhost:$DEFAULT_KAFKA_PORT") { | ||
init { | ||
asMap().setProperties() | ||
} | ||
|
||
fun asMap() = mapOf(bootstrapServers.pair()) | ||
} | ||
|
||
companion object { | ||
private const val DEFAULT_KAFKA_PORT = 9093 | ||
|
||
private const val NUMBER_OF_BROKERS = 1 | ||
private const val NUMBER_OF_PARTITIONS = 1 | ||
private const val CONTROLLED_SHUTDOWN = true | ||
|
||
const val PROP_BOOTSTRAPSERVERS = "env.mq.kafka.bootstrapServers" | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...bedded/src/test/kotlin/io/github/adven27/env/mq/kafka/embedded/EmbeddedKafkaSystemTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package io.github.adven27.env.mq.kafka.embedded | ||
|
||
import io.github.adven27.env.core.Environment | ||
import io.github.adven27.env.mq.kafka.embedded.EmbeddedKafkaSystem.Companion.PROP_BOOTSTRAPSERVERS | ||
import org.junit.After | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertNotNull | ||
import org.junit.Assert.assertTrue | ||
import org.junit.Test | ||
|
||
class EmbeddedKafkaSystemTest { | ||
private lateinit var sut: SomeEnvironment | ||
|
||
@Test | ||
fun embeddedKafkaSystemStartsInEnvironment() { | ||
sut = SomeEnvironment().apply { up() } | ||
|
||
assertTrue(sut.kafka().running()) | ||
|
||
val bootstrapServers = sut.kafka().config().bootstrapServers.value | ||
assertNotNull(bootstrapServers) | ||
assertEquals(bootstrapServers, System.getProperty(PROP_BOOTSTRAPSERVERS)) | ||
} | ||
|
||
@After | ||
fun tearDown() { | ||
sut.down() | ||
} | ||
} | ||
|
||
class SomeEnvironment : Environment( | ||
"EMBEDDED_KAFKA" to EmbeddedKafkaSystem(topics = arrayOf("some-topic")) | ||
) { | ||
fun kafka() = systems["EMBEDDED_KAFKA"] as EmbeddedKafkaSystem | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters