From 57a2ff2f9b918b4890f2707643342110fe31a2e4 Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Thu, 7 Mar 2019 14:53:12 -0500 Subject: 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 --- .../testclasses/TestParametersLGeneric.java | 35 ++++++++++------------ 1 file changed, 15 insertions(+), 20 deletions(-) (limited to 'common-parameters/src/test/java/org/onap/policy/common/parameters/testclasses/TestParametersLGeneric.java') 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; -- cgit 1.2.3-korg