diff --git a/unleash-client-java/src/main/no/finn/unleash/Toggle.java b/unleash-client-java/src/main/no/finn/unleash/Toggle.java index 0b8cf1ecb2..fa0e7dd3dc 100644 --- a/unleash-client-java/src/main/no/finn/unleash/Toggle.java +++ b/unleash-client-java/src/main/no/finn/unleash/Toggle.java @@ -30,4 +30,14 @@ public final class Toggle { public Map getParameters() { return parameters; } + + @Override + public String toString() { + return "Toggle{" + + "name='" + name + '\'' + + ", enabled=" + enabled + + ", strategy='" + strategy + '\'' + + ", parameters=" + parameters + + '}'; + } } diff --git a/unleash-client-java/src/main/no/finn/unleash/repository/HTTPToggleRepository.java b/unleash-client-java/src/main/no/finn/unleash/repository/HTTPToggleRepository.java index 65a579cff3..3273898274 100644 --- a/unleash-client-java/src/main/no/finn/unleash/repository/HTTPToggleRepository.java +++ b/unleash-client-java/src/main/no/finn/unleash/repository/HTTPToggleRepository.java @@ -2,7 +2,6 @@ package no.finn.unleash.repository; import java.io.IOException; import java.util.Collection; -import java.util.List; import no.finn.unleash.Toggle; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -49,9 +48,9 @@ public class HTTPToggleRepository implements ToggleRepository { } } - private List fetchToggles() throws IOException { + private Collection fetchToggles() throws IOException { HttpResponse httpResponse = httpClient.execute(new HttpGet(serverEndpoint)); final String jsonString = EntityUtils.toString(httpResponse.getEntity()); - return JsonParser.toListOfToggles(jsonString); + return JsonToggleParser.fromJson(jsonString); } } diff --git a/unleash-client-java/src/main/no/finn/unleash/repository/JsonParser.java b/unleash-client-java/src/main/no/finn/unleash/repository/JsonToggleParser.java similarity index 63% rename from unleash-client-java/src/main/no/finn/unleash/repository/JsonParser.java rename to unleash-client-java/src/main/no/finn/unleash/repository/JsonToggleParser.java index f8390ccab0..67ccb536c6 100644 --- a/unleash-client-java/src/main/no/finn/unleash/repository/JsonParser.java +++ b/unleash-client-java/src/main/no/finn/unleash/repository/JsonToggleParser.java @@ -1,13 +1,11 @@ package no.finn.unleash.repository; import java.util.Collection; -import java.util.List; import com.google.gson.Gson; import com.google.gson.GsonBuilder; -import com.google.gson.reflect.TypeToken; import no.finn.unleash.Toggle; -public final class JsonParser { +public final class JsonToggleParser { public static Toggle toToggle(String jsonString) { Gson gson = new GsonBuilder().create(); @@ -16,12 +14,12 @@ public final class JsonParser { public static String toJsonString(Collection toggles) { Gson gson = new GsonBuilder().create(); - return gson.toJson(toggles); + return gson.toJson(new ToggleCollection(toggles)); } - public static List toListOfToggles(String jsonString) { + public static Collection fromJson(String jsonString) { Gson gson = new GsonBuilder().create(); - return gson.fromJson(jsonString, new TypeToken>(){}.getType()); + return gson.fromJson(jsonString,ToggleCollection.class).getFeatures(); } } diff --git a/unleash-client-java/src/main/no/finn/unleash/repository/PollingToggleRepository.java b/unleash-client-java/src/main/no/finn/unleash/repository/PollingToggleRepository.java index d762d14196..d0c4b0e109 100644 --- a/unleash-client-java/src/main/no/finn/unleash/repository/PollingToggleRepository.java +++ b/unleash-client-java/src/main/no/finn/unleash/repository/PollingToggleRepository.java @@ -8,7 +8,6 @@ import java.io.IOException; import java.util.Collection; import java.util.Collections; import java.util.HashMap; -import java.util.List; import java.util.Map; import java.util.concurrent.Executors; import java.util.concurrent.RejectedExecutionException; @@ -95,7 +94,7 @@ public class PollingToggleRepository implements ToggleRepository { private Collection fetchToggles() throws ToggleException { try { Collection toggles = toggleRepository.getToggles(); - storeRepoAsTempFile(JsonParser.toJsonString(toggles)); + storeRepoAsTempFile(JsonToggleParser.toJsonString(toggles)); return toggles; } catch (ToggleException ex) { if(togglesCache.isEmpty()) { @@ -106,7 +105,7 @@ public class PollingToggleRepository implements ToggleRepository { } - private List loadFromTempFile() throws ToggleException { + private Collection loadFromTempFile() throws ToggleException { LOG.info("Unleash will try to load feature toggle states from temporary backup"); try(FileReader reader = new FileReader(pathToTmpBackupFile())) { BufferedReader br = new BufferedReader(reader); @@ -115,7 +114,7 @@ public class PollingToggleRepository implements ToggleRepository { while((line = br.readLine()) != null) { builder.append(line); } - return JsonParser.toListOfToggles(builder.toString()); + return JsonToggleParser.fromJson(builder.toString()); } catch (IOException e) { LOG.error("Unleash was unable to feature toggle repo from temporary backup: " + pathToTmpBackupFile()); throw new ToggleException("Unleash was unable to feature toggle states from temporary backup"); @@ -133,4 +132,4 @@ public class PollingToggleRepository implements ToggleRepository { private static String pathToTmpBackupFile() { return System.getProperty("java.io.tmpdir") + File.separatorChar + "unleash-repo.json"; } -} +} \ No newline at end of file diff --git a/unleash-client-java/src/main/no/finn/unleash/repository/ToggleCollection.java b/unleash-client-java/src/main/no/finn/unleash/repository/ToggleCollection.java new file mode 100644 index 0000000000..70cdd5ae41 --- /dev/null +++ b/unleash-client-java/src/main/no/finn/unleash/repository/ToggleCollection.java @@ -0,0 +1,17 @@ +package no.finn.unleash.repository; + +import java.util.ArrayList; +import java.util.Collection; +import no.finn.unleash.Toggle; + +public final class ToggleCollection { + private final Collection features; + + public ToggleCollection(final Collection features) { + this.features = features; + } + + public Collection getFeatures() { + return features; + } +} diff --git a/unleash-client-java/src/test/no/finn/unleash/repository/JsonToggleParserTest.java b/unleash-client-java/src/test/no/finn/unleash/repository/JsonToggleParserTest.java new file mode 100644 index 0000000000..ee98c896e9 --- /dev/null +++ b/unleash-client-java/src/test/no/finn/unleash/repository/JsonToggleParserTest.java @@ -0,0 +1,37 @@ +package no.finn.unleash.repository; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import no.finn.unleash.Toggle; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +public class JsonToggleParserTest { + + @Test + public void should_deserialize_correctly() throws IOException { + String content = readFile("/features.json"); + List toggles = new ArrayList<>(JsonToggleParser.fromJson(content)); + + assertThat(toggles.size(), is(3)); + } + + private String readFile(String filename) throws IOException { + InputStream in = this.getClass().getResourceAsStream(filename); + InputStreamReader reader = new InputStreamReader(in); + BufferedReader br = new BufferedReader(reader); + StringBuilder builder = new StringBuilder(); + String line; + while((line = br.readLine()) != null) { + builder.append(line); + } + return builder.toString(); + } +} \ No newline at end of file diff --git a/unleash-client-java/src/test/resources/features.json b/unleash-client-java/src/test/resources/features.json new file mode 100644 index 0000000000..2f4ff05d65 --- /dev/null +++ b/unleash-client-java/src/test/resources/features.json @@ -0,0 +1 @@ +{"features":[{"name":"featureX","status":"on","strategy":"default"},{"name":"featureY","status":"off","strategy":"baz","parameters":{"foo":"bar"}},{"name":"featureZ","status":"on","strategy":"baz","parameters":{"foo":"rab"}}]} \ No newline at end of file