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

com.github.dreamhead.moco.setting.BaseSetting 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.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(RequestMatcher matcher);

    protected abstract RequestMatcher configMatcher(RequestMatcher matcher, 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;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy