use of org.apache.sling.validation.model.ValidationModel in project sling by apache.
the class ValidationServiceImplTest method testValueMapWithEmptyOptionalValue.
@Test()
public void testValueMapWithEmptyOptionalValue() throws Exception {
propertyBuilder.optional();
propertyBuilder.validator(REGEX_VALIDATOR_ID, null, RegexValidator.REGEX_PARAM, "abc");
modelBuilder.resourceProperty(propertyBuilder.build("field1"));
ValidationModel vm = modelBuilder.build("sling/validation/test", "some source");
HashMap<String, Object> hashMap = new HashMap<String, Object>();
hashMap.put("field1", "");
ValidationResult vr = validationService.validate(new ValueMapDecorator(hashMap), vm);
Assert.assertFalse(vr.isValid());
Assert.assertThat(vr.getFailures(), Matchers.<ValidationFailure>contains(new DefaultValidationFailure("field1", 10, defaultResourceBundle, RegexValidator.I18N_KEY_PATTERN_DOES_NOT_MATCH, "abc")));
}
use of org.apache.sling.validation.model.ValidationModel 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());
}
use of org.apache.sling.validation.model.ValidationModel in project sling by apache.
the class ValidationServiceImplTest method testResourceWithValidatorLeveragingTheResource.
@Test
public void testResourceWithValidatorLeveragingTheResource() throws Exception {
Validator<String> extendedValidator = new Validator<String>() {
@Override
@Nonnull
public ValidationResult validate(@Nonnull String data, @Nonnull ValidatorContext context, @Nonnull ValueMap arguments) throws SlingValidationException {
Resource resource = context.getResource();
if (resource == null) {
Assert.fail("Resource must not be null");
} else {
Assert.assertThat(resource.getPath(), Matchers.equalTo("/content/validation/1/resource"));
}
return DefaultValidationResult.VALID;
}
};
// register validator
validationService.validatorMap.put("myid", extendedValidator, newValidatorServiceReference, null);
// accept any digits
propertyBuilder.validator("myid", null);
modelBuilder.resourceProperty(propertyBuilder.build("field1"));
ValidationModel vm = modelBuilder.build("sometype", "some source");
// create a resource
ResourceResolver rr = context.resourceResolver();
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("field1", "1");
Resource testResource = ResourceUtil.getOrCreateResource(rr, "/content/validation/1/resource", properties, JcrConstants.NT_UNSTRUCTURED, true);
ValidationResult vr = validationService.validate(testResource, vm);
Assert.assertTrue(vr.isValid());
}
use of org.apache.sling.validation.model.ValidationModel in project sling by apache.
the class ValidationServiceImplTest method testValueMapWithMissingField.
@Test()
public void testValueMapWithMissingField() throws Exception {
modelBuilder.resourceProperty(propertyBuilder.build("field1"));
modelBuilder.resourceProperty(propertyBuilder.build("field2"));
modelBuilder.resourceProperty(propertyBuilder.build("field3"));
modelBuilder.resourceProperty(propertyBuilder.build("field4"));
ValidationModel vm = modelBuilder.build("sling/validation/test", "some source");
// this should not be detected as missing property
HashMap<String, Object> hashMap = new HashMap<String, Object>();
hashMap.put("field1", new String[] {});
hashMap.put("field2", new String[] { "null" });
hashMap.put("field3", "");
ValidationResult vr = validationService.validate(new ValueMapDecorator(hashMap), vm);
Assert.assertThat(vr.getFailures(), Matchers.<ValidationFailure>contains(new DefaultValidationFailure("", 20, defaultResourceBundle, ValidationServiceImpl.I18N_KEY_MISSING_REQUIRED_PROPERTY_WITH_NAME, "field4")));
}
use of org.apache.sling.validation.model.ValidationModel in project sling by apache.
the class ResourceValidationModelProviderImplTest method testGetValidationModelsWithInvalidValidatorArguments1.
@Test(expected = IllegalStateException.class)
public void testGetValidationModelsWithInvalidValidatorArguments1() throws Exception {
// create a model with neither children nor properties
ValidationModel model1 = modelBuilder.build("sling/validation/test", libsValidatorsRoot.getPath() + "/testValidationModel1");
// create valid model first
Resource modelResource = createValidationModelResource(rr, libsValidatorsRoot.getPath(), "testValidationModel1", model1);
// and make parametrization of validator invalid afterwards
Resource validatorResource = modelResource.getChild("properties/field1/validators/validatorId");
ModifiableValueMap validatorArguments = validatorResource.adaptTo(ModifiableValueMap.class);
// value without "="
validatorArguments.put("validatorArguments", "key1");
Collection<ValidationModel> models = modelProvider.getValidationModels("sling/validation/test");
Assert.assertThat(models, Matchers.contains(model1));
}
Aggregations