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

org.camunda.bpm.engine.test.assertions.bpmn.AbstractProcessAssert Maven / Gradle / Ivy

The newest version!
package org.camunda.bpm.engine.test.assertions.bpmn;

import org.assertj.core.api.AbstractAssert;
import org.assertj.core.api.Assertions;
import org.camunda.bpm.engine.*;
import org.camunda.bpm.engine.externaltask.ExternalTaskQuery;
import org.camunda.bpm.engine.history.*;
import org.camunda.bpm.engine.repository.CaseDefinitionQuery;
import org.camunda.bpm.engine.repository.ProcessDefinitionQuery;
import org.camunda.bpm.engine.runtime.*;
import org.camunda.bpm.engine.task.TaskQuery;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Martin Schimak 
 */
public abstract class AbstractProcessAssert, A> extends AbstractAssert {

  protected ProcessEngine engine;

  private static ThreadLocal, AbstractProcessAssert>>
    lastAsserts = new ThreadLocal, AbstractProcessAssert>>();

  protected AbstractProcessAssert(ProcessEngine engine, A actual, Class selfType) {
    super(actual, selfType);
    this.engine = engine;
    setLastAssert(selfType, this);
  }

  /*
   * Delivers the the actual object under test.
   */
  public A getActual() {
    return actual;
  }

  /*
   * Method definition meant to deliver the current/refreshed persistent state of
   * the actual object under test and expecting that such a current state actually exists.
   */
  protected A getExistingCurrent() {
    Assertions.assertThat(actual)
      .overridingErrorMessage("Expecting assertion to be called on non-null object, but found it to be null!")
      .isNotNull();
    A current = getCurrent();
    Assertions.assertThat(current)
      .overridingErrorMessage(
        "Expecting %s to be unfinished, but found that it already finished!",
        toString(actual))
      .isNotNull();
    return current;
  }

  /*
   * Abstract method definition meant to deliver the current/refreshed persistent state of
   * the actual object under test. Needs to be correctly implemented by implementations of this.
   */
  protected abstract A getCurrent();

  /*
   * Abstract method definition meant to deliver a loggable string representation of the
   * given object of same type as the actual object under test.
   */
  protected abstract String toString(A object);

  public static void resetLastAsserts() {
    getLastAsserts().clear();
  }

  @SuppressWarnings("unchecked")
  public static  S getLastAssert(Class assertClass) {
    return (S) getLastAsserts().get(assertClass);
  }

  private static void setLastAssert(Class assertClass, AbstractProcessAssert assertInstance) {
    getLastAsserts().put(assertClass, assertInstance);
  }

  private static Map, AbstractProcessAssert> getLastAsserts() {
    Map, AbstractProcessAssert> asserts = lastAsserts.get();
    if (asserts == null)
      lastAsserts.set(asserts = new HashMap, AbstractProcessAssert>());
    return asserts;
  }

  protected RepositoryService repositoryService() {
    return engine.getRepositoryService();
  }

  protected RuntimeService runtimeService() {
    return engine.getRuntimeService();
  }

  protected FormService formService() {
    return engine.getFormService();
  }

  protected TaskService taskService() {
    return engine.getTaskService();
  }

  protected HistoryService historyService() {
    return engine.getHistoryService();
  }

  protected IdentityService identityService() {
    return engine.getIdentityService();
  }

  protected ManagementService managementService() {
    return engine.getManagementService();
  }

  protected AuthorizationService authorizationService() {
    return engine.getAuthorizationService();
  }

  protected CaseService caseService() {
    return engine.getCaseService();
  }

  protected ExternalTaskService externalTaskService() { return engine.getExternalTaskService(); }

  /*
   * TaskQuery, unnarrowed. Narrow this to {@link ProcessInstance} (or {@link ProcessDefinition})
   * by overriding this method in sub classes specialised to verify a specific
   * process engine domain class.
   */
  protected TaskQuery taskQuery() {
    return taskService().createTaskQuery();
  }

  /*
   * JobQuery, unnarrowed. Narrow this to {@link ProcessInstance} (or {@link ProcessDefinition})
   * by overriding this method in sub classes specialised to verify a specific
   * process engine domain class.
   */
  protected JobQuery jobQuery() {
    return managementService().createJobQuery();
  }

  /*
   * ProcessInstanceQuery, unnarrowed. Narrow this to {@link ProcessInstance} (or
   * {@link ProcessDefinition}) by overriding this method in sub classes specialised to
   * verify a specific process engine domain class.
   */
  protected ProcessInstanceQuery processInstanceQuery() {
    return runtimeService().createProcessInstanceQuery();
  }

