Skip to content

Commit

Permalink
Tested AuthConfig withEnv
Browse files Browse the repository at this point in the history
  • Loading branch information
blootsvoets committed Oct 1, 2020
1 parent 7cf9d71 commit 09cc6d8
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/test/kotlin/org/radarbase/jersey/auth/AuthConfigTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.radarbase.jersey.auth

import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
import org.hamcrest.Matchers.not
import org.junit.jupiter.api.Test
import java.util.*

internal class AuthConfigTest {
@Test
fun testEnv() {
val config = AuthConfig(jwtResourceName = "res_test")

setEnv(mapOf(
"AUTH_KEYSTORE_PASSWORD" to "test",
"MANAGEMENT_PORTAL_CLIENT_ID" to "clId",
"MANAGEMENT_PORTAL_CLIENT_SECRET" to "clSecret",
))
val newConfig = config.withEnv()
assertThat(newConfig, not(equalTo(config)))
assertThat(newConfig, equalTo(
config.copy(
managementPortal = config.managementPortal.copy(
clientId = "clId",
clientSecret = "clSecret",
),
jwtKeystorePassword = "test",
)))
}

@Suppress("UNCHECKED_CAST")
@Throws(Exception::class)
fun setEnv(newenv: Map<String, String>) {
try {
val processEnvironmentClass = Class.forName("java.lang.ProcessEnvironment")
processEnvironmentClass.putAll(null, "theEnvironment", newenv)
processEnvironmentClass.putAll(null, "theCaseInsensitiveEnvironment", newenv)
} catch (e: NoSuchFieldException) {
Collections::class.java.declaredClasses
.filter { "java.util.Collections\$UnmodifiableMap" == it.name }
.forEach { it.putAll(System.getenv(), "m", newenv) }
}
}

@Suppress("UNCHECKED_CAST")
private fun Class<*>.putAll(obj: Any?, fieldName: String, map: Map<String, String>) {
getDeclaredField(fieldName)
.apply { isAccessible = true }
.let { it.get(obj) as MutableMap<String, String> }
.putAll(map)

}
}

0 comments on commit 09cc6d8

Please sign in to comment.