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

com.codepoetics.fluvius.test.matchers.AFlowEvent Maven / Gradle / Ivy

There is a newer version: 1.10
Show newest version
package com.codepoetics.fluvius.test.matchers;

import com.codepoetics.fluvius.api.history.*;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;

import java.util.Map;
import java.util.UUID;

public final class AFlowEvent extends BasePropertyMatcher> {

  public static  AFlowEvent stepStarted() {
    return new AFlowEvent<>(StepStartedEvent.class);
  }

  public static  AFlowEvent stepStarted(Map scratchpadState) {
    return stepStarted(Matchers.equalTo(scratchpadState));
  }

  public static  AFlowEvent stepStarted(Matcher> scratchpadStateMatcher) {
    AFlowEvent flowEventMatcher = stepStarted();
    flowEventMatcher.scratchpadStateMatcher = scratchpadStateMatcher;
    return flowEventMatcher;
  }

  public static  AFlowEvent stepSucceeded() {
    return new AFlowEvent<>(StepSucceededEvent.class);
  }

  public static  AFlowEvent stepSucceeded(T result) {
    return stepSucceeded(Matchers.equalTo(result));
  }

  public static  AFlowEvent stepSucceeded(Matcher resultMatcher) {
    AFlowEvent flowEventMatcher = stepSucceeded();
    flowEventMatcher.resultMatcher = resultMatcher;
    return flowEventMatcher;
  }

  public static  AFlowEvent stepFailed() {
    return new AFlowEvent<>(StepFailedEvent.class);
  }

  public static  AFlowEvent stepFailed(T result) {
    return stepFailed(Matchers.equalTo(result));
  }

  public static  AFlowEvent stepFailed(Matcher reasonMatcher) {
    AFlowEvent flowEventMatcher = stepFailed();
    flowEventMatcher.reasonMatcher = reasonMatcher;
    return flowEventMatcher;
  }

  private final Class expectedEventType;
  private Matcher flowIdMatcher;
  private Matcher stepIdMatcher;
  private Matcher timestampMatcher;
  private Matcher> scratchpadStateMatcher;
  private Matcher resultMatcher;
  private Matcher reasonMatcher;

  private AFlowEvent(Class expectedEventType) {
    super(expectedEventType.getSimpleName());
    this.expectedEventType = expectedEventType;
  }

  public AFlowEvent withFlowId(UUID flowId) {
    return withFlowId(Matchers.equalTo(flowId));
  }

  public AFlowEvent withFlowId(Matcher flowIdMatcher) {
    this.flowIdMatcher = flowIdMatcher;
    return this;
  }

  public AFlowEvent withStepId(UUID stepId) {
    return withStepId(Matchers.equalTo(stepId));
  }

  public AFlowEvent withStepId(Matcher stepIdMatcher) {
    this.stepIdMatcher = stepIdMatcher;
    return this;
  }

  public AFlowEvent withTimestamp(long timestamp) {
    return withTimestamp(Matchers.equalTo(timestamp));
  }

  public AFlowEvent withTimestamp(Matcher timestampMatcher) {
    this.timestampMatcher = timestampMatcher;
    return this;
  }

  @Override
  protected void describeProperties(PropertyDescriber describer) {
    describer
        .describeProperty("flowId", flowIdMatcher)
        .describeProperty("stepId", stepIdMatcher)
        .describeProperty("timestamp", timestampMatcher)
        .describeProperty("scratchpadState", scratchpadStateMatcher)
        .describeProperty("result", resultMatcher)
        .describeProperty("reason", reasonMatcher);
  }

  @Override
  protected void checkProperties(FlowEvent flowEvent, final PropertyMismatchDescriber describer) {
    describer.check("type", flowEvent, Matchers.instanceOf(expectedEventType))
        .check("flowId", flowEvent.getFlowId(), flowIdMatcher)
        .check("stepId", flowEvent.getStepId(), stepIdMatcher)
        .check("timestamp", flowEvent.getTimestamp(), timestampMatcher);

    flowEvent.translate(new FlowEventTranslator() {
      @Override
      public PropertyMismatchDescriber translateStepStartedEvent(StepStartedEvent event) {
        return describer.check("scratchpadState", event.getScratchpadState(), scratchpadStateMatcher);
      }

      @Override
      public PropertyMismatchDescriber translateStepSucceededEvent(StepSucceededEvent event) {
        return describer.check("result", event.getResult(), resultMatcher);
      }

      @Override
      public PropertyMismatchDescriber translateStepFailedEvent(StepFailedEvent event) {
        return describer.check("reason", event.getReason(), reasonMatcher);
      }
    });
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy