Skip to content

Commit

Permalink
Merge pull request #94 from RADAR-base/feature/allow-env-override-req…
Browse files Browse the repository at this point in the history
…uired-config-options

Impl. override of required config parameters
  • Loading branch information
pvannierop authored Oct 30, 2024
2 parents 2976b6f + bbe5c6b commit 46c24d1
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ object ConfigLoader {
factoryClass: Class<out EnhancerFactory>,
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())
}

Expand All @@ -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())
Expand Down Expand Up @@ -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<JerseyResourceEnhancer>): 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<JerseyResourceEnhancer>): 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)

/**
Expand All @@ -137,12 +134,16 @@ object ConfigLoader {
*/
inline fun <T> T.copyEnv(
key: String,
doCopy: T.(String?) -> T,
): T = copyOnChange<T, String?>(
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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Config>(testFile, emptyArray())

@Test
fun loadConfig() {
val testFile = javaClass.getResource("/config/test.yaml")?.file.toString()
val config = ConfigLoader.loadConfig<Config>(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<String, String>)
data class Config(
val config: NestedConfig,
)
}
3 changes: 2 additions & 1 deletion radar-jersey/src/test/resources/config/test.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
config:
test: a
requiredArg: a
optionalArg: b

0 comments on commit 46c24d1

Please sign in to comment.