Search in sources :

Example 16 with ValidationModel

use of org.apache.sling.validation.model.ValidationModel in project sling by apache.

the class ValidationPostProcessor method process.

@Override
public void process(SlingHttpServletRequest request, List<Modification> changes) throws Exception {
    // is this globally disabled?
    if (configuration.disabled()) {
        LOG.debug("ValidationPostProcessor globally disabled!");
        return;
    }
    String path = request.getResource().getPath();
    if (enabledForPath(path)) {
        LOG.debug("ValidationPostProcessor is enabled for path {}", path);
    } else {
        LOG.debug("ValidationPostProcessor is not enabled for path {}", path);
        return;
    }
    // request.getResource() contains the old resource (might even be the non-existing one), 
    // therefore retrieve the transient new resource at the same path
    Resource newResource = request.getResourceResolver().getResource(request.getResource().getPath());
    if (newResource == null) {
        LOG.debug("Could not find new/modified resource at {} to validate", request.getResource().getPath());
        return;
    }
    // get model for resource type
    ValidationModel model = validationService.getValidationModel(newResource, configuration.considerResourceSuperTypes());
    if (model == null) {
        if (configuration.failForMissingValidationModels()) {
            throw new IllegalStateException("Could not find validation model for resource type " + newResource.getResourceType());
        } else {
            LOG.debug("Could not find validation model for resource type {} -> skip validation", newResource.getResourceType());
            return;
        }
    }
    ValidationResult validationResult = validationService.validate(newResource, model);
    if (!validationResult.isValid()) {
        throw new InvalidResourcePostProcessorException(validationResult, request.getResourceBundle(null));
    } else {
        LOG.debug("Successfully validated modified/created resource at '{}'", request.getResource().getPath());
    }
}
Also used : ValidationModel(org.apache.sling.validation.model.ValidationModel) Resource(org.apache.sling.api.resource.Resource) ValidationResult(org.apache.sling.validation.ValidationResult)

Example 17 with ValidationModel

use of org.apache.sling.validation.model.ValidationModel in project sling by apache.

the class ValidationModelRetrieverImplTest method testGetModelWithResourceInheritance.

@Test
public void testGetModelWithResourceInheritance() {
    // in case no super type is known, just return model
    applicablePathPerResourceType.put("test/type", "/content/site1");
    ValidationModel model = validationModelRetriever.getValidationModel("test/type", "/content/site1", true);
    Assert.assertNotNull(model);
    Assert.assertThat(model.getResourceProperties(), Matchers.contains(new ResourcePropertyNameMatcher("test/type")));
    // in case there is one super type make sure the merged model is returned!
    Mockito.when(resourceResolver.getParentResourceType("test/type")).thenReturn("test/supertype");
    applicablePathPerResourceType.put("test/supertype", "/content/site1");
    model = validationModelRetriever.getValidationModel("test/type", "/content/site1", true);
    Assert.assertNotNull(model);
    Assert.assertThat(model.getResourceProperties(), Matchers.<ResourceProperty>containsInAnyOrder(Arrays.asList(new ResourcePropertyNameMatcher("test/type"), new ResourcePropertyNameMatcher("test/supertype"))));
}
Also used : ValidationModel(org.apache.sling.validation.model.ValidationModel) ResourcePropertyNameMatcher(org.apache.sling.validation.impl.util.ResourcePropertyNameMatcher) Test(org.junit.Test)

Example 18 with ValidationModel

use of org.apache.sling.validation.model.ValidationModel in project sling by apache.

the class ValidationModelRetrieverImplTest method testGetModelWithExactlyMatchingApplicablePath.

@Test
public void testGetModelWithExactlyMatchingApplicablePath() {
    applicablePathPerResourceType.put("test/type", "/content/site1/subnode/test/somepage");
    applicablePathPerResourceType.put("test/type", "/content/site1/subnode/test/somepage/");
    ValidationModel model = validationModelRetriever.getValidationModel("test/type", "/content/site1/subnode/test/somepage", false);
    Assert.assertNotNull(model);
    Assert.assertThat(model.getApplicablePaths(), Matchers.contains("/content/site1/subnode/test/somepage"));
}
Also used : ValidationModel(org.apache.sling.validation.model.ValidationModel) Test(org.junit.Test)

Example 19 with ValidationModel

use of org.apache.sling.validation.model.ValidationModel in project sling by apache.

the class ValidationModelRetrieverImplTest method testGetModelWithMultipleProvidersHigherRanking.

