Search in sources :

Example 1 with ParameterDataProviderInitalizationException

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

the class FileNamesParameterDataProvider method createFileContainersArray.

/**
     * Generate array of 100 elements with folders mentioned in configuration according to the percentage dispersion.
     * I.e. for
     * <pre>
     *   Folder1/f1, 20%
     *   Folder2/f2, 30%
     *   Folder3/f3, 50%
     *   Will generate:
     *   [f1, f3, f2, f3, f2, f3, f1, f3, f2, f3, f3, f1, f2, f3, f2, f3, f1, f3, f2, f3, f3, f1, f2, f3, f2, f3, f1, f3, f2, f3, f3, f1, f2, f3, f2, f3, f1, f3, f2, f3, f3, f1, f2, f3, f2, f3, f1, f3, f2, f3, f3, f1, f2, f3, f2, f3, f1, f3, f2, f3, f3, f1, f2, f3, f2, f3, f1, f3, f2, f3, f3, f1, f2, f3, f2, f3, f1, f3, f2, f3, f3, f1, f2, f3, f2, f3, f1, f3, f2, f3, f3, f1, f2, f3, f2, f3, f1, f3, f2, f3]
     * </pre>
     * @throws ParameterDataProviderInitalizationException
     */
private void createFileContainersArray() throws ParameterDataProviderInitalizationException {
    for (FileContainer container : fileContainers) {
        File folder = new File(container.getFolderName());
        if (!folder.exists() || !folder.isDirectory()) {
            throw new ParameterDataProviderInitalizationException(container.getFolderName() + " does not exist or is not a directory");
        }
        List<String> fileNames = getFileNamesList(folder, container.getPattern(), !container.isPatternContainingParams());
        if (fileNames.size() == 0) {
            throw new ParameterDataProviderInitalizationException("Directory '" + container.getFolderName() + "' is empty or doesn't have files matching regex '" + container.getPattern() + "'");
        }
        container.setFileList(fileNames);
    }
    //sorted array according to the percentage
    containersArray = new ArrayList<FileContainer>();
    if (fileContainers.size() == 1) {
        containersArray.add(fileContainers.get(0));
        return;
    }
    boolean cantCalc = true;
    for (int i = 0; i < 100; i++) {
        cantCalc = true;
        // TODO: sort FileContainers by percentages from bigger to lower
        for (FileContainer container : fileContainers) {
            int freq = Collections.frequency(containersArray, container);
            int perc = 0;
            if (freq != 0) {
                perc = (int) (((double) freq / containersArray.size()) * 100);
            }
            if (perc < container.getPercentage()) {
                containersArray.add(container);
                cantCalc = false;
                break;
            }
        }
        if (cantCalc) {
            containersArray.add(fileContainers.get(0));
        }
    }
}
Also used : ParameterDataProviderInitalizationException(com.axway.ats.agent.core.threading.exceptions.ParameterDataProviderInitalizationException) File(java.io.File)

Example 2 with ParameterDataProviderInitalizationException

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

the class ParameterDataProviderFactory method createDataProvider.

/**
     * Create a data provider based on a configuration provider
     *
     * @param parameterConfig the configuration based on which to create the provider
     * @return the newly created provider
     * @throws ParameterDataProviderNotSupportedException if the provider is not supported
     * @throws ParameterDataProviderInitalizationException if there is an error during provider initialization
     */
