All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.trendyol.stove.testing.e2e.kafka.Options.kt Maven / Gradle / Ivy

There is a newer version: 1f1ca59
Show newest version
package com.trendyol.stove.testing.e2e.kafka

import arrow.core.getOrElse
import com.fasterxml.jackson.databind.ObjectMapper
import com.trendyol.stove.testing.e2e.containers.ContainerOptions
import com.trendyol.stove.testing.e2e.containers.DEFAULT_REGISTRY
import com.trendyol.stove.testing.e2e.containers.withProvidedRegistry
import com.trendyol.stove.testing.e2e.serialization.StoveObjectMapper
import com.trendyol.stove.testing.e2e.system.TestSystem
import com.trendyol.stove.testing.e2e.system.ValidationDsl
import com.trendyol.stove.testing.e2e.system.WithDsl
import com.trendyol.stove.testing.e2e.system.abstractions.*
import org.testcontainers.containers.KafkaContainer

data class KafkaExposedConfiguration(
    val bootstrapServers: String
) : ExposedConfiguration

data class KafkaContainerOptions(
    override val registry: String = DEFAULT_REGISTRY,
    override val image: String = "confluentinc/cp-kafka",
    override val tag: String = "latest",
    override val imageWithTag: String = "$image:$tag"
) : ContainerOptions

data class KafkaSystemOptions(
    val registry: String = DEFAULT_REGISTRY,
    val ports: List = listOf(9092, 9093),
    val objectMapper: ObjectMapper = StoveObjectMapper.Default,
    val containerOptions: ContainerOptions = KafkaContainerOptions(),
    override val configureExposedConfiguration: (KafkaExposedConfiguration) -> List = { _ -> listOf() }
) : SystemOptions, ConfiguresExposedConfiguration

data class KafkaContext(
    val container: KafkaContainer,
    val objectMapper: ObjectMapper,
    val configureExposedConfiguration: (KafkaExposedConfiguration) -> List
)

fun TestSystem.withKafka(
    options: KafkaSystemOptions = KafkaSystemOptions()
): TestSystem = withProvidedRegistry(options.containerOptions.imageWithTag, registry = options.registry) {
    KafkaContainer(it).withExposedPorts(*options.ports.toTypedArray())
        .withEmbeddedZookeeper()
        .withReuse(this.options.keepDependenciesRunning)
}.let { getOrRegister(KafkaSystem(this, KafkaContext(it, options.objectMapper, options.configureExposedConfiguration))) }
    .let { this }

fun TestSystem.kafka(): KafkaSystem =
    getOrNone().getOrElse { throw SystemNotRegisteredException(KafkaSystem::class) }

@ExperimentalStoveDsl
fun WithDsl.kafka(configure: () -> KafkaSystemOptions = { KafkaSystemOptions() }): TestSystem =
    this.testSystem.withKafka(configure())

suspend fun ValidationDsl.kafka(validation: suspend KafkaSystem.() -> Unit): Unit =
    validation(this.testSystem.kafka())




© 2015 - 2024 Weber Informatics LLC | Privacy Policy