use of org.apache.sling.validation.impl.model.ChildResourceImpl 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")));
}
use of org.apache.sling.validation.impl.model.ChildResourceImpl 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;
}
use of org.apache.sling.validation.impl.model.ChildResourceImpl in project sling by apache.
the class ValidationServiceImplTest method testResourceWithMissingOptionalChildResource.
@Test
public void testResourceWithMissingOptionalChildResource() throws Exception {
// accept any digits
propertyBuilder.validator(REGEX_VALIDATOR_ID, 0, RegexValidator.REGEX_PARAM, "\\d");
ResourceProperty property = propertyBuilder.build("field1");
ChildResource child = new ChildResourceImpl("child", null, false, Collections.singletonList(property), Collections.<ChildResource>emptyList());
modelBuilder.childResource(child);
ValidationModel vm = modelBuilder.build("type", "some source");
// create a resource (lacking the optional "child" sub resource)
ResourceResolver rr = context.resourceResolver();
Resource testResource = ResourceUtil.getOrCreateResource(rr, "/content/validation/1/resource", JcrConstants.NT_UNSTRUCTURED, JcrConstants.NT_UNSTRUCTURED, true);
ValidationResult vr = validationService.validate(testResource, vm);
Assert.assertThat(vr.getFailures(), Matchers.hasSize(0));
Assert.assertTrue(vr.isValid());
}
Aggregations