NetworkObjectController implements ParseObjectController { @Override public Task<ParseObject.State> saveAsync( final ParseObject.State state, final ParseOperationSet operations, String sessionToken, final ParseDecoder decoder) { JSONObject objectJSON = coder.encode(state, operations, PointerEncoder.get()); ParseRESTObjectCommand command = ParseRESTObjectCommand.saveObjectCommand( state, objectJSON, sessionToken); return command.executeAsync(client).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(); } }); } 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);  }
@Test public void testSaveAsync() throws Exception { JSONObject mockResponse = 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(); mockResponse.put("createdAt", createAtStr); mockResponse.put("objectId", "testObjectId"); mockResponse.put("updatedAt", updateAtStr); ParseHttpClient restClient = ParseTestUtils.mockParseHttpClientWithResponse(mockResponse, 200, "OK"); ParseObject object = new ParseObject("Test"); object.put("key", "value"); NetworkObjectController controller = new NetworkObjectController(restClient); ParseObject.State newState = ParseTaskUtils.wait(controller.saveAsync( object.getState(), object.startSave(), "sessionToken", ParseDecoder.get())); assertEquals(createAtLong, newState.createdAt()); assertEquals(updateAtLong, newState.updatedAt()); assertEquals("testObjectId", newState.objectId()); assertFalse(newState.isComplete()); }
@Test public void testFailingSaveAsync() throws Exception { JSONObject mockResponse = new JSONObject(); mockResponse.put("code", 141); mockResponse.put("error", "Save is not allowed"); ParseHttpClient restClient = ParseTestUtils.mockParseHttpClientWithResponse(mockResponse, 400, "Bad Request"); 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())); }