Search in sources :

Example 1 with RestPluginException

use of ru.sbtqa.tag.api.exception.RestPluginException in project page-factory-2 by sbtqa.

the class ReflectionUtils method set.

public static void set(EndpointEntry endpoint, Field field, Object value) {
    try {
        Object convertedValue;
        // If field type is BodyArray, parse it
        if (field.getType().equals(BodyArray.class)) {
            Type genericType = field.getGenericType();
            Map<TypeVariable<?>, Type> actualTypeArguments = TypeUtils.getTypeArguments((ParameterizedType) genericType);
            Class<?> typeArgument = (Class<?>) actualTypeArguments.values().toArray()[0];
            convertedValue = field.getType().getConstructor(String.class, Class.class).newInstance(value, typeArgument);
        } else {
            convertedValue = ConvertUtils.convert(value, field.getType());
        }
        field.setAccessible(true);
        field.set(endpoint, convertedValue);
    } catch (IllegalArgumentException | IllegalAccessException | NoSuchMethodException | InvocationTargetException | InstantiationException ex) {
        throw new RestPluginException(format("Body with name \"%s\" is not available", field.getName()), ex);
    }
}
Also used : RestPluginException(ru.sbtqa.tag.api.exception.RestPluginException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable)

Example 2 with RestPluginException

use of ru.sbtqa.tag.api.exception.RestPluginException in project page-factory-2 by sbtqa.

the class EndpointEntryReflection method validate.

/**
 * Invoke method annotated with {@link Validation}.
 * Works if endpoint contains only one validation rule
 *
 * @param params params to pass to validation rule method
 */
public void validate(Object... params) {
    if (validations.size() < 2) {
        Method validation = validations.values().stream().findFirst().orElseThrow(() -> new RestPluginException(format("There is no validation rules in \"%s\" endpoint", entryTitle)));
        invoke(validation, endpoint, params);
    } else {
        throw new RestPluginException(format("There is more then 1 validation rule in \"%s\" endpoint. Please specify validation rule with it title", entryTitle));
    }
}
Also used : Method(java.lang.reflect.Method) RestPluginException(ru.sbtqa.tag.api.exception.RestPluginException)

Example 3 with RestPluginException

use of ru.sbtqa.tag.api.exception.RestPluginException in project page-factory-2 by sbtqa.

the class EndpointManager method bootstrapEndpoint.

private static EndpointEntry bootstrapEndpoint(Class<?> entry) {
    try {
        Constructor<EndpointEntry> constructor = (Constructor<EndpointEntry>) entry.getConstructor();
        constructor.setAccessible(true);
        EndpointEntry endpoint = constructor.newInstance();
        EndpointContext.setCurrentEndpoint(endpoint);
        return endpoint;
    } catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
        throw new RestPluginException("Can't initialize current entry parameters", ex);
    }
}
Also used : Constructor(java.lang.reflect.Constructor) EndpointEntry(ru.sbtqa.tag.api.EndpointEntry) RestPluginException(ru.sbtqa.tag.api.exception.RestPluginException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 4 with RestPluginException

use of ru.sbtqa.tag.api.exception.RestPluginException in project page-factory-2 by sbtqa.

the class MutatorApplicator method getMutator.

private Method getMutator(EndpointEntry endpoint, Field field) {
    Mutator mutator = field.getAnnotation(Mutator.class);
    Class container = mutator.clazz() == EmptyClass.class ? endpoint.getClass() : mutator.clazz();
    Method setter = Arrays.stream(container.getMethods()).filter(method -> Objects.equals(method.getName(), mutator.method())).findFirst().orElseThrow(() -> new RestPluginException(format("Mutator method \"%s\" is not found", mutator.method())));
    setter.setAccessible(true);
    return setter;
}
Also used : Mutator(ru.sbtqa.tag.api.annotation.Mutator) EmptyClass(ru.sbtqa.tag.api.utils.EmptyClass) EmptyClass(ru.sbtqa.tag.api.utils.EmptyClass) Method(java.lang.reflect.Method) RestPluginException(ru.sbtqa.tag.api.exception.RestPluginException)

Aggregations

RestPluginException (ru.sbtqa.tag.api.exception.RestPluginException)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 Constructor (java.lang.reflect.Constructor)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 TypeVariable (java.lang.reflect.TypeVariable)1 EndpointEntry (ru.sbtqa.tag.api.EndpointEntry)1 Mutator (ru.sbtqa.tag.api.annotation.Mutator)1 EmptyClass (ru.sbtqa.tag.api.utils.EmptyClass)1