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

com.github.dreamhead.moco.matcher.CompositeRequestMatcher Maven / Gradle / Ivy

Go to download

Moco is an easy setup stub framework, mainly focusing on testing and integration.

There is a newer version: 1.5.0
Show newest version
package com.github.dreamhead.moco.matcher;

import com.github.dreamhead.moco.MocoConfig;
import com.github.dreamhead.moco.Request;
import com.github.dreamhead.moco.RequestMatcher;
import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;

import static com.google.common.collect.FluentIterable.from;

public abstract class CompositeRequestMatcher extends AbstractRequestMatcher {
    protected abstract RequestMatcher newMatcher(Iterable matchers);

    private final Iterable matchers;

    protected CompositeRequestMatcher(final Iterable matchers) {
        this.matchers = matchers;
    }

    private Iterable applyToMatchers(final MocoConfig config) {
        FluentIterable appliedMatchers = from(matchers).transform(applyConfig(config));
        if (matchers.equals(appliedMatchers)) {
            return this.matchers;
        }

        return appliedMatchers;
    }

    private Function applyConfig(final MocoConfig config) {
        return new Function() {
            @Override
            public RequestMatcher apply(final RequestMatcher matcher) {
                return matcher.apply(config);
            }
        };
    }


    @Override
    public final RequestMatcher doApply(final MocoConfig config) {
        Iterable appliedMatchers = applyToMatchers(config);
        if (appliedMatchers == this.matchers) {
            return this;
        }

        return newMatcher(appliedMatchers);
    }

    @Override
    public final boolean match(final Request request) {
        return doMatch(request, matchers);
    }

    protected abstract boolean doMatch(Request request, Iterable matchers);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy