From 8dfbfd2f2b4e99de4f50f53343084f79d41bdb05 Mon Sep 17 00:00:00 2001 From: Justin Kaufman Date: Wed, 19 Aug 2015 17:10:53 -0500 Subject: [PATCH] Improved failure behavior and spelling corrections. --- README.md | 2 +- client/java/README.md | 2 +- .../java/us/xeserv/ponyapi/JsonDecoder.java | 33 +++++++++++++++---- .../java/us/xeserv/ponyapi/PonyApiClient.java | 18 ++++++++-- .../us/xeserv/ponyapi/JsonDecoderTest.java | 17 ++++++++++ .../us/xeserv/ponyapi/PonyApiClientTest.java | 26 +++++++++------ 6 files changed, 76 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index bbf612c..78d608d 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ Clients - [Go](https://godoc.org/github.com/Xe/PonyAPI/client/go) - [Nim](https://github.com/Xe/PonyAPI/blob/master/client/nim/ponyapi.nim) [Docs](http://ponyapi.apps.xeserv.us/static/nim.html) - [Python](https://github.com/Xe/PonyAPI/blob/master/client/python/ponyapi.py) -- [Java](https://github.com/Xe/PonyAPI/client/java) +- [Java](https://github.com/Xe/PonyAPI/tree/master/client/java) Routes ------ diff --git a/client/java/README.md b/client/java/README.md index a3113ed..efb36f3 100644 --- a/client/java/README.md +++ b/client/java/README.md @@ -22,7 +22,7 @@ public class PonyApiExample { // Initialize a client with a custom API host PonyApiClient client = new PonyApiClient("some.fqdn.here"); // defaults to port 80 client = new PonyApiClient("some.fqdn.here", 8080); // with a custom port number - // Initialize a client using http://ponyapi.apps.xeserv.us/ as the API ohst + // Initialize a client using http://ponyapi.apps.xeserv.us/ as the API host client = new PonyApiClient(); // Get a list of all the episodes diff --git a/client/java/src/main/java/us/xeserv/ponyapi/JsonDecoder.java b/client/java/src/main/java/us/xeserv/ponyapi/JsonDecoder.java index d836929..b65179d 100644 --- a/client/java/src/main/java/us/xeserv/ponyapi/JsonDecoder.java +++ b/client/java/src/main/java/us/xeserv/ponyapi/JsonDecoder.java @@ -1,5 +1,6 @@ package us.xeserv.ponyapi; +import com.google.common.base.Strings; import com.google.gson.*; import java.util.ArrayList; @@ -24,17 +25,35 @@ class JsonDecoder { } protected static Episode fromJson(String json) { - JsonObject wrapper = (JsonObject) new JsonParser().parse(json); - JsonObject payload = wrapper.get("episode").getAsJsonObject(); - return gson.fromJson(payload, Episode.class); + if (Strings.isNullOrEmpty(json)) { + return null; + } + try { + JsonObject wrapper = (JsonObject) new JsonParser().parse(json); + JsonObject payload = wrapper.get("episode").getAsJsonObject(); + return gson.fromJson(payload, Episode.class); + } catch (Exception ignored) { + // TODO: Logging for parse errors or passing a general parse exception + } + return null; } protected static List listFromJson(String json) { + if (Strings.isNullOrEmpty(json)) { + return null; + } List list = new ArrayList<>(); - JsonObject wrapper = (JsonObject) new JsonParser().parse(json); - JsonArray payload = wrapper.get("episodes").getAsJsonArray(); - for (JsonElement episode : payload) { - list.add(gson.fromJson(episode, Episode.class)); + try { + JsonObject wrapper = (JsonObject) new JsonParser().parse(json); + JsonArray payload = wrapper.get("episodes").getAsJsonArray(); + for (JsonElement episode : payload) { + list.add(gson.fromJson(episode, Episode.class)); + } + } catch (Exception ignored) { + // TODO: Logging for parse errors or passing a general parse exception + } + if (list.isEmpty()) { + return null; } return list; } diff --git a/client/java/src/main/java/us/xeserv/ponyapi/PonyApiClient.java b/client/java/src/main/java/us/xeserv/ponyapi/PonyApiClient.java index 63aaa37..e0ec497 100644 --- a/client/java/src/main/java/us/xeserv/ponyapi/PonyApiClient.java +++ b/client/java/src/main/java/us/xeserv/ponyapi/PonyApiClient.java @@ -29,7 +29,16 @@ public class PonyApiClient { } /** - * Accepts a FQDN (and optionally port) running the PonyAPI service. + * Accepts a FQDN and port running the PonyAPI service on a specified port. + * @param host a FQDN running the PonyAPI service + * @param port the port the PonyAPI service is running on + */ + public PonyApiClient(String host, int port) { + this.host = host + ":" + port; + } + + /** + * Accepts a FQDN running the PonyAPI service on port 80. * @param host a FQDN running the PonyAPI service */ public PonyApiClient(String host) { @@ -136,8 +145,11 @@ public class PonyApiClient { if (Strings.isNullOrEmpty(query)) { throw new IllegalArgumentException("A search query is required."); } - String json = asJson(get("/search?q=" + URLEncoder.encode(query, "UTF-8"))); - return JsonDecoder.listFromJson(json); + HttpResponse response = get("/search?q=" + URLEncoder.encode(query, "UTF-8")); + if (statusCode(response) == 404) { + return null; + } + return JsonDecoder.listFromJson(asJson(response)); } private HttpResponse get(String path) throws IOException { diff --git a/client/java/src/test/java/us/xeserv/ponyapi/JsonDecoderTest.java b/client/java/src/test/java/us/xeserv/ponyapi/JsonDecoderTest.java index 985f685..584d287 100644 --- a/client/java/src/test/java/us/xeserv/ponyapi/JsonDecoderTest.java +++ b/client/java/src/test/java/us/xeserv/ponyapi/JsonDecoderTest.java @@ -6,9 +6,18 @@ import java.time.Instant; import java.util.List; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; public class JsonDecoderTest { + @Test + public void malformedInputTest() { + assertNull(JsonDecoder.fromJson(null)); + assertNull(JsonDecoder.fromJson("")); + assertNull(JsonDecoder.fromJson("{ \"error\": { \"msg\": \"some error\", \"code\": 404 }")); + } + @Test public void episodeTest() { String json = "{\n" @@ -51,6 +60,13 @@ public class JsonDecoderTest { assertEquals(true, episode.isMovie); } + @Test + public void multipleMalformedInputTest() { + assertNull(JsonDecoder.listFromJson(null)); + assertNull(JsonDecoder.listFromJson("")); + assertNull(JsonDecoder.listFromJson("{ \"error\": { \"msg\": \"some error\", \"code\": 404 }")); + } + @Test public void multipleTest() { String json = "{\n" @@ -73,6 +89,7 @@ public class JsonDecoderTest { + "}"; List episodes = JsonDecoder.listFromJson(json); + assertNotNull(episodes); assertEquals(2, episodes.size()); Episode episode1 = episodes.get(0); diff --git a/client/java/src/test/java/us/xeserv/ponyapi/PonyApiClientTest.java b/client/java/src/test/java/us/xeserv/ponyapi/PonyApiClientTest.java index 2eca18e..7c50243 100644 --- a/client/java/src/test/java/us/xeserv/ponyapi/PonyApiClientTest.java +++ b/client/java/src/test/java/us/xeserv/ponyapi/PonyApiClientTest.java @@ -33,7 +33,7 @@ public class PonyApiClientTest { @Test public void getSeasonTest() throws IOException { List list = client.getSeason(98); - assertEquals(null, list); + assertNull(list); list = client.getSeason(1); assertEquals(26, list.size()); } @@ -46,22 +46,26 @@ public class PonyApiClientTest { @Test public void getEpisodeTest() throws IOException { - Episode episode = client.getEpisode(1, 1); + Episode episode = client.getEpisode(98, 1); + assertNull(episode); + episode = client.getEpisode(1, 1); assertEquals("Friendship is Magic Part 1", episode.name); assertEquals(Instant.ofEpochSecond(1286735400), episode.airDate); assertEquals(1, episode.season); assertEquals(1, episode.episode); - assertEquals(false, episode.isMovie); + assertFalse(episode.isMovie); } @Test public void getMovieTest() throws IOException { - Episode episode = client.getMovie(1); - assertEquals("Equestria Girls", episode.name); - assertEquals(Instant.ofEpochSecond(1371340800), episode.airDate); - assertEquals(99, episode.season); - assertEquals(1, episode.episode); - assertEquals(true, episode.isMovie); + Episode movie = client.getMovie(98); + assertNull(movie); + movie = client.getMovie(1); + assertEquals("Equestria Girls", movie.name); + assertEquals(Instant.ofEpochSecond(1371340800), movie.airDate); + assertEquals(99, movie.season); + assertEquals(1, movie.episode); + assertTrue(movie.isMovie); } @Test @@ -72,7 +76,9 @@ public class PonyApiClientTest { @Test public void searchTest() throws IOException { - List episodes = client.search("Owl's Well"); + List episodes = client.search("No Results"); + assertNull(episodes); + episodes = client.search("Owl's Well"); assertEquals(1, episodes.size()); Episode episode = episodes.get(0); assertEquals("Owl's Well That Ends Well", episode.name);