Skip to content

Commit

Permalink
UnleashContext.Builder Convenience (#255)
Browse files Browse the repository at this point in the history
- made builder populate special properties by name

Co-authored-by: ms583h <[email protected]>
  • Loading branch information
mxsb and ms583h authored Nov 28, 2024
1 parent 19c1a3c commit e6f878b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
23 changes: 21 additions & 2 deletions src/main/java/io/getunleash/UnleashContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}

Expand Down
42 changes: 38 additions & 4 deletions src/test/java/io/getunleash/UnleashContextTest.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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("[email protected]")
Expand All @@ -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", "[email protected]")
.addProperty("sessionId", "123")
.addProperty("remoteAddress", "127.0.0.1")
.addProperty("environment", "env")
.addProperty("appName", "myApp")
.build();

assertThat(context.getUserId()).contains("[email protected]");
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"));
}

}

}

0 comments on commit e6f878b

Please sign in to comment.