use of org.apache.sling.validation.model.ValidationModel in project sling by apache.
the class ResourceValidationModelProviderImplTest method testGetValidationModelsWithEmptyApplicablePath.
@Test
public void testGetValidationModelsWithEmptyApplicablePath() throws Exception {
modelBuilder = new ValidationModelBuilder();
// build two models manually (which are identical except for the applicable path)
ResourcePropertyBuilder resourcePropertyBuilder = new ResourcePropertyBuilder();
ValidationModel model1 = modelBuilder.resourceProperty(resourcePropertyBuilder.build("property1")).build("sling/validation/test", libsValidatorsRoot.getPath() + "/testValidationModel1");
// build models in JCR
createValidationModelResource(rr, libsValidatorsRoot.getPath(), "testValidationModel1", model1);
// check that both models are returned
Collection<ValidationModel> models = modelProvider.getValidationModels("sling/validation/test");
Assert.assertThat(models, Matchers.containsInAnyOrder(model1));
}
use of org.apache.sling.validation.model.ValidationModel in project sling by apache.
the class ResourceValidationModelProviderImplTest method testGetValidationModelsWithComplexValidatorArguments.
@Test
public void testGetValidationModelsWithComplexValidatorArguments() throws Exception {
// create a model with neither children nor properties
Map<String, Object> validatorArguments = new HashMap<>();
validatorArguments.put("key1", "value1");
validatorArguments.put("key1", "value2");
validatorArguments.put("key1", "value3");
validatorArguments.put("key2", "value1");
validatorArguments.put("key3", "value1=value2");
modelBuilder = new ValidationModelBuilder();
modelBuilder.resourceProperty(new ResourcePropertyBuilder().validator("validatorId", 10, validatorArguments).build("field1"));
modelBuilder.addApplicablePath("content/site1");
ValidationModel model1 = modelBuilder.build("sling/validation/test", libsValidatorsRoot.getPath() + "/testValidationModel1");
createValidationModelResource(rr, libsValidatorsRoot.getPath(), "testValidationModel1", model1);
Collection<ValidationModel> models = modelProvider.getValidationModels("sling/validation/test");
Assert.assertThat(models, Matchers.contains(model1));
}
use of org.apache.sling.validation.model.ValidationModel in project sling by apache.
the class ResourceValidationModelProviderImplTest method testGetValidationModelsWithoutApplicablePath.
@Test
public void testGetValidationModelsWithoutApplicablePath() throws Exception {
modelBuilder = new ValidationModelBuilder();
// build two models manually (which are identical except for the applicable path)
ResourcePropertyBuilder resourcePropertyBuilder = new ResourcePropertyBuilder();
ValidationModel model1 = modelBuilder.resourceProperty(resourcePropertyBuilder.build("property1")).build("sling/validation/test", libsValidatorsRoot.getPath() + "/testValidationModel1");
// build models in JCR
Resource modelResource = createValidationModelResource(rr, libsValidatorsRoot.getPath(), "testValidationModel1", model1);
ModifiableValueMap properties = modelResource.adaptTo(ModifiableValueMap.class);
properties.remove("applicablePaths");
// check that both models are returned
Collection<ValidationModel> models = modelProvider.getValidationModels("sling/validation/test");
Assert.assertThat(models, Matchers.containsInAnyOrder(model1));
}
use of org.apache.sling.validation.model.ValidationModel in project sling by apache.
the class ResourceValidationModelProviderImplTest method testGetValidationModelsOutsideSearchPath.
@Test
public void testGetValidationModelsOutsideSearchPath() throws Exception {
// build two models manually (which are identical except for the applicable path)
ValidationModel model1 = modelBuilder.build("sling/validation/test", "some source");
Resource contentValidatorsRoot = ResourceUtil.getOrCreateResource(rr, "/content", (Map<String, Object>) null, "sling:Folder", true);
try {
// build models in JCR outside any search path /apps or /libs
createValidationModelResource(rr, contentValidatorsRoot.getPath(), "testValidationModel1", model1);
// check that no model is found
Collection<ValidationModel> models = modelProvider.getValidationModels("sling/validation/test");
Assert.assertThat("Model was placed outside resource resolver search path but still found", models, Matchers.empty());
} finally {
rr.delete(contentValidatorsRoot);
}
}
use of org.apache.sling.validation.model.ValidationModel in project sling by apache.
the class ValidationServiceImplTest method testValidateNeverCalledWithNullValues.
@Test
public void testValidateNeverCalledWithNullValues() throws Exception {
Validator<String> myValidator = new Validator<String>() {
@Override
@Nonnull
public ValidationResult validate(@Nonnull String data, @Nonnull ValidatorContext context, @Nonnull ValueMap arguments) throws SlingValidationException {
Assert.assertNotNull("data parameter for validate should never be null", data);
Assert.assertNotNull("location of context parameter for validate should never be null", context.getLocation());
Assert.assertNotNull("valueMap of context parameter for validate should never be null", context.getValueMap());
Assert.assertNull("resource of context parameter for validate cannot be set if validate was called only with a value map", context.getResource());
Assert.assertNotNull("arguments parameter for validate should never be null", arguments);
return DefaultValidationResult.VALID;
}
};
validationService.validatorMap.put("someId", myValidator, validatorServiceReference, 10);
propertyBuilder.validator("someId", 20);
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", "1");
ValidationResult vr = validationService.validate(new ValueMapDecorator(hashMap), vm);
Assert.assertThat(vr.getFailures(), Matchers.hasSize(0));
Assert.assertTrue(vr.isValid());
}
Aggregations