From e6f878bca60ed73f993842661c1893616964d7bd Mon Sep 17 00:00:00 2001 From: Max Schwaab Date: Thu, 28 Nov 2024 10:06:08 +0100 Subject: [PATCH] UnleashContext.Builder Convenience (#255) - made builder populate special properties by name Co-authored-by: ms583h --- .../java/io/getunleash/UnleashContext.java | 23 +++++++++- .../io/getunleash/UnleashContextTest.java | 42 +++++++++++++++++-- 2 files changed, 59 insertions(+), 6 deletions(-) diff --git a/src/main/java/io/getunleash/UnleashContext.java b/src/main/java/io/getunleash/UnleashContext.java index fa619cbfb..a8c3f50d4 100644 --- a/src/main/java/io/getunleash/UnleashContext.java +++ b/src/main/java/io/getunleash/UnleashContext.java @@ -129,7 +129,7 @@ public Builder(UnleashContext context) { context.sessionId.ifPresent(val -> this.sessionId = val); context.remoteAddress.ifPresent(val -> this.remoteAddress = val); context.currentTime.ifPresent(val -> this.currentTime = val); - context.properties.forEach(this.properties::put); + this.properties.putAll(context.properties); } public Builder appName(String appName) { @@ -173,7 +173,26 @@ public Builder now() { } public Builder addProperty(String name, String value) { - properties.put(name, value); + switch (name) { + case "environment": + this.environment = value; + break; + case "appName": + this.appName = value; + break; + case "userId": + this.userId = value; + break; + case "sessionId": + this.sessionId = value; + break; + case "remoteAddress": + this.remoteAddress = value; + break; + default: + this.properties.put(name, value); + } + return this; } diff --git a/src/test/java/io/getunleash/UnleashContextTest.java b/src/test/java/io/getunleash/UnleashContextTest.java index fa6b92113..f5e84fe41 100644 --- a/src/test/java/io/getunleash/UnleashContextTest.java +++ b/src/test/java/io/getunleash/UnleashContextTest.java @@ -1,10 +1,12 @@ package io.getunleash; -import static org.assertj.core.api.Assertions.assertThat; - import io.getunleash.util.UnleashConfig; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.data.MapEntry.entry; + public class UnleashContextTest { @Test @@ -39,7 +41,7 @@ public void should_get_context_with_fields_set() { assertThat(context.getRemoteAddress()).hasValue("127.0.0.1"); assertThat(context.getEnvironment()).hasValue("prod"); assertThat(context.getAppName()).hasValue("myapp"); - assertThat(context.getProperties().get("test")).isEqualTo("me"); + assertThat(context.getProperties()).containsExactly(entry("test", "me")); } @Test @@ -70,7 +72,7 @@ public void should_apply_context_fields() { } @Test - public void should_not_ovveride_static_context_fields() { + public void should_not_override_static_context_fields() { UnleashContext context = UnleashContext.builder() .userId("test@mail.com") @@ -96,4 +98,36 @@ public void should_not_ovveride_static_context_fields() { assertThat(enhanced.getEnvironment()).hasValue("env"); assertThat(enhanced.getAppName()).hasValue("myApp"); } + + @Nested + class BuilderTest { + + @Test + void should_set_special_properties() { + final UnleashContext context = UnleashContext.builder() + .addProperty("userId", "test@mail.com") + .addProperty("sessionId", "123") + .addProperty("remoteAddress", "127.0.0.1") + .addProperty("environment", "env") + .addProperty("appName", "myApp") + .build(); + + assertThat(context.getUserId()).contains("test@mail.com"); + assertThat(context.getSessionId()).contains("123"); + assertThat(context.getRemoteAddress()).contains("127.0.0.1"); + assertThat(context.getEnvironment()).contains("env"); + assertThat(context.getAppName()).contains("myApp"); + } + + @Test + void should_set_non_special_properties() { + final UnleashContext context = UnleashContext.builder() + .addProperty("foo", "bar") + .build(); + + assertThat(context.getProperties()).containsExactly(entry("foo", "bar")); + } + + } + }