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

io.descoped.dc.api.node.builder.PutBuilder Maven / Gradle / Ivy

There is a newer version: 2.0.4
Show newest version
package io.descoped.dc.api.node.builder;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import io.descoped.dc.api.http.Headers;
import io.descoped.dc.api.node.Base;
import io.descoped.dc.api.node.BodyPublisher;
import io.descoped.dc.api.node.Configurations;
import io.descoped.dc.api.node.Node;
import io.descoped.dc.api.node.Put;
import io.descoped.dc.api.node.Validator;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;

import static io.descoped.dc.api.Builders.status;

@JsonDeserialize(using = NodeBuilderDeserializer.class)
public class PutBuilder extends OperationBuilder {

    @JsonUnwrapped(prefix = "request")
    Headers requestHeaders = new Headers();
    @JsonProperty("bodyPublisher")
    BodyPublisherBuilder bodyPublisherBuilder;
    @JsonProperty("responseValidators")
    List validators = new ArrayList<>();
    @JsonProperty("pipes")
    List pipes = new ArrayList<>();
    @JsonProperty
    List returnVariables = new ArrayList<>();

    PutBuilder() {
        super(BuilderType.Put);
    }

    public PutBuilder(String id) {
        super(BuilderType.Put);
        setId(id);
    }

    public PutBuilder id(String id) {
        setId(id);
        return this;
    }

    public PutBuilder url(String urlString) {
        this.url = urlString;
        return this;
    }

    public PutBuilder header(String name, String value) {
        requestHeaders.put(name, value);
        return this;
    }

    public PutBuilder data(BodyPublisherBuilder bodyPublisherBuilder) {
        this.bodyPublisherBuilder = bodyPublisherBuilder;
        return this;
    }

    public PutBuilder validate(LeafNodeBuilder validationBuilder) {
        validators.add(validationBuilder);
        return this;
    }

    public PutBuilder pipe(NodeBuilder builder) {
        pipes.add(builder);
        return this;
    }

    public PutBuilder returnVariables(String... variableKeys) {
        for (String variableKey : variableKeys) {
            returnVariables.add(variableKey);
        }
        return this;
    }

    @SuppressWarnings("unchecked")
    @Override
     R build(BuildContext buildContext) {
        BodyPublisher bodyPublisher = bodyPublisherBuilder == null ? null : bodyPublisherBuilder.build(buildContext);

        List validators = new ArrayList<>();

        // add default http status validator if unassigned
        if (this.validators.isEmpty()) {
            this.validate(status().success(200));
        }

        for (LeafNodeBuilder validatorBuilder : this.validators) {
            Validator validator = validatorBuilder.build(buildContext);
            validators.add(validator);
        }

        List stepNodeList = new ArrayList<>();
        for (NodeBuilder stepBuilder : pipes) {
            Node stepNode = stepBuilder.build(buildContext);
            stepNodeList.add(stepNode);
        }

        return (R) new PutBuilder.PutNode(getId(), buildContext.getInstance(SpecificationBuilder.GLOBAL_CONFIGURATION), url, requestHeaders, bodyPublisher, validators, stepNodeList, returnVariables);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        if (!super.equals(o)) return false;
        PutBuilder that = (PutBuilder) o;
        return Objects.equals(requestHeaders, that.requestHeaders) &&
                Objects.equals(bodyPublisherBuilder, that.bodyPublisherBuilder) &&
                Objects.equals(validators, that.validators) &&
                Objects.equals(pipes, that.pipes) &&
                Objects.equals(returnVariables, that.returnVariables);
    }

    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), requestHeaders, bodyPublisherBuilder, validators, pipes, returnVariables);
    }

    @Override
    public String toString() {
        return "PutBuilder{" +
                "requestHeaders=" + requestHeaders +
                ", bodyPublisherBuilder=" + bodyPublisherBuilder +
                ", validators=" + validators +
                ", pipes=" + pipes +
                ", returnVariables=" + returnVariables +
                '}';
    }

    static class PutNode extends OperationNode implements Put {

        final String url;
        final Headers headers;
        final List pipes;
        final List returnVariables;
        final BodyPublisher bodyPublisher;
        final List validateResponse;

        PutNode(String id, Configurations configurations, String url, Headers headers, BodyPublisher bodyPublisher, List validateResponse, List pipes, List returnVariables) {
            super(configurations, id);
            this.url = url;
            this.headers = headers;
            this.bodyPublisher = bodyPublisher;
            this.validateResponse = validateResponse;
            this.pipes = pipes;
            this.returnVariables = returnVariables;
        }

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

        @Override
        public Headers headers() {
            return headers;
        }

        @Override
        public BodyPublisher bodyPublisher() {
            return bodyPublisher;
        }

        @Override
        public List responseValidators() {
            return validateResponse;
        }

        @Override
        public List steps() {
            return pipes;
        }

        @Override
        public List returnVariables() {
            return returnVariables;
        }

        @Override
        public Iterator iterator() {
            return pipes.iterator();
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            PutNode putNode = (PutNode) o;
            return Objects.equals(url, putNode.url) &&
                    Objects.equals(headers, putNode.headers) &&
                    Objects.equals(pipes, putNode.pipes) &&
                    Objects.equals(returnVariables, putNode.returnVariables) &&
                    Objects.equals(bodyPublisher, putNode.bodyPublisher) &&
                    Objects.equals(validateResponse, putNode.validateResponse);
        }

        @Override
        public int hashCode() {
            return Objects.hash(url, headers, pipes, returnVariables, bodyPublisher, validateResponse);
        }

        @Override
        public String toString() {
            return "PutNode{" +
                    "url='" + url + '\'' +
                    ", headers=" + headers +
                    ", pipes=" + pipes +
                    ", returnVariables=" + returnVariables +
                    ", bodyPublisher=" + bodyPublisher +
                    ", validateResponse=" + validateResponse +
                    '}';
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy