FragmentEntryValidatorImpl implements FragmentEntryValidator { @Override public void validateConfigurationValues( String configuration, JSONObject valuesJSONObject) throws FragmentEntryConfigurationException { if (Validator.isNull(configuration)) { return; } InputStream configurationJSONSchemaInputStream = FragmentEntryValidatorImpl.class.getResourceAsStream( "dependencies/configuration-json-schema.json"); try { JSONValidator.validate( configuration, configurationJSONSchemaInputStream); JSONObject configurationJSONObject = JSONFactoryUtil.createJSONObject(configuration); JSONArray fieldSetsJSONArray = configurationJSONObject.getJSONArray( "fieldSets"); Set<String> fieldNames = new HashSet<>(); for (int fieldSetIndex = 0; fieldSetIndex < fieldSetsJSONArray.length(); fieldSetIndex++) { JSONObject fieldSetJSONObject = fieldSetsJSONArray.getJSONObject(fieldSetIndex); JSONArray fieldsJSONArray = fieldSetJSONObject.getJSONArray( "fields"); for (int fieldIndex = 0; fieldIndex < fieldsJSONArray.length(); fieldIndex++) { JSONObject fieldJSONObject = fieldsJSONArray.getJSONObject( fieldIndex); String fieldName = fieldJSONObject.getString("name"); if (fieldNames.contains(fieldName)) { throw new FragmentEntryConfigurationException( "Field names must be unique"); } JSONObject typeOptionsJSONObject = fieldJSONObject.getJSONObject("typeOptions"); if (typeOptionsJSONObject != null) { String defaultValue = fieldJSONObject.getString( "defaultValue"); if (!_checkValidationRules( defaultValue, typeOptionsJSONObject.getJSONObject( "validation"))) { throw new FragmentEntryConfigurationException( "Invalid default configuration value for " + "field " + fieldName); } if (valuesJSONObject != null) { String value = valuesJSONObject.getString( fieldName); if (!_checkValidationRules( value, typeOptionsJSONObject.getJSONObject( "validation"))) { throw new FragmentEntryConfigurationException( "Invalid configuration value for field " + fieldName); } } } fieldNames.add(fieldName); } } } catch (JSONException jsonException) { throw new FragmentEntryConfigurationException( _getMessage(jsonException.getMessage()), jsonException); } catch (JSONValidatorException jsonValidatorException) { throw new FragmentEntryConfigurationException( _getMessage(jsonValidatorException.getMessage()), jsonValidatorException); } }  @Override void validateConfiguration(String configuration); @Override void validateConfigurationValues(
			String configuration, JSONObject valuesJSONObject);  }
@Test public void testValidateConfigurationValuesTextFieldTypeInvalidEmail() throws Exception { expectedException.expect(FragmentEntryConfigurationException.class); _fragmentEntryValidatorImpl.validateConfigurationValues( _read("configuration_field_text_typeoptions_validation_email.json"), JSONUtil.put("emailField", "test-liferay.com")); }
@Test public void testValidateConfigurationValuesTextFieldTypeInvalidLength() throws Exception { expectedException.expect(FragmentEntryConfigurationException.class); _fragmentEntryValidatorImpl.validateConfigurationValues( _read( "configuration_field_text_typeoptions_validation_length.json"), JSONUtil.put("textField", StringUtil.randomString(11))); }
@Test public void testValidateConfigurationValuesTextFieldTypeInvalidNumberLimit() throws Exception { expectedException.expect(FragmentEntryConfigurationException.class); _fragmentEntryValidatorImpl.validateConfigurationValues( _read( "configuration_field_text_typeoptions_validation_number.json"), JSONUtil.put("numberField", 1000)); }
@Test public void testValidateConfigurationValuesTextFieldTypeInvalidNumberNAN() throws Exception { expectedException.expect(FragmentEntryConfigurationException.class); _fragmentEntryValidatorImpl.validateConfigurationValues( _read( "configuration_field_text_typeoptions_validation_number.json"), JSONUtil.put("numberField", StringUtil.randomString())); }
@Test public void testValidateConfigurationValuesTextFieldTypeInvalidRegexp() throws Exception { expectedException.expect(FragmentEntryConfigurationException.class); _fragmentEntryValidatorImpl.validateConfigurationValues( _read( "configuration_field_text_typeoptions_validation_regexp.json"), JSONUtil.put("regexpField", StringUtil.randomString())); }
@Test public void testValidateConfigurationValuesTextFieldTypeInvalidURL() throws Exception { expectedException.expect(FragmentEntryConfigurationException.class); _fragmentEntryValidatorImpl.validateConfigurationValues( _read("configuration_field_text_typeoptions_validation_url.json"), JSONUtil.put("urlField", StringUtil.randomString())); }
@Test public void testValidateConfigurationValuesTextFieldTypeValidEmail() throws Exception { _fragmentEntryValidatorImpl.validateConfigurationValues( _read("configuration_field_text_typeoptions_validation_email.json"), JSONUtil.put("emailField", "test@liferay.com")); }
@Test public void testValidateConfigurationValuesTextFieldTypeValidLength() throws Exception { _fragmentEntryValidatorImpl.validateConfigurationValues( _read( "configuration_field_text_typeoptions_validation_length.json"), JSONUtil.put("textField", StringUtil.randomString(9))); }
@Test public void testValidateConfigurationValuesTextFieldTypeValidNumber() throws Exception { _fragmentEntryValidatorImpl.validateConfigurationValues( _read( "configuration_field_text_typeoptions_validation_number.json"), JSONUtil.put("numberField", 256)); }
@Test public void testValidateConfigurationValuesTextFieldTypeValidRegexp() throws Exception { _fragmentEntryValidatorImpl.validateConfigurationValues( _read( "configuration_field_text_typeoptions_validation_regexp.json"), JSONUtil.put("regexpField", "test-256")); }
@Test public void testValidateConfigurationValuesTextFieldTypeValidURL() throws Exception { _fragmentEntryValidatorImpl.validateConfigurationValues( _read("configuration_field_text_typeoptions_validation_url.json"), JSONUtil.put("urlField", "http: }