Search in sources :

Example 6 with ChildResource

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

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

the class ValidationServiceImplTest method testResourceWithMissingGrandChildProperty.

@Test
public void testResourceWithMissingGrandChildProperty() throws Exception {
    // accept any digits
    propertyBuilder.validator(REGEX_VALIDATOR_ID, 0, RegexValidator.REGEX_PARAM, "\\d");
    ResourceProperty property = propertyBuilder.build("field1");
    modelBuilder.resourceProperty(property);
    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);
    ModifiableValueMap mvm = testResource.adaptTo(ModifiableValueMap.class);
    mvm.put("field1", "1");
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put("field1", "1");
    Resource resourceChild = rr.create(testResource, "child", properties);
    // resourceGrandChild is missing the mandatory field1 property
    rr.create(resourceChild, "grandchild", null);
    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("child/grandchild", 20, defaultResourceBundle, ValidationServiceImpl.I18N_KEY_MISSING_REQUIRED_PROPERTY_WITH_NAME, "field1")));
}
Also used : HashMap(java.util.HashMap) 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) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) 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) DefaultValidationFailure(org.apache.sling.validation.spi.support.DefaultValidationFailure) Test(org.junit.Test)

Example 8 with ChildResource

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

the class ResourceValidationModelProviderImpl method buildChildren.

/** Searches children resources from a {@code modelResource}, starting from the {@code rootResource}. If one needs all the children
     * resources of a model, then the {@code modelResource} and the {@code rootResource} should be identical.
     *
     * @param modelResource the resource describing a {@link org.apache.sling.validation.api.ValidationModel}
     * @param rootResource the model's resource from which to search for children (this resource has to have a
     *            {@link ResourceValidationModelProviderImpl#CHILDREN} node directly underneath it)
     * @return a list of all the children resources; the list will be empty if there are no children resources */
@Nonnull
private List<ChildResource> buildChildren(@Nonnull Resource modelResource, @Nonnull Resource rootResource) {
    List<ChildResource> children = new ArrayList<ChildResource>();
    Resource childrenResource = rootResource.getChild(ResourceValidationModelProviderImpl.CHILDREN);
    if (childrenResource != null) {
        for (Resource child : childrenResource.getChildren()) {
            // if pattern is set, always use that
            ValueMap childrenProperties = child.adaptTo(ValueMap.class);
            if (childrenProperties == null) {
                throw new IllegalStateException("Could not adapt resource " + child.getPath() + " to ValueMap");
            }
            final String name = child.getName();
            final String nameRegex;
            if (childrenProperties.containsKey(ResourceValidationModelProviderImpl.NAME_REGEX)) {
                nameRegex = childrenProperties.get(ResourceValidationModelProviderImpl.NAME_REGEX, String.class);
            } else {
                // otherwise fall back to the name
                nameRegex = null;
            }
            boolean isRequired = !childrenProperties.get(ResourceValidationModelProviderImpl.OPTIONAL, false);
            ChildResource childResource = new ChildResourceImpl(name, nameRegex, isRequired, buildProperties(child.getChild(ResourceValidationModelProviderImpl.PROPERTIES)), buildChildren(modelResource, child));
            children.add(childResource);
        }
    }
    return children;
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) ArrayList(java.util.ArrayList) Resource(org.apache.sling.api.resource.Resource) ChildResource(org.apache.sling.validation.model.ChildResource) ChildResourceImpl(org.apache.sling.validation.impl.model.ChildResourceImpl) ChildResource(org.apache.sling.validation.model.ChildResource) Nonnull(javax.annotation.Nonnull)

Example 9 with ChildResource

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

the class ResourceValidationModelProviderImplTest method createValidationModelResource.

/*--- the following methods create validation model resources from ValidationModel objects --*/
private Resource createValidationModelResource(ResourceResolver rr, String root, String name, ValidationModel model) throws Exception {
    Map<String, Object> modelProperties = new HashMap<String, Object>();
    modelProperties.put(ResourceValidationModelProviderImpl.VALIDATING_RESOURCE_TYPE, model.getValidatingResourceType());
    modelProperties.put(ResourceValidationModelProviderImpl.APPLICABLE_PATHS, model.getApplicablePaths().toArray());
    modelProperties.put(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY, ResourceValidationModelProviderImpl.VALIDATION_MODEL_RESOURCE_TYPE);
    modelProperties.put(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED);
    Resource modelResource = ResourceUtil.getOrCreateResource(rr, root + "/" + name, modelProperties, JcrResourceConstants.NT_SLING_FOLDER, true);
    if (model != null) {
        createValidationModelProperties(modelResource, model.getResourceProperties());
        for (ChildResource child : model.getChildren()) {
            createValidationModelChildResource(modelResource, child);
        }
        // add to search handler (with root path)
        String prefix = Text.getAbsoluteParent(root, 0);
        PrefixAndResourceType prefixAndResourceType = new PrefixAndResourceType(prefix, model.getValidatingResourceType());
        List<Node> nodes;
        nodes = validatorModelNodesPerPrefixAndResourceType.get(prefixAndResourceType);
        if (nodes == null) {
            nodes = new ArrayList<Node>();
            validatorModelNodesPerPrefixAndResourceType.put(prefixAndResourceType, nodes);
        }
        nodes.add(modelResource.adaptTo(Node.class));
    }
    return modelResource;
}
Also used : HashMap(java.util.HashMap) Node(javax.jcr.Node) Resource(org.apache.sling.api.resource.Resource) ChildResource(org.apache.sling.validation.model.ChildResource) ChildResource(org.apache.sling.validation.model.ChildResource)

Example 10 with ChildResource

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

the class ResourceValidationModelProviderImplTest method createValidationModelChildResource.

private Resource createValidationModelChildResource(Resource parentResource, ChildResource child) throws PersistenceException {
    ResourceResolver rr = parentResource.getResourceResolver();
    Resource modelChildren = rr.create(parentResource, ResourceValidationModelProviderImpl.CHILDREN, primaryTypeUnstructuredMap);
    Resource modelResource = rr.create(modelChildren, child.getName(), primaryTypeUnstructuredMap);
    ModifiableValueMap mvm = modelResource.adaptTo(ModifiableValueMap.class);
    if (child.getNamePattern() != null) {
        mvm.put(ResourceValidationModelProviderImpl.NAME_REGEX, child.getNamePattern());
    }
    mvm.put(ResourceValidationModelProviderImpl.OPTIONAL, !child.isRequired());
    createValidationModelProperties(modelResource, child.getProperties());
    // recursion for all childs
    for (ChildResource grandChild : child.getChildren()) {
        createValidationModelChildResource(modelResource, grandChild);
    }
    return modelResource;
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) ChildResource(org.apache.sling.validation.model.ChildResource) ChildResource(org.apache.sling.validation.model.ChildResource) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap)

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