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

com.github.restup.test.AbstractApiRequestBuilder Maven / Gradle / Ivy

package com.github.restup.test;

import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import com.github.restup.test.resource.Contents;
import com.github.restup.test.resource.RelativeTestResource;

public abstract class AbstractApiRequestBuilder, H> {

    protected Map headers = new HashMap();
    private Class testClass;
    private String testName;
    private String testFileExtension;
    private Contents bodyResource;

    @SuppressWarnings({"unchecked"})
    protected B me() {
        return (B) this;
    }

    public B body(byte[] body) {
        return body(Contents.of(body));
    }

    public B body(String body) {
        return body(Contents.of(body));
    }

    public B body(Contents bodyResource) {
        this.bodyResource = bodyResource;
        return me();
    }

    public B header(String name, H value) {
        headers.put(name, value);
        return me();
    }

    public B headers(Map headers) {
        this.headers = headers;
        return me();
    }

    public B testClass(Class testClass) {
        this.testClass = testClass;
        return me();
    }

    public B testName(String testName) {
        this.testName = testName;
        return me();
    }

    public B testFileExtension(String testFileExtension) {
        this.testFileExtension = testFileExtension;
        return me();
    }

    protected void add(Map map, String key, String... values) {
        if (StringUtils.isNotEmpty(key)) {
            String[] existing = map.get(key);
            if (existing == null) {
                map.put(key, values);
            } else {
                map.put(key, ArrayUtils.addAll(existing, values));
            }
        }
    }

    protected Map getHeaders() {
        return headers;
    }

    protected abstract String getTestDir();

    protected boolean isDefaultTestResourceAllowed() {
        return StringUtils.isNotBlank(testName);
    }

    protected boolean hasConfiguredBody() {
        return bodyResource != null || isDefaultTestResourceAllowed();
    }

    public Contents getBody() {
        if (bodyResource != null) {
            return bodyResource;
        }
        if (isDefaultTestResourceAllowed()) {
            RelativeTestResource resource = new RelativeTestResource(testClass, getTestDir(), testName, testFileExtension);
            return resource;
        }
        return null;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy