Search in sources :

Example 6 with ValidationResult

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());
}
Also used : ValidationModel(org.apache.sling.validation.model.ValidationModel) HashMap(java.util.HashMap) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) DefaultValidationResult(org.apache.sling.validation.spi.support.DefaultValidationResult) ValidationResult(org.apache.sling.validation.ValidationResult) Test(org.junit.Test)

Example 7 with ValidationResult

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)));
}
Also used : ValidationModel(org.apache.sling.validation.model.ValidationModel) HashMap(java.util.HashMap) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) DefaultValidationFailure(org.apache.sling.validation.spi.support.DefaultValidationFailure) ResourcePropertyBuilder(org.apache.sling.validation.impl.model.ResourcePropertyBuilder) DefaultValidationResult(org.apache.sling.validation.spi.support.DefaultValidationResult) ValidationResult(org.apache.sling.validation.ValidationResult) Test(org.junit.Test)

Example 8 with ValidationResult

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());
}
Also used : NonExistingResource(org.apache.sling.api.resource.NonExistingResource) 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) DefaultValidationResult(org.apache.sling.validation.spi.support.DefaultValidationResult) ValidationResult(org.apache.sling.validation.ValidationResult) Test(org.junit.Test)

Example 9 with ValidationResult

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());
    }
}
Also used : ValidationModel(org.apache.sling.validation.model.ValidationModel) Resource(org.apache.sling.api.resource.Resource) ValidationResult(org.apache.sling.validation.ValidationResult)

Example 10 with ValidationResult

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));
}
Also used : Resource(org.apache.sling.api.resource.Resource) ValidationResult(org.apache.sling.validation.ValidationResult) ResourceMetadata(org.apache.sling.api.resource.ResourceMetadata) Test(org.junit.Test)

Aggregations

ValidationResult (org.apache.sling.validation.ValidationResult)25 ValidationModel (org.apache.sling.validation.model.ValidationModel)22 Test (org.junit.Test)19 DefaultValidationResult (org.apache.sling.validation.spi.support.DefaultValidationResult)18 Resource (org.apache.sling.api.resource.Resource)14 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)13 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)12 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)12 ChildResource (org.apache.sling.validation.model.ChildResource)12 HashMap (java.util.HashMap)11 DefaultValidationFailure (org.apache.sling.validation.spi.support.DefaultValidationFailure)11 ValueMapDecorator (org.apache.sling.api.wrappers.ValueMapDecorator)8 ChildResourceImpl (org.apache.sling.validation.impl.model.ChildResourceImpl)6 ResourceProperty (org.apache.sling.validation.model.ResourceProperty)6 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)5 Nonnull (javax.annotation.Nonnull)3 ValueMap (org.apache.sling.api.resource.ValueMap)3 Validator (org.apache.sling.validation.spi.Validator)3 ValidatorContext (org.apache.sling.validation.spi.ValidatorContext)3 LinkedHashMap (java.util.LinkedHashMap)2