From f805d9c591badb3021186e7f8f3281fb24eda293 Mon Sep 17 00:00:00 2001 From: nk2 Date: Mon, 21 Aug 2023 19:24:47 +0200 Subject: [PATCH] Fix Mongo --- .../autoconfigure/mongo/MongoInitializer.java | 6 +--- .../fu/kofu/mongo/AbstractMongoDsl.kt | 2 -- .../springframework/fu/kofu/mongo/MongoDsl.kt | 2 +- ...eddedMongoDslTests.kt => MongoDslTests.kt} | 33 +++++++++++++------ 4 files changed, 25 insertions(+), 18 deletions(-) rename kofu-mongo/src/test/kotlin/org/springframework/fu/kofu/mongo/{EmbeddedMongoDslTests.kt => MongoDslTests.kt} (80%) diff --git a/autoconfigure-adapter-mongo/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoInitializer.java b/autoconfigure-adapter-mongo/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoInitializer.java index 3ae0cb503..89a233763 100644 --- a/autoconfigure-adapter-mongo/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoInitializer.java +++ b/autoconfigure-adapter-mongo/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoInitializer.java @@ -31,14 +31,10 @@ public class MongoInitializer implements ApplicationContextInitializer Unit) : Abstrac protected val properties = MongoProperties() - internal var embedded = false - /** * Configure the database uri. By default set to `mongodb://localhost/test`. */ diff --git a/kofu-mongo/src/main/kotlin/org/springframework/fu/kofu/mongo/MongoDsl.kt b/kofu-mongo/src/main/kotlin/org/springframework/fu/kofu/mongo/MongoDsl.kt index cddacf2b4..ee01bc7ce 100644 --- a/kofu-mongo/src/main/kotlin/org/springframework/fu/kofu/mongo/MongoDsl.kt +++ b/kofu-mongo/src/main/kotlin/org/springframework/fu/kofu/mongo/MongoDsl.kt @@ -33,7 +33,7 @@ open class MongoDsl(private val init: MongoDsl.() -> Unit) : AbstractMongoDsl({} override fun initialize(context: GenericApplicationContext) { super.initialize(context) init() - MongoInitializer(properties, embedded).initialize(context) + MongoInitializer(properties).initialize(context) MongoDataInitializer(properties).initialize(context) } diff --git a/kofu-mongo/src/test/kotlin/org/springframework/fu/kofu/mongo/EmbeddedMongoDslTests.kt b/kofu-mongo/src/test/kotlin/org/springframework/fu/kofu/mongo/MongoDslTests.kt similarity index 80% rename from kofu-mongo/src/test/kotlin/org/springframework/fu/kofu/mongo/EmbeddedMongoDslTests.kt rename to kofu-mongo/src/test/kotlin/org/springframework/fu/kofu/mongo/MongoDslTests.kt index 729f3930e..c253fa8af 100644 --- a/kofu-mongo/src/test/kotlin/org/springframework/fu/kofu/mongo/EmbeddedMongoDslTests.kt +++ b/kofu-mongo/src/test/kotlin/org/springframework/fu/kofu/mongo/MongoDslTests.kt @@ -16,7 +16,6 @@ package org.springframework.fu.kofu.mongo -import de.flapdoodle.embed.mongo.distribution.Version import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Test import org.springframework.beans.factory.getBean @@ -25,21 +24,26 @@ import org.springframework.data.mongodb.core.MongoTemplate import org.springframework.data.mongodb.core.ReactiveMongoTemplate import org.springframework.data.mongodb.core.findById import org.springframework.fu.kofu.application -import org.springframework.util.SocketUtils +import org.testcontainers.containers.GenericContainer import java.time.Duration -class EmbeddedMongoModuleTests { +class MongoDslTests { @Test fun `enable mongodb embedded module with reactive support`() { - val port = SocketUtils.findAvailableTcpPort() + val mongo = object : GenericContainer("mongo:latest") { + init { + withExposedPorts(27017) + } + } + mongo.start() + val app = application { beans { bean() } reactiveMongodb { - uri = "mongodb://localhost:$port/test" - embedded(Version.Main.PRODUCTION) + uri = "mongodb://${mongo.containerIpAddress}:${mongo.firstMappedPort}/test" } } with(app.run()){ @@ -48,18 +52,25 @@ class EmbeddedMongoModuleTests { assertEquals("foo", repository.findById("1").block(Duration.ofSeconds(3))?.name) close() } + + mongo.stop() } @Test - fun `enable mongodb embedded module`() { - val port = SocketUtils.findAvailableTcpPort() + fun `enable mongodb`() { + val mongo = object : GenericContainer("mongo:latest") { + init { + withExposedPorts(27017) + } + } + mongo.start() + val app = application { beans { bean() } mongodb() { - uri = "mongodb://localhost:$port/test" - embedded(Version.Main.PRODUCTION) + uri = "mongodb://${mongo.containerIpAddress}:${mongo.firstMappedPort}/test" } } with(app.run()){ @@ -68,6 +79,8 @@ class EmbeddedMongoModuleTests { assertEquals("foo", repository.findById("1")?.name) close() } + + mongo.stop() } }