@Override public ParseFieldOperation decode(Parcel source, ParseParcelDecoder decoder) { return new ParseSetOperation(decoder.decode(source)); } });
@Override public ParseFieldOperation mergeWithPrevious(ParseFieldOperation previous) { if (previous == null) { return this; } else if (previous instanceof ParseDeleteOperation) { return new ParseSetOperation(objects); } else if (previous instanceof ParseSetOperation) { Object value = ((ParseSetOperation) previous).getValue(); if (value instanceof JSONArray) { ArrayList<Object> result = ParseFieldOperations.jsonArrayAsArrayList((JSONArray) value); result.addAll(objects); return new ParseSetOperation(new JSONArray(result)); } else if (value instanceof List) { ArrayList<Object> result = new ArrayList<>((List<?>) value); result.addAll(objects); return new ParseSetOperation(result); } else { throw new IllegalArgumentException("You can only add an item to a List or JSONArray."); } } else if (previous instanceof ParseAddOperation) { ArrayList<Object> result = new ArrayList<>(((ParseAddOperation) previous).objects); result.addAll(objects); return new ParseAddOperation(result); } else { throw new IllegalArgumentException("Operation is invalid after previous operation."); } }
@Override public ParseFieldOperation mergeWithPrevious(ParseFieldOperation previous) { if (previous == null) { return this; } else if (previous instanceof ParseDeleteOperation) { return new ParseSetOperation(objects); } else if (previous instanceof ParseSetOperation) { Object value = ((ParseSetOperation) previous).getValue(); if (value instanceof JSONArray || value instanceof List) { return new ParseSetOperation(this.apply(value, null)); } else { throw new IllegalArgumentException("You can only add an item to a List or JSONArray."); } } else if (previous instanceof ParseRemoveOperation) { HashSet<Object> result = new HashSet<>(((ParseRemoveOperation) previous).objects); result.addAll(objects); return new ParseRemoveOperation(result); } else { throw new IllegalArgumentException("Operation is invalid after previous operation."); } }
@Override @SuppressWarnings("unchecked") public ParseFieldOperation mergeWithPrevious(ParseFieldOperation previous) { if (previous == null) { return this; } else if (previous instanceof ParseDeleteOperation) { return new ParseSetOperation(objects); } else if (previous instanceof ParseSetOperation) { Object value = ((ParseSetOperation) previous).getValue(); if (value instanceof JSONArray || value instanceof List) { return new ParseSetOperation(this.apply(value, null)); } else { throw new IllegalArgumentException("You can only add an item to a List or JSONArray."); } } else if (previous instanceof ParseAddUniqueOperation) { List<Object> previousResult = new ArrayList<>(((ParseAddUniqueOperation) previous).objects); return new ParseAddUniqueOperation((List<Object>) this.apply(previousResult, null)); } else { throw new IllegalArgumentException("Operation is invalid after previous operation."); } }
@Override public ParseFieldOperation mergeWithPrevious(ParseFieldOperation previous) { if (previous == null) { return this; } else if (previous instanceof ParseDeleteOperation) { return new ParseSetOperation(amount); } else if (previous instanceof ParseSetOperation) { Object oldValue = ((ParseSetOperation) previous).getValue(); if (oldValue instanceof Number) { return new ParseSetOperation(Numbers.add((Number) oldValue, amount)); } else { throw new IllegalArgumentException("You cannot increment a non-number."); } } else if (previous instanceof ParseIncrementOperation) { Number oldAmount = ((ParseIncrementOperation) previous).amount; return new ParseIncrementOperation(Numbers.add(oldAmount, amount)); } else { throw new IllegalArgumentException("Operation is invalid after previous operation."); } }
void performPut(String key, Object value) { if (key == null) { throw new IllegalArgumentException("key may not be null."); } if (value == null) { throw new IllegalArgumentException("value may not be null."); } if (value instanceof JSONObject) { ParseDecoder decoder = ParseDecoder.get(); value = decoder.convertJSONObjectToMap((JSONObject) value); } else if (value instanceof JSONArray) { ParseDecoder decoder = ParseDecoder.get(); value = decoder.convertJSONArrayToList((JSONArray) value); } if (!ParseEncoder.isValidType(value)) { throw new IllegalArgumentException("invalid type for value: " + value.getClass().toString()); } performOperation(key, new ParseSetOperation(value)); }
fieldOp = (ParseFieldOperation) value; } else { fieldOp = new ParseSetOperation(value);
@Test public void testLoginAsyncWithUserStateNotCreated() throws Exception { // Make mock response and client JSONObject mockResponse = generateBasicMockResponse(); ParseHttpClient restClient = ParseTestUtils.mockParseHttpClientWithResponse(mockResponse, 200, "OK"); // Make test user state and operationSet ParseUser.State state = new ParseUser.State.Builder() .put("username", "testUserName") .put("password", "testPassword") .build(); ParseOperationSet operationSet = new ParseOperationSet(); operationSet.put("username", new ParseSetOperation("testUserName")); operationSet.put("password", new ParseSetOperation("testPassword")); NetworkUserController controller = new NetworkUserController(restClient, true); ParseUser.State newState = ParseTaskUtils.wait(controller.logInAsync(state, operationSet)); // Verify user state verifyBasicUserState(mockResponse, newState); assertFalse(newState.isNew()); assertTrue(newState.isComplete()); }
@Test public void testLoginAsyncWithUserStateCreated() throws Exception { // Make mock response and client JSONObject mockResponse = generateBasicMockResponse(); ParseHttpClient restClient = ParseTestUtils.mockParseHttpClientWithResponse(mockResponse, 201, "OK"); // Make test user state and operationSet ParseUser.State state = new ParseUser.State.Builder() .put("username", "testUserName") .put("password", "testPassword") .build(); ParseOperationSet operationSet = new ParseOperationSet(); operationSet.put("username", new ParseSetOperation("testUserName")); operationSet.put("password", new ParseSetOperation("testPassword")); NetworkUserController controller = new NetworkUserController(restClient, true); ParseUser.State newState = ParseTaskUtils.wait(controller.logInAsync(state, operationSet)); // Verify user state verifyBasicUserState(mockResponse, newState); assertTrue(newState.isNew()); assertFalse(newState.isComplete()); }
@Test public void testSignUpAsync() throws Exception { // Make mock response and client JSONObject mockResponse = generateBasicMockResponse(); ParseHttpClient restClient = ParseTestUtils.mockParseHttpClientWithResponse(generateBasicMockResponse(), 200, "OK"); // Make test user state and operationSet ParseUser.State state = new ParseUser.State.Builder() .put("username", "testUserName") .put("password", "testPassword") .build(); ParseOperationSet operationSet = new ParseOperationSet(); operationSet.put("username", new ParseSetOperation("testUserName")); operationSet.put("password", new ParseSetOperation("testPassword")); NetworkUserController controller = new NetworkUserController(restClient, true); ParseUser.State newState = ParseTaskUtils.wait( controller.signUpAsync(state, operationSet, "sessionToken")); verifyBasicUserState(mockResponse, newState); assertFalse(newState.isComplete()); assertTrue(newState.isNew()); }