Skip to content

Commit

Permalink
better handling for key and indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
osoykan committed Jul 7, 2023
1 parent 45ba431 commit 2949db2
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.stove.ktor.example.e2e

import arrow.core.None
import arrow.core.some
import com.trendol.stove.testing.e2e.rdbms.postgres.postgresql
import com.trendyol.stove.testing.e2e.http.HttpSystem.Companion.postAndExpectBodilessResponse
import com.trendyol.stove.testing.e2e.http.http
import com.trendyol.stove.testing.e2e.rdbms.RelationalDatabaseSystem.Companion.shouldQuery
import com.trendyol.stove.testing.e2e.system.TestSystem
Expand Down Expand Up @@ -36,7 +36,8 @@ class ExampleTest : FunSpec({
http {
postAndExpectBodilessResponse(
"/jedis/$givenId",
body = UpdateJediRequest(givenName).some()
body = UpdateJediRequest(givenName).some(),
token = None
) { actual ->
actual.status shouldBe 200
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
@file:Suppress("UNCHECKED_CAST")

package com.trendyol.stove.testing.e2e.couchbase

import com.couchbase.client.core.error.DocumentNotFoundException
Expand Down Expand Up @@ -68,14 +66,13 @@ class CouchbaseSystem internal constructor(

override suspend fun stop(): Unit = context.container.stop()

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

@PublishedApi
internal suspend fun <T : Any> shouldQuery(
Expand Down Expand Up @@ -103,6 +100,19 @@ class CouchbaseSystem internal constructor(
.let(assertion)
.let { this }

@PublishedApi
internal suspend fun <T : Any> shouldGet(
collection: String,
key: String,
assertion: (T) -> Unit,
clazz: KClass<T>
): CouchbaseSystem = cluster.bucket(context.bucket.name)
.collection(collection)
.get(key).awaitSingle()
.contentAs(clazz.java)
.let(assertion)
.let { this }

suspend fun shouldNotExist(
key: String
): CouchbaseSystem = when (
Expand All @@ -118,23 +128,37 @@ class CouchbaseSystem internal constructor(
else -> throw AssertionError("The document with the given id($key) was not expected, but found!")
}

@PublishedApi
internal suspend fun <T : Any> shouldGet(
@Suppress("unused")
suspend fun shouldNotExist(
collection: String,
key: String,
assertion: (T) -> Unit,
clazz: KClass<T>
): CouchbaseSystem = cluster.bucket(context.bucket.name)
.collection(collection)
.get(key).awaitSingle()
.contentAs(clazz.java)
.let(assertion)
.let { this }
key: String
): CouchbaseSystem = when (
cluster
.bucket(context.bucket.name)
.collection(collection)
.get(key)
.onErrorResume { throwable ->
when (throwable) {
is DocumentNotFoundException -> Mono.empty()
else -> throw throwable
}
}.awaitFirstOrNull()
) {
null -> this
else -> throw AssertionError("The document with the given id($key) was not expected, but found!")
}

suspend fun shouldDelete(key: String): CouchbaseSystem {
@Suppress("unused")
suspend fun shouldDelete(key: String): CouchbaseSystem =
collection.remove(key).awaitSingle()
return this
}
.let { this }

@Suppress("unused")
suspend fun shouldDelete(collection: String, key: String): CouchbaseSystem =
cluster.bucket(context.bucket.name)
.collection(collection)
.remove(key)
.awaitSingle().let { this }

/**
* Saves the [instance] with given [id] to the [collection]
Expand All @@ -144,19 +168,15 @@ class CouchbaseSystem internal constructor(
collection: String,
id: String,
instance: T
): CouchbaseSystem {
cluster
.bucket(context.bucket.name)
.collection(collection)
.insert(
id,
JsonObject.fromJson(objectMapper.writeValueAsString(instance)),
InsertOptions.insertOptions().durability(PERSIST_TO_MAJORITY)
)
.awaitSingle()

return this
}
): CouchbaseSystem = cluster
.bucket(context.bucket.name)
.collection(collection)
.insert(
id,
JsonObject.fromJson(objectMapper.writeValueAsString(instance)),
InsertOptions.insertOptions().durability(PERSIST_TO_MAJORITY)
)
.awaitSingle().let { this }

/**
* Saves the [instance] with given [id] to the default collection
Expand Down Expand Up @@ -201,5 +221,11 @@ class CouchbaseSystem internal constructor(
key: String,
noinline assertion: (T) -> Unit
): CouchbaseSystem = this.shouldGet(key, assertion, T::class)

@Suppress("unused")
suspend inline fun <reified T : Any> CouchbaseSystem.shouldQuery(
query: String,
noinline assertion: (List<T>) -> Unit
): CouchbaseSystem = this.shouldQuery(query, assertion, T::class)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import kotlin.reflect.KClass

class ElasticsearchSystem internal constructor(
override val testSystem: TestSystem,
private val context: ElasticsearchContext
val context: ElasticsearchContext
) : PluggedSystem, RunAware, AfterRunAware, ExposesConfiguration {
private lateinit var esClient: ElasticsearchClient
private lateinit var exposedConfiguration: ElasticSearchExposedConfiguration
Expand Down Expand Up @@ -105,18 +105,7 @@ class ElasticsearchSystem internal constructor(

@PublishedApi
internal fun <T : Any> shouldGet(
key: String,
assertion: (T) -> Unit,
clazz: KClass<T>
): ElasticsearchSystem = esClient.get({ req -> req.index(context.index).id(key).refresh(true) }, clazz.java)
.source().toOption()
.map(assertion)
.orElse { throw AssertionError("Resource with key ($key) is not found") }
.let { this }

@PublishedApi
internal fun <T : Any> shouldGet(
index: String,
index: String = context.index,
key: String,
assertion: (T) -> Unit,
clazz: KClass<T>
Expand All @@ -130,34 +119,29 @@ class ElasticsearchSystem internal constructor(
.let { this }
}

fun shouldNotExist(key: String): ElasticsearchSystem {
val exists = esClient.exists { req -> req.index(context.index).id(key) }
fun shouldNotExist(key: String, onIndex: String = context.index): ElasticsearchSystem {
val exists = esClient.exists { req -> req.index(onIndex).id(key) }
if (exists.value()) {
throw AssertionError("The document with the given id($key) was not expected, but found!")
}
return this
}

fun shouldDelete(key: String): ElasticsearchSystem = esClient
.delete(DeleteRequest.of { req -> req.index(context.index).id(key).refresh(Refresh.WaitFor) })
fun shouldDelete(key: String, fromIndex: String = context.index): ElasticsearchSystem = esClient
.delete(DeleteRequest.of { req -> req.index(fromIndex).id(key).refresh(Refresh.WaitFor) })
.let { this }

fun <T : Any> save(
collection: String,
id: String,
instance: T
instance: T,
toIndex: String = context.index
): ElasticsearchSystem = esClient.index { req ->
req.index(collection)
req.index(toIndex)
.id(id)
.document(instance)
.refresh(Refresh.WaitFor)
}.let { this }

fun <T : Any> save(
id: String,
instance: T
): ElasticsearchSystem = save(context.index, id, instance)

override fun close(): Unit = runBlocking(context = Dispatchers.IO) {
Try {
esClient._transport().close()
Expand Down Expand Up @@ -224,19 +208,9 @@ class ElasticsearchSystem internal constructor(
): ElasticsearchSystem = this.shouldQuery(query, assertion, T::class)

inline fun <reified T : Any> ElasticsearchSystem.shouldGet(
index: String,
key: String,
index: String = context.index,
noinline assertion: (T) -> Unit
): ElasticsearchSystem = this.shouldGet(index, key, assertion, T::class)

/**
* Finds the given [id] and returns the instance if exists, otherwise throws [Exception]
* Caller-side needs to assert based on the list
*
*/
inline fun <reified T : Any> ElasticsearchSystem.shouldGet(
id: String,
noinline assertion: (T) -> Unit
): ElasticsearchSystem = this.shouldGet(id, assertion, T::class)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ class ElasticsearchTestSystemTests : FunSpec({
val exampleInstance = ExampleInstance("1", "1312")
TestSystem.validate {
elasticsearch {
save(anotherIndex, exampleInstance.id, exampleInstance)
shouldGet<ExampleInstance>(anotherIndex, exampleInstance.id) {
save(exampleInstance.id, exampleInstance, anotherIndex)
shouldGet<ExampleInstance>(exampleInstance.id, anotherIndex) {
it.description shouldBe exampleInstance.description
}
}
Expand Down

0 comments on commit 2949db2

Please sign in to comment.