1
0

Compare commits

..

2 Commits

Author SHA1 Message Date
d4635f82ae Add config loading tests 2020-10-06 23:35:59 +02:00
009ddd3d16 Use name as default artifactId 2020-10-06 22:56:37 +02:00
7 changed files with 139 additions and 28 deletions

View File

@ -27,7 +27,7 @@ flyway = "7.0.0"
hikaricp = "3.4.5"
ktorm = "3.0.0"
junit = "5.7.0"
mokk = "1.10.0"
mockk = "1.10.0"
hamkrest = "1.7.0.3"
assertj = "3.17.2"
kodein-di = "7.1.0"
@ -54,7 +54,6 @@ default = true
[dependencies.http4k-server-jetty]
groupId = "org.http4k"
artifactId = "http4k-server-jetty"
version = "http4k"
category = "http4k"
default = true
@ -62,43 +61,36 @@ logger = "org.eclipse.jetty"
[dependencies.http4k-server-apache]
groupId = "org.http4k"
artifactId = "http4k-server-apache"
version = "http4k"
category = "http4k"
[dependencies.http4k-client-apache]
groupId = "org.http4k"
artifactId = "http4k-client-apache"
version = "http4k"
category = "http4k"
[dependencies.http4k-format-jackson]
groupId = "org.http4k"
artifactId = "http4k-format-jackson"
version = "http4k"
category = "http4k"
[dependencies.http4k-format-kotlinx-serialization]
groupId = "org.http4k"
artifactId = "http4k-format-kotlinx-serialization"
version = "http4k"
category = "http4k"
[dependencies.http4k-contract]
groupId = "org.http4k"
artifactId = "http4k-contract"
version = "http4k"
category = "http4k"
[dependencies.pebble]
groupId = "io.pebbletemplates"
artifactId = "pebble"
default = true
logger = "com.mitchellbosecke.pebble"
[dependencies.caffeine]
groupId = "com.github.ben-manes.caffeine"
artifactId = "caffeine"
[dependencies.logback]
groupId = "ch.qos.logback"
@ -112,7 +104,6 @@ category = "database"
[dependencies.h2]
groupId = "com.h2database"
artifactId = "h2"
category = "database"
[dependencies.flyway]
@ -154,7 +145,7 @@ scope = "test"
category = "test"
default = true
[dependencies.mokk]
[dependencies.mockk]
groupId = "io.mockk"
artifactId = "mockk"
scope = "test"
@ -162,7 +153,6 @@ category = "test"
[dependencies.hamkrest]
groupId = "com.natpryce"
artifactId = "hamkrest"
scope = "test"
category = "test"
@ -198,5 +188,4 @@ category = "serialization"
[dependencies.arrow-core]
groupId = "io.arrow-kt"
artifactId = "arrow-core"
repository = "arrow"

View File

@ -1,13 +1,10 @@
package starter
import org.koin.core.context.startKoin
import starter.modules.mainModule
import starter.modules.pebbleModule
import starter.modules.routesModule
import starter.modules.templateModule
import starter.modules.*
fun main() {
startKoin {
modules(mainModule, pebbleModule, templateModule, routesModule)
modules(mainModule, pebbleModule, templateModule, configModule, routesModule)
}
}

View File

