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

Commit

Permalink
env.wiremock.host + embedded kafka advertised listener config
Browse files Browse the repository at this point in the history
  • Loading branch information
Adven27 committed Oct 26, 2021
1 parent 0c3b78b commit cb3e9c5
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 25 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
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.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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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<EmbeddedKafkaBroker, EmbeddedKafkaSystem.Config>(
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 }
Expand All @@ -24,20 +33,19 @@ open class EmbeddedKafkaSystem(
constructor(
topics: Array<String>,
properties: MutableMap<String, String> = mutableMapOf(),
defaultPort: Int = DEFAULT_KAFKA_PORT,
advertisedHost: String? = null,
defaultPort: Int = DEFAULT_KAFKA_PORT
) : this(
EmbeddedKafkaBroker(
NUMBER_OF_BROKERS,
CONTROLLED_SHUTDOWN,
NUMBER_OF_PARTITIONS,
*topics
).brokerProperties(mapOf("group.initial.rebalance.delay.ms" to "0") + properties),
advertisedHost,
defaultPort
)

constructor(topics: Array<String>, properties: MutableMap<String, String> = mutableMapOf()) :
this(topics, properties, DEFAULT_KAFKA_PORT)

@Suppress("SpreadOperator")
constructor(vararg topics: String) : this(topics = arrayOf(*topics))

Expand All @@ -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",
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ open class WiremockSystem @JvmOverloads constructor(
configureJsonMapper()
start()
afterStart()
Config(port())
Config(port = port())
}
},
stop = { it.get()?.stop() },
Expand Down Expand Up @@ -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"
}
}

Expand Down

0 comments on commit cb3e9c5

Please sign in to comment.