Skip to content

Commit 8b6c1c1

Browse files
cjosephaJawnnypoo
authored andcommitted
Delete issue (#832)
* Added tests for #825 * Fixed ParseObjectTest.testFailingSave() issue that was due to ParsePlugins already initialized. * Fixed #825 * Fixed parseObjectTest.testFailingSave() issue due to other test class side effect. * Fixed code style
1 parent 76df4ae commit 8b6c1c1

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed

Parse/src/main/java/com/parse/ParseObject.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2175,6 +2175,9 @@ public Task<Void> then(Task<Void> task) throws Exception {
21752175
@Override
21762176
public Void then(Task<Void> task) throws Exception {
21772177
isDeleting = false;
2178+
if (task.isFaulted()) {
2179+
throw task.getError();
2180+
}
21782181
return null;
21792182
}
21802183
});

Parse/src/test/java/com/parse/NetworkObjectControllerTest.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
import org.json.JSONObject;
1616
import org.junit.After;
1717
import org.junit.Before;
18+
import org.junit.Rule;
1819
import org.junit.Test;
20+
import org.junit.rules.ExpectedException;
1921
import org.junit.runner.RunWith;
2022
import org.robolectric.RobolectricTestRunner;
2123
import org.robolectric.annotation.Config;
@@ -40,6 +42,9 @@
4042
@Config(constants = BuildConfig.class, sdk = TestHelper.ROBOLECTRIC_SDK_VERSION)
4143
public class NetworkObjectControllerTest {
4244

45+
@Rule
46+
public ExpectedException thrown = ExpectedException.none();
47+
4348
@Before
4449
public void setUp() throws MalformedURLException {
4550
ParseRESTCommand.server = new URL("https://api.parse.com/1");
@@ -138,6 +143,60 @@ public void testDeleteAsync() throws Exception {
138143

139144
//endregion
140145

146+
//region testFailingDeleteAsync
147+
148+
@Test
149+
public void testFailingDeleteAsync() throws Exception {
150+
// Make mock response and client
151+
JSONObject mockResponse = new JSONObject();
152+
mockResponse.put("code", 141);
153+
mockResponse.put("error", "Delete is not allowed");
154+
ParseHttpClient restClient =
155+
ParseTestUtils.mockParseHttpClientWithResponse(mockResponse, 400, "Bad Request");
156+
// Make test state
157+
ParseObject.State state = new ParseObject.State.Builder("Test")
158+
.objectId("testObjectId")
159+
.build();
160+
161+
NetworkObjectController controller = new NetworkObjectController(restClient);
162+
163+
thrown.expect(ParseException.class);
164+
thrown.expectMessage("Delete is not allowed");
165+
166+
ParseTaskUtils.wait(controller.deleteAsync(state, "sessionToken"));
167+
}
168+
169+
//endregion
170+
171+
//region testFailingSaveAsync
172+
173+
@Test
174+
public void testFailingSaveAsync() throws Exception {
175+
// Make mock response and client
176+
JSONObject mockResponse = new JSONObject();
177+
mockResponse.put("code", 141);
178+
mockResponse.put("error", "Save is not allowed");
179+
ParseHttpClient restClient =
180+
ParseTestUtils.mockParseHttpClientWithResponse(mockResponse, 400, "Bad Request");
181+
182+
// Make test object
183+
ParseObject object = new ParseObject("Test");
184+
object.put("key", "value");
185+
186+
NetworkObjectController controller = new NetworkObjectController(restClient);
187+
188+
thrown.expect(ParseException.class);
189+
thrown.expectMessage("Save is not allowed");
190+
191+
ParseTaskUtils.wait(controller.saveAsync(
192+
object.getState(),
193+
object.startSave(),
194+
"sessionToken",
195+
ParseDecoder.get()));
196+
}
197+
198+
//endregion
199+
141200
//region testSaveAllAsync
142201

143202
@Test

Parse/src/test/java/com/parse/ParseObjectTest.java

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
import org.junit.rules.ExpectedException;
2121
import org.junit.runner.RunWith;
2222
import org.robolectric.RobolectricTestRunner;
23+
import org.robolectric.RuntimeEnvironment;
2324
import org.robolectric.annotation.Config;
2425

26+
import java.net.URL;
2527
import java.util.ArrayList;
2628
import java.util.Arrays;
2729
import java.util.Collections;
@@ -64,6 +66,7 @@ public void setUp() {
6466
@After
6567
public void tearDown() {
6668
ParseCorePlugins.getInstance().reset();
69+
ParsePlugins.reset();
6770
}
6871

6972
@Test
@@ -761,6 +764,82 @@ public void testParcelWhileDeletingWithLDSEnabled() throws Exception {
761764

762765
//endregion
763766

767+
//region testFailingDelete
768+
769+
@Test
770+
public void testFailingDelete() throws Exception {
771+
772+
ParseRESTCommand.server = new URL("https://api.parse.com/1");
773+
774+
Parse.Configuration configuration = new Parse.Configuration.Builder(RuntimeEnvironment.application)
775+
.build();
776+
ParsePlugins plugins = mock(ParsePlugins.class);
777+
when(plugins.configuration()).thenReturn(configuration);
778+
when(plugins.applicationContext()).thenReturn(RuntimeEnvironment.application);
779+
ParsePlugins.set(plugins);
780+
781+
JSONObject mockResponse = new JSONObject();
782+
mockResponse.put("code", 141);
783+
mockResponse.put("error", "Delete is not allowed");
784+
ParseHttpClient restClient =
785+
ParseTestUtils.mockParseHttpClientWithResponse(mockResponse, 400, "Bad Request");
786+
when(plugins.restClient()).thenReturn(restClient);
787+
788+
ParseObject.State state = mock(ParseObject.State.class);
789+
when(state.className()).thenReturn("TestObject");
790+
when(state.objectId()).thenReturn("test_id");
791+
when(state.keySet()).thenReturn(Collections.singleton("key"));
792+
when(state.get("key")).thenReturn("data");
793+
ParseObject object = ParseObject.from(state);
794+
795+
thrown.expect(ParseException.class);
796+
thrown.expectMessage("Delete is not allowed");
797+
798+
object.delete();
799+
}
800+
801+
//endregion
802+
803+
//region testFailingSave
804+
805+
@Test
806+
public void testFailingSave() throws Exception {
807+
808+
ParseRESTCommand.server = new URL("https://api.parse.com/1");
809+
810+
ParseObject.registerSubclass(ParseUser.class);
811+
812+
Parse.Configuration configuration = new Parse.Configuration.Builder(RuntimeEnvironment.application)
813+
.build();
814+
ParsePlugins plugins = mock(ParsePlugins.class);
815+
when(plugins.configuration()).thenReturn(configuration);
816+
when(plugins.applicationContext()).thenReturn(RuntimeEnvironment.application);
817+
ParsePlugins.set(plugins);
818+
819+
JSONObject mockResponse = new JSONObject();
820+
mockResponse.put("code", 141);
821+
mockResponse.put("error", "Save is not allowed");
822+
ParseHttpClient restClient =
823+
ParseTestUtils.mockParseHttpClientWithResponse(mockResponse, 400, "Bad Request");
824+
when(plugins.restClient()).thenReturn(restClient);
825+
826+
ParseObject.State state = mock(ParseObject.State.class);
827+
when(state.className()).thenReturn("TestObject");
828+
when(state.objectId()).thenReturn("test_id");
829+
when(state.keySet()).thenReturn(Collections.singleton("key"));
830+
when(state.get("key")).thenReturn("data");
831+
ParseObject object = ParseObject.from(state);
832+
833+
object.put("key", "other data");
834+
835+
thrown.expect(ParseException.class);
836+
thrown.expectMessage("Save is not allowed");
837+
838+
object.save();
839+
}
840+
841+
//endregion
842+
764843
private static void mockCurrentUserController() {
765844
ParseCurrentUserController userController = mock(ParseCurrentUserController.class);
766845
when(userController.getCurrentSessionTokenAsync()).thenReturn(Task.forResult("token"));

0 commit comments

Comments
 (0)