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

org.stubit.http.Stubbing Maven / Gradle / Ivy

There is a newer version: 0.8.2
Show newest version
package org.stubit.http;

import java.util.function.Predicate;

/**
 * A {@link Stubbing} defines that all requests that match the given {@link #predicate} should
 * return the given {@link #response()}
 *
 * @param predicate {@link Predicate} that needs to be true for this {@link Stubbing} to be
 *     considered
 * @param response the {@link StubbedResponse} that will be returned if the {@link #predicate} is
 *     true
 */
public record Stubbing(Predicate predicate, StubbedResponse response) {

  /**
   * @return a newly created {@link Builder}
   */
  public static Builder stub() {
    return new Builder();
  }

  /** A fluent API builder for {@link Stubbing}. */
  public static class Builder {
    private PathPredicate pathPredicate;
    private MethodPredicate methodPredicate;

    /**
     * @param path the path that will be considered for the {@link Stubbing}.
     * @return this
     * @see PathPredicate#PathPredicate(String)
     */
    public Builder path(String path) {
      pathPredicate = new PathPredicate(path);
      return this;
    }

    /**
     * @param method the method to be considered
     * @param furtherMethods optional further methods to be considered
     * @return this
     * @see MethodPredicate#MethodPredicate(String, String...)
     */
    public Builder method(String method, String... furtherMethods) {
      methodPredicate = new MethodPredicate(method, furtherMethods);
      return this;
    }

    /**
     * Sets the response and builds the {@link Stubbing}.
     *
     * @param response the {@link StubbedResponse} to be used for the {@link Stubbing}
     * @return the build {@link Stubbing}
     */
    public Stubbing returns(StubbedResponse response) {
      final Predicate predicate =
          httpRequest ->
              (pathPredicate == null || pathPredicate.test(httpRequest))
                  && (methodPredicate == null || methodPredicate.test(httpRequest));
      return new Stubbing(predicate, response);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy