use of org.apache.sling.validation.impl.model.ResourcePropertyBuilder in project sling by apache.
the class ResourceValidationModelProviderImpl method buildProperties.
/** Creates a set of the properties that a resource is expected to have, together with the associated validators.
*
* @param propertiesResource the resource identifying the properties node from a validation model's structure (might be {@code null})
* @return a set of properties or an empty set if no properties are defined
* @see ResourceProperty */
@Nonnull
private List<ResourceProperty> buildProperties(@Nonnull Resource propertiesResource) {
List<ResourceProperty> properties = new ArrayList<ResourceProperty>();
if (propertiesResource != null) {
for (Resource propertyResource : propertiesResource.getChildren()) {
ResourcePropertyBuilder resourcePropertyBuilder = new ResourcePropertyBuilder();
String fieldName = propertyResource.getName();
ValueMap propertyValueMap = propertyResource.getValueMap();
if (propertyValueMap.get(ResourceValidationModelProviderImpl.PROPERTY_MULTIPLE, false)) {
resourcePropertyBuilder.multiple();
}
if (propertyValueMap.get(ResourceValidationModelProviderImpl.OPTIONAL, false)) {
resourcePropertyBuilder.optional();
}
String nameRegex = propertyValueMap.get(ResourceValidationModelProviderImpl.NAME_REGEX, String.class);
if (nameRegex != null) {
resourcePropertyBuilder.nameRegex(nameRegex);
}
Resource validators = propertyResource.getChild(ResourceValidationModelProviderImpl.VALIDATORS);
if (validators != null) {
Iterator<Resource> validatorsIterator = validators.listChildren();
while (validatorsIterator.hasNext()) {
Resource validatorResource = validatorsIterator.next();
ValueMap validatorProperties = validatorResource.adaptTo(ValueMap.class);
if (validatorProperties == null) {
throw new IllegalStateException("Could not adapt resource at '" + validatorResource.getPath() + "' to ValueMap");
}
String validatorId = validatorResource.getName();
// get arguments for validator
String[] validatorArguments = validatorProperties.get(ResourceValidationModelProviderImpl.VALIDATOR_ARGUMENTS, String[].class);
Map<String, Object> validatorArgumentsMap = new HashMap<String, Object>();
if (validatorArguments != null) {
for (String arg : validatorArguments) {
int positionOfSeparator = arg.indexOf("=");
if (positionOfSeparator < 1 || positionOfSeparator >= arg.length() - 1) {
throw new IllegalArgumentException("Invalid validator argument '" + arg + "' found, because it does not follow the format '<key>=<value>'");
}
String key = arg.substring(0, positionOfSeparator);
String value = arg.substring(positionOfSeparator + 1);
Object newValue;
if (validatorArgumentsMap.containsKey(key)) {
Object oldValue = validatorArgumentsMap.get(key);
// either extend old array
if (oldValue instanceof String[]) {
String[] oldArray = (String[]) oldValue;
int newLength = oldArray.length + 1;
String[] newArray = Arrays.copyOf(oldArray, oldArray.length + 1);
newArray[newLength - 1] = value;
newValue = newArray;
} else {
// or turn the single value into a multi-value array
newValue = new String[] { (String) oldValue, value };
}
} else {
newValue = value;
}
validatorArgumentsMap.put(key, newValue);
}
}
// get severity
Integer severity = validatorProperties.get(SEVERITY, Integer.class);
resourcePropertyBuilder.validator(validatorId, severity, validatorArgumentsMap);
}
}
properties.add(resourcePropertyBuilder.build(fieldName));
}
}
return properties;
}
use of org.apache.sling.validation.impl.model.ResourcePropertyBuilder in project sling by apache.
the class ValidationServiceImplTest method setUp.
@Before
public void setUp() throws LoginException, PersistenceException, RepositoryException {
validationService = new ValidationServiceImpl();
validationService.searchPaths = Arrays.asList(context.resourceResolver().getSearchPath());
validationService.configuration = configuration;
Mockito.doReturn(20).when(configuration).defaultSeverity();
validationService.resourceBundleProviders = Collections.singletonList(resourceBundleProvider);
Mockito.doReturn(defaultResourceBundle).when(resourceBundleProvider).getResourceBundle(Mockito.anyObject());
modelBuilder = new ValidationModelBuilder();
propertyBuilder = new ResourcePropertyBuilder();
dateValidator = new DateValidator();
Mockito.doReturn(1l).when(providingBundle).getBundleId();
Mockito.doReturn(providingBundle).when(validatorServiceReference).getBundle();
Mockito.doReturn(providingBundle).when(newValidatorServiceReference).getBundle();
validationService.validatorMap.put(DATE_VALIDATOR_ID, dateValidator, validatorServiceReference, 10);
validationService.validatorMap.put(REGEX_VALIDATOR_ID, new RegexValidator(), validatorServiceReference, 10);
validationService.modelRetriever = modelRetriever;
}
use of org.apache.sling.validation.impl.model.ResourcePropertyBuilder in project sling by apache.
the class ResourceValidationModelProviderImplTest method testGetValidationModelsWithChildren.
@Test
public void testGetValidationModelsWithChildren() throws Exception {
// build two models manually (which are identical except for the applicable path)
ResourcePropertyBuilder resourcePropertyBuilder = new ResourcePropertyBuilder();
resourcePropertyBuilder.multiple();
resourcePropertyBuilder.optional();
ResourceProperty childproperty = resourcePropertyBuilder.build("child1property");
modelBuilder.childResource(new ChildResourceImpl("child1", null, true, Collections.singletonList(childproperty), Collections.<ChildResource>emptyList()));
ValidationModel model1 = modelBuilder.build("sling/validation/test", libsValidatorsRoot.getPath() + "/testValidationModel1");
// build models in JCR
createValidationModelResource(rr, libsValidatorsRoot.getPath(), "testValidationModel1", model1);
// compare both models
Collection<ValidationModel> models = modelProvider.getValidationModels("sling/validation/test");
Assert.assertThat(models, Matchers.contains(model1));
}
use of org.apache.sling.validation.impl.model.ResourcePropertyBuilder in project sling by apache.
the class ResourceValidationModelProviderImplTest method testGetValidationModelsWithMissingChildrenAndProperties.
@Test(expected = IllegalStateException.class)
public void testGetValidationModelsWithMissingChildrenAndProperties() throws Exception {
// create a model with properties (otherwise build() will already throw an exception)
modelBuilder = new ValidationModelBuilder();
modelBuilder.resourceProperty(new ResourcePropertyBuilder().build("field1"));
modelBuilder.addApplicablePath("content/site1");
ValidationModel model1 = modelBuilder.build("sling/validation/test", libsValidatorsRoot.getPath() + "/testValidationModel1");
Resource resource = createValidationModelResource(rr, libsValidatorsRoot.getPath(), "testValidationModel1", model1);
// make created model invalid by removing the properties sub resource
rr.delete(resource.getChild("properties"));
modelProvider.getValidationModels("sling/validation/test");
}
use of org.apache.sling.validation.impl.model.ResourcePropertyBuilder in project sling by apache.
the class ResourceValidationModelProviderImplTest method testCachingOfGetValidationModels.
@Test
public void testCachingOfGetValidationModels() throws Exception {
// build one model
ResourcePropertyBuilder resourcePropertyBuilder = new ResourcePropertyBuilder();
ValidationModel model1 = modelBuilder.resourceProperty(resourcePropertyBuilder.build("property1")).build("sling/validation/test", libsValidatorsRoot.getPath() + "/testValidationModel1");
modelBuilder.setApplicablePath("/content/site2");
// build models in JCR
createValidationModelResource(rr, libsValidatorsRoot.getPath(), "testValidationModel1", model1);
// check that both models are returned
Collection<ValidationModel> models = modelProvider.getValidationModels("sling/validation/test");
Assert.assertThat(models, Matchers.containsInAnyOrder(model1));
// the 2nd time the same instance should be returned
Collection<ValidationModel> models2 = modelProvider.getValidationModels("sling/validation/test");
Assert.assertEquals("Due to caching both models should be actually the same instance", System.identityHashCode(models), System.identityHashCode(models2));
}
Aggregations