diff options
Diffstat (limited to 'models-base/src/main')
6 files changed, 28 insertions, 412 deletions
diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfModel.java b/models-base/src/main/java/org/onap/policy/models/base/PfModel.java index 82b8f93f6..bdd652a92 100644 --- a/models-base/src/main/java/org/onap/policy/models/base/PfModel.java +++ b/models-base/src/main/java/org/onap/policy/models/base/PfModel.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019 Nordix Foundation. - * Modifications Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2019-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. @@ -159,7 +159,7 @@ public abstract class PfModel extends PfConcept { private void validateArtifactKeyInModel(final PfConceptKey artifactKey, final Set<PfConceptKey> artifactKeySet, final BeanValidationResult result) { - result.addResult(validateKeyNotNull(KEYS_TOKEN, artifactKey)); + validateKeyNotNull(result, KEYS_TOKEN, artifactKey); BeanValidationResult result2 = new BeanValidationResult(KEYS_TOKEN, artifactKey); diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfValidator.java b/models-base/src/main/java/org/onap/policy/models/base/PfValidator.java index c4e46e0b5..cbe9c2ca7 100644 --- a/models-base/src/main/java/org/onap/policy/models/base/PfValidator.java +++ b/models-base/src/main/java/org/onap/policy/models/base/PfValidator.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. @@ -20,16 +20,12 @@ package org.onap.policy.models.base; -import java.util.Map; import org.onap.policy.common.parameters.BeanValidationResult; import org.onap.policy.common.parameters.BeanValidator; -import org.onap.policy.common.parameters.EntryValidator; import org.onap.policy.common.parameters.ObjectValidationResult; import org.onap.policy.common.parameters.ValidationResult; import org.onap.policy.common.parameters.ValidationStatus; import org.onap.policy.common.parameters.ValueValidator; -import org.onap.policy.models.base.validation.annotations.PfEntries; -import org.onap.policy.models.base.validation.annotations.PfItems; import org.onap.policy.models.base.validation.annotations.PfMin; import org.onap.policy.models.base.validation.annotations.VerifyKey; @@ -39,8 +35,6 @@ public class PfValidator extends BeanValidator { protected void addValidators(ValueValidator validator) { super.addValidators(validator); - validator.addAnnotation(PfItems.class, this::verCollection); - validator.addAnnotation(PfEntries.class, this::verMap); validator.addAnnotation(VerifyKey.class, this::verKey); validator.addAnnotation(PfMin.class, this::verPfMin); } @@ -69,26 +63,6 @@ public class PfValidator extends BeanValidator { } /** - * Validates the items in a Map. - * - * @param result where to add the validation result - * @param fieldName name of the field containing the collection - * @param annot validation annotations for individual entries - * @param value value to be verified - * @return {@code true} if the next check should be performed, {@code false} otherwise - */ - public boolean verMap(BeanValidationResult result, String fieldName, PfEntries annot, Object value) { - - if (!(value instanceof Map)) { - return true; - } - - EntryValidator entryValidator = makeEntryValidator(annot.key(), annot.value()); - - return verMap(result, fieldName, entryValidator, value); - } - - /** * Invokes the value's {@link Validated#validate(String) validate()} method, if the * value is of type {@link Validated}. */ @@ -96,7 +70,14 @@ public class PfValidator extends BeanValidator { public boolean verCascade(BeanValidationResult result, String fieldName, Object value) { if (value instanceof Validated) { ValidationResult result2 = ((Validated) value).validate(fieldName); - result.addResult(result2); + if (result2 == null) { + return true; + } + + if (!result2.isClean()) { + result.addResult(result2); + } + return result2.isValid(); } diff --git a/models-base/src/main/java/org/onap/policy/models/base/Validated.java b/models-base/src/main/java/org/onap/policy/models/base/Validated.java index 2c059ba4b..5ec6ab7f7 100644 --- a/models-base/src/main/java/org/onap/policy/models/base/Validated.java +++ b/models-base/src/main/java/org/onap/policy/models/base/Validated.java @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-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. @@ -20,14 +20,7 @@ package org.onap.policy.models.base; -import com.google.re2j.Pattern; -import java.util.Collection; -import java.util.Map; -import java.util.Map.Entry; -import java.util.function.BiFunction; -import java.util.function.Function; import lombok.NonNull; -import org.apache.commons.lang3.StringUtils; import org.onap.policy.common.parameters.BeanValidationResult; import org.onap.policy.common.parameters.ObjectValidationResult; import org.onap.policy.common.parameters.ValidationResult; @@ -84,276 +77,44 @@ public class Validated { } /** - * Validates a value, if is not {@code null}, by invoking it's validate() method. - * - * @param result where to put the result - * @param fieldName name of the field containing the value - * @param value the field's value - */ - public static void validateOptional(@NonNull BeanValidationResult result, @NonNull String fieldName, - Validated value) { - if (value != null) { - result.addResult(value.validate(fieldName)); - } - } - - /** - * Validates that a value is not {@code null}. If the value is a subclass of this - * class, then it's {@link #validate(String)} method is invoked, too. - * - * @param fieldName name of the field containing the value - * @param value the field's value - * @return a result, or {@code null} - */ - public static ValidationResult validateNotNull(@NonNull String fieldName, Object value) { - if (value == null) { - return new ObjectValidationResult(fieldName, value, ValidationStatus.INVALID, IS_NULL); - } - - if (value instanceof Validated) { - return ((Validated) value).validate(fieldName); - } - - return null; - } - - /** - * Validates that a value is not "blank" (i.e., empty). value. - * - * @param fieldName name of the field containing the value - * @param value the field's value - * @param checkNull {@code true} if to validate that the value is not {@code null} - * @return a result, or {@code null} - */ - public static ValidationResult validateNotBlank(@NonNull String fieldName, String value, boolean checkNull) { - if (value == null && !checkNull) { - return null; - } - - if (StringUtils.isBlank(value)) { - return new ObjectValidationResult(fieldName, value, ValidationStatus.INVALID, IS_BLANK); - } - - return null; - } - - /** - * Validates that a value matches regular expression. - * - * @param fieldName name of the field containing the value - * @param value the field's value - * @param pattern regular expression to be matched - * @return a result, or {@code null} - */ - public static ValidationResult validateRegex(@NonNull String fieldName, String value, @NonNull String pattern) { - if (value == null) { - return makeNullResult(fieldName, value); - } - - if (!Pattern.matches(pattern, value)) { - return new ObjectValidationResult(fieldName, value, ValidationStatus.INVALID, - "does not match regular expression " + pattern); - } - - return null; - } - - /** * Validates a key, ensuring that it isn't null and that it's structurally sound. * + * @param result where to add the validation result * @param fieldName name of the field containing the key * @param key the field's value - * @return a result, or {@code null} */ - public static ValidationResult validateKeyNotNull(@NonNull String fieldName, PfKey key) { + public static void validateKeyNotNull(BeanValidationResult result, @NonNull String fieldName, PfKey key) { if (key == null) { - return new ObjectValidationResult(fieldName, key, ValidationStatus.INVALID, IS_A_NULL_KEY); + result.addResult(new ObjectValidationResult(fieldName, key, ValidationStatus.INVALID, IS_A_NULL_KEY)); + return; } if (key.isNullKey()) { - return new ObjectValidationResult(fieldName, key.getId(), ValidationStatus.INVALID, IS_A_NULL_KEY); + result.addResult(new ObjectValidationResult(fieldName, key.getId(), ValidationStatus.INVALID, + IS_A_NULL_KEY)); + return; } - return key.validate(fieldName); + result.addResult(key.validate(fieldName)); } /** * Validates a key's version, ensuring that it isn't null. * + * @param result where to add the validation result * @param fieldName name of the field containing the key * @param key the field's value - * @return a result, or {@code null} */ - public static BeanValidationResult validateKeyVersionNotNull(@NonNull String fieldName, PfConceptKey key) { + public static void validateKeyVersionNotNull(BeanValidationResult result, @NonNull String fieldName, + PfConceptKey key) { if (key != null && key.isNullVersion()) { - BeanValidationResult result = new BeanValidationResult(fieldName, key); - result.addResult(makeNullResult(PfKeyImpl.VERSION_TOKEN, key.getVersion())); - return result; - } - - return null; - } - - /** - * Generates a function to validate that a value is not below a minimum. - * - * @param min minimum value allowed - * @param allowedValue {@code null} or an allowed value outside the range - * @param checkRef {@code true} to generate an error if the value is {@code null} - * @return a function to validate that a value is not below a minimum - */ - public static BiFunction<String, Integer, ValidationResult> validateMin(int min, Integer allowedValue, - boolean checkRef) { - return (name, value) -> validateMin(name, value, min, allowedValue, checkRef); - } - - /** - * Validates that a value is not below a minimum. - * - * @param fieldName name of the field containing the key - * @param value the field's value - * @param min minimum value allowed - * @param allowedValue {@code null} or an allowed value outside the range - * @param checkRef {@code true} to generate an error if the value is {@code null} - * @return a result, or {@code null} - */ - public static ValidationResult validateMin(@NonNull String fieldName, Integer value, int min, Integer allowedValue, - boolean checkRef) { - if (value == null) { - if (checkRef) { - return makeNullResult(fieldName, value); - } - - return null; - } - - if (value < min && !value.equals(allowedValue)) { - return new ObjectValidationResult(fieldName, value, ValidationStatus.INVALID, - "is below the minimum value: " + min); - } - - return null; - } - - /** - * Validates the items in a list. - * - * @param result where to add the results - * @param fieldName name of the field containing the list - * @param list the field's list (may be {@code null}) - * @param checker function to validate in individual item in the list - */ - public static <T> void validateList(@NonNull BeanValidationResult result, @NonNull String fieldName, - Collection<T> list, @NonNull BiFunction<String, T, ValidationResult> checker) { - if (list == null) { - return; - } - - BeanValidationResult result2 = new BeanValidationResult(fieldName, list); - - int count = 0; - for (T value : list) { - result2.addResult(checker.apply(String.valueOf(count++), value)); - } - - if (!result2.isClean()) { + BeanValidationResult result2 = new BeanValidationResult(fieldName, key); + result2.addResult(makeNullResult(PfKeyImpl.VERSION_TOKEN, key.getVersion())); result.addResult(result2); } } /** - * Validates the items in a map. - * - * @param result where to add the results - * @param fieldName name of the field containing the list - * @param map the field's map (may be {@code null}) - * @param checker function to validate in individual item in the list - */ - public static <T> void validateMap(@NonNull BeanValidationResult result, @NonNull String fieldName, - Map<String, T> map, @NonNull Function<Map.Entry<String, T>, ValidationResult> checker) { - if (map == null) { - return; - } - - BeanValidationResult result2 = new BeanValidationResult(fieldName, map); - - for (Entry<String, T> entry : map.entrySet()) { - result2.addResult(checker.apply(entry)); - } - - if (!result2.isClean()) { - result.addResult(result2); - } - } - - /** - * Validates a Map entry, ensuring that neither the key nor the value are "blank" - * (i.e., empty or {@code null}). - * - * @param entry entry to be validated - * @return a result, or {@code null} - */ - public static BeanValidationResult validateEntryNotBlankNotBlank(Map.Entry<String, String> entry) { - BeanValidationResult result = new BeanValidationResult("" + entry.getKey(), entry.getKey()); - - if (StringUtils.isBlank(entry.getKey())) { - Validated.addResult(result, KEY_TOKEN, entry.getKey(), IS_BLANK); - } - - if (StringUtils.isBlank(entry.getValue())) { - Validated.addResult(result, VALUE_TOKEN, entry.getValue(), IS_BLANK); - } - - return (result.isClean() ? null : result); - } - - /** - * Validates a Map entry, ensuring that the key is not "blank" (i.e., empty or - * {@code null}) and the value is not {@code null}. - * - * @param entry entry to be validated - * @return a result, or {@code null} - */ - public static BeanValidationResult validateEntryNotBlankNotNull(Map.Entry<String, String> entry) { - BeanValidationResult result = new BeanValidationResult("" + entry.getKey(), entry.getKey()); - - if (StringUtils.isBlank(entry.getKey())) { - Validated.addResult(result, KEY_TOKEN, entry.getKey(), IS_BLANK); - } - - if (entry.getValue() == null) { - result.addResult(makeNullResult(VALUE_TOKEN, entry.getValue())); - } - - return (result.isClean() ? null : result); - } - - /** - * Validates a Map entry, ensuring that neither the key nor the value are - * {@code null}. If the value is a subclass of this class, then it's - * {@link #validate(String)} method is invoked. - * - * @param entry entry to be validated - * @return a result, or {@code null} - */ - public static <V> BeanValidationResult validateEntryValueNotNull(Map.Entry<String, V> entry) { - BeanValidationResult result = new BeanValidationResult("" + entry.getKey(), entry.getKey()); - - if (entry.getKey() == null) { - result.addResult(makeNullResult(KEY_TOKEN, entry.getKey())); - } - - V value = entry.getValue(); - if (value == null) { - result.addResult(makeNullResult(VALUE_TOKEN, value)); - } else if (value instanceof Validated) { - result.addResult(((Validated) value).validate(VALUE_TOKEN)); - } - - return (result.isClean() ? null : result); - } - - /** * Gets a key's ID, if the value is a {@link PfKey}. * * @param value value from which to get the ID diff --git a/models-base/src/main/java/org/onap/policy/models/base/validation/annotations/PfEntries.java b/models-base/src/main/java/org/onap/policy/models/base/validation/annotations/PfEntries.java deleted file mode 100644 index f3726003b..000000000 --- a/models-base/src/main/java/org/onap/policy/models/base/validation/annotations/PfEntries.java +++ /dev/null @@ -1,45 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP - * ================================================================================ - * Copyright (C) 2020 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.models.base.validation.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; - -/** - * Validations on entries within a Map. - */ -@Retention(RUNTIME) -@Target(FIELD) -public @interface PfEntries { - - /** - * Validations to perform on each entry's key. - */ - PfItems key(); - - /** - * Validations to perform on each entry's value. - */ - PfItems value(); -} diff --git a/models-base/src/main/java/org/onap/policy/models/base/validation/annotations/PfItems.java b/models-base/src/main/java/org/onap/policy/models/base/validation/annotations/PfItems.java deleted file mode 100644 index 363cf6f34..000000000 --- a/models-base/src/main/java/org/onap/policy/models/base/validation/annotations/PfItems.java +++ /dev/null @@ -1,82 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP - * ================================================================================ - * Copyright (C) 2020 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.models.base.validation.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; -import org.intellij.lang.annotations.Pattern; -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.Valid; - -/** - * Validations on individual items, typically within a collection. - */ -@Retention(RUNTIME) -@Target(FIELD) -public @interface PfItems { - - /** - * Validates the item is not {@code null}. - */ - NotNull[] notNull() default {}; - - /** - * Validates the item is not blank. - */ - NotBlank[] notBlank() default {}; - - /** - * Validates the item matches a regular expression. - */ - Pattern[] pattern() default {}; - - /** - * Validates the item is not greater than a certain value. - */ - Max[] max() default {}; - - /** - * Validates the item is not less than a certain value. - */ - Min[] min() default {}; - - /** - * Validates the item is not less than a certain value. - */ - PfMin[] pfMin() default {}; - - /** - * Validates the item is valid, using a {@link BeanValidator}. - */ - Valid[] valid() default {}; - - /** - * Validates a key. - */ - VerifyKey[] key() default {}; - -} diff --git a/models-base/src/main/java/org/onap/policy/models/base/validation/annotations/PfMin.java b/models-base/src/main/java/org/onap/policy/models/base/validation/annotations/PfMin.java index 71f001848..8b7190a6e 100644 --- a/models-base/src/main/java/org/onap/policy/models/base/validation/annotations/PfMin.java +++ b/models-base/src/main/java/org/onap/policy/models/base/validation/annotations/PfMin.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019 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. @@ -21,6 +21,7 @@ package org.onap.policy.models.base.validation.annotations; import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.TYPE_USE; import static java.lang.annotation.RetentionPolicy.RUNTIME; import java.lang.annotation.Retention; @@ -30,7 +31,7 @@ import java.lang.annotation.Target; * Same as the "Min" annotation, but allows an extra value that is not in the range. */ @Retention(RUNTIME) -@Target(FIELD) +@Target({FIELD, TYPE_USE}) public @interface PfMin { /** |