diff options
Diffstat (limited to 'common-parameters/src/main')
10 files changed, 181 insertions, 685 deletions
diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/BeanValidationResult.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/BeanValidationResult.java index 752e4d40..fa0c66c0 100644 --- a/common-parameters/src/main/java/org/onap/policy/common/parameters/BeanValidationResult.java +++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/BeanValidationResult.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-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. @@ -68,6 +68,19 @@ public class BeanValidationResult extends ValidationResultImpl { } /** + * Adds a result to this result. + * @param name name of the object of this result + * @param object object being validated + * @param status status of the new result + * @param message new result message + * @return {@code true} if the status is {@code null} or valid, {@code false} if the + * status is invalid + */ + public boolean addResult(String name, Object object, ValidationStatus status, String message) { + return addResult(new ObjectValidationResult(name, object, status, message)); + } + + /** * Validates that a sub-object within the bean is not {@code null}. * * @param subName name of the sub-object @@ -116,7 +129,7 @@ public class BeanValidationResult extends ValidationResultImpl { BeanValidationResult result = new BeanValidationResult(listName, null); for (T item : list) { if (item == null) { - result.addResult(new ObjectValidationResult("item", item, ValidationStatus.INVALID, "null")); + result.addResult("item", item, ValidationStatus.INVALID, "null"); } else { result.addResult(itemValidator.apply(item)); } 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 68455ac3..947def42 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 @@ -25,11 +25,13 @@ 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.ClassName; import org.onap.policy.common.parameters.annotations.Max; 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.annotations.Pattern; +import org.onap.policy.common.parameters.annotations.Size; import org.onap.policy.common.parameters.annotations.Valid; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -74,9 +76,11 @@ public class BeanValidator { protected void addValidators(ValueValidator validator) { validator.addAnnotation(NotNull.class, this::verNotNull); validator.addAnnotation(NotBlank.class, this::verNotBlank); + validator.addAnnotation(Size.class, this::verSize); validator.addAnnotation(Max.class, this::verMax); validator.addAnnotation(Min.class, this::verMin); validator.addAnnotation(Pattern.class, this::verRegex); + validator.addAnnotation(ClassName.class, this::verClassName); validator.addAnnotation(Valid.class, this::verCascade); } @@ -105,9 +109,7 @@ public class BeanValidator { */ public boolean verNotNull(BeanValidationResult result, String fieldName, Object value) { if (value == null) { - ObjectValidationResult result2 = - new ObjectValidationResult(fieldName, xlate(value), ValidationStatus.INVALID, "is null"); - result.addResult(result2); + result.addResult(fieldName, xlate(value), ValidationStatus.INVALID, "is null"); return false; } @@ -125,9 +127,38 @@ public class BeanValidator { */ public boolean verNotBlank(BeanValidationResult result, String fieldName, Object value) { if (value instanceof String && StringUtils.isBlank(value.toString())) { - ObjectValidationResult result2 = - new ObjectValidationResult(fieldName, xlate(value), ValidationStatus.INVALID, "is blank"); - result.addResult(result2); + result.addResult(fieldName, xlate(value), ValidationStatus.INVALID, "is blank"); + return false; + } + + return true; + } + + /** + * Verifies that the value has the specified number of elements. + * + * @param result where to add the validation result + * @param fieldName field whose value is being verified + * @param annot annotation against which the value is being verified + * @param value value to be verified + * @return {@code true} if the next check should be performed, {@code false} otherwise + */ + public boolean verSize(BeanValidationResult result, String fieldName, Size annot, Object value) { + int size; + if (value instanceof Collection) { + size = ((Collection<?>) value).size(); + + } else if (value instanceof Map) { + size = ((Map<?, ?>) value).size(); + + } else { + return true; + } + + + if (size < annot.min()) { + result.addResult(fieldName, xlate(value), ValidationStatus.INVALID, + "minimum number of elements: " + annot.min()); return false; } @@ -153,9 +184,8 @@ public class BeanValidator { logger.warn("validation error for regular expression: {}", annot.regexp(), e); } - ObjectValidationResult result2 = new ObjectValidationResult(fieldName, xlate(value), ValidationStatus.INVALID, + result.addResult(fieldName, xlate(value), ValidationStatus.INVALID, "does not match regular expression " + annot.regexp()); - result.addResult(result2); return false; } @@ -188,9 +218,8 @@ public class BeanValidator { return true; } - ObjectValidationResult result2 = new ObjectValidationResult(fieldName, xlate(value), ValidationStatus.INVALID, + result.addResult(fieldName, xlate(value), ValidationStatus.INVALID, "exceeds the maximum value: " + annot.value()); - result.addResult(result2); return false; } @@ -236,13 +265,35 @@ public class BeanValidator { return true; } - ObjectValidationResult result2 = new ObjectValidationResult(fieldName, xlate(value), ValidationStatus.INVALID, + result.addResult(fieldName, xlate(value), ValidationStatus.INVALID, "is below the minimum value: " + min); - result.addResult(result2); return false; } /** + * Verifies that the value is a valid class name. + * + * @param result where to add the validation result + * @param fieldName field whose value is being verified + * @param value value to be verified + * @return {@code true} if the next check should be performed, {@code false} otherwise + */ + public boolean verClassName(BeanValidationResult result, String fieldName, Object value) { + if (!(value instanceof String)) { + return true; + } + + try { + Class.forName(value.toString()); + return true; + + } catch (final ClassNotFoundException exp) { + result.addResult(fieldName, value, ValidationStatus.INVALID, "class is not in the classpath"); + return false; + } + } + + /** * Verifies that the value is valid by recursively invoking * {@link #validateTop(String, Object)}. * @@ -256,7 +307,8 @@ public class BeanValidator { return true; } - BeanValidationResult result2 = validateTop(fieldName, value); + BeanValidationResult result2 = (value instanceof ParameterGroup ? ((ParameterGroup) value).validate() + : validateTop(fieldName, value)); if (result2.isClean()) { return true; diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/CommonGroupValidationResult.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/CommonGroupValidationResult.java deleted file mode 100644 index ba31698a..00000000 --- a/common-parameters/src/main/java/org/onap/policy/common/parameters/CommonGroupValidationResult.java +++ /dev/null @@ -1,138 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2020 Bell Canada. 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========================================================= - */ - -package org.onap.policy.common.parameters; - -import java.util.LinkedHashMap; -import java.util.Map; - -/** - * This class holds the result of the validation of a parameter group. - */ -public abstract class CommonGroupValidationResult implements ValidationResult { - - - private final String messagePrefix; - - /** - * Validation status for the entire class. - */ - protected ValidationStatus status = ValidationStatus.CLEAN; - - /** - * Status message. - */ - protected String message; - - /** - * Validation results for each parameter in the group. - */ - protected final Map<String, ValidationResult> validationResultMap = new LinkedHashMap<>(); - - - /** - * Constructs the object. - * - * @param messagePrefix status message prefix - */ - protected CommonGroupValidationResult(String messagePrefix) { - this.messagePrefix = messagePrefix; - this.message = messagePrefix + status.toString(); - } - - /** - * Gets the status of validation. - * - * @return the status - */ - @Override - public ValidationStatus getStatus() { - return status; - } - - /** - * Set the validation result on a parameter group. - * - * @param status The validation status the parameter group is receiving - * @param message The validation message explaining the validation status - */ - @Override - public void setResult(ValidationStatus status, String message) { - setResult(status); - this.message = message; - } - - /** - * Set the validation result on a parameter group. On a sequence of calls, the most - * serious validation status is recorded, assuming the status enum ordinal increase in - * order of severity - * - * @param status The validation status the parameter group is receiving - */ - public void setResult(final ValidationStatus status) { - if (this.status.ordinal() < status.ordinal()) { - this.status = status; - this.message = messagePrefix + status; - } - } - - /** - * Gets the validation result. - * - * @param initialIndentation the indentation to use on the main result output - * @param subIndentation the indentation to use on sub parts of the result output - * @param showClean output information on clean fields - * @return the result - */ - @Override - public String getResult(final String initialIndentation, final String subIndentation, final boolean showClean) { - if (status == ValidationStatus.CLEAN && !showClean) { - return null; - } - - StringBuilder result = new StringBuilder(); - - result.append(initialIndentation); - - addGroupTypeName(result); - - result.append(status); - result.append(", "); - result.append(message); - result.append('\n'); - - for (ValidationResult fieldResult : validationResultMap.values()) { - String msg = fieldResult.getResult(initialIndentation + subIndentation, subIndentation, showClean); - if (msg != null) { - result.append(msg); - } - } - - return result.toString(); - } - - /** - * Adds the group type and name to the result string. - * - * @param result result string - */ - protected abstract void addGroupTypeName(StringBuilder result); -} diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/GroupMapValidationResult.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/GroupMapValidationResult.java deleted file mode 100644 index 2a616dba..00000000 --- a/common-parameters/src/main/java/org/onap/policy/common/parameters/GroupMapValidationResult.java +++ /dev/null @@ -1,112 +0,0 @@ -/*- - * ============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========================================================= - */ - -package org.onap.policy.common.parameters; - -import java.lang.reflect.Field; -import java.util.Map; -import java.util.Map.Entry; - -/** - * This class holds the result of the validation of a map of parameter groups. - */ -public class GroupMapValidationResult extends CommonGroupValidationResult { - // The name of the parameter group map - final String mapParameterName; - - /** - * Constructor, create the group map validation result. - * - * @param field the map parameter field - * @param mapObject the value of the map parameter field - */ - protected GroupMapValidationResult(final Field field, final Object mapObject) { - super(ParameterConstants.PARAMETER_GROUP_MAP_HAS_STATUS_MESSAGE); - - this.mapParameterName = field.getName(); - - // Cast the map object to a map of parameter groups keyed by string, we can't type check maps - // due to restrictions on generics so we have to check each entry key is a string and each entry - // value is a parameter group - @SuppressWarnings("unchecked") - Map<String, ParameterGroup> parameterGroupMap = (Map<String, ParameterGroup>) mapObject; - - // Add a validation result per map entry - for (Entry<String, ParameterGroup> parameterGroupMapEntry : parameterGroupMap.entrySet()) { - // Create a validation status entry for the map - validationResultMap.put(parameterGroupMapEntry.getKey(), - new GroupValidationResult(parameterGroupMapEntry.getValue())); - } - } - - /** - * Gets the name of the parameter being validated. - * - * @return the name - */ - @Override - public String getName() { - return mapParameterName; - } - - /** - * Set the validation result on a parameter map entry. - * - * @param entryName The name of the parameter map entry - * @param status The validation status for the entry - * @param message The validation message for the entry - */ - public void setResult(final String entryName, final ValidationStatus status, final String message) { - ValidationResult validationResult = validationResultMap.get(entryName); - if (validationResult == null) { - throw new ParameterRuntimeException("no entry with name \"" + entryName + "\" exists"); - } - - // Set the status of the parameter group and replace the field result - validationResult.setResult(status, message); - this.setResult(status); - } - - - /** - * Set the validation result on a parameter map entry. - * - * @param entryName The name of the parameter map entry - * @param mapEntryValidationResult The validation result for the entry - */ - public void setResult(final String entryName, final ValidationResult mapEntryValidationResult) { - ValidationResult validationResult = validationResultMap.get(entryName); - if (validationResult == null) { - throw new ParameterRuntimeException("no entry with name \"" + entryName + "\" exists"); - } - - // Set the status of the parameter group and replace the field result - validationResultMap.put(entryName, mapEntryValidationResult); - this.setResult(mapEntryValidationResult.getStatus()); - } - - @Override - protected void addGroupTypeName(StringBuilder result) { - result.append("parameter group map \""); - result.append(mapParameterName); - result.append("\" "); - } -}
\ No newline at end of file diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/GroupValidationResult.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/GroupValidationResult.java deleted file mode 100644 index 1fe560d8..00000000 --- a/common-parameters/src/main/java/org/onap/policy/common/parameters/GroupValidationResult.java +++ /dev/null @@ -1,390 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2018-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========================================================= - */ - -package org.onap.policy.common.parameters; - -import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.lang.reflect.Modifier; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import org.apache.commons.lang3.StringUtils; - -/** - * This class holds the result of the validation of a parameter group. - */ -public class GroupValidationResult extends CommonGroupValidationResult { - // The parameter group which the validation result applies - private final ParameterGroup parameterGroup; - - /** - * Constructor, create the field validation result with default arguments. - * - * @param parameterGroup the parameter group being validated - */ - public GroupValidationResult(final ParameterGroup parameterGroup) { - super(ParameterConstants.PARAMETER_GROUP_HAS_STATUS_MESSAGE); - - this.parameterGroup = parameterGroup; - - // Parameter group definitions may be optional - if (parameterGroup == null) { - return; - } - - // Add a validation result for all fields in the declared class - for (Field field : parameterGroup.getClass().getDeclaredFields()) { - // Check if a validation result should be added for this declared field - if (isIncludedField(field)) { - // Set a validation result for the field - validationResultMap.put(field.getName(), getSetValidationResult(field, parameterGroup)); - } - } - - // Add a validation result for protected and public fields in super classes - for (Field field : getSuperclassFields(parameterGroup.getClass().getSuperclass())) { - // Check if a validation result should be added for this declared field - if (isIncludedField(field)) { - // Set a validation result for the field - validationResultMap.putIfAbsent(field.getName(), getSetValidationResult(field, parameterGroup)); - } - } - } - - /** - * Construct a validation result for a field, updating "this" status. - * - * @param field The parameter field - * @param ParameterGroup The parameter group containing the field - * @return the validation result - * @throws Exception on accessing private fields - */ - private ValidationResult getSetValidationResult(Field field, ParameterGroup parameterGroup) { - ValidationResult result = getValidationResult(field, parameterGroup); - setResult(result.getStatus()); - - return result; - } - - /** - * Construct a validation result for a field. - * - * @param field The parameter field - * @param ParameterGroup The parameter group containing the field - * @return the validation result - * @throws Exception on accessing private fields - */ - private ValidationResult getValidationResult(final Field field, final ParameterGroup parameterGroup) { - final String fieldName = field.getName(); - final Class<?> fieldType = field.getType(); - final Object fieldObject = getObjectField(parameterGroup, field); - - // perform null checks - ParameterValidationResult result = new ParameterValidationResult(field, fieldObject); - if (!result.isValid()) { - return result; - } - - // Nested parameter groups are allowed - if (ParameterGroup.class.isAssignableFrom(fieldType)) { - if (null != fieldObject) { - return ((ParameterGroup) fieldObject).validate(); - } else { - return new GroupValidationResult((ParameterGroup) fieldObject); - } - } - - // Nested maps of parameter groups are allowed - if (Map.class.isAssignableFrom(field.getType())) { - checkMapIsParameterGroupMap(fieldName, fieldObject); - return new GroupMapValidationResult(field, fieldObject); - } - - // Collections of parameter groups are not allowed - if (Collection.class.isAssignableFrom(field.getType())) { - checkCollection4ParameterGroups(fieldName, fieldObject); - return result; - } - - // It's a regular parameter - return result; - } - - /** - * Get the value of a field in an object using a getter found with reflection. - * - * @param targetObject The object on which to read the field value - * @param fieldName The name of the field - * @return The field value - */ - private Object getObjectField(final Object targetObject, final Field field) { - String getterMethodName; - - // Check for Boolean fields, the convention for boolean getters is that they start with "is" - // If the field name already starts with "is" then the getter has the field name otherwise - // the field name is prepended with "is" - if (boolean.class.equals(field.getType())) { - if (field.getName().startsWith("is")) { - getterMethodName = field.getName(); - } else { - getterMethodName = "is" + StringUtils.capitalize(field.getName()); - } - } else { - getterMethodName = "get" + StringUtils.capitalize(field.getName()); - } - - // Look up the getter method for the field - Method getterMethod; - try { - getterMethod = targetObject.getClass().getMethod(getterMethodName, (Class<?>[]) null); - } catch (NoSuchMethodException | SecurityException e) { - throw new ParameterRuntimeException("could not get getter method for parameter \"" + field.getName() + "\"", - e); - } - - // Invoke the getter - try { - return getterMethod.invoke(targetObject, (Object[]) null); - } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { - throw new ParameterRuntimeException("error calling getter method for parameter \"" + field.getName() + "\"", - e); - } - } - - /** - * Check if this field is a map of parameter groups indexed by string keys. - * - * @param fieldName the name of the collection field. - * @param mapObject the map object to check - */ - private void checkMapIsParameterGroupMap(String fieldName, Object mapObject) { - if (mapObject == null) { - throw new ParameterRuntimeException("map parameter \"" + fieldName + "\" is null"); - } - - Map<?, ?> incomingMap = (Map<?, ?>) mapObject; - - for (Entry<?, ?> mapEntry : incomingMap.entrySet()) { - // Check the key is a string - if (!String.class.isAssignableFrom(mapEntry.getKey().getClass())) { - throw new ParameterRuntimeException("map entry is not a parameter group keyed by a string, key \"" - + mapEntry.getKey() + "\" in map \"" + fieldName + "\" is not a string"); - } - - // Check the value is a parameter group - if (!ParameterGroup.class.isAssignableFrom(mapEntry.getValue().getClass())) { - throw new ParameterRuntimeException("map entry is not a parameter group keyed by a string, value \"" - + mapEntry.getValue() + "\" in map \"" + fieldName + "\" is not a parameter group"); - } - } - } - - /** - * Check if this field contains parameter groups. - * - * @param fieldName the name of the collection field. - * @param collectionObject the collection object to check - */ - private void checkCollection4ParameterGroups(final String fieldName, final Object collectionObject) { - if (collectionObject == null) { - throw new ParameterRuntimeException("collection parameter \"" + fieldName + "\" is null"); - } - - Collection<?> collection2Check = (Collection<?>) collectionObject; - - for (Object collectionMember : collection2Check) { - if (ParameterGroup.class.isAssignableFrom(collectionMember.getClass())) { - throw new ParameterRuntimeException("collection parameter \"" + fieldName + "\" is illegal," - + " parameter groups are not allowed as collection members"); - } - } - } - - /** - * Gets the parameter group for this validation result. - * - * @return the parameter class - */ - public ParameterGroup getParameterGroup() { - return parameterGroup; - } - - /** - * Gets the name of the parameter group being validated. - * - * @return the name - */ - @Override - public String getName() { - return parameterGroup.getName(); - } - - /** - * Set the validation result on a parameter in a parameter group. - * - * @param parameterName The name of the parameter - * @param status The validation status the field is receiving - * @param message The validation message explaining the validation status - */ - public void setResult(final String parameterName, final ValidationStatus status, final String message) { - ValidationResult validationResult = validationResultMap.get(parameterName); - - if (validationResult == null) { - throw new ParameterRuntimeException("no parameter field exists for parameter: " + parameterName); - } - - // Set the status and the message on the result irrespective of validation result type - validationResult.setResult(status, message); - - // Set the status of this result - this.setResult(status); - } - - /** - * Set the validation result on a nested parameter group. - * - * @param parameterName The name of the parameter field - * @param nestedValidationResult The validation result from a nested field - */ - public void setResult(final String parameterName, final ValidationResult nestedValidationResult) { - GroupValidationResult groupValidationResult; - try { - groupValidationResult = (GroupValidationResult) validationResultMap.get(parameterName); - } catch (ClassCastException e) { - throw new ParameterRuntimeException("parameter is not a nested group parameter: " + parameterName, e); - } - - if (groupValidationResult == null) { - throw new ParameterRuntimeException("no nested parameter field exists for parameter: " + parameterName); - } - - // Set the status of the parameter group and replace the field result - validationResultMap.put(parameterName, nestedValidationResult); - this.setResult(nestedValidationResult.getStatus()); - } - - /** - * Set the validation result on a nested parameter group map entry. - * - * @param parameterName The name of the parameter field - * @param key The key of the map entry - * @param nestedMapValidationResult The validation result from a nested map entry - */ - public void setResult(final String parameterName, final String key, - final ValidationResult nestedMapValidationResult) { - GroupMapValidationResult groupMapValidationResult; - try { - groupMapValidationResult = (GroupMapValidationResult) validationResultMap.get(parameterName); - } catch (ClassCastException e) { - throw new ParameterRuntimeException("parameter is not a nested group map parameter: " + parameterName, e); - } - - if (groupMapValidationResult == null) { - throw new ParameterRuntimeException("no group map parameter field exists for parameter: " + parameterName); - } - - // Set the status of the parameter group and the field - groupMapValidationResult.setResult(key, nestedMapValidationResult); - this.setResult(nestedMapValidationResult.getStatus()); - } - - /** - * Set the validation status on a group map entry. - * - * @param parameterName The name of the parameter field - * @param key The key of the map entry - * @param status The validation status of the entry - * @param message The message for the parameter group - */ - public void setResult(final String parameterName, final String key, final ValidationStatus status, - final String message) { - GroupMapValidationResult groupMapValidationResult; - try { - groupMapValidationResult = (GroupMapValidationResult) validationResultMap.get(parameterName); - } catch (ClassCastException e) { - throw new ParameterRuntimeException("parameter is not a nested group map parameter: " + parameterName, e); - } - - if (groupMapValidationResult == null) { - throw new ParameterRuntimeException("no group map parameter field exists for parameter: " + parameterName); - } - - // Set the status of the parameter group and the field - groupMapValidationResult.setResult(key, status, message); - this.setResult(status); - } - - @Override - protected void addGroupTypeName(StringBuilder result) { - result.append("parameter group \""); - - if (parameterGroup != null) { - result.append(parameterGroup.getName()); - result.append("\" type \""); - result.append(parameterGroup.getClass().getName()); - } else { - result.append("UNDEFINED"); - } - - result.append("\" "); - } - - - /** - * Check if a field should be included for validation. - * - * @param field the field to check for inclusion - * @return true of the field should be included - */ - private boolean isIncludedField(final Field field) { - return !field.getName().startsWith("$") && !field.getName().startsWith("_") - && !Modifier.isStatic(field.getModifiers()); - } - - /** - * Get the public and protected fields of super classes. - * @param firstSuperClass the first superclass to check - * - * @return a set of the superclass fields - */ - private List<Field> getSuperclassFields(final Class<?> firstSuperClass) { - List<Field> superclassFields = new ArrayList<>(); - - Class<?> currentClass = firstSuperClass; - while (currentClass.getSuperclass() != null) { - for (Field field : currentClass.getDeclaredFields()) { - // Check if this field is public or protected - if (Modifier.isPublic(field.getModifiers()) || Modifier.isProtected(field.getModifiers())) { - superclassFields.add(field); - } - } - - // Check the next super class down - currentClass = currentClass.getSuperclass(); - } - - return superclassFields; - } -} diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/ObjectValidationResult.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/ObjectValidationResult.java index e5597206..af1884a3 100644 --- a/common-parameters/src/main/java/org/onap/policy/common/parameters/ObjectValidationResult.java +++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/ObjectValidationResult.java @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019, 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. @@ -40,6 +40,8 @@ public class ObjectValidationResult extends ValidationResultImpl { * * @param name name of the object of this result * @param object object being validated + * @param status result status + * @param message result message */ public ObjectValidationResult(String name, Object object, ValidationStatus status, String message) { super(name, object, status, message); diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterGroup.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterGroup.java index 8bfa183b..ab610933 100644 --- a/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterGroup.java +++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterGroup.java @@ -1,19 +1,20 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. + * Modifications 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. - * + * * SPDX-License-Identifier: Apache-2.0 * ============LICENSE_END========================================================= */ @@ -29,28 +30,28 @@ package org.onap.policy.common.parameters; public interface ParameterGroup { /** * Get the group name. - * + * * @return the group name */ public String getName(); /** * Set the group name. - * + * * @param name the group name */ public void setName(final String name); /** * Validate parameters. - * + * * @return the result of the parameter validation */ - GroupValidationResult validate(); + BeanValidationResult validate(); /** * Check if the parameters are valid. - * + * * @return true if the parameters are valid */ default boolean isValid() { 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 index f0f34ac1..8a987bdd 100644 --- 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 @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019, 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. @@ -20,7 +20,9 @@ package org.onap.policy.common.parameters; +import lombok.AllArgsConstructor; import lombok.Getter; +import lombok.NoArgsConstructor; import lombok.Setter; import org.onap.policy.common.parameters.annotations.NotBlank; import org.onap.policy.common.parameters.annotations.NotNull; @@ -32,30 +34,16 @@ import org.onap.policy.common.parameters.annotations.NotNull; @NotBlank @Getter @Setter +@NoArgsConstructor +@AllArgsConstructor public class ParameterGroupImpl implements ParameterGroup { /** - * Group name. Note: this MUST not be "private" or it will not be validated. + * Group name. */ - 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; - } + private String name; @Override - public GroupValidationResult validate() { - return new GroupValidationResult(this); + public BeanValidationResult validate() { + return new BeanValidator().validateTop(getClass().getSimpleName(), this); } } diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/ClassName.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/ClassName.java new file mode 100644 index 00000000..14d76fd7 --- /dev/null +++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/ClassName.java @@ -0,0 +1,37 @@ +/* + * ============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.annotations; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.TYPE_USE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +/** + * Indicates that a field (i.e., String) identifies the name of a class in the classpath. + */ +@Retention(RUNTIME) +@Target({FIELD, TYPE_USE}) +public @interface ClassName { + +} diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/Size.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/Size.java new file mode 100644 index 00000000..160e0124 --- /dev/null +++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/Size.java @@ -0,0 +1,43 @@ +/* + * ============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.annotations; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.TYPE_USE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +/** + * Indicates the size of a Map or Collection. + */ +@Retention(RUNTIME) +@Target({FIELD, TYPE_USE}) +public @interface Size { + + /** + * The minimum size allowed. + * + * @return the minimum size allowed + */ + int min(); +} |