com.noenv.wiremongo.mapping.MappingBase Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of vertx-wiremongo Show documentation
Show all versions of vertx-wiremongo Show documentation
Lightweight mongo mocking for Vert.x
package com.noenv.wiremongo.mapping;
import com.noenv.wiremongo.StubBase;
import com.noenv.wiremongo.command.Command;
import com.noenv.wiremongo.command.CommandBase;
import com.noenv.wiremongo.verification.Verification;
import io.vertx.core.json.JsonObject;
import java.util.LinkedList;
import java.util.Objects;
public abstract class MappingBase> extends CommandBase implements Mapping {
private static final StubBase DUMMY_STUB = x -> null;
private final LinkedList> stubs = new LinkedList<>();
private int priority;
private int times;
private Verification verification;
public MappingBase(String method) {
super(method);
stubs.add(DUMMY_STUB);
}
public MappingBase(JsonObject json) {
super(json.getString("method"));
priority = json.getInteger("priority", 0);
parseStub(json);
}
@Override
public int priority() {
return priority;
}
@Override
public C priority(int priority) {
this.priority = priority;
return self();
}
@Override
public C validFor(int times) {
this.times = times;
return self();
}
@Override
public int validFor() {
return this.times;
}
@Override
public T invoke(U command) throws Throwable {
StubBase s = stubs.size() > 1 ? stubs.pop() : stubs.peek();
if (verification != null) {
verification.execute();
}
return s.invoke(command);
}
@Override
public C stub(StubBase stub) {
stubs.remove(DUMMY_STUB);
stubs.add(stub);
return self();
}
@Override
public boolean matches(Command c) {
return Objects.equals(method(), c.method());
}
@Override
public C verify(Verification verification) {
this.verification = verification;
return self();
}
private void parseStub(JsonObject json) {
if (json.containsKey("response")) {
returns(parseResponse(json.getValue("response")));
} else if (json.containsKey("error")) {
returnsError(new Exception(json.getJsonObject("error").getString("message")));
} else {
throw new IllegalArgumentException("either response or error must be set!");
}
}
protected abstract T parseResponse(Object jsonValue);
@SuppressWarnings("unchecked")
protected C self() {
return (C) this;
}
}