Skip to content

Commit

Permalink
improve mongo
Browse files Browse the repository at this point in the history
  • Loading branch information
osoykan committed Nov 28, 2024
1 parent eec0c92 commit 61d578a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 22 deletions.
3 changes: 0 additions & 3 deletions lib/stove-testing-e2e-mongodb/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
dependencies {
api(projects.lib.stoveTestingE2e)
api(libs.testcontainers.mongodb)
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import com.trendyol.stove.testing.e2e.system.abstractions.*
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.*
Expand Down Expand Up @@ -53,7 +51,7 @@ class MongodbSystem internal constructor(
): MongodbSystem = mongoClient
.getDatabase(context.options.databaseOptions.default.name)
.getCollection<Document>(collection)
.withDocumentClass(T::class.java)
.withDocumentClass<T>()
.find(BsonDocument.parse(query))
.toList()
.also(assertion)
Expand Down Expand Up @@ -81,16 +79,15 @@ class MongodbSystem internal constructor(
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<Document>(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)
context.options.serde.serialize(instance)
.let { BsonDocument.parse(it) }
.let { doc -> Document(doc) }
.append(RESERVED_ID, ObjectId(objectId))
.let { coll.insertOne(it) }
}
.let { this }

Expand Down Expand Up @@ -152,7 +149,9 @@ class MongodbSystem internal constructor(
.retryWrites(true)
.readConcern(ReadConcern.MAJORITY)
.writeConcern(WriteConcern.MAJORITY)
.build().let { MongoClient.create(it) }
.apply(context.options.configureClient)
.build()
.let { MongoClient.create(it) }

companion object {
const val RESERVED_ID = "_id"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
package com.trendyol.stove.testing.e2e.mongodb

import com.mongodb.MongoClientSettings
import com.trendyol.stove.testing.e2e.serialization.StoveSerde
import com.trendyol.stove.testing.e2e.system.abstractions.*
import com.trendyol.stove.testing.e2e.system.annotations.StoveDsl
import org.bson.UuidRepresentation
import org.bson.codecs.configuration.CodecRegistry
import org.bson.codecs.pojo.PojoCodecProvider
import org.bson.internal.ProvidersCodecRegistry
import org.mongojack.JacksonCodecRegistry

@StoveDsl
data class MongodbSystemOptions(
val databaseOptions: DatabaseOptions = DatabaseOptions(),
val container: MongoContainerOptions = MongoContainerOptions(),
val codecRegistry: CodecRegistry = JacksonCodecRegistry(
StoveSerde.jackson.byConfiguring { addModule(ObjectIdModule()) },
ProvidersCodecRegistry(listOf(PojoCodecProvider.builder().automatic(true).build())),
UuidRepresentation.STANDARD
val configureClient: (MongoClientSettings.Builder) -> Unit = { },
val serde: StoveSerde<Any, String> = StoveSerde.jackson.anyJsonStringSerde(
StoveSerde.jackson.byConfiguring { addModule(ObjectIdModule()) }
),
override val configureExposedConfiguration: (MongodbExposedConfiguration) -> List<String>
) : SystemOptions, ConfiguresExposedConfiguration<MongodbExposedConfiguration>

0 comments on commit 61d578a

Please sign in to comment.