Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.
Closed
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 @@ -19,7 +19,7 @@ repositories {

allprojects {
group = 'com.launchdarkly'
version = "2.0.7-SNAPSHOT"
version = "2.0.8-SNAPSHOT"
sourceCompatibility = 1.7
targetCompatibility = 1.7
}
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/com/launchdarkly/client/LDClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,11 @@ public Integer intVariation(String featureKey, LDUser user, int defaultValue) {
*/
@Override
public Double doubleVariation(String featureKey, LDUser user, Double defaultValue) {
JsonElement value = evaluate(featureKey, user, new JsonPrimitive(defaultValue), VariationType.Double);
JsonElement defaultJson = defaultValue == null ? null : new JsonPrimitive(defaultValue);
JsonElement value = evaluate(featureKey, user, defaultJson, VariationType.Double);
if (value == null) {
return null;
}
return value.getAsJsonPrimitive().getAsDouble();
}

Expand All @@ -290,7 +294,11 @@ public Double doubleVariation(String featureKey, LDUser user, Double defaultValu
*/
@Override
public String stringVariation(String featureKey, LDUser user, String defaultValue) {
JsonElement value = evaluate(featureKey, user, new JsonPrimitive(defaultValue), VariationType.String);
JsonElement defaultJson = defaultValue == null ? null : new JsonPrimitive(defaultValue);
JsonElement value = evaluate(featureKey, user, defaultJson, VariationType.String);
if (value == null) {
return null;
}
return value.getAsJsonPrimitive().getAsString();
}

Expand Down
23 changes: 23 additions & 0 deletions src/test/java/com/launchdarkly/client/FeatureRequestEventTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.launchdarkly.client;

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

import static org.junit.Assert.*;

public class FeatureRequestEventTest {

@Test
public void testSerializeNulls() {
FeatureRequestEvent expectedFre = new FeatureRequestEvent(null, null, null, null, 0, null);
Gson gson = new Gson();
String json = gson.toJson(expectedFre);
FeatureRequestEvent actualFre = gson.fromJson(json, FeatureRequestEvent.class);

assertNull(actualFre.key);
assertNull(actualFre.defaultVal);
assertNull(actualFre.prereqOf);
assertNull(actualFre.value);
assertEquals((Integer) 0, actualFre.version);
}
}
60 changes: 53 additions & 7 deletions src/test/java/com/launchdarkly/client/LDClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,56 @@ public void testTestFeatureStoreSetFeatureTrue() throws IOException, Interrupted
@Test
public void testTestOfflineModeAllFlags() throws IOException, InterruptedException, ExecutionException, TimeoutException {
TestFeatureStore testFeatureStore = new TestFeatureStore();
LDConfig config = new LDConfig.Builder()
.startWaitMillis(10L)
.offline(true)
.featureStore(testFeatureStore)
.build();

client = new LDClient("", config);//createMockClient(config);
client = createOfflineClient(testFeatureStore);
testFeatureStore.setFeatureTrue("key");
Map<String, JsonElement> allFlags = client.allFlags(new LDUser("user"));
assertNotNull("Expected non-nil response from allFlags() when offline mode is set to true", allFlags);
assertEquals("Didn't get expected flag count from allFlags() in offline mode", 1, allFlags.size());
assertTrue("Test flag should be true, but was not.", allFlags.get("key").getAsBoolean());
}

@Test
public void testTestOfflineModeStringVariationNullDefault() {
TestFeatureStore testFeatureStore = new TestFeatureStore();
client = createOfflineClient(testFeatureStore);

String actual = client.stringVariation("missingKey", new LDUser(""), null);
assertNull("Expected null response:", actual);

String expected = "stringValue";
testFeatureStore.setStringValue("key", expected);
actual = client.stringVariation("key", new LDUser(""), null);
assertEquals(expected, actual);
}

@Test
public void testTestOfflineModeDoubleVariationNullDefault() {
TestFeatureStore testFeatureStore = new TestFeatureStore();
client = createOfflineClient(testFeatureStore);

Double actual = client.doubleVariation("missingKey", new LDUser(""), null);
assertNull("Expected null response:", actual);

Double expected = 100.0;
testFeatureStore.setDoubleValue("key", expected);
actual = client.doubleVariation("key", new LDUser(""), null);
assertEquals(expected, actual);
}

@Test
public void testTestOfflineModeJsonVariationNullDefault() {
TestFeatureStore testFeatureStore = new TestFeatureStore();
client = createOfflineClient(testFeatureStore);

JsonElement actual = client.jsonVariation("missingKey", new LDUser(""), null);
assertNull("Expected null response:", actual);

JsonElement expected = new JsonArray();
testFeatureStore.setJsonValue("key", expected);
actual = client.jsonVariation("key", new LDUser(""), null);
assertEquals(expected, actual);
}

@Test
public void testTestFeatureStoreSetFalse() throws IOException, InterruptedException, ExecutionException, TimeoutException {
TestFeatureStore testFeatureStore = new TestFeatureStore();
Expand Down Expand Up @@ -396,4 +432,14 @@ protected EventProcessor createEventProcessor(String sdkKey, LDConfig config) {
}
};
}

private LDClient createOfflineClient(FeatureStore featureStore) {
LDConfig config = new LDConfig.Builder()
.startWaitMillis(10L)
.offline(true)
.featureStore(featureStore)
.build();

return new LDClient("", config);
}
}