@Deployment
  public void testParseNamespaceInConditionExpressionType() {
    CommandExecutor commandExecutor = processEngineConfiguration.getCommandExecutorTxRequired();
    ProcessDefinitionEntity processDefinitionEntity =
        commandExecutor.execute(
            new Command<ProcessDefinitionEntity>() {
              public ProcessDefinitionEntity execute(CommandContext commandContext) {
                return Context.getProcessEngineConfiguration()
                    .getDeploymentCache()
                    .findDeployedLatestProcessDefinitionByKey("resolvableNamespacesProcess");
              }
            });

    // Test that the process definition has been deployed
    assertNotNull(processDefinitionEntity);
    ActivityImpl activity = processDefinitionEntity.findActivity("ExclusiveGateway_1");
    assertNotNull(activity);

    // Test that the conditions has been resolved
    for (PvmTransition transition : activity.getOutgoingTransitions()) {
      if (transition.getDestination().getId().equals("Task_2")) {
        assertTrue(transition.getProperty("conditionText").equals("#{approved}"));
      } else if (transition.getDestination().getId().equals("Task_3")) {
        assertTrue(transition.getProperty("conditionText").equals("#{!approved}"));
      } else {
        fail("Something went wrong");
      }
    }
  }
  @Deployment(resources = {"org/camunda/bpm/engine/test/api/mgmt/timerOnTask.bpmn20.xml"})
  public void testDeleteJobThatWasAlreadyAcquired() {
    ClockUtil.setCurrentTime(new Date());

    ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("timerOnTask");
    Job timerJob =
        managementService
            .createJobQuery()
            .processInstanceId(processInstance.getId())
            .singleResult();

    // We need to move time at least one hour to make the timer executable
    ClockUtil.setCurrentTime(new Date(ClockUtil.getCurrentTime().getTime() + 7200000L));

    // Acquire job by running the acquire command manually
    ProcessEngineImpl processEngineImpl = (ProcessEngineImpl) processEngine;
    JobExecutor jobExecutor = processEngineImpl.getProcessEngineConfiguration().getJobExecutor();
    AcquireJobsCmd acquireJobsCmd = new AcquireJobsCmd(jobExecutor);
    CommandExecutor commandExecutor =
        processEngineImpl.getProcessEngineConfiguration().getCommandExecutorTxRequired();
    commandExecutor.execute(acquireJobsCmd);

    // Try to delete the job. This should fail.
    try {
      managementService.deleteJob(timerJob.getId());
      fail();
    } catch (ProcessEngineException e) {
      // Exception is expected
    }

    // Clean up
    managementService.executeJob(timerJob.getId());
  }
  protected void deleteJobAndIncidents(final Job job) {
    final List<HistoricIncident> incidents =
        historyService
            .createHistoricIncidentQuery()
            .incidentType(FailedJobIncidentHandler.INCIDENT_HANDLER_TYPE)
            .list();

    CommandExecutor commandExecutor = processEngineConfiguration.getCommandExecutorTxRequired();
    commandExecutor.execute(
        new Command<Void>() {
          public Void execute(CommandContext commandContext) {
            ((JobEntity) job).delete();

            HistoricIncidentManager historicIncidentManager =
                commandContext.getHistoricIncidentManager();
            for (HistoricIncident incident : incidents) {
              HistoricIncidentEntity incidentEntity = (HistoricIncidentEntity) incident;
              historicIncidentManager.delete(incidentEntity);
            }

            commandContext.getHistoricJobLogManager().deleteHistoricJobLogByJobId(job.getId());
            return null;
          }
        });
  }
 public void tearDown() throws Exception {
   CommandExecutor commandExecutor = processEngineConfiguration.getCommandExecutorTxRequired();
   commandExecutor.execute(
       new Command<Object>() {
         public Object execute(CommandContext commandContext) {
           commandContext
               .getHistoricJobLogManager()
               .deleteHistoricJobLogsByHandlerType(TimerActivateProcessDefinitionHandler.TYPE);
           return null;
         }
       });
 }
  protected void createJob(final int retries, final String owner, final Date lockExpirationTime) {
    CommandExecutor commandExecutor = processEngineConfiguration.getCommandExecutorTxRequired();
    commandExecutor.execute(
        new Command<Void>() {
          public Void execute(CommandContext commandContext) {
            JobManager jobManager = commandContext.getJobManager();
            MessageEntity job = new MessageEntity();
            job.setJobHandlerType("any");
            job.setLockOwner(owner);
            job.setLockExpirationTime(lockExpirationTime);
            job.setRetries(retries);

            jobManager.send(job);
            return null;
          }
        });
  }
  public static ExecutionTree forExecution(final String executionId, ProcessEngine processEngine) {
    ProcessEngineConfigurationImpl configuration =
        (ProcessEngineConfigurationImpl) processEngine.getProcessEngineConfiguration();

    CommandExecutor commandExecutor = configuration.getCommandExecutorTxRequired();

    ExecutionTree executionTree =
        commandExecutor.execute(
            new Command<ExecutionTree>() {
              public ExecutionTree execute(CommandContext commandContext) {
                ExecutionEntity execution =
                    commandContext.getExecutionManager().findExecutionById(executionId);
                return ExecutionTree.forExecution(execution);
              }
            });

    return executionTree;
  }
  @Deployment(
      resources = {
        "org/camunda/bpm/engine/test/api/mgmt/ManagementServiceTest.testGetJobExceptionStacktrace.bpmn20.xml"
      })
  public void testSetJobRetriesByDefinitionUnlocksInconsistentJobs() {
    // given a job definition
    final JobDefinition jobDefinition = managementService.createJobDefinitionQuery().singleResult();

    // and an inconsistent job that is never again picked up by a job executor
    CommandExecutor commandExecutor = processEngineConfiguration.getCommandExecutorTxRequired();
    commandExecutor.execute(
        new Command<Void>() {
          public Void execute(CommandContext commandContext) {
            JobManager jobManager = commandContext.getJobManager();
            MessageEntity job = new MessageEntity();
            job.setJobDefinitionId(jobDefinition.getId());
            job.setJobHandlerType("any");
            job.setLockOwner("owner");
            job.setLockExpirationTime(ClockUtil.getCurrentTime());
            job.setRetries(0);

            jobManager.send(job);
            return null;
          }
        });

    // when the job retries are reset
    managementService.setJobRetriesByJobDefinitionId(jobDefinition.getId(), 3);

    // then the job can be picked up again
    JobEntity job = (JobEntity) managementService.createJobQuery().singleResult();
    assertNotNull(job);
    assertNull(job.getLockOwner());
    assertNull(job.getLockExpirationTime());
    assertEquals(3, job.getRetries());

    deleteJobAndIncidents(job);
  }
  @Deployment
  public void testParseDiagramInterchangeElements() {

    // Graphical information is not yet exposed publicly, so we need to do some plumbing
    CommandExecutor commandExecutor = processEngineConfiguration.getCommandExecutorTxRequired();
    ProcessDefinitionEntity processDefinitionEntity =
        commandExecutor.execute(
            new Command<ProcessDefinitionEntity>() {
              public ProcessDefinitionEntity execute(CommandContext commandContext) {
                return Context.getProcessEngineConfiguration()
                    .getDeploymentCache()
                    .findDeployedLatestProcessDefinitionByKey("myProcess");
              }
            });

    assertNotNull(processDefinitionEntity);
    assertEquals(7, processDefinitionEntity.getActivities().size());

    // Check if diagram has been created based on Diagram Interchange when it's not a headless
    // instance
    List<String> resourceNames =
        repositoryService.getDeploymentResourceNames(processDefinitionEntity.getDeploymentId());
    if (processEngineConfiguration.isCreateDiagramOnDeploy()) {
      assertEquals(2, resourceNames.size());
    } else {
      assertEquals(1, resourceNames.size());
    }

    for (ActivityImpl activity : processDefinitionEntity.getActivities()) {

      if (activity.getId().equals("theStart")) {
        assertActivityBounds(activity, 70, 255, 30, 30);
      } else if (activity.getId().equals("task1")) {
        assertActivityBounds(activity, 176, 230, 100, 80);
      } else if (activity.getId().equals("gateway1")) {
        assertActivityBounds(activity, 340, 250, 40, 40);
      } else if (activity.getId().equals("task2")) {
        assertActivityBounds(activity, 445, 138, 100, 80);
      } else if (activity.getId().equals("gateway2")) {
        assertActivityBounds(activity, 620, 250, 40, 40);
      } else if (activity.getId().equals("task3")) {
        assertActivityBounds(activity, 453, 304, 100, 80);
      } else if (activity.getId().equals("theEnd")) {
        assertActivityBounds(activity, 713, 256, 28, 28);
      }

      for (PvmTransition sequenceFlow : activity.getOutgoingTransitions()) {
        assertTrue(((TransitionImpl) sequenceFlow).getWaypoints().size() >= 4);

        TransitionImpl transitionImpl = (TransitionImpl) sequenceFlow;
        if (transitionImpl.getId().equals("flowStartToTask1")) {
          assertSequenceFlowWayPoints(transitionImpl, 100, 270, 176, 270);
        } else if (transitionImpl.getId().equals("flowTask1ToGateway1")) {
          assertSequenceFlowWayPoints(transitionImpl, 276, 270, 340, 270);
        } else if (transitionImpl.getId().equals("flowGateway1ToTask2")) {
          assertSequenceFlowWayPoints(transitionImpl, 360, 250, 360, 178, 445, 178);
        } else if (transitionImpl.getId().equals("flowGateway1ToTask3")) {
          assertSequenceFlowWayPoints(transitionImpl, 360, 290, 360, 344, 453, 344);
        } else if (transitionImpl.getId().equals("flowTask2ToGateway2")) {
          assertSequenceFlowWayPoints(transitionImpl, 545, 178, 640, 178, 640, 250);
        } else if (transitionImpl.getId().equals("flowTask3ToGateway2")) {
          assertSequenceFlowWayPoints(transitionImpl, 553, 344, 640, 344, 640, 290);
        } else if (transitionImpl.getId().equals("flowGateway2ToEnd")) {
          assertSequenceFlowWayPoints(transitionImpl, 660, 270, 713, 270);
        }
      }
    }
  }