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

com.github.tonivade.zeromock.api.Extractors Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2018-2024, Antonio Gabriel Muñoz Conejo 
 * Distributed under the terms of the MIT License
 */
package com.github.tonivade.zeromock.api;

import static com.jayway.jsonpath.Configuration.defaultConfiguration;
import static com.jayway.jsonpath.Option.SUPPRESS_EXCEPTIONS;

import java.lang.reflect.Type;

import com.github.tonivade.purefun.core.Function1;
import com.github.tonivade.purefun.type.Option;
import com.github.tonivade.purefun.type.Try;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath;

public final class Extractors {

  private static final Configuration CONFIGURATION = defaultConfiguration().addOptions(SUPPRESS_EXCEPTIONS);

  private Extractors() {}

  public static Function1 body() {
    return HttpRequest::body;
  }

  public static  Function1 extract(String jsonPath) {
    return body().andThen(asString()).andThen(jsonPath(jsonPath));
  }

  public static Function1 queryParam(String name) {
    return request -> request.param(name);
  }

  public static Function1 pathParam(int position) {
    return request -> request.pathParam(position);
  }

  public static  Function1>> jsonTo(Type type) {
    return body().andThen(Deserializers.jsonTo(type));
  }

  public static Function1 asString() {
    return Bytes::asString;
  }

  public static Function1 asInteger() {
    return Integer::parseInt;
  }

  public static Function1 asLong() {
    return Long::parseLong;
  }

  private static  Function1 jsonPath(String jsonPath) {
    return json -> JsonPath.parse(json, CONFIGURATION).read(jsonPath);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy