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

io.restassured.matcher.ResponseAwareMatcherComposer Maven / Gradle / Ivy

There is a newer version: 5.5.0
Show newest version
/*
 * Copyright 2019 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.matcher;

import io.restassured.internal.common.assertion.AssertParameter;
import io.restassured.response.ResponseBody;
import io.restassured.response.ResponseOptions;
import org.hamcrest.Matcher;

import java.util.ArrayList;
import java.util.List;

import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.anyOf;

/**
 * Allows you to compose {@link ResponseAwareMatcher}'s with other {@link ResponseAwareMatcher}s or Hamcrest {@link Matcher}s.
 *
 * @since 2.5.0
 */
public class ResponseAwareMatcherComposer {

    // and

    /**
     * Compose this {@link ResponseAwareMatcher} with another {@link ResponseAwareMatcher}.
     *
     * @param matcher1 The first matcher to compose
     * @param matcher2 The second matcher to compose
     * @return A and-composed {@link ResponseAwareMatcher}.
     */
    @SuppressWarnings("unchecked")
    public static  & ResponseOptions> ResponseAwareMatcher and(
            final ResponseAwareMatcher matcher1, final ResponseAwareMatcher matcher2) {
        return and(matcher1, matcher2, new ResponseAwareMatcher[0]);
    }

    /**
     * Compose this {@link ResponseAwareMatcher} with another {@link ResponseAwareMatcher}.
     *
     * @param matcher1           The first matcher to compose
     * @param matcher2           The second matcher to compose
     * @param additionalMatchers The additional matchers to compose
     * @return A and-composed {@link ResponseAwareMatcher}.
     */
    @SuppressWarnings("unchecked")
    public static  & ResponseOptions> ResponseAwareMatcher and(
            final ResponseAwareMatcher matcher1, final ResponseAwareMatcher matcher2,
            final ResponseAwareMatcher... additionalMatchers) {
        return response -> {
            Matcher[] matchers = toHamcrestMatchers(response, matcher1, matcher2, additionalMatchers);
            return allOf((Matcher[]) matchers);
        };
    }

    /**
     * Compose this {@link ResponseAwareMatcher} with another {@link ResponseAwareMatcher}.
     *
     * @param matcher1           The first matcher to compose
     * @param matcher2           The second matcher to compose
     * @param additionalMatchers The additional matchers to compose
     * @return A and-composed {@link ResponseAwareMatcher}.
     * // TODO Add @SafeVarargs when migrating to Java 7+
     */
    public static  & ResponseOptions> ResponseAwareMatcher and(
            final ResponseAwareMatcher matcher1, final Matcher matcher2,
            final ResponseAwareMatcher... additionalMatchers) {
        return and(matcher1, response -> matcher2, additionalMatchers);
    }

    /**
     * Compose this {@link ResponseAwareMatcher} with another {@link ResponseAwareMatcher}.
     *
     * @param matcher1 The first matcher to compose
     * @param matcher2 The second matcher to compose
     * @return A and-composed {@link ResponseAwareMatcher}.
     */
    @SuppressWarnings("unchecked")
    public static  & ResponseOptions> ResponseAwareMatcher and(
            final ResponseAwareMatcher matcher1, final Matcher matcher2) {
        return and(matcher1, matcher2, new ResponseAwareMatcher[0]);
    }

    /**
     * Compose this {@link ResponseAwareMatcher} with another {@link ResponseAwareMatcher}.
     *
     * @param matcher1           The first matcher to compose
     * @param matcher2           The second matcher to compose
     * @param additionalMatchers The additional matchers to compose
     * @return A and-composed {@link ResponseAwareMatcher}.
     * // TODO Add @SafeVarargs when migrating to Java 7+
     */
    public static  & ResponseOptions> ResponseAwareMatcher and(
            final Matcher matcher1, final ResponseAwareMatcher matcher2,
            final ResponseAwareMatcher... additionalMatchers) {
        return and(matcher2, matcher1, additionalMatchers);
    }

    // or

