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());
}
}
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"))));
}
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"));
}
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());
}
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")));
}
Aggregations