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

Change subject: Replace EditPreviewTest with MockWebServer-based 
EditPreviewClientTest
......................................................................

Replace EditPreviewTest with MockWebServer-based EditPreviewClientTest

Bug: T154809
Change-Id: I1767d12be1f9617bd3c38a3b7306f1ff634f1532
---
D app/src/androidTest/java/org/wikipedia/edit/preview/EditPreviewTest.java
M app/src/main/java/org/wikipedia/edit/preview/EditPreviewClient.java
A app/src/test/java/org/wikipedia/edit/preview/EditPreviewClientTest.java
A app/src/test/res/raw/edit_preview.json
4 files changed, 107 insertions(+), 77 deletions(-)


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

diff --git 
a/app/src/androidTest/java/org/wikipedia/edit/preview/EditPreviewTest.java 
b/app/src/androidTest/java/org/wikipedia/edit/preview/EditPreviewTest.java
deleted file mode 100644
index b40a554..0000000
--- a/app/src/androidTest/java/org/wikipedia/edit/preview/EditPreviewTest.java
+++ /dev/null
@@ -1,66 +0,0 @@
-package org.wikipedia.edit.preview;
-
-import android.support.annotation.NonNull;
-import android.support.test.filters.SmallTest;
-
-import org.junit.Test;
-import org.wikipedia.dataclient.WikiSite;
-import org.wikipedia.page.PageTitle;
-import org.wikipedia.testlib.TestLatch;
-
-import retrofit2.Call;
-
-import static org.hamcrest.Matchers.containsString;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertThat;
-
-@SmallTest public class EditPreviewTest {
-    private static WikiSite TESTWIKI = WikiSite.forLanguageCode("test");
-
-    @Test
-    public void testPreview() throws Throwable {
-        PageTitle title = new PageTitle(null, 
"Test_page_for_app_testing/Section1", TESTWIKI);
-        final long randomTime = System.currentTimeMillis();
-        String wikiText = "== Section 2 ==\n\nEditing section INSERT RANDOM & 
HERE test at " + randomTime;
-
-        final TestLatch latch = new TestLatch();
-
-        new EditPreviewClient().request(TESTWIKI, title, wikiText,
-                new EditPreviewClient.Callback() {
-                    @Override
-                    public void success(@NonNull Call<EditPreview> call, 
@NonNull String preview) {
-                        assertThat(preview, 
containsString(String.valueOf(randomTime)));
-                        latch.countDown();
-                    }
-
-                    @Override
-                    public void failure(@NonNull Call<EditPreview> call, 
@NonNull Throwable caught) {
-                        throw new RuntimeException(caught);
-                    }
-                });
-        latch.await();
-    }
-
-    @Test
-    public void testErrorResponse() throws Throwable {
-        PageTitle title = new PageTitle(null, "#[]", TESTWIKI);
-        String wikiText = "foo";
-
-        final TestLatch latch = new TestLatch();
-
-        new EditPreviewClient().request(TESTWIKI, title, wikiText,
-                new EditPreviewClient.Callback() {
-                    @Override
-                    public void success(@NonNull Call<EditPreview> call, 
@NonNull String preview) {
-                        throw new RuntimeException("This should generate an 
error response!");
-                    }
-
-                    @Override
-                    public void failure(@NonNull Call<EditPreview> call, 
@NonNull Throwable caught) {
-                        assertNotNull(caught.getMessage());
-                        latch.countDown();
-                    }
-                });
-        latch.await();
-    }
-}
diff --git 
a/app/src/main/java/org/wikipedia/edit/preview/EditPreviewClient.java 
b/app/src/main/java/org/wikipedia/edit/preview/EditPreviewClient.java
index aee0a35..bf967a9 100644
--- a/app/src/main/java/org/wikipedia/edit/preview/EditPreviewClient.java
+++ b/app/src/main/java/org/wikipedia/edit/preview/EditPreviewClient.java
@@ -4,10 +4,13 @@
 import android.support.annotation.VisibleForTesting;
 
 import org.wikipedia.dataclient.WikiSite;
+import org.wikipedia.dataclient.mwapi.MwApiException;
 import org.wikipedia.dataclient.retrofit.MwCachedService;
 import org.wikipedia.dataclient.retrofit.RetrofitException;
 import org.wikipedia.page.PageTitle;
 import org.wikipedia.server.restbase.RbPageEndpointsCache;
+
+import java.io.IOException;
 
 import retrofit2.Call;
 import retrofit2.Response;
@@ -17,8 +20,7 @@
 import retrofit2.http.POST;
 
 class EditPreviewClient {
-    @NonNull private final MwCachedService<Service> cachedService
-            = new MwCachedService<>(Service.class);
+    @NonNull private final MwCachedService<Service> cachedService = new 
MwCachedService<>(Service.class);
     @NonNull private final Retrofit retrofit = 
RbPageEndpointsCache.INSTANCE.getRetrofit();
 
     Call<EditPreview> request(@NonNull WikiSite wiki, @NonNull PageTitle title,
@@ -35,13 +37,12 @@
             @Override
             public void onResponse(Call<EditPreview> call, 
Response<EditPreview> response) {
                 if (response.isSuccessful()) {
-                    EditPreview preview = response.body();
-                    if (preview.hasPreviewResult()) {
-                        cb.success(call, preview.result());
-                    } else if (preview.info() != null) {
-                        cb.failure(call, new RuntimeException(preview.info()));
+                    if (response.body().success() && 
response.body().hasPreviewResult()) {
+                        cb.success(call, response.body().result());
+                    } else if (response.body().hasError()) {
+                        cb.failure(call, new 
MwApiException(response.body().getError()));
                     } else {
-                        cb.failure(call, new RuntimeException("Received 
unrecognized edit preview response"));
+                        cb.failure(call, new IOException("An unknown error 
occurred."));
                     }
                 } else {
                     cb.failure(call, RetrofitException.httpError(response, 
retrofit));
@@ -61,10 +62,9 @@
         void failure(@NonNull Call<EditPreview> call, @NonNull Throwable 
caught);
     }
 
-    private interface Service {
+    @VisibleForTesting interface Service {
         @FormUrlEncoded
-        
@POST("w/api.php?action=parse&format=json&sectionpreview=true&pst=true&mobileformat=true"
-                + "&prop=text")
+        
@POST("w/api.php?action=parse&format=json&sectionpreview=true&pst=true&mobileformat=true&prop=text")
         Call<EditPreview> previewEdit(@NonNull @Field("title") String title,
                                       @NonNull @Field("text") String text);
     }
diff --git 
a/app/src/test/java/org/wikipedia/edit/preview/EditPreviewClientTest.java 
b/app/src/test/java/org/wikipedia/edit/preview/EditPreviewClientTest.java
new file mode 100644
index 0000000..83c5244
--- /dev/null
+++ b/app/src/test/java/org/wikipedia/edit/preview/EditPreviewClientTest.java
@@ -0,0 +1,87 @@
+package org.wikipedia.edit.preview;
+
+import android.support.annotation.NonNull;
+
+import com.google.gson.stream.MalformedJsonException;
+
+import org.junit.Test;
+import org.wikipedia.dataclient.WikiSite;
+import org.wikipedia.dataclient.mwapi.MwApiException;
+import org.wikipedia.dataclient.retrofit.RetrofitException;
+import org.wikipedia.page.PageTitle;
+import org.wikipedia.test.MockWebServerTest;
+
+import retrofit2.Call;
+
+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 EditPreviewClientTest extends MockWebServerTest {
+    private EditPreviewClient subject = new EditPreviewClient();
+    private PageTitle title = new PageTitle(null, "TEST", 
WikiSite.forLanguageCode("test"));
+
+    @Test public void testRequestSuccessHasResults() throws Throwable {
+        String expected = "<div class=\"mf-section-0\" 
id=\"mf-section-0\"><p>\\o/\\n\\ntest12\\n\\n3</p>\n\n\n\n\n</div>";
+        enqueueFromFile("edit_preview.json");
+
+        EditPreviewClient.Callback cb = mock(EditPreviewClient.Callback.class);
+        Call<EditPreview> call = request(cb);
+
+        server().takeRequest();
+        assertCallbackSuccess(call, cb, expected);
+    }
+
+    @Test public void testRequestResponseApiError() throws Throwable {
+        enqueueFromFile("api_error.json");
+
+        EditPreviewClient.Callback cb = mock(EditPreviewClient.Callback.class);
+        Call<EditPreview> call = request(cb);
+
+        server().takeRequest();
+        assertCallbackFailure(call, cb, MwApiException.class);
+    }
+
+    @Test public void testRequestResponse404() throws Throwable {
+        enqueue404();
+
+        EditPreviewClient.Callback cb = mock(EditPreviewClient.Callback.class);
+        Call<EditPreview> call = request(cb);
+
+        server().takeRequest();
+        assertCallbackFailure(call, cb, RetrofitException.class);
+    }
+
+    @Test public void testRequestResponseMalformed() throws Throwable {
+        server().enqueue("(-(-_(-_-)_-)-)");
+
+        EditPreviewClient.Callback cb = mock(EditPreviewClient.Callback.class);
+        Call<EditPreview> call = request(cb);
+
+        server().takeRequest();
+        assertCallbackFailure(call, cb, MalformedJsonException.class);
+    }
+
+    private void assertCallbackSuccess(@NonNull Call<EditPreview> call,
+                                       @NonNull EditPreviewClient.Callback cb,
+                                       @NonNull String expected) {
+        verify(cb).success(eq(call), eq(expected));
+        //noinspection unchecked
+        verify(cb, never()).failure(any(Call.class), any(Throwable.class));
+    }
+
+    private void assertCallbackFailure(@NonNull Call<EditPreview> call,
+                                       @NonNull EditPreviewClient.Callback cb,
+                                       @NonNull Class<? extends Throwable> 
throwable) {
+        //noinspection unchecked
+        verify(cb, never()).success(any(Call.class), any(String.class));
+        verify(cb).failure(eq(call), isA(throwable));
+    }
+
+    private Call<EditPreview> request(@NonNull EditPreviewClient.Callback cb) {
+        return subject.request(service(EditPreviewClient.Service.class), 
title, "wikitext of change", cb);
+    }
+}
diff --git a/app/src/test/res/raw/edit_preview.json 
b/app/src/test/res/raw/edit_preview.json
new file mode 100644
index 0000000..8162898
--- /dev/null
+++ b/app/src/test/res/raw/edit_preview.json
@@ -0,0 +1,9 @@
+{
+  "parse": {
+    "title": "User:Mhollo/sandbox",
+    "pageid": 46498401,
+    "text": {
+      "*": "<div class=\"mf-section-0\" 
id=\"mf-section-0\"><p>\\o/\\n\\ntest12\\n\\n3</p>\n\n\n\n\n</div>"
+    }
+  }
+}
\ No newline at end of file

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I1767d12be1f9617bd3c38a3b7306f1ff634f1532
Gerrit-PatchSet: 1
Gerrit-Project: apps/android/wikipedia
Gerrit-Branch: master
Gerrit-Owner: Mholloway <mhollo...@wikimedia.org>

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

Reply via email to