@ -1,7 +1,6 @@
package starter.config
import com.electronwill.nightconfig.core.UnmodifiableConfig
import com.electronwill.nightconfig.core.file.FileConfig
import starter.*
import com.electronwill.nightconfig.core.Config as NightConfig
@ -10,17 +9,19 @@ data class StarterConfig(
val inputs: List<Input>,
)
class Config {
class Config(private val cfg: NightConfig) {
@Suppress("UNCHECKED_CAST")
private fun FileConfig.configMap(key: String) = this.get<NightConfig>(key).valueMap() as Map<String, NightConfig>
private fun NightConfig.configMap(key: String) = this.get<NightConfig>(key)
?.valueMap() as Map<String, NightConfig>?
?: emptyMap()
fun load(): StarterConfig {
val cfg = FileConfig.of("config.toml")
cfg.load()
@Suppress("UNCHECKED_CAST")
val versions = cfg.get<UnmodifiableConfig>("versions").valueMap() as Map<String, String>
val versions = cfg.get<UnmodifiableConfig>("versions")
?.valueMap() as Map<String, String>?
?: emptyMap()
val repositories = cfg.configMap("repositories")
.map { (name, values) ->
@ -38,7 +39,7 @@ class Config {
Dependency(
name = name,
groupId = values["groupId"],
artifactId = values["artifactId"],
artifactId = values["artifactId"] ?: name,
version = Version(versionKey, version),
default = values.getOrElse("default", false),
category = values.getEnumOrElse("category", Category.Other),

View File

@ -0,0 +1,11 @@
package starter.modules
import com.electronwill.nightconfig.core.file.FileConfig
import org.koin.dsl.module
import starter.config.Config
import com.electronwill.nightconfig.core.Config as NightConfig
val configModule = module {
single<NightConfig> { FileConfig.of("config.toml").apply { load() } }
single { Config(get()).load() }
}

View File

@ -17,7 +17,6 @@ val mainModule = module {
get<RoutingHttpHandler>().asServer(SunHttp(7000)).start()
} onClose { it?.stop() }
single { Config().load() }
single { LoggerFactory.getLogger("Starter") }
single { Views(get(), get()) }
single { ProjectZip(getAll()) }

View File

@ -0,0 +1,114 @@
package starter.config
import com.electronwill.nightconfig.toml.TomlParser
import org.assertj.core.api.Assertions.assertThat
import org.intellij.lang.annotations.Language
import org.junit.jupiter.api.Test
import starter.*
import java.io.StringReader
internal class ConfigTest {
private val parser = TomlParser()
private fun parse(config: String) = parser.parse(StringReader(config))
@Test
fun inputs() {
@Language("toml")
val toml = """
[inputs]
[inputs.name]
default = "example"
display = "Project Name"
[inputs.basePackage]
default = "org.example"
display = "Base package"
""".trimIndent()
val starterConfig = Config(parse(toml)).load()
val expected = listOf(
Input("basePackage", "Base package", "org.example"),
Input("name", "Project Name", "example"),
)
assertThat(starterConfig.inputs).containsExactlyInAnyOrderElementsOf(expected)
}
@Test
fun dependencies() {
@Language("toml")
val toml = """
[versions]
assertj = "3.17.2"
koin = "2.1.6"
h2 = "1.4.200"
[repositories]
[repositories.jcenter]
url = "https://jcenter.bintray.com"
[dependencies]
[dependencies.assertj]
groupId = "org.assertj"
artifactId = "assertj-core"
scope = "test"
category = "test"
default = true
[dependencies.koin]
groupId = "org.koin"
artifactId = "koin-core"
category = "injection"
repository = "jcenter"
default = true
[dependencies.h2]
groupId = "com.h2database"
category = "database"
""".trimIndent()
val starterConfig = Config(parse(toml)).load()
val expected = listOf(
Dependency(
name = "h2",
groupId = "com.h2database",
artifactId = "h2",
version = Version("h2", "1.4.200"),
default = false,
category = Category.Database,
scope = Scope.Compile,
logger = null,
repository = null,
),
Dependency(
name = "assertj",
groupId = "org.assertj",
artifactId = "assertj-core",
version = Version("assertj", "3.17.2"),
default = true,
category = Category.Test,
scope = Scope.Test,
logger = null,
repository = null,
),
Dependency(
name = "koin",
groupId = "org.koin",
artifactId = "koin-core",
version = Version("koin", "2.1.6"),
default = true,
category = Category.Injection,
scope = Scope.Compile,
logger = null,
repository = Repository("jcenter", "https://jcenter.bintray.com"),
),
)
assertThat(starterConfig.dependencies).containsExactlyInAnyOrderElementsOf(expected)
}
}

View File

@ -9,7 +9,7 @@ import org.w3c.dom.Node
import org.w3c.dom.NodeList
import starter.Project
import starter.config.StarterConfig
import starter.modules.mainModule
import starter.modules.configModule
import starter.modules.pebbleModule
import starter.modules.templateModule
import javax.xml.parsers.DocumentBuilderFactory
@ -20,7 +20,7 @@ import javax.xml.xpath.XPathFactory
internal class PomTemplateTest {
private val koin = koinApplication {
modules(mainModule, pebbleModule, templateModule)
modules(configModule, pebbleModule, templateModule)
}.koin
private val pomTemplate = koin.get<PomTemplate>()