com.github.dreamhead.moco.setting.BaseSetting 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.setting;
import com.github.dreamhead.moco.MocoConfig;
import com.github.dreamhead.moco.MocoEvent;
import com.github.dreamhead.moco.MocoEventTrigger;
import com.github.dreamhead.moco.Request;
import com.github.dreamhead.moco.RequestMatcher;
import com.github.dreamhead.moco.ResponseSetting;
import com.github.dreamhead.moco.internal.BaseResponseSettingConfiguration;
import com.github.dreamhead.moco.internal.SessionContext;
import static com.github.dreamhead.moco.util.Configs.configItem;
import static com.github.dreamhead.moco.util.Configs.configItems;
public abstract class BaseSetting>
extends BaseResponseSettingConfiguration implements Setting {
private final RequestMatcher matcher;
protected abstract BaseSetting createSetting(final RequestMatcher matcher);
protected abstract RequestMatcher configMatcher(final RequestMatcher matcher, final MocoConfig config);
protected BaseSetting(final RequestMatcher matcher) {
this.matcher = matcher;
}
@Override
public boolean match(final Request request) {
return this.matcher.match(request) && this.handler != null;
}
@Override
public void writeToResponse(final SessionContext context) {
this.handler.writeToResponse(context);
this.fireCompleteEvent(context.getRequest());
}
private void fireCompleteEvent(final Request request) {
for (MocoEventTrigger eventTrigger : eventTriggers) {
if (eventTrigger.isFor(MocoEvent.COMPLETE)) {
eventTrigger.fireEvent(request);
}
}
}
@Override
public Setting apply(final MocoConfig config) {
BaseSetting setting = createSetting(configMatcher(this.matcher, config));
setting.handler = configItem(this.handler, config);
setting.eventTriggers = configItems(eventTriggers, config);
return setting;
}
}