Skip to content

Commit

Permalink
breaking: move baseUrl to httpClient system options (#494)
Browse files Browse the repository at this point in the history
  • Loading branch information
osoykan authored Jun 27, 2024
1 parent fbad811 commit a2e78e8
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 102 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,4 @@ kotlin-ide/

# Ignore Gradle build output directory
build
.kotlin
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Frameworks:
### Setting-up all the physical dependencies with application

```kotlin
TestSystem(baseUrl = "http://localhost:8001") {
TestSystem() {
if (isRunningLocally()) {
enableReuseForTestContainers()
keepDendenciesRunning() // this will keep the dependencies running after the tests are finished, so next run will be blazing fast :)
Expand All @@ -50,7 +50,11 @@ TestSystem(baseUrl = "http://localhost:8001") {
// Enables http client
// to make real http calls
// against the application under test
http()
httpClient {
HttpClientSystemOptions(
baseUrl = "http://localhost:8001",
)
}

// Enables Couchbase physically
// and exposes the configuration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ it is time to run your application for the first time from the test-context with
class TestSystemConfig : AbstractProjectConfig() {

override suspend fun beforeProject(): Unit =
TestSystem(baseUrl = "http://localhost:8001")
TestSystem()
.with {
httpClient()
httpClient {
HttpClientSystemOptions {
baseUrl = "http://localhost:8001"
}
}
springBoot(
runner = { parameters ->
/*
Expand Down Expand Up @@ -49,9 +53,13 @@ it is time to run your application for the first time from the test-context with

@BeforeAll
fun beforeProject() = runBlocking {
TestSystem(baseUrl = "http://localhost:8001")
TestSystem()
.with {
httpClient()
httpClient {
HttpClientSystemOptions {
baseUrl = "http://localhost:8001"
}
}
springBoot(
runner = { parameters ->
/*
Expand Down
8 changes: 6 additions & 2 deletions docs/how-to-write-tests/1.Application-Aware/2.Ktor/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@
## Example Setup

```kotlin
TestSystem(baseUrl = "http://localhost:8080")
TestSystem()
.with {
httpClient()
httpClient {
HttpClientSystemOptions {
baseUrl = "http://localhost:8080"
}
}
bridge()
postgresql {
PostgresqlOptions(configureExposedConfiguration = { cfg ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.stove.ktor.example.e2e

import com.trendol.stove.testing.e2e.rdbms.postgres.*
import com.trendyol.stove.testing.e2e.*
import com.trendyol.stove.testing.e2e.http.httpClient
import com.trendyol.stove.testing.e2e.http.*
import com.trendyol.stove.testing.e2e.standalone.kafka.*
import com.trendyol.stove.testing.e2e.system.*
import com.trendyol.stove.testing.e2e.system.abstractions.*
Expand Down Expand Up @@ -31,57 +31,59 @@ class GitlabStateStorageFactory : StateStorageFactory {
class TestSystemConfig : AbstractProjectConfig() {
private val logger: Logger = LoggerFactory.getLogger("WireMockMonitor")

override suspend fun beforeProject() =
TestSystem(baseUrl = "http://localhost:8080") {
// this.stateStorage(GitlabStateStorageFactory())
if (this.isRunningLocally()) {
enableReuseForTestContainers()
keepDependenciesRunning()
}
}.with {
httpClient()
bridge()
postgresql {
PostgresqlOptions(configureExposedConfiguration = { cfg ->
listOf(
"database.jdbcUrl=${cfg.jdbcUrl}",
"database.host=${cfg.host}",
"database.port=${cfg.port}",
"database.name=${cfg.database}",
"database.username=${cfg.username}",
"database.password=${cfg.password}"
)
})
}
kafka {
stoveKafkaObjectMapperRef = objectMapperRef
KafkaSystemOptions {
listOf(
"kafka.bootstrapServers=${it.bootstrapServers}",
"kafka.interceptorClasses=${it.interceptorClass}"
)
}
}
wiremock {
WireMockSystemOptions(
port = 9090,
removeStubAfterRequestMatched = true,
afterRequest = { e, _ ->
logger.info(e.request.toString())
}
override suspend fun beforeProject() = TestSystem {
if (this.isRunningLocally()) {
enableReuseForTestContainers()
keepDependenciesRunning()
}
}.with {
httpClient {
HttpClientSystemOptions(
baseUrl = "http://localhost:8080"
)
}
bridge()
postgresql {
PostgresqlOptions(configureExposedConfiguration = { cfg ->
listOf(
"database.jdbcUrl=${cfg.jdbcUrl}",
"database.host=${cfg.host}",
"database.port=${cfg.port}",
"database.name=${cfg.database}",
"database.username=${cfg.username}",
"database.password=${cfg.password}"
)
})
}
kafka {
stoveKafkaObjectMapperRef = objectMapperRef
KafkaSystemOptions {
listOf(
"kafka.bootstrapServers=${it.bootstrapServers}",
"kafka.interceptorClasses=${it.interceptorClass}"
)
}
ktor(
withParameters = listOf(
"port=8080"
),
runner = { parameters ->
stove.ktor.example.run(parameters) {
addTestSystemDependencies()
}
}
wiremock {
WireMockSystemOptions(
port = 9090,
removeStubAfterRequestMatched = true,
afterRequest = { e, _ ->
logger.info(e.request.toString())
}
)
}.run()
}
ktor(
withParameters = listOf(
"port=8080"
),
runner = { parameters ->
stove.ktor.example.run(parameters) {
addTestSystemDependencies()
}
}
)
}.run()

override suspend fun afterProject() {
TestSystem.stop()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.stove.spring.example.e2e

import com.trendyol.stove.testing.e2e.*
import com.trendyol.stove.testing.e2e.couchbase.*
import com.trendyol.stove.testing.e2e.http.httpClient
import com.trendyol.stove.testing.e2e.http.*
import com.trendyol.stove.testing.e2e.kafka.*
import com.trendyol.stove.testing.e2e.system.TestSystem
import com.trendyol.stove.testing.e2e.wiremock.*
Expand All @@ -12,10 +12,15 @@ import org.slf4j.*
class TestSystemConfig : AbstractProjectConfig() {
private val logger: Logger = LoggerFactory.getLogger("WireMockMonitor")

@Suppress("LongMethod")
override suspend fun beforeProject(): Unit =
TestSystem(baseUrl = "http://localhost:8001")
TestSystem()
.with {
httpClient()
httpClient {
HttpClientSystemOptions(
baseUrl = "http://localhost:8001"
)
}
couchbase {
CouchbaseSystemOptions(
"Stove",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.stove.spring.standalone.example.e2e

import com.trendyol.stove.testing.e2e.*
import com.trendyol.stove.testing.e2e.couchbase.*
import com.trendyol.stove.testing.e2e.http.httpClient
import com.trendyol.stove.testing.e2e.http.*
import com.trendyol.stove.testing.e2e.standalone.kafka.*
import com.trendyol.stove.testing.e2e.system.TestSystem
import com.trendyol.stove.testing.e2e.wiremock.*
Expand All @@ -13,10 +13,15 @@ import stove.spring.standalone.example.infrastructure.ObjectMapperConfig
class TestSystemConfig : AbstractProjectConfig() {
private val logger: Logger = LoggerFactory.getLogger("WireMockMonitor")

@Suppress("LongMethod")
override suspend fun beforeProject(): Unit =
TestSystem(baseUrl = "http://localhost:8001")
TestSystem()
.with {
httpClient()
httpClient {
HttpClientSystemOptions(
baseUrl = "http://localhost:8001"
)
}
couchbase {
CouchbaseSystemOptions(
"Stove",
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ projectUrl=https://github.com/Trendyol/stove
licenceUrl=https://github.com/Trendyol/stove/blob/master/LICENCE
licence=Apache-2.0 license
snapshot=1.0.0-SNAPSHOT
kotlin.daemon.jvmargs=-Xmx1536m

Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ import kotlin.time.Duration.Companion.seconds
import kotlin.time.toJavaDuration

@HttpDsl
data class HttpClientSystemOptions(val objectMapper: ObjectMapper = StoveObjectMapper.Default) : SystemOptions
data class HttpClientSystemOptions(
val baseUrl: String,
val objectMapper: ObjectMapper = StoveObjectMapper.Default
) : SystemOptions

internal fun TestSystem.withHttpClient(options: HttpClientSystemOptions = HttpClientSystemOptions()): TestSystem {
this.getOrRegister(HttpSystem(this, options.objectMapper))
internal fun TestSystem.withHttpClient(options: HttpClientSystemOptions): TestSystem {
this.getOrRegister(HttpSystem(this, options))
return this
}

Expand All @@ -37,7 +40,7 @@ internal fun TestSystem.http(): HttpSystem = getOrNone<HttpSystem>().getOrElse {
}

@StoveDsl
fun WithDsl.httpClient(configure: @StoveDsl () -> HttpClientSystemOptions = { HttpClientSystemOptions() }): TestSystem =
fun WithDsl.httpClient(configure: @StoveDsl () -> HttpClientSystemOptions): TestSystem =
this.testSystem.withHttpClient(configure())

@StoveDsl
Expand All @@ -48,7 +51,7 @@ suspend fun ValidationDsl.http(
@HttpDsl
class HttpSystem(
override val testSystem: TestSystem,
@PublishedApi internal val objectMapper: ObjectMapper
@PublishedApi internal val options: HttpClientSystemOptions
) : PluggedSystem {
private val logger: org.slf4j.Logger = LoggerFactory.getLogger(javaClass)

Expand Down Expand Up @@ -232,7 +235,7 @@ class HttpSystem(
}

@PublishedApi
internal fun relative(uri: String): Url = URLBuilder(testSystem.baseUrl)
internal fun relative(uri: String): Url = URLBuilder(options.baseUrl)
.apply { appendEncodedPathSegments(uri) }.build()

@PublishedApi
Expand Down Expand Up @@ -281,7 +284,7 @@ class HttpSystem(
}

install(ContentNegotiation) {
register(ContentType.Application.Json, JacksonConverter(objectMapper))
register(ContentType.Application.Json, JacksonConverter(options.objectMapper))
}

defaultRequest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ class NoApplication : ApplicationUnderTest<Unit> {

class TestConfig : AbstractProjectConfig() {
override suspend fun beforeProject(): Unit =
TestSystem("http://localhost:8086")
TestSystem()
.with {
httpClient {
HttpClientSystemOptions(
baseUrl = "http://localhost:8086",
objectMapper = StoveObjectMapper.byConfiguring {
findAndRegisterModules()
}
Expand Down
Loading

0 comments on commit a2e78e8

Please sign in to comment.