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

com.testingsyndicate.jms.responder.matcher.BodyMatcher Maven / Gradle / Ivy

There is a newer version: 2.2.0
Show newest version
package com.testingsyndicate.jms.responder.matcher;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
import com.testingsyndicate.jms.responder.model.BodySource;
import com.testingsyndicate.jms.responder.model.RequestInfo;

import java.util.Objects;

@JsonDeserialize(builder = BodyMatcher.Builder.class)
public final class BodyMatcher implements Matcher {

    private final String body;
    private final boolean trim;

    private BodyMatcher(Builder builder) {
        trim = builder.trim;
        if (null != builder.body && trim) {
            body = builder.body.trim();
        } else {
            body = builder.body;
        }
    }

    public boolean matches(RequestInfo requestInfo) {
        String requestBody = requestInfo.getBody();

        if (null != requestBody && trim) {
            requestBody = requestBody.trim();
        }

        return Objects.equals(body, requestBody);
    }

    public String getBody() {
        return body;
    }

    public static Builder newBuilder() {
        return new Builder();
    }

    @JsonPOJOBuilder
    public static final class Builder {

        private String body;
        private boolean trim;

        private Builder() {
            trim = false;
        }

        public Builder withBody(BodySource body) {
            this.body = body.getBody();
            return this;
        }

        public Builder withTrim(boolean trim) {
            this.trim = trim;
            return this;
        }

        public BodyMatcher build() {
            return new BodyMatcher(this);
        }

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy