summaryrefslogtreecommitdiffstats
path: root/common-parameters
diff options
context:
space:
mode:
Diffstat (limited to 'common-parameters')
-rw-r--r--common-parameters/src/main/java/org/onap/policy/common/parameters/BeanValidator.java100
-rw-r--r--common-parameters/src/main/java/org/onap/policy/common/parameters/EntryValidator.java84
-rw-r--r--common-parameters/src/main/java/org/onap/policy/common/parameters/FieldValidator.java6
-rw-r--r--common-parameters/src/main/java/org/onap/policy/common/parameters/Item2Validator.java71
-rw-r--r--common-parameters/src/main/java/org/onap/policy/common/parameters/ItemValidator.java65
-rw-r--r--common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/Entries.java45
-rw-r--r--common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/Items.java66
-rw-r--r--common-parameters/src/test/java/org/onap/policy/common/parameters/TestBeanValidator.java6
-rw-r--r--common-parameters/src/test/java/org/onap/policy/common/parameters/TestEntryValidator.java108
-rw-r--r--common-parameters/src/test/java/org/onap/policy/common/parameters/TestItem2Validator.java121
-rw-r--r--common-parameters/src/test/java/org/onap/policy/common/parameters/TestItemValidator.java68
11 files changed, 30 insertions, 710 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 6791c616..68455ac3 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
@@ -20,14 +20,11 @@
package org.onap.policy.common.parameters;
-import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.lang3.StringUtils;
-import org.onap.policy.common.parameters.annotations.Entries;
-import org.onap.policy.common.parameters.annotations.Items;
import org.onap.policy.common.parameters.annotations.Max;
import org.onap.policy.common.parameters.annotations.Min;
import org.onap.policy.common.parameters.annotations.NotBlank;
@@ -81,8 +78,6 @@ public class BeanValidator {
validator.addAnnotation(Min.class, this::verMin);
validator.addAnnotation(Pattern.class, this::verRegex);
validator.addAnnotation(Valid.class, this::verCascade);
- validator.addAnnotation(Items.class, this::verCollection);
- validator.addAnnotation(Entries.class, this::verMap);
}
/**
@@ -277,29 +272,6 @@ public class BeanValidator {
*
* @param result where to add the validation result
* @param fieldName name of the field containing the collection
- * @param annot validation annotations for individual items
- * @param value value to be verified
- * @return {@code true} if the next check should be performed, {@code false} otherwise
- */
- public boolean verCollection(BeanValidationResult result, String fieldName, Annotation annot, Object value) {
-
- if (!(value instanceof Collection)) {
- return true;
- }
-
- ItemValidator itemValidator = makeItemValidator(annot);
- if (itemValidator.isEmpty()) {
- return true;
- }
-
- return verCollection(result, fieldName, itemValidator, value);
- }
-
- /**
- * Validates the items in a collection.
- *
- * @param result where to add the validation result
- * @param fieldName name of the field containing the collection
* @param itemValidator validator for individual items within the list
* @param value value to be verified
* @return {@code true} if the next check should be performed, {@code false} otherwise
@@ -332,57 +304,6 @@ public class BeanValidator {
*
* @param result where to add the validation result
* @param fieldName name of the field containing the map
- * @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, Entries annot, Object value) {
-
- if (!(value instanceof Map)) {
- return true;
- }
-
- EntryValidator entryValidator = makeEntryValidator(annot.key(), annot.value());
-
- return verMap(result, fieldName, entryValidator, value);
- }
-
- /**
- * Validates the items in a Map.
- *
- * @param result where to add the validation result
- * @param fieldName name of the field containing the map
- * @param entryValidator validator for individual entries within the Map
- * @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, EntryValidator entryValidator, Object value) {
-
- if (!(value instanceof Map) || entryValidator.isEmpty()) {
- return true;
- }
-
- Map<?, ?> map = (Map<?, ?>) value;
-
- BeanValidationResult result2 = new BeanValidationResult(fieldName, value);
-
- for (Entry<?, ?> entry : map.entrySet()) {
- entryValidator.validateEntry(result2, entry);
- }
-
- if (result2.isClean()) {
- return true;
- }
-
- result.addResult(result2);
- return false;
- }
-
- /**
- * Validates the items in a Map.
- *
- * @param result where to add the validation result
- * @param fieldName name of the field containing the map
* @param keyValidator validator for an individual key within the Map entry
* @param valueValidator validator for an individual value within the Map entry
* @param value value to be verified
@@ -446,27 +367,6 @@ public class BeanValidator {
}
/**
- * Makes an item validator.
- *
- * @param annot container for the item annotations
- * @return a new item validator
- */
- protected ItemValidator makeItemValidator(Annotation annot) {
- return new ItemValidator(this, annot);
- }
-
- /**
- * Makes an entry validator.
- *
- * @param keyAnnot container for the annotations associated with the entry key
- * @param valueAnnot container for the annotations associated with the entry value
- * @return a new entry validator
- */
- protected EntryValidator makeEntryValidator(Annotation keyAnnot, Annotation valueAnnot) {
- return new EntryValidator(this, keyAnnot, valueAnnot);
- }
-
- /**
* Translates a value to something printable, for use by
* {@link ObjectValidationResult}. This default method simply returns the original
* value.
diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/EntryValidator.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/EntryValidator.java
deleted file mode 100644
index 965c95e5..00000000
--- a/common-parameters/src/main/java/org/onap/policy/common/parameters/EntryValidator.java
+++ /dev/null
@@ -1,84 +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.common.parameters;
-
-import java.lang.annotation.Annotation;
-import java.util.Map;
-
-/**
- * Validator of an entry within a Map.
- */
-public class EntryValidator {
- private final ItemValidator keyValidator;
- private final ItemValidator valueValidator;
-
- /**
- * Constructs the object.
- *
- * @param validator provider of validation methods
- * @param keyAnnotationContainer an annotation containing validation annotations to be
- * applied to the entry key
- * @param valueAnnotationContainer an annotation containing validation annotations to
- * be applied to the entry value
- */
- public EntryValidator(BeanValidator validator, Annotation keyAnnotationContainer,
- Annotation valueAnnotationContainer) {
- keyValidator = new ItemValidator(validator, keyAnnotationContainer);
- valueValidator = new ItemValidator(validator, valueAnnotationContainer);
- }
-
- public boolean isEmpty() {
- return (keyValidator.isEmpty() && valueValidator.isEmpty());
- }
-
- /**
- * Performs validation of a single entry.
- *
- * @param result validation results are added here
- * @param entry value to be validated
- */
- public <K, V> void validateEntry(BeanValidationResult result, Map.Entry<K, V> entry) {
- String name = getName(entry);
-
- BeanValidationResult result2 = new BeanValidationResult(name, entry);
- keyValidator.validateValue(result2, "key", entry.getKey());
- valueValidator.validateValue(result2, "value", entry.getValue());
-
- if (!result2.isClean()) {
- result.addResult(result2);
- }
- }
-
- /**
- * Gets a name for the entry.
- *
- * @param entry entry whose name is to be determined
- * @return a name for the entry
- */
- protected <K, V> String getName(Map.Entry<K, V> entry) {
- K key = entry.getKey();
- if (key == null) {
- return "";
- }
-
- return key.toString();
- }
-}
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 58f3e833..efe48bb3 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
@@ -131,7 +131,7 @@ public class FieldValidator extends ValueValidator {
return;
}
- Item2Validator itemValidator = new Item2Validator(validator, targs[0]);
+ ItemValidator itemValidator = new ItemValidator(validator, targs[0]);
if (itemValidator.isEmpty()) {
return;
}
@@ -159,8 +159,8 @@ public class FieldValidator extends ValueValidator {
return;
}
- Item2Validator keyValidator = new Item2Validator(validator, targs[0]);
- Item2Validator valueValidator = new Item2Validator(validator, targs[1]);
+ ItemValidator keyValidator = new ItemValidator(validator, targs[0]);
+ ItemValidator valueValidator = new ItemValidator(validator, targs[1]);
if (keyValidator.isEmpty() && valueValidator.isEmpty()) {
return;
}
diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/Item2Validator.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/Item2Validator.java
deleted file mode 100644
index c82244d6..00000000
--- a/common-parameters/src/main/java/org/onap/policy/common/parameters/Item2Validator.java
+++ /dev/null
@@ -1,71 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- * ONAP
- * ================================================================================
- * Copyright (C) 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.
- * 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;
-
-import java.lang.annotation.Annotation;
-import java.lang.reflect.AnnotatedType;
-
-/**
- * Validator of an "item", which is typically found in a collection, or the key or value
- * components of an entry in a Map.
- */
-public class Item2Validator extends ValueValidator {
- private final AnnotatedType annotatedType;
-
- /**
- * Constructs the object.
- *
- * @param validator provider of validation methods
- * @param annotatedType a type having validation annotations to be
- * applied to the item
- */
- public Item2Validator(BeanValidator validator, AnnotatedType annotatedType) {
- this(validator, annotatedType, true);
- }
-
- /**
- * Constructs the object.
- *
- * @param validator provider of validation methods
- * @param annotatedType a type having validation annotations to be
- * applied to the item
- * @param addValidators {@code true} if to add validators
- */
- public Item2Validator(BeanValidator validator, AnnotatedType annotatedType, boolean addValidators) {
- this.annotatedType = annotatedType;
-
- if (addValidators) {
- validator.addValidators(this);
- }
- }
-
- /**
- * Gets an annotation from the field or the class.
- *
- * @param annotClass annotation class of interest
- * @return the annotation, or {@code null} if the {@link #annotatedType} does
- * not contain the desired annotation
- */
- @Override
- public <T extends Annotation> T getAnnotation(Class<T> annotClass) {
- return annotatedType.getAnnotation(annotClass);
- }
-}
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 07efebbe..44b70cdf 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-2021 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 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,37 +21,36 @@
package org.onap.policy.common.parameters;
import java.lang.annotation.Annotation;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
+import java.lang.reflect.AnnotatedType;
/**
* Validator of an "item", which is typically found in a collection, or the key or value
* components of an entry in a Map.
*/
public class ItemValidator extends ValueValidator {
- private final Annotation annotationContainer;
+ private final AnnotatedType annotatedType;
/**
* Constructs the object.
*
* @param validator provider of validation methods
- * @param annotationContainer an annotation containing validation annotations to be
+ * @param annotatedType a type having validation annotations to be
* applied to the item
*/
- public ItemValidator(BeanValidator validator, Annotation annotationContainer) {
- this(validator, annotationContainer, true);
+ public ItemValidator(BeanValidator validator, AnnotatedType annotatedType) {
+ this(validator, annotatedType, true);
}
/**
* Constructs the object.
*
* @param validator provider of validation methods
- * @param annotationContainer an annotation containing validation annotations to be
+ * @param annotatedType a type having validation annotations to be
* applied to the item
* @param addValidators {@code true} if to add validators
*/
- public ItemValidator(BeanValidator validator, Annotation annotationContainer, boolean addValidators) {
- this.annotationContainer = annotationContainer;
+ public ItemValidator(BeanValidator validator, AnnotatedType annotatedType, boolean addValidators) {
+ this.annotatedType = annotatedType;
if (addValidators) {
validator.addValidators(this);
@@ -62,53 +61,11 @@ public class ItemValidator extends ValueValidator {
* Gets an annotation from the field or the class.
*
* @param annotClass annotation class of interest
- * @return the annotation, or {@code null} if the {@link #annotationContainer} does
+ * @return the annotation, or {@code null} if the {@link #annotatedType} does
* not contain the desired annotation
*/
@Override
public <T extends Annotation> T getAnnotation(Class<T> annotClass) {
- try {
- for (Method meth : annotationContainer.getClass().getDeclaredMethods()) {
- T annot = getAnnotation2(annotClass, meth);
- if (annot != null) {
- return annot;
- }
- }
- } catch (RuntimeException | IllegalAccessException | InvocationTargetException e) {
- throw new IllegalArgumentException("cannot determine " + annotClass.getName(), e);
- }
-
- return null;
- }
-
- /**
- * 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();
- if (!ret.isArray()) {
- return null;
- }
-
- Class<?> comp = ret.getComponentType();
- if (comp != annotClass) {
- return null;
- }
-
- // get the array for this type of annotation
- @SuppressWarnings("unchecked")
- T[] arrobj = (T[]) method.invoke(annotationContainer);
-
- if (arrobj.length == 0) {
- return null;
- }
-
- if (arrobj.length > 1) {
- throw new IllegalArgumentException("extra item annotations of type: " + annotClass.getName());
- }
-
- return arrobj[0];
+ return annotatedType.getAnnotation(annotClass);
}
}
diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/Entries.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/Entries.java
deleted file mode 100644
index 89c9ce26..00000000
--- a/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/Entries.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.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;
-
-/**
- * Validations on entries within a Map.
- */
-@Retention(RUNTIME)
-@Target(FIELD)
-public @interface Entries {
-
- /**
- * Validations to perform on each entry's key.
- */
- Items key();
-
- /**
- * Validations to perform on each entry's value.
- */
- Items value();
-}
diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/Items.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/Items.java
deleted file mode 100644
index d022d95e..00000000
--- a/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/Items.java
+++ /dev/null
@@ -1,66 +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.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;
-
-/**
- * Validations on individual items, typically within a collection.
- */
-@Retention(RUNTIME)
-@Target(FIELD)
-public @interface Items {
-
- /**
- * 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 valid, using a {@link BeanValidator}.
- */
- Valid[] valid() default {};
-
-}
diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/TestBeanValidator.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/TestBeanValidator.java
index 00ed972c..1095ff40 100644
--- a/common-parameters/src/test/java/org/onap/policy/common/parameters/TestBeanValidator.java
+++ b/common-parameters/src/test/java/org/onap/policy/common/parameters/TestBeanValidator.java
@@ -31,7 +31,6 @@ import java.util.function.Consumer;
import lombok.Getter;
import org.junit.Before;
import org.junit.Test;
-import org.onap.policy.common.parameters.annotations.Items;
import org.onap.policy.common.parameters.annotations.Max;
import org.onap.policy.common.parameters.annotations.Min;
import org.onap.policy.common.parameters.annotations.NotBlank;
@@ -453,11 +452,10 @@ public class TestBeanValidator {
public void testVerCollection() {
@Getter
class Container {
- @Items(min = @Min(5))
- List<Integer> items;
+ List<@Min(5) Integer> items;
// not a collection - should not be checked
- @Items(valid = {@Valid})
+ @Valid
String strValue;
String noAnnotations;
diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/TestEntryValidator.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/TestEntryValidator.java
deleted file mode 100644
index 1c93d6c8..00000000
--- a/common-parameters/src/test/java/org/onap/policy/common/parameters/TestEntryValidator.java
+++ /dev/null
@@ -1,108 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- * ONAP
- * ================================================================================
- * Copyright (C) 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.
- * 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;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-import java.util.HashMap;
-import java.util.Map;
-import org.junit.Before;
-import org.junit.Test;
-import org.onap.policy.common.parameters.annotations.Items;
-import org.onap.policy.common.parameters.annotations.Min;
-import org.onap.policy.common.parameters.annotations.NotBlank;
-
-public class TestEntryValidator extends ValidatorUtil {
-
- // annotations for keys and values
-
- @Items()
- private int emptyAnnot;
-
- @Items(notBlank = {@NotBlank})
- private int keyAnnot;
-
- @Items(min = {@Min(5)})
- private int valueAnnot;
-
-
- @Before
- public void setUp() {
- bean = new BeanValidator();
- }
-
- @Test
- public void testIsEmpty() {
- // no annotations for key or value
- assertThat(new EntryValidator(bean, getAnnot("emptyAnnot"), getAnnot("emptyAnnot")).isEmpty()).isTrue();
-
- // annotations for key, value, or both
- assertThat(new EntryValidator(bean, getAnnot("keyAnnot"), getAnnot("emptyAnnot")).isEmpty()).isFalse();
- assertThat(new EntryValidator(bean, getAnnot("emptyAnnot"), getAnnot("valueAnnot")).isEmpty()).isFalse();
- assertThat(new EntryValidator(bean, getAnnot("keyAnnot"), getAnnot("valueAnnot")).isEmpty()).isFalse();
- }
-
- @Test
- public void testValidateEntry() {
- EntryValidator validator = new EntryValidator(bean, getAnnot("keyAnnot"), getAnnot("valueAnnot"));
-
- // valid key & value
- BeanValidationResult result = new BeanValidationResult(MY_NAME, this);
- validator.validateEntry(result, makeEntry(HELLO, 10));
- assertThat(result.getResult()).isNull();
-
- // invalid key
- result = new BeanValidationResult(MY_NAME, this);
- validator.validateEntry(result, makeEntry("", 20));
- assertThat(result.getResult()).doesNotContain("\"value\"").contains("\"key\"", "blank");
-
- // invalid value
- result = new BeanValidationResult(MY_NAME, this);
- validator.validateEntry(result, makeEntry(HELLO, -10));
- assertThat(result.getResult()).contains(HELLO, "\"value\"", "-10").doesNotContain("\"key\"");
-
- // both invalid
- result = new BeanValidationResult(MY_NAME, this);
- validator.validateEntry(result, makeEntry("", -100));
- assertThat(result.getResult()).contains("\"key\"", "blank", "\"value\"", "-100");
- }
-
- @Test
- public void testGetName() {
- EntryValidator validator = new EntryValidator(bean, getAnnot("emptyAnnot"), getAnnot("emptyAnnot"));
- assertThat(validator.getName(makeEntry(null, 0))).isEmpty();
- assertThat(validator.getName(makeEntry("", 0))).isEmpty();
- assertThat(validator.getName(makeEntry(HELLO, 0))).isEqualTo(HELLO);
- }
-
- /**
- * Makes a Map entry with the given key and value.
- *
- * @param key desired key
- * @param value desired value
- * @return a new Map entry
- */
- Map.Entry<String, Integer> makeEntry(String key, int value) {
- HashMap<String, Integer> map = new HashMap<>();
- map.put(key, value);
- return map.entrySet().iterator().next();
- }
-}
diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/TestItem2Validator.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/TestItem2Validator.java
deleted file mode 100644
index f8d38642..00000000
--- a/common-parameters/src/test/java/org/onap/policy/common/parameters/TestItem2Validator.java
+++ /dev/null
@@ -1,121 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- * ONAP
- * ================================================================================
- * Copyright (C) 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.
- * 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;
-
-import static java.lang.annotation.ElementType.FIELD;
-import static java.lang.annotation.RetentionPolicy.RUNTIME;
-import static org.assertj.core.api.Assertions.assertThat;
-
-import java.lang.annotation.Retention;
-import java.lang.annotation.Target;
-import org.junit.Before;
-import org.junit.Test;
-import org.onap.policy.common.parameters.annotations.Min;
-import org.onap.policy.common.parameters.annotations.NotBlank;
-import org.onap.policy.common.parameters.annotations.NotNull;
-
-public class TestItem2Validator extends ValidatorUtil {
-
- // annotated fields - each field must have exactly one annotation
-
- /**
- * This annotation does not contain a method returning an array.
- */
- @Min(value = 0)
- private int notArray;
-
- /**
- * This annotation doesn't contain any annotations that the {@link BeanValidator}
- * recognizes.
- */
- @Simple
- private int mismatch;
-
- /**
- * No annotations.
- */
- @SuppressWarnings("unused")
- private int noAnnotations;
-
- /**
- * One matching annotation.
- */
- @NotNull
- private int match;
-
- /**
- * Multiple matching annotations.
- */
- @NotNull
- @NotBlank
- private String multiMatch;
-
-
- @Before
- public void setUp() {
- bean = new BeanValidator();
- }
-
- @Test
- public void testGetAnnotation() {
- // no matches
- assertThat(new Item2Validator(bean, getAnnotType("noAnnotations"), true).isEmpty()).isTrue();
-
- // had a match
- assertThat(new Item2Validator(bean, getAnnotType("match"), true).checkers).hasSize(1);
-
- // multiple matches
- Item2Validator validator = new Item2Validator(bean, getAnnotType("multiMatch"), true);
- assertThat(validator.checkers).hasSize(2);
-
- BeanValidationResult result = new BeanValidationResult(MY_NAME, this);
- validator.validateValue(result, MY_FIELD, HELLO);
- assertThat(result.getResult()).isNull();
-
- result = new BeanValidationResult(MY_NAME, this);
- validator.validateValue(result, MY_FIELD, null);
- assertThat(result.getResult()).isNotNull();
-
- result = new BeanValidationResult(MY_NAME, this);
- validator.validateValue(result, MY_FIELD, "");
- assertThat(result.getResult()).isNotNull();
- }
-
- @Test
- public void testItem2ValidatorBeanValidatorAnnotation() {
- assertThat(new Item2Validator(bean, getAnnotType("match")).isEmpty()).isFalse();
- }
-
- @Test
- public void testItem2ValidatorBeanValidatorAnnotationBoolean() {
- assertThat(new Item2Validator(bean, getAnnotType("match"), true).isEmpty()).isFalse();
-
- assertThat(new Item2Validator(bean, getAnnotType("match"), false).isEmpty()).isTrue();
- }
-
- // these annotations are not recognized by the BeanValidator
-
- @Retention(RUNTIME)
- @Target(FIELD)
- public @interface Simple {
-
- }
-}
diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/TestItemValidator.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/TestItemValidator.java
index 2a0394dc..cadcfdee 100644
--- a/common-parameters/src/test/java/org/onap/policy/common/parameters/TestItemValidator.java
+++ b/common-parameters/src/test/java/org/onap/policy/common/parameters/TestItemValidator.java
@@ -23,15 +23,11 @@ package org.onap.policy.common.parameters;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatThrownBy;
-import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
-import java.lang.reflect.Method;
import org.junit.Before;
import org.junit.Test;
-import org.onap.policy.common.parameters.annotations.Items;
import org.onap.policy.common.parameters.annotations.Min;
import org.onap.policy.common.parameters.annotations.NotBlank;
import org.onap.policy.common.parameters.annotations.NotNull;
@@ -50,31 +46,26 @@ public class TestItemValidator extends ValidatorUtil {
* This annotation doesn't contain any annotations that the {@link BeanValidator}
* recognizes.
*/
- @SimpleItems(simple = {@Simple})
+ @Simple
private int mismatch;
/**
- * Annotation with no sub-annotations.
+ * No annotations.
*/
- @Items()
+ @SuppressWarnings("unused")
private int noAnnotations;
/**
- * One matching sub-annotation.
+ * One matching annotation.
*/
- @Items(notNull = {@NotNull})
+ @NotNull
private int match;
/**
- * Excess matching sub-annotations of a single type.
- */
- @Items(notNull = {@NotNull, @NotNull})
- private int excess;
-
- /**
* Multiple matching annotations.
*/
- @Items(notNull = {@NotNull}, notBlank = {@NotBlank})
+ @NotNull
+ @NotBlank
private String multiMatch;
@@ -86,24 +77,14 @@ public class TestItemValidator extends ValidatorUtil {
@Test
public void testGetAnnotation() {
// no matches
- assertThat(new ItemValidator(bean, getAnnot("noAnnotations"), true).isEmpty()).isTrue();
+ assertThat(new ItemValidator(bean, getAnnotType("noAnnotations"), true).isEmpty()).isTrue();
// had a match
- assertThat(new ItemValidator(bean, getAnnot("match"), true).isEmpty()).isFalse();
-
- // with an exception
- IllegalAccessException ex = new IllegalAccessException("expected exception");
-
- assertThatThrownBy(() -> new ItemValidator(bean, getAnnot("match"), true) {
- @Override
- protected <T extends Annotation> T getAnnotation2(Class<T> annotClass, Method method)
- throws IllegalAccessException {
- throw ex;
- }
- }).hasCause(ex);
+ assertThat(new ItemValidator(bean, getAnnotType("match"), true).checkers).hasSize(1);
// multiple matches
- ItemValidator validator = new ItemValidator(bean, getAnnot("multiMatch"), true);
+ ItemValidator validator = new ItemValidator(bean, getAnnotType("multiMatch"), true);
+ assertThat(validator.checkers).hasSize(2);
BeanValidationResult result = new BeanValidationResult(MY_NAME, this);
validator.validateValue(result, MY_FIELD, HELLO);
@@ -120,26 +101,14 @@ public class TestItemValidator extends ValidatorUtil {
@Test
public void testItemValidatorBeanValidatorAnnotation() {
- assertThat(new ItemValidator(bean, getAnnot("match")).isEmpty()).isFalse();
+ assertThat(new ItemValidator(bean, getAnnotType("match")).isEmpty()).isFalse();
}
@Test
public void testItemValidatorBeanValidatorAnnotationBoolean() {
- assertThat(new ItemValidator(bean, getAnnot("match"), true).isEmpty()).isFalse();
-
- assertThat(new ItemValidator(bean, getAnnot("match"), false).isEmpty()).isTrue();
- }
+ assertThat(new ItemValidator(bean, getAnnotType("match"), true).isEmpty()).isFalse();
- @Test
- public void testGetAnnotation2() {
- assertThat(new ItemValidator(bean, getAnnot("notArray"), true).isEmpty()).isTrue();
- assertThat(new ItemValidator(bean, getAnnot("mismatch"), true).isEmpty()).isTrue();
- assertThat(new ItemValidator(bean, getAnnot("noAnnotations"), true).isEmpty()).isTrue();
-
- assertThat(new ItemValidator(bean, getAnnot("match"), true).isEmpty()).isFalse();
-
- Annotation excess = getAnnot("excess");
- assertThatThrownBy(() -> new ItemValidator(bean, excess, true)).isInstanceOf(IllegalArgumentException.class);
+ assertThat(new ItemValidator(bean, getAnnotType("match"), false).isEmpty()).isTrue();
}
// these annotations are not recognized by the BeanValidator
@@ -149,13 +118,4 @@ public class TestItemValidator extends ValidatorUtil {
public @interface Simple {
}
-
- @Retention(RUNTIME)
- @Target(FIELD)
- public @interface SimpleItems {
- /**
- * Validates that it's simple.
- */
- Simple[] simple() default {};
- }
}