Search in sources :

Example 1 with ResourcePropertyBuilder

use of org.apache.sling.validation.impl.model.ResourcePropertyBuilder 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 2 with ResourcePropertyBuilder

use of org.apache.sling.validation.impl.model.ResourcePropertyBuilder in project sling by apache.

the class ResourceValidationModelProviderImplTest method setUp.

@Before
public void setUp() throws LoginException, PersistenceException, RepositoryException {
    primaryTypeUnstructuredMap = new HashMap<String, Object>();
    primaryTypeUnstructuredMap.put(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED);
    modelProvider = new ResourceValidationModelProviderImpl();
    // one default model
    modelBuilder = new ValidationModelBuilder();
    modelBuilder.setApplicablePath("/content/site1");
    ResourcePropertyBuilder propertyBuilder = new ResourcePropertyBuilder();
    propertyBuilder.validator("validatorId", 10, RegexValidator.REGEX_PARAM, "prefix.*");
    ResourceProperty property = propertyBuilder.build("field1");
    modelBuilder.resourceProperty(property);
    prefixBasedResultHandler = new MockQueryResultHandler() {

        @Override
        public MockQueryResult executeQuery(MockQuery query) {
            if (!"xpath".equals(query.getLanguage())) {
                return null;
            }
            String statement = query.getStatement();
            // @validatingResourceType="<some-resource-type>"]
            if (statement.startsWith("/jcr:root/")) {
                statement = statement.substring("/jcr:root/".length() - 1);
            }
            // extract the prefix from the statement
            String prefix = Text.getAbsoluteParent(statement, 0);
            // extract the resource type from the statement
            Matcher matcher = RESOURCE_TYPE_PATTERN.matcher(statement);
            if (!matcher.matches()) {
                throw new IllegalArgumentException("Can only process query statements which contain a validatedResourceType but the statement is: " + statement);
            }
            String resourceType = matcher.group(1);
            PrefixAndResourceType prefixAndResourceType = new PrefixAndResourceType(prefix, resourceType);
            if (validatorModelNodesPerPrefixAndResourceType.keySet().contains(prefixAndResourceType)) {
                return new MockQueryResult(validatorModelNodesPerPrefixAndResourceType.get(prefixAndResourceType));
            }
            return null;
        }
    };
    rr = context.resourceResolver();
    modelProvider.rrf = resourceResolverFactory;
    // create a wrapper resource resolver, which cannot be closed (as the SlingContext will take care of that)
    ResourceResolver nonClosableResourceResolverWrapper = Mockito.spy(rr);
    // intercept all close calls
    Mockito.doNothing().when(nonClosableResourceResolverWrapper).close();
    // always use the context's resource resolver (because we never commit)
    Mockito.when(resourceResolverFactory.getServiceResourceResolver(Mockito.anyObject())).thenReturn(nonClosableResourceResolverWrapper);
    MockJcr.addQueryResultHandler(rr.adaptTo(Session.class), prefixBasedResultHandler);
    validatorModelNodesPerPrefixAndResourceType = new HashMap<PrefixAndResourceType, List<Node>>();
    appsValidatorsRoot = ResourceUtil.getOrCreateResource(rr, APPS + "/" + VALIDATION_MODELS_RELATIVE_PATH, (Map<String, Object>) null, "sling:Folder", true);
    libsValidatorsRoot = ResourceUtil.getOrCreateResource(rr, LIBS + "/" + VALIDATION_MODELS_RELATIVE_PATH, (Map<String, Object>) null, "sling:Folder", true);
}
Also used : Matcher(java.util.regex.Matcher) ResourcePropertyBuilder(org.apache.sling.validation.impl.model.ResourcePropertyBuilder) MockQueryResultHandler(org.apache.sling.testing.mock.jcr.MockQueryResultHandler) ResourceProperty(org.apache.sling.validation.model.ResourceProperty) MockQueryResult(org.apache.sling.testing.mock.jcr.MockQueryResult) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) ArrayList(java.util.ArrayList) List(java.util.List) MockQuery(org.apache.sling.testing.mock.jcr.MockQuery) ValueMap(org.apache.sling.api.resource.ValueMap) HashMap(java.util.HashMap) Map(java.util.Map) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) ValidationModelBuilder(org.apache.sling.validation.impl.model.ValidationModelBuilder) Session(javax.jcr.Session) Before(org.junit.Before)

Example 3 with ResourcePropertyBuilder

use of org.apache.sling.validation.impl.model.ResourcePropertyBuilder 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 4 with ResourcePropertyBuilder

use of org.apache.sling.validation.impl.model.ResourcePropertyBuilder 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 5 with ResourcePropertyBuilder

use of org.apache.sling.validation.impl.model.ResourcePropertyBuilder 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)));
}
Also used : ValidationModel(org.apache.sling.validation.model.ValidationModel) HashMap(java.util.HashMap) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) DefaultValidationFailure(org.apache.sling.validation.spi.support.DefaultValidationFailure) ResourcePropertyBuilder(org.apache.sling.validation.impl.model.ResourcePropertyBuilder) DefaultValidationResult(org.apache.sling.validation.spi.support.DefaultValidationResult) ValidationResult(org.apache.sling.validation.ValidationResult) Test(org.junit.Test)

Aggregations

ResourcePropertyBuilder (org.apache.sling.validation.impl.model.ResourcePropertyBuilder)11 ValidationModel (org.apache.sling.validation.model.ValidationModel)8 Test (org.junit.Test)8 ValidationModelBuilder (org.apache.sling.validation.impl.model.ValidationModelBuilder)6 HashMap (java.util.HashMap)4 ChildResource (org.apache.sling.validation.model.ChildResource)4 Resource (org.apache.sling.api.resource.Resource)3 ResourceProperty (org.apache.sling.validation.model.ResourceProperty)3 ArrayList (java.util.ArrayList)2 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)2 ValueMap (org.apache.sling.api.resource.ValueMap)2 Before (org.junit.Before)2 List (java.util.List)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Matcher (java.util.regex.Matcher)1 Nonnull (javax.annotation.Nonnull)1 Session (javax.jcr.Session)1 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)1 ValueMapDecorator (org.apache.sling.api.wrappers.ValueMapDecorator)1