Search in sources :

Example 16 with ValidationResult

use of org.apache.sling.validation.ValidationResult in project sling by apache.

the class ValidationPostOperation method doRun.

@Override
protected void doRun(SlingHttpServletRequest request, PostResponse response, List<Modification> changes) {
    if (response instanceof ValidationPostResponse) {
        final Map<String, Object> base = new LinkedHashMap<>();
        final ValueMapDecorator valueMap = new ValueMapDecorator(base);
        final Enumeration<String> names = request.getParameterNames();
        while (names.hasMoreElements()) {
            final String name = names.nextElement();
            valueMap.put(name, request.getRequestParameter(name).getString());
        }
        final String resourceType = request.getRequestParameter("sling:resourceType").getString();
        if (resourceType != null && !"".equals(resourceType)) {
            final String resourcePath = request.getRequestPathInfo().getResourcePath();
            final ValidationModel validationModel = validationService.getValidationModel(resourceType, resourcePath, false);
            if (validationModel != null) {
                final ValidationResult validationResult = validationService.validate(valueMap, validationModel);
                final ValidationPostResponse validationPostResponse = (ValidationPostResponse) response;
                validationPostResponse.setValidationResult(validationResult);
            } else {
                logger.error("No validation model for resourceType {} and resourcePath {} ", resourceType, resourcePath);
            }
        } else {
            logger.error("resource type is empty");
        }
    }
}
Also used : ValidationModel(org.apache.sling.validation.model.ValidationModel) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) ValidationResult(org.apache.sling.validation.ValidationResult) LinkedHashMap(java.util.LinkedHashMap)

Example 17 with ValidationResult

use of org.apache.sling.validation.ValidationResult in project sling by apache.

the class ValidationServiceImplTest method testResourceWithMultivalueProperties.

@Test
public void testResourceWithMultivalueProperties() throws Exception {
    // accept any digits
    propertyBuilder.validator(REGEX_VALIDATOR_ID, 0, RegexValidator.REGEX_PARAM, "\\d");
    propertyBuilder.multiple();
    modelBuilder.resourceProperty(propertyBuilder.build("field"));
    ValidationModel vm = modelBuilder.build("type", "some source");
    ResourceResolver rr = context.resourceResolver();
    Resource testResource = ResourceUtil.getOrCreateResource(rr, "/content/validation/1/resource", JcrConstants.NT_UNSTRUCTURED, JcrConstants.NT_UNSTRUCTURED, true);
    ModifiableValueMap mvm = testResource.adaptTo(ModifiableValueMap.class);
    mvm.put("field", new String[] { "1", "abc", "2" });
    ValidationResult vr = validationService.validate(testResource, vm);
    Assert.assertFalse("resource should have been considered invalid", vr.isValid());
    Assert.assertThat(vr.getFailures(), Matchers.<ValidationFailure>contains(new DefaultValidationFailure("field[1]", 0, defaultResourceBundle, RegexValidator.I18N_KEY_PATTERN_DOES_NOT_MATCH, "\\d")));
}
Also used : ValidationModel(org.apache.sling.validation.model.ValidationModel) 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) DefaultValidationFailure(org.apache.sling.validation.spi.support.DefaultValidationFailure) DefaultValidationResult(org.apache.sling.validation.spi.support.DefaultValidationResult) ValidationResult(org.apache.sling.validation.ValidationResult) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Test(org.junit.Test)

Example 18 with ValidationResult

use of org.apache.sling.validation.ValidationResult in project sling by apache.

the class ValidationServiceImplTest method testValueMapWithWrongDataType.

@Test()
public void testValueMapWithWrongDataType() throws Exception {
    propertyBuilder.validator(DATE_VALIDATOR_ID, 10);
    modelBuilder.resourceProperty(propertyBuilder.build("field1"));
    ValidationModel vm = modelBuilder.build("sling/validation/test", "some source");
    HashMap<String, Object> hashMap = new HashMap<String, Object>();
    hashMap.put("field1", "1");
    ValidationResult vr = validationService.validate(new ValueMapDecorator(hashMap), vm);
    Assert.assertThat(vr.getFailures(), Matchers.<ValidationFailure>contains(new DefaultValidationFailure("field1", 10, defaultResourceBundle, ValidationServiceImpl.I18N_KEY_WRONG_PROPERTY_TYPE, Date.class)));
}
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) DefaultValidationResult(org.apache.sling.validation.spi.support.DefaultValidationResult) ValidationResult(org.apache.sling.validation.ValidationResult) Test(org.junit.Test)

