From 2817393a163142cbb3faf0bb43b76b0862134c58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Claude=20Joseph-Ang=C3=A9lique?= <claude.josephangelique@gmail.com> Date: Mon, 16 Jul 2018 16:12:21 -0400 Subject: [PATCH] Improved exception handling in EventuallyPin.findAllPinned() (#843) --- .../main/java/com/parse/EventuallyPin.java | 2 +- .../java/com/parse/EventuallyPinTest.java | 59 +++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 Parse/src/test/java/com/parse/EventuallyPinTest.java diff --git a/Parse/src/main/java/com/parse/EventuallyPin.java b/Parse/src/main/java/com/parse/EventuallyPin.java index 7e0ee444e..a50b632f8 100644 --- a/Parse/src/main/java/com/parse/EventuallyPin.java +++ b/Parse/src/main/java/com/parse/EventuallyPin.java @@ -166,7 +166,7 @@ public static Task<List<EventuallyPin>> findAllPinned(Collection<String> exclude // We need pass in a null user because we don't want the query to fetch the current user // from LDS. - return query.findInBackground().continueWithTask(new Continuation<List<EventuallyPin>, Task<List<EventuallyPin>>>() { + return query.findInBackground().onSuccessTask(new Continuation<List<EventuallyPin>, Task<List<EventuallyPin>>>() { @Override public Task<List<EventuallyPin>> then(Task<List<EventuallyPin>> task) throws Exception { final List<EventuallyPin> pins = task.getResult(); diff --git a/Parse/src/test/java/com/parse/EventuallyPinTest.java b/Parse/src/test/java/com/parse/EventuallyPinTest.java new file mode 100644 index 000000000..e3942c35d --- /dev/null +++ b/Parse/src/test/java/com/parse/EventuallyPinTest.java @@ -0,0 +1,59 @@ +package com.parse; + +import android.database.sqlite.SQLiteException; + +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; + +import bolts.Task; + +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +@RunWith(RobolectricTestRunner.class) +@Config(constants = BuildConfig.class, sdk = TestHelper.ROBOLECTRIC_SDK_VERSION) +public class EventuallyPinTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Before + public void setUp() throws Exception { + ParseObject.registerSubclass(EventuallyPin.class); + ParseObject.registerSubclass(ParsePin.class); + } + + @After + public void tearDown() throws Exception { + ParseObject.unregisterSubclass(EventuallyPin.class); + ParseObject.unregisterSubclass(ParsePin.class); + Parse.setLocalDatastore(null); + ParsePlugins.reset(); + } + + @Test + public void testFailingFindAllPinned() throws Exception { + OfflineStore offlineStore = mock(OfflineStore.class); + Parse.setLocalDatastore(offlineStore); + when(offlineStore.findFromPinAsync(eq(EventuallyPin.PIN_NAME), + any(ParseQuery.State.class), + any(ParseUser.class))) + .thenReturn(Task.forError(new SQLiteException())); + + ParsePlugins plugins = mock(ParsePlugins.class); + ParsePlugins.set(plugins); + when(plugins.restClient()).thenReturn(ParseHttpClient.createClient(null)); + + thrown.expect(SQLiteException.class); + + ParseTaskUtils.wait(EventuallyPin.findAllPinned()); + } +} \ No newline at end of file