JsonHelper { public static String generateCoordinates(String json) { try { JSONObject obj = new JSONObject(json); Double[] longLat = Extractor.extractLocation(obj); if (obj.has(KEY_COORDINATES) && obj.isNull(KEY_COORDINATES)) { JSONArray longLatArr = new JSONArray(); if (longLat != null && longLat[0] != null && longLat[1] != null) { longLatArr.put(longLat[0]); longLatArr.put(longLat[1]); JSONObject coordinates1 = new JSONObject().put(KEY_COORDINATES, longLatArr); obj.put(KEY_COORDINATES, coordinates1); } } if (obj.has(KEY_PLACE)) { obj.remove(KEY_PLACE); } return obj.toString(); } catch (JSONException e) { log.error("no correct JSON string:" + json); return null; } }  static String generateCoordinates(String json); static String createTweetsWithHashtagRanking(String tweetsArray); static String createTweetsWithoutHashtagRanking(String tweetsArray); static final String KEY_COORDINATES; static final String KEY_PLACE; static final String KEY_TWEETS; static final String KEY_TOPTEN; }
@Test public void generateCoordinates() throws Exception { String result = JsonHelper.generateCoordinates(tweetWithoutCoord); JSONObject tweetJson = new JSONObject(result); assertTrue(tweetJson.has(JsonHelper.KEY_COORDINATES)); assertTrue(tweetJson.getJSONObject(JsonHelper.KEY_COORDINATES).has(JsonHelper.KEY_COORDINATES)); assertFalse(tweetJson.isNull(JsonHelper.KEY_COORDINATES)); assertFalse(tweetJson.has(JsonHelper.KEY_PLACE)); assertTrue(tweetJson.getJSONObject(JsonHelper.KEY_COORDINATES).getJSONArray(JsonHelper.KEY_COORDINATES).getDouble(0) == 16.379845500000002); assertTrue(tweetJson.getJSONObject(JsonHelper.KEY_COORDINATES).getJSONArray(JsonHelper.KEY_COORDINATES).getDouble(1) == 48.220119999999994); }