Skip to content

Commit

Permalink
refactor(mongo): read operations by ser/de
Browse files Browse the repository at this point in the history
  • Loading branch information
osoykan committed Dec 3, 2024
1 parent f5deaca commit 065ef5a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ class MongodbSystem internal constructor(
): MongodbSystem = mongoClient
.getDatabase(context.options.databaseOptions.default.name)
.getCollection<Document>(collection)
.withDocumentClass<T>()
.find(BsonDocument.parse(query))
.map { context.options.serde.deserialize(it.toJson(context.options.jsonWriterSettings), T::class.java) }
.toList()
.also(assertion)
.let { this }
Expand All @@ -65,8 +65,8 @@ class MongodbSystem internal constructor(
): MongodbSystem = mongoClient
.getDatabase(context.options.databaseOptions.default.name)
.getCollection<Document>(collection)
.withDocumentClass<T>()
.find(filterById(objectId))
.map { context.options.serde.deserialize(it.toJson(context.options.jsonWriterSettings), T::class.java) }
.first()
.also(assertion)
.let { this }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
package com.trendyol.stove.testing.e2e.mongodb

import com.fasterxml.jackson.databind.*
import com.fasterxml.jackson.module.kotlin.KotlinModule
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.json.JsonWriterSettings

@StoveDsl
data class MongodbSystemOptions(
val databaseOptions: DatabaseOptions = DatabaseOptions(),
val container: MongoContainerOptions = MongoContainerOptions(),
val configureClient: (MongoClientSettings.Builder) -> Unit = { },
val serde: StoveSerde<Any, String> = StoveSerde.jackson.anyJsonStringSerde(
StoveSerde.jackson.byConfiguring { addModule(ObjectIdModule()) }
StoveSerde.jackson.byConfiguring {
disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
enable(MapperFeature.DEFAULT_VIEW_INCLUSION)
addModule(ObjectIdModule())
addModule(KotlinModule.Builder().build())
}
),
val jsonWriterSettings: JsonWriterSettings = StoveMongoJsonWriterSettings.objectIdAsString,
override val configureExposedConfiguration: (MongodbExposedConfiguration) -> List<String>
) : SystemOptions, ConfiguresExposedConfiguration<MongodbExposedConfiguration>

object StoveMongoJsonWriterSettings {
val objectIdAsString: JsonWriterSettings = JsonWriterSettings.builder()
.objectIdConverter { value, writer -> writer.writeString(value.toHexString()) }
.build()
}

0 comments on commit 065ef5a

Please sign in to comment.