@Test
public void testGetModelWithMultipleProvidersHigherRanking() {
    ValidationModelProvider modelProvider2 = new TestModelProvider("source2");
    validationModelRetriever.modelProviders.clear();
    validationModelRetriever.modelProviders.add(modelProvider);
    validationModelRetriever.modelProviders.add(modelProvider2);
    // each provider must return the same applicable path but different
    applicablePathPerResourceType.put("test/type", "/content/site1");
    ValidationModel model = validationModelRetriever.getValidationModel("test/type", "/content/site1/somepage", false);
    Assert.assertNotNull(model);
    Assert.assertEquals("source2", model.getSource());
}
Also used : ValidationModel(org.apache.sling.validation.model.ValidationModel) ValidationModelProvider(org.apache.sling.validation.model.spi.ValidationModelProvider) Test(org.junit.Test)

Example 20 with ValidationModel

use of org.apache.sling.validation.model.ValidationModel in project sling by apache.

the class ValidationServiceImplTest method testNonExistingResource.

// see https://issues.apache.org/jira/browse/SLING-5674
@Test
public void testNonExistingResource() throws Exception {
    // accept any digits
    propertyBuilder.validator(REGEX_VALIDATOR_ID, 0, RegexValidator.REGEX_PARAM, "\\d");
    ResourceProperty property = propertyBuilder.build("field1");
    modelBuilder.resourceProperty(property);
    ChildResource modelChild = new ChildResourceImpl("child", null, true, Collections.singletonList(property), Collections.emptyList());
    modelBuilder.childResource(modelChild);
    modelChild = new ChildResourceImpl("optionalChild", null, false, Collections.singletonList(property), Collections.emptyList());
    modelBuilder.childResource(modelChild);
    ValidationModel vm = modelBuilder.build("sometype", "some source");
    ResourceResolver rr = context.resourceResolver();
    Resource nonExistingResource = new NonExistingResource(rr, "non-existing-resource");
    ValidationResult vr = validationService.validate(nonExistingResource, vm);
    Assert.assertFalse("resource should have been considered invalid", vr.isValid());
    Assert.assertThat(vr.getFailures(), Matchers.<ValidationFailure>containsInAnyOrder(new DefaultValidationFailure("", 20, defaultResourceBundle, ValidationServiceImpl.I18N_KEY_MISSING_REQUIRED_PROPERTY_WITH_NAME, "field1"), new DefaultValidationFailure("", 20, defaultResourceBundle, ValidationServiceImpl.I18N_KEY_MISSING_REQUIRED_CHILD_RESOURCE_WITH_NAME, "child")));
}
Also used : ResourceProperty(org.apache.sling.validation.model.ResourceProperty) ValidationModel(org.apache.sling.validation.model.ValidationModel) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) ChildResourceImpl(org.apache.sling.validation.impl.model.ChildResourceImpl) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource) ChildResource(org.apache.sling.validation.model.ChildResource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) ChildResource(org.apache.sling.validation.model.ChildResource) DefaultValidationFailure(org.apache.sling.validation.spi.support.DefaultValidationFailure) DefaultValidationResult(org.apache.sling.validation.spi.support.DefaultValidationResult) ValidationResult(org.apache.sling.validation.ValidationResult) Test(org.junit.Test)

Aggregations

ValidationModel (org.apache.sling.validation.model.ValidationModel)53 Test (org.junit.Test)43 ValidationResult (org.apache.sling.validation.ValidationResult)22 Resource (org.apache.sling.api.resource.Resource)20 ChildResource (org.apache.sling.validation.model.ChildResource)20 DefaultValidationResult (org.apache.sling.validation.spi.support.DefaultValidationResult)17 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)15 HashMap (java.util.HashMap)13 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)12 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)12 DefaultValidationFailure (org.apache.sling.validation.spi.support.DefaultValidationFailure)11 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)9 ValueMapDecorator (org.apache.sling.api.wrappers.ValueMapDecorator)9 ResourcePropertyBuilder (org.apache.sling.validation.impl.model.ResourcePropertyBuilder)8 ChildResourceImpl (org.apache.sling.validation.impl.model.ChildResourceImpl)7 ResourceProperty (org.apache.sling.validation.model.ResourceProperty)7 ValidationModelBuilder (org.apache.sling.validation.impl.model.ValidationModelBuilder)6 CheckForNull (javax.annotation.CheckForNull)5 Nonnull (javax.annotation.Nonnull)5 ValueMap (org.apache.sling.api.resource.ValueMap)4