aboutsummaryrefslogtreecommitdiffstats
path: root/common-parameters/src/test/java/org/onap/policy/common/parameters/TestFieldValidator.java
diff options
context:
space:
mode:
Diffstat (limited to 'common-parameters/src/test/java/org/onap/policy/common/parameters/TestFieldValidator.java')
-rw-r--r--common-parameters/src/test/java/org/onap/policy/common/parameters/TestFieldValidator.java274
1 files changed, 274 insertions, 0 deletions
diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/TestFieldValidator.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/TestFieldValidator.java
new file mode 100644
index 00000000..e4432c8d
--- /dev/null
+++ b/common-parameters/src/test/java/org/onap/policy/common/parameters/TestFieldValidator.java
@@ -0,0 +1,274 @@
+/*-
+ * ============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 static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import lombok.Getter;
+import org.junit.Before;
+import org.junit.Test;
+import org.onap.policy.common.parameters.annotations.Min;
+import org.onap.policy.common.parameters.annotations.NotNull;
+
+public class TestFieldValidator extends ValidatorUtil {
+ private static final String UNANNOTATED_FIELD = "unannotated";
+ private static final String INT_FIELD = "intValue";
+ private static final int VALID_INT = 10;
+ private static final int INVALID_INT = -10;
+
+ @Getter
+ private int unannotated;
+
+ @Min(0)
+ @Getter
+ private int intValue;
+
+ @NotNull
+ @Getter
+ private boolean boolValue;
+
+ @NotNull
+ @Getter
+ private String notNullValue;
+
+ @Min(0)
+ @Getter
+ private static int staticField;
+
+ /**
+ * Has no accessor.
+ */
+ @Min(0)
+ private int noMethod;
+
+ /**
+ * Accessor is {@link #getStaticMethod()}, which is static.
+ */
+ @Min(0)
+ private int staticMethod;
+
+ /**
+ * Accessor is {@link #getVoidMethod()}, which returns a void.
+ */
+ @Min(0)
+ private int voidMethod;
+
+ /**
+ * Accessor is {@link #getParameterizedMethod()}, which requires a parameter.
+ */
+ @Min(0)
+ private int parameterizedMethod;
+
+ /**
+ * Accessor is {@link #getExMethod()}, which throws an exception.
+ */
+ @Min(0)
+ private int exMethod;
+
+
+ @Before
+ public void setUp() {
+ bean = new BeanValidator();
+ }
+
+ @Test
+ public void testGetAnnotation() {
+ // field-level annotation
+ assertThat(new FieldValidator(bean, TestFieldValidator.class, getField(INT_FIELD)).isEmpty()).isFalse();
+
+ // class-level annotation
+ assertThat(new FieldValidator(bean, ClassAnnot.class, getField(ClassAnnot.class, "text")).isEmpty()).isFalse();
+ }
+
+ @Test
+ public void testFieldValidator() throws NoSuchFieldException, SecurityException {
+ /*
+ * Note: nested classes contain fields like "$this", thus the check for "$" in the
+ * variable name is already covered by the other tests.
+ */
+
+ /*
+ * Class with no annotations.
+ */
+ @NotNull
+ class NoAnnotations {
+ @SuppressWarnings("unused")
+ String strValue;
+ }
+
+ Field field = NoAnnotations.class.getDeclaredField("this$0");
+
+ assertThat(new FieldValidator(bean, NoAnnotations.class, field).isEmpty()).isTrue();
+
+ // unannotated
+ assertThat(new FieldValidator(bean, TestFieldValidator.class, getField("unannotated")).isEmpty()).isTrue();
+
+ // these are invalid for various reasons
+
+ Field staticField2 = getField("staticField");
+ assertThatThrownBy(() -> new FieldValidator(bean, TestFieldValidator.class, staticField2))
+ .isInstanceOf(IllegalArgumentException.class);
+
+ Field noMethodField = getField("noMethod");
+ assertThatThrownBy(() -> new FieldValidator(bean, TestFieldValidator.class, noMethodField))
+ .isInstanceOf(IllegalArgumentException.class);
+
+ // annotated
+ assertThat(new FieldValidator(bean, TestFieldValidator.class, getField(INT_FIELD)).isEmpty()).isFalse();
+ }
+
+ @Test
+ public void testFieldValidator_SetNullAllowed() {
+ // default - null is allowed
+ assertThat(new FieldValidator(bean, TestFieldValidator.class, getField(INT_FIELD)).isNullAllowed()).isTrue();
+
+ // field-level NotNull
+ assertThat(new FieldValidator(bean, TestFieldValidator.class, getField("notNullValue")).isNullAllowed())
+ .isFalse();
+
+ // class-level NotNull
+ assertThat(new FieldValidator(bean, ClassAnnot.class, getField(ClassAnnot.class, "noMethod")).isNullAllowed())
+ .isFalse();
+ }
+
+ @Test
+ public void testValidateField_testGetValue() {
+ // unannotated
+ BeanValidationResult result = new BeanValidationResult(MY_NAME, this);
+ new FieldValidator(bean, getClass(), getField(UNANNOTATED_FIELD)).validateField(result, this);
+ assertThat(result.getResult()).isNull();
+
+ // valid
+ intValue = VALID_INT;
+ result = new BeanValidationResult(MY_NAME, this);
+ new FieldValidator(bean, getClass(), getField(INT_FIELD)).validateField(result, this);
+ assertThat(result.getResult()).isNull();
+
+ // invalid
+ intValue = INVALID_INT;
+ result = new BeanValidationResult(MY_NAME, this);
+ new FieldValidator(bean, getClass(), getField(INT_FIELD)).validateField(result, this);
+ assertThat(result.getResult()).contains(INT_FIELD);
+
+ // throws an exception
+ FieldValidator validator = new FieldValidator(bean, TestFieldValidator.class, getField("exMethod"));
+ BeanValidationResult result2 = new BeanValidationResult(MY_NAME, this);
+ assertThatThrownBy(() -> validator.validateField(result2, this)).isInstanceOf(IllegalArgumentException.class)
+ .getCause().isInstanceOf(InvocationTargetException.class).getCause()
+ .hasMessage("expected exception");
+ }
+
+ @Test
+ public void testClassOnly() {
+ // class-level annotation has no bearing on a static field
+ assertThat(new FieldValidator(bean, ClassAnnot.class, getField(ClassAnnot.class, "staticValue")).isEmpty())
+ .isTrue();
+
+ // field-level annotation on a static field
+ Field staticField2 = getField("staticField");
+ assertThatThrownBy(() -> new FieldValidator(bean, TestFieldValidator.class, staticField2))
+ .isInstanceOf(IllegalArgumentException.class);
+ }
+
+ @Test
+ public void testGetAccessor() {
+ // uses "getXxx"
+ assertThat(new FieldValidator(bean, TestFieldValidator.class, getField(INT_FIELD)).isEmpty()).isFalse();
+
+ // uses "isXxx"
+ assertThat(new FieldValidator(bean, TestFieldValidator.class, getField("boolValue")).isEmpty()).isFalse();
+ }
+
+ @Test
+ public void testGetMethod() {
+ assertThat(new FieldValidator(bean, TestFieldValidator.class, getField(INT_FIELD)).isEmpty()).isFalse();
+
+ // these are invalid for various reasons
+
+ Field noMethodField = getField("noMethod");
+ assertThatThrownBy(() -> new FieldValidator(bean, TestFieldValidator.class, noMethodField))
+ .isInstanceOf(IllegalArgumentException.class);
+
+ Field staticMethodField = getField("staticMethod");
+ assertThatThrownBy(() -> new FieldValidator(bean, TestFieldValidator.class, staticMethodField))
+ .isInstanceOf(IllegalArgumentException.class);
+ }
+
+ @Test
+ public void testValidMethod() {
+ assertThat(new FieldValidator(bean, TestFieldValidator.class, getField(INT_FIELD)).isEmpty()).isFalse();
+
+ // these are invalid for various reasons
+
+ Field staticMethodField = getField("staticMethod");
+ assertThatThrownBy(() -> new FieldValidator(bean, TestFieldValidator.class, staticMethodField))
+ .isInstanceOf(IllegalArgumentException.class);
+
+ Field voidMethodField = getField("voidMethod");
+ assertThatThrownBy(() -> new FieldValidator(bean, TestFieldValidator.class, voidMethodField))
+ .isInstanceOf(IllegalArgumentException.class);
+
+ Field paramMethodField = getField("parameterizedMethod");
+ assertThatThrownBy(() -> new FieldValidator(bean, TestFieldValidator.class, paramMethodField))
+ .isInstanceOf(IllegalArgumentException.class);
+ }
+
+ @Test
+ public void testIsFieldAnnotated_testSetFieldAnnotated() {
+ // annotated at the field level
+ assertThat(new FieldValidator(bean, getClass(), getField(INT_FIELD)).isFieldAnnotated()).isTrue();
+
+ // unannotated
+ assertThat(new FieldValidator(bean, getClass(), getField(UNANNOTATED_FIELD)).isFieldAnnotated()).isFalse();
+ }
+
+ public static int getStaticMethod() {
+ return -1000;
+ }
+
+ public void getVoidMethod() {
+ // do nothing
+ }
+
+ public int getParameterizedMethod(boolean flag) {
+ return 0;
+ }
+
+ public int getExMethod() {
+ throw new RuntimeException("expected exception");
+ }
+
+ @NotNull
+ public static class ClassAnnot {
+ @Getter
+ private String text;
+
+ // no "get" method
+ @SuppressWarnings("unused")
+ private String noMethod;
+
+ @Getter
+ private static int staticValue;
+ }
+}