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

com.github.dreamhead.moco.internal.ApiUtils 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.internal;

import com.github.dreamhead.moco.Moco;
import com.github.dreamhead.moco.MocoMonitor;
import com.github.dreamhead.moco.RequestExtractor;
import com.github.dreamhead.moco.RequestMatcher;
import com.github.dreamhead.moco.ResponseHandler;
import com.github.dreamhead.moco.handler.failover.DefaultFailoverExecutor;
import com.github.dreamhead.moco.handler.failover.FailoverExecutor;
import com.github.dreamhead.moco.matcher.ContainMatcher;
import com.github.dreamhead.moco.matcher.EndsWithMatcher;
import com.github.dreamhead.moco.matcher.MatchMatcher;
import com.github.dreamhead.moco.matcher.StartsWithMatcher;
import com.github.dreamhead.moco.monitor.CompositeMonitor;
import com.github.dreamhead.moco.monitor.DefaultLogFormatter;
import com.github.dreamhead.moco.monitor.FileLogWriter;
import com.github.dreamhead.moco.monitor.LogMonitor;
import com.github.dreamhead.moco.monitor.LogWriter;
import com.github.dreamhead.moco.resource.Resource;
import com.github.dreamhead.moco.resource.reader.ExtractorVariable;
import com.github.dreamhead.moco.resource.reader.Variable;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;

import java.io.File;
import java.nio.charset.Charset;

import static com.github.dreamhead.moco.util.Iterables.asIterable;
import static com.google.common.collect.Maps.transformEntries;

public final class ApiUtils {
    public static MocoMonitor mergeMonitor(final MocoMonitor monitor, final MocoMonitor monitor2,
                                           final MocoMonitor[] monitors) {
        return new CompositeMonitor(asIterable(monitor, monitor2, monitors));
    }

    private static Maps.EntryTransformer, Variable> toVariable() {
        return new Maps.EntryTransformer, Variable>() {
            @Override
            @SuppressWarnings("unchecked")
            public Variable transformEntry(final String key, final RequestExtractor value) {
                return new ExtractorVariable(value);
            }
        };
    }

    public static ImmutableMap toVariables(
            final ImmutableMap> variables) {
        return ImmutableMap.copyOf(transformEntries(variables, toVariable()));
    }

    public static Function textToResource() {
        return new Function() {
            @Override
            public ResponseHandler apply(final String content) {
                return Moco.with(Moco.text(content));
            }
        };
    }

    public static Function resourceToResourceHandler() {
        return new Function() {
            @Override
            public ResponseHandler apply(final Resource content) {
                return Moco.with(content);
            }
        };
    }

    public static FailoverExecutor failoverExecutor(final String file) {
        return new DefaultFailoverExecutor(new File(file));
    }

    public static LogWriter fileLogWriter(final String filename, final Optional charset) {
        return new FileLogWriter(filename, charset);
    }

    public static MocoMonitor log(final LogWriter writer) {
        return new LogMonitor(new DefaultLogFormatter(), writer);
    }

    public static  RequestMatcher match(final RequestExtractor extractor, final Resource expected) {
        return new MatchMatcher(extractor, expected);
    }

    public static  RequestMatcher startsWith(final RequestExtractor extractor, final Resource resource) {
        return new StartsWithMatcher(extractor, resource);
    }

    public static  RequestMatcher endsWith(final RequestExtractor extractor, final Resource resource) {
        return new EndsWithMatcher(extractor, resource);
    }

    public static  RequestMatcher contain(final RequestExtractor extractor, final Resource resource) {
        return new ContainMatcher(extractor, resource);
    }

    private ApiUtils() {
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy