/** * Regenerates the estimatedData map from the serverData and operations. */ private void rebuildEstimatedData() { synchronized (mutex) { estimatedData.clear(); for (String key : state.keySet()) { estimatedData.put(key, state.get(key)); } for (ParseOperationSet operations : operationSetQueue) { applyOperations(operations, estimatedData); } } }
ParseDateFormat.getInstance().format(new Date(state.updatedAt()))); for (String key : state.keySet()) { Object value = state.get(key); json.put(key, objectEncoder.encode(value));
Init(State state) { className = state.className(); objectId = state.objectId(); createdAt = state.createdAt(); updatedAt = state.updatedAt(); availableKeys = Collections.synchronizedSet(state.availableKeys()); for (String key : state.keySet()) { serverData.put(key, state.get(key)); availableKeys.add(key); } isComplete = state.isComplete(); }
when(state.className()).thenReturn("TestObject"); when(state.objectId()).thenReturn("test_id"); when(state.keySet()).thenReturn(Collections.singleton("foo")); when(state.get("foo")).thenReturn("bar"); object = ParseObject.from(state);
for (String key : state.keySet()) { Object object = state.get(key); dataJSON.put(key, encoder.encode(object));
when(state.className()).thenReturn("TestObject"); when(state.objectId()).thenReturn("test_id"); when(state.keySet()).thenReturn(Collections.singleton("foo")); when(state.get("foo")).thenReturn("bar"); object = ParseObject.from(state);
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);
@Test public void testParcelable() { long updatedAt = System.currentTimeMillis(); long createdAt = updatedAt + 10; ParseObject.State state = new ParseObject.State.Builder("TestObject") .objectId("fake") .createdAt(new Date(createdAt)) .updatedAt(new Date(updatedAt)) .isComplete(true) .put("foo", "bar") .put("baz", "qux") .availableKeys(Arrays.asList("safe", "keys")) .build(); Parcel parcel = Parcel.obtain(); state.writeToParcel(parcel, ParseParcelEncoder.get()); parcel.setDataPosition(0); ParseObject.State copy = ParseObject.State.createFromParcel(parcel, ParseParcelDecoder.get()); assertEquals(state.className(), copy.className()); assertEquals(state.objectId(), copy.objectId()); assertEquals(state.createdAt(), copy.createdAt()); assertEquals(state.updatedAt(), copy.updatedAt()); assertEquals(state.isComplete(), copy.isComplete()); assertEquals(state.keySet().size(), copy.keySet().size()); assertEquals(state.get("foo"), copy.get("foo")); assertEquals(state.get("baz"), copy.get("baz")); assertEquals(state.availableKeys().size(), copy.availableKeys().size()); assertTrue(state.availableKeys().containsAll(copy.availableKeys())); assertTrue(copy.availableKeys().containsAll(state.availableKeys())); }
@Test public void testCopy() { long updatedAt = System.currentTimeMillis(); long createdAt = updatedAt + 10; ParseObject.State state = new ParseObject.State.Builder("TestObject") .objectId("fake") .createdAt(new Date(createdAt)) .updatedAt(new Date(updatedAt)) .isComplete(true) .put("foo", "bar") .put("baz", "qux") .availableKeys(Arrays.asList("safe", "keys")) .build(); ParseObject.State copy = new ParseObject.State.Builder(state).build(); assertEquals(state.className(), copy.className()); assertEquals(state.objectId(), copy.objectId()); assertEquals(state.createdAt(), copy.createdAt()); assertEquals(state.updatedAt(), copy.updatedAt()); assertEquals(state.isComplete(), copy.isComplete()); assertEquals(state.keySet().size(), copy.keySet().size()); assertEquals(state.get("foo"), copy.get("foo")); assertEquals(state.get("baz"), copy.get("baz")); assertEquals(state.availableKeys().size(), copy.availableKeys().size()); assertTrue(state.availableKeys().containsAll(copy.availableKeys())); assertTrue(copy.availableKeys().containsAll(state.availableKeys())); }
@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(); }
@Test public void testFromJSONPayload() throws JSONException { JSONObject json = new JSONObject( "{" + "\"className\":\"GameScore\"," + "\"createdAt\":\"2015-06-22T21:23:41.733Z\"," + "\"objectId\":\"TT1ZskATqS\"," + "\"updatedAt\":\"2015-06-22T22:06:18.104Z\"," + "\"score\":{" + "\"__op\":\"Increment\"," + "\"amount\":1" + "}," + "\"age\":33" + "}"); ParseObject parseObject = ParseObject.fromJSONPayload(json, ParseDecoder.get()); assertEquals("GameScore", parseObject.getClassName()); assertEquals("TT1ZskATqS", parseObject.getObjectId()); ParseDateFormat format = ParseDateFormat.getInstance(); assertTrue(parseObject.getCreatedAt().equals(format.parse("2015-06-22T21:23:41.733Z"))); assertTrue(parseObject.getUpdatedAt().equals(format.parse("2015-06-22T22:06:18.104Z"))); Set<String> keys = parseObject.getState().keySet(); assertEquals(0, keys.size()); ParseOperationSet currentOperations = parseObject.operationSetQueue.getLast(); assertEquals(2, currentOperations.size()); }
/** * Applies a {@code State} on top of this {@code Builder} instance. * * @param other The {@code State} to apply over this instance. * @return A new {@code Builder} instance. */ public T apply(State other) { if (other.objectId() != null) { objectId(other.objectId()); } if (other.createdAt() > 0) { createdAt(other.createdAt()); } if (other.updatedAt() > 0) { updatedAt(other.updatedAt()); } isComplete(isComplete || other.isComplete()); for (String key : other.keySet()) { put(key, other.get(key)); } availableKeys(other.availableKeys()); return self(); }
@Test public void testServerData() { ParseObject.State.Builder builder = new ParseObject.State.Builder("TestObject"); ParseObject.State state = builder.build(); assertTrue(state.keySet().isEmpty()); builder.put("foo", "bar") .put("baz", "qux"); state = builder.build(); assertEquals(2, state.keySet().size()); assertEquals("bar", state.get("foo")); assertEquals("qux", state.get("baz")); builder.remove("foo"); state = builder.build(); assertEquals(1, state.keySet().size()); assertNull(state.get("foo")); assertEquals("qux", state.get("baz")); builder.clear(); state = builder.build(); assertTrue(state.keySet().isEmpty()); assertNull(state.get("foo")); assertNull(state.get("baz")); }
void markAllFieldsDirty() { synchronized (mutex) { for (String key : state.keySet()) { performPut(key, state.get(key)); } } }
@Test public void testDefaults() { ParseObject.State state = new ParseObject.State.Builder("TestObject").build(); assertEquals("TestObject", state.className()); assertNull(state.objectId()); assertEquals(-1, state.createdAt()); assertEquals(-1, state.updatedAt()); assertFalse(state.isComplete()); assertTrue(state.keySet().isEmpty()); assertTrue(state.availableKeys().isEmpty()); }