From 4b0d1fc494fca87e4f9b0a333333a7c42c830c2f Mon Sep 17 00:00:00 2001 From: wally5077 Date: Sun, 22 Oct 2023 23:47:59 +0800 Subject: [PATCH] :bug: Fix Mongo test kit connection failure --- .../main/kotlin/tw/waterballsa/utopia/Main.kt | 12 +++- .../gatweay/config/MongoDBConfiguration.kt | 61 ++++++++++--------- .../configs/MongoDbTestContainerConfig.kt | 9 +-- 3 files changed, 43 insertions(+), 39 deletions(-) diff --git a/main/src/main/kotlin/tw/waterballsa/utopia/Main.kt b/main/src/main/kotlin/tw/waterballsa/utopia/Main.kt index 11ba0dcc..5ce51ca5 100644 --- a/main/src/main/kotlin/tw/waterballsa/utopia/Main.kt +++ b/main/src/main/kotlin/tw/waterballsa/utopia/Main.kt @@ -1,8 +1,11 @@ package tw.waterballsa.utopia import ch.qos.logback.core.util.OptionHelper -import com.fasterxml.jackson.databind.DeserializationFeature +import com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule +import com.fasterxml.jackson.module.kotlin.registerKotlinModule import net.dv8tion.jda.api.JDA import net.dv8tion.jda.api.entities.Guild import org.springframework.beans.factory.config.ConfigurableBeanFactory.* @@ -31,8 +34,11 @@ open class MyDependencyInjectionConfig { CommonAnnotationBeanPostProcessor() @Bean - open fun objectMapper(): ObjectMapper = - ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) + open fun objectMapper(): ObjectMapper = ObjectMapper() + .registerKotlinModule() + .registerModule(JavaTimeModule()) + .disable(WRITE_DATES_AS_TIMESTAMPS) + .configure(FAIL_ON_UNKNOWN_PROPERTIES, false) @Bean open fun wsaProperties(): WsaDiscordProperties { diff --git a/mongo-gateway-impl/src/main/kotlin/tw/waterballsa/utopia/mongo/gatweay/config/MongoDBConfiguration.kt b/mongo-gateway-impl/src/main/kotlin/tw/waterballsa/utopia/mongo/gatweay/config/MongoDBConfiguration.kt index 0f1da358..3e4bd787 100644 --- a/mongo-gateway-impl/src/main/kotlin/tw/waterballsa/utopia/mongo/gatweay/config/MongoDBConfiguration.kt +++ b/mongo-gateway-impl/src/main/kotlin/tw/waterballsa/utopia/mongo/gatweay/config/MongoDBConfiguration.kt @@ -1,14 +1,17 @@ package tw.waterballsa.utopia.mongo.gatweay.config -import ch.qos.logback.core.util.OptionHelper -import com.fasterxml.jackson.databind.DeserializationFeature +import ch.qos.logback.core.util.OptionHelper.getEnv +import ch.qos.logback.core.util.OptionHelper.getSystemProperty +import com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES import com.fasterxml.jackson.databind.ObjectMapper -import com.fasterxml.jackson.databind.SerializationFeature +import com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule import com.fasterxml.jackson.module.kotlin.registerKotlinModule import com.mongodb.ConnectionString import com.mongodb.MongoClientSettings import com.mongodb.client.MongoClients +import org.reflections.Reflections +import org.springframework.beans.factory.annotation.Autowired import org.springframework.beans.factory.config.BeanFactoryPostProcessor import org.springframework.beans.factory.config.ConfigurableListableBeanFactory import org.springframework.beans.factory.support.AbstractBeanDefinition @@ -36,28 +39,29 @@ import kotlin.reflect.full.findAnnotation @Configuration open class MongoDBConfiguration { - companion object { + private const val MONGO_CONNECTION_URI_KEY = "MONGO_CONNECTION_URI" + internal val MAPPER = ObjectMapper() - .registerKotlinModule() - .registerModule(JavaTimeModule()) - .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) - .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) + .registerKotlinModule() + .registerModule(JavaTimeModule()) + .disable(WRITE_DATES_AS_TIMESTAMPS) + .configure(FAIL_ON_UNKNOWN_PROPERTIES, false) } @Bean open fun mongoTemplate(context: ApplicationContext): MongoTemplate { - val uri = OptionHelper.getEnv("MONGO_CONNECTION_URI")?.trim() - ?: "mongodb://localhost:28017" + val uri = getEnv(MONGO_CONNECTION_URI_KEY) + ?: getSystemProperty(MONGO_CONNECTION_URI_KEY, "mongodb://localhost:28017") val settings = MongoClientSettings.builder() - .applyConnectionString(ConnectionString(uri)) - .applyToConnectionPoolSettings { builder -> - builder.maxSize(10) - } - .build() + .applyConnectionString(ConnectionString(uri.trim())) + .applyToConnectionPoolSettings { builder -> + builder.maxSize(10) + } + .build() val wsaDiscordProperties = context.getBean(WsaDiscordProperties::class.java) val factory = - SimpleMongoClientDatabaseFactory(MongoClients.create(settings), wsaDiscordProperties.mongoDatabase) + SimpleMongoClientDatabaseFactory(MongoClients.create(settings), wsaDiscordProperties.mongoDatabase) // remove _class field val converter = MappingMongoConverter(DefaultDbRefResolver(factory), MongoMappingContext()) @@ -67,24 +71,25 @@ open class MongoDBConfiguration { } } - @Component open class MyBeanFactoryPostProcessor : BeanFactoryPostProcessor, ApplicationContextAware { private lateinit var applicationContext: ApplicationContext + override fun setApplicationContext(applicationContext: ApplicationContext) { + this.applicationContext = applicationContext + } + override fun postProcessBeanFactory(beanFactory: ConfigurableListableBeanFactory) { val packageName = "tw.waterballsa.utopia" - val reflections = org.reflections.Reflections(packageName) - val annotatedClasses = reflections.getTypesAnnotatedWith(Document::class.java) - val mongoTemplate = applicationContext.getBean(MongoTemplate::class.java) + val annotatedClasses = Reflections(packageName).getTypesAnnotatedWith(Document::class.java) for (annotatedClass in annotatedClasses) { val idField = annotatedClass.declaredFields - .first { it.isAnnotationPresent(Id::class.java) }!! + .first { it.isAnnotationPresent(Id::class.java) }!! val resolvableType: ResolvableType = - ResolvableType.forClassWithGenerics(MongoCollection::class.java, annotatedClass, idField.type) + ResolvableType.forClassWithGenerics(MongoCollection::class.java, annotatedClass, idField.type) val beanDefinition = RootBeanDefinition() beanDefinition.setTargetType(resolvableType) beanDefinition.autowireMode = AbstractBeanDefinition.AUTOWIRE_BY_TYPE @@ -92,12 +97,11 @@ open class MyBeanFactoryPostProcessor : BeanFactoryPostProcessor, ApplicationCon val bf: DefaultListableBeanFactory = beanFactory as DefaultListableBeanFactory val collectionName = annotatedClass.kotlin.findAnnotation()?.collection - ?.ifEmpty { annotatedClass.simpleName }!! + ?.ifEmpty { annotatedClass.simpleName }!! - val mongoCollection = MongoCollectionAdapter( - mongoTemplate, - MappingMongoDocumentInformation(collectionName, annotatedClass, idField.type, idField.name) - ) + val mongoTemplate = applicationContext.getBean(MongoTemplate::class.java) + val documentInformation = MappingMongoDocumentInformation(collectionName, annotatedClass, idField.type, idField.name) + val mongoCollection = MongoCollectionAdapter(mongoTemplate, documentInformation) val beanName = "${annotatedClass.simpleName}MongoCollection" bf.registerBeanDefinition(beanName, beanDefinition) @@ -105,7 +109,4 @@ open class MyBeanFactoryPostProcessor : BeanFactoryPostProcessor, ApplicationCon } } - override fun setApplicationContext(applicationContext: ApplicationContext) { - this.applicationContext = applicationContext - } } diff --git a/utopia-test-kit/src/main/koltin/tw/waterballsa/utopia/utopiatestkit/configs/MongoDbTestContainerConfig.kt b/utopia-test-kit/src/main/koltin/tw/waterballsa/utopia/utopiatestkit/configs/MongoDbTestContainerConfig.kt index 93bf9027..45084b09 100644 --- a/utopia-test-kit/src/main/koltin/tw/waterballsa/utopia/utopiatestkit/configs/MongoDbTestContainerConfig.kt +++ b/utopia-test-kit/src/main/koltin/tw/waterballsa/utopia/utopiatestkit/configs/MongoDbTestContainerConfig.kt @@ -5,16 +5,14 @@ import org.junit.jupiter.api.extension.BeforeEachCallback import org.junit.jupiter.api.extension.ExtensionContext import org.springframework.beans.factory.annotation.Autowired import org.springframework.context.annotation.Configuration -import org.springframework.data.mongodb.config.AbstractMongoClientConfiguration import org.springframework.data.mongodb.core.MongoTemplate import org.testcontainers.containers.MongoDBContainer import java.lang.System.setProperty @Configuration -open class MongoDbTestContainerConfig : AbstractMongoClientConfiguration(), BeforeEachCallback, AfterAllCallback { +open class MongoDbTestContainerConfig : BeforeEachCallback, AfterAllCallback { companion object { - private const val TEST_DATABASE = "TEST_DATABASE" private lateinit var MONGO_DB_CONTAINER: MongoDBContainer init { @@ -34,10 +32,9 @@ open class MongoDbTestContainerConfig : AbstractMongoClientConfiguration(), Befo @Autowired private lateinit var mongo: MongoTemplate - override fun getDatabaseName(): String = TEST_DATABASE - override fun beforeEach(context: ExtensionContext?) { - mongo.db.drop() + mongo.db.listCollectionNames() + .forEach(mongo::dropCollection) } override fun afterAll(context: ExtensionContext?) {