Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.
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
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repositories {

allprojects {
group = 'com.launchdarkly'
version = "0.16.0-SNAPSHOT"
version = "0.17.0-SNAPSHOT"
sourceCompatibility = 1.6
targetCompatibility = 1.6
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/launchdarkly/client/FeatureRep.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private Float paramForUser(LDUser user) {
String hash;

if (user.getKey() != null) {
idHash = user.getKey();
idHash = user.getKey().getAsString();
}
else {
return null;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/launchdarkly/client/IdentifyEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
class IdentifyEvent extends Event {

IdentifyEvent(LDUser user) {
super("identify", user.getKey(), user);
super("identify", user.getKey().getAsString(), user);
}
}
64 changes: 32 additions & 32 deletions src/main/java/com/launchdarkly/client/LDUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@
* launch a feature to the top 10% of users on a site.
*/
public class LDUser {
private String key;
private String secondary;
private String ip;
private String email;
private String name;
private String avatar;
private String firstName;
private String lastName;
private Boolean anonymous;

private LDCountryCode country;
private JsonPrimitive key;
private JsonPrimitive secondary;
private JsonPrimitive ip;
private JsonPrimitive email;
private JsonPrimitive name;
private JsonPrimitive avatar;
private JsonPrimitive firstName;
private JsonPrimitive lastName;
private JsonPrimitive anonymous;

private JsonPrimitive country;
private Map<String, JsonElement> custom;
private static final Logger logger = LoggerFactory.getLogger(LDUser.class);

Expand All @@ -44,16 +44,16 @@ public class LDUser {
}

protected LDUser(Builder builder) {
this.key = builder.key;
this.ip = builder.ip;
this.country = builder.country;
this.secondary = builder.secondary;
this.firstName = builder.firstName;
this.lastName = builder.lastName;
this.email = builder.email;
this.name = builder.name;
this.avatar = builder.avatar;
this.anonymous = builder.anonymous;
this.key = builder.key == null? null : new JsonPrimitive(builder.key);
this.ip = builder.ip == null? null : new JsonPrimitive(builder.ip);
this.country = builder.country == null? null : new JsonPrimitive(builder.country.getAlpha2());
this.secondary = builder.secondary == null ? null : new JsonPrimitive(builder.secondary);
this.firstName = builder.firstName == null ? null : new JsonPrimitive(builder.firstName);
this.lastName = builder.lastName == null ? null : new JsonPrimitive(builder.lastName);
this.email = builder.email == null ? null : new JsonPrimitive(builder.email);
this.name = builder.name == null ? null : new JsonPrimitive(builder.name);
this.avatar = builder.avatar == null ? null : new JsonPrimitive(builder.avatar);
this.anonymous = builder.anonymous == null ? null : new JsonPrimitive(builder.anonymous);
this.custom = new HashMap<String, JsonElement>(builder.custom);
}

Expand All @@ -62,31 +62,31 @@ protected LDUser(Builder builder) {
* @param key a {@code String} that uniquely identifies a user
*/
public LDUser(String key) {
this.key = key;
this.key = new JsonPrimitive(key);
this.custom = new HashMap<String, JsonElement>();
}

String getKey() {
JsonPrimitive getKey() {
return key;
}

String getIp() { return ip; }
JsonPrimitive getIp() { return ip; }

LDCountryCode getCountry() { return country; }
JsonPrimitive getCountry() { return country; }

String getSecondary() { return secondary; }
JsonPrimitive getSecondary() { return secondary; }

String getName() { return name; }
JsonPrimitive getName() { return name; }

String getFirstName() { return firstName; }
JsonPrimitive getFirstName() { return firstName; }

String getLastName() { return lastName; }
JsonPrimitive getLastName() { return lastName; }

String getEmail() { return email; }
JsonPrimitive getEmail() { return email; }

String getAvatar() { return avatar; }
JsonPrimitive getAvatar() { return avatar; }

Boolean getAnonymous() { return anonymous; }
JsonPrimitive getAnonymous() { return anonymous; }

JsonElement getCustom(String key) {
return custom.get(key);
Expand Down
27 changes: 12 additions & 15 deletions src/main/java/com/launchdarkly/client/Variation.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ static class Builder<E> {
Builder(E value, int weight) {
this.value = value;
this.weight = weight;
this.userTarget = new TargetRule("key", "in", new ArrayList<Object>());
this.userTarget = new TargetRule("key", "in", new ArrayList<JsonPrimitive>());
targets = new ArrayList<TargetRule>();
}

Expand All @@ -115,17 +115,21 @@ Variation<E> build() {
static class TargetRule {
String attribute;
String operator;
List<Object> values;
List<JsonPrimitive> values;

private final Logger logger = LoggerFactory.getLogger(TargetRule.class);

TargetRule(String attribute, String operator, List<Object> values) {
public TargetRule() {

}

TargetRule(String attribute, String operator, List<JsonPrimitive> values) {
this.attribute = attribute;
this.operator = operator;
this.values = new ArrayList<Object>(values);
this.values = new ArrayList<JsonPrimitive>(values);
}

TargetRule(String attribute, List<Object> values) {
TargetRule(String attribute, List<JsonPrimitive> values) {
this(attribute, "in", values);
}

Expand All @@ -143,7 +147,7 @@ else if (attribute.equals("ip") && user.getIp() != null) {
}
else if (attribute.equals("country")) {
if (user.getCountry() != null) {
uValue = user.getCountry().getAlpha2();
uValue = user.getCountry();
}
}
else if (attribute.equals("email")) {
Expand Down Expand Up @@ -187,21 +191,14 @@ else if (attribute.equals("anonymous")) {
logger.error("Invalid custom attribute value in user object: " + elt);
return false;
}
else if (values.contains(elt.getAsString())) {
else if (values.contains(elt.getAsJsonPrimitive())) {
return true;
}
}
return false;
}
else if (custom.isJsonPrimitive()) {
JsonPrimitive prim = custom.getAsJsonPrimitive();
if (prim.isNumber()) {
return values.contains(custom.getAsDouble());
} else if (prim.isBoolean()) {
return values.contains(custom.getAsBoolean());
} else {
return values.contains(custom.getAsString());
}
return values.contains(custom.getAsJsonPrimitive());
}
}
return false;
Expand Down
63 changes: 38 additions & 25 deletions src/test/java/com/launchdarkly/client/FeatureRepTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.launchdarkly.client;

import com.google.gson.JsonPrimitive;
import org.glassfish.jersey.server.JSONP;
import org.junit.Test;

import java.util.Arrays;
Expand All @@ -8,24 +10,24 @@

public class FeatureRepTest {

private final Variation.TargetRule targetUserOn = new Variation.TargetRule("key", Collections.<Object>singletonList("targetOn@test.com"));
private final Variation.TargetRule targetUserOn = new Variation.TargetRule("key", Collections.<JsonPrimitive>singletonList(new JsonPrimitive("targetOn@test.com")));

private final Variation.TargetRule targetGroupOn = new Variation.TargetRule("groups", Arrays.<Object>asList("google", "microsoft"));
private final Variation.TargetRule targetGroupOn = new Variation.TargetRule("groups", Arrays.<JsonPrimitive>asList(new JsonPrimitive("google"), new JsonPrimitive("microsoft")));

// GSON will deserialize numbers as decimals
private final Variation.TargetRule targetFavoriteNumberOn = new Variation.TargetRule("favorite_number", Arrays.<Object>asList(42.0));
private final Variation.TargetRule targetFavoriteNumberOn = new Variation.TargetRule("favorite_number", Arrays.<JsonPrimitive>asList(new JsonPrimitive(42)));

private final Variation.TargetRule targetLikesCatsOn = new Variation.TargetRule("likes_cats", Arrays.<Object>asList(true));
private final Variation.TargetRule targetLikesCatsOn = new Variation.TargetRule("likes_cats", Arrays.<JsonPrimitive>asList(new JsonPrimitive(true)));

private final Variation.TargetRule targetUserOff = new Variation.TargetRule("key", Collections.<Object>singletonList("targetOff@test.com"));
private final Variation.TargetRule targetUserOff = new Variation.TargetRule("key", Collections.<JsonPrimitive>singletonList(new JsonPrimitive("targetOff@test.com")));

private final Variation.TargetRule targetGroupOff = new Variation.TargetRule("groups", Arrays.<Object>asList("oracle"));
private final Variation.TargetRule targetGroupOff = new Variation.TargetRule("groups", Arrays.<JsonPrimitive>asList(new JsonPrimitive("oracle")));

private final Variation.TargetRule targetFavoriteNumberOff = new Variation.TargetRule("favorite_number", Arrays.<Object>asList(33.0));
private final Variation.TargetRule targetFavoriteNumberOff = new Variation.TargetRule("favorite_number", Arrays.<JsonPrimitive>asList(new JsonPrimitive(33.0)));

private final Variation.TargetRule targetLikesDogsOff = new Variation.TargetRule("likes_dogs", Arrays.<Object>asList(false));
private final Variation.TargetRule targetLikesDogsOff = new Variation.TargetRule("likes_dogs", Arrays.<JsonPrimitive>asList(new JsonPrimitive(false)));

private final Variation.TargetRule targetAnonymousOn = new Variation.TargetRule("anonymous", Collections.<Object>singletonList(true));
private final Variation.TargetRule targetAnonymousOn = new Variation.TargetRule("anonymous", Collections.<JsonPrimitive>singletonList(new JsonPrimitive(true)));

private final Variation<Boolean> trueVariation = new Variation.Builder<Boolean>(true, 80)
.target(targetUserOn)
Expand Down Expand Up @@ -119,16 +121,27 @@ public void testFlagForTargetNumericTestOn() {
assertEquals(true, b);
}

@Test
public void testFlagForTargetBooleanTestOn() {
LDUser user = new LDUser.Builder("targetOther@test.com")
.custom("likes_cats", true)
.build();
@Test
public void testFlagForTargetNumericListTestOn() {
LDUser user = new LDUser.Builder("targetOther@test.com")
.customNumber("favorite_number", Arrays.<Number>asList(42, 32))
.build();

Boolean b = simpleFlag.evaluate(user);
Boolean b = simpleFlag.evaluate(user);

assertEquals(true, b);
}
assertEquals(true, b);
}

@Test
public void testFlagForTargetBooleanTestOn() {
LDUser user = new LDUser.Builder("targetOther@test.com")
.custom("likes_cats", true)
.build();

Boolean b = simpleFlag.evaluate(user);

assertEquals(true, b);
}

@Test
public void testFlagForTargetGroupOff() {
Expand All @@ -152,16 +165,16 @@ public void testFlagForTargetNumericTestOff() {
assertEquals(false, b);
}

@Test
public void testFlagForTargetBooleanTestOff() {
LDUser user = new LDUser.Builder("targetOther@test.com")
.custom("likes_dogs", false)
.build();
@Test
public void testFlagForTargetBooleanTestOff() {
LDUser user = new LDUser.Builder("targetOther@test.com")
.custom("likes_dogs", false)
.build();

Boolean b = simpleFlag.evaluate(user);
Boolean b = simpleFlag.evaluate(user);

assertEquals(false, b);
}
assertEquals(false, b);
}

@Test
public void testDisabledFlagAlwaysOff() {
Expand Down
15 changes: 9 additions & 6 deletions src/test/java/com/launchdarkly/client/LDUserTest.java
Original file line number Diff line number Diff line change
@@ -1,37 +1,40 @@
package com.launchdarkly.client;

import com.google.gson.Gson;
import com.google.gson.JsonPrimitive;
import org.junit.Test;

public class LDUserTest {

private JsonPrimitive us = new JsonPrimitive(LDCountryCode.US.getAlpha2());

@Test
public void testValidCountryCodeSetsCountry() {
LDUser user = new LDUser.Builder("key").country(LDCountryCode.US).build();

assert(user.getCountry().equals(LDCountryCode.US));
assert(user.getCountry().equals(us));
}


@Test
public void testValidCountryCodeStringSetsCountry() {
LDUser user = new LDUser.Builder("key").country("US").build();

assert(user.getCountry().equals(LDCountryCode.US));
assert(user.getCountry().equals(us));
}

@Test
public void testValidCountryCode3SetsCountry() {
LDUser user = new LDUser.Builder("key").country("USA").build();

assert(user.getCountry().equals(LDCountryCode.US));
assert(user.getCountry().equals(us));
}

@Test
public void testAmbiguousCountryNameSetsCountryWithExactMatch() {
// "United States" is ambiguous: can also match "United States Minor Outlying Islands"
LDUser user = new LDUser.Builder("key").country("United States").build();
assert(user.getCountry().equals(LDCountryCode.US));
assert(user.getCountry().equals(us));
}

@Test
Expand All @@ -45,7 +48,7 @@ public void testAmbiguousCountryNameSetsCountryWithPartialMatch() {
@Test
public void testPartialUniqueMatchSetsCountry() {
LDUser user = new LDUser.Builder("key").country("United States Minor").build();
assert(user.getCountry().equals(LDCountryCode.UM));
assert(user.getCountry().equals(new JsonPrimitive(LDCountryCode.UM.getAlpha2())));
}

@Test
Expand All @@ -63,6 +66,6 @@ public void testLDUserJsonSerializationContainsCountryAsTwoDigitCode() {

LDUser deserialized = gson.fromJson(jsonStr, LDUser.class);

assert(deserialized.getCountry().equals(LDCountryCode.US));
assert(deserialized.getCountry().equals(us));
}
}