public static ParameterDataProvider createDataProvider(ParameterDataConfig parameterConfig) throws ParameterDataProviderNotSupportedException, ParameterDataProviderInitalizationException {
    ParameterDataProvider parameterDataProvider;
    Class<?> parameterConfigClass = parameterConfig.getClass();
    if (parameterConfigClass == RangeDataConfig.class) {
        RangeDataConfig rangeDataConfig = (RangeDataConfig) parameterConfig;
        //choose the right one, based on the arguments
        if (rangeDataConfig.getStaticValue() != null) {
            parameterDataProvider = new StringRangeParameterDataProvider(rangeDataConfig.getParameterName(), rangeDataConfig.getStaticValue(), (Integer) rangeDataConfig.getRangeStart(), (Integer) rangeDataConfig.getRangeEnd(), parameterConfig.getParameterProviderLevel());
        } else {
            parameterDataProvider = new IntegerRangeParameterDataProvider(rangeDataConfig.getParameterName(), (Integer) rangeDataConfig.getRangeStart(), (Integer) rangeDataConfig.getRangeEnd(), parameterConfig.getParameterProviderLevel());
        }
    } else if (parameterConfigClass == ListDataConfig.class) {
        ListDataConfig listDataConfig = (ListDataConfig) parameterConfig;
        parameterDataProvider = new ListParameterDataProvider(listDataConfig.getParameterName(), listDataConfig.getValues(), listDataConfig.getParameterProviderLevel());
    } else if (parameterConfigClass == FileNamesDataConfig.class) {
        FileNamesDataConfig fileNamesDataConfig = (FileNamesDataConfig) parameterConfig;
        parameterDataProvider = new FileNamesParameterDataProvider(fileNamesDataConfig.getParameterName(), fileNamesDataConfig.getFileContainers(), fileNamesDataConfig.getRecursiveSearch(), fileNamesDataConfig.getReturnFullPath(), fileNamesDataConfig.getParameterProviderLevel());
    } else if (parameterConfigClass == UsernameDataConfig.class) {
        UsernameDataConfig nameDataConfig = (UsernameDataConfig) parameterConfig;
        if (nameDataConfig.getValues() != null) {
            parameterDataProvider = new ListParameterDataProvider(nameDataConfig.getParameterName(), nameDataConfig.getValues(), nameDataConfig.getParameterProviderLevel(), UsernameDataConfig.class);
        } else {
            parameterDataProvider = new StringRangeParameterDataProvider(nameDataConfig.getParameterName(), nameDataConfig.getStaticValue(), (Integer) nameDataConfig.getRangeStart(), (Integer) nameDataConfig.getRangeEnd(), parameterConfig.getParameterProviderLevel(), UsernameDataConfig.class);
        }
    } else if (parameterConfigClass.getGenericSuperclass() == CustomParameterDataConfig.class) {
        // custom data provider
        CustomParameterDataConfig customDataConfig = (CustomParameterDataConfig) parameterConfig;
        try {
            // find the constructor of the custom data provider
            Constructor<? extends CustomParameterDataProvider> constructor = customDataConfig.getDataProviderClass().getDeclaredConstructor(String.class, Map.class, ParameterProviderLevel.class);
            // call the constructor of the custom data provider
            parameterDataProvider = (CustomParameterDataProvider) constructor.newInstance(customDataConfig.getParameterName(), customDataConfig.getControlTokens(), customDataConfig.getParameterProviderLevel());
        } catch (Exception e) {
            throw new ParameterDataProviderNotSupportedException(parameterConfigClass);
        }
    } else {
        throw new ParameterDataProviderNotSupportedException(parameterConfigClass);
    }
    //initialize the newly created provider
    parameterDataProvider.initialize();
    //and return it back to the caller
    return parameterDataProvider;
}
Also used : RangeDataConfig(com.axway.ats.agent.core.threading.data.config.RangeDataConfig) FileNamesDataConfig(com.axway.ats.agent.core.threading.data.config.FileNamesDataConfig) UsernameDataConfig(com.axway.ats.agent.core.threading.data.config.UsernameDataConfig) ParameterDataProviderNotSupportedException(com.axway.ats.agent.core.threading.exceptions.ParameterDataProviderNotSupportedException) ParameterDataProviderInitalizationException(com.axway.ats.agent.core.threading.exceptions.ParameterDataProviderInitalizationException) CustomParameterDataConfig(com.axway.ats.agent.core.threading.data.config.CustomParameterDataConfig) ParameterDataProviderNotSupportedException(com.axway.ats.agent.core.threading.exceptions.ParameterDataProviderNotSupportedException) ListDataConfig(com.axway.ats.agent.core.threading.data.config.ListDataConfig)

Aggregations

ParameterDataProviderInitalizationException (com.axway.ats.agent.core.threading.exceptions.ParameterDataProviderInitalizationException)2 CustomParameterDataConfig (com.axway.ats.agent.core.threading.data.config.CustomParameterDataConfig)1 FileNamesDataConfig (com.axway.ats.agent.core.threading.data.config.FileNamesDataConfig)1 ListDataConfig (com.axway.ats.agent.core.threading.data.config.ListDataConfig)1 RangeDataConfig (com.axway.ats.agent.core.threading.data.config.RangeDataConfig)1 UsernameDataConfig (com.axway.ats.agent.core.threading.data.config.UsernameDataConfig)1 ParameterDataProviderNotSupportedException (com.axway.ats.agent.core.threading.exceptions.ParameterDataProviderNotSupportedException)1 File (java.io.File)1