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 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",
                hasJsonPath(jsonPath, not(anyOf(nullValue(), empty()))),
                jsonPath);
    }

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

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

    public static Matcher isJson() {
        return isJson(withJsonPath("$..*"));
    }

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

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

    public static Matcher isJsonFile(final 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, not(anyOf(nullValue(), empty()))),
                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(final JsonPath jsonPath, final Matcher resultMatcher) {
        return new WithJsonPath(jsonPath, resultMatcher);
    }
}