NCMBObject extends NCMBBase { public void save() throws NCMBException { NCMBObjectService objService = (NCMBObjectService) NCMB.factory(NCMB.ServiceType.OBJECT); JSONObject res = null; if (getObjectId() == null){ res = objService.saveObject(mClassName, mFields); } else { JSONObject updateJson = null; try { updateJson = createUpdateJsonData(); } catch (JSONException e) { throw new IllegalArgumentException(e.getMessage()); } res = objService.updateObject(mClassName, getObjectId(), updateJson); } setServerDataToProperties(res); mUpdateKeys.clear(); } NCMBObject(String className);   NCMBObject(String className, JSONObject params); void save(); void saveInBackground(final DoneCallback callback); void fetch(); void fetchInBackground(); void fetchInBackground(final FetchCallback callback); void deleteObject(); void deleteObjectInBackground(final DoneCallback callback); void increment(String key, int amount); void addToList(String key, List objects); void addUniqueToList(String key, List objects); void removeFromList(String key, List objects);  }
@Test public void save_object_default_class() throws NCMBException { NCMBObject obj = new NCMBObject("user"); obj.put("key", "value"); NCMBException error = null; try { obj.save(); } catch (NCMBException e) { error = e; } Assert.assertEquals(NCMBException.OPERATION_FORBIDDEN, error.getCode()); }
@Test public void save_object_with_acl() { try { NCMBObject obj = new NCMBObject("TestClass"); obj.put("key", "value"); NCMBAcl acl = new NCMBAcl(new JSONObject("{\"*\":{\"read\":true, \"write\":true}}")); obj.setAcl(acl); obj.save(); Assert.assertEquals("7FrmPTBKSNtVjajm", obj.getObjectId()); SimpleDateFormat df = NCMBDateFormat.getIso8601(); Assert.assertEquals(df.parse("2015-09-14T11:28:30.348Z"), obj.getCreateDate()); JSONAssert.assertEquals(obj.getAcl().toJson(), acl.toJson(), false); } catch (NCMBException | JSONException | ParseException e) { Assert.fail(e.getMessage()); } }
@Test public void save_object_with_acl_parameter() { try { NCMBObject obj = new NCMBObject("TestClass", new JSONObject("{\"acl\":{\"*\":{\"read\":true, \"write\":true}}}")); obj.put("key", "value"); obj.save(); Assert.assertEquals("7FrmPTBKSNtVjajm", obj.getObjectId()); SimpleDateFormat df = NCMBDateFormat.getIso8601(); Assert.assertEquals(df.parse("2015-09-14T11:28:30.348Z"), obj.getCreateDate()); JSONAssert.assertEquals(obj.getAcl().toJson(), new JSONObject("{\"*\":{\"read\":true, \"write\":true}}"), false); } catch (NCMBException | JSONException | ParseException e) { Assert.fail(e.getMessage()); } }
@Test public void save_object_with_remove() { try { NCMBObject obj = new NCMBObject("TestClass", new JSONObject("{\"key\":\"value\"}")); obj.setObjectId("testObjectId"); obj.remove("key"); obj.save(); Assert.assertFalse(obj.containsKey("key")); SimpleDateFormat df = NCMBDateFormat.getIso8601(); Assert.assertEquals(df.parse("2015-09-16T11:28:30.348Z"), obj.getUpdateDate()); } catch (NCMBException | JSONException | ParseException e) { Assert.fail(e.getMessage()); } }
@Test public void update_object_with_update_value() throws Exception { NCMBObject obj = new NCMBObject("TestClass"); obj.setObjectId("updateTestObjectId"); obj.put("updateKey", "updateValue"); obj.save(); SimpleDateFormat df = NCMBDateFormat.getIso8601(); Assert.assertTrue(obj.getUpdateDate().equals(df.parse("2014-06-04T11:28:30.348Z"))); }
@Test public void update_object_non_update_value() throws Exception { NCMBObject obj = new NCMBObject("TestClass"); obj.setObjectId("updateTestObjectId"); obj.save(); SimpleDateFormat df = NCMBDateFormat.getIso8601(); Assert.assertTrue(obj.getUpdateDate().equals(df.parse("2014-06-04T11:28:30.348Z"))); }
@Test public void update_object_with_acl() { try { NCMBObject obj = new NCMBObject("TestClass"); obj.setObjectId("updateWithAclTestObjectId"); obj.put("updateKey", "updateValue"); NCMBAcl acl = new NCMBAcl(new JSONObject("{\"*\":{\"read\":true, \"write\":true}}")); obj.setAcl(acl); obj.save(); SimpleDateFormat df = NCMBDateFormat.getIso8601(); Assert.assertEquals(df.parse("2015-09-14T11:28:30.348Z"), obj.getUpdateDate()); JSONAssert.assertEquals(obj.getAcl().toJson(), acl.toJson(), false); } catch (NCMBException | JSONException | ParseException e) { Assert.fail(e.getMessage()); } }
@Test public void update_object_with_acl_parameter_dont_update_acl() { try { NCMBObject obj = new NCMBObject("TestClass", new JSONObject("{\"acl\":{\"*\":{\"read\":true, \"write\":true}}}")); obj.setObjectId("updateTestObjectId"); obj.put("updateKey", "updateValue"); obj.save(); SimpleDateFormat df = NCMBDateFormat.getIso8601(); Assert.assertEquals(df.parse("2014-06-04T11:28:30.348Z"), obj.getUpdateDate()); JSONAssert.assertEquals(obj.getAcl().toJson(), new JSONObject("{\"*\":{\"read\":true, \"write\":true}}"), false); } catch (NCMBException | JSONException | ParseException e) { Assert.fail(e.getMessage()); } }
@Test public void update_object_only_update_key() { try { NCMBObject obj = new NCMBObject("TestClass"); obj.setObjectId("updateTestObjectId"); obj.put("updateKey", "updateValue"); NCMBAcl acl = new NCMBAcl(new JSONObject("{\"*\":{\"read\":true, \"write\":true}}")); obj.setAclFromInternal(acl); obj.save(); SimpleDateFormat df = NCMBDateFormat.getIso8601(); Assert.assertEquals(df.parse("2014-06-04T11:28:30.348Z"), obj.getUpdateDate()); JSONAssert.assertEquals(obj.getAcl().toJson(), acl.toJson(), false); } catch (NCMBException | JSONException | ParseException e) { Assert.fail(e.getMessage()); } }
@Test public void save_object_valid_class() throws Exception { NCMBObject obj = new NCMBObject("SaveObjectTest"); obj.put("key", "value"); NCMBException error = null; try { obj.save(); } catch (NCMBException e) { error = e; } Assert.assertNull(error); Assert.assertNotNull(obj); Assert.assertEquals("7FrmPTBKSNtVjajm9", obj.getObjectId()); SimpleDateFormat df = NCMBDateFormat.getIso8601(); Assert.assertTrue(obj.getCreateDate().equals(df.parse("2014-06-03T11:28:30.348Z"))); Assert.assertTrue(obj.getUpdateDate().equals(df.parse("2014-06-03T11:28:30.348Z"))); Assert.assertEquals(0, obj.mUpdateKeys.size()); }