sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From k...@apache.org
Subject svn commit: r1734530 - in /sling/trunk/bundles/extensions/validation: api/src/main/java/org/apache/sling/validation/ api/src/main/java/org/apache/sling/validation/model/ api/src/main/java/org/apache/sling/validation/spi/ core/src/main/java/org/apache/s...
Date Fri, 11 Mar 2016 11:13:32 GMT
Author: kwin
Date: Fri Mar 11 11:13:32 2016
New Revision: 1734530

URL: http://svn.apache.org/viewvc?rev=1734530&view=rev
Log:
SLING-5373 allow to set severity per validator in the model

Modified:
    sling/trunk/bundles/extensions/validation/api/src/main/java/org/apache/sling/validation/ValidationFailure.java
    sling/trunk/bundles/extensions/validation/api/src/main/java/org/apache/sling/validation/model/ParameterizedValidator.java
    sling/trunk/bundles/extensions/validation/api/src/main/java/org/apache/sling/validation/spi/DefaultValidationFailure.java
    sling/trunk/bundles/extensions/validation/api/src/main/java/org/apache/sling/validation/spi/DefaultValidationResult.java
    sling/trunk/bundles/extensions/validation/api/src/main/java/org/apache/sling/validation/spi/ValidationContext.java
    sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/CompositeValidationResult.java
    sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/ValidationContextImpl.java
    sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/ValidationServiceImpl.java
    sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/model/ParameterizedValidatorImpl.java
    sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/model/ResourcePropertyBuilder.java
    sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/resourcemodel/ResourceValidationModelProviderImpl.java
    sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/validators/RegexValidator.java
    sling/trunk/bundles/extensions/validation/core/src/test/java/org/apache/sling/validation/impl/ValidationServiceImplTest.java
    sling/trunk/bundles/extensions/validation/core/src/test/java/org/apache/sling/validation/impl/resourcemodel/ResourceValidationModelProviderImplTest.java
    sling/trunk/bundles/extensions/validation/it-http/pom.xml
    sling/trunk/bundles/extensions/validation/test-services/src/main/java/org/apache/sling/validation/testservices/ValidationPostResponse.java
    sling/trunk/bundles/extensions/validation/test-services/src/main/resources/SLING-CONTENT/apps/sling/validation/models/model1.json

Modified: sling/trunk/bundles/extensions/validation/api/src/main/java/org/apache/sling/validation/ValidationFailure.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/validation/api/src/main/java/org/apache/sling/validation/ValidationFailure.java?rev=1734530&r1=1734529&r2=1734530&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/validation/api/src/main/java/org/apache/sling/validation/ValidationFailure.java
(original)
+++ sling/trunk/bundles/extensions/validation/api/src/main/java/org/apache/sling/validation/ValidationFailure.java
Fri Mar 11 11:13:32 2016
@@ -32,7 +32,7 @@ public interface ValidationFailure {
      * @return the failure message
      */
     @Nonnull String getMessage(@Nonnull ResourceBundle resourceBundle);
-    
+
     /**
      * Returns the relative location of the property/resource/value which triggered this
validation failure.
      * The location 
@@ -44,4 +44,9 @@ public interface ValidationFailure {
      */
     @Nonnull String getLocation();
 
+    /**
+     * @return the severity of this validation failure. If no explicit severity was set either
in the validation model or in the validator, this returns {@code 0}.
+     */
+    int getSeverity();
+
 }

Modified: sling/trunk/bundles/extensions/validation/api/src/main/java/org/apache/sling/validation/model/ParameterizedValidator.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/validation/api/src/main/java/org/apache/sling/validation/model/ParameterizedValidator.java?rev=1734530&r1=1734529&r2=1734530&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/validation/api/src/main/java/org/apache/sling/validation/model/ParameterizedValidator.java
(original)
+++ sling/trunk/bundles/extensions/validation/api/src/main/java/org/apache/sling/validation/model/ParameterizedValidator.java
Fri Mar 11 11:13:32 2016
@@ -18,6 +18,7 @@
  */
 package org.apache.sling.validation.model;
 
+import javax.annotation.CheckForNull;
 import javax.annotation.Nonnull;
 
 import org.apache.sling.api.resource.ValueMap;
@@ -50,4 +51,10 @@ public interface ParameterizedValidator
      */
     @Nonnull Class<?> getType();
 
+    /**
+     *
+     * @return the severity of validation failures emitted by this validator. May be {@code
null} in case it is not specified.
+     */
+    @CheckForNull Integer getSeverity();
+
 }
\ No newline at end of file

Modified: sling/trunk/bundles/extensions/validation/api/src/main/java/org/apache/sling/validation/spi/DefaultValidationFailure.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/validation/api/src/main/java/org/apache/sling/validation/spi/DefaultValidationFailure.java?rev=1734530&r1=1734529&r2=1734530&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/validation/api/src/main/java/org/apache/sling/validation/spi/DefaultValidationFailure.java
(original)
+++ sling/trunk/bundles/extensions/validation/api/src/main/java/org/apache/sling/validation/spi/DefaultValidationFailure.java
Fri Mar 11 11:13:32 2016
@@ -35,16 +35,25 @@ public class DefaultValidationFailure im
     private final @Nonnull String location;
     private final @Nonnull String messageKey;
     private final Object[] messageArguments;
