From d4635f82ae1b0074d3758bc14a326f4768e54f1f Mon Sep 17 00:00:00 2001 From: Hubert Van De Walle Date: Tue, 6 Oct 2020 23:30:57 +0200 Subject: [PATCH] Add config loading tests --- src/main/kotlin/starter/KotlinStarter.kt | 7 +- src/main/kotlin/starter/config/Config.kt | 13 +- .../kotlin/starter/modules/ConfigModule.kt | 11 ++ src/main/kotlin/starter/modules/MainModule.kt | 1 - src/test/kotlin/starter/config/ConfigTest.kt | 114 ++++++++++++++++++ .../starter/templates/PomTemplateTest.kt | 4 +- 6 files changed, 136 insertions(+), 14 deletions(-) create mode 100644 src/main/kotlin/starter/modules/ConfigModule.kt create mode 100644 src/test/kotlin/starter/config/ConfigTest.kt diff --git a/src/main/kotlin/starter/KotlinStarter.kt b/src/main/kotlin/starter/KotlinStarter.kt index 4106225..2d617d3 100644 --- a/src/main/kotlin/starter/KotlinStarter.kt +++ b/src/main/kotlin/starter/KotlinStarter.kt @@ -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) } } diff --git a/src/main/kotlin/starter/config/Config.kt b/src/main/kotlin/starter/config/Config.kt index 7aa4dac..d6557ad 100644 --- a/src/main/kotlin/starter/config/Config.kt +++ b/src/main/kotlin/starter/config/Config.kt @@ -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, ) -class Config { +class Config(private val cfg: NightConfig) { @Suppress("UNCHECKED_CAST") - private fun FileConfig.configMap(key: String) = this.get(key).valueMap() as Map + private fun NightConfig.configMap(key: String) = this.get(key) + ?.valueMap() as Map? + ?: emptyMap() fun load(): StarterConfig { - val cfg = FileConfig.of("config.toml") - cfg.load() @Suppress("UNCHECKED_CAST") - val versions = cfg.get("versions").valueMap() as Map + val versions = cfg.get("versions") + ?.valueMap() as Map? + ?: emptyMap() val repositories = cfg.configMap("repositories") .map { (name, values) -> diff --git a/src/main/kotlin/starter/modules/ConfigModule.kt b/src/main/kotlin/starter/modules/ConfigModule.kt new file mode 100644 index 0000000..1bd4d3f --- /dev/null +++ b/src/main/kotlin/starter/modules/ConfigModule.kt @@ -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 { FileConfig.of("config.toml").apply { load() } } + single { Config(get()).load() } +} diff --git a/src/main/kotlin/starter/modules/MainModule.kt b/src/main/kotlin/starter/modules/MainModule.kt index f485ed2..9b448dd 100644 --- a/src/main/kotlin/starter/modules/MainModule.kt +++ b/src/main/kotlin/starter/modules/MainModule.kt @@ -17,7 +17,6 @@ val mainModule = module { get().asServer(SunHttp(7000)).start() } onClose { it?.stop() } - single { Config().load() } single { LoggerFactory.getLogger("Starter") } single { Views(get(), get()) } single { ProjectZip(getAll()) } diff --git a/src/test/kotlin/starter/config/ConfigTest.kt b/src/test/kotlin/starter/config/ConfigTest.kt new file mode 100644 index 0000000..9c75251 --- /dev/null +++ b/src/test/kotlin/starter/config/ConfigTest.kt @@ -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) + } +} diff --git a/src/test/kotlin/starter/templates/PomTemplateTest.kt b/src/test/kotlin/starter/templates/PomTemplateTest.kt index 88fbd8b..9af02a9 100644 --- a/src/test/kotlin/starter/templates/PomTemplateTest.kt +++ b/src/test/kotlin/starter/templates/PomTemplateTest.kt @@ -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()