All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.chutneytesting.action.domain.ActionTemplateV2 Maven / Gradle / Ivy

The newest version!
/*
 * SPDX-FileCopyrightText: 2017-2024 Enedis
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 */

package com.chutneytesting.action.domain;

import com.chutneytesting.action.domain.parameter.Parameter;
import com.chutneytesting.action.domain.parameter.ParameterResolver;
import com.chutneytesting.action.spi.Action;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

public class ActionTemplateV2 implements ActionTemplate {

    private final String identifier;
    private final Class implementationClass;
    private final Constructor constructor;
    private final List parameters;

    public ActionTemplateV2(String identifier, Class implementationClass, Constructor constructor, List parameters) {
        this.identifier = identifier;
        this.implementationClass = implementationClass;
        this.constructor = constructor;
        this.parameters = parameters;
    }

    @Override
    public String identifier() {
        return identifier;
    }

    @Override
    public Class implementationClass() {
        return implementationClass;
    }

    @Override
    public Set parameters() {
        return new LinkedHashSet<>(parameters);
    }

    @Override
    public Action create(List parameterResolvers) {
        Object[] parameterValues = parameters.stream()
            .map(p -> resolveParameter(parameterResolvers, p))
            .toArray(Object[]::new);
        try {
            return constructor.newInstance(parameterValues);
        } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
            throw new ActionInstantiationFailureException(identifier, e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy