@Test public void testSaveAllAsync() throws Exception { JSONObject objectSaveResult = new JSONObject(); String createAtStr = "2015-08-09T22:15:13.460Z"; long createAtLong = ParseDateFormat.getInstance().parse(createAtStr).getTime(); String updateAtStr = "2015-08-09T22:15:13.497Z"; long updateAtLong = ParseDateFormat.getInstance().parse(updateAtStr).getTime(); objectSaveResult.put("createdAt", createAtStr); objectSaveResult.put("objectId", "testObjectId"); objectSaveResult.put("updatedAt", updateAtStr); JSONObject objectResponse = new JSONObject(); objectResponse.put("success", objectSaveResult); JSONObject objectResponseAgain = new JSONObject(); JSONObject objectSaveResultAgain = new JSONObject(); objectSaveResultAgain.put("code", 101); objectSaveResultAgain.put("error", "Error"); objectResponseAgain.put("error", objectSaveResultAgain); JSONArray mockResponse = new JSONArray(); mockResponse.put(objectResponse); mockResponse.put(objectResponseAgain); byte[] contentBytes = mockResponse.toString().getBytes(); ParseHttpResponse response = new ParseHttpResponse.Builder() .setContent(new ByteArrayInputStream(contentBytes)) .setStatusCode(200) .setTotalSize(contentBytes.length) .setContentType("application/json") .build(); ParseHttpClient client = mock(ParseHttpClient.class); when(client.execute(any(ParseHttpRequest.class))).thenReturn(response); List<ParseObject.State> states = new ArrayList<>(); List<ParseOperationSet> operationsList = new ArrayList<>(); List<ParseDecoder> decoders = new ArrayList<>(); ParseObject object = new ParseObject("Test"); object.put("key", "value"); states.add(object.getState()); operationsList.add(object.startSave()); decoders.add(ParseDecoder.get()); ParseObject objectAgain = new ParseObject("Test"); object.put("keyAgain", "valueAgain"); states.add(objectAgain.getState()); operationsList.add(objectAgain.startSave()); decoders.add(ParseDecoder.get()); NetworkObjectController controller = new NetworkObjectController(client); List<Task<ParseObject.State>> saveTaskList = controller.saveAllAsync(states, operationsList, "sessionToken", decoders); Task.whenAll(saveTaskList).waitForCompletion(); ParseObject.State newState = saveTaskList.get(0).getResult(); assertEquals(createAtLong, newState.createdAt()); assertEquals(updateAtLong, newState.updatedAt()); assertEquals("testObjectId", newState.objectId()); assertFalse(newState.isComplete()); assertTrue(saveTaskList.get(1).isFaulted()); assertTrue(saveTaskList.get(1).getError() instanceof ParseException); ParseException parseException = (ParseException) saveTaskList.get(1).getError(); assertEquals(101, parseException.getCode()); assertEquals("Error", parseException.getMessage()); }
@Override public List<Task<ParseObject.State>> saveAllAsync( List<ParseObject.State> states, List<ParseOperationSet> operationsList, String sessionToken, List<ParseDecoder> decoders) { int batchSize = states.size(); List<ParseRESTObjectCommand> commands = new ArrayList<>(batchSize); ParseEncoder encoder = PointerEncoder.get(); for (int i = 0; i < batchSize; i++) { ParseObject.State state = states.get(i); ParseOperationSet operations = operationsList.get(i); JSONObject objectJSON = coder.encode(state, operations, encoder); ParseRESTObjectCommand command = ParseRESTObjectCommand.saveObjectCommand( state, objectJSON, sessionToken); commands.add(command); } final List<Task<JSONObject>> batchTasks = ParseRESTObjectBatchCommand.executeBatch(client, commands, sessionToken); final List<Task<ParseObject.State>> tasks = new ArrayList<>(batchSize); for (int i = 0; i < batchSize; i++) { final ParseObject.State state = states.get(i); final ParseDecoder decoder = decoders.get(i); tasks.add(batchTasks.get(i).onSuccess(new Continuation<JSONObject, ParseObject.State>() { @Override public ParseObject.State then(Task<JSONObject> task) { JSONObject result = task.getResult(); ParseObject.State.Init<?> builder = state.newBuilder().clear(); return coder.decode(builder, result, decoder) .isComplete(false) .build(); } })); } return tasks; }
NetworkObjectController implements ParseObjectController { @Override public List<Task<ParseObject.State>> saveAllAsync( List<ParseObject.State> states, List<ParseOperationSet> operationsList, String sessionToken, List<ParseDecoder> decoders) { int batchSize = states.size(); List<ParseRESTObjectCommand> commands = new ArrayList<>(batchSize); ParseEncoder encoder = PointerEncoder.get(); for (int i = 0; i < batchSize; i++) { ParseObject.State state = states.get(i); ParseOperationSet operations = operationsList.get(i); JSONObject objectJSON = coder.encode(state, operations, encoder); ParseRESTObjectCommand command = ParseRESTObjectCommand.saveObjectCommand( state, objectJSON, sessionToken); commands.add(command); } final List<Task<JSONObject>> batchTasks = ParseRESTObjectBatchCommand.executeBatch(client, commands, sessionToken); final List<Task<ParseObject.State>> tasks = new ArrayList<>(batchSize); for (int i = 0; i < batchSize; i++) { final ParseObject.State state = states.get(i); final ParseDecoder decoder = decoders.get(i); tasks.add(batchTasks.get(i).onSuccess(new Continuation<JSONObject, ParseObject.State>() { @Override public ParseObject.State then(Task<JSONObject> task) { JSONObject result = task.getResult(); ParseObject.State.Init<?> builder = state.newBuilder().clear(); return coder.decode(builder, result, decoder) .isComplete(false) .build(); } })); } return tasks; } }
NetworkObjectController implements ParseObjectController { @Override public List<Task<ParseObject.State>> saveAllAsync( List<ParseObject.State> states, List<ParseOperationSet> operationsList, String sessionToken, List<ParseDecoder> decoders) { int batchSize = states.size(); List<ParseRESTObjectCommand> commands = new ArrayList<>(batchSize); ParseEncoder encoder = PointerEncoder.get(); for (int i = 0; i < batchSize; i++) { ParseObject.State state = states.get(i); ParseOperationSet operations = operationsList.get(i); JSONObject objectJSON = coder.encode(state, operations, encoder); ParseRESTObjectCommand command = ParseRESTObjectCommand.saveObjectCommand( state, objectJSON, sessionToken); commands.add(command); } final List<Task<JSONObject>> batchTasks = ParseRESTObjectBatchCommand.executeBatch(client, commands, sessionToken); final List<Task<ParseObject.State>> tasks = new ArrayList<>(batchSize); for (int i = 0; i < batchSize; i++) { final ParseObject.State state = states.get(i); final ParseDecoder decoder = decoders.get(i); tasks.add(batchTasks.get(i).onSuccess(new Continuation<JSONObject, ParseObject.State>() { @Override public ParseObject.State then(Task<JSONObject> task) { JSONObject result = task.getResult(); ParseObject.State.Init<?> builder = state.newBuilder().clear(); return coder.decode(builder, result, decoder) .isComplete(false) .build(); } })); } return tasks; } NetworkObjectController(ParseHttpClient client); }
NetworkObjectController implements ParseObjectController { @Override public List<Task<ParseObject.State>> saveAllAsync( List<ParseObject.State> states, List<ParseOperationSet> operationsList, String sessionToken, List<ParseDecoder> decoders) { int batchSize = states.size(); List<ParseRESTObjectCommand> commands = new ArrayList<>(batchSize); ParseEncoder encoder = PointerEncoder.get(); for (int i = 0; i < batchSize; i++) { ParseObject.State state = states.get(i); ParseOperationSet operations = operationsList.get(i); JSONObject objectJSON = coder.encode(state, operations, encoder); ParseRESTObjectCommand command = ParseRESTObjectCommand.saveObjectCommand( state, objectJSON, sessionToken); commands.add(command); } final List<Task<JSONObject>> batchTasks = ParseRESTObjectBatchCommand.executeBatch(client, commands, sessionToken); final List<Task<ParseObject.State>> tasks = new ArrayList<>(batchSize); for (int i = 0; i < batchSize; i++) { final ParseObject.State state = states.get(i); final ParseDecoder decoder = decoders.get(i); tasks.add(batchTasks.get(i).onSuccess(new Continuation<JSONObject, ParseObject.State>() { @Override public ParseObject.State then(Task<JSONObject> task) { JSONObject result = task.getResult(); ParseObject.State.Init<?> builder = state.newBuilder().clear(); return coder.decode(builder, result, decoder) .isComplete(false) .build(); } })); } return tasks; } NetworkObjectController(ParseHttpClient client); @Override Task<ParseObject.State> fetchAsync(
            final ParseObject.State state, String sessionToken, final ParseDecoder decoder); @Override Task<ParseObject.State> saveAsync(
            final ParseObject.State state,
            final ParseOperationSet operations,
            String sessionToken,
            final ParseDecoder decoder); @Override List<Task<ParseObject.State>> saveAllAsync(
            List<ParseObject.State> states,
            List<ParseOperationSet> operationsList,
            String sessionToken,
            List<ParseDecoder> decoders); @Override Task<Void> deleteAsync(ParseObject.State state, String sessionToken); @Override List<Task<Void>> deleteAllAsync(
            List<ParseObject.State> states, String sessionToken); }
