Yashasvi has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/361222 )

Change subject: Tests for Retorfit GalleryItemClient
......................................................................

Tests for Retorfit GalleryItemClient

Change-Id: I8a177aa34aeaa0b83f42a6818c8029923ad8cd34
---
A app/src/test/java/org/wikipedia/gallery/GalleryItemClientTest.java
A app/src/test/res/raw/gallery_item_image.json
2 files changed, 225 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/apps/android/wikipedia 
refs/changes/22/361222/1

diff --git a/app/src/test/java/org/wikipedia/gallery/GalleryItemClientTest.java 
b/app/src/test/java/org/wikipedia/gallery/GalleryItemClientTest.java
new file mode 100644
index 0000000..ca0ac71
--- /dev/null
+++ b/app/src/test/java/org/wikipedia/gallery/GalleryItemClientTest.java
@@ -0,0 +1,119 @@
+package org.wikipedia.gallery;
+
+import android.support.annotation.NonNull;
+
+import com.google.gson.stream.MalformedJsonException;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.mockito.ArgumentCaptor;
+import org.wikipedia.dataclient.WikiSite;
+import org.wikipedia.dataclient.mwapi.MwException;
+import org.wikipedia.dataclient.mwapi.MwQueryResponse;
+import org.wikipedia.dataclient.okhttp.HttpStatusException;
+import org.wikipedia.gallery.GalleryItemClient.Callback;
+import org.wikipedia.page.PageTitle;
+import org.wikipedia.test.MockWebServerTest;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import retrofit2.Call;
+
+import static org.hamcrest.Matchers.is;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Matchers.isA;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+
+public class GalleryItemClientTest extends MockWebServerTest {
+
+    private static final WikiSite WIKISITE_EN = WikiSite.forLanguageCode("en");
+    private static final PageTitle PAGE_TITLE = new PageTitle("File", 
"Kozanji_Kyoto_Kyoto11s5s4592", WIKISITE_EN);
+
+    @NonNull private final GalleryItemClient subject = new GalleryItemClient();
+
+    @Test
+    public void testRequestSuccessForImage() throws Throwable {
+
+        enqueueFromFile("gallery_item_image.json");
+
+        Callback cb = mock(Callback.class);
+
+        Call<MwQueryResponse> call = request(cb, false);
+
+        server().takeRequest();
+
+        ArgumentCaptor<HashMap> captor = 
ArgumentCaptor.forClass(HashMap.class);
+
+        //noinspection unchecked
+        verify(cb).success(eq(call), captor.capture());
+
+        //noinspection unchecked
+        Map<PageTitle, GalleryItem> result = captor.getValue();
+
+        Assert.assertTrue(result.size() > 0);
+
+        GalleryItem galleryItem = (GalleryItem) result.values().toArray()[0];
+
+        Assert.assertTrue(galleryItem != null);
+
+        Assert.assertThat(galleryItem.getHeight(), is(1489));
+
+        Assert.assertThat(galleryItem.getWidth(), is(2125));
+
+        Assert.assertThat(galleryItem.getThumbUrl(), 
is("https://upload.wikimedia.org/wikipedia/commons/thumb/c/c9/Kinkaku3402CBcropped.jpg/1280px-Kinkaku3402CBcropped.jpg";));
+
+        Assert.assertThat(galleryItem.getMimeType(), is("image/jpeg"));
+
+        Assert.assertThat(galleryItem.getUrl(), 
is("https://upload.wikimedia.org/wikipedia/commons/c/c9/Kinkaku3402CBcropped.jpg";));
+
+    }
+
+    @Test
+    public void testRequestResponseMalformed() throws Throwable {
+        server().enqueue("'");
+
+        GalleryItemClient.Callback cb = mock(GalleryItemClient.Callback.class);
+        Call<MwQueryResponse> call = request(cb, false);
+
+        server().takeRequest();
+        assertCallbackFailure(call, cb, MalformedJsonException.class);
+    }
+
+    @Test
+    public void testRequestResponseFailure() throws Throwable {
+        enqueue404();
+
+        GalleryItemClient.Callback cb = mock(GalleryItemClient.Callback.class);
+        Call<MwQueryResponse> call = request(cb, false);
+
+        server().takeRequest();
+        assertCallbackFailure(call, cb, HttpStatusException.class);
+    }
+
+    @Test
+    public void testRequestResponseApiError() throws Throwable {
+        enqueueFromFile("api_error.json");
+
+        GalleryItemClient.Callback cb = mock(GalleryItemClient.Callback.class);
+        Call<MwQueryResponse> call = request(cb, false);
+
+        server().takeRequest();
+        assertCallbackFailure(call, cb, MwException.class);
+    }
+
+    private void assertCallbackFailure(@NonNull Call<MwQueryResponse> call,
+                                       @NonNull GalleryItemClient.Callback cb,
+                                       @NonNull Class<? extends Throwable> 
throwable) {
+        //noinspection unchecked
+        verify(cb, never()).success(any(Call.class), any(HashMap.class));
+        verify(cb).failure(eq(call), isA(throwable));
+    }
+
+    private Call<MwQueryResponse> request(@NonNull Callback cb, boolean 
isVideo) {
+        return subject.request(WIKISITE_EN, 
service(GalleryItemClient.Service.class), PAGE_TITLE, cb, isVideo);
+    }
+}
diff --git a/app/src/test/res/raw/gallery_item_image.json 
b/app/src/test/res/raw/gallery_item_image.json
new file mode 100644
index 0000000..eb4c01e
--- /dev/null
+++ b/app/src/test/res/raw/gallery_item_image.json
@@ -0,0 +1,106 @@
+{
+  "batchcomplete": true,
+  "query": {
+    "pages": [
+      {
+        "imageinfo": [
+          {
+            "descriptionshorturl": 
"https://commons.wikimedia.org/w/index.php?curid=316138";,
+            "descriptionurl": 
"https://commons.wikimedia.org/wiki/File:Kinkaku3402CBcropped.jpg";,
+            "height": 1489,
+            "extmetadata": {
+              "Artist": {
+                "source": "commons-desc-page",
+                "value": "<a href=\"//commons.wikimedia.org/wiki/User:Fg2\" 
title=\"User:Fg2\">Fg2</a>"
+              },
+              "Assessments": {
+                "hidden": "",
+                "source": "commons-categories",
+                "value": "potd"
+              },
+              "AttributionRequired": {
+                "hidden": "",
+                "source": "commons-desc-page",
+                "value": "false"
+              },
+              "Categories": {
+                "hidden": "",
+                "source": "commons-categories",
+                "value": "Featured pictures on Wikipedia, Hebrew|Featured 
pictures on Wikipedia, Indonesian|Featured pictures on Wikipedia, 
Spanish|Full-frame views of Kinkakuji|PD-self|PD-user|Self-published work"
+              },
+              "CommonsMetadataExtension": {
+                "hidden": "",
+                "source": "extension",
+                "value": "1.2"
+              },
+              "Copyrighted": {
+                "hidden": "",
+                "source": "commons-desc-page",
+                "value": "False"
+              },
+              "Credit": {
+                "hidden": "",
+                "source": "commons-desc-page",
+                "value": "<span class=\"int-own-work\" lang=\"en\">Own 
work</span>"
+              },
+              "DateTime": {
+                "hidden": "",
+                "source": "mediawiki-metadata",
+                "value": "2005-09-09 10:36:38"
+              },
+              "DateTimeOriginal": {
+                "source": "commons-desc-page",
+                "value": ""
+              },
+              "ImageDescription": {
+                "source": "commons-desc-page",
+                "value": "The building in this photograph is the Kinkaku, or 
Golden Pavilion, which is the shariden at Rokuonji, the Temple of the Golden 
Pavilion, in <a href=\"//commons.wikimedia.org/wiki/Kyoto\" 
class=\"mw-redirect\" title=\"Kyoto\">Kyoto</a>, <a 
href=\"//commons.wikimedia.org/wiki/Japan\" class=\"mw-redirect\" 
title=\"Japan\">Japan</a>. Same exposure as Image:Kinkaku3402.jpg and 
Image:Kinkaku3402CB.jpg (see gallery). I retouched the upper left corner to 
remove some pine needles."
+              },
+              "License": {
+                "hidden": "",
+                "source": "commons-templates",
+                "value": "pd"
+              },
+              "LicenseShortName": {
+                "hidden": "",
+                "source": "commons-desc-page",
+                "value": "Public domain"
+              },
+              "ObjectName": {
+                "hidden": "",
+                "source": "mediawiki-metadata",
+                "value": "Kinkaku3402CBcropped"
+              },
+              "Permission": {
+                "hidden": "",
+                "source": "commons-desc-page",
+                "value": "I took the photo and contribute my rights in it to 
the public domain."
+              },
+              "Restrictions": {
+                "hidden": "",
+                "source": "commons-desc-page",
+                "value": ""
+              },
+              "UsageTerms": {
+                "hidden": "",
+                "source": "commons-desc-page",
+                "value": "Public domain"
+              }
+            },
+            "mime": "image/jpeg",
+            "url": 
"https://upload.wikimedia.org/wikipedia/commons/c/c9/Kinkaku3402CBcropped.jpg";,
+            "size": 1378812,
+            "thumbheight": 897,
+            "thumburl": 
"https://upload.wikimedia.org/wikipedia/commons/thumb/c/c9/Kinkaku3402CBcropped.jpg/1280px-Kinkaku3402CBcropped.jpg";,
+            "thumbwidth": 1280,
+            "width": 2125
+          }
+        ],
+        "index": 0,
+        "ns": 6,
+        "pageid": 0,
+        "title": "File:Kinkaku3402CBcropped.jpg"
+      }
+    ]
+  }
+}
\ No newline at end of file

-- 
To view, visit https://gerrit.wikimedia.org/r/361222
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I8a177aa34aeaa0b83f42a6818c8029923ad8cd34
Gerrit-PatchSet: 1
Gerrit-Project: apps/android/wikipedia
Gerrit-Branch: master
Gerrit-Owner: Yashasvi <yash.gird...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to