summaryrefslogtreecommitdiffstats
path: root/common-parameters/src/main/java/org
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
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')
-rw-r--r--common-parameters/src/main/java/org/onap/policy/common/parameters/GroupValidationResult.java70
-rw-r--r--common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterValidationResult.java62
-rw-r--r--common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/Min.java39
-rw-r--r--common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/NotBlank.java37
-rw-r--r--common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/NotNull.java37
5 files changed, 214 insertions, 31 deletions
diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/GroupValidationResult.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/GroupValidationResult.java
index 283f36b9..6da36c19 100644
--- a/common-parameters/src/main/java/org/onap/policy/common/parameters/GroupValidationResult.java
+++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/GroupValidationResult.java
@@ -1,20 +1,20 @@
-/*-
+/*
* ============LICENSE_START=======================================================
* Copyright (C) 2018 Ericsson. All rights reserved.
- * Modifications Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2018-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=========================================================
*/
@@ -66,7 +66,7 @@ public class GroupValidationResult implements ValidationResult {
// Check if a validation result should be added for this declared field
if (isIncludedField(field)) {
// Set a validation result for the field
- validationResultMap.put(field.getName(), getValidationResult(field, parameterGroup));
+ validationResultMap.put(field.getName(), getSetValidationResult(field, parameterGroup));
}
}
@@ -75,13 +75,29 @@ public class GroupValidationResult implements ValidationResult {
// Check if a validation result should be added for this declared field
if (isIncludedField(field)) {
// Set a validation result for the field
- validationResultMap.putIfAbsent(field.getName(), getValidationResult(field, parameterGroup));
+ validationResultMap.putIfAbsent(field.getName(), getSetValidationResult(field, parameterGroup));
}
}
}
+
+ /**
+ * Construct a validation result for a field, updating "this" status.
+ *
+ * @param field The parameter field
+ * @param ParameterGroup The parameter group containing the field
+ * @return the validation result
+ * @throws Exception on accessing private fields
+ */
+ private ValidationResult getSetValidationResult(Field field, ParameterGroup parameterGroup) {
+ ValidationResult result = getValidationResult(field, parameterGroup);
+ setResult(result.getStatus());
+
+ return result;
+ }
+
/**
* Construct a validation result for a field.
- *
+ *
* @param field The parameter field
* @param ParameterGroup The parameter group containing the field
* @return the validation result
@@ -92,6 +108,12 @@ public class GroupValidationResult implements ValidationResult {
final Class<?> fieldType = field.getType();
final Object fieldObject = getObjectField(parameterGroup, field);
+ // perform null checks
+ ParameterValidationResult result = new ParameterValidationResult(field, fieldObject);
+ if (!result.isValid()) {
+ return result;
+ }
+
// Nested parameter groups are allowed
if (ParameterGroup.class.isAssignableFrom(fieldType)) {
return new GroupValidationResult((ParameterGroup) fieldObject);
@@ -106,16 +128,16 @@ public class GroupValidationResult implements ValidationResult {
// Collections of parameter groups are not allowed
if (Collection.class.isAssignableFrom(field.getType())) {
checkCollection4ParameterGroups(fieldName, fieldObject);
- return new ParameterValidationResult(field, fieldObject);
+ return result;
}
// It's a regular parameter
- return new ParameterValidationResult(field, fieldObject);
+ return result;
}
/**
* Get the value of a field in an object using a getter found with reflection.
- *
+ *
* @param targetObject The object on which to read the field value
* @param fieldName The name of the field
* @return The field value
@@ -156,7 +178,7 @@ public class GroupValidationResult implements ValidationResult {
/**
* Check if this field is a map of parameter groups indexed by string keys.
- *
+ *
* @param fieldName the name of the collection field.
* @param mapObject the map object to check
*/
@@ -184,7 +206,7 @@ public class GroupValidationResult implements ValidationResult {
/**
* Check if this field contains parameter groups.
- *
+ *
* @param fieldName the name of the collection field.
* @param collectionObject the collection object to check
*/
@@ -234,7 +256,7 @@ public class GroupValidationResult implements ValidationResult {
/**
* Set the validation result on a parameter group.
- *
+ *
* @param status The validation status the parameter group is receiving
* @param message The validation message explaining the validation status
*/
@@ -247,7 +269,7 @@ public class GroupValidationResult implements ValidationResult {
/**
* Set the validation result on a parameter group. On a sequence of calls, the most serious validation status is
* recorded, assuming the status enum ordinal increase in order of severity
- *
+ *
* @param status The validation status the parameter group is receiving
*/
public void setResult(final ValidationStatus status) {
@@ -260,7 +282,7 @@ public class GroupValidationResult implements ValidationResult {
/**
* Set the validation result on a parameter in a parameter group.
- *
+ *
* @param parameterName The name of the parameter
* @param status The validation status the field is receiving
* @param message The validation message explaining the validation status
@@ -281,7 +303,7 @@ public class GroupValidationResult implements ValidationResult {
/**
* Set the validation result on a nested parameter group.
- *
+ *
* @param parameterName The name of the parameter field
* @param nestedValidationResult The validation result from a nested field
*/
@@ -304,7 +326,7 @@ public class GroupValidationResult implements ValidationResult {
/**
* Set the validation result on a nested parameter group map entry.
- *
+ *
* @param parameterName The name of the parameter field
* @param key The key of the map entry
* @param nestedMapValidationResult The validation result from a nested map entry
@@ -329,7 +351,7 @@ public class GroupValidationResult implements ValidationResult {
/**
* Set the validation status on a group map entry.
- *
+ *
* @param parameterName The name of the parameter field
* @param key The key of the map entry
* @param status The validation status of the entry
@@ -395,11 +417,11 @@ public class GroupValidationResult implements ValidationResult {
return validationResultBuilder.toString();
}
-
+
/**
* Check if a field should be included for validation.
- *
+ *
* @param field the field to check for inclusion
* @return true of the field should be included
*/
@@ -425,11 +447,11 @@ public class GroupValidationResult implements ValidationResult {
superclassFields.add(field);
}
}
-
+
// Check the next super class down
currentClass = currentClass.getSuperclass();
}
-
+
return superclassFields;
}
-} \ No newline at end of file
+}
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;
+ }
}
/**
diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/Min.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/Min.java
new file mode 100644
index 00000000..9ad6d7e0
--- /dev/null
+++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/Min.java
@@ -0,0 +1,39 @@
+/*
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.common.parameters.annotations;
+
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+@Retention(RUNTIME)
+@Target(FIELD)
+public @interface Min {
+
+ /**
+ * The minimum value allowed.
+ *
+ * @return the minimum value allowed
+ */
+ long value();
+}
diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/NotBlank.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/NotBlank.java
new file mode 100644
index 00000000..169fa593
--- /dev/null
+++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/NotBlank.java
@@ -0,0 +1,37 @@
+/*
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.common.parameters.annotations;
+
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.TYPE;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+/**
+ * Indicates that a field (i.e., String) may not be empty.
+ */
+@Retention(RUNTIME)
+@Target({TYPE, FIELD})
+public @interface NotBlank {
+
+}
diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/NotNull.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/NotNull.java
new file mode 100644
index 00000000..3c7bc8b7
--- /dev/null
+++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/NotNull.java
@@ -0,0 +1,37 @@
+/*
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.common.parameters.annotations;
+
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.TYPE;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+/**
+ * Indicates that a field may not be null.
+ */
+@Retention(RUNTIME)
+@Target({TYPE, FIELD})
+public @interface NotNull {
+
+}