Switch from koin to micronaut-inject
This commit is contained in:
@@ -10,8 +10,6 @@ dependencies {
|
||||
implementation(project(":simplenotes-types"))
|
||||
implementation(project(":simplenotes-config"))
|
||||
|
||||
implementation(Libs.mapstruct)
|
||||
implementation(Libs.koinCore)
|
||||
implementation(Libs.mariadbClient)
|
||||
implementation(Libs.h2)
|
||||
implementation(Libs.flywayCore)
|
||||
@@ -19,19 +17,36 @@ dependencies {
|
||||
implementation(Libs.ktormCore)
|
||||
implementation(Libs.ktormMysql)
|
||||
|
||||
implementation(Libs.mapstruct)
|
||||
kapt(Libs.mapstructProcessor)
|
||||
|
||||
implementation(Libs.micronaut)
|
||||
kapt(Libs.micronautProcessor)
|
||||
|
||||
testImplementation(Libs.micronaut)
|
||||
kaptTest(Libs.micronautProcessor)
|
||||
|
||||
testImplementation(Libs.junit)
|
||||
testImplementation(Libs.assertJ)
|
||||
testImplementation(Libs.logbackClassic)
|
||||
testImplementation("org.testcontainers:mariadb:1.15.0-rc2")
|
||||
testImplementation(Libs.mariaTestContainer)
|
||||
|
||||
testFixturesImplementation(project(":simplenotes-types"))
|
||||
testFixturesImplementation(project(":simplenotes-config"))
|
||||
testFixturesImplementation("com.github.javafaker:javafaker:1.0.2")
|
||||
testFixturesImplementation("org.testcontainers:mariadb:1.15.0-rc2")
|
||||
testFixturesImplementation(Libs.koinCore)
|
||||
testFixturesImplementation(project(":simplenotes-persistance"))
|
||||
|
||||
testFixturesImplementation(Libs.micronaut)
|
||||
kaptTestFixtures(Libs.micronautProcessor)
|
||||
|
||||
testFixturesImplementation(Libs.faker) {
|
||||
exclude(group = "org.yaml")
|
||||
}
|
||||
|
||||
testFixturesImplementation(Libs.snakeyaml)
|
||||
|
||||
testFixturesImplementation(Libs.mariaTestContainer)
|
||||
testFixturesImplementation(Libs.flywayCore)
|
||||
testFixturesImplementation(Libs.junit)
|
||||
|
||||
testFixturesImplementation(Libs.ktormCore)
|
||||
testFixturesImplementation(Libs.hikariCP)
|
||||
}
|
||||
|
||||
@@ -7,11 +7,13 @@ import me.liuwj.ktorm.database.Database
|
||||
import me.liuwj.ktorm.database.asIterable
|
||||
import me.liuwj.ktorm.database.use
|
||||
import java.sql.SQLTransientException
|
||||
import javax.inject.Singleton
|
||||
|
||||
interface DbHealthCheck {
|
||||
fun isOk(): Boolean
|
||||
}
|
||||
|
||||
@Singleton
|
||||
internal class DbHealthCheckImpl(
|
||||
private val db: Database,
|
||||
private val dataSourceConfig: DataSourceConfig,
|
||||
|
||||
@@ -4,12 +4,14 @@ import be.simplenotes.config.DataSourceConfig
|
||||
import be.simplenotes.persistance.utils.DbType
|
||||
import be.simplenotes.persistance.utils.type
|
||||
import org.flywaydb.core.Flyway
|
||||
import javax.inject.Singleton
|
||||
import javax.sql.DataSource
|
||||
|
||||
interface DbMigrations {
|
||||
fun migrate()
|
||||
}
|
||||
|
||||
@Singleton
|
||||
internal class DbMigrationsImpl(
|
||||
private val dataSource: DataSource,
|
||||
private val dataSourceConfig: DataSourceConfig,
|
||||
|
||||
+29
-33
@@ -2,46 +2,42 @@ package be.simplenotes.persistance
|
||||
|
||||
import be.simplenotes.config.DataSourceConfig
|
||||
import be.simplenotes.persistance.converters.NoteConverter
|
||||
import be.simplenotes.persistance.converters.NoteConverterImpl
|
||||
import be.simplenotes.persistance.converters.UserConverter
|
||||
import be.simplenotes.persistance.converters.UserConverterImpl
|
||||
import be.simplenotes.persistance.notes.NoteRepositoryImpl
|
||||
import be.simplenotes.persistance.repositories.NoteRepository
|
||||
import be.simplenotes.persistance.repositories.UserRepository
|
||||
import be.simplenotes.persistance.users.UserRepositoryImpl
|
||||
import com.zaxxer.hikari.HikariConfig
|
||||
import com.zaxxer.hikari.HikariDataSource
|
||||
import io.micronaut.context.annotation.Bean
|
||||
import io.micronaut.context.annotation.Factory
|
||||
import me.liuwj.ktorm.database.Database
|
||||
import org.koin.dsl.bind
|
||||
import org.koin.dsl.module
|
||||
import org.koin.dsl.onClose
|
||||
import org.mapstruct.factory.Mappers
|
||||
import javax.inject.Singleton
|
||||
import javax.sql.DataSource
|
||||
|
||||
private fun hikariDataSource(conf: DataSourceConfig): HikariDataSource {
|
||||
val hikariConfig = HikariConfig().also {
|
||||
it.jdbcUrl = conf.jdbcUrl
|
||||
it.driverClassName = conf.driverClassName
|
||||
it.username = conf.username
|
||||
it.password = conf.password
|
||||
it.maximumPoolSize = conf.maximumPoolSize
|
||||
it.connectionTimeout = conf.connectionTimeout
|
||||
}
|
||||
return HikariDataSource(hikariConfig)
|
||||
}
|
||||
@Factory
|
||||
class PersistanceModule {
|
||||
|
||||
val migrationModule = module {
|
||||
single<DbMigrations> { DbMigrationsImpl(get(), get()) }
|
||||
}
|
||||
@Singleton
|
||||
internal fun noteConverter() = Mappers.getMapper(NoteConverter::class.java)
|
||||
|
||||
val persistanceModule = module {
|
||||
single<NoteConverter> { NoteConverterImpl() }
|
||||
single<UserConverter> { UserConverterImpl() }
|
||||
single<UserRepository> { UserRepositoryImpl(get(), get()) }
|
||||
single<NoteRepository> { NoteRepositoryImpl(get(), get()) }
|
||||
single { hikariDataSource(get()) } bind DataSource::class onClose { it?.close() }
|
||||
single {
|
||||
get<DbMigrations>().migrate()
|
||||
Database.connect(get<DataSource>())
|
||||
@Singleton
|
||||
internal fun userConverter() = Mappers.getMapper(UserConverter::class.java)
|
||||
|
||||
@Singleton
|
||||
internal fun database(migrations: DbMigrations, dataSource: DataSource): Database {
|
||||
migrations.migrate()
|
||||
return Database.connect(dataSource)
|
||||
}
|
||||
|
||||
@Singleton
|
||||
@Bean(preDestroy = "close")
|
||||
internal fun dataSource(conf: DataSourceConfig): HikariDataSource {
|
||||
val hikariConfig = HikariConfig().also {
|
||||
it.jdbcUrl = conf.jdbcUrl
|
||||
it.driverClassName = conf.driverClassName
|
||||
it.username = conf.username
|
||||
it.password = conf.password
|
||||
it.maximumPoolSize = conf.maximumPoolSize
|
||||
it.connectionTimeout = conf.connectionTimeout
|
||||
}
|
||||
return HikariDataSource(hikariConfig)
|
||||
}
|
||||
single<DbHealthCheck> { DbHealthCheckImpl(get(), get()) }
|
||||
}
|
||||
|
||||
+6
-1
@@ -11,9 +11,14 @@ import me.liuwj.ktorm.dsl.*
|
||||
import me.liuwj.ktorm.entity.*
|
||||
import java.time.LocalDateTime
|
||||
import java.util.*
|
||||
import javax.inject.Singleton
|
||||
import kotlin.collections.HashMap
|
||||
|
||||
internal class NoteRepositoryImpl(private val db: Database, private val converter: NoteConverter) : NoteRepository {
|
||||
@Singleton
|
||||
internal class NoteRepositoryImpl(
|
||||
private val db: Database,
|
||||
private val converter: NoteConverter,
|
||||
) : NoteRepository {
|
||||
|
||||
@Throws(IllegalArgumentException::class)
|
||||
override fun findAll(
|
||||
|
||||
+6
-1
@@ -9,8 +9,13 @@ import me.liuwj.ktorm.dsl.*
|
||||
import me.liuwj.ktorm.entity.any
|
||||
import me.liuwj.ktorm.entity.find
|
||||
import java.sql.SQLIntegrityConstraintViolationException
|
||||
import javax.inject.Singleton
|
||||
|
||||
internal class UserRepositoryImpl(private val db: Database, private val converter: UserConverter) : UserRepository {
|
||||
@Singleton
|
||||
internal class UserRepositoryImpl(
|
||||
private val db: Database,
|
||||
private val converter: UserConverter,
|
||||
) : UserRepository {
|
||||
override fun create(user: User): PersistedUser? {
|
||||
return try {
|
||||
val id = db.insertAndGenerateKey(Users) {
|
||||
|
||||
+2
-5
@@ -7,19 +7,16 @@ import org.junit.jupiter.api.parallel.ResourceLock
|
||||
|
||||
@ResourceLock("h2")
|
||||
class H2DbHealthCheckImplTest : DbTest() {
|
||||
private val healthCheck = koin.get<DbHealthCheck>()
|
||||
override fun dataSourceConfig() = h2dataSourceConfig()
|
||||
|
||||
@Test
|
||||
fun healthCheck() {
|
||||
assertThat(healthCheck.isOk()).isTrue
|
||||
assertThat(beanContext.getBean<DbHealthCheck>().isOk()).isTrue
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ResourceLock("mariadb")
|
||||
class MariaDbHealthCheckImplTest : DbTest() {
|
||||
private val healthCheck = koin.get<DbHealthCheck>()
|
||||
lateinit var mariaDB: KMariadbContainer
|
||||
|
||||
override fun dataSourceConfig(): DataSourceConfig {
|
||||
@@ -30,9 +27,9 @@ class MariaDbHealthCheckImplTest : DbTest() {
|
||||
|
||||
@Test
|
||||
fun healthCheck() {
|
||||
val healthCheck = beanContext.getBean<DbHealthCheck>()
|
||||
assertThat(healthCheck.isOk()).isTrue
|
||||
mariaDB.stop()
|
||||
assertThat(healthCheck.isOk()).isFalse
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package be.simplenotes.persistance
|
||||
|
||||
import be.simplenotes.config.DataSourceConfig
|
||||
import io.micronaut.context.BeanContext
|
||||
import org.flywaydb.core.Flyway
|
||||
import org.junit.jupiter.api.AfterAll
|
||||
import org.junit.jupiter.api.BeforeAll
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import javax.sql.DataSource
|
||||
|
||||
abstract class DbTest {
|
||||
|
||||
abstract fun dataSourceConfig(): DataSourceConfig
|
||||
|
||||
val beanContext = BeanContext
|
||||
.build()
|
||||
|
||||
inline fun <reified T> BeanContext.getBean(): T = getBean(T::class.java)
|
||||
|
||||
@BeforeAll
|
||||
fun setComponent() {
|
||||
beanContext
|
||||
.registerSingleton(dataSourceConfig())
|
||||
.start()
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
fun beforeEach() {
|
||||
val migration = beanContext.getBean<DbMigrations>()
|
||||
val dataSource = beanContext.getBean<DataSource>()
|
||||
|
||||
Flyway.configure()
|
||||
.dataSource(dataSource)
|
||||
.load()
|
||||
.clean()
|
||||
|
||||
migration.migrate()
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
fun closeCtx() {
|
||||
beanContext.close()
|
||||
}
|
||||
}
|
||||
+6
-3
@@ -21,15 +21,18 @@ import java.sql.SQLIntegrityConstraintViolationException
|
||||
|
||||
internal abstract class BaseNoteRepositoryImplTest : DbTest() {
|
||||
|
||||
private val noteRepo = koin.get<NoteRepository>()
|
||||
private val userRepo = koin.get<UserRepository>()
|
||||
private val db = koin.get<Database>()
|
||||
private lateinit var noteRepo: NoteRepository
|
||||
private lateinit var userRepo: UserRepository
|
||||
private lateinit var db: Database
|
||||
|
||||
private lateinit var user1: PersistedUser
|
||||
private lateinit var user2: PersistedUser
|
||||
|
||||
@BeforeEach
|
||||
fun insertUsers() {
|
||||
noteRepo = beanContext.getBean()
|
||||
userRepo = beanContext.getBean()
|
||||
db = beanContext.getBean()
|
||||
user1 = userRepo.createFakeUser()!!
|
||||
user2 = userRepo.createFakeUser()!!
|
||||
}
|
||||
|
||||
+9
-2
@@ -7,13 +7,20 @@ import me.liuwj.ktorm.dsl.eq
|
||||
import me.liuwj.ktorm.entity.find
|
||||
import me.liuwj.ktorm.entity.toList
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.Nested
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
internal abstract class BaseUserRepositoryImplTest : DbTest() {
|
||||
|
||||
private val userRepo = koin.get<UserRepository>()
|
||||
private val db = koin.get<Database>()
|
||||
private lateinit var userRepo: UserRepository
|
||||
private lateinit var db: Database
|
||||
|
||||
@BeforeEach
|
||||
fun setup() {
|
||||
userRepo = beanContext.getBean()
|
||||
db = beanContext.getBean()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `insert user`() {
|
||||
|
||||
+14
-19
@@ -1,40 +1,35 @@
|
||||
package be.simplenotes.persistance
|
||||
|
||||
import be.simplenotes.config.DataSourceConfig
|
||||
import org.flywaydb.core.Flyway.*
|
||||
import org.junit.jupiter.api.AfterAll
|
||||
import io.micronaut.context.BeanContext
|
||||
import org.flywaydb.core.Flyway
|
||||
import org.junit.jupiter.api.BeforeAll
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.koin.dsl.koinApplication
|
||||
import org.koin.dsl.module
|
||||
import javax.sql.DataSource
|
||||
|
||||
abstract class DbTest {
|
||||
|
||||
abstract fun dataSourceConfig(): DataSourceConfig
|
||||
|
||||
private val testModule = module {
|
||||
single { dataSourceConfig() }
|
||||
val beanContext = BeanContext.build()
|
||||
|
||||
inline fun <reified T> BeanContext.getBean(): T = getBean(T::class.java)
|
||||
|
||||
@BeforeAll
|
||||
fun setComponent() {
|
||||
beanContext.registerSingleton(dataSourceConfig())
|
||||
}
|
||||
|
||||
private val koinApp = koinApplication {
|
||||
modules(persistanceModule, migrationModule, testModule)
|
||||
}
|
||||
|
||||
val koin = koinApp.koin
|
||||
|
||||
@AfterAll
|
||||
fun afterAll() = koinApp.close()
|
||||
|
||||
@BeforeEach
|
||||
fun beforeEach() {
|
||||
val migration = koin.get<DbMigrations>()
|
||||
val dataSource = koin.get<DataSource>()
|
||||
configure()
|
||||
val migration = beanContext.getBean<DbMigrations>()
|
||||
val dataSource = beanContext.getBean<DataSource>()
|
||||
|
||||
Flyway.configure()
|
||||
.dataSource(dataSource)
|
||||
.load()
|
||||
.clean()
|
||||
|
||||
migration.migrate()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user