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

com.github.dreamhead.moco.mount.MountMatcher 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.mount;

import com.github.dreamhead.moco.MocoConfig;
import com.github.dreamhead.moco.Request;
import com.github.dreamhead.moco.RequestMatcher;
import com.github.dreamhead.moco.matcher.AbstractRequestMatcher;
import com.google.common.base.Optional;

import java.io.File;

import static com.google.common.base.Predicates.and;
import static com.google.common.base.Strings.isNullOrEmpty;

public final class MountMatcher extends AbstractRequestMatcher {
    private final MountPathExtractor extractor;

    private final File dir;
    private final Iterable predicates;
    private final MountTo target;

    public MountMatcher(final File dir, final MountTo target, final Iterable predicates) {
        this.dir = dir;
        this.predicates = predicates;
        this.target = target;
        this.extractor = new MountPathExtractor(target);
    }

    @Override
    public boolean match(final Request request) {
        Optional optionalPath = extractor.extract(request);
        if (optionalPath.isPresent()) {
            String relativePath = optionalPath.get();
            return isTarget(relativePath) && new File(dir, relativePath).exists();
        }
        return false;
    }

    @Override
    @SuppressWarnings("unchecked")
    public RequestMatcher doApply(final MocoConfig config) {
        if (config.isFor(MocoConfig.URI_ID)) {
            return new MountMatcher(this.dir, this.target.apply(config), this.predicates);
        }

        if (config.isFor(MocoConfig.FILE_ID)) {
            return new MountMatcher(new File((String) config.apply(this.dir.getName())), this.target, this.predicates);
        }

        return this;
    }

    private boolean isTarget(final String relativePath) {
        return !isNullOrEmpty(relativePath) && and(predicates).apply(relativePath);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy