From f9add40e0d87baff4ed56529bc61c31a577dc93d Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Mon, 4 Jan 2021 12:11:32 -0500 Subject: 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 --- .../policy/common/parameters/BeanValidator.java | 23 ++++++++++++---------- .../policy/common/parameters/FieldValidator.java | 9 +++++---- .../policy/common/parameters/ItemValidator.java | 12 ++++++++--- .../policy/common/parameters/ValueValidator.java | 8 +------- 4 files changed, 28 insertions(+), 24 deletions(-) (limited to 'common-parameters/src/main/java/org') 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 getAnnotation(Class 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 getAnnotation(Class annotClass) { try { for (Method meth : annotationContainer.getClass().getDeclaredMethods()) { @@ -80,7 +81,10 @@ public class ItemValidator extends ValueValidator { return null; } - private T getAnnotation2(Class annotClass, Method method) + /** + * Note: this is only marked "protected" so it can be overridden for junit testing. + */ + protected T getAnnotation2(Class 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. @@ -36,12 +36,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 -- cgit 1.2.3-korg