@Test public void fromJSON_whenPreferencesAreInvalid_callsFailureListener() throws Exception { JSONObject content = new JSONObject(); JSONObject general = new JSONObject(); content.put("general", general); content.put("admin", new JSONObject()); general.put(KEY_PASSWORD, false); GeneralSharedPreferences generalSharedPreferences = mock(GeneralSharedPreferences.class); ActionListener listener = mock(ActionListener.class); new PreferenceSaver(generalSharedPreferences, AdminSharedPreferences.getInstance()).fromJSON(content.toString(), listener); verify(listener).onFailure(any(GeneralSharedPreferences.ValidationException.class)); }
public void fromJSON(String content, @Nullable ActionListener listener) { try { JSONObject settingsJson = new JSONObject(content); Map<String, Object> generalPrefs = convertJSONToMap(settingsJson.getJSONObject("general")); Map<String, Object> adminPrefsJson = convertJSONToMap(settingsJson.getJSONObject("admin")); if (!(new PreferenceValidator(GENERAL_KEYS).isValid(generalPrefs))) { if (listener != null) { listener.onFailure(new GeneralSharedPreferences.ValidationException()); } return; } saveGeneralPrefs(generalSharedPreferences, generalPrefs); saveAdminPrefs(adminSharedPreferences, adminPrefsJson); AutoSendPreferenceMigrator.migrate(settingsJson.getJSONObject("general")); if (listener != null) { listener.onSuccess(); } } catch (JSONException exception) { if (listener != null) { listener.onFailure(exception); } } }
PreferenceSaver { public void fromJSON(String content, @Nullable ActionListener listener) { try { JSONObject settingsJson = new JSONObject(content); Map<String, Object> generalPrefs = convertJSONToMap(settingsJson.getJSONObject("general")); Map<String, Object> adminPrefsJson = convertJSONToMap(settingsJson.getJSONObject("admin")); if (!(new PreferenceValidator(GENERAL_KEYS).isValid(generalPrefs))) { if (listener != null) { listener.onFailure(new GeneralSharedPreferences.ValidationException()); } return; } saveGeneralPrefs(generalSharedPreferences, generalPrefs); saveAdminPrefs(adminSharedPreferences, adminPrefsJson); AutoSendPreferenceMigrator.migrate(settingsJson.getJSONObject("general")); if (listener != null) { listener.onSuccess(); } } catch (JSONException exception) { if (listener != null) { listener.onFailure(exception); } } } }
PreferenceSaver { public void fromJSON(String content, @Nullable ActionListener listener) { try { JSONObject settingsJson = new JSONObject(content); Map<String, Object> generalPrefs = convertJSONToMap(settingsJson.getJSONObject("general")); Map<String, Object> adminPrefsJson = convertJSONToMap(settingsJson.getJSONObject("admin")); if (!(new PreferenceValidator(GENERAL_KEYS).isValid(generalPrefs))) { if (listener != null) { listener.onFailure(new GeneralSharedPreferences.ValidationException()); } return; } saveGeneralPrefs(generalSharedPreferences, generalPrefs); saveAdminPrefs(adminSharedPreferences, adminPrefsJson); AutoSendPreferenceMigrator.migrate(settingsJson.getJSONObject("general")); if (listener != null) { listener.onSuccess(); } } catch (JSONException exception) { if (listener != null) { listener.onFailure(exception); } } } PreferenceSaver(GeneralSharedPreferences generalSharedPreferences, AdminSharedPreferences adminSharedPreferences); }
PreferenceSaver { public void fromJSON(String content, @Nullable ActionListener listener) { try { JSONObject settingsJson = new JSONObject(content); Map<String, Object> generalPrefs = convertJSONToMap(settingsJson.getJSONObject("general")); Map<String, Object> adminPrefsJson = convertJSONToMap(settingsJson.getJSONObject("admin")); if (!(new PreferenceValidator(GENERAL_KEYS).isValid(generalPrefs))) { if (listener != null) { listener.onFailure(new GeneralSharedPreferences.ValidationException()); } return; } saveGeneralPrefs(generalSharedPreferences, generalPrefs); saveAdminPrefs(adminSharedPreferences, adminPrefsJson); AutoSendPreferenceMigrator.migrate(settingsJson.getJSONObject("general")); if (listener != null) { listener.onSuccess(); } } catch (JSONException exception) { if (listener != null) { listener.onFailure(exception); } } } PreferenceSaver(GeneralSharedPreferences generalSharedPreferences, AdminSharedPreferences adminSharedPreferences); void fromJSON(String content, @Nullable ActionListener listener); static void saveGeneralPrefs(GeneralSharedPreferences generalSharedPreferences, Map<String, Object> generalPrefs); static void saveAdminPrefs(AdminSharedPreferences adminSharedPreferences, Map<String, Object> adminPrefsJson); }
PreferenceSaver { public void fromJSON(String content, @Nullable ActionListener listener) { try { JSONObject settingsJson = new JSONObject(content); Map<String, Object> generalPrefs = convertJSONToMap(settingsJson.getJSONObject("general")); Map<String, Object> adminPrefsJson = convertJSONToMap(settingsJson.getJSONObject("admin")); if (!(new PreferenceValidator(GENERAL_KEYS).isValid(generalPrefs))) { if (listener != null) { listener.onFailure(new GeneralSharedPreferences.ValidationException()); } return; } saveGeneralPrefs(generalSharedPreferences, generalPrefs); saveAdminPrefs(adminSharedPreferences, adminPrefsJson); AutoSendPreferenceMigrator.migrate(settingsJson.getJSONObject("general")); if (listener != null) { listener.onSuccess(); } } catch (JSONException exception) { if (listener != null) { listener.onFailure(exception); } } } PreferenceSaver(GeneralSharedPreferences generalSharedPreferences, AdminSharedPreferences adminSharedPreferences); void fromJSON(String content, @Nullable ActionListener listener); static void saveGeneralPrefs(GeneralSharedPreferences generalSharedPreferences, Map<String, Object> generalPrefs); static void saveAdminPrefs(AdminSharedPreferences adminSharedPreferences, Map<String, Object> adminPrefsJson);  }