From 6e68e1e30406d7686e4c1ddbccf6b5aaa4b4ea19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Claude=20Joseph-Ange=CC=81lique?= Date: Thu, 24 May 2018 16:08:09 -0400 Subject: [PATCH 1/5] Added tests for https://github.com/parse-community/Parse-SDK-Android/issues/825 --- .../parse/NetworkObjectControllerTest.java | 59 ++++++++++++++ .../test/java/com/parse/ParseObjectTest.java | 76 +++++++++++++++++++ 2 files changed, 135 insertions(+) diff --git a/Parse/src/test/java/com/parse/NetworkObjectControllerTest.java b/Parse/src/test/java/com/parse/NetworkObjectControllerTest.java index a5df28f55..5cafc9a8f 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..a91dd7db1 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; @@ -761,6 +763,80 @@ 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"); + + 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")); From c20ce2ebefa3b993db2e008d84842f323d5f811d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Claude=20Joseph-Ange=CC=81lique?= Date: Sun, 27 May 2018 20:10:39 -0400 Subject: [PATCH 2/5] Fixed ParseObjectTest.testFailingSave() issue that was due to ParsePlugins already initialized. --- Parse/src/test/java/com/parse/ParseObjectTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/Parse/src/test/java/com/parse/ParseObjectTest.java b/Parse/src/test/java/com/parse/ParseObjectTest.java index a91dd7db1..05f3f0aea 100644 --- a/Parse/src/test/java/com/parse/ParseObjectTest.java +++ b/Parse/src/test/java/com/parse/ParseObjectTest.java @@ -66,6 +66,7 @@ public void setUp() { @After public void tearDown() { ParseCorePlugins.getInstance().reset(); + ParsePlugins.reset(); } @Test From 8b02505c24601955d5e94690797a4581d6683d72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Claude=20Joseph-Ange=CC=81lique?= Date: Sun, 27 May 2018 20:36:11 -0400 Subject: [PATCH 3/5] Fixed https://github.com/parse-community/Parse-SDK-Android/issues/825 --- Parse/src/main/java/com/parse/ParseObject.java | 3 +++ 1 file changed, 3 insertions(+) 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; } }); From ee96f1abd739ef6f4252641d8148f1f1297a3556 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Claude=20Joseph-Ange=CC=81lique?= Date: Sun, 27 May 2018 21:49:13 -0400 Subject: [PATCH 4/5] Fixed parseObjectTest.testFailingSave() issue due to other test class side effect. --- Parse/src/test/java/com/parse/ParseObjectTest.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Parse/src/test/java/com/parse/ParseObjectTest.java b/Parse/src/test/java/com/parse/ParseObjectTest.java index 05f3f0aea..5150554d4 100644 --- a/Parse/src/test/java/com/parse/ParseObjectTest.java +++ b/Parse/src/test/java/com/parse/ParseObjectTest.java @@ -807,6 +807,8 @@ 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); From 71cf2bbf7e9eb89ee58f2b4031429d38b89263e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Claude=20Joseph-Ange=CC=81lique?= Date: Tue, 5 Jun 2018 16:23:52 -0400 Subject: [PATCH 5/5] Fixed code style --- Parse/src/test/java/com/parse/NetworkObjectControllerTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Parse/src/test/java/com/parse/NetworkObjectControllerTest.java b/Parse/src/test/java/com/parse/NetworkObjectControllerTest.java index 5cafc9a8f..ec3fee473 100644 --- a/Parse/src/test/java/com/parse/NetworkObjectControllerTest.java +++ b/Parse/src/test/java/com/parse/NetworkObjectControllerTest.java @@ -43,7 +43,7 @@ public class NetworkObjectControllerTest { @Rule - public ExpectedException thrown= ExpectedException.none(); + public ExpectedException thrown = ExpectedException.none(); @Before public void setUp() throws MalformedURLException {