Search in sources :

Example 6 with ValidationModelBuilder

use of org.apache.sling.validation.impl.model.ValidationModelBuilder 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 7 with ValidationModelBuilder

use of org.apache.sling.validation.impl.model.ValidationModelBuilder 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 8 with ValidationModelBuilder

use of org.apache.sling.validation.impl.model.ValidationModelBuilder in project sling by apache.

the class ResourceValidationModelProviderImpl method doGetModels.

/** Searches for validation models bound to a specific resource type through a search query.
     *
     * @param relativeResourceType the resource type to look for
     * @return a List of {@link ValidationModel}s. Never {@code null}, but might be empty collection in case no model for the given resource
     *         type could be found. Returns the models below "/apps" before the models below "/libs".
     * @throws IllegalStateException in case a validation model is found but it is invalid */
@Nonnull
private List<ValidationModel> doGetModels(@Nonnull String relativeResourceType) {
    List<ValidationModel> validationModels = new ArrayList<ValidationModel>();
    ResourceResolver resourceResolver = null;
    try {
        resourceResolver = rrf.getServiceResourceResolver(null);
        String[] searchPaths = resourceResolver.getSearchPath();
        for (String searchPath : searchPaths) {
            final String queryString = String.format(MODEL_XPATH_QUERY, searchPath, relativeResourceType);
            LOG.debug("Looking for validation models with query '{}'", queryString);
            Iterator<Resource> models = resourceResolver.findResources(queryString, "xpath");
            while (models.hasNext()) {
                Resource model = models.next();
                LOG.debug("Found validation model resource {}.", model.getPath());
                String resourcePath = model.getPath();
                try {
                    ValidationModelBuilder modelBuilder = new ValidationModelBuilder();
                    ValueMap validationModelProperties = model.getValueMap();
                    modelBuilder.addApplicablePaths(validationModelProperties.get(ResourceValidationModelProviderImpl.APPLICABLE_PATHS, new String[] {}));
                    Resource propertiesResource = model.getChild(ResourceValidationModelProviderImpl.PROPERTIES);
                    modelBuilder.resourceProperties(buildProperties(propertiesResource));
                    modelBuilder.childResources(buildChildren(model, model));
                    ValidationModel vm = modelBuilder.build(relativeResourceType, resourcePath);
                    validationModels.add(vm);
                } catch (IllegalArgumentException e) {
                    throw new IllegalStateException("Found invalid validation model in '" + resourcePath + "': " + e.getMessage(), e);
                }
            }
            if (!validationModels.isEmpty()) {
                // the applicable content paths do not matter here!
                break;
            }
        }
        return validationModels;
    } catch (LoginException e) {
        throw new IllegalStateException("Could not get service resource resolver", e);
    } finally {
        if (resourceResolver != null) {
            resourceResolver.close();
        }
    }
}
Also used : 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) ValidationModel(org.apache.sling.validation.model.ValidationModel) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) LoginException(org.apache.sling.api.resource.LoginException) ValidationModelBuilder(org.apache.sling.validation.impl.model.ValidationModelBuilder) Nonnull(javax.annotation.Nonnull)

Aggregations

ValidationModelBuilder (org.apache.sling.validation.impl.model.ValidationModelBuilder)8 ResourcePropertyBuilder (org.apache.sling.validation.impl.model.ResourcePropertyBuilder)6 ValidationModel (org.apache.sling.validation.model.ValidationModel)6 Test (org.junit.Test)5 Resource (org.apache.sling.api.resource.Resource)4 ChildResource (org.apache.sling.validation.model.ChildResource)4 HashMap (java.util.HashMap)3 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)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 Predicate (java.util.function.Predicate)1 Matcher (java.util.regex.Matcher)1 CheckForNull (javax.annotation.CheckForNull)1 Nonnull (javax.annotation.Nonnull)1 Session (javax.jcr.Session)1 LoginException (org.apache.sling.api.resource.LoginException)1