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

hope.kola.contract.api.QueryParameters Maven / Gradle / Ivy

There is a newer version: 1.1.2-RELEASE
Show newest version
package hope.kola.contract.api;

import static hope.kola.contract.api.KolaTestFunctions.$VString;

import com.squareup.javapoet.CodeBlock;
import groovy.lang.Closure;
import groovy.lang.DelegatesTo;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.function.Consumer;

public class QueryParameters {

  protected Set entries = new LinkedHashSet<>();

  public Set entries() {
    return entries;
  }

  public boolean hasEntries() {
    return !entries.isEmpty();
  }

  public void parameter(String name, @DelegatesTo(QueryParameter.class) Closure consumer) {
    final QueryParameter qp = new QueryParameter(name);
    consumer.setDelegate(qp);
    consumer.call();
    entries.add(qp);
  }

  /**
   * Populate dynamic value from the global context by the key
   *
   * @param name the query parameter name
   * @param key the key from the context
   */
  public void fromGlobal(String name, final String key) {
    final QueryParameter qp = new QueryParameter(name);
    qp.code(CodeBlock.builder().add("$L($S)", $VString, key));
    entries.add(qp);
  }

  public void parameter(final String name, final Consumer consumer) {
    final QueryParameter qp = new QueryParameter(name);
    consumer.accept(qp);
    entries.add(qp);
  }

  public void parameter(String name, String value) {
    final QueryParameter qp = new QueryParameter(name);
    qp.withString(value);
    entries.add(qp);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy