From 182f75219c2b1b89d8fefa417d0ef414b5907708 Mon Sep 17 00:00:00 2001 From: Mateusz Kwasniewski Date: Wed, 7 Feb 2024 08:31:12 +0100 Subject: [PATCH] fix: use config fallback for appName and environment (#70) --- .../kotlin/io/getunleash/UnleashClient.kt | 11 +++++--- .../kotlin/io/getunleash/UnleashClientTest.kt | 28 +++++++++++++++++++ 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/io/getunleash/UnleashClient.kt b/src/main/kotlin/io/getunleash/UnleashClient.kt index 38def2e..d77a228 100644 --- a/src/main/kotlin/io/getunleash/UnleashClient.kt +++ b/src/main/kotlin/io/getunleash/UnleashClient.kt @@ -48,14 +48,14 @@ class UnleashClient( unleashFetcher = fetcher, cache = cache, config = unleashConfig, - context = unleashContext, + context = this.getContext(), unleashConfig.pollingMode ) is FilePollingMode -> FilePollingPolicy( unleashFetcher = fetcher, cache = cache, config = unleashConfig, - context = unleashContext, + context = this.getContext(), unleashConfig.pollingMode ) else -> throw InvalidParameterException("The polling mode parameter is invalid") @@ -98,13 +98,16 @@ class UnleashClient( } override fun updateContext(context: UnleashContext): CompletableFuture { - refreshPolicy.context = context this.unleashContext = context + refreshPolicy.context = this.getContext() return refreshPolicy.refreshAsync() } override fun getContext(): UnleashContext { - return unleashContext + return unleashContext.copy( + appName = unleashContext.appName ?: unleashConfig.appName, + environment = unleashContext.environment ?: unleashConfig.environment + ) } override fun close() { diff --git a/src/test/kotlin/io/getunleash/UnleashClientTest.kt b/src/test/kotlin/io/getunleash/UnleashClientTest.kt index c164741..c6f9770 100644 --- a/src/test/kotlin/io/getunleash/UnleashClientTest.kt +++ b/src/test/kotlin/io/getunleash/UnleashClientTest.kt @@ -188,4 +188,32 @@ class UnleashClientTest { assertThat(updatedFuture).succeedsWithin(Duration.ofSeconds(2)) } } + + @Test + fun `Context falls back to config values for appName and environment when not provided`() { + val fallbackConfig = config.newBuilder().appName("fallbackApp").environment("fallbackEnv").build() + val incompleteContext = UnleashContext.newBuilder().userId("someUserId").build() + + UnleashClient.newBuilder().unleashConfig(fallbackConfig).unleashContext(incompleteContext).build().use { client -> + val usedContext = client.getContext() + + assertThat(usedContext.appName).isEqualTo("fallbackApp") + assertThat(usedContext.environment).isEqualTo("fallbackEnv") + assertThat(usedContext.userId).isEqualTo("someUserId") + } + } + + @Test + fun `Explicit context takes precedence over config appName and environment`() { + val fallbackConfig = config.newBuilder().appName("fallbackApp").environment("fallbackEnv").build() + val explicitContext = UnleashContext.newBuilder().appName("contextApp").environment("contextEnvironment").build() + + UnleashClient.newBuilder().unleashConfig(fallbackConfig).unleashContext(explicitContext).build().use { client -> + val usedContext = client.getContext() + + assertThat(usedContext.appName).isEqualTo("contextApp") + assertThat(usedContext.environment).isEqualTo("contextEnvironment") + } + } + }