-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7cf9d71
commit 09cc6d8
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
src/test/kotlin/org/radarbase/jersey/auth/AuthConfigTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} | ||
} |