Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Parse/src/main/java/com/parse/ParseObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -2175,6 +2175,9 @@ public Task<Void> then(Task<Void> task) throws Exception {
@Override
public Void then(Task<Void> task) throws Exception {
isDeleting = false;
if (task.isFaulted()) {
throw task.getError();
}
return null;
}
});
Expand Down
59 changes: 59 additions & 0 deletions Parse/src/test/java/com/parse/NetworkObjectControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
Expand Down Expand Up @@ -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
Expand Down
79 changes: 79 additions & 0 deletions Parse/src/test/java/com/parse/ParseObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -64,6 +66,7 @@ public void setUp() {
@After
public void tearDown() {
ParseCorePlugins.getInstance().reset();
ParsePlugins.reset();
}

@Test
Expand Down Expand Up @@ -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"));
Expand Down