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

net.serenitybdd.screenplay.BaseConsequence Maven / Gradle / Ivy

There is a newer version: 4.2.12
Show newest version
package net.serenitybdd.screenplay;

import com.google.common.base.Optional;
import net.thucydides.core.steps.StepEventBus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class BaseConsequence implements Consequence {

    private Class complaintType;
    private String complaintDetails;
    protected Optional optionalPrecondition = Optional.absent();
    protected Optional explanation = Optional.absent();

    private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());

    protected Error errorFrom(Throwable actualError) {
        if (actualError instanceof Error) {
            return (Error) actualError;
        }
        return new Error(actualError);
    }

    protected void throwComplaintTypeErrorIfSpecified(Error actualError) {
        if (complaintType != null) {
            LOGGER.error("Could not resolve question", actualError);
            throw Complaint.from(complaintType, complaintDetails, actualError);
        }
    }

    protected boolean thisStepShouldBeIgnored() {
        return (StepEventBus.getEventBus().currentTestIsSuspended() || StepEventBus.getEventBus().aStepInTheCurrentTestHasFailed());
    }

    @Override
    public BaseConsequence orComplainWith(Class complaintType) {
        return orComplainWith(complaintType, null);
    }

    @Override
    public BaseConsequence orComplainWith(Class complaintType, String complaintDetails) {
        this.complaintType = complaintType;
        this.complaintDetails = complaintDetails;
        return this;
    }

    @Override
    public Consequence whenAttemptingTo(Performable performable) {
        this.optionalPrecondition = Optional.of(performable);
        return this;
    }
    @Override
    public Consequence because(String explanation) {
        this.explanation = Optional.fromNullable(explanation);
        return this;
    }

    protected Optional inputValues() {
        if (!optionalPrecondition.isPresent()) {
            return Optional.absent();
        }

        if (!(optionalPrecondition.get() instanceof RecordsInputs)) {
            return Optional.absent();
        }

        return Optional.of(((RecordsInputs) optionalPrecondition.get()).getInputValues());
    }

    protected String addRecordedInputValuesTo(String message) {
        if (!inputValues().isPresent()) {
            return message;
        }
        return message + " [" + inputValues().get() + "]";
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy