//package io.delphi.qa.auto.awsmfrw.common_baby.perf;
//
//import org.apache.jmeter.config.Arguments;
//import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy;
//import org.apache.jmeter.protocol.http.util.HTTPArgument;
//import org.apache.jmeter.testelement.TestElement;
//import org.apache.jorphan.collections.HashTree;
//import org.eclipse.jetty.http.HttpMethod;
//import org.eclipse.jetty.http.MimeTypes;
//import us.abstracta.jmeter.javadsl.core.DslTestElement;
//import us.abstracta.jmeter.javadsl.core.preprocessors.DslJsr223PreProcessor;
//import us.abstracta.jmeter.javadsl.http.DslHttpSampler;
//import us.abstracta.jmeter.javadsl.http.HttpHeaders;
//
//import java.util.function.Function;
//
//import static us.abstracta.jmeter.javadsl.JmeterDsl.jsr223PreProcessor;
//
//public class IsaDslHttpSampler extends DslHttpSampler implements DslTestElement {
//
//    public IsaDslHttpSampler(String url) {
//        super(null, url);
//    }
//
//    private String url;
//    private HttpMethod method = HttpMethod.GET;
//    private final HttpHeaders headers = new HttpHeaders();
//    private String body;
//
//    public IsaDslHttpSampler(String name, String url) {
//        super(name, url);
//    }
//
//    public IsaDslHttpSampler(String name, Function<DslJsr223PreProcessor.PreProcessorVars, String> urlSupplier) {
//        super(name, urlSupplier);
//    }
//
//    @Override public DslHttpSampler post(String body, MimeTypes.Type contentType) {
//        return method(HttpMethod.POST)
//            .contentType(contentType)
//            .body(body);
//    }
//
//    @Override public IsaDslHttpSampler post(Function<DslJsr223PreProcessor.PreProcessorVars, String> bodySupplier,
//                                         MimeTypes.Type contentType) {
//        return method(HttpMethod.POST)
//            .contentType(contentType)
//            .body(bodySupplier);
//    }
//
//     @Override public IsaDslHttpSampler method(HttpMethod method) {
//        this.method = method;
//        return this;
//    }
//
//    @Override public IsaDslHttpSampler header(String name, String value) {
//        headers.header(name, value);
//        return this;
//    }
//
//    @Override public IsaDslHttpSampler header(String name, Function<DslJsr223PreProcessor.PreProcessorVars, String> valueSupplier) {
//        String variableNamePrefix = "PRE_PROCESSOR_HEADER~";
//        headers.header(name, "${" + variableNamePrefix + name + "}");
//        return children(
//            jsr223PreProcessor(s -> s.vars.put(variableNamePrefix + name, valueSupplier.apply(s)))
//        );
//    }
//
//    @Override public IsaDslHttpSampler contentType(MimeTypes.Type contentType) {
//        headers.contentType(contentType);
//        return this;
//    }
//
//    @Override public IsaDslHttpSampler body(Function<DslJsr223PreProcessor.PreProcessorVars, String> bodySupplier) {
//        String variableName = "PRE_PROCESSOR_REQUEST_BODY";
//        this.body = "${" + variableName + "}";
//        return children(jsr223PreProcessor(s -> s.vars.put(variableName, bodySupplier.apply(s))));
//    }
//
//    @Override public IsaDslHttpSampler body(String body) {
//        this.body = body;
//        return this;
//    }
//
//    @Override public IsaDslHttpSampler children(SamplerChild... children) {
//        return (IsaDslHttpSampler) super.children(children);
//    }
//
//    @Override
//    public TestElement buildTestElement() {
//        HTTPSamplerProxy ret = new HTTPSamplerProxy();
//        ret.setFollowRedirects(true);
//        ret.setUseKeepAlive(true);
//        ret.setPath(url);
//        ret.setMethod(method.name());
//        ret.setArguments(buildArguments());
//        return ret;
//    }
//
//    private Arguments buildArguments() {
//        Arguments args = new Arguments();
//        if (body != null) {
//            HTTPArgument arg = new HTTPArgument("", body, false);
//            arg.setAlwaysEncoded(false);
//            args.addArgument(arg);
//        }
//        return args;
//    }
//
//    @Override
//    public HashTree buildTreeUnder(HashTree parent) {
//        HashTree ret = super.buildTreeUnder(parent);
//        if (!headers.isEmpty()) {
//            headers.buildTreeUnder(ret);
//        }
//        return ret;
//    }
//}
