@Before
  public void deployProcessesAndCreateMigrationPlan() {
    ProcessDefinition sourceDefinition =
        testHelper.deployAndGetDefinition(ProcessModels.ONE_TASK_PROCESS);
    ProcessDefinition targetDefinition =
        testHelper.deployAndGetDefinition(ProcessModels.ONE_TASK_PROCESS);

    migrationPlan =
        engineRule
            .getRuntimeService()
            .createMigrationPlan(sourceDefinition.getId(), targetDefinition.getId())
            .mapEqualActivities()
            .build();

    ProcessInstance pi =
        engineRule.getRuntimeService().startProcessInstanceById(sourceDefinition.getId());

    batch1 =
        engineRule
            .getRuntimeService()
            .newMigration(migrationPlan)
            .processInstanceIds(Arrays.asList(pi.getId()))
            .executeAsync();

    batch2 =
        engineRule
            .getRuntimeService()
            .newMigration(migrationPlan)
            .processInstanceIds(Arrays.asList(pi.getId()))
            .executeAsync();
  }
  @Before
  public void setUp() {
    processEngineConfiguration = engineRule.getProcessEngineConfiguration();
    identityService = engineRule.getIdentityService();
    caseService = engineRule.getCaseService();
    historyService = engineRule.getHistoryService();

    testRule.deployForTenant(TENANT_ONE, CMMN_MODEL);

    caseInstanceId = createCaseInstance(null);

    caseExecutionId = getCaseExecution().getId();
  }
  /** Remove all batches and historic batches. Usually called in {@link org.junit.After} method. */
  public void removeAllRunningAndHistoricBatches() {
    HistoryService historyService = engineRule.getHistoryService();
    ManagementService managementService = engineRule.getManagementService();

    for (Batch batch : managementService.createBatchQuery().list()) {
      managementService.deleteBatch(batch.getId(), true);
    }

    // remove history of completed batches
    for (HistoricBatch historicBatch : historyService.createHistoricBatchQuery().list()) {
      historyService.deleteHistoricBatch(historicBatch.getId());
    }
  }
  /** Unnamed @Deployment annotations don't work with parameterized Unit tests */
  @Ignore
  @Test
  @Deployment
  public void ruleUsageExample() {
    RuntimeService runtimeService = engineRule.getRuntimeService();
    runtimeService.startProcessInstanceByKey("ruleUsage");

    TaskService taskService = engineRule.getTaskService();
    Task task = taskService.createTaskQuery().singleResult();
    assertEquals("My Task", task.getName());

    taskService.complete(task.getId());
    assertEquals(0, runtimeService.createProcessInstanceQuery().count());
  }
  @Test
  @Deployment(
      resources =
          "org/camunda/bpm/engine/test/standalone/testing/ProcessEngineRuleParameterizedJunit4Test.ruleUsageExample.bpmn20.xml")
  public void ruleUsageExampleWithNamedAnnotation() {
    RuntimeService runtimeService = engineRule.getRuntimeService();
    runtimeService.startProcessInstanceByKey("ruleUsage");

    TaskService taskService = engineRule.getTaskService();
    Task task = taskService.createTaskQuery().singleResult();
    assertEquals("My Task", task.getName());

    taskService.complete(task.getId());
    assertEquals(0, runtimeService.createProcessInstanceQuery().count());
  }
  @Before
  public void setUp() {
    decisionService = engineRule.getDecisionService();
    repositoryService = engineRule.getRepositoryService();
    historyService = engineRule.getHistoryService();
    identityService = engineRule.getIdentityService();

    testRule.deployForTenant(TENANT_ONE, DISH_DRG_DMN);

    decisionService
        .evaluateDecisionByKey(DISH_DECISION)
        .decisionDefinitionTenantId(TENANT_ONE)
        .variables(
            Variables.createVariables().putValue(TEMPERATURE, 21).putValue(DAY_TYPE, WEEKEND))
        .evaluate();
  }
 public HistoricBatch getHistoricBatch(Batch batch) {
   return engineRule
       .getHistoryService()
       .createHistoricBatchQuery()
       .batchId(batch.getId())
       .singleResult();
 }
 public long countTargetProcessInstances() {
   return engineRule
       .getRuntimeService()
       .createProcessInstanceQuery()
       .processDefinitionId(targetProcessDefinition.getId())
       .count();
 }
 public Batch createMigrationBatchWithSize(int batchSize) {
   int invocationsPerBatchJob =
       ((ProcessEngineConfigurationImpl)
               engineRule.getProcessEngine().getProcessEngineConfiguration())
           .getInvocationsPerBatchJob();
   return migrateProcessInstancesAsync(invocationsPerBatchJob * batchSize);
 }
 public List<Job> getJobsForDefinition(JobDefinition jobDefinition) {
   return engineRule
       .getManagementService()
       .createJobQuery()
       .jobDefinitionId(jobDefinition.getId())
       .list();
 }
 public void executeJob(Job job) {
   assertNotNull("Job to execute does not exist", job);
   try {
     engineRule.getManagementService().executeJob(job.getId());
   } catch (Exception e) {
     // ignore
   }
 }
 public JobDefinition getMonitorJobDefinition(Batch batch) {
   return engineRule
       .getManagementService()
       .createJobDefinitionQuery()
       .jobDefinitionId(batch.getMonitorJobDefinitionId())
       .jobType(BatchMonitorJobHandler.TYPE)
       .singleResult();
 }
 public JobDefinition getMigrationJobDefinition(Batch batch) {
   return engineRule
       .getManagementService()
       .createJobDefinitionQuery()
       .jobDefinitionId(batch.getBatchJobDefinitionId())
       .jobType(Batch.TYPE_PROCESS_INSTANCE_MIGRATION)
       .singleResult();
 }
 public List<HistoricJobLog> getHistoricBatchJobLog(Batch batch) {
   return engineRule
       .getHistoryService()
       .createHistoricJobLogQuery()
       .jobDefinitionId(batch.getBatchJobDefinitionId())
       .orderPartiallyByOccurrence()
       .asc()
       .list();
 }
  public void setRetries(Batch batch, int count, int retries) {
    List<Job> migrationJobs = getMigrationJobs(batch);
    assertTrue(migrationJobs.size() >= count);

    ManagementService managementService = engineRule.getManagementService();
    for (int i = 0; i < count; i++) {
      managementService.setJobRetries(migrationJobs.get(i).getId(), retries);
    }
  }
  @Test
  public void testQueryNoAuthorizations() {
    // when
    authRule.enableAuthorization("user");
    long count = engineRule.getHistoryService().createHistoricBatchQuery().count();
    authRule.disableAuthorization();

    // then
    Assert.assertEquals(0, count);
  }
  @Test
  @Deployment(resources = {"invokeRestService.bpmn"})
  public void shouldPackForWork() {
    Map<String, Object> variables = new HashMap<String, Object>();
    variables.put("date", "2014-01-02");

    RuntimeService runtimeService = processEngineRule.getRuntimeService();
    TaskService taskService = processEngineRule.getTaskService();

    runtimeService.startProcessInstanceByKey("holiday", variables);

    Task task = taskService.createTaskQuery().singleResult();
    Assert.assertNotNull(task);
    assertEquals("Pack for work", task.getName());

    boolean isHoliday =
        Boolean.parseBoolean(taskService.getVariable(task.getId(), "isHoliday").toString());
    Assert.assertFalse(isHoliday);
  }
 public Job getJobForDefinition(JobDefinition jobDefinition) {
   if (jobDefinition != null) {
     return engineRule
         .getManagementService()
         .createJobQuery()
         .jobDefinitionId(jobDefinition.getId())
         .singleResult();
   } else {
     return null;
   }
 }
  @Test
  public void testQueryListAccessAll() {
    // given
    authRule.createGrantAuthorization(Resources.BATCH, "*", "user", Permissions.READ_HISTORY);

    // when
    authRule.enableAuthorization("user");
    List<HistoricBatch> batches = engineRule.getHistoryService().createHistoricBatchQuery().list();
    authRule.disableAuthorization();

    // then
    Assert.assertEquals(2, batches.size());
  }
  @Test
  public void testQueryCount() {
    // given
    authRule.createGrantAuthorization(
        Resources.BATCH, batch1.getId(), "user", Permissions.READ_HISTORY);

    // when
    authRule.enableAuthorization("user");
    long count = engineRule.getHistoryService().createHistoricBatchQuery().count();
    authRule.disableAuthorization();

    // then
    Assert.assertEquals(1, count);
  }
  public Batch migrateProcessInstancesAsync(
      int numberOfProcessInstances,
      ProcessDefinition sourceProcessDefinition,
      ProcessDefinition targetProcessDefinition) {
    RuntimeService runtimeService = engineRule.getRuntimeService();

    List<String> processInstanceIds = new ArrayList<String>(numberOfProcessInstances);
    for (int i = 0; i < numberOfProcessInstances; i++) {
      processInstanceIds.add(
          runtimeService.startProcessInstanceById(sourceProcessDefinition.getId()).getId());
    }

    MigrationPlan migrationPlan =
        engineRule
            .getRuntimeService()
            .createMigrationPlan(sourceProcessDefinition.getId(), targetProcessDefinition.getId())
            .mapEqualActivities()
            .build();

    return runtimeService
        .newMigration(migrationPlan)
        .processInstanceIds(processInstanceIds)
        .executeAsync();
  }
  @Test
  public void testQueryDisabledTenantCheck() {
    DecisionRequirementsDefinition decisionRequirementsDefinition =
        repositoryService
            .createDecisionRequirementsDefinitionQuery()
            .tenantIdIn(TENANT_ONE)
            .singleResult();

    engineRule.getProcessEngineConfiguration().setTenantCheckEnabled(false);
    identityService.setAuthentication(USER_ID, null, null);

    HistoricDecisionInstanceStatisticsQuery query =
        historyService.createHistoricDecisionInstanceStatisticsQuery(
            decisionRequirementsDefinition.getId());

    assertThat(query.count(), is(3L));
  }
 @Before
 public void initServices() {
   repositoryService = rule.getRepositoryService();
 }
 @After
 public void deleteBatches() {
   engineRule.getManagementService().deleteBatch(batch1.getId(), true);
   engineRule.getManagementService().deleteBatch(batch2.getId(), true);
 }