    /**
     * Compose this {@link ResponseAwareMatcher} with another {@link ResponseAwareMatcher}.
     *
     * @param matcher1 The first matcher to compose
     * @param matcher2 The second matcher to compose
     * @return A or-composed {@link ResponseAwareMatcher}.
     */
    @SuppressWarnings("unchecked")
    public static  & ResponseOptions> ResponseAwareMatcher or(
            final ResponseAwareMatcher matcher1, final ResponseAwareMatcher matcher2) {
        return or(matcher1, matcher2, new ResponseAwareMatcher[0]);
    }

    /**
     * Compose this {@link ResponseAwareMatcher} with another {@link ResponseAwareMatcher}.
     *
     * @param matcher1           The first matcher to compose
     * @param matcher2           The second matcher to compose
     * @param additionalMatchers The additional matchers to compose
     * @return A or-composed {@link ResponseAwareMatcher}.
     */
    @SuppressWarnings("unchecked")
    public static  & ResponseOptions> ResponseAwareMatcher or(
            final ResponseAwareMatcher matcher1, final ResponseAwareMatcher matcher2,
            final ResponseAwareMatcher... additionalMatchers) {
        return response -> {
            Matcher[] matchers = toHamcrestMatchers(response, matcher1, matcher2, additionalMatchers);
            return anyOf((Matcher[]) matchers);
        };
    }

    /**
     * Compose this {@link ResponseAwareMatcher} with another {@link ResponseAwareMatcher}.
     *
     * @param matcher1           The first matcher to compose
     * @param matcher2           The second matcher to compose
     * @param additionalMatchers The additional matchers to compose
     * @return A or-composed {@link ResponseAwareMatcher}.
     * // TODO Add @SafeVarargs when migrating to Java 7+
     */
    public static  & ResponseOptions> ResponseAwareMatcher or(
            final ResponseAwareMatcher matcher1, final Matcher matcher2,
            final ResponseAwareMatcher... additionalMatchers) {
        return or(matcher1, response -> matcher2, additionalMatchers);
    }

    /**
     * Compose this {@link ResponseAwareMatcher} with another {@link ResponseAwareMatcher}.
     *
     * @param matcher1 The first matcher to compose
     * @param matcher2 The second matcher to compose
     * @return A and-composed {@link ResponseAwareMatcher}.
     */
    @SuppressWarnings("unchecked")
    public static  & ResponseOptions> ResponseAwareMatcher or(
            final ResponseAwareMatcher matcher1, final Matcher matcher2) {
        return or(matcher1, matcher2, new ResponseAwareMatcher[0]);
    }

    /**
     * Compose this {@link ResponseAwareMatcher} with another {@link ResponseAwareMatcher}.
     *
     * @param matcher1           The first matcher to compose
     * @param matcher2           The second matcher to compose
     * @param additionalMatchers The additional matchers to compose
     * @return A or-composed {@link ResponseAwareMatcher}.
     * // TODO Add @SafeVarargs when migrating to Java 7+
     */
    public static  & ResponseOptions> ResponseAwareMatcher or(
            final Matcher matcher1, final ResponseAwareMatcher matcher2,
            final ResponseAwareMatcher... additionalMatchers) {
        return or(matcher2, matcher1, additionalMatchers);
    }

    private static  & ResponseOptions> Matcher[] toHamcrestMatchers(T response,
                                                                                                    ResponseAwareMatcher matcher1,
                                                                                                    ResponseAwareMatcher matcher2,
                                                                                                    ResponseAwareMatcher[] additionalMatchers) throws Exception {
        AssertParameter.notNull(matcher1, ResponseAwareMatcher.class);
        AssertParameter.notNull(matcher2, ResponseAwareMatcher.class);

        List> hamcrestMatchers = new ArrayList>();
        hamcrestMatchers.add(matcher1.matcher(response));
        hamcrestMatchers.add(matcher2.matcher(response));

        if (additionalMatchers != null) {
            for (ResponseAwareMatcher additionalMatcher : additionalMatchers) {
                hamcrestMatchers.add(additionalMatcher.matcher(response));
            }
        }

        return hamcrestMatchers.toArray(new Matcher[hamcrestMatchers.size()]);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy