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

com.github.dreamhead.moco.internal.BaseResponseSettingConfiguration 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.internal;

import com.github.dreamhead.moco.MocoEventTrigger;
import com.github.dreamhead.moco.ResponseHandler;
import com.github.dreamhead.moco.ResponseSetting;
import com.google.common.reflect.TypeToken;

import java.util.List;

import static com.github.dreamhead.moco.handler.AndResponseHandler.and;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Lists.newArrayList;

public abstract class BaseResponseSettingConfiguration>
        extends AbstractResponseBase implements ResponseSetting {

    protected ResponseHandler handler;
    protected List eventTriggers = newArrayList();
    private final Class clazz;

    @SuppressWarnings("unchecked")
    protected BaseResponseSettingConfiguration() {
        this.clazz = (Class) new TypeToken(getClass()) {}.getRawType();
    }

    private T self() {
        return clazz.cast(this);
    }

    @Override
    public T response(final ResponseHandler handler, final ResponseHandler... handlers) {
        ResponseHandler responseHandler = and(checkNotNull(handler, "Handler should not be null"),
                checkNotNull(handlers, "Handlers should not be null"));
        this.handler = targetHandler(responseHandler);
        return self();
    }

    private ResponseHandler targetHandler(final ResponseHandler responseHandler) {
        if (this.handler == null) {
            return responseHandler;
        }

        return and(this.handler, responseHandler);
    }

    @Override
    public T on(final MocoEventTrigger trigger) {
        this.eventTriggers.add(checkNotNull(trigger, "Trigger should not be null"));
        return self();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy