Utils { static List<String> jsonToGeoFenceList(@NonNull JSONObject jsonObject) { ArrayList<String> geofenceIdList = new ArrayList<>(); try { JSONArray array = jsonObject.getJSONArray("geofences"); for (int i = 0; i < array.length(); i++) { JSONObject object = array.getJSONObject(i); geofenceIdList.add(object.getString(CTGeofenceConstants.KEY_ID)); } } catch (Exception e) { CTGeofenceAPI.getLogger().debug(CTGeofenceAPI.GEOFENCE_LOG_TAG, "Could not convert JSON to GeofenceIdList - " + e.getMessage()); e.printStackTrace(); } return geofenceIdList; }    }
@Test public void testJsonToGeoFenceList() { List<String> actualList = Utils.jsonToGeoFenceList(GeofenceJSON.getGeofence()); List<String> expectedList = Arrays.asList("310001", "310002"); assertThat(actualList, CoreMatchers.is(expectedList)); List<String> actualListWhenJsonArrayIsEmpty = Utils.jsonToGeoFenceList(GeofenceJSON.getEmptyGeofence()); List<String> expectedListWhenJsonArrayIsEmpty = new ArrayList<>(); assertThat(actualListWhenJsonArrayIsEmpty, CoreMatchers.is(expectedListWhenJsonArrayIsEmpty)); JSONObject emptyJson = GeofenceJSON.getEmptyJson(); List<String> actualListWhenJsonIsEmpty = Utils.jsonToGeoFenceList(emptyJson); List<String> expectedListWhenJsonIsEmpty = new ArrayList<>(); assertThat(actualListWhenJsonIsEmpty, CoreMatchers.is(expectedListWhenJsonIsEmpty)); }