use of org.apache.sling.validation.model.ResourceProperty 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());
}
use of org.apache.sling.validation.model.ResourceProperty 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));
}
use of org.apache.sling.validation.model.ResourceProperty 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.*")));
}
use of org.apache.sling.validation.model.ResourceProperty 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.model.ResourceProperty 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