summaryrefslogtreecommitdiffstats
path: root/common-parameters/src/test/java/org/onap
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2019-03-07 14:53:12 -0500
committerJim Hahn <jrh3@att.com>2019-03-07 16:37:49 -0500
commit57a2ff2f9b918b4890f2707643342110fe31a2e4 (patch)
tree43f9e12ff1ce63f0f61e3801a4ebb83005f52456 /common-parameters/src/test/java/org/onap
parent23a3dc4ece2f1533fe1d6b627b5db05e7754a70c (diff)
Add NotNull and NotBlank parameter validation
Modified the ParameterValidator to support new NotNull and NotBlank annotations indicating that a field should not be null or blank. These annotations can be made at class level or individual field level. Moved annotations to their own subdirectory. Added a comment to a method. Extracted constant strings. Moved one annotation to the subclass level. Added support for "Min" annotation. Propagate validation errors up from nested items. Apply field-level validations, even when field is a ParameterGroup. Change-Id: Ic90df55487dc5db7b7b0be5397624d1957904a81 Issue-ID: POLICY-1542 Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'common-parameters/src/test/java/org/onap')
-rw-r--r--common-parameters/src/test/java/org/onap/policy/common/parameters/TestValidation.java270
-rw-r--r--common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/TestParametersLGeneric.java35
2 files changed, 263 insertions, 42 deletions
diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/TestValidation.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/TestValidation.java
index fb08d325..57d69f8d 100644
--- a/common-parameters/src/test/java/org/onap/policy/common/parameters/TestValidation.java
+++ b/common-parameters/src/test/java/org/onap/policy/common/parameters/TestValidation.java
@@ -1,19 +1,20 @@
-/*-
+/*
* ============LICENSE_START=======================================================
* Copyright (C) 2018 Ericsson. All rights reserved.
+ * Modifications Copyright (C) 2019 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.
- *
+ *
* SPDX-License-Identifier: Apache-2.0
* ============LICENSE_END=========================================================
*/
@@ -28,12 +29,43 @@ import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
-
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;
import org.onap.policy.common.parameters.testclasses.TestParametersL00;
import org.onap.policy.common.parameters.testclasses.TestParametersL10;
public class TestValidation {
+ private static final String NOT_BLANK_STRING_MESSAGE =
+ "field 'notBlankString' type 'java.lang.String' value '' INVALID, must be a non-blank string\n"
+ .replace('\'', '"');
+
+ private static final String NULL_STRING_MESSAGE =
+ "field 'notNullString' type 'java.lang.String' value 'null' INVALID, is null\n".replace('\'', '"');
+
+
+ private static final String NOT_BLANK_OBJECT_NAME = "notBlankObject";
+ private static final String NOT_BLANK_STRING_NAME = "notBlankString";
+ private static final String NOT_NULL_OBJECT_NAME = "notNullObject";
+ private static final String NOT_NULL_STRING_NAME = "notNullString";
+ private static final String MIN_LONG_NAME = "minLong";
+
+ @NotNull
+ private String notNullString;
+
+ @NotNull
+ private Object notNullObject;
+
+ @NotBlank
+ private String notBlankString;
+
+ @NotBlank
+ private Object notBlankObject;
+
+ @Min(value = 10)
+ private long minLong;
+
@Test
public void testValidationOk() throws IOException {
TestParametersL00 l0Parameters = new TestParametersL00("l0Parameters");
@@ -50,11 +82,11 @@ public class TestValidation {
.replaceAll("\\s+", "");
assertEquals(expectedResult, validationResult.getResult("", " ", true).replaceAll("\\s+", ""));
}
-
+
@Test
public void testValidationObservation() throws IOException {
TestParametersL00 l0Parameters = new TestParametersL00("l0Parameters");
-
+
l0Parameters.triggerValidationStatus(ValidationStatus.OBSERVATION, 3);
String expectedResult = new String(Files.readAllBytes(
@@ -65,7 +97,7 @@ public class TestValidation {
assertTrue(validationResult.isValid());
assertFalse(validationResult.isClean());
assertEquals(expectedResult, validationResult.getResult().replaceAll("\\s+", ""));
-
+
l0Parameters.triggerValidationStatus(ValidationStatus.CLEAN, 3);
l0Parameters.triggerValidationStatus(ValidationStatus.OBSERVATION, 2);
@@ -76,7 +108,7 @@ public class TestValidation {
validationResult = l0Parameters.validate();
assertTrue(validationResult.isValid());
assertEquals(expectedResult, validationResult.getResult().replaceAll("\\s+", ""));
-
+
l0Parameters.triggerValidationStatus(ValidationStatus.CLEAN, 3);
l0Parameters.triggerValidationStatus(ValidationStatus.OBSERVATION, 1);
@@ -87,7 +119,7 @@ public class TestValidation {
validationResult = l0Parameters.validate();
assertTrue(validationResult.isValid());
assertEquals(expectedResult, validationResult.getResult().replaceAll("\\s+", ""));
-
+
l0Parameters.triggerValidationStatus(ValidationStatus.CLEAN, 3);
l0Parameters.triggerValidationStatus(ValidationStatus.OBSERVATION, 0);
@@ -95,11 +127,11 @@ public class TestValidation {
assertTrue(validationResult.isValid());
assertEquals(null, validationResult.getResult());
}
-
+
@Test
public void testValidationWarning() throws IOException {
TestParametersL00 l0Parameters = new TestParametersL00("l0Parameters");
-
+
l0Parameters.triggerValidationStatus(ValidationStatus.WARNING, 3);
String expectedResult = new String(Files.readAllBytes(
@@ -109,7 +141,7 @@ public class TestValidation {
GroupValidationResult validationResult = l0Parameters.validate();
assertTrue(validationResult.isValid());
assertEquals(expectedResult, validationResult.getResult().replaceAll("\\s+", ""));
-
+
l0Parameters.triggerValidationStatus(ValidationStatus.CLEAN, 3);
l0Parameters.triggerValidationStatus(ValidationStatus.WARNING, 2);
@@ -120,7 +152,7 @@ public class TestValidation {
validationResult = l0Parameters.validate();
assertTrue(validationResult.isValid());
assertEquals(expectedResult, validationResult.getResult().replaceAll("\\s+", ""));
-
+
l0Parameters.triggerValidationStatus(ValidationStatus.CLEAN, 3);
l0Parameters.triggerValidationStatus(ValidationStatus.WARNING, 1);
@@ -131,7 +163,7 @@ public class TestValidation {
validationResult = l0Parameters.validate();
assertTrue(validationResult.isValid());
assertEquals(expectedResult, validationResult.getResult().replaceAll("\\s+", ""));
-
+
l0Parameters.triggerValidationStatus(ValidationStatus.CLEAN, 3);
l0Parameters.triggerValidationStatus(ValidationStatus.WARNING, 0);
@@ -139,11 +171,11 @@ public class TestValidation {
assertTrue(validationResult.isValid());
assertEquals(null, validationResult.getResult());
}
-
+
@Test
public void testValidationInvalid() throws IOException {
TestParametersL00 l0Parameters = new TestParametersL00("l0Parameters");
-
+
l0Parameters.triggerValidationStatus(ValidationStatus.INVALID, 3);
String expectedResult = new String(Files.readAllBytes(
@@ -153,7 +185,7 @@ public class TestValidation {
GroupValidationResult validationResult = l0Parameters.validate();
assertFalse(validationResult.isValid());
assertEquals(expectedResult, validationResult.getResult().replaceAll("\\s+", ""));
-
+
l0Parameters.triggerValidationStatus(ValidationStatus.CLEAN, 3);
l0Parameters.triggerValidationStatus(ValidationStatus.INVALID, 2);
@@ -164,7 +196,7 @@ public class TestValidation {
validationResult = l0Parameters.validate();
assertFalse(validationResult.isValid());
assertEquals(expectedResult, validationResult.getResult().replaceAll("\\s+", ""));
-
+
l0Parameters.triggerValidationStatus(ValidationStatus.CLEAN, 3);
l0Parameters.triggerValidationStatus(ValidationStatus.INVALID, 1);
@@ -175,7 +207,7 @@ public class TestValidation {
validationResult = l0Parameters.validate();
assertFalse(validationResult.isValid());
assertEquals(expectedResult, validationResult.getResult().replaceAll("\\s+", ""));
-
+
l0Parameters.triggerValidationStatus(ValidationStatus.CLEAN, 3);
l0Parameters.triggerValidationStatus(ValidationStatus.INVALID, 0);
@@ -183,7 +215,7 @@ public class TestValidation {
assertTrue(validationResult.isValid());
assertEquals(null, validationResult.getResult());
}
-
+
@Test
public void testValidationEmptySubGroup() throws IOException {
TestParametersL10 l10Parameters = new TestParametersL10("l10Parameters");
@@ -192,7 +224,201 @@ public class TestValidation {
GroupValidationResult validationResult = l10Parameters.validate();
assertTrue(validationResult.isValid());
-
+
assertTrue(validationResult.getResult("", "", true).contains("UNDEFINED"));
}
+
+ @Test
+ public void testGetValidationResult() throws Exception {
+ Contained item = new Contained();
+ item.setName("item");
+
+ Container cont = new Container();
+ cont.item = item;
+ GroupValidationResult result = cont.validate();
+ assertEquals(ValidationStatus.INVALID, result.getStatus());
+ assertTrue(result.getResult().contains(">= 1"));
+
+ item.minInt = 1000;
+ result = cont.validate();
+ assertEquals(ValidationStatus.CLEAN, result.getStatus());
+
+ cont.item = null;
+ result = cont.validate();
+ assertEquals(ValidationStatus.INVALID, result.getStatus());
+ assertTrue(result.getResult().contains("is null"));
+ }
+
+ @Test
+ public void testParameterValidationResult_NotNull() throws Exception {
+ ParameterValidationResult result = new ParameterValidationResult(
+ TestValidation.class.getDeclaredField(NOT_NULL_STRING_NAME), null);
+ assertEquals(ValidationStatus.INVALID, result.getStatus());
+ assertEquals(NULL_STRING_MESSAGE, result.getResult());
+
+ // don't allow overwrite - values should remain unchanged
+ result.setResult(ValidationStatus.WARNING, "unknown");
+ assertEquals(ValidationStatus.INVALID, result.getStatus());
+ assertEquals(NULL_STRING_MESSAGE, result.getResult());
+
+ // non-null should be OK
+ result = new ParameterValidationResult(TestValidation.class.getDeclaredField(NOT_NULL_STRING_NAME), "");
+ assertEquals(ValidationStatus.CLEAN, result.getStatus());
+
+ // non-null should be OK
+ result = new ParameterValidationResult(TestValidation.class.getDeclaredField(NOT_NULL_STRING_NAME), "abc");
+ assertEquals(ValidationStatus.CLEAN, result.getStatus());
+
+ /*
+ * Check plain object fields, too.
+ */
+ result = new ParameterValidationResult(TestValidation.class.getDeclaredField(NOT_NULL_OBJECT_NAME), null);
+ assertEquals(ValidationStatus.INVALID, result.getStatus());
+ assertEquals("field 'notNullObject' type 'java.lang.Object' value 'null' INVALID, is null\n".replace('\'', '"'),
+ result.getResult());
+
+ // non-null should be OK
+ result = new ParameterValidationResult(TestValidation.class.getDeclaredField(NOT_NULL_OBJECT_NAME),
+ new Object());
+ assertEquals(ValidationStatus.CLEAN, result.getStatus());
+
+ /*
+ * Class-level annotation.
+ */
+
+ result = new ParameterValidationResult(NotNullSub.class.getDeclaredField(NOT_NULL_STRING_NAME), null);
+ assertEquals(ValidationStatus.INVALID, result.getStatus());
+ assertEquals(NULL_STRING_MESSAGE, result.getResult());
+
+ // non-null should be OK
+ result = new ParameterValidationResult(NotNullSub.class.getDeclaredField(NOT_NULL_STRING_NAME), "");
+ assertEquals(ValidationStatus.CLEAN, result.getStatus());
+ }
+
+ @Test
+ public void testParameterValidationResult_NotBlank() throws Exception {
+ ParameterValidationResult result =
+ new ParameterValidationResult(TestValidation.class.getDeclaredField(NOT_BLANK_STRING_NAME), "");
+ assertEquals(ValidationStatus.INVALID, result.getStatus());
+ assertEquals(NOT_BLANK_STRING_MESSAGE, result.getResult());
+
+ // spaces only
+ result = new ParameterValidationResult(TestValidation.class.getDeclaredField(NOT_BLANK_STRING_NAME), " \t");
+ assertEquals(ValidationStatus.INVALID, result.getStatus());
+
+ // null should be OK
+ result = new ParameterValidationResult(TestValidation.class.getDeclaredField(NOT_BLANK_STRING_NAME), null);
+ assertEquals(ValidationStatus.CLEAN, result.getStatus());
+
+ // null should be OK
+ result = new ParameterValidationResult(TestValidation.class.getDeclaredField(NOT_BLANK_STRING_NAME), "abc");
+ assertEquals(ValidationStatus.CLEAN, result.getStatus());
+
+ /*
+ * Check plain object fields, too.
+ */
+ result = new ParameterValidationResult(TestValidation.class.getDeclaredField(NOT_BLANK_OBJECT_NAME), null);
+ assertEquals(ValidationStatus.CLEAN, result.getStatus());
+
+ result = new ParameterValidationResult(TestValidation.class.getDeclaredField(NOT_BLANK_OBJECT_NAME),
+ new Object());
+ assertEquals(ValidationStatus.CLEAN, result.getStatus());
+
+ /*
+ * Class-level annotation.
+ */
+ result = new ParameterValidationResult(NotBlankSub.class.getDeclaredField(NOT_BLANK_STRING_NAME), "");
+ assertEquals(ValidationStatus.INVALID, result.getStatus());
+ assertEquals(NOT_BLANK_STRING_MESSAGE, result.getResult());
+
+ // non-null should be OK
+ result = new ParameterValidationResult(NotBlankSub.class.getDeclaredField(NOT_BLANK_STRING_NAME), "abc");
+ assertEquals(ValidationStatus.CLEAN, result.getStatus());
+ }
+
+ @Test
+ public void testParameterValidationResult_Min() throws Exception {
+ ParameterValidationResult result =
+ new ParameterValidationResult(TestValidation.class.getDeclaredField(MIN_LONG_NAME), 9L);
+ assertEquals(ValidationStatus.INVALID, result.getStatus());
+ assertEquals("field 'minLong' type 'long' value '9' INVALID, must be >= 10\n".replace('\'', '"'),
+ result.getResult());
+
+ result = new ParameterValidationResult(TestValidation.class.getDeclaredField(MIN_LONG_NAME), -2);
+ assertEquals(ValidationStatus.INVALID, result.getStatus());
+ assertEquals("field 'minLong' type 'long' value '-2' INVALID, must be >= 10\n".replace('\'', '"'),
+ result.getResult());
+
+ result = new ParameterValidationResult(TestValidation.class.getDeclaredField(MIN_LONG_NAME), 10L);
+ assertEquals(ValidationStatus.CLEAN, result.getStatus());
+
+ result = new ParameterValidationResult(TestValidation.class.getDeclaredField(MIN_LONG_NAME), 11);
+ assertEquals(ValidationStatus.CLEAN, result.getStatus());
+ }
+
+ // these classes are used to test class-level annotations
+
+ @NotNull
+ private static class NotNullBase {
+
+ }
+
+ private static class NotNullSub extends NotNullBase {
+ @SuppressWarnings("unused")
+ private String notNullString;
+ }
+
+ private static class NotBlankBase {
+
+ }
+
+ @NotBlank
+ private static class NotBlankSub extends NotBlankBase {
+ @SuppressWarnings("unused")
+ private String notBlankString;
+ }
+
+ private abstract static class SimpleGroup implements ParameterGroup {
+ private String name;
+
+ @Override
+ public String getName() {
+ return name;
+ }
+
+ @Override
+ public void setName(String name) {
+ this.name = name;
+ }
+ }
+
+ private static class Contained extends SimpleGroup {
+ @Min(value = 1)
+ private int minInt;
+
+ @Override
+ public GroupValidationResult validate() {
+ return new GroupValidationResult(this);
+ }
+
+ @SuppressWarnings("unused")
+ public int getMinInt() {
+ return minInt;
+ }
+ }
+
+ private static class Container extends SimpleGroup {
+ @NotNull
+ private Contained item;
+
+ @Override
+ public GroupValidationResult validate() {
+ return new GroupValidationResult(this);
+ }
+
+ @SuppressWarnings("unused")
+ public Contained getItem() {
+ return item;
+ }
+ }
}
diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/TestParametersLGeneric.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/TestParametersLGeneric.java
index 2d263fc7..1e5764c6 100644
--- a/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/TestParametersLGeneric.java
+++ b/common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/TestParametersLGeneric.java
@@ -1,19 +1,20 @@
-/*-
+/*
* ============LICENSE_START=======================================================
* Copyright (C) 2018 Ericsson. All rights reserved.
+ * Modifications Copyright (C) 2019 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.
- *
+ *
* SPDX-License-Identifier: Apache-2.0
* ============LICENSE_END=========================================================
*/
@@ -21,25 +22,28 @@
package org.onap.policy.common.parameters.testclasses;
import org.onap.policy.common.parameters.GroupValidationResult;
-import org.onap.policy.common.parameters.ParameterConstants;
import org.onap.policy.common.parameters.ParameterGroup;
import org.onap.policy.common.parameters.ValidationStatus;
+import org.onap.policy.common.parameters.annotations.NotBlank;
+import org.onap.policy.common.parameters.annotations.NotNull;
public class TestParametersLGeneric implements ParameterGroup {
private String name;
private int lgenericIntField = 0;
+
+ @NotNull @NotBlank
private String lgenericStringField = "Legal " + this.getClass().getCanonicalName();
-
+
/**
* Default constructor.
*/
public TestParametersLGeneric() {
// Default Constructor
}
-
+
/**
* Create a test parameter group.
- *
+ *
* @param name the parameter group name
*/
public TestParametersLGeneric(final String name) {
@@ -68,7 +72,7 @@ public class TestParametersLGeneric implements ParameterGroup {
/**
* Trigger a validation message.
- *
+ *
* @param level Number of levels to recurse before stopping
*/
public void triggerValidationStatus(final ValidationStatus triggerStatus, int level) {
@@ -111,18 +115,12 @@ public class TestParametersLGeneric implements ParameterGroup {
public GroupValidationResult validate() {
GroupValidationResult validationResult = new GroupValidationResult(this);
- if (lgenericStringField == null || lgenericStringField.trim().length() == 0) {
- validationResult.setResult("lgenericStringField", ValidationStatus.INVALID,
- "lgenericStringField must be a non-blank string");
- } else if (lgenericStringField.equals("lgenericStringField")) {
+ if ("lgenericStringField".equals(lgenericStringField)) {
validationResult.setResult("lgenericStringField", ValidationStatus.WARNING,
"using the field name for the parameter value is dangerous");
- } else if (lgenericStringField.equals("aString")) {
+ } else if ("aString".equals(lgenericStringField)) {
validationResult.setResult("lgenericStringField", ValidationStatus.OBSERVATION,
"this value for name is unhelpful");
- } else {
- validationResult.setResult("lgenericStringField", ValidationStatus.CLEAN,
- ParameterConstants.PARAMETER_HAS_STATUS_MESSAGE + ValidationStatus.CLEAN.toString());
}
if (lgenericIntField < 0) {
@@ -134,9 +132,6 @@ public class TestParametersLGeneric implements ParameterGroup {
} else if (lgenericIntField == 2) {
validationResult.setResult("lgenericIntField", ValidationStatus.OBSERVATION,
"this field has been set to 2");
- } else {
- validationResult.setResult("lgenericIntField", ValidationStatus.CLEAN,
- ParameterConstants.PARAMETER_HAS_STATUS_MESSAGE + ValidationStatus.CLEAN.toString());
}
return validationResult;