Search in sources :

Example 1 with BaseType

use of com.axway.ats.core.validation.types.BaseType in project ats-framework by Axway.

the class ActionMethod method createBaseTypes.

/** Creates as much validation types as needed to validate the input data */
private List<BaseType> createBaseTypes(ValidationType type, String paramName, Object values, Object[] args) {
    List<BaseType> typeValidators = new ArrayList<BaseType>();
    // of them separately to the list
    if ((values != null) && values.getClass().isArray()) {
        for (int i = 0; i < Array.getLength(values); i++) {
            Object value = Array.get(values, i);
            TypeFactory factory = TypeFactory.getInstance();
            BaseType baseType = factory.createValidationType(type, paramName, value, args);
            typeValidators.add(baseType);
        }
    // otherwise just add the single validation type
    } else {
        TypeFactory factory = TypeFactory.getInstance();
        BaseType baseType = factory.createValidationType(type, paramName, values, args);
        typeValidators.add(baseType);
    }
    return typeValidators;
}
Also used : BaseType(com.axway.ats.core.validation.types.BaseType) ArrayList(java.util.ArrayList) TypeFactory(com.axway.ats.core.validation.types.TypeFactory)

Example 2 with BaseType

use of com.axway.ats.core.validation.types.BaseType in project ats-framework by Axway.

the class Validator method validate.

/**
     * Validates the value of an object of a certain {@link ValidationType}
     * Returns true if the object is properly validated.<BR>
     * <BR>
     * This method allows the passing of additional arguments (for
     * validation types who need them).
     *
     * @param type the specific object's {@link ValidationType}
     * @param value the value of the object
     * @param args an {@link Object} array containing the arguments
     * @return true if the validation was properly validated
     * @see ValidationType
     */
public boolean validate(ValidationType type, Object value, Object[] args) {
    TypeFactory factory = TypeFactory.getInstance();
    BaseType baseType;
    if (value != null) {
        if (value.getClass().isArray()) {
            for (int i = 0; i < Array.getLength(value); i++) {
                baseType = factory.createValidationType(type, Array.get(value, i), args);
                if (!validate(baseType)) {
                    return false;
                }
            }
        } else {
            baseType = factory.createValidationType(type, value, args);
            if (!validate(baseType)) {
                return false;
            }
        }
        return true;
    }
    return false;
}
Also used : BaseType(com.axway.ats.core.validation.types.BaseType) TypeFactory(com.axway.ats.core.validation.types.TypeFactory)

Example 3 with BaseType

use of com.axway.ats.core.validation.types.BaseType in project ats-framework by Axway.

the class ActionMethod method validateArguments.

/**
     * Validate the arguments according to the rules specified in the action
     * using the Parameter annotations
     *
     * @param actionMethod      the implementation of the action
     * @param args              the arguments to validate
     * @throws ActionExecutionException     if exception occurs during arguments validation
     */
