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);
}
}
}
}
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());
}
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(""));
}
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")));
}
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.*")));
}
Aggregations