+    private final int severity;
+
+    public final static int DEFAULT_SEVERITY = 0;
 
     /**
      * Constructor of a validation failure. The message is constructed by looking up the
given messageKey from a resourceBundle.
      * and formatting it using the given messageArguments via {@link MessageFormat#format(String,
Object...)}.
      * @param location the location
+     * @param severity the severity of this failure (may be {@code null}), which leads to
setting it to the {@link #DEFAULT_SEVERITY}
      * @param messageKey the key to look up in the resource bundle
      * @param messageArguments the arguments to be used with the looked up value from the
resource bundle (given in {@link #getMessage(ResourceBundle)}
      */
-    public DefaultValidationFailure(@Nonnull String location, @Nonnull String messageKey,
Object... messageArguments) {
+    public DefaultValidationFailure(@Nonnull String location, Integer severity, @Nonnull
String messageKey, Object... messageArguments) {
         this.location = location;
+        if (severity != null) {
+            this.severity = severity;
+        } else {
+            this.severity = DEFAULT_SEVERITY;
+        }
         this.messageKey = messageKey;
         this.messageArguments = messageArguments;
     }
@@ -61,8 +70,14 @@ public class DefaultValidationFailure im
     }
 
     @Override
+    public int getSeverity() {
+        return severity;
+    }
+
+    @Override
     public String toString() {
-        return "DefaultValidationFailure [location=" + location + ", messageKey=" + messageKey
+ ", messageArguments=" + Arrays.toString(messageArguments) + "]";
+        return "DefaultValidationFailure [location=" + location + ", messageKey=" + messageKey
+ ", messageArguments="
+                + Arrays.toString(messageArguments) + ", severity=" + severity + "]";
     }
 
     @Override
@@ -72,6 +87,7 @@ public class DefaultValidationFailure im
         result = prime * result + ((location == null) ? 0 : location.hashCode());
         result = prime * result + Arrays.hashCode(messageArguments);
         result = prime * result + ((messageKey == null) ? 0 : messageKey.hashCode());
+        result = prime * result + severity;
         return result;
     }
 
@@ -96,6 +112,8 @@ public class DefaultValidationFailure im
                 return false;
         } else if (!messageKey.equals(other.messageKey))
             return false;
+        if (severity != other.severity)
+            return false;
         return true;
     }
 

Modified: sling/trunk/bundles/extensions/validation/api/src/main/java/org/apache/sling/validation/spi/DefaultValidationResult.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/validation/api/src/main/java/org/apache/sling/validation/spi/DefaultValidationResult.java?rev=1734530&r1=1734529&r2=1734530&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/validation/api/src/main/java/org/apache/sling/validation/spi/DefaultValidationResult.java
(original)
+++ sling/trunk/bundles/extensions/validation/api/src/main/java/org/apache/sling/validation/spi/DefaultValidationResult.java
Fri Mar 11 11:13:32 2016
@@ -42,12 +42,13 @@ public class DefaultValidationResult imp
      * Constructs a result with one failure message. The message is constructed by looking
up the given messageKey from a resourceBundle.
      * and formatting it using the given messageArguments via {@link MessageFormat#format(String,
Object...)}.
      * @param location the location.
+     * @param severity the severity of the embedded failure (may be {@code null}), which
leads to setting it to the {@link #DEFAULT_SEVERITY}.
      * @param messageKey the message key used for looking up a value in the resource bundle
given in {@link ValidationFailure#getMessage(java.util.ResourceBundle)}.
      * @param messageArguments optional number of arguments being used in {@link MessageFormat#format(String,
Object...)}
      */
-    public DefaultValidationResult(@Nonnull String location, @Nonnull String messageKey,
Object... messageArguments) {
+    public DefaultValidationResult(@Nonnull String location, Integer severity, @Nonnull String
messageKey, Object... messageArguments) {
         this.isValid = false;
-        this.failures = Collections.<ValidationFailure>singletonList(new DefaultValidationFailure(location,
messageKey, messageArguments));
+        this.failures = Collections.<ValidationFailure>singletonList(new DefaultValidationFailure(location,
severity, messageKey, messageArguments));
     }
 
     public DefaultValidationResult(ValidationFailure... failures) {

Modified: sling/trunk/bundles/extensions/validation/api/src/main/java/org/apache/sling/validation/spi/ValidationContext.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/validation/api/src/main/java/org/apache/sling/validation/spi/ValidationContext.java?rev=1734530&r1=1734529&r2=1734530&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/validation/api/src/main/java/org/apache/sling/validation/spi/ValidationContext.java
(original)
+++ sling/trunk/bundles/extensions/validation/api/src/main/java/org/apache/sling/validation/spi/ValidationContext.java
Fri Mar 11 11:13:32 2016
@@ -48,4 +48,10 @@ public interface ValidationContext {
      * @return the resource on which the validation was triggered. {@code null} in case the
validation was triggered on a {@link ValueMap} (via {@link ValidationService#validate(ValueMap,
org.apache.sling.validation.model.ValidationModel)}).
      */
     @CheckForNull Resource getResource();
+
+    /**
+     * Returns the severity to be issued for validation failures in this context.
+     * @return the severity of the validation failure. May be {@code null} when no explicit
severity has been set in the model.
+     */
+    @CheckForNull Integer getSeverity();
 }

Modified: sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/CompositeValidationResult.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/CompositeValidationResult.java?rev=1734530&r1=1734529&r2=1734530&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/CompositeValidationResult.java
(original)
+++ sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/CompositeValidationResult.java
Fri Mar 11 11:13:32 2016
@@ -43,8 +43,8 @@ public class CompositeValidationResult i
         results.add(result);
     }
 
-    public void addFailure(@Nonnull String location, @Nonnull String message, Object... messageArguments)
{
-        results.add(new DefaultValidationResult(location, message, messageArguments));
+    public void addFailure(@Nonnull String location, Integer severity, @Nonnull String message,
Object... messageArguments) {
+        results.add(new DefaultValidationResult(location, severity, message, messageArguments));
     }
 
     @Override

Modified: sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/ValidationContextImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/ValidationContextImpl.java?rev=1734530&r1=1734529&r2=1734530&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/ValidationContextImpl.java
(original)
+++ sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/ValidationContextImpl.java
Fri Mar 11 11:13:32 2016
@@ -28,12 +28,14 @@ import org.apache.sling.validation.spi.V
 public class ValidationContextImpl implements ValidationContext {
 
     private final @Nonnull String location;
+    private final Integer severity;
     private final @Nonnull ValueMap valueMap;
     private final Resource resource;
 
-    public ValidationContextImpl(@Nonnull String location, @Nonnull ValueMap valueMap, Resource
resource) {
+    public ValidationContextImpl(@Nonnull String location, Integer severity,  @Nonnull ValueMap
valueMap, Resource resource) {
         super();
         this.location = location;
+        this.severity = severity;
         this.valueMap = valueMap;
         this.resource = resource;
     }
@@ -56,4 +58,10 @@ public class ValidationContextImpl imple
         return resource;
     }
 
+    @Override
+    @CheckForNull
+    public Integer getSeverity() {
+        return severity;
+    }
+
 }

Modified: sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/ValidationServiceImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/ValidationServiceImpl.java?rev=1734530&r1=1734529&r2=1734530&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/ValidationServiceImpl.java
(original)
+++ sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/ValidationServiceImpl.java
Fri Mar 11 11:13:32 2016
@@ -178,14 +178,14 @@ public class ValidationServiceImpl imple
                     }
                 }
                 if (!foundMatch && childResource.isRequired()) {
-                    result.addFailure(relativePath, I18N_KEY_MISSING_REQUIRED_CHILD_RESOURCE_MATCHING_PATTERN,
pattern.toString());
+                    result.addFailure(relativePath, null, I18N_KEY_MISSING_REQUIRED_CHILD_RESOURCE_MATCHING_PATTERN,
pattern.toString());
                 }
             } else {
                 Resource expectedResource = resource.getChild(childResource.getName());
                 if (expectedResource != null) {
                     validateChildResource(expectedResource, relativePath, childResource,
result);
                 } else if (childResource.isRequired()) {
-                    result.addFailure(relativePath, I18N_KEY_MISSING_REQUIRED_CHILD_RESOURCE_WITH_NAME,
childResource.getName());
+                    result.addFailure(relativePath, null, I18N_KEY_MISSING_REQUIRED_CHILD_RESOURCE_WITH_NAME,
childResource.getName());
                 }
             } 
         }
@@ -236,7 +236,7 @@ public class ValidationServiceImpl imple
                     }
                 }
                 if (!foundMatch && resourceProperty.isRequired()) {
-                    result.addFailure(relativePath, I18N_KEY_MISSING_REQUIRED_PROPERTY_MATCHING_PATTERN,
pattern.toString());
+                    result.addFailure(relativePath, null, I18N_KEY_MISSING_REQUIRED_PROPERTY_MATCHING_PATTERN,
pattern.toString());
                 }
             } else {
                 validatePropertyValue(resourceProperty.getName(), valueMap, resource, relativePath,
resourceProperty, result);
@@ -248,14 +248,14 @@ public class ValidationServiceImpl imple
         Object fieldValues = valueMap.get(property);
         if (fieldValues == null) {
             if (resourceProperty.isRequired()) {
-                result.addFailure(relativePath, I18N_KEY_MISSING_REQUIRED_PROPERTY_WITH_NAME,
property);
+                result.addFailure(relativePath, null, I18N_KEY_MISSING_REQUIRED_PROPERTY_WITH_NAME,
property);
             }
             return;
         }
         List<ParameterizedValidator> validators = resourceProperty.getValidators();
         if (resourceProperty.isMultiple()) {
             if (!fieldValues.getClass().isArray()) {
-                result.addFailure(relativePath + property, I18N_KEY_EXPECTED_MULTIVALUE_PROPERTY);
+                result.addFailure(relativePath + property, null, I18N_KEY_EXPECTED_MULTIVALUE_PROPERTY);
                 return;
             }
         }
@@ -276,7 +276,7 @@ public class ValidationServiceImpl imple
             // see https://issues.apache.org/jira/browse/SLING-4178 for why the second check
is necessary
             if (typedValue == null || (typedValue.length > 0 && typedValue[0]
== null)) {
                 // here the missing required property case was already treated in validateValueMap
-                result.addFailure(relativePath + property, I18N_KEY_WRONG_PROPERTY_TYPE,
validator.getType());
+                result.addFailure(relativePath + property, validator.getSeverity(), I18N_KEY_WRONG_PROPERTY_TYPE,
validator.getType());
                 return;
             }
             
@@ -303,7 +303,7 @@ public class ValidationServiceImpl imple
     private void validateValue(CompositeValidationResult result, @Nonnull Object value, String
property, String relativePath, @Nonnull ValueMap valueMap, Resource resource, ParameterizedValidator
validator) {
         try {
             @SuppressWarnings("unchecked")
-            ValidationContext validationContext = new ValidationContextImpl(relativePath
+ property, valueMap, resource);
+            ValidationContext validationContext = new ValidationContextImpl(relativePath
+ property, validator.getSeverity(), valueMap, resource);
             ValidationResult validatorResult = ((Validator)validator.getValidator()).validate(value,
validationContext, validator.getParameters());
             result.addValidationResult(validatorResult);
         } catch (SlingValidationException e) {

Modified: sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/model/ParameterizedValidatorImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/model/ParameterizedValidatorImpl.java?rev=1734530&r1=1734529&r2=1734530&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/model/ParameterizedValidatorImpl.java
(original)
+++ sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/model/ParameterizedValidatorImpl.java
Fri Mar 11 11:13:32 2016
@@ -20,6 +20,7 @@ package org.apache.sling.validation.impl
 
 import java.util.Map;
 
+import javax.annotation.CheckForNull;
 import javax.annotation.Nonnull;
 
 import org.apache.sling.api.resource.ValueMap;
@@ -32,19 +33,22 @@ public class ParameterizedValidatorImpl
     private final @Nonnull Validator<?> validator;
     private final @Nonnull Map<String, Object> parameters;
     private final @Nonnull Class<?> type;
+    private final Integer severity;
     
     /**
      * 
      * Only the map has proper support for equals (see https://issues.apache.org/jira/browse/SLING-4784)
      * @param validator
      * @param parameters
+     * @param severity
      */
-    public ParameterizedValidatorImpl(@Nonnull Validator<?> validator, @Nonnull Map<String,
Object> parameters) {
+    public ParameterizedValidatorImpl(@Nonnull Validator<?> validator, @Nonnull Map<String,
Object> parameters, Integer severity) {
         super();
         this.validator = validator;
         this.parameters = parameters;
         // cache type information as this is using reflection
         this.type = ValidatorTypeUtil.getValidatorType(validator);
+        this.severity = severity;
     }
 
     /* (non-Javadoc)
@@ -72,18 +76,25 @@ public class ParameterizedValidatorImpl
     }
 
     @Override
+    @CheckForNull
+    public Integer getSeverity() {
+        return severity;
+    }
+
+    @Override
     public String toString() {
         return "ParameterizedValidatorImpl [validator=" + validator + ", parameters=" + parameters
+ ", type=" + type
-                + "]";
+                + ", severity=" + severity + "]";
     }
 
-    @Override
+     @Override
     public int hashCode() {
         final int prime = 31;
         int result = 1;
         result = prime * result + ((parameters == null) ? 0 : parameters.hashCode());
         result = prime * result + ((type == null) ? 0 : type.hashCode());
         result = prime * result + ((validator == null) ? 0 : validator.hashCode());
+        result = prime * result + ((severity == null) ? 0 : severity.hashCode());
         return result;
     }
 
@@ -102,7 +113,13 @@ public class ParameterizedValidatorImpl
             return false;
         if (!validator.getClass().getName().equals(other.validator.getClass().getName()))
             return false;
+        if (severity == null && other.severity != null) {
+            return false;
+        } else if (severity != null && other.severity == null) {
+            return false;
+        } else if (severity != null && other.severity != null && !severity.equals(other.severity))
{
+            return false;
+        }
         return true;
     }
-    
 }

Modified: sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/model/ResourcePropertyBuilder.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/model/ResourcePropertyBuilder.java?rev=1734530&r1=1734529&r2=1734530&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/model/ResourcePropertyBuilder.java
(original)
+++ sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/model/ResourcePropertyBuilder.java
Fri Mar 11 11:13:32 2016
@@ -47,14 +47,13 @@ public class ResourcePropertyBuilder {
         this.nameRegex = nameRegex;
         return this;
     }
-    
+
     public @Nonnull ResourcePropertyBuilder validator(@Nonnull Validator<?> validator)
{
-        validators.add(new ParameterizedValidatorImpl(validator, new HashMap<String, Object>()));
+        validators.add(new ParameterizedValidatorImpl(validator, new HashMap<String, Object>(),
null));
         return this;
     }
-    
-    
-    public @Nonnull ResourcePropertyBuilder validator(@Nonnull Validator<?> validator,
String... parametersNamesAndValues) {
+
+    public @Nonnull ResourcePropertyBuilder validator(@Nonnull Validator<?> validator,
Integer severity, String... parametersNamesAndValues) {
         if (parametersNamesAndValues.length % 2 != 0) {
             throw new IllegalArgumentException("array parametersNamesAndValues must be even!
(first specify name then value, separated by comma)");
         }
@@ -63,7 +62,7 @@ public class ResourcePropertyBuilder {
         for (int i=0; i<parametersNamesAndValues.length; i=i+2) {
             parameterMap.put(parametersNamesAndValues[i], parametersNamesAndValues[i+1]);
         }
-        validators.add(new ParameterizedValidatorImpl(validator, parameterMap));
+        validators.add(new ParameterizedValidatorImpl(validator, parameterMap, severity));
         return this;
     }
 
@@ -71,12 +70,12 @@ public class ResourcePropertyBuilder {
         this.optional = true;
         return this;
     }
-    
+
     public @Nonnull ResourcePropertyBuilder multiple() {
         this.multiple = true;
         return this;
     }
-    
+
     public @Nonnull ResourceProperty build(String name) {
         return new ResourcePropertyImpl(name, nameRegex, multiple, !optional, validators);
     }

Modified: sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/resourcemodel/ResourceValidationModelProviderImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/resourcemodel/ResourceValidationModelProviderImpl.java?rev=1734530&r1=1734529&r2=1734530&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/resourcemodel/ResourceValidationModelProviderImpl.java
(original)
+++ sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/resourcemodel/ResourceValidationModelProviderImpl.java
Fri Mar 11 11:13:32 2016
@@ -83,6 +83,7 @@ public class ResourceValidationModelProv
     public static final String MODELS_HOME = "validation/";
     public static final String APPLICABLE_PATHS = "applicablePaths";
     public static final String VALIDATED_RESOURCE_TYPE = "validatedResourceType";
+    public static final String SEVERITY = "severity";
 
     @Reference
     private ResourceResolverFactory rrf = null;
@@ -210,7 +211,7 @@ public class ResourceValidationModelProv
     /**
      * Creates a set of the properties that a resource is expected to have, together with
the associated validators.
      *
-     * @param validatorsMap      a map containing {@link Validator}s as values and their
class names as values
+     * @param validatorsMap      a map containing {@link Validator}s as values and their
class names as keys
      * @param propertiesResource the resource identifying the properties node from a validation
model's structure (might be {@code null})
      * @return a set of properties or an empty set if no properties are defined
      * @see ResourceProperty
@@ -239,7 +240,7 @@ public class ResourceValidationModelProv
                         if (v == null) {
                             throw new IllegalArgumentException("Could not find validator
with name '" + validatorName + "'");
                         }
-                        // get type of validator
+                        // get arguments for validator
                         String[] validatorArguments = validatorProperties.get(ResourceValidationModelProviderImpl.VALIDATOR_ARGUMENTS,
String[].class);
                         Map<String, Object> validatorArgumentsMap = new HashMap<String,
Object>();
                         if (validatorArguments != null) {
@@ -251,7 +252,9 @@ public class ResourceValidationModelProv
                                 validatorArgumentsMap.put(keyValuePair[0], keyValuePair[1]);
                             }
                         }
-                        parameterizedValidators.add(new ParameterizedValidatorImpl(v, validatorArgumentsMap));
+                        // get severity
+                        Integer severity = validatorProperties.get(SEVERITY, Integer.class);
+                        parameterizedValidators.add(new ParameterizedValidatorImpl(v, validatorArgumentsMap,
severity));
                     }
                 }
                 ResourceProperty f = new ResourcePropertyImpl(fieldName, nameRegex, propertyMultiple,
propertyRequired, parameterizedValidators);

Modified: sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/validators/RegexValidator.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/validators/RegexValidator.java?rev=1734530&r1=1734529&r2=1734530&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/validators/RegexValidator.java
(original)
+++ sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/validators/RegexValidator.java
Fri Mar 11 11:13:32 2016
@@ -56,7 +56,7 @@ public class RegexValidator implements V
             if (pattern.matcher((String)data).matches()) {
                 return DefaultValidationResult.VALID;
             }
-            return new DefaultValidationResult(context.getLocation(), I18N_KEY_PATTERN_DOES_NOT_MATCH,
regex);
+            return new DefaultValidationResult(context.getLocation(), context.getSeverity(),
I18N_KEY_PATTERN_DOES_NOT_MATCH, regex);
         } catch (PatternSyntaxException e) {
             throw new SlingValidationException("Given pattern in argument '" + REGEX_PARAM
+ "' is invalid", e);
         }

Modified: sling/trunk/bundles/extensions/validation/core/src/test/java/org/apache/sling/validation/impl/ValidationServiceImplTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/validation/core/src/test/java/org/apache/sling/validation/impl/ValidationServiceImplTest.java?rev=1734530&r1=1734529&r2=1734530&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/validation/core/src/test/java/org/apache/sling/validation/impl/ValidationServiceImplTest.java
(original)
+++ sling/trunk/bundles/extensions/validation/core/src/test/java/org/apache/sling/validation/impl/ValidationServiceImplTest.java
Fri Mar 11 11:13:32 2016
@@ -97,7 +97,7 @@ public class ValidationServiceImplTest {
         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.<ValidationFailure>contains(new
DefaultValidationFailure("field1", ValidationServiceImpl.I18N_KEY_WRONG_PROPERTY_TYPE, Date.class)));
+        Assert.assertThat(vr.getFailures(), Matchers.<ValidationFailure>contains(new
DefaultValidationFailure("field1", null, ValidationServiceImpl.I18N_KEY_WRONG_PROPERTY_TYPE,
Date.class)));
     }
 
     @Test
@@ -140,7 +140,7 @@ public class ValidationServiceImplTest {
         hashMap.put("field3", "");
 
         ValidationResult vr = validationService.validate(new ValueMapDecorator(hashMap),
vm);
-        Assert.assertThat(vr.getFailures(), Matchers.<ValidationFailure>contains(new
DefaultValidationFailure("", ValidationServiceImpl.I18N_KEY_MISSING_REQUIRED_PROPERTY_WITH_NAME,
"field4")));
+        Assert.assertThat(vr.getFailures(), Matchers.<ValidationFailure>contains(new
DefaultValidationFailure("", null, ValidationServiceImpl.I18N_KEY_MISSING_REQUIRED_PROPERTY_WITH_NAME,
"field4")));
     }
 
     @Test()
@@ -158,7 +158,7 @@ public class ValidationServiceImplTest {
     @Test()
     public void testValueMapWithEmptyOptionalValue() throws Exception {
         propertyBuilder.optional();
-        propertyBuilder.validator(new RegexValidator(), RegexValidator.REGEX_PARAM, "abc");
+        propertyBuilder.validator(new RegexValidator(), 2, RegexValidator.REGEX_PARAM, "abc");
         modelBuilder.resourceProperty(propertyBuilder.build("field1"));
         ValidationModel vm = modelBuilder.build("sling/validation/test");
 
@@ -167,16 +167,16 @@ public class ValidationServiceImplTest {
         ValidationResult vr = validationService.validate(new ValueMapDecorator(hashMap),
vm);
 
         Assert.assertFalse(vr.isValid()); // check for correct error message Map<String,
List<String>>
-        Assert.assertThat(vr.getFailures(), Matchers.<ValidationFailure>contains(new
DefaultValidationFailure("field1", RegexValidator.I18N_KEY_PATTERN_DOES_NOT_MATCH, "abc")));
+        Assert.assertThat(vr.getFailures(), Matchers.<ValidationFailure>contains(new
DefaultValidationFailure("field1", 2, RegexValidator.I18N_KEY_PATTERN_DOES_NOT_MATCH, "abc")));
     }
 
     @Test
     public void testValueMapWithCorrectDataType() throws Exception {
-        propertyBuilder.validator(new RegexValidator(), RegexValidator.REGEX_PARAM, "abc");
+        propertyBuilder.validator(new RegexValidator(), 0, RegexValidator.REGEX_PARAM, "abc");
         modelBuilder.resourceProperty(propertyBuilder.build("field1"));
         propertyBuilder = new ResourcePropertyBuilder();
         final String TEST_REGEX = "^test$";
-        propertyBuilder.validator(new RegexValidator(), RegexValidator.REGEX_PARAM, TEST_REGEX);
+        propertyBuilder.validator(new RegexValidator(), 0, RegexValidator.REGEX_PARAM, TEST_REGEX);
         modelBuilder.resourceProperty(propertyBuilder.build("field2"));
         ValidationModel vm = modelBuilder.build("sling/validation/test");
 
@@ -187,12 +187,12 @@ public class ValidationServiceImplTest {
         ValidationResult vr = validationService.validate(new ValueMapDecorator(hashMap),
vm);
 
         Assert.assertFalse(vr.isValid());
-        Assert.assertThat(vr.getFailures(), Matchers.<ValidationFailure> hasItem(new
DefaultValidationFailure("field2", RegexValidator.I18N_KEY_PATTERN_DOES_NOT_MATCH, TEST_REGEX)));
+        Assert.assertThat(vr.getFailures(), Matchers.<ValidationFailure> hasItem(new
DefaultValidationFailure("field2", 0, RegexValidator.I18N_KEY_PATTERN_DOES_NOT_MATCH, TEST_REGEX)));
     }
 
     @Test
     public void testResourceWithMissingGrandChildProperty() throws Exception {
-        propertyBuilder.validator(new RegexValidator(), RegexValidator.REGEX_PARAM, "\\d");
// accept any digits
+        propertyBuilder.validator(new RegexValidator(), 0, RegexValidator.REGEX_PARAM, "\\d");
// accept any digits
         ResourceProperty property = propertyBuilder.build("field1");
         modelBuilder.resourceProperty(property);
 
@@ -219,12 +219,12 @@ public class ValidationServiceImplTest {
 
         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("child/grandchild", ValidationServiceImpl.I18N_KEY_MISSING_REQUIRED_PROPERTY_WITH_NAME,
"field1")));
+        Assert.assertThat(vr.getFailures(), Matchers.<ValidationFailure>contains(new
DefaultValidationFailure("child/grandchild", 0, ValidationServiceImpl.I18N_KEY_MISSING_REQUIRED_PROPERTY_WITH_NAME,
"field1")));
     }
 
     @Test
     public void testResourceWithMissingOptionalChildResource() throws Exception {
-        propertyBuilder.validator(new RegexValidator(), RegexValidator.REGEX_PARAM, "\\d");
// accept any digits
+        propertyBuilder.validator(new RegexValidator(), 0, RegexValidator.REGEX_PARAM, "\\d");
// accept any digits
         ResourceProperty property = propertyBuilder.build("field1");
 
         ChildResource child = new ChildResourceImpl("child", null, false, Collections.singletonList(property),
@@ -244,7 +244,7 @@ public class ValidationServiceImplTest {
 
     @Test
     public void testResourceWithNestedChildren() throws Exception {
-        propertyBuilder.validator(new RegexValidator(), RegexValidator.REGEX_PARAM, "\\d");
// accept any digits
+        propertyBuilder.validator(new RegexValidator(), 0, RegexValidator.REGEX_PARAM, "\\d");
// accept any digits
         ResourceProperty property = propertyBuilder.build("field1");
 
         ChildResource modelGrandChild = new ChildResourceImpl("grandchild", null, true,
@@ -301,7 +301,7 @@ public class ValidationServiceImplTest {
 
     @Test
     public void testResourceWithNestedChildrenAndPatternMatching() throws Exception {
-        propertyBuilder.validator(new RegexValidator(), RegexValidator.REGEX_PARAM, "\\d");
// accept any digits
+        propertyBuilder.validator(new RegexValidator(), 0, RegexValidator.REGEX_PARAM, "\\d");
// accept any digits
         ResourceProperty property = propertyBuilder.build("field1");
 
         ChildResource modelGrandChild = new ChildResourceImpl("grandchild", "grandchild.*",
true,
@@ -332,15 +332,15 @@ public class ValidationServiceImplTest {
         Assert.assertFalse("resource should have been considered invalid", vr.isValid());
         
         Assert.assertThat(vr.getFailures(), Matchers.<ValidationFailure>containsInAnyOrder(
-                new DefaultValidationFailure("child2", ValidationServiceImpl.I18N_KEY_MISSING_REQUIRED_CHILD_RESOURCE_MATCHING_PATTERN,
"grandchild.*"),
-                new DefaultValidationFailure("child3", ValidationServiceImpl.I18N_KEY_MISSING_REQUIRED_CHILD_RESOURCE_MATCHING_PATTERN,
"grandchild.*"),
-                new DefaultValidationFailure("child3", ValidationServiceImpl.I18N_KEY_MISSING_REQUIRED_PROPERTY_WITH_NAME,
"field1"),
-                new DefaultValidationFailure("", ValidationServiceImpl.I18N_KEY_MISSING_REQUIRED_CHILD_RESOURCE_MATCHING_PATTERN,
"siblingchild.*")));
+                new DefaultValidationFailure("child2", 0, ValidationServiceImpl.I18N_KEY_MISSING_REQUIRED_CHILD_RESOURCE_MATCHING_PATTERN,
"grandchild.*"),
+                new DefaultValidationFailure("child3", 0, ValidationServiceImpl.I18N_KEY_MISSING_REQUIRED_CHILD_RESOURCE_MATCHING_PATTERN,
"grandchild.*"),
+                new DefaultValidationFailure("child3", 0, ValidationServiceImpl.I18N_KEY_MISSING_REQUIRED_PROPERTY_WITH_NAME,
"field1"),
+                new DefaultValidationFailure("", 0, ValidationServiceImpl.I18N_KEY_MISSING_REQUIRED_CHILD_RESOURCE_MATCHING_PATTERN,
"siblingchild.*")));
     }
 
     @Test
     public void testResourceWithPropertyPatternMatching() throws Exception {
-        propertyBuilder.validator(new RegexValidator(), RegexValidator.REGEX_PARAM, "\\d");
// accept any digits
+        propertyBuilder.validator(new RegexValidator(), 0, RegexValidator.REGEX_PARAM, "\\d");
// accept any digits
         propertyBuilder.nameRegex("field.*");
         modelBuilder.resourceProperty(propertyBuilder.build("field"));
         propertyBuilder.nameRegex("otherfield.*");
@@ -363,13 +363,13 @@ public class ValidationServiceImplTest {
         Assert.assertFalse("resource should have been considered invalid", vr.isValid());
         
         Assert.assertThat(vr.getFailures(), Matchers.<ValidationFailure>contains(
-                new DefaultValidationFailure("field3", RegexValidator.I18N_KEY_PATTERN_DOES_NOT_MATCH,
"\\d"),
-                new DefaultValidationFailure("", ValidationServiceImpl.I18N_KEY_MISSING_REQUIRED_PROPERTY_MATCHING_PATTERN,
"otherfield.*")));
+                new DefaultValidationFailure("field3", 0, RegexValidator.I18N_KEY_PATTERN_DOES_NOT_MATCH,
"\\d"),
+                new DefaultValidationFailure("", 0, ValidationServiceImpl.I18N_KEY_MISSING_REQUIRED_PROPERTY_MATCHING_PATTERN,
"otherfield.*")));
     }
 
     @Test
     public void testResourceWithMultivalueProperties() throws Exception {
-        propertyBuilder.validator(new RegexValidator(), RegexValidator.REGEX_PARAM, "\\d");
// accept any digits
+        propertyBuilder.validator(new RegexValidator(), 0, RegexValidator.REGEX_PARAM, "\\d");
// accept any digits
         propertyBuilder.multiple();
         modelBuilder.resourceProperty(propertyBuilder.build("field"));
         ValidationModel vm = modelBuilder.build("type");
@@ -382,7 +382,7 @@ public class ValidationServiceImplTest {
 
         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("field[1]", RegexValidator.I18N_KEY_PATTERN_DOES_NOT_MATCH, "\\d")));
+        Assert.assertThat(vr.getFailures(), Matchers.<ValidationFailure>contains(new
DefaultValidationFailure("field[1]", 0, RegexValidator.I18N_KEY_PATTERN_DOES_NOT_MATCH, "\\d")));
     }
 
     @Test()
@@ -439,8 +439,8 @@ public class ValidationServiceImplTest {
         ValidationResult vr = validationService.validateResourceRecursively(testResource,
true, ignoreResourceType3Filter, false);
         Assert.assertFalse("resource should have been considered invalid", vr.isValid());
         Assert.assertThat(vr.getFailures(), Matchers.<ValidationFailure>contains(
-                new DefaultValidationFailure("", ValidationServiceImpl.I18N_KEY_MISSING_REQUIRED_PROPERTY_WITH_NAME,
"field1"),
-                new DefaultValidationFailure("child2", ValidationServiceImpl.I18N_KEY_MISSING_REQUIRED_PROPERTY_WITH_NAME,
"field2")));
+                new DefaultValidationFailure("", null,  ValidationServiceImpl.I18N_KEY_MISSING_REQUIRED_PROPERTY_WITH_NAME,
"field1"),
+                new DefaultValidationFailure("child2", null, ValidationServiceImpl.I18N_KEY_MISSING_REQUIRED_PROPERTY_WITH_NAME,
"field2")));
     }
 
     @Test(expected = IllegalArgumentException.class)

Modified: sling/trunk/bundles/extensions/validation/core/src/test/java/org/apache/sling/validation/impl/resourcemodel/ResourceValidationModelProviderImplTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/validation/core/src/test/java/org/apache/sling/validation/impl/resourcemodel/ResourceValidationModelProviderImplTest.java?rev=1734530&r1=1734529&r2=1734530&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/validation/core/src/test/java/org/apache/sling/validation/impl/resourcemodel/ResourceValidationModelProviderImplTest.java
(original)
+++ sling/trunk/bundles/extensions/validation/core/src/test/java/org/apache/sling/validation/impl/resourcemodel/ResourceValidationModelProviderImplTest.java
Fri Mar 11 11:13:32 2016
@@ -145,7 +145,7 @@ public class ResourceValidationModelProv
         modelBuilder = new ValidationModelBuilder();
         modelBuilder.setApplicablePath("/content/site1");
         ResourcePropertyBuilder propertyBuilder = new ResourcePropertyBuilder();
-        propertyBuilder.validator(new RegexValidator(), RegexValidator.REGEX_PARAM, "prefix.*");
+        propertyBuilder.validator(new RegexValidator(), 10, RegexValidator.REGEX_PARAM, "prefix.*");
         ResourceProperty property = propertyBuilder.build("field1");
         modelBuilder.resourceProperty(property);
 
@@ -358,6 +358,10 @@ public class ResourceValidationModelProv
                             validatorProperties.put(ResourceValidationModelProviderImpl.VALIDATOR_ARGUMENTS,
                                     convertMapToJcrValidatorArguments(parameters));
                         }
+                        Integer severity = validator.getSeverity();
+                        if (severity != null) {
+                            validatorProperties.put(ResourceValidationModelProviderImpl.SEVERITY,
severity);
+                        }
                         ResourceUtil.getOrCreateResource(rr, validators.getPath() + "/"
                                 + validator.getValidator().getClass().getName(), validatorProperties,
null, true);
                     }

Modified: sling/trunk/bundles/extensions/validation/it-http/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/validation/it-http/pom.xml?rev=1734530&r1=1734529&r2=1734530&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/validation/it-http/pom.xml (original)
+++ sling/trunk/bundles/extensions/validation/it-http/pom.xml Fri Mar 11 11:13:32 2016
@@ -47,6 +47,9 @@
         optionally using -Dmaven.surefire.debug to enable debugging.
      -->
     <properties>
+        <!-- API and Core bundle require Java 8 -->
+        <sling.java.version>8</sling.java.version>
+    
         <!-- Set this to run the server on a specific port
         <http.port></http.port>
          -->

Modified: sling/trunk/bundles/extensions/validation/test-services/src/main/java/org/apache/sling/validation/testservices/ValidationPostResponse.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/validation/test-services/src/main/java/org/apache/sling/validation/testservices/ValidationPostResponse.java?rev=1734530&r1=1734529&r2=1734530&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/validation/test-services/src/main/java/org/apache/sling/validation/testservices/ValidationPostResponse.java
(original)
+++ sling/trunk/bundles/extensions/validation/test-services/src/main/java/org/apache/sling/validation/testservices/ValidationPostResponse.java
Fri Mar 11 11:13:32 2016
@@ -62,6 +62,7 @@ public class ValidationPostResponse exte
                     JSONObject failureJson = new JSONObject();
                     failureJson.put("message", failure.getMessage(resourceBundle));
                     failureJson.put("location", failure.getLocation());
+                    failureJson.put("severity", failure.getSeverity());
                     failures.put(failureJson);
                 }
                 jsonResponse.put("failures", failures);

Modified: sling/trunk/bundles/extensions/validation/test-services/src/main/resources/SLING-CONTENT/apps/sling/validation/models/model1.json
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/validation/test-services/src/main/resources/SLING-CONTENT/apps/sling/validation/models/model1.json?rev=1734530&r1=1734529&r2=1734530&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/validation/test-services/src/main/resources/SLING-CONTENT/apps/sling/validation/models/model1.json
(original)
+++ sling/trunk/bundles/extensions/validation/test-services/src/main/resources/SLING-CONTENT/apps/sling/validation/models/model1.json
Fri Mar 11 11:13:32 2016
@@ -13,7 +13,8 @@
                 "jcr:primaryType" : "nt:unstructured",
                 "org.apache.sling.validation.impl.validators.RegexValidator": {
                     "jcr:primaryType" : "nt:unstructured",
-                    "validatorArguments" : ["regex=^\\\p{Upper}+$"]
+                    "validatorArguments" : ["regex=^\\\p{Upper}+$"],
+                    "severity" : "10"
                 }
             }
         },




Mime
View raw message