use of org.apache.sling.validation.spi.support.DefaultValidationFailure in project sling by apache.
the class ValidationServiceImplTest method testValidateResourceRecursively.
@Test()
public void testValidateResourceRecursively() throws Exception {
modelBuilder.resourceProperty(propertyBuilder.build("field1"));
final ValidationModel vm1 = modelBuilder.build("resourcetype1", "some source");
modelBuilder = new ValidationModelBuilder();
modelBuilder.resourceProperty(propertyBuilder.build("field2"));
final ValidationModel vm2 = modelBuilder.build("resourcetype2", "some source");
// set model retriever
validationService.modelRetriever = new ValidationModelRetriever() {
@Override
@CheckForNull
public ValidationModel getValidationModel(@Nonnull String resourceType, String resourcePath, boolean considerResourceSuperTypeModels) {
if (resourceType.equals("resourcetype1")) {
return vm1;
} else if (resourceType.equals("resourcetype2")) {
return vm2;
} else {
return null;
}
}
};
ResourceResolver rr = context.resourceResolver();
// resource is lacking the required field (is invalid)
Resource testResource = ResourceUtil.getOrCreateResource(rr, "/content/validation/1/resource", "resourcetype1", JcrConstants.NT_UNSTRUCTURED, true);
// child1 is valid
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY, "resourcetype2");
properties.put("field2", "test");
rr.create(testResource, "child1", properties);
// child2 is invalid
properties.clear();
properties.put(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY, "resourcetype2");
rr.create(testResource, "child2", properties);
// child3 has no model (but its resource type is ignored)
properties.clear();
properties.put(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY, "resourcetype3");
rr.create(testResource, "child3", properties);
final Predicate<Resource> ignoreResourceType3Filter = new Predicate<Resource>() {
@Override
public boolean test(final Resource resource) {
return !"resourcetype3".equals(resource.getResourceType());
}
};
ValidationResult vr = validationService.validateResourceRecursively(testResource, true, ignoreResourceType3Filter, false);
Assert.assertFalse("resource should have been considered invalid", vr.isValid());
Assert.assertThat(vr.getFailures(), Matchers.<ValidationFailure>contains(new DefaultValidationFailure("", 20, defaultResourceBundle, ValidationServiceImpl.I18N_KEY_MISSING_REQUIRED_PROPERTY_WITH_NAME, "field1"), new DefaultValidationFailure("child2", 20, defaultResourceBundle, ValidationServiceImpl.I18N_KEY_MISSING_REQUIRED_PROPERTY_WITH_NAME, "field2")));
}
use of org.apache.sling.validation.spi.support.DefaultValidationFailure 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")));
}
use of org.apache.sling.validation.spi.support.DefaultValidationFailure in project sling by apache.
the class ValidationServiceImplTest method testValueMapWithCorrectDataType.
@Test
public void testValueMapWithCorrectDataType() throws Exception {
propertyBuilder.validator(REGEX_VALIDATOR_ID, 0, RegexValidator.REGEX_PARAM, "abc");
modelBuilder.resourceProperty(propertyBuilder.build("field1"));
propertyBuilder = new ResourcePropertyBuilder();
final String TEST_REGEX = "^test$";
propertyBuilder.validator(REGEX_VALIDATOR_ID, 0, RegexValidator.REGEX_PARAM, TEST_REGEX);
modelBuilder.resourceProperty(propertyBuilder.build("field2"));
ValidationModel vm = modelBuilder.build("sling/validation/test", "some source");
HashMap<String, Object> hashMap = new HashMap<String, Object>();
hashMap.put("field1", "HelloWorld");
hashMap.put("field2", "HelloWorld");
ValidationResult vr = validationService.validate(new ValueMapDecorator(hashMap), vm);
Assert.assertFalse(vr.isValid());
Assert.assertThat(vr.getFailures(), Matchers.<ValidationFailure>hasItem(new DefaultValidationFailure("field2", 0, defaultResourceBundle, RegexValidator.I18N_KEY_PATTERN_DOES_NOT_MATCH, TEST_REGEX)));
}
use of org.apache.sling.validation.spi.support.DefaultValidationFailure 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")));
}
use of org.apache.sling.validation.spi.support.DefaultValidationFailure in project sling by apache.
the class ValidationServiceImplTest method testResourceWithPropertyPatternMatching.
@Test
public void testResourceWithPropertyPatternMatching() throws Exception {
// accept any digits
propertyBuilder.validator(REGEX_VALIDATOR_ID, 1, RegexValidator.REGEX_PARAM, "\\d");
propertyBuilder.nameRegex("field.*");
modelBuilder.resourceProperty(propertyBuilder.build("field"));
propertyBuilder.nameRegex("otherfield.*");
modelBuilder.resourceProperty(propertyBuilder.build("otherfield"));
propertyBuilder.nameRegex("optionalfield.*").optional();
modelBuilder.resourceProperty(propertyBuilder.build("optionalfield"));
ValidationModel vm = modelBuilder.build("type", "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");
mvm.put("field2", "1");
// does not validate
mvm.put("field3", "abc");
// otherfield.* property is missing
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("field3", 1, defaultResourceBundle, RegexValidator.I18N_KEY_PATTERN_DOES_NOT_MATCH, "\\d"), new DefaultValidationFailure("", 20, defaultResourceBundle, ValidationServiceImpl.I18N_KEY_MISSING_REQUIRED_PROPERTY_MATCHING_PATTERN, "otherfield.*")));
}
Aggregations