aboutsummaryrefslogtreecommitdiffstats
path: root/common-parameters/src/main
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2021-01-04 12:11:32 -0500
committerJim Hahn <jrh3@att.com>2021-01-04 17:32:12 -0500
commitf9add40e0d87baff4ed56529bc61c31a577dc93d (patch)
treed1be521c0f4e84c8fd6a75b196b7f372c87aa38b /common-parameters/src/main
parent5f1b1162d047d2a743f1ce57cc17494a6150c75c (diff)
Update junits for Validation
The Validation code was previously refactored. Added/updated junits correspondingly. Issue-ID: POLICY-2648 Change-Id: I570c0ec692ecfcb6e69ada45f7997f6e63735ea0 Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'common-parameters/src/main')
-rw-r--r--common-parameters/src/main/java/org/onap/policy/common/parameters/BeanValidator.java23
-rw-r--r--common-parameters/src/main/java/org/onap/policy/common/parameters/FieldValidator.java9
-rw-r--r--common-parameters/src/main/java/org/onap/policy/common/parameters/ItemValidator.java12
-rw-r--r--common-parameters/src/main/java/org/onap/policy/common/parameters/ValueValidator.java8
4 files changed, 28 insertions, 24 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 3f5abccc..51b11402 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
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2020-2021 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.
@@ -34,11 +34,14 @@ 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.Valid;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* Bean validator, supporting the parameter annotations.
*/
public class BeanValidator {
+ public static final Logger logger = LoggerFactory.getLogger(BeanValidator.class);
/**
* Validates top level fields within an object. For each annotated field, it retrieves
@@ -147,18 +150,18 @@ public class BeanValidator {
*/
public boolean verRegex(BeanValidationResult result, String fieldName, Pattern annot, Object value) {
try {
- if (value instanceof String && !com.google.re2j.Pattern.matches(annot.regexp(), value.toString())) {
- ObjectValidationResult result2 = new ObjectValidationResult(fieldName, xlate(value),
- ValidationStatus.INVALID, "does not match regular expression " + annot.regexp());
- result.addResult(result2);
- return false;
+ if (value instanceof String && com.google.re2j.Pattern.matches(annot.regexp(), value.toString())) {
+ return true;
}
+
} catch (RuntimeException e) {
- // TODO log at trace level
- return true;
+ logger.warn("validation error for regular expression: {}", annot.regexp(), e);
}
- return true;
+ ObjectValidationResult result2 = new ObjectValidationResult(fieldName, xlate(value), ValidationStatus.INVALID,
+ "does not match regular expression " + annot.regexp());
+ result.addResult(result2);
+ return false;
}
/**
@@ -352,7 +355,7 @@ public class BeanValidator {
*/
public boolean verMap(BeanValidationResult result, String fieldName, EntryValidator entryValidator, Object value) {
- if (!(value instanceof Map)) {
+ if (!(value instanceof Map) || entryValidator.isEmpty()) {
return true;
}
diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/FieldValidator.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/FieldValidator.java
index e762dc0e..249185c7 100644
--- a/common-parameters/src/main/java/org/onap/policy/common/parameters/FieldValidator.java
+++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/FieldValidator.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2020-2021 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.
@@ -157,6 +157,7 @@ public class FieldValidator extends ValueValidator {
* @return the annotation, or {@code null} if neither the field nor the class has the
* desired annotation
*/
+ @Override
public <T extends Annotation> T getAnnotation(Class<T> annotClass) {
// field annotation takes precedence over class annotation
@@ -178,9 +179,9 @@ public class FieldValidator extends ValueValidator {
*/
private Method getAccessor(Class<?> clazz, String fieldName) {
String capname = StringUtils.capitalize(fieldName);
- Method accessor = getMethod(clazz, "get" + capname);
- if (accessor != null) {
- return accessor;
+ Method accessor2 = getMethod(clazz, "get" + capname);
+ if (accessor2 != null) {
+ return accessor2;
}
return getMethod(clazz, "is" + capname);
diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/ItemValidator.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/ItemValidator.java
index d0c027c1..07efebbe 100644
--- a/common-parameters/src/main/java/org/onap/policy/common/parameters/ItemValidator.java
+++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/ItemValidator.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2020-2021 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.
@@ -65,6 +65,7 @@ public class ItemValidator extends ValueValidator {
* @return the annotation, or {@code null} if the {@link #annotationContainer} does
* not contain the desired annotation
*/
+ @Override
public <T extends Annotation> T getAnnotation(Class<T> annotClass) {
try {
for (Method meth : annotationContainer.getClass().getDeclaredMethods()) {
@@ -80,7 +81,10 @@ public class ItemValidator extends ValueValidator {
return null;
}
- private <T extends Annotation> T getAnnotation2(Class<T> annotClass, Method method)
+ /**
+ * Note: this is only marked "protected" so it can be overridden for junit testing.
+ */
+ protected <T extends Annotation> T getAnnotation2(Class<T> annotClass, Method method)
throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Class<?> ret = method.getReturnType();
@@ -101,7 +105,9 @@ public class ItemValidator extends ValueValidator {
return null;
}
- // TODO log if there's more than one item
+ if (arrobj.length > 1) {
+ throw new IllegalArgumentException("extra item annotations of type: " + annotClass.getName());
+ }
return arrobj[0];
}
diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/ValueValidator.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/ValueValidator.java
index 9095bfd0..6a641a17 100644
--- a/common-parameters/src/main/java/org/onap/policy/common/parameters/ValueValidator.java
+++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/ValueValidator.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2020-2021 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.
@@ -37,12 +37,6 @@ import org.onap.policy.common.parameters.annotations.NotNull;
public class ValueValidator {
/**
- * {@code True} if there is a field-level annotation, {@code false} otherwise.
- */
- @Setter(AccessLevel.PROTECTED)
- private boolean fieldIsAnnotated = false;
-
- /**
* {@code True} if the value is allowed to be {@code null}, {@code false} otherwise.
* Subclasses are expected to set this, typically based on the validation annotations
* associated with the value.