diff --git a/radar-jersey/src/main/kotlin/org/radarbase/jersey/config/ConfigLoader.kt b/radar-jersey/src/main/kotlin/org/radarbase/jersey/config/ConfigLoader.kt index 3ed3080..8db16c3 100644 --- a/radar-jersey/src/main/kotlin/org/radarbase/jersey/config/ConfigLoader.kt +++ b/radar-jersey/src/main/kotlin/org/radarbase/jersey/config/ConfigLoader.kt @@ -31,9 +31,8 @@ object ConfigLoader { factoryClass: Class, vararg parameters: Any, ): ResourceConfig { - val enhancerFactory = factoryClass - .getConstructor(*parameters.map { it.javaClass }.toTypedArray()) - .newInstance(*parameters) + val enhancerFactory = + factoryClass.getConstructor(*parameters.map { it.javaClass }.toTypedArray()).newInstance(*parameters) return createResourceConfig(enhancerFactory.createEnhancers()) } @@ -57,8 +56,7 @@ object ConfigLoader { exitProcess(0) } - val configFile = args.firstOrNull()?.let { Path(it) } - ?: fileNames.map { Path(it) }.firstOrNull { it.exists() } + val configFile = args.firstOrNull()?.let { Path(it) } ?: fileNames.map { Path(it) }.firstOrNull { it.exists() } requireNotNull(configFile) { "Configuration not provided." } logger.info("Reading configuration from {}", configFile.toAbsolutePath()) @@ -113,22 +111,21 @@ object ConfigLoader { * Create a resourceConfig based on the provided resource enhancers. This method also disables * the WADL since it may be identified as a security risk. */ - fun createResourceConfig(enhancers: List): ResourceConfig = - ResourceConfig().apply { - property("jersey.config.server.wadl.disableWadl", true) - enhancers.forEach { enhancer -> - packages(*enhancer.packages) - registerClasses(*enhancer.classes) - enhancer.enhanceResources(this@apply) - } - - register(object : AbstractBinder() { - override fun configure() { - enhancers.forEach { it.enhanceBinder(this) } - } - }) + fun createResourceConfig(enhancers: List): ResourceConfig = ResourceConfig().apply { + property("jersey.config.server.wadl.disableWadl", true) + enhancers.forEach { enhancer -> + packages(*enhancer.packages) + registerClasses(*enhancer.classes) + enhancer.enhanceResources(this@apply) } + register(object : AbstractBinder() { + override fun configure() { + enhancers.forEach { it.enhanceBinder(this) } + } + }) + } + val logger: Logger = LoggerFactory.getLogger(ConfigLoader::class.java) /** @@ -137,12 +134,16 @@ object ConfigLoader { */ inline fun T.copyEnv( key: String, - doCopy: T.(String?) -> T, - ): T = copyOnChange( - original = null, - modification = { System.getenv(key) }, - doCopy = doCopy, - ) + envVarAccessor: (String?) -> String? = System::getenv, + doCopy: T.(String) -> T, + ): T { + val newValue = envVarAccessor(key) + return if (newValue != null) { + doCopy(newValue) + } else { + this + } + } /** * Perform copy if the [modification] function makes any change to an [original] value. diff --git a/radar-jersey/src/test/kotlin/org/radarbase/jersey/config/ConfigLoaderTest.kt b/radar-jersey/src/test/kotlin/org/radarbase/jersey/config/ConfigLoaderTest.kt index 7fd52f6..7f892e1 100644 --- a/radar-jersey/src/test/kotlin/org/radarbase/jersey/config/ConfigLoaderTest.kt +++ b/radar-jersey/src/test/kotlin/org/radarbase/jersey/config/ConfigLoaderTest.kt @@ -2,14 +2,44 @@ package org.radarbase.jersey.config import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Test +import org.radarbase.jersey.config.ConfigLoader.copyEnv internal class ConfigLoaderTest { + + val testFile = javaClass.getResource("/config/test.yaml")?.file.toString() + val config = ConfigLoader.loadConfig(testFile, emptyArray()) + @Test fun loadConfig() { - val testFile = javaClass.getResource("/config/test.yaml")?.file.toString() - val config = ConfigLoader.loadConfig(testFile, emptyArray()) - assertEquals(Config(mapOf("test" to "a")), config) + assertEquals( + NestedConfig(requiredArg = "a", optionalArg = "b"), + config.config, + ) + } + + @Test + fun testEnvOverride() { + assertEquals( + NestedConfig(requiredArg = "overridden_required_arg", optionalArg = "overridden_optional_arg"), + config.config.withEnv(), + ) + } + + data class NestedConfig( + val requiredArg: String, + val optionalArg: String?, + ) { + val envVarsMock = mapOf( + Pair("OPTIONAL_ARG", "overridden_optional_arg"), + Pair("REQUIRED_ARG", "overridden_required_arg"), + ) + + fun withEnv(): NestedConfig = this + .copyEnv("OPTIONAL_ARG", { envVarsMock[it] }) { copy(optionalArg = it) } + .copyEnv("REQUIRED_ARG", { envVarsMock[it] }) { copy(requiredArg = it) } } - data class Config(val config: Map) + data class Config( + val config: NestedConfig, + ) } diff --git a/radar-jersey/src/test/resources/config/test.yaml b/radar-jersey/src/test/resources/config/test.yaml index 36257ca..03b2646 100644 --- a/radar-jersey/src/test/resources/config/test.yaml +++ b/radar-jersey/src/test/resources/config/test.yaml @@ -1,2 +1,3 @@ config: - test: a + requiredArg: a + optionalArg: b