StructureAndTypeSettingsValidator implements SettingsValidator { @Override public boolean isValid(String json) { try { JSONObject jsonObject = new JSONObject(json); JSONObject general = jsonObject.getJSONObject("general"); JSONObject admin = jsonObject.getJSONObject("admin"); return !hasInvalidTypes(general, generalDefaults) && !hasInvalidTypes(admin, adminDefaults); } catch (JSONException e) { return false; } } StructureAndTypeSettingsValidator(Map<String, Object> generalDefaults, Map<String, Object> adminDefaults); @Override boolean isValid(String json);  }
@Test public void forEmptySettings_returnsTrue() throws Exception { assertThat(validator.isValid(emptySettings()), is(true)); }
@Test public void forNonJSON_returnsFalse() { assertThat(validator.isValid("blah"), is(false)); }
@Test public void withoutGeneralObject_returnsFalse() throws Exception { JSONObject json = emptySettingsObject(); json.remove("general"); assertThat(validator.isValid(json.toString()), is(false)); }
@Test public void withoutAdminObject_returnsFalse() throws Exception { JSONObject json = emptySettingsObject(); json.remove("admin"); assertThat(validator.isValid(json.toString()), is(false)); }
@Test public void ifGeneralValueDoesNotMatchDefaultType_returnsFalse() throws Exception { JSONObject json = emptySettingsObject() .put("general", new JSONObject() .put("key1", "string")); assertThat(validator.isValid(json.toString()), is(false)); }
@Test public void ifAdminValueDoesNotMatchDefaultType_returnsFalse() throws Exception { JSONObject json = emptySettingsObject() .put("admin", new JSONObject() .put("key1", false)); assertThat(validator.isValid(json.toString()), is(false)); }