diff --git a/Parse/src/main/java/com/parse/ParseObject.java b/Parse/src/main/java/com/parse/ParseObject.java index 9822bfb2f..6c4046bb6 100644 --- a/Parse/src/main/java/com/parse/ParseObject.java +++ b/Parse/src/main/java/com/parse/ParseObject.java @@ -2175,6 +2175,9 @@ public Task then(Task task) throws Exception { @Override public Void then(Task task) throws Exception { isDeleting = false; + if (task.isFaulted()) { + throw task.getError(); + } return null; } }); diff --git a/Parse/src/test/java/com/parse/NetworkObjectControllerTest.java b/Parse/src/test/java/com/parse/NetworkObjectControllerTest.java index a5df28f55..ec3fee473 100644 --- a/Parse/src/test/java/com/parse/NetworkObjectControllerTest.java +++ b/Parse/src/test/java/com/parse/NetworkObjectControllerTest.java @@ -15,7 +15,9 @@ import org.json.JSONObject; 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; @@ -40,6 +42,9 @@ @Config(constants = BuildConfig.class, sdk = TestHelper.ROBOLECTRIC_SDK_VERSION) public class NetworkObjectControllerTest { + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Before public void setUp() throws MalformedURLException { ParseRESTCommand.server = new URL("https://api.parse.com/1"); @@ -138,6 +143,60 @@ public void testDeleteAsync() throws Exception { //endregion + //region testFailingDeleteAsync + + @Test + public void testFailingDeleteAsync() throws Exception { + // Make mock response and client + JSONObject mockResponse = new JSONObject(); + mockResponse.put("code", 141); + mockResponse.put("error", "Delete is not allowed"); + ParseHttpClient restClient = + ParseTestUtils.mockParseHttpClientWithResponse(mockResponse, 400, "Bad Request"); + // Make test state + ParseObject.State state = new ParseObject.State.Builder("Test") + .objectId("testObjectId") + .build(); + + NetworkObjectController controller = new NetworkObjectController(restClient); + + thrown.expect(ParseException.class); + thrown.expectMessage("Delete is not allowed"); + + ParseTaskUtils.wait(controller.deleteAsync(state, "sessionToken")); + } + + //endregion + + //region testFailingSaveAsync + + @Test + public void testFailingSaveAsync() throws Exception { + // Make mock response and client + JSONObject mockResponse = new JSONObject(); + mockResponse.put("code", 141); + mockResponse.put("error", "Save is not allowed"); + ParseHttpClient restClient = + ParseTestUtils.mockParseHttpClientWithResponse(mockResponse, 400, "Bad Request"); + + // Make test object + ParseObject object = new ParseObject("Test"); + object.put("key", "value"); + + NetworkObjectController controller = new NetworkObjectController(restClient); + + thrown.expect(ParseException.class); + thrown.expectMessage("Save is not allowed"); + + ParseTaskUtils.wait(controller.saveAsync( + object.getState(), + object.startSave(), + "sessionToken", + ParseDecoder.get())); + } + + //endregion + //region testSaveAllAsync @Test diff --git a/Parse/src/test/java/com/parse/ParseObjectTest.java b/Parse/src/test/java/com/parse/ParseObjectTest.java index bb11aefa7..5150554d4 100644 --- a/Parse/src/test/java/com/parse/ParseObjectTest.java +++ b/Parse/src/test/java/com/parse/ParseObjectTest.java @@ -20,8 +20,10 @@ import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.robolectric.RobolectricTestRunner; +import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.Config; +import java.net.URL; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -64,6 +66,7 @@ public void setUp() { @After public void tearDown() { ParseCorePlugins.getInstance().reset(); + ParsePlugins.reset(); } @Test @@ -761,6 +764,82 @@ public void testParcelWhileDeletingWithLDSEnabled() throws Exception { //endregion + //region testFailingDelete + + @Test + public void testFailingDelete() throws Exception { + + ParseRESTCommand.server = new URL("https://api.parse.com/1"); + + Parse.Configuration configuration = new Parse.Configuration.Builder(RuntimeEnvironment.application) + .build(); + ParsePlugins plugins = mock(ParsePlugins.class); + when(plugins.configuration()).thenReturn(configuration); + when(plugins.applicationContext()).thenReturn(RuntimeEnvironment.application); + ParsePlugins.set(plugins); + + JSONObject mockResponse = new JSONObject(); + mockResponse.put("code", 141); + mockResponse.put("error", "Delete is not allowed"); + ParseHttpClient restClient = + ParseTestUtils.mockParseHttpClientWithResponse(mockResponse, 400, "Bad Request"); + when(plugins.restClient()).thenReturn(restClient); + + ParseObject.State state = mock(ParseObject.State.class); + when(state.className()).thenReturn("TestObject"); + when(state.objectId()).thenReturn("test_id"); + when(state.keySet()).thenReturn(Collections.singleton("key")); + when(state.get("key")).thenReturn("data"); + ParseObject object = ParseObject.from(state); + + thrown.expect(ParseException.class); + thrown.expectMessage("Delete is not allowed"); + + object.delete(); + } + + //endregion + + //region testFailingSave + + @Test + public void testFailingSave() throws Exception { + + ParseRESTCommand.server = new URL("https://api.parse.com/1"); + + ParseObject.registerSubclass(ParseUser.class); + + Parse.Configuration configuration = new Parse.Configuration.Builder(RuntimeEnvironment.application) + .build(); + ParsePlugins plugins = mock(ParsePlugins.class); + when(plugins.configuration()).thenReturn(configuration); + when(plugins.applicationContext()).thenReturn(RuntimeEnvironment.application); + ParsePlugins.set(plugins); + + JSONObject mockResponse = new JSONObject(); + mockResponse.put("code", 141); + mockResponse.put("error", "Save is not allowed"); + ParseHttpClient restClient = + ParseTestUtils.mockParseHttpClientWithResponse(mockResponse, 400, "Bad Request"); + when(plugins.restClient()).thenReturn(restClient); + + ParseObject.State state = mock(ParseObject.State.class); + when(state.className()).thenReturn("TestObject"); + when(state.objectId()).thenReturn("test_id"); + when(state.keySet()).thenReturn(Collections.singleton("key")); + when(state.get("key")).thenReturn("data"); + ParseObject object = ParseObject.from(state); + + object.put("key", "other data"); + + thrown.expect(ParseException.class); + thrown.expectMessage("Save is not allowed"); + + object.save(); + } + + //endregion + private static void mockCurrentUserController() { ParseCurrentUserController userController = mock(ParseCurrentUserController.class); when(userController.getCurrentSessionTokenAsync()).thenReturn(Task.forResult("token"));