@Override public boolean matchesSafely(Throwable item) { return item instanceof ParseException && matcher.matches(((ParseException) item).getCode()); }
@SuppressWarnings("ThrowableResultOfMethodCallIgnored") @Override public Task<TResult> then(Task<TResult> task) { Exception error = task.getError(); if (error instanceof ParseException && ((ParseException) error).getCode() == ParseException.CONNECTION_FAILED) { return c.runFromCacheAsync(); } // Either the query succeeded, or there was an an error with the query, not the // network return task; } });
@Override public void done(ParseConfig config, ParseException e) { assertEquals(ParseException.CONNECTION_FAILED, e.getCode()); assertEquals("error", e.getMessage()); done.release(); } });
@Test public void testCallFunctionInBackgroundFailure() throws Exception { // TODO(mengyan): Remove once we no longer rely on retry logic. ParseRequest.setDefaultInitialRetryDelay(1L); ParseHttpClient restClient = mock(ParseHttpClient.class); when(restClient.execute(any(ParseHttpRequest.class))).thenThrow(new IOException()); ParseCloudCodeController controller = new ParseCloudCodeController(restClient); Task<String> cloudCodeTask = controller.callFunctionInBackground("test", new HashMap<String, Object>(), "sessionToken"); // Do not use ParseTaskUtils.wait() since we do not want to throw the exception cloudCodeTask.waitForCompletion(); // TODO(mengyan): Abstract out command runner so we don't have to account for retries. verify(restClient, times(5)).execute(any(ParseHttpRequest.class)); assertTrue(cloudCodeTask.isFaulted()); Exception error = cloudCodeTask.getError(); assertThat(error, instanceOf(ParseException.class)); assertEquals(ParseException.CONNECTION_FAILED, ((ParseException) error).getCode()); }
@Override public Task<JSONObject> then(Task<JSONObject> task) { String localId = command.getLocalId(); Exception error = task.getError(); if (error != null) { if (error instanceof ParseException && ((ParseException) error).getCode() == ParseException.CONNECTION_FAILED) { // do nothing } else { if (tcs != null) { tcs.setError(error); } } return task; } JSONObject json = task.getResult(); if (tcs != null) { tcs.setResult(json); } else if (localId != null) { // If this command created a new objectId, add it to the map. String objectId = json.optString("objectId", null); if (objectId != null) { ParseCorePlugins.getInstance() .getLocalIdManager().setObjectId(localId, objectId); } } return task; } });
@Test public void testGetAsyncFailureWithConnectionFailure() throws Exception { // TODO(mengyan): Remove once we no longer rely on retry logic. ParseRequest.setDefaultInitialRetryDelay(1L); // Make ParseConfigController and call getAsync ParseHttpClient restClient = mock(ParseHttpClient.class); when(restClient.execute(any(ParseHttpRequest.class))).thenThrow(new IOException()); ParseCurrentConfigController currentConfigController = mockParseCurrentConfigController(); ParseConfigController configController = new ParseConfigController(restClient, currentConfigController); Task<ParseConfig> configTask = configController.getAsync(null); // Do not use ParseTaskUtils.wait() since we do not want to throw the exception configTask.waitForCompletion(); // Verify httpClient is tried enough times // TODO(mengyan): Abstract out command runner so we don't have to account for retries. verify(restClient, times(5)).execute(any(ParseHttpRequest.class)); assertTrue(configTask.isFaulted()); Exception error = configTask.getError(); assertThat(error, instanceOf(ParseException.class)); assertEquals(ParseException.CONNECTION_FAILED, ((ParseException) error).getCode()); // Verify currentConfigController is not called verify(currentConfigController, times(0)).setCurrentConfigAsync(any(ParseConfig.class)); }
@Test public void testPermanentFailures() throws Exception { JSONObject json = new JSONObject(); json.put("code", 1337); json.put("error", "mock error"); ParseHttpResponse response = newMockParseHttpResponse(400, json); ParseHttpClient client = mock(ParseHttpClient.class); when(client.execute(any(ParseHttpRequest.class))).thenReturn(response); ParseRESTCommand command = new ParseRESTCommand.Builder() .method(ParseHttpRequest.Method.GET) .installationId("fake_installation_id") .build(); Task<JSONObject> task = command.executeAsync(client); task.waitForCompletion(); verify(client, times(1)).execute(any(ParseHttpRequest.class)); assertTrue(task.isFaulted()); assertEquals(1337, ((ParseException) task.getError()).getCode()); assertEquals("mock error", task.getError().getMessage()); }
@Test public void testSendInBackgroundFailWithIOException() throws Exception { // TODO(mengyan): Remove once we no longer rely on retry logic. ParseRequest.setDefaultInitialRetryDelay(1L); ParseHttpClient restClient = mock(ParseHttpClient.class); when(restClient.execute(any(ParseHttpRequest.class))).thenThrow(new IOException()); ParsePushController controller = new ParsePushController(restClient); JSONObject data = new JSONObject(); data.put(ParsePush.KEY_DATA_MESSAGE, "hello world"); ParsePush.State state = new ParsePush.State.Builder() .data(data) .build(); Task<Void> pushTask = controller.sendInBackground(state, "sessionToken"); // Do not use ParseTaskUtils.wait() since we do not want to throw the exception pushTask.waitForCompletion(); // Verify httpClient is tried enough times // TODO(mengyan): Abstract out command runner so we don't have to account for retries. verify(restClient, times(5)).execute(any(ParseHttpRequest.class)); assertTrue(pushTask.isFaulted()); Exception error = pushTask.getError(); assertThat(error, instanceOf(ParseException.class)); assertEquals(ParseException.CONNECTION_FAILED, ((ParseException) error).getCode()); }
@Test public void testSaveAsyncFailureWithByteArray() throws Exception { // TODO(grantland): Remove once we no longer rely on retry logic. ParseRequest.setDefaultInitialRetryDelay(1L); ParseHttpClient restClient = mock(ParseHttpClient.class); when(restClient.execute(any(ParseHttpRequest.class))).thenThrow(new IOException()); File root = temporaryFolder.getRoot(); ParseFileController controller = new ParseFileController(restClient, root); byte[] data = "hello".getBytes(); ParseFile.State state = new ParseFile.State.Builder() .build(); Task<ParseFile.State> task = controller.saveAsync(state, data, null, null, null); task.waitForCompletion(); // TODO(grantland): Abstract out command runner so we don't have to account for retries. verify(restClient, times(5)).execute(any(ParseHttpRequest.class)); assertTrue(task.isFaulted()); Exception error = task.getError(); assertThat(error, instanceOf(ParseException.class)); assertEquals(ParseException.CONNECTION_FAILED, ((ParseException) error).getCode()); assertEquals(0, root.listFiles().length); }
/** * Test to verify that handle 401 unauthorized */ @Test public void test401Unauthorized() throws Exception { JSONObject json = new JSONObject(); json.put("error", "unauthorized"); ParseHttpResponse response = newMockParseHttpResponse(401, json); ParseHttpClient client = mock(ParseHttpClient.class); when(client.execute(any(ParseHttpRequest.class))).thenReturn(response); ParseRESTCommand command = new ParseRESTCommand.Builder() .method(ParseHttpRequest.Method.GET) .installationId("fake_installation_id") .build(); Task<JSONObject> task = command.executeAsync(client); task.waitForCompletion(); verify(client, times(1)).execute(any(ParseHttpRequest.class)); assertTrue(task.isFaulted()); assertEquals(0, ((ParseException) task.getError()).getCode()); assertEquals("unauthorized", task.getError().getMessage()); }
@Test public void testSaveAsyncFailureWithFile() throws Exception { // TODO(grantland): Remove once we no longer rely on retry logic. ParseRequest.setDefaultInitialRetryDelay(1L); ParseHttpClient restClient = mock(ParseHttpClient.class); when(restClient.execute(any(ParseHttpRequest.class))).thenThrow(new IOException()); File root = temporaryFolder.getRoot(); ParseFileController controller = new ParseFileController(restClient, root); File file = temporaryFolder.newFile("test"); ParseFile.State state = new ParseFile.State.Builder() .build(); Task<ParseFile.State> task = controller.saveAsync(state, file, null, null, null); task.waitForCompletion(); // TODO(grantland): Abstract out command runner so we don't have to account for retries. verify(restClient, times(5)).execute(any(ParseHttpRequest.class)); assertTrue(task.isFaulted()); Exception error = task.getError(); assertThat(error, instanceOf(ParseException.class)); assertEquals(ParseException.CONNECTION_FAILED, ((ParseException) error).getCode()); // Make sure the original file is not deleted and there is no cache file in the folder assertEquals(1, root.listFiles().length); assertTrue(file.exists()); }
&& ((ParseException) task.getError()).getCode() == ParseException.CACHE_MISS) { return Task.forResult(null);
@Test public void testGetSyncFail() { ParseException exception = new ParseException(ParseException.CONNECTION_FAILED, "error"); ParseConfigController controller = mockParseConfigControllerWithException(exception); ParseCorePlugins.getInstance().registerConfigController(controller); try { ParseConfig.get(); fail("Should throw an exception"); } catch (ParseException e) { verify(controller, times(1)).getAsync(anyString()); assertEquals(ParseException.CONNECTION_FAILED, e.getCode()); assertEquals("error", e.getMessage()); } }
@Test public void testFetchAsyncFailure() throws Exception { // TODO(grantland): Remove once we no longer rely on retry logic. ParseRequest.setDefaultInitialRetryDelay(1L); ParseHttpClient fileClient = mock(ParseHttpClient.class); when(fileClient.execute(any(ParseHttpRequest.class))).thenThrow(new IOException()); File root = temporaryFolder.getRoot(); ParseFileController controller = new ParseFileController(null, root).fileClient(fileClient); // We need to set url to make getTempFile() work and check it ParseFile.State state = new ParseFile.State.Builder() .url("test") .build(); Task<File> task = controller.fetchAsync(state, null, null, null); task.waitForCompletion(); // TODO(grantland): Abstract out command runner so we don't have to account for retries. verify(fileClient, times(5)).execute(any(ParseHttpRequest.class)); assertTrue(task.isFaulted()); Exception error = task.getError(); assertThat(error, instanceOf(ParseException.class)); assertEquals(ParseException.CONNECTION_FAILED, ((ParseException) error).getCode()); assertEquals(0, root.listFiles().length); assertFalse(controller.getTempFile(state).exists()); }
@Override public Task<Void> then(Task<Void> task) { // Retry the fetch as a save operation because this Installation was deleted on the server. if (task.getError() != null && task.getError() instanceof ParseException) { int errCode = ((ParseException) task.getError()).getCode(); if (errCode == ParseException.OBJECT_NOT_FOUND || (errCode == ParseException.MISSING_REQUIRED_FIELD_ERROR && getObjectId() == null)) { synchronized (mutex) { setState(new State.Builder(getState()).objectId(null).build()); markAllFieldsDirty(); return ParseInstallation.super.saveAsync(sessionToken, toAwait); } } } return task; } });
assertTrue(deleteTaskList.get(1).getError() instanceof ParseException); ParseException parseException = (ParseException) deleteTaskList.get(1).getError(); assertEquals(101, parseException.getCode()); assertEquals("Error", parseException.getMessage());
@Test public void testTemporaryFailures() throws Exception { JSONObject json = new JSONObject(); json.put("code", 1337); json.put("error", "mock error"); ParseHttpResponse response1 = newMockParseHttpResponse(500, json); ParseHttpResponse response2 = newMockParseHttpResponse(500, json); ParseHttpResponse response3 = newMockParseHttpResponse(500, json); ParseHttpResponse response4 = newMockParseHttpResponse(500, json); ParseHttpResponse response5 = newMockParseHttpResponse(500, json); ParseHttpClient client = mock(ParseHttpClient.class); when(client.execute(any(ParseHttpRequest.class))).thenReturn( response1, response2, response3, response4, response5 ); ParseRESTCommand command = new ParseRESTCommand.Builder() .method(ParseHttpRequest.Method.GET) .installationId("fake_installation_id") .build(); Task<JSONObject> task = command.executeAsync(client); task.waitForCompletion(); verify(client, times(5)).execute(any(ParseHttpRequest.class)); assertTrue(task.isFaulted()); assertEquals(1337, ((ParseException) task.getError()).getCode()); assertEquals("mock error", task.getError().getMessage()); }
@SuppressWarnings("ThrowableResultOfMethodCallIgnored") @Test public void testGetInBackgroundFail() throws Exception { ParseException exception = new ParseException(ParseException.CONNECTION_FAILED, "error"); ParseConfigController controller = mockParseConfigControllerWithException(exception); ParseCorePlugins.getInstance().registerConfigController(controller); Task<ParseConfig> configTask = ParseConfig.getInBackground(); configTask.waitForCompletion(); verify(controller, times(1)).getAsync(anyString()); assertThat(configTask.getError(), instanceOf(ParseException.class)); assertEquals(ParseException.CONNECTION_FAILED, ((ParseException) configTask.getError()).getCode()); assertEquals("error", configTask.getError().getMessage()); }
assertTrue(saveTaskList.get(1).getError() instanceof ParseException); ParseException parseException = (ParseException) saveTaskList.get(1).getError(); assertEquals(101, parseException.getCode()); assertEquals("Error", parseException.getMessage());
@SuppressWarnings("ThrowableResultOfMethodCallIgnored") public void test4XXThrowsException() throws Exception { ParseRequest.setDefaultInitialRetryDelay(1L); InputStream mockInputStream = new ByteArrayInputStream( "An Error occurred while saving".getBytes()); ParseHttpResponse mockResponse = new ParseHttpResponse.Builder() .setStatusCode(400) .setTotalSize(0L) .setReasonPhrase("Bad Request") .setContent(mockInputStream) .build(); ParseHttpClient mockHttpClient = mock(ParseHttpClient.class); when(mockHttpClient.execute(any(ParseHttpRequest.class))).thenReturn(mockResponse); ParseFileRequest request = new ParseFileRequest(ParseHttpRequest.Method.GET, "http://parse.com", null); Task<Void> task = request.executeAsync(mockHttpClient); task.waitForCompletion(); assertTrue(task.isFaulted()); assertTrue(task.getError() instanceof ParseException); ParseException error = (ParseException) task.getError(); assertEquals(error.getCode(), ParseException.CONNECTION_FAILED); assertTrue(error.getMessage().contains("Download from file server")); } }