use of org.apache.sling.validation.ValidationResult in project sling by apache.
the class ValidationServiceImplTest method testValueMapWithMissingOptionalValue.
@Test()
public void testValueMapWithMissingOptionalValue() throws Exception {
modelBuilder.resourceProperty(propertyBuilder.optional().build("field1"));
ValidationModel vm = modelBuilder.build("sling/validation/test", "some source");
HashMap<String, Object> hashMap = new HashMap<String, Object>();
hashMap.put("field2", "1");
ValidationResult vr = validationService.validate(new ValueMapDecorator(hashMap), vm);
Assert.assertThat(vr.getFailures(), Matchers.hasSize(0));
Assert.assertTrue(vr.isValid());
}
use of org.apache.sling.validation.ValidationResult in project sling by apache.
the class ValidationServiceImplTest method testValueMapWithCorrectDataType.
@Test
public void testValueMapWithCorrectDataType() throws Exception {
propertyBuilder.validator(REGEX_VALIDATOR_ID, 0, RegexValidator.REGEX_PARAM, "abc");
modelBuilder.resourceProperty(propertyBuilder.build("field1"));
propertyBuilder = new ResourcePropertyBuilder();
final String TEST_REGEX = "^test$";
propertyBuilder.validator(REGEX_VALIDATOR_ID, 0, RegexValidator.REGEX_PARAM, TEST_REGEX);
modelBuilder.resourceProperty(propertyBuilder.build("field2"));
ValidationModel vm = modelBuilder.build("sling/validation/test", "some source");
HashMap<String, Object> hashMap = new HashMap<String, Object>();
hashMap.put("field1", "HelloWorld");
hashMap.put("field2", "HelloWorld");
ValidationResult vr = validationService.validate(new ValueMapDecorator(hashMap), vm);
Assert.assertFalse(vr.isValid());
Assert.assertThat(vr.getFailures(), Matchers.<ValidationFailure>hasItem(new DefaultValidationFailure("field2", 0, defaultResourceBundle, RegexValidator.I18N_KEY_PATTERN_DOES_NOT_MATCH, TEST_REGEX)));
}
use of org.apache.sling.validation.ValidationResult in project sling by apache.
the class ValidationServiceImplTest method testValidateResourceRecursivelyOnNonExistingResource.
// see https://issues.apache.org/jira/browse/SLING-5674
@Test
public void testValidateResourceRecursivelyOnNonExistingResource() throws Exception {
ResourceResolver rr = context.resourceResolver();
Resource nonExistingResource = new NonExistingResource(rr, "non-existing-resource");
ValidationResult vr = validationService.validateResourceRecursively(nonExistingResource, true, null, true);
Assert.assertTrue("resource should have been considered valid", vr.isValid());
}
use of org.apache.sling.validation.ValidationResult 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.ValidationResult in project sling by apache.
the class ResourceToValidationResultAdapterFactoryTest method testResourceToValidationResultAdaption.
@Test
public void testResourceToValidationResultAdaption() {
final ValidationResult result = mock(ValidationResult.class);
when(result.isValid()).thenReturn(true);
when(result.getFailures()).thenReturn(Collections.emptyList());
final Resource resource = mock(Resource.class);
final ResourceMetadata metadata = mock(ResourceMetadata.class);
when(metadata.get("sling.validation.result")).thenReturn(result);
when(resource.getResourceMetadata()).thenReturn(metadata);
final ResourceToValidationResultAdapterFactory factory = new ResourceToValidationResultAdapterFactory();
final ValidationResult adapter = factory.getAdapter(resource, ValidationResult.class);
assertThat(adapter, is(result));
}
Aggregations