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

io.restassured.module.mockmvc.matcher.RestAssuredMockMvcMatchers Maven / Gradle / Ivy

/*
 * Copyright 2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.restassured.module.mockmvc.matcher;

import io.restassured.internal.matcher.xml.XmlDtdMatcher;
import io.restassured.internal.matcher.xml.XmlXsdMatcher;
import io.restassured.matcher.ResponseAwareMatcher;
import io.restassured.matcher.RestAssuredMatchers;
import io.restassured.module.mockmvc.response.MockMvcResponse;
import org.hamcrest.Matcher;

import java.io.File;
import java.io.InputStream;
import java.io.Reader;
import java.net.URL;

import static org.hamcrest.Matchers.*;

/**
 * Providers Hamcrest matchers that may be useful when validating a MockMvc response.
 */
public class RestAssuredMockMvcMatchers {

    /**
     * Evaluates to true if an XML string matches the supplied XSD (Xml Schema).
     *
     * @param xsd The XSD to match
     * @return The XSD matcher
     */
    public static Matcher matchesXsd(String xsd) {
        return RestAssuredMatchers.matchesXsd(xsd);
    }

    /**
     * Evaluates to true if an XML string matches the supplied XSD (Xml Schema).
     *
     * @param xsd The XSD to match
     * @return The XSD matcher
     */
    public static Matcher matchesXsd(InputStream xsd) {
        return RestAssuredMatchers.matchesXsd(xsd);
    }

    /**
     * Evaluates to true if an XML string matches the supplied XSD (Xml Schema).
     *
     * @param xsd The XSD to match
     * @return The XSD matcher
     */
    public static Matcher matchesXsd(Reader xsd) {
        return RestAssuredMatchers.matchesXsd(xsd);
    }

    /**
     * Evaluates to true if an XML string matches the supplied XSD (Xml Schema).
     *
     * @param xsd The XSD to match
     * @return The XSD matcher
     */
    public static Matcher matchesXsd(File xsd) {
        return RestAssuredMatchers.matchesXsd(xsd);
    }

    /**
     * Evaluates to true if an XML file in classpath matches the supplied XSD.
     *
     * @param path The path to the XSD located in classpath
     * @return The DTD matcher
     */
    public static Matcher matchesXsdInClasspath(String path) {
        return XmlXsdMatcher.matchesXsdInClasspath(path);
    }

    /**
     * Evaluates to true if an XML string matches the supplied DTD.
     *
     * @param dtd The DTD to match
     * @return The DTD matcher
     */
    public static Matcher matchesDtd(String dtd) {
        return RestAssuredMatchers.matchesDtd(dtd);
    }

    /**
     * Evaluates to true if an XML string matches the supplied DTD.
     *
     * @param dtd The DTD to match
     * @return The DTD matcher
     */
    public static Matcher matchesDtd(InputStream dtd) {
        return RestAssuredMatchers.matchesDtd(dtd);
    }

    /**
     * Evaluates to true if an XML string matches the supplied DTD.
     *
     * @param dtd The DTD to match
     * @return The DTD matcher
     */
    public static Matcher matchesDtd(File dtd) {
        return RestAssuredMatchers.matchesDtd(dtd);
    }

    /**
     * Evaluates to true if an XML string matches the supplied DTD.
     *
     * @param url The DTD to match
     * @return The DTD matcher
     */
    public static Matcher matchesDtd(URL url) {
        return RestAssuredMatchers.matchesDtd(url);
    }

    /**
     * Evaluates to true if an XML file in classpath matches the supplied DTD.
     *
     * @param path The path to the DTD file in classpath
     * @return The DTD matcher
     */
    public static Matcher matchesDtdInClasspath(String path) {
        return XmlDtdMatcher.matchesDtdInClasspath(path);
    }

    /**
     * Creates a {@link ResponseAwareMatcher} that extracts the given path from the response and
     * wraps it in a {@link org.hamcrest.Matchers#equalTo(Object)} matcher. This is useful if you have a resource that e.g. returns the given JSON:
     * 
     * {
     *      "userId" : "my-id",
     *      "playerId" : "my-id"
     * }
     * 
* you can then test it like this: *
     * get("/x").then().body("userId", equalToPath("playerId"));
     * 
* * @param path The path to check * @return A {@link ResponseAwareMatcher} */ public static ResponseAwareMatcher equalToPath(final String path) { return new ResponseAwareMatcher() { public Matcher matcher(MockMvcResponse response) { return equalTo(response.path(path)); } }; } /** * Creates a {@link ResponseAwareMatcher} that extracts the given path from the response and * wraps it in a {@link org.hamcrest.Matchers#equalTo(Object)} matcher. This is useful if you have a resource that e.g. returns the given JSON: *
     * {
     *      "userId" : "my-id",
     *      "href" : "http://localhost:8080/my-id"
     * }
     * 
* you can then test it like this: *
     * get("/x").then().body("href", endsWithPath("userId"));
     * 
* * @param path The path to check * @return A {@link ResponseAwareMatcher} */ public static ResponseAwareMatcher endsWithPath(final String path) { return new ResponseAwareMatcher() { public Matcher matcher(MockMvcResponse response) { return endsWith(response.path(path)); } }; } /** * Creates a {@link ResponseAwareMatcher} that extracts the given path from the response and * wraps it in a {@link org.hamcrest.Matchers#equalTo(Object)} matcher. This is useful if you have a resource that e.g. returns the given JSON: *
     * {
     *      "userId" : "my-id",
     *      "baseUri" : "http://localhost:8080",
     *      "href" : "http://localhost:8080/my-id"
     * }
     * 
* you can then test it like this: *
     * get("/x").then().body("href", startsWithPath("baseUri"));
     * 
* * @param path The path to check * @return A {@link ResponseAwareMatcher} */ public static ResponseAwareMatcher startsWithPath(final String path) { return new ResponseAwareMatcher() { public Matcher matcher(MockMvcResponse response) { return startsWith(response.path(path)); } }; } /** * Creates a {@link ResponseAwareMatcher} that extracts the given path from the response and * wraps it in a {@link org.hamcrest.Matchers#equalTo(Object)} matcher. This is useful if you have a resource that e.g. returns the given JSON: *
     * {
     *      "userId" : "my-id",
     *      "href" : "http://localhost:8080/my-id"
     * }
     * 
* you can then test it like this: *
     * get("/x").then().body("href", containsPath("userId"));
     * 
* * @param path The path to check * @return A {@link ResponseAwareMatcher} */ public static ResponseAwareMatcher containsPath(final String path) { return new ResponseAwareMatcher() { public Matcher matcher(MockMvcResponse response) { return containsString(response.path(path)); } }; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy