aboutsummaryrefslogtreecommitdiffstats
path: root/common-parameters/src/main/java/org/onap/policy/common/parameters/BeanValidator.java
diff options
context:
space:
mode:
Diffstat (limited to 'common-parameters/src/main/java/org/onap/policy/common/parameters/BeanValidator.java')
-rw-r--r--common-parameters/src/main/java/org/onap/policy/common/parameters/BeanValidator.java78
1 files changed, 65 insertions, 13 deletions
diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/BeanValidator.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/BeanValidator.java
index 68455ac3..947def42 100644
--- a/common-parameters/src/main/java/org/onap/policy/common/parameters/BeanValidator.java
+++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/BeanValidator.java
@@ -25,11 +25,13 @@ import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.lang3.StringUtils;
+import org.onap.policy.common.parameters.annotations.ClassName;
import org.onap.policy.common.parameters.annotations.Max;
import org.onap.policy.common.parameters.annotations.Min;
import org.onap.policy.common.parameters.annotations.NotBlank;
import org.onap.policy.common.parameters.annotations.NotNull;
import org.onap.policy.common.parameters.annotations.Pattern;
+import org.onap.policy.common.parameters.annotations.Size;
import org.onap.policy.common.parameters.annotations.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -74,9 +76,11 @@ public class BeanValidator {
protected void addValidators(ValueValidator validator) {
validator.addAnnotation(NotNull.class, this::verNotNull);
validator.addAnnotation(NotBlank.class, this::verNotBlank);
+ validator.addAnnotation(Size.class, this::verSize);
validator.addAnnotation(Max.class, this::verMax);
validator.addAnnotation(Min.class, this::verMin);
validator.addAnnotation(Pattern.class, this::verRegex);
+ validator.addAnnotation(ClassName.class, this::verClassName);
validator.addAnnotation(Valid.class, this::verCascade);
}
@@ -105,9 +109,7 @@ public class BeanValidator {
*/
public boolean verNotNull(BeanValidationResult result, String fieldName, Object value) {
if (value == null) {
- ObjectValidationResult result2 =
- new ObjectValidationResult(fieldName, xlate(value), ValidationStatus.INVALID, "is null");
- result.addResult(result2);
+ result.addResult(fieldName, xlate(value), ValidationStatus.INVALID, "is null");
return false;
}
@@ -125,9 +127,38 @@ public class BeanValidator {
*/
public boolean verNotBlank(BeanValidationResult result, String fieldName, Object value) {
if (value instanceof String && StringUtils.isBlank(value.toString())) {
- ObjectValidationResult result2 =
- new ObjectValidationResult(fieldName, xlate(value), ValidationStatus.INVALID, "is blank");
- result.addResult(result2);
+ result.addResult(fieldName, xlate(value), ValidationStatus.INVALID, "is blank");
+ return false;
+ }
+
+ return true;
+ }
+
+ /**
+ * Verifies that the value has the specified number of elements.
+ *
+ * @param result where to add the validation result
+ * @param fieldName field whose value is being verified
+ * @param annot annotation against which the value is being verified
+ * @param value value to be verified
+ * @return {@code true} if the next check should be performed, {@code false} otherwise
+ */
+ public boolean verSize(BeanValidationResult result, String fieldName, Size annot, Object value) {
+ int size;
+ if (value instanceof Collection) {
+ size = ((Collection<?>) value).size();
+
+ } else if (value instanceof Map) {
+ size = ((Map<?, ?>) value).size();
+
+ } else {
+ return true;
+ }
+
+
+ if (size < annot.min()) {
+ result.addResult(fieldName, xlate(value), ValidationStatus.INVALID,
+ "minimum number of elements: " + annot.min());
return false;
}
@@ -153,9 +184,8 @@ public class BeanValidator {
logger.warn("validation error for regular expression: {}", annot.regexp(), e);
}
- ObjectValidationResult result2 = new ObjectValidationResult(fieldName, xlate(value), ValidationStatus.INVALID,
+ result.addResult(fieldName, xlate(value), ValidationStatus.INVALID,
"does not match regular expression " + annot.regexp());
- result.addResult(result2);
return false;
}
@@ -188,9 +218,8 @@ public class BeanValidator {
return true;
}
- ObjectValidationResult result2 = new ObjectValidationResult(fieldName, xlate(value), ValidationStatus.INVALID,
+ result.addResult(fieldName, xlate(value), ValidationStatus.INVALID,
"exceeds the maximum value: " + annot.value());
- result.addResult(result2);
return false;
}
@@ -236,13 +265,35 @@ public class BeanValidator {
return true;
}
- ObjectValidationResult result2 = new ObjectValidationResult(fieldName, xlate(value), ValidationStatus.INVALID,
+ result.addResult(fieldName, xlate(value), ValidationStatus.INVALID,
"is below the minimum value: " + min);
- result.addResult(result2);
return false;
}
/**
+ * Verifies that the value is a valid class name.
+ *
+ * @param result where to add the validation result
+ * @param fieldName field whose value is being verified
+ * @param value value to be verified
+ * @return {@code true} if the next check should be performed, {@code false} otherwise
+ */
+ public boolean verClassName(BeanValidationResult result, String fieldName, Object value) {
+ if (!(value instanceof String)) {
+ return true;
+ }
+
+ try {
+ Class.forName(value.toString());
+ return true;
+
+ } catch (final ClassNotFoundException exp) {
+ result.addResult(fieldName, value, ValidationStatus.INVALID, "class is not in the classpath");
+ return false;
+ }
+ }
+
+ /**
* Verifies that the value is valid by recursively invoking
* {@link #validateTop(String, Object)}.
*
@@ -256,7 +307,8 @@ public class BeanValidator {
return true;
}
- BeanValidationResult result2 = validateTop(fieldName, value);
+ BeanValidationResult result2 = (value instanceof ParameterGroup ? ((ParameterGroup) value).validate()
+ : validateTop(fieldName, value));
if (result2.isClean()) {
return true;