diff --git a/lib/stove-testing-e2e-mongodb/build.gradle.kts b/lib/stove-testing-e2e-mongodb/build.gradle.kts index 07832f61..7010b00a 100644 --- a/lib/stove-testing-e2e-mongodb/build.gradle.kts +++ b/lib/stove-testing-e2e-mongodb/build.gradle.kts @@ -1,7 +1,9 @@ dependencies { api(projects.lib.stoveTestingE2e) api(libs.testcontainers.mongodb) - api(libs.mongojack) + api(libs.mongojack) { + exclude(group = "org.mongodb", module = "mongodb-driver-sync") + } implementation(libs.mongodb.kotlin.coroutine) implementation(libs.kotlinx.io.reactor.extensions) implementation(libs.kotlinx.reactive) diff --git a/lib/stove-testing-e2e-mongodb/src/main/kotlin/com/trendyol/stove/testing/e2e/mongodb/MongodbSystem.kt b/lib/stove-testing-e2e-mongodb/src/main/kotlin/com/trendyol/stove/testing/e2e/mongodb/MongodbSystem.kt index c27eac13..eebaa8a2 100644 --- a/lib/stove-testing-e2e-mongodb/src/main/kotlin/com/trendyol/stove/testing/e2e/mongodb/MongodbSystem.kt +++ b/lib/stove-testing-e2e-mongodb/src/main/kotlin/com/trendyol/stove/testing/e2e/mongodb/MongodbSystem.kt @@ -11,6 +11,7 @@ import kotlinx.coroutines.flow.* import kotlinx.coroutines.runBlocking import org.bson.* import org.bson.codecs.EncoderContext +import org.bson.codecs.configuration.CodecRegistry import org.bson.conversions.Bson import org.bson.types.ObjectId import org.slf4j.* @@ -49,8 +50,8 @@ class MongodbSystem internal constructor( query: String, collection: String = context.options.databaseOptions.default.collection, assertion: (List) -> Unit - ): MongodbSystem = mongoClient.getDatabase(context.options.databaseOptions.default.name) - .let { it.withCodecRegistry(PojoRegistry(it.codecRegistry).register(T::class).build()) } + ): MongodbSystem = mongoClient + .getDatabase(context.options.databaseOptions.default.name) .getCollection(collection) .withDocumentClass(T::class.java) .find(BsonDocument.parse(query)) @@ -63,21 +64,43 @@ class MongodbSystem internal constructor( objectId: String, collection: String = context.options.databaseOptions.default.collection, assertion: (T) -> Unit - ): MongodbSystem = mongoClient.getDatabase(context.options.databaseOptions.default.name) + ): MongodbSystem = mongoClient + .getDatabase(context.options.databaseOptions.default.name) .getCollection(collection) .withDocumentClass() - .let { it.withCodecRegistry(PojoRegistry(it.codecRegistry).register(T::class).build()) } .find(filterById(objectId)) .first() .also(assertion) .let { this } + /** + * Saves the [instance] with given [objectId] to the [collection] + */ + @MongoDsl + suspend inline fun save( + instance: T, + objectId: String = ObjectId().toHexString(), + collection: String = context.options.databaseOptions.default.collection, + codecRegistry: CodecRegistry = context.options.codecRegistry + ): MongodbSystem = mongoClient + .getDatabase(context.options.databaseOptions.default.name) + .getCollection(collection) + .also { coll -> + val bson = BsonDocument() + codecRegistry.get(T::class.java) + .encode(BsonDocumentWriter(bson), instance, EncoderContext.builder().build()) + val doc = Document(bson).append(RESERVED_ID, ObjectId(objectId)) + coll.insertOne(doc) + } + .let { this } + @MongoDsl suspend fun shouldNotExist( objectId: String, collection: String = context.options.databaseOptions.default.collection ): MongodbSystem { - val exists = mongoClient.getDatabase(context.options.databaseOptions.default.name) + val exists = mongoClient + .getDatabase(context.options.databaseOptions.default.name) .getCollection(collection) .find(filterById(objectId)) .firstOrNull() != null @@ -91,30 +114,11 @@ class MongodbSystem internal constructor( suspend fun shouldDelete( objectId: String, collection: String = context.options.databaseOptions.default.collection - ): MongodbSystem = mongoClient.getDatabase(context.options.databaseOptions.default.name) + ): MongodbSystem = mongoClient + .getDatabase(context.options.databaseOptions.default.name) .getCollection(collection) .deleteOne(filterById(objectId)).let { this } - /** - * Saves the [instance] with given [objectId] to the [collection] - */ - @MongoDsl - suspend inline fun save( - instance: T, - objectId: String = ObjectId().toHexString(), - collection: String = context.options.databaseOptions.default.collection - ): MongodbSystem = mongoClient.getDatabase(context.options.databaseOptions.default.name) - .let { it.withCodecRegistry(PojoRegistry(it.codecRegistry).register(T::class).build()) } - .getCollection(collection) - .also { coll -> - val bson = BsonDocument() - context.options.codecRegistry.get(T::class.java) - .encode(BsonDocumentWriter(bson), instance, EncoderContext.builder().build()) - val doc = Document(bson).append(RESERVED_ID, ObjectId(objectId)) - coll.insertOne(doc) - } - .let { this } - /** * Pauses the container. Use with care, as it will pause the container which might affect other tests. * @return KafkaSystem diff --git a/lib/stove-testing-e2e-mongodb/src/main/kotlin/com/trendyol/stove/testing/e2e/mongodb/PojoRegistry.kt b/lib/stove-testing-e2e-mongodb/src/main/kotlin/com/trendyol/stove/testing/e2e/mongodb/PojoRegistry.kt deleted file mode 100644 index 97e36a73..00000000 --- a/lib/stove-testing-e2e-mongodb/src/main/kotlin/com/trendyol/stove/testing/e2e/mongodb/PojoRegistry.kt +++ /dev/null @@ -1,19 +0,0 @@ -package com.trendyol.stove.testing.e2e.mongodb - -import com.mongodb.reactivestreams.client.MongoClients.getDefaultCodecRegistry -import com.trendyol.stove.testing.e2e.system.annotations.StoveDsl -import org.bson.codecs.configuration.CodecRegistries.* -import org.bson.codecs.configuration.CodecRegistry -import org.bson.codecs.pojo.* -import kotlin.reflect.KClass - -@StoveDsl -data class PojoRegistry( - val registry: CodecRegistry = fromRegistries() -) { - private var builder: PojoCodecProvider.Builder = PojoCodecProvider.builder().conventions(Conventions.DEFAULT_CONVENTIONS) - - fun register(clazz: KClass): PojoRegistry = builder.register(clazz.java).let { this } - - fun build(): CodecRegistry = fromRegistries(registry, getDefaultCodecRegistry(), fromProviders(builder.build())) -}