Search in sources :

Example 6 with ActionExecutionException

use of com.axway.ats.agent.core.exceptions.ActionExecutionException 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 7 with ActionExecutionException

use of com.axway.ats.agent.core.exceptions.ActionExecutionException in project ats-framework by Axway.

the class ActionMethod method convertToEnums.

/**
     * Convert any String arguments to proper Enumerations if
     * necessary
     *
     * @param args the arguments
     * @return arguments with Strings converted to Enums
     * @throws ActionExecutionException if a given String cannot be converted to the proper Enum
     */
@SuppressWarnings({ "rawtypes", "unchecked" })
protected Object[] convertToEnums(Object[] args) throws ActionExecutionException {
    Object[] processedArgs = new Object[args.length];
    //try to convert all strings to enums
    Class<?>[] parameterTypes = method.getParameterTypes();
    for (int i = 0; i < parameterTypes.length; i++) {
        if (args[i] == null) {
            processedArgs[i] = null;
            continue;
        }
        boolean isParamArray = parameterTypes[i].isArray();
        Class<?> paramType;
        Class<?> argType;
        if (isParamArray) {
            paramType = parameterTypes[i].getComponentType();
            argType = args[i].getClass().getComponentType();
        } else {
            paramType = parameterTypes[i];
            argType = args[i].getClass();
        }
        if (argType == String.class && paramType.isEnum()) {
            try {
                if (isParamArray) {
                    Object convertedEnums = Array.newInstance(paramType, Array.getLength(args[i]));
                    //convert all array elements to enums
                    for (int j = 0; j < Array.getLength(args[i]); j++) {
                        String currentValue = (String) Array.get(args[i], j);
                        if (currentValue != null) {
                            Array.set(convertedEnums, j, Enum.valueOf((Class<? extends Enum>) paramType, currentValue));
                        }
                    }
                    processedArgs[i] = convertedEnums;
                } else {
                    processedArgs[i] = Enum.valueOf((Class<? extends Enum>) paramType, (String) args[i]);
                }
            } catch (IllegalArgumentException iae) {
                throw new ActionExecutionException("Could not convert string " + args[i] + " to enumeration of type " + paramType.getName());
            }
        } else {
            processedArgs[i] = args[i];
        }
    }
    return processedArgs;
}
Also used : ActionExecutionException(com.axway.ats.agent.core.exceptions.ActionExecutionException)

Example 8 with ActionExecutionException

use of com.axway.ats.agent.core.exceptions.ActionExecutionException in project ats-framework by Axway.

the class UserTemplateActionInvoker method invokeAndReturnTheLastResponseBody.

public String invokeAndReturnTheLastResponseBody(Class<?> actionClass, String actionMethodName, Object[] parameterValues) throws ActionExecutionException, NoSuchActionException, NoSuchComponentException, SecurityException, InternalComponentException, NoCompatibleMethodFoundException, NoSuchMethodException {
    Object actionObject = resolveActionObject(actionClass);
    Method actionMethod = resolveActionMethod(actionMethodName, parameterValues);
    String actionName = resolveActionName(false, actionMethod);
    // construct a template action and invoke it
    TemplateActionMethod templateActionMethod = new TemplateActionMethod(componentName, actionName, actionClass.getSimpleName(), actionMethod.getName(), actionMethod, actionClass);
    templateActionMethod.setReturnResponseBodyAsString(true);
    String content = (String) templateActionMethod.invoke(actionObject, parameterValues, false);
    if (content == null) {
        throw new ActionExecutionException("Can't return the last response body");
    }
    return content;
}
Also used : TemplateActionMethod(com.axway.ats.agent.core.action.TemplateActionMethod) Method(java.lang.reflect.Method) ActionExecutionException(com.axway.ats.agent.core.exceptions.ActionExecutionException) TemplateActionMethod(com.axway.ats.agent.core.action.TemplateActionMethod)

Aggregations

ActionExecutionException (com.axway.ats.agent.core.exceptions.ActionExecutionException)8 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 TemplateActionMethod (com.axway.ats.agent.core.action.TemplateActionMethod)1 TemplateActionsResponseVerificationConfigurator (com.axway.ats.agent.core.configuration.TemplateActionsResponseVerificationConfigurator)1 InternalComponentException (com.axway.ats.agent.core.exceptions.InternalComponentException)1 EnvironmentCleanupHandler (com.axway.ats.agent.core.model.EnvironmentCleanupHandler)1 Parameter (com.axway.ats.agent.core.model.Parameter)1 CompositeResult (com.axway.ats.agent.core.templateactions.CompositeResult)1 HttpClient (com.axway.ats.agent.core.templateactions.model.HttpClient)1 XmlReader (com.axway.ats.agent.core.templateactions.model.XmlReader)1 XmlUtilities (com.axway.ats.agent.core.templateactions.model.XmlUtilities)1 ActionHeader (com.axway.ats.agent.core.templateactions.model.objects.ActionHeader)1 ActionParser (com.axway.ats.agent.core.templateactions.model.objects.ActionParser)1 ActionResponseObject (com.axway.ats.agent.core.templateactions.model.objects.ActionResponseObject)1 ParameterDataConfig (com.axway.ats.agent.core.threading.data.config.ParameterDataConfig)1 UsernameDataConfig (com.axway.ats.agent.core.threading.data.config.UsernameDataConfig)1 PublicAtsApi (com.axway.ats.common.PublicAtsApi)1 NetworkingStopWatch (com.axway.ats.common.agent.templateactions.NetworkingStopWatch)1 ValidationType (com.axway.ats.core.validation.ValidationType)1 InvalidInputArgumentsException (com.axway.ats.core.validation.exceptions.InvalidInputArgumentsException)1