protected void validateArguments(Object[] args) throws ActionExecutionException {
    Annotation[][] annotations = method.getParameterAnnotations();
    for (int i = 0; i < annotations.length; i++) {
        Annotation[] paramAnnotations = annotations[i];
        for (Annotation paramAnnotation : paramAnnotations) {
            if (paramAnnotation instanceof Parameter) {
                Parameter paramDescriptionAnnotation = (Parameter) paramAnnotation;
                ValidationType validationType = paramDescriptionAnnotation.validation();
                String[] validationArgs;
                // the name of the array holding the valid constants
                if (validationType == ValidationType.STRING_CONSTANT || validationType == ValidationType.NUMBER_CONSTANT) {
                    try {
                        String arrayName = paramDescriptionAnnotation.args()[0];
                        // get the field and set access level if
                        // necessary
                        Field arrayField = method.getDeclaringClass().getDeclaredField(arrayName);
                        if (!arrayField.isAccessible()) {
                            arrayField.setAccessible(true);
                        }
                        Object arrayValidConstants = arrayField.get(null);
                        // convert the object array to string array
                        String[] arrayValidConstatnsStr = new String[Array.getLength(arrayValidConstants)];
                        for (int j = 0; j < Array.getLength(arrayValidConstants); j++) {
                            arrayValidConstatnsStr[j] = Array.get(arrayValidConstants, j).toString();
                        }
                        validationArgs = arrayValidConstatnsStr;
                    } catch (IndexOutOfBoundsException iobe) {
                        // this is a fatal error
                        throw new ActionExecutionException("You need to specify the name of the array with valid constants in the 'args' field of the Parameter annotation");
                    } catch (Exception e) {
                        // this is a fatal error
                        throw new ActionExecutionException("Could not get array with valid constants - action annotations are incorrect");
                    }
                } else {
                    validationArgs = paramDescriptionAnnotation.args();
                }
                List<BaseType> typeValidators = createBaseTypes(paramDescriptionAnnotation.validation(), paramDescriptionAnnotation.name(), args[i], validationArgs);
                //perform validation
                for (BaseType baseType : typeValidators) {
                    if (baseType != null) {
                        try {
                            baseType.validate();
                        } catch (TypeException e) {
                            throw new InvalidInputArgumentsException("Validation failed while validating argument " + paramDescriptionAnnotation.name() + e.getMessage());
                        }
                    } else {
                        log.warn("Could not perform validation on argument " + paramDescriptionAnnotation.name());
                    }
                }
            }
        }
    }
}
Also used : InvalidInputArgumentsException(com.axway.ats.core.validation.exceptions.InvalidInputArgumentsException) ValidationType(com.axway.ats.core.validation.ValidationType) TypeException(com.axway.ats.core.validation.exceptions.TypeException) Annotation(java.lang.annotation.Annotation) InvalidInputArgumentsException(com.axway.ats.core.validation.exceptions.InvalidInputArgumentsException) InternalComponentException(com.axway.ats.agent.core.exceptions.InternalComponentException) ActionExecutionException(com.axway.ats.agent.core.exceptions.ActionExecutionException) TypeException(com.axway.ats.core.validation.exceptions.TypeException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Field(java.lang.reflect.Field) BaseType(com.axway.ats.core.validation.types.BaseType) Parameter(com.axway.ats.agent.core.model.Parameter) ActionExecutionException(com.axway.ats.agent.core.exceptions.ActionExecutionException)

Example 4 with BaseType

use of com.axway.ats.core.validation.types.BaseType in project ats-framework by Axway.

the class Validator method createBaseTypes.

/** Creates as much validation types as needed to validate the input data */
private void createBaseTypes(ValidationType type, String paramName, Object values, Object[] args) {
    // of them separatly to the list
    if ((values != null) && values.getClass().isArray()) {
        for (int i = 0; i < Array.getLength(values); i++) {
            Object value = Array.get(values, i);
            TypeFactory factory = TypeFactory.getInstance();
            BaseType baseType = factory.createValidationType(type, paramName, value, args);
            this.typeValidators.add(baseType);
        }
    // otherwise just add the single validation type
    } else {
        TypeFactory factory = TypeFactory.getInstance();
        String message = new StringBuilder().append("Validating if parameter with the name of [").append(paramName).append("] and value [").append(values).append("] is by the type of [").append(type).append("]").toString();
        log.debug(message);
        BaseType baseType = factory.createValidationType(type, paramName, values, args);
        this.typeValidators.add(baseType);
    }
}
Also used : BaseType(com.axway.ats.core.validation.types.BaseType) TypeFactory(com.axway.ats.core.validation.types.TypeFactory)

Aggregations

BaseType (com.axway.ats.core.validation.types.BaseType)4 TypeFactory (com.axway.ats.core.validation.types.TypeFactory)3 ActionExecutionException (com.axway.ats.agent.core.exceptions.ActionExecutionException)1 InternalComponentException (com.axway.ats.agent.core.exceptions.InternalComponentException)1 Parameter (com.axway.ats.agent.core.model.Parameter)1 ValidationType (com.axway.ats.core.validation.ValidationType)1 InvalidInputArgumentsException (com.axway.ats.core.validation.exceptions.InvalidInputArgumentsException)1 TypeException (com.axway.ats.core.validation.exceptions.TypeException)1 Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1