mirror of
https://github.com/Unleash/unleash.git
synced 2024-12-28 00:06:53 +01:00
Added a simple json-parsing via gson.
This commit is contained in:
parent
4078ed8d38
commit
4e8d42ff55
@ -30,4 +30,14 @@ public final class Toggle {
|
||||
public Map<String, String> getParameters() {
|
||||
return parameters;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Toggle{" +
|
||||
"name='" + name + '\'' +
|
||||
", enabled=" + enabled +
|
||||
", strategy='" + strategy + '\'' +
|
||||
", parameters=" + parameters +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
@ -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<Toggle> fetchToggles() throws IOException {
|
||||
private Collection<Toggle> 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);
|
||||
}
|
||||
}
|
||||
|
@ -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<Toggle> toggles) {
|
||||
Gson gson = new GsonBuilder().create();
|
||||
return gson.toJson(toggles);
|
||||
return gson.toJson(new ToggleCollection(toggles));
|
||||
}
|
||||
|
||||
|
||||
public static List<Toggle> toListOfToggles(String jsonString) {
|
||||
public static Collection<Toggle> fromJson(String jsonString) {
|
||||
Gson gson = new GsonBuilder().create();
|
||||
return gson.fromJson(jsonString, new TypeToken<List<Toggle>>(){}.getType());
|
||||
return gson.fromJson(jsonString,ToggleCollection.class).getFeatures();
|
||||
}
|
||||
}
|
@ -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<Toggle> fetchToggles() throws ToggleException {
|
||||
try {
|
||||
Collection<Toggle> 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<Toggle> loadFromTempFile() throws ToggleException {
|
||||
private Collection<Toggle> 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";
|
||||
}
|
||||
}
|
||||
}
|
@ -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<Toggle> features;
|
||||
|
||||
public ToggleCollection(final Collection<Toggle> features) {
|
||||
this.features = features;
|
||||
}
|
||||
|
||||
public Collection<Toggle> getFeatures() {
|
||||
return features;
|
||||
}
|
||||
}
|
@ -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<Toggle> 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();
|
||||
}
|
||||
}
|
1
unleash-client-java/src/test/resources/features.json
Normal file
1
unleash-client-java/src/test/resources/features.json
Normal file
@ -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"}}]}
|
Loading…
Reference in New Issue
Block a user