Example 19 with ValidationResult

use of org.apache.sling.validation.ValidationResult in project sling by apache.

the class ValidationServiceImplTest method testResourceWithNestedChildrenAndPatternMatching.

@Test
public void testResourceWithNestedChildrenAndPatternMatching() throws Exception {
    // accept any digits
    propertyBuilder.validator(REGEX_VALIDATOR_ID, 0, RegexValidator.REGEX_PARAM, "\\d");
    ResourceProperty property = propertyBuilder.build("field1");
    ChildResource modelGrandChild = new ChildResourceImpl("grandchild", "grandchild.*", true, Collections.singletonList(property), Collections.<ChildResource>emptyList());
    ChildResource modelChild = new ChildResourceImpl("child", "child.*", true, Collections.singletonList(property), Collections.singletonList(modelGrandChild));
    ChildResource siblingChild = new ChildResourceImpl("siblingchild", "siblingchild.*", true, Collections.singletonList(property), Collections.singletonList(modelGrandChild));
    modelBuilder.childResource(modelChild);
    modelBuilder.childResource(siblingChild);
    ValidationModel vm = modelBuilder.build("sometype", "some source");
    ResourceResolver rr = context.resourceResolver();
    Resource testResource = ResourceUtil.getOrCreateResource(rr, "/apps/validation/1/resource", JcrConstants.NT_UNSTRUCTURED, JcrConstants.NT_UNSTRUCTURED, true);
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put("field1", "1");
    Resource resourceChild = rr.create(testResource, "child1", properties);
    rr.create(resourceChild, "grandchild1", properties);
    // child2 is lacking its mandatory sub resource
    rr.create(testResource, "child2", properties);
    rr.create(testResource, "child3", null);
    // sibling child is not there at all (although mandatory)
    ValidationResult vr = validationService.validate(testResource, vm);
    Assert.assertFalse("resource should have been considered invalid", vr.isValid());
    Assert.assertThat(vr.getFailures(), Matchers.<ValidationFailure>containsInAnyOrder(new DefaultValidationFailure("child2", 20, defaultResourceBundle, ValidationServiceImpl.I18N_KEY_MISSING_REQUIRED_CHILD_RESOURCE_MATCHING_PATTERN, "grandchild.*"), new DefaultValidationFailure("child3", 20, defaultResourceBundle, ValidationServiceImpl.I18N_KEY_MISSING_REQUIRED_CHILD_RESOURCE_MATCHING_PATTERN, "grandchild.*"), new DefaultValidationFailure("child3", 20, defaultResourceBundle, ValidationServiceImpl.I18N_KEY_MISSING_REQUIRED_PROPERTY_WITH_NAME, "field1"), new DefaultValidationFailure("", 20, defaultResourceBundle, ValidationServiceImpl.I18N_KEY_MISSING_REQUIRED_CHILD_RESOURCE_MATCHING_PATTERN, "siblingchild.*")));
}
Also used : ResourceProperty(org.apache.sling.validation.model.ResourceProperty) ValidationModel(org.apache.sling.validation.model.ValidationModel) HashMap(java.util.HashMap) 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)

Example 20 with ValidationResult

use of org.apache.sling.validation.ValidationResult in project sling by apache.

the class ValidationServiceImplTest method testValidateResourceRecursivelyWithMissingValidatorAndNoEnforcement.

@Test()
public void testValidateResourceRecursivelyWithMissingValidatorAndNoEnforcement() throws Exception {
    // set model retriever which never retrieves anything
    validationService.modelRetriever = new ValidationModelRetriever() {

        @Override
        @CheckForNull
        public ValidationModel getValidationModel(@Nonnull String resourceType, String resourcePath, boolean considerResourceSuperTypeModels) {
            return null;
        }
    };
    ResourceResolver rr = context.resourceResolver();
    // resource is having no connected validation model
    Resource testResource = ResourceUtil.getOrCreateResource(rr, "/content/validation/1/resource", "resourcetype1", JcrConstants.NT_UNSTRUCTURED, true);
    ValidationResult vr = validationService.validateResourceRecursively(testResource, false, null, false);
    Assert.assertTrue(vr.isValid());
}
Also used : ValidationModel(org.apache.sling.validation.model.ValidationModel) ValidationModelRetriever(org.apache.sling.validation.model.spi.ValidationModelRetriever) CheckForNull(javax.annotation.CheckForNull) 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)

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