Search in sources :

Example 1 with ChildResource

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

the class ValidationServiceImpl method validateChildren.

/**
     * Validates a child resource with the help of the given {@code ChildResource} entry from the validation model
     * @param resource
     * @param relativePath relativePath of the resource (must be empty or end with "/")
     * @param result
     * @param childResources
     */
private void validateChildren(@Nonnull Resource resource, @Nonnull String relativePath, @Nonnull Collection<ChildResource> childResources, @Nonnull CompositeValidationResult result, @Nonnull ResourceBundle defaultResourceBundle) {
    // validate children resources, if any
    for (ChildResource childResource : childResources) {
        // if a pattern is set we validate all children matching that pattern
        Pattern pattern = childResource.getNamePattern();
        if (pattern != null) {
            boolean foundMatch = false;
            for (Resource child : resource.getChildren()) {
                Matcher matcher = pattern.matcher(child.getName());
                if (matcher.matches()) {
                    validateChildResource(child, relativePath, childResource, result, defaultResourceBundle);
                    foundMatch = true;
                }
            }
            if (!foundMatch && childResource.isRequired()) {
                result.addFailure(relativePath, configuration.defaultSeverity(), defaultResourceBundle, I18N_KEY_MISSING_REQUIRED_CHILD_RESOURCE_MATCHING_PATTERN, pattern.toString());
            }
        } else {
            Resource expectedResource = resource.getChild(childResource.getName());
            if (expectedResource != null) {
                validateChildResource(expectedResource, relativePath, childResource, result, defaultResourceBundle);
            } else if (childResource.isRequired()) {
                result.addFailure(relativePath, configuration.defaultSeverity(), defaultResourceBundle, I18N_KEY_MISSING_REQUIRED_CHILD_RESOURCE_WITH_NAME, childResource.getName());
            }
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Resource(org.apache.sling.api.resource.Resource) ChildResource(org.apache.sling.validation.model.ChildResource) ChildResource(org.apache.sling.validation.model.ChildResource)

Example 2 with ChildResource

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

the class ValidationServiceImplTest method testSyntheticResource.

// see https://issues.apache.org/jira/browse/SLING-5749
@Test
public void testSyntheticResource() 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 SyntheticResource(rr, "someresource", "resourceType");
    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) 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) 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 3 with ChildResource

use of org.apache.sling.validation.model.ChildResource 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)

Example 4 with ChildResource

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

the class ValidationServiceImplTest method testResourceWithNestedChildren.

@Test
public void testResourceWithNestedChildren() 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", null, true, Collections.singletonList(property), Collections.<ChildResource>emptyList());
    ChildResource modelChild = new ChildResourceImpl("child", null, true, Collections.singletonList(property), Collections.singletonList(modelGrandChild));
    modelBuilder.childResource(modelChild);
    ValidationModel vm = modelBuilder.build("sometype", "some source");
    // create a resource
    ResourceResolver rr = context.resourceResolver();
    Resource testResource = ResourceUtil.getOrCreateResource(rr, "/content/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, "child", properties);
    rr.create(resourceChild, "grandchild", properties);
    ValidationResult vr = validationService.validate(testResource, vm);
    Assert.assertThat(vr.getFailures(), Matchers.hasSize(0));
    Assert.assertTrue(vr.isValid());
}
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) DefaultValidationResult(org.apache.sling.validation.spi.support.DefaultValidationResult) ValidationResult(org.apache.sling.validation.ValidationResult) Test(org.junit.Test)

Example 5 with ChildResource

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

the class ResourceValidationModelProviderImplTest method testGetValidationModelsWithChildren.

@Test
public void testGetValidationModelsWithChildren() throws Exception {
    // build two models manually (which are identical except for the applicable path)
    ResourcePropertyBuilder resourcePropertyBuilder = new ResourcePropertyBuilder();
    resourcePropertyBuilder.multiple();
    resourcePropertyBuilder.optional();
    ResourceProperty childproperty = resourcePropertyBuilder.build("child1property");
    modelBuilder.childResource(new ChildResourceImpl("child1", null, true, Collections.singletonList(childproperty), Collections.<ChildResource>emptyList()));
    ValidationModel model1 = modelBuilder.build("sling/validation/test", libsValidatorsRoot.getPath() + "/testValidationModel1");
    // build models in JCR
    createValidationModelResource(rr, libsValidatorsRoot.getPath(), "testValidationModel1", model1);
    // compare both models
    Collection<ValidationModel> models = modelProvider.getValidationModels("sling/validation/test");
    Assert.assertThat(models, Matchers.contains(model1));
}
Also used : ResourceProperty(org.apache.sling.validation.model.ResourceProperty) ValidationModel(org.apache.sling.validation.model.ValidationModel) ChildResourceImpl(org.apache.sling.validation.impl.model.ChildResourceImpl) ChildResource(org.apache.sling.validation.model.ChildResource) ResourcePropertyBuilder(org.apache.sling.validation.impl.model.ResourcePropertyBuilder) Test(org.junit.Test)

Aggregations

ChildResource (org.apache.sling.validation.model.ChildResource)11 Resource (org.apache.sling.api.resource.Resource)10 ChildResourceImpl (org.apache.sling.validation.impl.model.ChildResourceImpl)8 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)7 ResourceProperty (org.apache.sling.validation.model.ResourceProperty)7 ValidationModel (org.apache.sling.validation.model.ValidationModel)7 Test (org.junit.Test)7 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)6 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)6 ValidationResult (org.apache.sling.validation.ValidationResult)6 DefaultValidationResult (org.apache.sling.validation.spi.support.DefaultValidationResult)6 HashMap (java.util.HashMap)4 DefaultValidationFailure (org.apache.sling.validation.spi.support.DefaultValidationFailure)4 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)2 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Nonnull (javax.annotation.Nonnull)1 Node (javax.jcr.Node)1 ValueMap (org.apache.sling.api.resource.ValueMap)1