aboutsummaryrefslogtreecommitdiffstats
path: root/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterValidationResult.java
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2019-03-07 14:53:12 -0500
committerJim Hahn <jrh3@att.com>2019-03-07 16:37:49 -0500
commit57a2ff2f9b918b4890f2707643342110fe31a2e4 (patch)
tree43f9e12ff1ce63f0f61e3801a4ebb83005f52456 /common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterValidationResult.java
parent23a3dc4ece2f1533fe1d6b627b5db05e7754a70c (diff)
Add NotNull and NotBlank parameter validation
Modified the ParameterValidator to support new NotNull and NotBlank annotations indicating that a field should not be null or blank. These annotations can be made at class level or individual field level. Moved annotations to their own subdirectory. Added a comment to a method. Extracted constant strings. Moved one annotation to the subclass level. Added support for "Min" annotation. Propagate validation errors up from nested items. Apply field-level validations, even when field is a ParameterGroup. Change-Id: Ic90df55487dc5db7b7b0be5397624d1957904a81 Issue-ID: POLICY-1542 Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterValidationResult.java')
-rw-r--r--common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterValidationResult.java62
1 files changed, 55 insertions, 7 deletions
diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterValidationResult.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterValidationResult.java
index 9c829f4b..e43c2d17 100644
--- a/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterValidationResult.java
+++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterValidationResult.java
@@ -1,26 +1,31 @@
-/*-
+/*
* ============LICENSE_START=======================================================
* Copyright (C) 2018 Ericsson. All rights reserved.
+ * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
- *
+ *
* SPDX-License-Identifier: Apache-2.0
* ============LICENSE_END=========================================================
*/
package org.onap.policy.common.parameters;
+import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
+import org.onap.policy.common.parameters.annotations.Min;
+import org.onap.policy.common.parameters.annotations.NotBlank;
+import org.onap.policy.common.parameters.annotations.NotNull;
/**
* This class holds the result of the validation of a parameter.
@@ -43,6 +48,47 @@ public class ParameterValidationResult implements ValidationResult {
protected ParameterValidationResult(final Field field, final Object parameterValue) {
this.field = field;
this.parameterValue = parameterValue;
+
+ if (parameterValue == null && getAnyAnnotation(field, NotNull.class) != null) {
+ setResult(ValidationStatus.INVALID, "is null");
+
+ } else if (parameterValue instanceof String && getAnyAnnotation(field, NotBlank.class) != null
+ && parameterValue.toString().trim().isEmpty()) {
+ setResult(ValidationStatus.INVALID, "must be a non-blank string");
+
+ } else if (parameterValue instanceof Number) {
+ Min minAnnot = field.getAnnotation(Min.class);
+ if (minAnnot != null && ((Number) parameterValue).longValue() < minAnnot.value()) {
+ setResult(ValidationStatus.INVALID, "must be >= " + minAnnot.value());
+ }
+ }
+ }
+
+ /**
+ * Gets an annotation for a field, first checking the field, itself, and then checking
+ * at the class level for the current and superclasses.
+ *
+ * @param field field of interest
+ * @param annotClass class of annotation that is desired
+ * @return the field's annotation, or {@code null} if it does not exist
+ */
+ private static <T extends Annotation> T getAnyAnnotation(final Field field, Class<T> annotClass) {
+ T annot = field.getAnnotation(annotClass);
+ if (annot != null) {
+ return annot;
+ }
+
+ // check class level
+ Class<?> clazz = field.getDeclaringClass();
+ while (clazz != Object.class) {
+ if ((annot = clazz.getAnnotation(annotClass)) != null) {
+ return annot;
+ }
+
+ clazz = clazz.getSuperclass();
+ }
+
+ return null;
}
/**
@@ -66,14 +112,16 @@ public class ParameterValidationResult implements ValidationResult {
}
/**
- * Set the validation result on on a parameter field.
+ * Set the validation result on on a parameter field.
* @param status The validation status the field is receiving
* @param message The validation message explaining the validation status
*/
@Override
public void setResult(final ValidationStatus status, final String message) {
- this.status = status;
- this.message = message;
+ if (this.status == ValidationStatus.CLEAN) {
+ this.status = status;
+ this.message = message;
+ }
}
/**