Search in sources :

Example 6 with ResourcePropertyBuilder

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;
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ValueMap(org.apache.sling.api.resource.ValueMap) ArrayList(java.util.ArrayList) Resource(org.apache.sling.api.resource.Resource) ChildResource(org.apache.sling.validation.model.ChildResource) ResourcePropertyBuilder(org.apache.sling.validation.impl.model.ResourcePropertyBuilder) ResourceProperty(org.apache.sling.validation.model.ResourceProperty) Nonnull(javax.annotation.Nonnull)

Example 7 with ResourcePropertyBuilder

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;
}
Also used : DateValidator(org.apache.sling.validation.impl.util.examplevalidators.DateValidator) RegexValidator(org.apache.sling.validation.impl.validators.RegexValidator) ResourcePropertyBuilder(org.apache.sling.validation.impl.model.ResourcePropertyBuilder) ValidationModelBuilder(org.apache.sling.validation.impl.model.ValidationModelBuilder) Before(org.junit.Before)

Example 8 with ResourcePropertyBuilder

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));
}
Also used : ResourceProperty(org.apache.sling.validation.model.ResourceProperty) ValidationModel(org.apache.sling.validation.model.ValidationModel) ChildResourceImpl(org.apache.sling.validation.impl.model.ChildResourceImpl) ChildResource(org.apache.sling.validation.model.ChildResource) ResourcePropertyBuilder(org.apache.sling.validation.impl.model.ResourcePropertyBuilder) Test(org.junit.Test)

Example 9 with ResourcePropertyBuilder

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");
}
Also used : ValidationModel(org.apache.sling.validation.model.ValidationModel) Resource(org.apache.sling.api.resource.Resource) ChildResource(org.apache.sling.validation.model.ChildResource) ResourcePropertyBuilder(org.apache.sling.validation.impl.model.ResourcePropertyBuilder) ValidationModelBuilder(org.apache.sling.validation.impl.model.ValidationModelBuilder) Test(org.junit.Test)

Example 10 with ResourcePropertyBuilder

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));
}
Also used : ValidationModel(org.apache.sling.validation.model.ValidationModel) ResourcePropertyBuilder(org.apache.sling.validation.impl.model.ResourcePropertyBuilder) Test(org.junit.Test)

Aggregations

ResourcePropertyBuilder (org.apache.sling.validation.impl.model.ResourcePropertyBuilder)11 ValidationModel (org.apache.sling.validation.model.ValidationModel)8 Test (org.junit.Test)8 ValidationModelBuilder (org.apache.sling.validation.impl.model.ValidationModelBuilder)6 HashMap (java.util.HashMap)4 ChildResource (org.apache.sling.validation.model.ChildResource)4 Resource (org.apache.sling.api.resource.Resource)3 ResourceProperty (org.apache.sling.validation.model.ResourceProperty)3 ArrayList (java.util.ArrayList)2 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)2 ValueMap (org.apache.sling.api.resource.ValueMap)2 Before (org.junit.Before)2 List (java.util.List)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Matcher (java.util.regex.Matcher)1 Nonnull (javax.annotation.Nonnull)1 Session (javax.jcr.Session)1 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)1 ValueMapDecorator (org.apache.sling.api.wrappers.ValueMapDecorator)1