Search in sources :

Example 26 with ValidationModel

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

the class ModifyUserServlet method doPost.

@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
    ValueMap requestParameters = request.adaptTo(ValueMap.class);
    String[] resourceTypeValues = requestParameters.get("sling:resourceType", String[].class);
    String resourceType = null;
    if (resourceTypeValues != null && resourceTypeValues.length > 0) {
        resourceType = resourceTypeValues[0];
    }
    if (resourceType != null && !"".equals(resourceType)) {
        String resourcePath = request.getRequestPathInfo().getResourcePath();
        ValidationModel vm = validationService.getValidationModel(resourceType, resourcePath, false);
        if (vm != null) {
            ValidationResult vr = validationService.validate(requestParameters, vm);
            if (vr.isValid()) {
                RequestDispatcherOptions options = new RequestDispatcherOptions();
                options.setReplaceSelectors(" ");
                request.getRequestDispatcher(request.getResource(), options).forward(request, response);
            } else {
                response.setContentType("application/json");
                JSONWriter writer = new JSONWriter(response.getWriter());
                writer.object();
                writer.key("success").value(false);
                writer.key("failures").array();
                for (ValidationFailure failure : vr.getFailures()) {
                    writer.object();
                    writer.key("message").value(failure.getMessage(request.getResourceBundle(Locale.US)));
                    writer.key("location").value(failure.getLocation());
                    writer.endObject();
                }
                writer.endArray();
                writer.endObject();
                response.setStatus(400);
            }
        }
    }
}
Also used : JSONWriter(org.apache.felix.utils.json.JSONWriter) ValidationModel(org.apache.sling.validation.model.ValidationModel) RequestDispatcherOptions(org.apache.sling.api.request.RequestDispatcherOptions) ValueMap(org.apache.sling.api.resource.ValueMap) ValidationResult(org.apache.sling.validation.ValidationResult) ValidationFailure(org.apache.sling.validation.ValidationFailure)

Example 27 with ValidationModel

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

the class ValidationModelRetrieverImplTest method testGetModelWithMultipleProvidersLowerRanking.

@Test
public void testGetModelWithMultipleProvidersLowerRanking() {
    ValidationModelProvider modelProvider2 = new TestModelProvider("source2");
    validationModelRetriever.modelProviders.clear();
    validationModelRetriever.modelProviders.add(modelProvider2);
    validationModelRetriever.modelProviders.add(modelProvider);
    // each provider must return the same applicable path but different
    applicablePathPerResourceType.put("test/type", "/content/site1");
    ValidationModel model = validationModelRetriever.getValidationModel("test/type", "/content/site1/somepage", false);
    Assert.assertNotNull(model);
    Assert.assertEquals("source1", model.getSource());
}
Also used : ValidationModel(org.apache.sling.validation.model.ValidationModel) ValidationModelProvider(org.apache.sling.validation.model.spi.ValidationModelProvider) Test(org.junit.Test)

Example 28 with ValidationModel

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

the class ValidationModelRetrieverImplTest method testGetModelWithNullApplicablePathPath.

@Test
public void testGetModelWithNullApplicablePathPath() {
    applicablePathPerResourceType.put("test/type", "/content/site1");
    applicablePathPerResourceType.put("test/type", "");
    applicablePathPerResourceType.put("test/type", "/content/site1/subnode");
    ValidationModel model = validationModelRetriever.getValidationModel("test/type", null, false);
    Assert.assertNotNull(model);
    Assert.assertThat(model.getApplicablePaths(), Matchers.contains(""));
}
Also used : ValidationModel(org.apache.sling.validation.model.ValidationModel) Test(org.junit.Test)

Example 29 with ValidationModel

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

the class ValidationModelRetrieverImplTest method testGetModelWithResourceInheritanceAndNoSuitableBaseModelFound.

@Test
public void testGetModelWithResourceInheritanceAndNoSuitableBaseModelFound() {
    // no model found for base type and no resource super type set
    ValidationModel model = validationModelRetriever.getValidationModel("test/type", "/content/site1", true);
    Assert.assertNull("Found model although no model has been specified", model);
    // set super super type
    Mockito.when(resourceResolver.getParentResourceType("test/type")).thenReturn("test/supertype");
    // no model found at all (neither base nor super type)
    model = validationModelRetriever.getValidationModel("test/type", "/content/site1", true);
    Assert.assertNull("Found model although no model has been specified (neither in base nor in super type)", model);
    // only supertype has model being set
    applicablePathPerResourceType.put("test/supertype", "/content/site1");
    model = validationModelRetriever.getValidationModel("test/type", "/content/site1", true);
    Assert.assertNotNull(model);
    Assert.assertThat(model.getResourceProperties(), Matchers.contains(new ResourcePropertyNameMatcher("test/supertype")));
}
Also used : ValidationModel(org.apache.sling.validation.model.ValidationModel) ResourcePropertyNameMatcher(org.apache.sling.validation.impl.util.ResourcePropertyNameMatcher) Test(org.junit.Test)

Example 30 with ValidationModel

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

the class ValidationServiceImplTest method testResourceWithPropertyPatternMatching.

@Test
public void testResourceWithPropertyPatternMatching() throws Exception {
    // accept any digits
    propertyBuilder.validator(REGEX_VALIDATOR_ID, 1, RegexValidator.REGEX_PARAM, "\\d");
    propertyBuilder.nameRegex("field.*");
    modelBuilder.resourceProperty(propertyBuilder.build("field"));
    propertyBuilder.nameRegex("otherfield.*");
    modelBuilder.resourceProperty(propertyBuilder.build("otherfield"));
    propertyBuilder.nameRegex("optionalfield.*").optional();
    modelBuilder.resourceProperty(propertyBuilder.build("optionalfield"));
    ValidationModel vm = modelBuilder.build("type", "some source");
    // create a resource
    ResourceResolver rr = context.resourceResolver();
    Resource testResource = ResourceUtil.getOrCreateResource(rr, "/content/validation/1/resource", JcrConstants.NT_UNSTRUCTURED, JcrConstants.NT_UNSTRUCTURED, true);
    ModifiableValueMap mvm = testResource.adaptTo(ModifiableValueMap.class);
    mvm.put("field1", "1");
    mvm.put("field2", "1");
    // does not validate
    mvm.put("field3", "abc");
    // otherfield.* property is missing
    ValidationResult vr = validationService.validate(testResource, vm);
    Assert.assertFalse("resource should have been considered invalid", vr.isValid());
    Assert.assertThat(vr.getFailures(), Matchers.<ValidationFailure>contains(new DefaultValidationFailure("field3", 1, defaultResourceBundle, RegexValidator.I18N_KEY_PATTERN_DOES_NOT_MATCH, "\\d"), new DefaultValidationFailure("", 20, defaultResourceBundle, ValidationServiceImpl.I18N_KEY_MISSING_REQUIRED_PROPERTY_MATCHING_PATTERN, "otherfield.*")));
}
Also used : ValidationModel(org.apache.sling.validation.model.ValidationModel) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource) ChildResource(org.apache.sling.validation.model.ChildResource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) DefaultValidationFailure(org.apache.sling.validation.spi.support.DefaultValidationFailure) DefaultValidationResult(org.apache.sling.validation.spi.support.DefaultValidationResult) ValidationResult(org.apache.sling.validation.ValidationResult) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) 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