Skip to content

Commit

Permalink
Expose clients and connections for the extensions to provide an exten…
Browse files Browse the repository at this point in the history
…sion point to the end users
  • Loading branch information
osoykan committed Aug 1, 2023
1 parent b67e503 commit 8cae98f
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 29 deletions.
27 changes: 21 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,34 @@ TestSystem(baseUrl = "http://localhost:8001") {
keepDendenciesRunning() // this will keep the dependencies running after the tests are finished, so next run will be blazing fast :)
}
}.with {
httpClient() // Enables http client to make real http calls against the application under test

couchbase { // Enables Couchbase physically and exposes the configuration to the application under test
// Enables http client
// to make real http calls
// against the application under test
http()

// Enables Couchbase physically
// and exposes the configuration
// to the application under test
couchbase {
CouchbaseSystemOptions(
defaultBucket = "Stove",
configureExposedConfiguration = { cfg -> listOf("couchbase.hosts=${cfg.hostsWithPort}") },
)
}

kafka { // Enables Kafka physically and exposes the configuration to the application under test
// Enables Kafka physically
// and exposes the configuration
// to the application under test
kafka {
KafkaSystemOptions(
configureExposedConfiguration = { cfg -> listOf("kafka.bootstrapServers=${cfg.boostrapServers}") },
)
}

wiremock { // Enables Wiremock on the given port and provides configurable mock HTTP server for your external API calls
// Enables Wiremock on the given port
// and provides configurable mock HTTP server
// for your external API calls
wiremock {
WireMockSystemOptions(
port = 9090,
removeStubAfterRequestMatched = true,
Expand All @@ -60,7 +72,10 @@ TestSystem(baseUrl = "http://localhost:8001") {
)
}

springBoot( // The Application Under Test. Enables Spring Boot application to be run with the given parameters.
// The Application Under Test.
// Enables Spring Boot application
// to be run with the given parameters.
springBoot(
runner = { parameters ->
stove.spring.example.run(parameters) { it.addTestSystemDependencies() }
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ package com.trendyol.stove.testing.e2e.couchbase

import com.couchbase.client.core.error.DocumentNotFoundException
import com.couchbase.client.core.msg.kv.DurabilityLevel.PERSIST_TO_MAJORITY
import com.couchbase.client.java.Cluster
import com.couchbase.client.java.ClusterOptions
import com.couchbase.client.java.ReactiveCluster
import com.couchbase.client.java.ReactiveCollection
import com.couchbase.client.java.*
import com.couchbase.client.java.codec.JacksonJsonSerializer
import com.couchbase.client.java.env.ClusterEnvironment
import com.couchbase.client.java.json.JsonObject
Expand Down Expand Up @@ -68,11 +65,11 @@ class CouchbaseSystem internal constructor(

override fun configuration(): List<String> =
context.options.configureExposedConfiguration(exposedConfiguration) +
listOf(
"couchbase.hosts=${exposedConfiguration.hostsWithPort}",
"couchbase.username=${exposedConfiguration.username}",
"couchbase.password=${exposedConfiguration.password}"
)
listOf(
"couchbase.hosts=${exposedConfiguration.hostsWithPort}",
"couchbase.username=${exposedConfiguration.username}",
"couchbase.password=${exposedConfiguration.password}"
)

@PublishedApi
internal suspend fun <T : Any> shouldQuery(
Expand Down Expand Up @@ -211,6 +208,18 @@ class CouchbaseSystem internal constructor(

companion object {

/**
* Exposes the [ReactiveCluster] of the [CouchbaseSystem]
*/
@Suppress("unused")
fun CouchbaseSystem.cluster(): ReactiveCluster = this.cluster

/**
* Exposes the [ReactiveBucket] of the [CouchbaseSystem]
*/
@Suppress("unused")
fun CouchbaseSystem.bucket(): ReactiveBucket = this.cluster.bucket(this.context.bucket.name)

suspend inline fun <reified T : Any> CouchbaseSystem.shouldGet(
collection: String,
key: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,10 @@ class ElasticsearchSystem internal constructor(
}

override fun configuration(): List<String> = context.options.configureExposedConfiguration(exposedConfiguration) +
listOf(
"elasticsearch.host=${exposedConfiguration.host}",
"elasticsearch.port=${exposedConfiguration.port}"
)
listOf(
"elasticsearch.host=${exposedConfiguration.host}",
"elasticsearch.port=${exposedConfiguration.port}"
)

private fun createEsClient(
exposedConfiguration: ElasticSearchExposedConfiguration
Expand Down Expand Up @@ -192,6 +192,14 @@ class ElasticsearchSystem internal constructor(
}

companion object {

/**
* Exposes the [ElasticsearchClient] for the given [ElasticsearchSystem]
* This is useful for custom queries
*/
@Suppress("unused")
fun ElasticsearchSystem.client(): ElasticsearchClient = this.esClient

/**
* Executes the given [query] and returns a list for [assertion]
* Caller-side needs to assert based on the list
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@ class HttpSystem(
fun bearer(token: String) = "Bearer $token"
}

/**
* Exposes the [HttpClient] used by the [HttpSystem].
*/
@Suppress("unused")
fun HttpSystem.client(): HttpClient = this.httpClient

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ class MongodbSystem internal constructor(

override fun configuration(): List<String> {
return context.options.configureExposedConfiguration(exposedConfiguration) +
listOf(
"mongodb.connectionString=${exposedConfiguration.connectionString}"
)
listOf(
"mongodb.connectionString=${exposedConfiguration.connectionString}"
)
}

@PublishedApi
Expand Down Expand Up @@ -157,6 +157,12 @@ class MongodbSystem internal constructor(
private const val RESERVED_ID = "_id"
private fun filterById(key: String): Bson = eq(RESERVED_ID, ObjectId(key))

/**
* Exposes the [MongoClient] to the [MongodbSystem]
*/
@Suppress("unused")
fun MongodbSystem.client(): MongoClient = mongoClient

suspend inline fun <reified T : Any> MongodbSystem.shouldQuery(
query: String,
noinline assertion: (List<T>) -> Unit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ abstract class RelationalDatabaseSystem<SELF : RelationalDatabaseSystem<SELF>> p
}

companion object {

/**
* Exposes the [SqlOperations] of the [RelationalDatabaseSystem].
*/
@Suppress("unused")
fun RelationalDatabaseSystem<*>.operations(): SqlOperations = this.sqlOperations

suspend inline fun <reified T : Any> RelationalDatabaseSystem<*>.shouldQuery(
id: String,
noinline assertion: (List<T>) -> Unit
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package com.trendyol.stove.testing.e2e.rdbms

import io.r2dbc.spi.Connection
import io.r2dbc.spi.ConnectionFactory
import io.r2dbc.spi.IsolationLevel
import io.r2dbc.spi.Row
import io.r2dbc.spi.RowMetadata
import io.r2dbc.spi.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.reactive.asFlow
import kotlinx.coroutines.reactive.awaitFirst
Expand All @@ -13,7 +9,7 @@ import kotlinx.coroutines.reactive.awaitFirstOrNull
/**
* An R2DBC abstraction that uses Kotlin coroutines.
*/
internal class SqlOperations(private val connectionFactory: ConnectionFactory) {
class SqlOperations(private val connectionFactory: ConnectionFactory) {

private lateinit var connection: Connection

Expand Down Expand Up @@ -48,7 +44,7 @@ internal class SqlOperations(private val connectionFactory: ConnectionFactory) {
/**
* Wrapper for [Connection].
*/
internal class Handle(val connection: Connection) {
class Handle(val connection: Connection) {

/**
* Change transaction isolation level.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,13 @@ class WireMockSystem(
responseBody.map { mockResponse.withBody(json.writeValueAsBytes(it)) }
return mockResponse
}

companion object {

/**
* Exposes the [WireMockServer] instance for the given [WireMockSystem].
*/
@Suppress("unused")
fun WireMockSystem.server(): WireMockServer = wireMock
}
}

0 comments on commit 8cae98f

Please sign in to comment.