org.mockserver.mock.MockServer Maven / Gradle / Ivy
package org.mockserver.mock;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import org.mockserver.client.serialization.ExpectationSerializer;
import org.mockserver.matchers.Times;
import org.mockserver.model.EqualsHashCodeToString;
import org.mockserver.model.HttpRequest;
import org.mockserver.model.HttpResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* @author jamesdbloom
*/
public class MockServer extends EqualsHashCodeToString {
protected final List expectations = new ArrayList();
private Logger requestLogger = LoggerFactory.getLogger("REQUEST");
public synchronized Expectation when(HttpRequest httpRequest) {
return when(httpRequest, Times.unlimited());
}
public synchronized Expectation when(final HttpRequest httpRequest, Times times) {
Expectation expectation;
if (times.isUnlimited()) {
Collection existingExpectationsWithMatchingRequest = Collections2.filter(expectations, new Predicate() {
public boolean apply(Expectation expectation) {
return expectation.contains(httpRequest);
}
});
if (!existingExpectationsWithMatchingRequest.isEmpty()) {
for (Expectation existingExpectation : existingExpectationsWithMatchingRequest) {
existingExpectation.setNotUnlimitedResponses();
}
expectation = new Expectation(httpRequest, Times.once());
} else {
expectation = new Expectation(httpRequest, Times.unlimited());
}
} else {
expectation = new Expectation(httpRequest, times);
}
expectations.add(expectation);
return expectation;
}
public synchronized HttpResponse handle(HttpRequest httpRequest) {
ArrayList expectations = new ArrayList(this.expectations);
for (Expectation expectation : expectations) {
if (expectation.matches(httpRequest)) {
if (!expectation.getTimes().greaterThenZero()) {
if (this.expectations.contains(expectation)) {
this.expectations.remove(expectation);
}
}
return expectation.getHttpResponse();
}
}
return null;
}
public synchronized void clear(HttpRequest httpRequest) {
if (httpRequest != null) {
for (Expectation expectation : new ArrayList(expectations)) {
if (expectation.matches(httpRequest)) {
if (this.expectations.contains(expectation)) {
this.expectations.remove(expectation);
}
}
}
} else {
reset();
}
}
public synchronized void reset() {
this.expectations.clear();
}
public synchronized void dumpToLog(HttpRequest httpRequest) {
if (httpRequest != null) {
ExpectationSerializer expectationSerializer = new ExpectationSerializer();
for (Expectation expectation : new ArrayList(expectations)) {
if (expectation.matches(httpRequest)) {
requestLogger.warn(expectationSerializer.serialize(expectation));
}
}
} else {
ExpectationSerializer expectationSerializer = new ExpectationSerializer();
for (Expectation expectation : new ArrayList(expectations)) {
requestLogger.warn(expectationSerializer.serialize(expectation));
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy