com.github.dreamhead.moco.internal.ApiUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of moco-core Show documentation
Show all versions of moco-core Show documentation
Moco is an easy setup stub framework, mainly focusing on testing and integration.
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.extractor.ContentRequestExtractor;
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.EqRequestMatcher;
import com.github.dreamhead.moco.matcher.JsonRequestMatcher;
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);
}
public static RequestMatcher by(final RequestExtractor extractor, final Resource expected) {
if ("json".equalsIgnoreCase(expected.id())) {
return new JsonRequestMatcher(expected, ContentRequestExtractor.class.cast(extractor));
}
return new EqRequestMatcher<>(extractor, expected);
}
private ApiUtils() {
}
}