From d1b313a14995b87011c36f6b0ae52091302c3246 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20K=C3=B6rber?= Date: Sun, 16 Apr 2017 16:26:41 +0200 Subject: [PATCH] Return empty JSON array for easier processing --- .../java/org/shredzone/acme4j/Authorization.java | 2 +- .../main/java/org/shredzone/acme4j/Metadata.java | 11 +++-------- .../acme4j/connector/ResourceIterator.java | 9 +++------ .../main/java/org/shredzone/acme4j/util/JSON.java | 11 +++++++++-- .../java/org/shredzone/acme4j/util/JSONTest.java | 14 ++++++++++++++ 5 files changed, 30 insertions(+), 17 deletions(-) diff --git a/acme4j-client/src/main/java/org/shredzone/acme4j/Authorization.java b/acme4j-client/src/main/java/org/shredzone/acme4j/Authorization.java index 77472a9b..28634ac3 100644 --- a/acme4j-client/src/main/java/org/shredzone/acme4j/Authorization.java +++ b/acme4j-client/src/main/java/org/shredzone/acme4j/Authorization.java @@ -272,7 +272,7 @@ public class Authorization extends AcmeResource { */ private List> fetchCombinations(JSON json, List challenges) { JSON.Array jsonCombinations = json.get("combinations").asArray(); - if (jsonCombinations == null) { + if (jsonCombinations.isEmpty()) { return Arrays.asList(challenges); } diff --git a/acme4j-client/src/main/java/org/shredzone/acme4j/Metadata.java b/acme4j-client/src/main/java/org/shredzone/acme4j/Metadata.java index 6448fb86..f3d25105 100644 --- a/acme4j-client/src/main/java/org/shredzone/acme4j/Metadata.java +++ b/acme4j-client/src/main/java/org/shredzone/acme4j/Metadata.java @@ -17,10 +17,8 @@ import static java.util.stream.Collectors.toList; import java.net.URI; import java.util.Collection; -import java.util.Collections; import org.shredzone.acme4j.util.JSON; -import org.shredzone.acme4j.util.JSON.Array; import org.shredzone.acme4j.util.JSON.Value; /** @@ -61,12 +59,9 @@ public class Metadata { * itself for the purposes of CAA record validation. Empty if not available. */ public Collection getCaaIdentities() { - Array array = meta.get("caa-identities").asArray(); - if (array == null) { - return Collections.emptyList(); - } - - return array.stream().map(Value::asString).collect(toList()); + return meta.get("caa-identities").asArray().stream() + .map(Value::asString) + .collect(toList()); } /** diff --git a/acme4j-client/src/main/java/org/shredzone/acme4j/connector/ResourceIterator.java b/acme4j-client/src/main/java/org/shredzone/acme4j/connector/ResourceIterator.java index 0ef74d32..5bf80717 100644 --- a/acme4j-client/src/main/java/org/shredzone/acme4j/connector/ResourceIterator.java +++ b/acme4j-client/src/main/java/org/shredzone/acme4j/connector/ResourceIterator.java @@ -157,12 +157,9 @@ public class ResourceIterator implements Iterator { * JSON map to read from */ private void fillUriList(JSON json) { - JSON.Array array = json.get(field).asArray(); - if (array == null) { - return; - } - - array.stream().map(JSON.Value::asURI).forEach(uriList::add); + json.get(field).asArray().stream() + .map(JSON.Value::asURI) + .forEach(uriList::add); } } diff --git a/acme4j-client/src/main/java/org/shredzone/acme4j/util/JSON.java b/acme4j-client/src/main/java/org/shredzone/acme4j/util/JSON.java index a17be6ee..87d61620 100644 --- a/acme4j-client/src/main/java/org/shredzone/acme4j/util/JSON.java +++ b/acme4j-client/src/main/java/org/shredzone/acme4j/util/JSON.java @@ -207,6 +207,13 @@ public final class JSON implements Serializable { return data.size(); } + /** + * Returns {@code true} if the array is empty. + */ + public boolean isEmpty() { + return data.isEmpty(); + } + /** * Gets the {@link Value} at the given index. * @@ -298,11 +305,11 @@ public final class JSON implements Serializable { /** * Returns the value as JSON array. * - * @return {@link JSON.Array}, or {@code null} if the value was not set. + * @return {@link JSON.Array}, which is empty if the value was not set. */ public Array asArray() { if (val == null) { - return null; + return new Array(path, Collections.emptyList()); } try { diff --git a/acme4j-client/src/test/java/org/shredzone/acme4j/util/JSONTest.java b/acme4j-client/src/test/java/org/shredzone/acme4j/util/JSONTest.java index 110f8e4b..b30f0d46 100644 --- a/acme4j-client/src/test/java/org/shredzone/acme4j/util/JSONTest.java +++ b/acme4j-client/src/test/java/org/shredzone/acme4j/util/JSONTest.java @@ -102,12 +102,26 @@ public class JSONTest { JSON.Array array = json.get("array").asArray(); assertThat(array.size(), is(4)); + assertThat(array.isEmpty(), is(false)); assertThat(array.get(0), is(notNullValue())); assertThat(array.get(1), is(notNullValue())); assertThat(array.get(2), is(notNullValue())); assertThat(array.get(3), is(notNullValue())); } + /** + * Test empty array. + */ + @Test + public void testEmptyArray() { + JSON json = TestUtils.getJsonAsObject("json"); + JSON.Array array = json.get("missingArray").asArray(); + + assertThat(array.size(), is(0)); + assertThat(array.isEmpty(), is(true)); + assertThat(array.stream().count(), is(0L)); + } + /** * Test all array iterator related methods. */