Utils { @WorkerThread @Nullable static CTGeofenceSettings readSettingsFromFile(@NonNull Context context) { CTGeofenceAPI.getLogger().debug(CTGeofenceAPI.GEOFENCE_LOG_TAG, "Reading settings from file..."); CTGeofenceSettings ctGeofenceSettings = null; String settingsString = FileUtils.readFromFile(context, FileUtils.getCachedFullPath(context, CTGeofenceConstants.SETTINGS_FILE_NAME)); if (!settingsString.trim().equals("")) { try { JSONObject jsonObject = new JSONObject(settingsString); ctGeofenceSettings = new CTGeofenceSettings.Builder() .enableBackgroundLocationUpdates( jsonObject.getBoolean(CTGeofenceConstants.KEY_LAST_BG_LOCATION_UPDATES)) .setLocationAccuracy((byte) jsonObject.getInt(CTGeofenceConstants.KEY_LAST_ACCURACY)) .setLocationFetchMode((byte) jsonObject.getInt(CTGeofenceConstants.KEY_LAST_FETCH_MODE)) .setLogLevel(jsonObject.getInt(CTGeofenceConstants.KEY_LAST_LOG_LEVEL)) .setGeofenceMonitoringCount(jsonObject.getInt(CTGeofenceConstants.KEY_LAST_GEO_COUNT)) .setId(jsonObject.getString(CTGeofenceConstants.KEY_ID)) .setInterval(jsonObject.getLong(CTGeofenceConstants.KEY_LAST_INTERVAL)) .setFastestInterval(jsonObject.getLong(CTGeofenceConstants.KEY_LAST_FASTEST_INTERVAL)) .setSmallestDisplacement( (float) jsonObject.getDouble(CTGeofenceConstants.KEY_LAST_DISPLACEMENT)) .build(); CTGeofenceAPI.getLogger().debug(CTGeofenceAPI.GEOFENCE_LOG_TAG, "Read settings successfully from file"); } catch (Exception e) { CTGeofenceAPI.getLogger().debug(CTGeofenceAPI.GEOFENCE_LOG_TAG, "Failed to read geofence settings from file"); } } else { CTGeofenceAPI.getLogger().debug(CTGeofenceAPI.GEOFENCE_LOG_TAG, "Settings not found in file..."); } return ctGeofenceSettings; }    }
@Test public void testReadSettingsFromFile() { mockStatic(FileUtils.class); when(FileUtils.getCachedFullPath(any(Context.class), anyString())).thenReturn(""); when(FileUtils.readFromFile(any(Context.class), anyString())).thenReturn(CTGeofenceSettingsFake.getSettingsJsonString()); CTGeofenceSettings settingsActualWhenNotEmpty = Utils.readSettingsFromFile(application); CTGeofenceSettings settingsExpectedWhenNotEmpty = CTGeofenceSettingsFake.getSettings(CTGeofenceSettingsFake.getSettingsJsonObject()); assertThat(settingsActualWhenNotEmpty, samePropertyValuesAs(settingsExpectedWhenNotEmpty)); when(FileUtils.readFromFile(any(Context.class), anyString())).thenReturn(""); CTGeofenceSettings settingsActualWhenEmpty = Utils.readSettingsFromFile(application); assertNull(settingsActualWhenEmpty); }