NetworkObjectController implements ParseObjectController { @Override public List<Task<ParseObject.State>> saveAllAsync( List<ParseObject.State> states, List<ParseOperationSet> operationsList, String sessionToken, List<ParseDecoder> decoders) { int batchSize = states.size(); List<ParseRESTObjectCommand> commands = new ArrayList<>(batchSize); ParseEncoder encoder = PointerEncoder.get(); for (int i = 0; i < batchSize; i++) { ParseObject.State state = states.get(i); ParseOperationSet operations = operationsList.get(i); JSONObject objectJSON = coder.encode(state, operations, encoder); ParseRESTObjectCommand command = ParseRESTObjectCommand.saveObjectCommand( state, objectJSON, sessionToken); commands.add(command); } final List<Task<JSONObject>> batchTasks = ParseRESTObjectBatchCommand.executeBatch(client, commands, sessionToken); final List<Task<ParseObject.State>> tasks = new ArrayList<>(batchSize); for (int i = 0; i < batchSize; i++) { final ParseObject.State state = states.get(i); final ParseDecoder decoder = decoders.get(i); tasks.add(batchTasks.get(i).onSuccess(new Continuation<JSONObject, ParseObject.State>() { @Override public ParseObject.State then(Task<JSONObject> task) { JSONObject result = task.getResult(); ParseObject.State.Init<?> builder = state.newBuilder().clear(); return coder.decode(builder, result, decoder) .isComplete(false) .build(); } })); } return tasks; } NetworkObjectController(ParseHttpClient client); @Override Task<ParseObject.State> fetchAsync(
            final ParseObject.State state, String sessionToken, final ParseDecoder decoder); @Override Task<ParseObject.State> saveAsync(
            final ParseObject.State state,
            final ParseOperationSet operations,
            String sessionToken,
            final ParseDecoder decoder); @Override List<Task<ParseObject.State>> saveAllAsync(
            List<ParseObject.State> states,
            List<ParseOperationSet> operationsList,
            String sessionToken,
            List<ParseDecoder> decoders); @Override Task<Void> deleteAsync(ParseObject.State state, String sessionToken); @Override List<Task<Void>> deleteAllAsync(
            List<ParseObject.State> states, String sessionToken);  }