diff --git a/build.gradle b/build.gradle index 8c21e6a5e..df72ecf82 100644 --- a/build.gradle +++ b/build.gradle @@ -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 } diff --git a/src/main/java/com/launchdarkly/client/LDClient.java b/src/main/java/com/launchdarkly/client/LDClient.java index e9594f910..11ceb7c42 100644 --- a/src/main/java/com/launchdarkly/client/LDClient.java +++ b/src/main/java/com/launchdarkly/client/LDClient.java @@ -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(); } @@ -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(); } diff --git a/src/test/java/com/launchdarkly/client/FeatureRequestEventTest.java b/src/test/java/com/launchdarkly/client/FeatureRequestEventTest.java new file mode 100644 index 000000000..afe024829 --- /dev/null +++ b/src/test/java/com/launchdarkly/client/FeatureRequestEventTest.java @@ -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); + } +} diff --git a/src/test/java/com/launchdarkly/client/LDClientTest.java b/src/test/java/com/launchdarkly/client/LDClientTest.java index b3f2355a4..f81999079 100644 --- a/src/test/java/com/launchdarkly/client/LDClientTest.java +++ b/src/test/java/com/launchdarkly/client/LDClientTest.java @@ -74,13 +74,7 @@ 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 allFlags = client.allFlags(new LDUser("user")); assertNotNull("Expected non-nil response from allFlags() when offline mode is set to true", allFlags); @@ -88,6 +82,48 @@ public void testTestOfflineModeAllFlags() throws IOException, InterruptedExcepti 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(); @@ -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); + } }