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);  }
@Test public void fromJSON_whenPreferencesAreValid_savesIncludedGeneralKeys_andResetsMissingGeneralKeys() throws Exception { JSONObject content = new JSONObject(); JSONObject general = new JSONObject(); content.put("general", general); content.put("admin", new JSONObject()); general.put(KEY_PASSWORD, "password"); GeneralSharedPreferences generalSharedPreferences = mock(GeneralSharedPreferences.class); new PreferenceSaver(generalSharedPreferences, AdminSharedPreferences.getInstance()).fromJSON(content.toString(), mock(ActionListener.class)); verify(generalSharedPreferences).save(KEY_PASSWORD, "password"); verify(generalSharedPreferences).reset(KEY_SERVER_URL); }
@Test public void fromJSON_whenPreferencesAreValid_callsSuccessListener() throws Exception { JSONObject content = new JSONObject(); JSONObject general = new JSONObject(); content.put("general", general); content.put("admin", new JSONObject()); general.put(KEY_PASSWORD, "password"); ActionListener listener = mock(ActionListener.class); GeneralSharedPreferences generalSharedPreferences = mock(GeneralSharedPreferences.class); new PreferenceSaver(generalSharedPreferences, AdminSharedPreferences.getInstance()).fromJSON(content.toString(), listener); verify(listener).onSuccess(); }
@Test public void fromJSON_whenPreferencesAreInvalid_DoesNotSaveOrResetGeneralKeys() 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); new PreferenceSaver(generalSharedPreferences, AdminSharedPreferences.getInstance()).fromJSON(content.toString(), mock(ActionListener.class)); verify(generalSharedPreferences, never()).save(any(), any()); verify(generalSharedPreferences, never()).reset(any()); }
@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)); }