Search in sources :

Example 6 with ValidationModel

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));
}
Also used : ValidationModel(org.apache.sling.validation.model.ValidationModel) ResourcePropertyBuilder(org.apache.sling.validation.impl.model.ResourcePropertyBuilder) ValidationModelBuilder(org.apache.sling.validation.impl.model.ValidationModelBuilder) Test(org.junit.Test)

Example 7 with ValidationModel

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));
}
Also used : ValidationModel(org.apache.sling.validation.model.ValidationModel) HashMap(java.util.HashMap) ResourcePropertyBuilder(org.apache.sling.validation.impl.model.ResourcePropertyBuilder) ValidationModelBuilder(org.apache.sling.validation.impl.model.ValidationModelBuilder) Test(org.junit.Test)

Example 8 with ValidationModel

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));
}
Also used : ValidationModel(org.apache.sling.validation.model.ValidationModel) Resource(org.apache.sling.api.resource.Resource) ChildResource(org.apache.sling.validation.model.ChildResource) ResourcePropertyBuilder(org.apache.sling.validation.impl.model.ResourcePropertyBuilder) ValidationModelBuilder(org.apache.sling.validation.impl.model.ValidationModelBuilder) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Test(org.junit.Test)

Example 9 with ValidationModel

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);
    }
}
Also used : ValidationModel(org.apache.sling.validation.model.ValidationModel) Resource(org.apache.sling.api.resource.Resource) ChildResource(org.apache.sling.validation.model.ChildResource) Test(org.junit.Test)

Example 10 with ValidationModel

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());
}
Also used : ValidationModel(org.apache.sling.validation.model.ValidationModel) Nonnull(javax.annotation.Nonnull) ValidatorContext(org.apache.sling.validation.spi.ValidatorContext) HashMap(java.util.HashMap) ValueMap(org.apache.sling.api.resource.ValueMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) DefaultValidationResult(org.apache.sling.validation.spi.support.DefaultValidationResult) ValidationResult(org.apache.sling.validation.ValidationResult) DateValidator(org.apache.sling.validation.impl.util.examplevalidators.DateValidator) RegexValidator(org.apache.sling.validation.impl.validators.RegexValidator) Validator(org.apache.sling.validation.spi.Validator) Test(org.junit.Test)

Aggregations

ValidationModel (org.apache.sling.validation.model.ValidationModel)53 Test (org.junit.Test)43 ValidationResult (org.apache.sling.validation.ValidationResult)22 Resource (org.apache.sling.api.resource.Resource)20 ChildResource (org.apache.sling.validation.model.ChildResource)20 DefaultValidationResult (org.apache.sling.validation.spi.support.DefaultValidationResult)17 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)15 HashMap (java.util.HashMap)13 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)12 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)12 DefaultValidationFailure (org.apache.sling.validation.spi.support.DefaultValidationFailure)11 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)9 ValueMapDecorator (org.apache.sling.api.wrappers.ValueMapDecorator)9 ResourcePropertyBuilder (org.apache.sling.validation.impl.model.ResourcePropertyBuilder)8 ChildResourceImpl (org.apache.sling.validation.impl.model.ChildResourceImpl)7 ResourceProperty (org.apache.sling.validation.model.ResourceProperty)7 ValidationModelBuilder (org.apache.sling.validation.impl.model.ValidationModelBuilder)6 CheckForNull (javax.annotation.CheckForNull)5 Nonnull (javax.annotation.Nonnull)5 ValueMap (org.apache.sling.api.resource.ValueMap)4