  /*
   * ExecutionQuery, unnarrowed. Narrow this to {@link ProcessInstance} (or {@link ProcessDefinition})
   * by overriding this method in sub classes specialised to verify a specific
   * process engine domain class.
   */
  protected ExecutionQuery executionQuery() {
    return runtimeService().createExecutionQuery();
  }

  /*
   * VariableInstanceQuery, unnarrowed. Narrow this to {@link ProcessInstance} (or
   * {@link ProcessDefinition}) by overriding this method in sub classes specialised to
   * verify a specific process engine domain class.
   */
  protected VariableInstanceQuery variableInstanceQuery() {
    return runtimeService().createVariableInstanceQuery();
  }

  /*
   * HistoricActivityInstanceQuery, unnarrowed. Narrow this to {@link ProcessInstance} (or
   * {@link ProcessDefinition}) by overriding this method in sub classes specialised to
   * verify a specific process engine domain class.
   */
  protected HistoricActivityInstanceQuery historicActivityInstanceQuery() {
    return historyService().createHistoricActivityInstanceQuery();
  }

  /*
   * HistoricDetailQuery, unnarrowed. Narrow this to {@link ProcessInstance} (or
   * {@link ProcessDefinition}) by overriding this method in sub classes specialised to
   * verify a specific process engine domain class.
   */
  protected HistoricDetailQuery historicDetailQuery() {
    return historyService().createHistoricDetailQuery();
  }

  /*
   * HistoricProcessInstanceQuery, unnarrowed. Narrow this to {@link ProcessInstance} (or
   * {@link ProcessDefinition}) by overriding this method in sub classes specialised to
   * verify a specific process engine domain class.
   */
  protected HistoricProcessInstanceQuery historicProcessInstanceQuery() {
    return historyService().createHistoricProcessInstanceQuery();
  }

  /*
   * HistoricTaskInstanceQuery, unnarrowed. Narrow this to {@link ProcessInstance} (or
   * {@link ProcessDefinition}) by overriding this method in sub classes specialised to
   * verify a specific process engine domain class.
   */
  protected HistoricTaskInstanceQuery historicTaskInstanceQuery() {
    return historyService().createHistoricTaskInstanceQuery();
  }

  /*
   * HistoricVariableInstanceQuery, unnarrowed. Narrow this to {@link ProcessInstance} (or
   * {@link ProcessDefinition}) by overriding this method in sub classes specialised to
   * verify a specific process engine domain class.
   */
  protected HistoricVariableInstanceQuery historicVariableInstanceQuery() {
    return historyService().createHistoricVariableInstanceQuery();
  }

  /*
   * ProcessDefinitionQuery, unnarrowed. Narrow this to {@link ProcessInstance} (or
   * {@link ProcessDefinition}) by overriding this method in sub classes specialised to
   * verify a specific process engine domain class.
   */
  protected ProcessDefinitionQuery processDefinitionQuery() {
    return repositoryService().createProcessDefinitionQuery();
  }

  /*
   * CaseExecutionQuery, unnarrowed. Narrow this to {@link CaseInstance} (or
   * {@link CaseDefinition}) by overriding this method in sub classes specialized to
   * verify a specific process engine domain class.
   */
  protected CaseExecutionQuery caseExecutionQuery() {
    return caseService().createCaseExecutionQuery();
  }

  /*
   * CaseDefinitionQuery, unnarrowed. Narrow this to {@link CaseInstance} (or
   * {@link CaseDefinition}) by overriding this method in sub classes specialized to
   * verify a specific process engine domain class.
   */
  protected CaseDefinitionQuery caseDefinitionQuery() {
    return repositoryService().createCaseDefinitionQuery();
  }

  /*
   * CaseInstanceQuery, unnarrowed. Narrow this to {@link CaseInstance} (or
   * {@link CaseDefinition}) by overriding this method in sub classes specialized to
   * verify a specific process engine domain class.
   */
  protected CaseInstanceQuery caseInstanceQuery() {
    return caseService().createCaseInstanceQuery();
  }

  /*
   * HistoricCaseActivityInstanceQuery, unnarrowed. Narrow this to {@link CaseInstance} (or
   * {@link CaseDefinition}) by overriding this method in sub classes specialised to
   * verify a specific process engine domain class.
   */
  protected HistoricCaseActivityInstanceQuery historicCaseActivityInstanceQuery() {
    return historyService().createHistoricCaseActivityInstanceQuery();
  }

  /**
   * ExternalTaskQuery, unnarrowed. Narrow this to by overriding this method in sub classes specialized to
   * verify a specific process engine domain class.
   */
  protected ExternalTaskQuery externalTaskQuery() { return externalTaskService().createExternalTaskQuery(); }

}