Skip to content

Commit 2949db2

Browse files
committed
better handling for key and indexes
1 parent 45ba431 commit 2949db2

File tree

4 files changed

+73
-72
lines changed

4 files changed

+73
-72
lines changed

examples/ktor-example/src/test/kotlin/com/stove/ktor/example/e2e/ExampleTest.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.stove.ktor.example.e2e
22

3+
import arrow.core.None
34
import arrow.core.some
45
import com.trendol.stove.testing.e2e.rdbms.postgres.postgresql
5-
import com.trendyol.stove.testing.e2e.http.HttpSystem.Companion.postAndExpectBodilessResponse
66
import com.trendyol.stove.testing.e2e.http.http
77
import com.trendyol.stove.testing.e2e.rdbms.RelationalDatabaseSystem.Companion.shouldQuery
88
import com.trendyol.stove.testing.e2e.system.TestSystem
@@ -36,7 +36,8 @@ class ExampleTest : FunSpec({
3636
http {
3737
postAndExpectBodilessResponse(
3838
"/jedis/$givenId",
39-
body = UpdateJediRequest(givenName).some()
39+
body = UpdateJediRequest(givenName).some(),
40+
token = None
4041
) { actual ->
4142
actual.status shouldBe 200
4243
}

lib/stove-testing-e2e-couchbase/src/main/kotlin/com/trendyol/stove/testing/e2e/couchbase/CouchbaseSystem.kt

Lines changed: 58 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
@file:Suppress("UNCHECKED_CAST")
2-
31
package com.trendyol.stove.testing.e2e.couchbase
42

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

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

71-
override fun configuration(): List<String> {
72-
return context.options.configureExposedConfiguration(exposedConfiguration) +
69+
override fun configuration(): List<String> =
70+
context.options.configureExposedConfiguration(exposedConfiguration) +
7371
listOf(
7472
"couchbase.hosts=${exposedConfiguration.hostsWithPort}",
7573
"couchbase.username=${exposedConfiguration.username}",
7674
"couchbase.password=${exposedConfiguration.password}"
7775
)
78-
}
7976

8077
@PublishedApi
8178
internal suspend fun <T : Any> shouldQuery(
@@ -103,6 +100,19 @@ class CouchbaseSystem internal constructor(
103100
.let(assertion)
104101
.let { this }
105102

103+
@PublishedApi
104+
internal suspend fun <T : Any> shouldGet(
105+
collection: String,
106+
key: String,
107+
assertion: (T) -> Unit,
108+
clazz: KClass<T>
109+
): CouchbaseSystem = cluster.bucket(context.bucket.name)
110+
.collection(collection)
111+
.get(key).awaitSingle()
112+
.contentAs(clazz.java)
113+
.let(assertion)
114+
.let { this }
115+
106116
suspend fun shouldNotExist(
107117
key: String
108118
): CouchbaseSystem = when (
@@ -118,23 +128,37 @@ class CouchbaseSystem internal constructor(
118128
else -> throw AssertionError("The document with the given id($key) was not expected, but found!")
119129
}
120130

121-
@PublishedApi
122-
internal suspend fun <T : Any> shouldGet(
131+
@Suppress("unused")
132+
suspend fun shouldNotExist(
123133
collection: String,
124-
key: String,
125-
assertion: (T) -> Unit,
126-
clazz: KClass<T>
127-
): CouchbaseSystem = cluster.bucket(context.bucket.name)
128-
.collection(collection)
129-
.get(key).awaitSingle()
130-
.contentAs(clazz.java)
131-
.let(assertion)
132-
.let { this }
134+
key: String
135+
): CouchbaseSystem = when (
136+
cluster
137+
.bucket(context.bucket.name)
138+
.collection(collection)
139+
.get(key)
140+
.onErrorResume { throwable ->
141+
when (throwable) {
142+
is DocumentNotFoundException -> Mono.empty()
143+
else -> throw throwable
144+
}
145+
}.awaitFirstOrNull()
146+
) {
147+
null -> this
148+
else -> throw AssertionError("The document with the given id($key) was not expected, but found!")
149+
}
133150

134-
suspend fun shouldDelete(key: String): CouchbaseSystem {
151+
@Suppress("unused")
152+
suspend fun shouldDelete(key: String): CouchbaseSystem =
135153
collection.remove(key).awaitSingle()
136-
return this
137-
}
154+
.let { this }
155+
156+
@Suppress("unused")
157+
suspend fun shouldDelete(collection: String, key: String): CouchbaseSystem =
158+
cluster.bucket(context.bucket.name)
159+
.collection(collection)
160+
.remove(key)
161+
.awaitSingle().let { this }
138162

139163
/**
140164
* Saves the [instance] with given [id] to the [collection]
@@ -144,19 +168,15 @@ class CouchbaseSystem internal constructor(
144168
collection: String,
145169
id: String,
146170
instance: T
147-
): CouchbaseSystem {
148-
cluster
149-
.bucket(context.bucket.name)
150-
.collection(collection)
151-
.insert(
152-
id,
153-
JsonObject.fromJson(objectMapper.writeValueAsString(instance)),
154-
InsertOptions.insertOptions().durability(PERSIST_TO_MAJORITY)
155-
)
156-
.awaitSingle()
157-
158-
return this
159-
}
171+
): CouchbaseSystem = cluster
172+
.bucket(context.bucket.name)
173+
.collection(collection)
174+
.insert(
175+
id,
176+
JsonObject.fromJson(objectMapper.writeValueAsString(instance)),
177+
InsertOptions.insertOptions().durability(PERSIST_TO_MAJORITY)
178+
)
179+
.awaitSingle().let { this }
160180

161181
/**
162182
* Saves the [instance] with given [id] to the default collection
@@ -201,5 +221,11 @@ class CouchbaseSystem internal constructor(
201221
key: String,
202222
noinline assertion: (T) -> Unit
203223
): CouchbaseSystem = this.shouldGet(key, assertion, T::class)
224+
225+
@Suppress("unused")
226+
suspend inline fun <reified T : Any> CouchbaseSystem.shouldQuery(
227+
query: String,
228+
noinline assertion: (List<T>) -> Unit
229+
): CouchbaseSystem = this.shouldQuery(query, assertion, T::class)
204230
}
205231
}

lib/stove-testing-e2e-elasticsearch/src/main/kotlin/com/trendyol/stove/testing/e2e/elasticsearch/ElasticsearchSystem.kt

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import kotlin.reflect.KClass
3434

3535
class ElasticsearchSystem internal constructor(
3636
override val testSystem: TestSystem,
37-
private val context: ElasticsearchContext
37+
val context: ElasticsearchContext
3838
) : PluggedSystem, RunAware, AfterRunAware, ExposesConfiguration {
3939
private lateinit var esClient: ElasticsearchClient
4040
private lateinit var exposedConfiguration: ElasticSearchExposedConfiguration
@@ -105,18 +105,7 @@ class ElasticsearchSystem internal constructor(
105105

106106
@PublishedApi
107107
internal fun <T : Any> shouldGet(
108-
key: String,
109-
assertion: (T) -> Unit,
110-
clazz: KClass<T>
111-
): ElasticsearchSystem = esClient.get({ req -> req.index(context.index).id(key).refresh(true) }, clazz.java)
112-
.source().toOption()
113-
.map(assertion)
114-
.orElse { throw AssertionError("Resource with key ($key) is not found") }
115-
.let { this }
116-
117-
@PublishedApi
118-
internal fun <T : Any> shouldGet(
119-
index: String,
108+
index: String = context.index,
120109
key: String,
121110
assertion: (T) -> Unit,
122111
clazz: KClass<T>
@@ -130,34 +119,29 @@ class ElasticsearchSystem internal constructor(
130119
.let { this }
131120
}
132121

133-
fun shouldNotExist(key: String): ElasticsearchSystem {
134-
val exists = esClient.exists { req -> req.index(context.index).id(key) }
122+
fun shouldNotExist(key: String, onIndex: String = context.index): ElasticsearchSystem {
123+
val exists = esClient.exists { req -> req.index(onIndex).id(key) }
135124
if (exists.value()) {
136125
throw AssertionError("The document with the given id($key) was not expected, but found!")
137126
}
138127
return this
139128
}
140129

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

145134
fun <T : Any> save(
146-
collection: String,
147135
id: String,
148-
instance: T
136+
instance: T,
137+
toIndex: String = context.index
149138
): ElasticsearchSystem = esClient.index { req ->
150-
req.index(collection)
139+
req.index(toIndex)
151140
.id(id)
152141
.document(instance)
153142
.refresh(Refresh.WaitFor)
154143
}.let { this }
155144

156-
fun <T : Any> save(
157-
id: String,
158-
instance: T
159-
): ElasticsearchSystem = save(context.index, id, instance)
160-
161145
override fun close(): Unit = runBlocking(context = Dispatchers.IO) {
162146
Try {
163147
esClient._transport().close()
@@ -224,19 +208,9 @@ class ElasticsearchSystem internal constructor(
224208
): ElasticsearchSystem = this.shouldQuery(query, assertion, T::class)
225209

226210
inline fun <reified T : Any> ElasticsearchSystem.shouldGet(
227-
index: String,
228211
key: String,
212+
index: String = context.index,
229213
noinline assertion: (T) -> Unit
230214
): ElasticsearchSystem = this.shouldGet(index, key, assertion, T::class)
231-
232-
/**
233-
* Finds the given [id] and returns the instance if exists, otherwise throws [Exception]
234-
* Caller-side needs to assert based on the list
235-
*
236-
*/
237-
inline fun <reified T : Any> ElasticsearchSystem.shouldGet(
238-
id: String,
239-
noinline assertion: (T) -> Unit
240-
): ElasticsearchSystem = this.shouldGet(id, assertion, T::class)
241215
}
242216
}

lib/stove-testing-e2e-elasticsearch/src/test/kotlin/com/trendyol/stove/testing/e2e/elasticsearch/ElasticsearchTestSystemTests.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ class ElasticsearchTestSystemTests : FunSpec({
9797
val exampleInstance = ExampleInstance("1", "1312")
9898
TestSystem.validate {
9999
elasticsearch {
100-
save(anotherIndex, exampleInstance.id, exampleInstance)
101-
shouldGet<ExampleInstance>(anotherIndex, exampleInstance.id) {
100+
save(exampleInstance.id, exampleInstance, anotherIndex)
101+
shouldGet<ExampleInstance>(exampleInstance.id, anotherIndex) {
102102
it.description shouldBe exampleInstance.description
103103
}
104104
}

0 commit comments

Comments
 (0)