diff --git a/README.md b/README.md index 367159a..66af81c 100644 --- a/README.md +++ b/README.md @@ -11,18 +11,18 @@ Idea is to be agnostic to tools used for specific external system emulation (e.g 1. Add needed dependencies: ```groovy -testImplementation "io.github.adven27:env-db-postgresql:4.0.4" -testImplementation "io.github.adven27:env-db-mssql:4.0.4" -testImplementation "io.github.adven27:env-db-mysql:4.0.4" -testImplementation "io.github.adven27:env-db-oracle:4.0.4" -testImplementation "io.github.adven27:env-db-oracle-temp:4.0.4" -testImplementation "io.github.adven27:env-db-db2:4.0.4" -testImplementation "io.github.adven27:env-jar-application:4.0.4" -testImplementation "io.github.adven27:env-mq-rabbit:4.0.4" -testImplementation "io.github.adven27:env-mq-ibmmq:4.0.4" -testImplementation "io.github.adven27:env-mq-redis:4.0.4" -testImplementation "io.github.adven27:env-grpc-mock:4.0.4" -testImplementation "io.github.adven27:env-wiremock:4.0.4" +testImplementation "io.github.adven27:env-db-postgresql:4.0.5" +testImplementation "io.github.adven27:env-db-mssql:4.0.5" +testImplementation "io.github.adven27:env-db-mysql:4.0.5" +testImplementation "io.github.adven27:env-db-oracle:4.0.5" +testImplementation "io.github.adven27:env-db-oracle-temp:4.0.5" +testImplementation "io.github.adven27:env-db-db2:4.0.5" +testImplementation "io.github.adven27:env-jar-application:4.0.5" +testImplementation "io.github.adven27:env-mq-rabbit:4.0.5" +testImplementation "io.github.adven27:env-mq-ibmmq:4.0.5" +testImplementation "io.github.adven27:env-mq-redis:4.0.5" +testImplementation "io.github.adven27:env-grpc-mock:4.0.5" +testImplementation "io.github.adven27:env-wiremock:4.0.5" ``` 2. Set up systems: diff --git a/build.gradle b/build.gradle index 0124f03..9808a70 100644 --- a/build.gradle +++ b/build.gradle @@ -3,7 +3,7 @@ buildscript { ext.testContainers_version = '1.16.0' ext.wiremock_version = '2.30.1' ext.klogging_version = '2.0.11' - ext.libVersion = "4.0.4" + ext.libVersion = "4.0.5" ext.libGroup = 'io.github.adven27' repositories { mavenCentral() diff --git a/env-core/src/main/java/io/github/adven27/env/core/Environment.kt b/env-core/src/main/java/io/github/adven27/env/core/Environment.kt index ab8acf8..01548a1 100644 --- a/env-core/src/main/java/io/github/adven27/env/core/Environment.kt +++ b/env-core/src/main/java/io/github/adven27/env/core/Environment.kt @@ -121,7 +121,9 @@ open class Environment @JvmOverloads constructor( .toTypedArray() @JvmStatic - fun findAvailableTcpPort(): Int = SocketUtils.findAvailableTcpPort() + @JvmOverloads + fun findAvailableTcpPort(minPort: Int = 1024, maxPort: Int = 65535): Int = + SocketUtils.findAvailableTcpPort(minPort, maxPort) @JvmStatic fun String.fromPropertyOrElse(orElse: Long) = System.getProperty(this, orElse.toString()).toLong() diff --git a/env-mq-kafka-embedded/src/main/java/io/github/adven27/env/mq/kafka/embedded/EmbeddedKafkaSystem.kt b/env-mq-kafka-embedded/src/main/java/io/github/adven27/env/mq/kafka/embedded/EmbeddedKafkaSystem.kt index 4a017eb..b4412d1 100644 --- a/env-mq-kafka-embedded/src/main/java/io/github/adven27/env/mq/kafka/embedded/EmbeddedKafkaSystem.kt +++ b/env-mq-kafka-embedded/src/main/java/io/github/adven27/env/mq/kafka/embedded/EmbeddedKafkaSystem.kt @@ -1,20 +1,29 @@ package io.github.adven27.env.mq.kafka.embedded +import io.github.adven27.env.core.Environment import io.github.adven27.env.core.ExternalSystemConfig import io.github.adven27.env.core.GenericExternalSystem import org.springframework.kafka.test.EmbeddedKafkaBroker @Suppress("unused") -open class EmbeddedKafkaSystem( +open class EmbeddedKafkaSystem @JvmOverloads constructor( private val embeddedKafka: EmbeddedKafkaBroker, + advertisedHost: String? = null, defaultPort: Int = DEFAULT_KAFKA_PORT, ) : GenericExternalSystem( system = embeddedKafka, config = Config(), start = { fixedEnv, system -> - if (fixedEnv) system.kafkaPorts(defaultPort) - system.afterPropertiesSet() - Config(system.brokersAsString) + val port = if (fixedEnv) defaultPort else Environment.findAvailableTcpPort() + var bootstrapServers = "localhost:$port" + advertisedHost?.let { + advertisedListener(it, port).also { (broker, props) -> + system.brokerProperties(props) + bootstrapServers += ", $broker" + } + } + system.kafkaPorts(port).afterPropertiesSet() + Config(bootstrapServers) }, stop = { embeddedKafka.destroy() }, running = { System.getProperty(EmbeddedKafkaBroker.SPRING_EMBEDDED_ZOOKEEPER_CONNECT) != null } @@ -24,7 +33,8 @@ open class EmbeddedKafkaSystem( constructor( topics: Array, properties: MutableMap = mutableMapOf(), - defaultPort: Int = DEFAULT_KAFKA_PORT, + advertisedHost: String? = null, + defaultPort: Int = DEFAULT_KAFKA_PORT ) : this( EmbeddedKafkaBroker( NUMBER_OF_BROKERS, @@ -32,12 +42,10 @@ open class EmbeddedKafkaSystem( NUMBER_OF_PARTITIONS, *topics ).brokerProperties(mapOf("group.initial.rebalance.delay.ms" to "0") + properties), + advertisedHost, defaultPort ) - constructor(topics: Array, properties: MutableMap = mutableMapOf()) : - this(topics, properties, DEFAULT_KAFKA_PORT) - @Suppress("SpreadOperator") constructor(vararg topics: String) : this(topics = arrayOf(*topics)) @@ -55,5 +63,14 @@ open class EmbeddedKafkaSystem( private const val NUMBER_OF_BROKERS = 1 private const val NUMBER_OF_PARTITIONS = 1 private const val CONTROLLED_SHUTDOWN = true + + private fun advertisedListener(host: String, port: Int) = + Environment.findAvailableTcpPort().let { + ("$host:$it") to mapOf( + "listeners" to "PLAINTEXT://:$port, REMOTE://:$it", + "advertised.listeners" to "PLAINTEXT://localhost:$port, REMOTE://$host:$it", + "listener.security.protocol.map" to "PLAINTEXT:PLAINTEXT, REMOTE:PLAINTEXT", + ) + } } } diff --git a/env-wiremock/src/main/java/io/github/adven27/env/wiremock/WiremockSystem.kt b/env-wiremock/src/main/java/io/github/adven27/env/wiremock/WiremockSystem.kt index b8be231..01d8c8e 100644 --- a/env-wiremock/src/main/java/io/github/adven27/env/wiremock/WiremockSystem.kt +++ b/env-wiremock/src/main/java/io/github/adven27/env/wiremock/WiremockSystem.kt @@ -38,7 +38,7 @@ open class WiremockSystem @JvmOverloads constructor( configureJsonMapper() start() afterStart() - Config(port()) + Config(port = port()) } }, stop = { it.get()?.stop() }, @@ -76,12 +76,17 @@ open class WiremockSystem @JvmOverloads constructor( config().properties.entries.joinToString("\n\t") { it.toString() } } - fun client() = WireMock(config().port) + fun client() = WireMock(config().host, config().port) - data class Config(val port: Int = DEFAULT_PORT) : ExternalSystemConfig(PROP_PORT to port.toString()) { + data class Config(val host: String = DEFAULT_HOST, val port: Int = DEFAULT_PORT) : ExternalSystemConfig( + PROP_PORT to port.toString(), + PROP_HOST to port.toString() + ) { companion object { const val PROP_PORT = "env.wiremock.port" + const val PROP_HOST = "env.wiremock.host" const val DEFAULT_PORT = 8888 + const val DEFAULT_HOST = "localhost" } }