Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/main/java/dev/openfeature/sdk/ImmutableContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import dev.openfeature.sdk.internal.ExcludeFromGeneratedCoverageReport;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import lombok.ToString;
Expand Down Expand Up @@ -32,7 +31,7 @@ public final class ImmutableContext implements EvaluationContext {
* provided.
*/
public ImmutableContext() {
this(new HashMap<>());
this(Collections.emptyMap());
}

/**
Expand All @@ -41,7 +40,7 @@ public ImmutableContext() {
* @param targetingKey targeting key
*/
public ImmutableContext(String targetingKey) {
this(targetingKey, new HashMap<>());
this(targetingKey, Collections.emptyMap());
}

/**
Expand All @@ -63,6 +62,8 @@ public ImmutableContext(Map<String, Value> attributes) {
public ImmutableContext(String targetingKey, Map<String, Value> attributes) {
if (targetingKey != null) {
this.structure = new ImmutableStructure(targetingKey, attributes);
} else if (attributes == null || attributes.isEmpty()) {
this.structure = ImmutableStructure.EMPTY;
} else {
this.structure = new ImmutableStructure(attributes);
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/dev/openfeature/sdk/ImmutableStructure.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.openfeature.sdk;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
Expand All @@ -21,6 +22,8 @@
@SuppressWarnings({"PMD.BeanMembersShouldSerialize", "checkstyle:MissingJavadocType"})
public final class ImmutableStructure extends AbstractStructure {

static final ImmutableStructure EMPTY = new ImmutableStructure(Collections.emptyMap());

/**
* create an immutable structure with the empty attributes.
*/
Expand Down
37 changes: 37 additions & 0 deletions src/test/java/dev/openfeature/sdk/ImmutableContextTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.openfeature.sdk;

import static dev.openfeature.sdk.EvaluationContext.TARGETING_KEY;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
Expand Down Expand Up @@ -216,6 +217,42 @@ void immutableContextHashCodeIsStable() {
assertEquals(first, second);
}

@Nested
@DisplayName("ImmutableContext(String, Map) branch logic")
class ConstructorBranches {

@Test
@DisplayName("non-null targeting key with empty attributes is preserved")
void nonNullTargetingKeyWithEmptyAttributesIsPreserved() {
ImmutableContext ctx = new ImmutableContext("key", Collections.emptyMap());
assertThat(ctx.getTargetingKey()).isEqualTo("key");
assertThat(ctx.keySet()).contains(TARGETING_KEY);
}

@Test
@DisplayName("non-null targeting key with null attributes is preserved")
void nonNullTargetingKeyWithNullAttributesIsPreserved() {
ImmutableContext ctx = new ImmutableContext("key", null);
assertThat(ctx.getTargetingKey()).isEqualTo("key");
}

@Test
@DisplayName("null targeting key with empty attributes yields empty context")
void nullTargetingKeyWithEmptyAttributesYieldsEmptyContext() {
ImmutableContext ctx = new ImmutableContext((String) null, Collections.emptyMap());
assertThat(ctx.getTargetingKey()).isNull();
assertThat(ctx.isEmpty()).isTrue();
}

@Test
@DisplayName("null targeting key with null attributes yields empty context")
void nullTargetingKeyWithNullAttributesYieldsEmptyContext() {
ImmutableContext ctx = new ImmutableContext((String) null, null);
assertThat(ctx.getTargetingKey()).isNull();
assertThat(ctx.isEmpty()).isTrue();
}
}

@Nested
class Equals {
ImmutableContext ctx = new ImmutableContext("c", Map.of("a", new Value("b")));
Expand Down
Loading