Search in sources :

Example 41 with ValidationModel

use of org.apache.sling.validation.model.ValidationModel in project sling by apache.

the class ValidationModelRetrieverImpl method fillTrieForResourceType.

@Nonnull
private PatriciaTrie<ValidationModel> fillTrieForResourceType(@Nonnull String resourceType) {
    // create a new (empty) trie
    PatriciaTrie<ValidationModel> modelsForResourceType = new PatriciaTrie<ValidationModel>();
    // lowest ranked model provider inserts first (i.e. higher ranked should overwrite)
    for (ValidationModelProvider modelProvider : modelProviders) {
        LOG.debug("Retrieving validation models with resource type {} from provider {}...", resourceType, modelProvider.getClass().getName());
        List<ValidationModel> models = modelProvider.getValidationModels(resourceType);
        for (ValidationModel model : models) {
            for (String applicablePath : model.getApplicablePaths()) {
                LOG.debug("Found validation model for resource type {} for applicable path {}", resourceType, applicablePath);
                modelsForResourceType.put(applicablePath, model);
            }
        }
        if (models.isEmpty()) {
            LOG.debug("Found no validation model with resource type {} from provider {}", resourceType, modelProvider.getClass().getName());
        }
    }
    return modelsForResourceType;
}
Also used : MergedValidationModel(org.apache.sling.validation.impl.model.MergedValidationModel) ValidationModel(org.apache.sling.validation.model.ValidationModel) PatriciaTrie(org.apache.commons.collections4.trie.PatriciaTrie) ValidationModelProvider(org.apache.sling.validation.model.spi.ValidationModelProvider) Nonnull(javax.annotation.Nonnull)

Example 42 with ValidationModel

use of org.apache.sling.validation.model.ValidationModel in project sling by apache.

the class MergedValidationModelTest method testLessSpecificApplicationPathInModelToMerge.

@Test
public void testLessSpecificApplicationPathInModelToMerge() {
    modelBuilder.addApplicablePath("/base/path").addApplicablePath("/base/path2");
    ValidationModel baseValidationModel = modelBuilder.build("base", "some source");
    modelBuilder.setApplicablePath("/base");
    ValidationModel mergedModel = new MergedValidationModel(baseValidationModel, modelBuilder.build("superType", "some source"));
    Assert.assertThat(mergedModel.getApplicablePaths(), Matchers.contains("/base/path", "/base/path2"));
}
Also used : ValidationModel(org.apache.sling.validation.model.ValidationModel) Test(org.junit.Test)

Example 43 with ValidationModel

use of org.apache.sling.validation.model.ValidationModel in project sling by apache.

the class MergedValidationModelTest method testValidatedResourceTypes.

@Test
public void testValidatedResourceTypes() {
    ValidationModel mergedModel = new MergedValidationModel(modelBuilder.build("base", "some source"), modelBuilder.build("superType", "some source"));
    Assert.assertThat(mergedModel.getValidatingResourceType(), Matchers.equalTo("base"));
}
Also used : ValidationModel(org.apache.sling.validation.model.ValidationModel) Test(org.junit.Test)

Example 44 with ValidationModel

use of org.apache.sling.validation.model.ValidationModel in project sling by apache.

the class ResourceValidationModelProviderImplTest method testGetValidationModelsWithInvalidValidatorArguments2.

@Test(expected = IllegalStateException.class)
public void testGetValidationModelsWithInvalidValidatorArguments2() 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);
    // starting with "="
    validatorArguments.put("validatorArguments", "=value2");
    Collection<ValidationModel> models = modelProvider.getValidationModels("sling/validation/test");
    Assert.assertThat(models, Matchers.contains(model1));
}
Also used : ValidationModel(org.apache.sling.validation.model.ValidationModel) Resource(org.apache.sling.api.resource.Resource) ChildResource(org.apache.sling.validation.model.ChildResource) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Test(org.junit.Test)

Example 45 with ValidationModel

use of org.apache.sling.validation.model.ValidationModel in project sling by apache.

the class ValidationModelRetrieverImplTest method testGetModel.

@Test
public void testGetModel() {
    applicablePathPerResourceType.put("test/type", "/content/site1");
    applicablePathPerResourceType.put("test/type", "/content/site1/subnode/test");
    applicablePathPerResourceType.put("test/type", "/content/site1/subnode");
    applicablePathPerResourceType.put("test/type", "/content/site1/subnode/test/somepage/within");
    applicablePathPerResourceType.put("test/type", "/content/site1/subnode/test/testoutside");
    ValidationModel model = validationModelRetriever.getValidationModel("test/type", "/content/site1/subnode/test/somepage", false);
    Assert.assertNotNull(model);
    Assert.assertThat(model.getApplicablePaths(), Matchers.contains("/content/site1/subnode/test"));
}
Also used : ValidationModel(org.apache.sling.validation.model.ValidationModel) 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