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

dev.sixpack.generator.OrchestratorRecordBuilder Maven / Gradle / Ivy

The newest version!
package dev.sixpack.generator;

import dev.sixpack.api.data.Template;
import dev.sixpack.api.exception.SixpackFatalException;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

import static dev.sixpack.utils.FormatUtils.mFormat;

public class OrchestratorRecordBuilder {
    private final String environment;
    private final String supplier;
    final private Class orchestratorClass;

    public OrchestratorRecordBuilder(String environment,
                                     String supplier,
                                     Class orchestratorClass) {
        this.environment = environment;
        this.supplier = supplier;
        this.orchestratorClass = orchestratorClass;
    }

    public FactoryRecord build() {
        return new FactoryRecord(
                environment,
                supplier,
                itemNameAnnotation(),
                inputType(),
                null, // TODO: implement
                null, // TODO: implement
                outputType(),
                null,
                orchestratorClass,
                templates());
    }

    private String itemNameAnnotation() {
        ItemName annotation = orchestratorClass.getAnnotation(ItemName.class);
        if (annotation == null) {
            throw new IllegalArgumentException(mFormat(
                    "The generator class {} is not annotated with @ItemName. This annotation is used to uniquely identify item within the scope of this supplier on the given test environment. Please carefully chose the value and add the annotation @ItemName.",
                    orchestratorClass.getSimpleName()));
        }
        return annotation.value();
    }

    private Class inputType() {
        return getMethodGenerate().getParameterTypes()[0];
    }

    private Class outputType() {
        return getMethodGenerate().getReturnType();
    }

    private List