Skip to content
This repository has been archived by the owner on Sep 22, 2022. It is now read-only.

Commit

Permalink
Add embedded kafka system (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
MetallFoX authored Jun 3, 2021
1 parent af0731d commit 74d2871
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class SomeEnvironment : Environment(
mapOf(
"RABBIT" to RabbitContainerSystem(),
"IBMMQ" to IbmMQContainerSystem(),
"KAFKA" to KafkaContainerSystem(),
"REDIS" to RedisContainerSystem(),
"POSTGRES" to PostgreSqlContainerSystem(),
"ORACLE" to OracleContainerSystem(),
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ buildscript {
ext.testContainers_version = '1.15.2'
ext.wiremock_version = '2.27.2'
ext.klogging_version = '2.0.6'
ext.libVersion = "2.1.0"
ext.libVersion = "2.2.0"
ext.libGroup = 'io.github.adven27'
repositories {
mavenCentral()
Expand Down
11 changes: 11 additions & 0 deletions env-mq-kafka-embedded/build.gradle
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")
}
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"
}
}
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
}
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ include 'env-db-postgresql'
include 'env-db-db2'
include 'env-db-oracle'
include 'env-mq-kafka'
include 'env-mq-kafka-embedded'
include 'env-mq-rabbit'
include 'env-mq-redis'
include 'env-mq-ibmmq'
Expand Down

0 comments on commit 74d2871

Please sign in to comment.