From 0af104298947b796ebd511dcabd17209bf452a06 Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Thu, 7 Mar 2019 19:51:59 -0500 Subject: Create ParameterGroupImpl Classes that implement ParameterGroup all have to add their own name and validate() fields and methods. Added an "impl" class that provides the standard functionality and modified subclasses to use it. Change-Id: Ic6ee1607fb4fe7164a4e1eeebc480ea7d1e7e4d7 Issue-ID: POLICY-1542 Signed-off-by: Jim Hahn --- .../common/parameters/ParameterGroupImpl.java | 61 ++++++++++++++++++++++ .../parameters/ParameterValidationResult.java | 28 ++++------ 2 files changed, 72 insertions(+), 17 deletions(-) create mode 100644 common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterGroupImpl.java (limited to 'common-parameters/src/main') diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterGroupImpl.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterGroupImpl.java new file mode 100644 index 00000000..f0f34ac1 --- /dev/null +++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterGroupImpl.java @@ -0,0 +1,61 @@ +/* + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.common.parameters; + +import lombok.Getter; +import lombok.Setter; +import org.onap.policy.common.parameters.annotations.NotBlank; +import org.onap.policy.common.parameters.annotations.NotNull; + +/** + * Implementation of a parameter group. + */ +@NotNull +@NotBlank +@Getter +@Setter +public class ParameterGroupImpl implements ParameterGroup { + /** + * Group name. Note: this MUST not be "private" or it will not be validated. + */ + protected String name; + + /** + * Constructs the object, with a {@code null} name. + */ + public ParameterGroupImpl() { + this.name = null; + } + + /** + * Constructs the object. + * + * @param name the group's name + */ + public ParameterGroupImpl(String name) { + this.name = name; + } + + @Override + public GroupValidationResult validate() { + return new GroupValidationResult(this); + } +} diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterValidationResult.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterValidationResult.java index e43c2d17..57232e1c 100644 --- a/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterValidationResult.java +++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterValidationResult.java @@ -49,12 +49,15 @@ public class ParameterValidationResult implements ValidationResult { this.field = field; this.parameterValue = parameterValue; - if (parameterValue == null && getAnyAnnotation(field, NotNull.class) != null) { - setResult(ValidationStatus.INVALID, "is null"); + if (parameterValue == null) { + if (getAnyAnnotation(field, NotNull.class) != null) { + setResult(ValidationStatus.INVALID, "is null"); + } - } else if (parameterValue instanceof String && getAnyAnnotation(field, NotBlank.class) != null - && parameterValue.toString().trim().isEmpty()) { - setResult(ValidationStatus.INVALID, "must be a non-blank string"); + } else if (parameterValue instanceof String) { + if (getAnyAnnotation(field, NotBlank.class) != null && parameterValue.toString().trim().isEmpty()) { + setResult(ValidationStatus.INVALID, "must be a non-blank string"); + } } else if (parameterValue instanceof Number) { Min minAnnot = field.getAnnotation(Min.class); @@ -66,7 +69,8 @@ public class ParameterValidationResult implements ValidationResult { /** * Gets an annotation for a field, first checking the field, itself, and then checking - * at the class level for the current and superclasses. + * at the class level. Does not check super classes as class-level annotations should + * only apply to the fields within the class. * * @param field field of interest * @param annotClass class of annotation that is desired @@ -78,17 +82,7 @@ public class ParameterValidationResult implements ValidationResult { return annot; } - // check class level - Class clazz = field.getDeclaringClass(); - while (clazz != Object.class) { - if ((annot = clazz.getAnnotation(annotClass)) != null) { - return annot; - } - - clazz = clazz.getSuperclass(); - } - - return null; + return field.getDeclaringClass().getAnnotation(annotClass); } /** -- cgit 1.2.3-korg