public static ParseRESTUserCommand serviceLogInUserCommand( String authType, Map<String, String> authData, boolean revocableSession) { // Mimic ParseSetOperation JSONObject parameters; try { JSONObject authenticationData = new JSONObject(); authenticationData.put(authType, PointerEncoder.get().encode(authData)); parameters = new JSONObject(); parameters.put("authData", authenticationData); } catch (JSONException e) { throw new RuntimeException("could not serialize object to JSON"); } return serviceLogInUserCommand(parameters, null, revocableSession); }
/** * Access a {@link JSONObject} value, returning a default value if it doesn't exist. * * @param key The key to access the value for. * @param defaultValue The value to return if the key is not present or has the wrong type. * @return The default value if there is no such key or if it is not a {@link JSONObject}. */ public JSONObject getJSONObject(String key, JSONObject defaultValue) { Map<String, Object> map = getMap(key); Object encoded = (map != null) ? PointerEncoder.get().encode(map) : null; //TODO(mengyan) There are actually two cases, getList(key) will return null // case 1: key not exist, in this situation, we should return JSONArray defaultValue // case 2: key exist but value is Json.NULL, in this situation, we should return null // The following line we only cover case 2. We can not revise it since it may break some // existing app, but we should do it someday. return (encoded == null || encoded instanceof JSONObject) ? (JSONObject) encoded : defaultValue; }
/** * Access a {@link JSONArray} value, returning a default value if it doesn't exist. * * @param key The key to access the value for. * @param defaultValue The value to return if the key is not present or has the wrong type. * @return The default value if there is no such key or if it is not a {@link JSONArray}. */ public JSONArray getJSONArray(String key, JSONArray defaultValue) { List<Object> list = getList(key); Object encoded = (list != null) ? PointerEncoder.get().encode(list) : null; //TODO(mengyan) There are actually two cases, getList(key) will return null // case 1: key not exist, in this situation, we should return JSONArray defaultValue // case 2: key exist but value is Json.NULL, in this situation, we should return null // The following line we only cover case 2. We can not revise it since it may break some // existing app, but we should do it someday. return (encoded == null || encoded instanceof JSONArray) ? (JSONArray) encoded : defaultValue; }
if (query != null) { ParseQuery.QueryConstraints where = query.constraints(); whereJSON = (JSONObject) PointerEncoder.get().encode(where);
@Test public void testServiceLogInUserCommandWithAuthType() throws Exception { Map<String, String> facebookAuthData = new HashMap<>(); facebookAuthData.put("token", "test"); ParseRESTUserCommand command = ParseRESTUserCommand.serviceLogInUserCommand("facebook", facebookAuthData, true); assertEquals("users", command.httpPath); assertEquals(ParseHttpRequest.Method.POST, command.method); assertNull(command.getSessionToken()); JSONObject authenticationData = new JSONObject(); authenticationData.put("facebook", PointerEncoder.get().encode(facebookAuthData)); JSONObject parameters = new JSONObject(); parameters.put("authData", authenticationData); assertEquals(parameters, command.jsonParameters, JSONCompareMode.NON_EXTENSIBLE); // TODO(mengyan): Find a way to verify revocableSession }
Object restFormat = null; try { restFormat = PointerEncoder.get().encode(value); } catch (Exception e) {
@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 testEncodeRelatedObjectWithoutObjectId() { thrown.expect(IllegalStateException.class); thrown.expectMessage("unable to encode an association with an unsaved ParseObject"); ParseObject parseObject = new ParseObject("TestObject"); JSONObject jsonObject = (JSONObject) PointerEncoder.get().encode(parseObject); }
@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); }