Skip to content

Commit

Permalink
🐛 Fix Mongo test kit connection failure
Browse files Browse the repository at this point in the history
  • Loading branch information
Wally5077 committed Oct 22, 2023
1 parent 2e18dc1 commit 4b0d1fc
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 39 deletions.
12 changes: 9 additions & 3 deletions main/src/main/kotlin/tw/waterballsa/utopia/Main.kt
Original file line number Diff line number Diff line change
@@ -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.*
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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())
Expand All @@ -67,45 +71,42 @@ 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
beanDefinition.isAutowireCandidate = true

val bf: DefaultListableBeanFactory = beanFactory as DefaultListableBeanFactory
val collectionName = annotatedClass.kotlin.findAnnotation<Document>()?.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)
bf.registerSingleton(beanName, mongoCollection)
}
}

override fun setApplicationContext(applicationContext: ApplicationContext) {
this.applicationContext = applicationContext
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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?) {
Expand Down

0 comments on commit 4b0d1fc

Please sign in to comment.