Skip to content
This repository has been archived by the owner on May 30, 2024. It is now read-only.

Commit

Permalink
prepare 3.0.2 release (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
eli-darkly authored Mar 1, 2018
1 parent 044d314 commit d255ded
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/main/java/com/launchdarkly/client/LDUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,13 @@ public LDUser(String key) {
}

protected JsonElement getValueForEvaluation(String attribute) {
try {
return UserAttribute.valueOf(attribute).get(this);
} catch (IllegalArgumentException expected) {
return getCustom(attribute);
// Don't use Enum.valueOf because we don't want to trigger unnecessary exceptions
for (UserAttribute builtIn: UserAttribute.values()) {
if (builtIn.name().equals(attribute)) {
return builtIn.get(this);
}
}
return getCustom(attribute);
}

JsonPrimitive getKey() {
Expand Down
36 changes: 35 additions & 1 deletion src/test/java/com/launchdarkly/client/LDUserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.reflect.TypeToken;

import org.junit.Assert;
import org.junit.Test;

import java.lang.reflect.Type;
Expand Down Expand Up @@ -198,6 +200,38 @@ public void testLDUserCustomMarshalWithBuiltInAttributesRedactsCorrectAttrs() {
Type type = new TypeToken<Map<String, JsonElement>>(){}.getType();
Map<String, JsonElement> privateJson = config.gson.fromJson(config.gson.toJson(user), type);
assertNull(privateJson.get("email"));

}

@Test
public void getValueGetsBuiltInAttribute() {
LDUser user = new LDUser.Builder("key")
.name("Jane")
.build();
assertEquals(new JsonPrimitive("Jane"), user.getValueForEvaluation("name"));
}

@Test
public void getValueGetsCustomAttribute() {
LDUser user = new LDUser.Builder("key")
.custom("height", 5)
.build();
assertEquals(new JsonPrimitive(5), user.getValueForEvaluation("height"));
}

@Test
public void getValueGetsBuiltInAttributeEvenIfCustomAttrHasSameName() {
LDUser user = new LDUser.Builder("key")
.name("Jane")
.custom("name", "Joan")
.build();
assertEquals(new JsonPrimitive("Jane"), user.getValueForEvaluation("name"));
}

@Test
public void getValueReturnsNullIfNotFound() {
LDUser user = new LDUser.Builder("key")
.name("Jane")
.build();
assertNull(user.getValueForEvaluation("height"));
}
}

0 comments on commit d255ded

Please sign in to comment.