@Test public void testConstructorWithJSONAndDecoder() throws Exception { // Make ParseRelation JSONArray ParseObject object = mock(ParseObject.class); when(object.getClassName()).thenReturn("Test"); when(object.getObjectId()).thenReturn("objectId"); object.setObjectId("objectId"); JSONArray objectJSONArray = new JSONArray(); objectJSONArray.put(PointerEncoder.get().encode(object)); JSONObject relationJSON = new JSONObject(); relationJSON.put("className", "Test"); relationJSON.put("objects", objectJSONArray); ParseRelation relationFromJSON = new ParseRelation(relationJSON, ParseDecoder.get()); assertEquals("Test", relationFromJSON.getTargetClass()); assertEquals(1, relationFromJSON.getKnownObjects().size()); Object[] objects = relationFromJSON.getKnownObjects().toArray(); assertEquals("objectId", ((ParseObject) objects[0]).getObjectId()); }
@Test public void testEncodeRelatedObjectWithObjectId() { ParseObject parseObject = new ParseObject("TestObject"); parseObject.setObjectId("1234"); JSONObject jsonObject = (JSONObject) PointerEncoder.get().encode(parseObject); assertNotNull(jsonObject); } }
@Test public void testEncodeToJSON() throws Exception { ParseObject parent = new ParseObject("Parent"); ParseRelation relation = new ParseRelation(parent, "key"); relation.setTargetClass("Test"); ParseObject object = new ParseObject("Test"); object.setObjectId("objectId"); relation.addKnownObject(object); JSONObject json = relation.encodeToJSON(PointerEncoder.get()); assertEquals("Relation", json.getString("__type")); assertEquals("Test", json.getString("className")); JSONArray knownObjectsArray = json.getJSONArray("objects"); assertEquals( (JSONObject) PointerEncoder.get().encode(object), knownObjectsArray.getJSONObject(0), JSONCompareMode.NON_EXTENSIBLE); }
@Test public void testObjectSerializationFormat() throws Exception { ParseObject childObject = new ParseObject("child"); childObject.setObjectId("childObjectId");
@Test public void testParcelWhileDeletingWithLDSEnabled() throws Exception { mockCurrentUserController(); TaskCompletionSource<Void> tcs = mockObjectControllerForDelete(); ParseObject object = new ParseObject("TestObject"); object.setObjectId("id"); OfflineStore lds = mock(OfflineStore.class); when(lds.getObject("TestObject", "id")).thenReturn(object); Parse.setLocalDatastore(lds); Task<Void> deleteTask = object.deleteInBackground(); assertTrue(object.isDeleting); Parcel parcel = Parcel.obtain(); object.writeToParcel(parcel, 0); parcel.setDataPosition(0); ParseObject other = ParseObject.CREATOR.createFromParcel(parcel); assertSame(object, other); assertTrue(other.isDeleting); // Still deleting tcs.setResult(null); deleteTask.waitForCompletion(); // complete deletion on original object. assertFalse(other.isDeleting); assertTrue(other.isDeleted); Parse.setLocalDatastore(null); }
@Test public void testParcelable() { ParseFieldOperations.registerDefaultDecoders(); ParseRelation<ParseObject> relation = new ParseRelation<>("Test"); ParseObject parent = new ParseObject("Parent"); parent.setObjectId("parentId"); relation.ensureParentAndKey(parent, "key"); ParseObject inner = new ParseObject("Test"); inner.setObjectId("innerId"); relation.add(inner); Parcel parcel = Parcel.obtain(); relation.writeToParcel(parcel, 0); parcel.setDataPosition(0); //noinspection unchecked ParseRelation<ParseObject> newRelation = ParseRelation.CREATOR.createFromParcel(parcel); assertEquals(newRelation.getTargetClass(), "Test"); assertEquals(newRelation.getKey(), "key"); assertEquals(newRelation.getParent().getClassName(), "Parent"); assertEquals(newRelation.getParent().getObjectId(), "parentId"); assertEquals(newRelation.getKnownObjects().size(), 1); // This would fail assertTrue(newRelation.hasKnownObject(inner)). // That is because ParseRelation uses == to check for known objects. }
@Test public void testParcelWhileSavingWithLDSEnabled() throws Exception { mockCurrentUserController(); TaskCompletionSource<ParseObject.State> tcs = mockObjectControllerForSave(); ParseObject object = new ParseObject("TestObject"); object.setObjectId("id"); OfflineStore lds = mock(OfflineStore.class); when(lds.getObject("TestObject", "id")).thenReturn(object); Parse.setLocalDatastore(lds); object.put("key", "value"); object.increment("number", 3); Task<Void> saveTask = object.saveInBackground(); assertTrue(object.hasOutstandingOperations()); // Saving assertFalse(object.isDirty()); // Not dirty because it's saving Parcel parcel = Parcel.obtain(); object.writeToParcel(parcel, 0); parcel.setDataPosition(0); ParseObject other = ParseObject.CREATOR.createFromParcel(parcel); assertSame(object, other); assertTrue(other.hasOutstandingOperations()); // Still saving assertFalse(other.isDirty()); // Still not dirty assertEquals(other.getNumber("number"), 3); tcs.setResult(null); saveTask.waitForCompletion(); Parse.setLocalDatastore(null); }
@Test public void testParcelWithCircularReference() { ParseObject parent = new ParseObject("Parent"); ParseObject child = new ParseObject("Child"); parent.setObjectId("parentId"); parent.put("self", parent); child.setObjectId("childId"); child.put("self", child); child.put("parent", parent); parent.put("child", child); Parcel parcel = Parcel.obtain(); parent.writeToParcel(parcel, 0); parcel.setDataPosition(0); parent = ParseObject.CREATOR.createFromParcel(parcel); assertEquals(parent.getObjectId(), "parentId"); assertEquals(parent.getParseObject("self").getObjectId(), "parentId"); child = parent.getParseObject("child"); assertEquals(child.getObjectId(), "childId"); assertEquals(child.getParseObject("self").getObjectId(), "childId"); assertEquals(child.getParseObject("parent").getObjectId(), "parentId"); }
object.setObjectId("id"); object.put("key", "value"); object.put("number", 5);
@Test public void testParcelWhileDeleting() throws Exception { mockCurrentUserController(); TaskCompletionSource<Void> tcs = mockObjectControllerForDelete(); ParseObject object = new ParseObject("TestObject"); object.setObjectId("id"); Task<Void> deleteTask = object.deleteInBackground(); // ensure Log.w is called.. assertTrue(object.isDeleting); Parcel parcel = Parcel.obtain(); object.writeToParcel(parcel, 0); parcel.setDataPosition(0); ParseObject other = ParseObject.CREATOR.createFromParcel(parcel); // By design, when LDS is off, we assume that old operations failed even if // they are still running on the old instance. assertFalse(other.isDeleting); assertTrue(object.isDeleting); tcs.setResult(null); deleteTask.waitForCompletion(); assertFalse(object.isDeleting); assertTrue(object.isDeleted); }
@Test public void testParcelWithCircularReferenceFromServer() { ParseObject parent = new ParseObject("Parent"); ParseObject child = new ParseObject("Child"); parent.setState(new ParseObject.State.Builder("Parent") .objectId("parentId") .put("self", parent) .put("child", child).build()); parent.setObjectId("parentId"); child.setState(new ParseObject.State.Builder("Child") .objectId("childId") .put("self", child) .put("parent", parent).build()); Parcel parcel = Parcel.obtain(); parent.writeToParcel(parcel, 0); parcel.setDataPosition(0); parent = ParseObject.CREATOR.createFromParcel(parcel); assertEquals(parent.getObjectId(), "parentId"); assertEquals(parent.getParseObject("self").getObjectId(), "parentId"); child = parent.getParseObject("child"); assertEquals(child.getObjectId(), "childId"); assertEquals(child.getParseObject("self").getObjectId(), "childId"); assertEquals(child.getParseObject("parent").getObjectId(), "parentId"); }