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

com.jayway.jsonpath.matchers.JsonPathMatchers Maven / Gradle / Ivy

There is a newer version: 2.9.0
Show newest version
package com.jayway.jsonpath.matchers;

import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Predicate;
import com.jayway.jsonpath.ReadContext;
import org.hamcrest.Matcher;

import java.io.File;
import java.util.List;
import java.util.Map;

import static org.hamcrest.Matchers.*;

public class JsonPathMatchers {

    private JsonPathMatchers() {
        throw new AssertionError("prevent instantiation");
    }

    public static Matcher hasJsonPath(String jsonPath) {
        return describedAs("has json path %0",
                isJson(withJsonPath(jsonPath)),
                jsonPath);
    }

    public static  Matcher hasJsonPath(String jsonPath, Matcher resultMatcher) {
        return isJson(withJsonPath(jsonPath, resultMatcher));
    }

    public static Matcher hasNoJsonPath(String jsonPath) {
        return isJson(withoutJsonPath(jsonPath));
    }

    public static Matcher isJson() {
        return isJson(withJsonPath("$", anyOf(instanceOf(Map.class), instanceOf(List.class))));
    }

    public static Matcher isJson(Matcher matcher) {
        return new IsJson(matcher);
    }

    public static Matcher isJsonString(Matcher matcher) {
        return new IsJson(matcher);
    }

    public static Matcher isJsonFile(Matcher matcher) {
        return new IsJson(matcher);
    }

    public static Matcher withJsonPath(String jsonPath, Predicate... filters) {
        return withJsonPath(JsonPath.compile(jsonPath, filters));
    }

    public static Matcher withJsonPath(JsonPath jsonPath) {
        return describedAs("with json path %0",
                withJsonPath(jsonPath, anything()),
                jsonPath.getPath());
    }

    public static Matcher withoutJsonPath(String jsonPath, Predicate... filters) {
        return withoutJsonPath(JsonPath.compile(jsonPath, filters));
    }

    public static Matcher withoutJsonPath(JsonPath jsonPath) {
        return new WithoutJsonPath(jsonPath);
    }

    public static  Matcher withJsonPath(String jsonPath, Matcher resultMatcher) {
        return withJsonPath(JsonPath.compile(jsonPath), resultMatcher);
    }

    public static  Matcher withJsonPath(JsonPath jsonPath, Matcher resultMatcher) {
        return new WithJsonPath(jsonPath, resultMatcher);
    }
}