Bug 1393700 - Add test for parsing JSON response. r?mcomella draft pt
authorChenxia Liu <liuche@mozilla.com>
Mon, 28 Aug 2017 16:40:39 -0700
changeset 655417 9e066b20bb4d9112479ca932666eabe45355e1ec
parent 654398 3529b653ede26f990eb7320649015294ad0f8e76
child 728835 926b8a74aa22cf6a6ebeaecbacd66e3c7e54d787
push id76875
push usercliu@mozilla.com
push dateWed, 30 Aug 2017 00:56:52 +0000
reviewersmcomella
bugs1393700
milestone57.0a1
Bug 1393700 - Add test for parsing JSON response. r?mcomella MozReview-Commit-ID: KTy3Mltrfg9
mobile/android/base/java/org/mozilla/gecko/activitystream/homepanel/topstories/PocketStoriesLoader.java
mobile/android/tests/background/junit4/src/org/mozilla/gecko/activitystream/homepanel/topstories/TestPocketStoriesLoader.java
--- a/mobile/android/base/java/org/mozilla/gecko/activitystream/homepanel/topstories/PocketStoriesLoader.java
+++ b/mobile/android/base/java/org/mozilla/gecko/activitystream/homepanel/topstories/PocketStoriesLoader.java
@@ -140,17 +140,18 @@ public class PocketStoriesLoader extends
             return null;
         } finally {
             if (connection != null) {
                 connection.disconnect();
             }
         }
     }
 
-    private static List<TopStory> jsonStringToTopStories(String jsonResponse) {
+    /* package-private */
+    static List<TopStory> jsonStringToTopStories(String jsonResponse) {
         final List<TopStory> topStories = new LinkedList<>();
 
         if (TextUtils.isEmpty(jsonResponse)) {
             return topStories;
         }
 
         try {
             final JSONObject jsonObject = new JSONObject(jsonResponse);
new file mode 100644
--- /dev/null
+++ b/mobile/android/tests/background/junit4/src/org/mozilla/gecko/activitystream/homepanel/topstories/TestPocketStoriesLoader.java
@@ -0,0 +1,116 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file,
+ * You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+package org.mozilla.gecko.activitystream.homepanel.topstories;
+
+import junit.framework.Assert;
+
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mozilla.gecko.activitystream.homepanel.model.TopStory;
+import org.mozilla.gecko.background.testhelpers.TestRunner;
+
+import java.util.List;
+
+@RunWith(TestRunner.class)
+public class TestPocketStoriesLoader {
+    private static final String KEY_STATUS = "status";
+    private static final String KEY_LIST = "list";
+
+    private static final String KEY_URL = "url";
+    private static final String KEY_DEDUPE_URL = "dedupe_url";
+    private static final String KEY_TITLE = "title";
+    private static final String KEY_IMAGE_SRC = "image_src";
+
+    private static final String POCKET_URL = "POCKET_URL";
+    private static final String PAGE_URL = "PAGE_URL";
+    private static final String TITLE = "TITLE";
+    private static final String IMAGE_URL = "IMAGE_URL";
+
+    private String makeBasicPocketResponse(JSONObject[] storyItems) {
+        final JSONObject basicResponse = new JSONObject();
+        try {
+            final JSONArray array = new JSONArray();
+            for (JSONObject item : storyItems) {
+                array.put(item);
+            }
+
+            basicResponse.put(KEY_STATUS, 1); // arbitrarily chosen
+            basicResponse.put(KEY_LIST, array);
+
+        } catch (JSONException e) {
+            Assert.fail("Problem creating Pocket response JSON object");
+        }
+        return basicResponse.toString();
+    }
+
+    private JSONObject makeBasicStoryItem() {
+        final JSONObject storyItem = new JSONObject();
+        try {
+            storyItem.put(KEY_URL, POCKET_URL);
+            storyItem.put(KEY_DEDUPE_URL, PAGE_URL);
+            storyItem.put(KEY_TITLE, TITLE);
+            storyItem.put(KEY_IMAGE_SRC, IMAGE_URL);
+        } catch (JSONException e) {
+            Assert.fail("Problem creating Pocket story JSON object");
+        }
+        return storyItem;
+    }
+
+    @Test
+    public void testJSONStringToTopStoriesWithBasicObject() {
+        final String basicResponseString = makeBasicPocketResponse(new JSONObject[] { makeBasicStoryItem() });
+        final List<TopStory> stories = PocketStoriesLoader.jsonStringToTopStories(basicResponseString);
+        Assert.assertEquals(1, stories.size());
+
+        final TopStory story = stories.get(0);
+        Assert.assertEquals(PAGE_URL, story.getUrl());
+        Assert.assertEquals(TITLE, story.getTitle());
+        Assert.assertEquals(IMAGE_URL, story.getImageUrl());
+    }
+
+    @Test
+    public void testJSONStringToTopStoriesWithMissingTitle() {
+        final JSONObject storyItem = makeBasicStoryItem();
+        storyItem.remove(KEY_TITLE);
+        final String malformedResponseString = makeBasicPocketResponse(new JSONObject[] { storyItem });
+        final List<TopStory> stories = PocketStoriesLoader.jsonStringToTopStories(malformedResponseString);
+        Assert.assertEquals(0, stories.size()); // Should skip malformed item.
+    }
+
+    // Pulled 8/28 Pocket response, with some trimming for content/brevity.
+    final String VALID_POCKET_RESPONSE = "{\"status\":1,\"list\":[" +
+            "{\"id\":2910,\"url\":\"https:\\/\\/pocket.co\\/pocket-shorted-url\"," +
+            "\"dedupe_url\":\"http:\\/\\/www.bbc.co.uk\\/actual-url\"," +
+            "\"title\":\"A Title Here\"," +
+            "\"excerpt\":\"Middle-aged people are being urged...\"," +
+            "\"domain\":\"bbc.co.uk\"," +
+            "\"image_src\":\"https:\\/\\/img.cloudfront.net\"," +
+            "\"published_timestamp\":\"1503550800\",\"sort_id\":0}," +
+
+            "{\"id\":2909,\"url\":\"https:\\/\\/pocket.co\\/00000\"," +
+            "\"dedupe_url\":\"http:\\/\\/fortune.com\"," +
+            "\"title\":\"The Mattress Industry\",\"excerpt\":\"In any random commute...\"," +
+            "\"domain\":\"fortune.com\",\"image_src\":\"https:\\/\\/d33ypg4xwx0n86.cloudfront.net\"," +
+            "\"published_timestamp\":\"1503464400\",\"sort_id\":1}," +
+
+            "{\"id\":2908,\"url\":\"https:\\/\\/pocket.co\\/00000\"," +
+            "\"dedupe_url\":\"https:\\/\\/medium.com\",\"title\":\"Mediocrity is a Virus.\"," +
+            "\"excerpt\":\"Little things become big things.\",\"domain\":\"medium.com\"," +
+            "\"image_src\":\"https:\\/\\/d33ypg4xwx0n86.cloudfront.net\",\"published_timestamp\":\"1503578016\",\"sort_id\":2}]}";
+
+    @Test
+    public void testJSONStringToTopStoriesWithPocketServerResponse() {
+        final List<TopStory> stories = PocketStoriesLoader.jsonStringToTopStories(VALID_POCKET_RESPONSE);
+        Assert.assertEquals(3, stories.size());
+        final TopStory story = stories.get(0);
+
+        Assert.assertEquals("http://www.bbc.co.uk/actual-url", story.getUrl());
+        Assert.assertEquals("A Title Here", story.getTitle());
+        Assert.assertEquals("https://img.cloudfront.net", story.getImageUrl());
+    }
+}
\ No newline at end of file