public void testSpinAvailableInBpmn() {

    BpmnModelInstance bpmnModelInstance =
        Bpmn.createExecutableProcess("testProcess")
            .startEvent()
            .serviceTask()
            .camundaExpression(
                "${ execution.setVariable('customer', "
                    + "S(xmlVar).xPath('/customers/customer').element().toString()"
                    + ")}")
            .receiveTask("wait")
            .endEvent()
            .done();

    Deployment deployment =
        repositoryService
            .createDeployment()
            .addModelInstance("process.bpmn", bpmnModelInstance)
            .deploy();

    Map<String, Object> variables = new HashMap<String, Object>();
    variables.put("xmlVar", "<customers><customer /></customers>");
    ProcessInstance pi = runtimeService.startProcessInstanceByKey("testProcess", variables);

    String customerXml = (String) runtimeService.getVariable(pi.getId(), "customer");
    assertNotNull(customerXml);
    assertTrue(customerXml.contains("customer"));
    assertFalse(customerXml.contains("customers"));

    runtimeService.signal(pi.getId());

    repositoryService.deleteDeployment(deployment.getId(), true);
  }
  public void testProcessOnePreviousDeploymentWithPA() {
    // given

    MyEmbeddedProcessApplication application = new MyEmbeddedProcessApplication();

    // first deployment
    Deployment deployment1 =
        repositoryService
            .createDeployment(application.getReference())
            .name(DEPLOYMENT_NAME)
            .addModelInstance(BPMN_RESOURCE_1, createProcessWithServiceTask(PROCESS_KEY_1))
            .deploy();

    // second deployment
    Deployment deployment2 =
        repositoryService
            .createDeployment()
            .name(DEPLOYMENT_NAME)
            .addDeploymentResources(deployment1.getId())
            .deploy();

    verifyQueryResults(processDefinitionQueryByKey(PROCESS_KEY_1), 2);

    // when
    runtimeService.startProcessInstanceByKey(PROCESS_KEY_1);

    // then
    assertTrue(application.isCalled());

    deleteDeployments(deployment1, deployment2);
  }
  public void testCaseOnePreviousDeploymentWithPA() {
    // given

    MyEmbeddedProcessApplication application = new MyEmbeddedProcessApplication();

    // first deployment
    Deployment deployment1 =
        repositoryService
            .createDeployment(application.getReference())
            .name(DEPLOYMENT_NAME)
            .addClasspathResource(CMMN_RESOURCE_1)
            .deploy();

    // second deployment
    Deployment deployment2 =
        repositoryService
            .createDeployment()
            .name(DEPLOYMENT_NAME)
            .addDeploymentResources(deployment1.getId())
            .deploy();

    verifyQueryResults(caseDefinitionQueryByKey(CASE_KEY_1), 2);

    // when
    caseService.createCaseInstanceByKey(CASE_KEY_1);

    // then
    assertTrue(application.isCalled());

    deleteDeployments(deployment1, deployment2);
  }
  public void testDeploymentWithDelayedProcessDefinitionAndJobDefinitionActivation() {

    Date startTime = new Date();
    ClockUtil.setCurrentTime(startTime);
    Date inThreeDays = new Date(startTime.getTime() + (3 * 24 * 60 * 60 * 1000));

    // Deploy process, but activate after three days
    org.camunda.bpm.engine.repository.Deployment deployment =
        repositoryService
            .createDeployment()
            .addClasspathResource("org/camunda/bpm/engine/test/api/oneAsyncTask.bpmn")
            .activateProcessDefinitionsOn(inThreeDays)
            .deploy();

    assertEquals(1, repositoryService.createDeploymentQuery().count());

    assertEquals(1, repositoryService.createProcessDefinitionQuery().count());
    assertEquals(1, repositoryService.createProcessDefinitionQuery().suspended().count());
    assertEquals(0, repositoryService.createProcessDefinitionQuery().active().count());

    assertEquals(1, managementService.createJobDefinitionQuery().count());
    assertEquals(1, managementService.createJobDefinitionQuery().suspended().count());
    assertEquals(0, managementService.createJobDefinitionQuery().active().count());

    // Shouldn't be able to start a process instance
    try {
      runtimeService.startProcessInstanceByKey("oneTaskProcess");
      fail();
    } catch (ProcessEngineException e) {
      assertTextPresentIgnoreCase("suspended", e.getMessage());
    }

    Job job = managementService.createJobQuery().singleResult();
    managementService.executeJob(job.getId());

    assertEquals(1, repositoryService.createDeploymentQuery().count());

    assertEquals(1, repositoryService.createProcessDefinitionQuery().count());
    assertEquals(0, repositoryService.createProcessDefinitionQuery().suspended().count());
    assertEquals(1, repositoryService.createProcessDefinitionQuery().active().count());

    assertEquals(1, managementService.createJobDefinitionQuery().count());
    assertEquals(0, managementService.createJobDefinitionQuery().suspended().count());
    assertEquals(1, managementService.createJobDefinitionQuery().active().count());

    // Should be able to start process instance
    runtimeService.startProcessInstanceByKey("oneTaskProcess");
    assertEquals(1, runtimeService.createProcessInstanceQuery().count());

    // Cleanup
    repositoryService.deleteDeployment(deployment.getId(), true);
  }
  public void testCaseTwoPreviousDeploymentsWithDifferentPA() {
    // given

    // first deployment
    MyEmbeddedProcessApplication application1 = new MyEmbeddedProcessApplication();
    Deployment deployment1 =
        repositoryService
            .createDeployment(application1.getReference())
            .name(DEPLOYMENT_NAME)
            .addClasspathResource(CMMN_RESOURCE_1)
            .addClasspathResource(CMMN_RESOURCE_2)
            .deploy();

    // second deployment
    MyEmbeddedProcessApplication application2 = new MyEmbeddedProcessApplication();
    Deployment deployment2 =
        repositoryService
            .createDeployment(application2.getReference())
            .name(DEPLOYMENT_NAME)
            .addClasspathResource(CMMN_RESOURCE_1)
            .deploy();

    // third deployment
    Deployment deployment3 =
        repositoryService
            .createDeployment()
            .name(DEPLOYMENT_NAME)
            .addDeploymentResources(deployment1.getId())
            .deploy();

    verifyQueryResults(caseDefinitionQueryByKey(CASE_KEY_1), 3);
    verifyQueryResults(caseDefinitionQueryByKey(CASE_KEY_2), 2);

    // when (1)
    caseService.createCaseInstanceByKey(CASE_KEY_1);

    // then (1)
    assertFalse(application1.isCalled());
    assertTrue(application2.isCalled());

    // reset flag
    application2.setCalled(false);

    // when (2)
    caseService.createCaseInstanceByKey(CASE_KEY_2);

    // then (2)
    assertTrue(application1.isCalled());
    assertFalse(application2.isCalled());

    deleteDeployments(deployment1, deployment2, deployment3);
  }
  public void testProcessTwoPreviousDeploymentsWithDifferentPA() {
    // given

    // first deployment
    MyEmbeddedProcessApplication application1 = new MyEmbeddedProcessApplication();
    Deployment deployment1 =
        repositoryService
            .createDeployment(application1.getReference())
            .name(DEPLOYMENT_NAME)
            .addModelInstance(BPMN_RESOURCE_1, createProcessWithServiceTask(PROCESS_KEY_1))
            .addModelInstance(BPMN_RESOURCE_2, createProcessWithServiceTask(PROCESS_KEY_2))
            .deploy();

    // second deployment
    MyEmbeddedProcessApplication application2 = new MyEmbeddedProcessApplication();
    Deployment deployment2 =
        repositoryService
            .createDeployment(application2.getReference())
            .name(DEPLOYMENT_NAME)
            .addModelInstance(BPMN_RESOURCE_1, createProcessWithServiceTask(PROCESS_KEY_1))
            .deploy();

    // third deployment
    Deployment deployment3 =
        repositoryService
            .createDeployment()
            .name(DEPLOYMENT_NAME)
            .addDeploymentResources(deployment1.getId())
            .deploy();

    verifyQueryResults(processDefinitionQueryByKey(PROCESS_KEY_1), 3);
    verifyQueryResults(processDefinitionQueryByKey(PROCESS_KEY_2), 2);

    // when (1)
    runtimeService.startProcessInstanceByKey(PROCESS_KEY_1);

    // then (1)
    assertFalse(application1.isCalled());
    assertTrue(application2.isCalled());

    // reset flag
    application2.setCalled(false);

    // when (2)
    runtimeService.startProcessInstanceByKey(PROCESS_KEY_2);

    // then (2)
    assertTrue(application1.isCalled());
    assertFalse(application2.isCalled());

    deleteDeployments(deployment1, deployment2, deployment3);
  }
  @Deployment(resources = {"org/camunda/bpm/engine/test/api/oneTaskProcess.bpmn20.xml"})
  public void testGetResourceAsStreamUnexistingResourceInExistingDeployment() {
    // Get hold of the deployment id
    org.camunda.bpm.engine.repository.Deployment deployment =
        repositoryService.createDeploymentQuery().singleResult();

    try {
      repositoryService.getResourceAsStream(
          deployment.getId(), "org/camunda/bpm/engine/test/api/unexistingProcess.bpmn.xml");
      fail("ProcessEngineException expected");
    } catch (ProcessEngineException ae) {
      assertTextPresent("no resource found with name", ae.getMessage());
    }
  }
  public void testDeploymentSourceProperty() {
    registerProcessEngine();

    TestApplicationWithResources processApplication = new TestApplicationWithResources();
    processApplication.deploy();

    Deployment deployment = repositoryService.createDeploymentQuery().singleResult();

    assertNotNull(deployment);
    assertEquals(
        ProcessApplicationDeployment.PROCESS_APPLICATION_DEPLOYMENT_SOURCE, deployment.getSource());

    processApplication.undeploy();
  }
  public void testSuspendMultipleProcessDefinitionsByKey() {

    // Deploy three processes
    int nrOfProcessDefinitions = 3;
    for (int i = 0; i < nrOfProcessDefinitions; i++) {
      repositoryService
          .createDeployment()
          .addClasspathResource("org/camunda/bpm/engine/test/api/runtime/oneTaskProcess.bpmn20.xml")
          .deploy();
    }
    assertEquals(nrOfProcessDefinitions, repositoryService.createProcessDefinitionQuery().count());
    assertEquals(
        nrOfProcessDefinitions, repositoryService.createProcessDefinitionQuery().active().count());
    assertEquals(0, repositoryService.createProcessDefinitionQuery().suspended().count());

    // Suspend all process definitions with same key
    repositoryService.suspendProcessDefinitionByKey("oneTaskProcess");
    assertEquals(nrOfProcessDefinitions, repositoryService.createProcessDefinitionQuery().count());
    assertEquals(0, repositoryService.createProcessDefinitionQuery().active().count());
    assertEquals(
        nrOfProcessDefinitions,
        repositoryService.createProcessDefinitionQuery().suspended().count());

    // Activate again
    repositoryService.activateProcessDefinitionByKey("oneTaskProcess");
    assertEquals(nrOfProcessDefinitions, repositoryService.createProcessDefinitionQuery().count());
    assertEquals(
        nrOfProcessDefinitions, repositoryService.createProcessDefinitionQuery().active().count());
    assertEquals(0, repositoryService.createProcessDefinitionQuery().suspended().count());

    // Start process instance
    runtimeService.startProcessInstanceByKey("oneTaskProcess");

    // And suspend again, cascading to process instances
    repositoryService.suspendProcessDefinitionByKey("oneTaskProcess", true, null);
    assertEquals(nrOfProcessDefinitions, repositoryService.createProcessDefinitionQuery().count());
    assertEquals(0, repositoryService.createProcessDefinitionQuery().active().count());
    assertEquals(
        nrOfProcessDefinitions,
        repositoryService.createProcessDefinitionQuery().suspended().count());
    assertEquals(1, runtimeService.createProcessInstanceQuery().suspended().count());
    assertEquals(0, runtimeService.createProcessInstanceQuery().active().count());
    assertEquals(1, runtimeService.createProcessInstanceQuery().count());

    // Clean DB
    for (org.camunda.bpm.engine.repository.Deployment deployment :
        repositoryService.createDeploymentQuery().list()) {
      repositoryService.deleteDeployment(deployment.getId(), true);
    }
  }
  public void testDeployAppWithResources() {
    registerProcessEngine();

    TestApplicationWithResources processApplication = new TestApplicationWithResources();
    processApplication.deploy();

    Deployment deployment = repositoryService.createDeploymentQuery().singleResult();

    assertNotNull(deployment);

    List<Resource> deploymentResources =
        repositoryService.getDeploymentResources(deployment.getId());
    assertEquals(4, deploymentResources.size());

    processApplication.undeploy();
    assertEquals(0, repositoryService.createDeploymentQuery().count());
  }
  public void testUnregisterProcessApplicationOnDeploymentDeletion() {
    // given a deployment with a process application registration
    Deployment deployment =
        repositoryService
            .createDeployment()
            .addModelInstance("process.bpmn", Bpmn.createExecutableProcess("foo").done())
            .deploy();

    // and a process application registration
    managementService.registerProcessApplication(
        deployment.getId(), processApplication.getReference());

    // when deleting the deploymen
    repositoryService.deleteDeployment(deployment.getId(), true);

    // then the registration is removed
    assertNull(managementService.getProcessApplicationForDeployment(deployment.getId()));
  }
  public void testCaseTwoPreviousDeploymentUnregisterSecondPA() {
    // given

    // first deployment
    MyEmbeddedProcessApplication application1 = new MyEmbeddedProcessApplication();
    Deployment deployment1 =
        repositoryService
            .createDeployment(application1.getReference())
            .name(DEPLOYMENT_NAME)
            .addClasspathResource(CMMN_RESOURCE_1)
            .deploy();

    // second deployment
    MyEmbeddedProcessApplication application2 = new MyEmbeddedProcessApplication();
    Deployment deployment2 =
        repositoryService
            .createDeployment(application2.getReference())
            .name(DEPLOYMENT_NAME)
            .addClasspathResource(CMMN_RESOURCE_1)
            .deploy();

    // second deployment
    Deployment deployment3 =
        repositoryService
            .createDeployment()
            .name(DEPLOYMENT_NAME)
            .addDeploymentResources(deployment1.getId())
            .deploy();

    verifyQueryResults(caseDefinitionQueryByKey(CASE_KEY_1), 3);

    // when
    managementService.unregisterProcessApplication(deployment2.getId(), true);
    caseService.createCaseInstanceByKey(CASE_KEY_1);

    // then
    assertTrue(application1.isCalled());
    assertFalse(application2.isCalled());

    deleteDeployments(deployment1, deployment2, deployment3);
  }
  public void testMultipleActivationByProcessDefinitionKey_shouldActivateJob() {
    // given
    String key = "suspensionProcess";

    // Deploy three processes and start for each deployment a process instance
    // with a failed job
    int nrOfProcessDefinitions = 3;
    for (int i = 0; i < nrOfProcessDefinitions; i++) {
      repositoryService
          .createDeployment()
          .addClasspathResource("org/camunda/bpm/engine/test/api/mgmt/SuspensionTest.testBase.bpmn")
          .deploy();
      Map<String, Object> params = new HashMap<String, Object>();
      params.put("fail", Boolean.TRUE);
      runtimeService.startProcessInstanceByKey(key, params);
    }

    // suspended job definitions and corresponding jobs
    managementService.suspendJobDefinitionByProcessDefinitionKey(key, true);

    // when
    // the job will be suspended
    managementService.activateJobByProcessDefinitionKey(key);

    // then
    // the job should be activated
    JobQuery jobQuery = managementService.createJobQuery();

    assertEquals(0, jobQuery.suspended().count());
    assertEquals(3, jobQuery.active().count());

    // Clean DB
    for (org.camunda.bpm.engine.repository.Deployment deployment :
        repositoryService.createDeploymentQuery().list()) {
      repositoryService.deleteDeployment(deployment.getId(), true);
    }
  }
 /** Deletes the deployments cascading. */
 private void deleteDeployments(Deployment... deployments) {
   for (Deployment deployment : deployments) {
     repositoryService.deleteDeployment(deployment.getId(), true);
   }
 }
  public void testDelayedSuspendMultipleProcessDefinitionsByKey() {

    Date startTime = new Date();
    ClockUtil.setCurrentTime(startTime);
    final long hourInMs = 60 * 60 * 1000;

    // Deploy five versions of the same process
    int nrOfProcessDefinitions = 5;
    for (int i = 0; i < nrOfProcessDefinitions; i++) {
      repositoryService
          .createDeployment()
          .addClasspathResource("org/camunda/bpm/engine/test/api/runtime/oneTaskProcess.bpmn20.xml")
          .deploy();
    }
    assertEquals(nrOfProcessDefinitions, repositoryService.createProcessDefinitionQuery().count());
    assertEquals(
        nrOfProcessDefinitions, repositoryService.createProcessDefinitionQuery().active().count());
    assertEquals(0, repositoryService.createProcessDefinitionQuery().suspended().count());

    // Start process instance
    runtimeService.startProcessInstanceByKey("oneTaskProcess");

    // Suspend all process definitions with same key in 2 hourse from now
    repositoryService.suspendProcessDefinitionByKey(
        "oneTaskProcess", true, new Date(startTime.getTime() + (2 * hourInMs)));
    assertEquals(nrOfProcessDefinitions, repositoryService.createProcessDefinitionQuery().count());
    assertEquals(
        nrOfProcessDefinitions, repositoryService.createProcessDefinitionQuery().active().count());
    assertEquals(0, repositoryService.createProcessDefinitionQuery().suspended().count());
    assertEquals(1, runtimeService.createProcessInstanceQuery().active().count());

    // Move time 3 hours and run job executor
    ClockUtil.setCurrentTime(new Date(startTime.getTime() + (3 * hourInMs)));
    waitForJobExecutorToProcessAllJobs(5000L);
    assertEquals(nrOfProcessDefinitions, repositoryService.createProcessDefinitionQuery().count());
    assertEquals(0, repositoryService.createProcessDefinitionQuery().active().count());
    assertEquals(
        nrOfProcessDefinitions,
        repositoryService.createProcessDefinitionQuery().suspended().count());
    assertEquals(1, runtimeService.createProcessInstanceQuery().suspended().count());

    // Activate again in 5 hourse from now
    repositoryService.activateProcessDefinitionByKey(
        "oneTaskProcess", true, new Date(startTime.getTime() + (5 * hourInMs)));
    assertEquals(nrOfProcessDefinitions, repositoryService.createProcessDefinitionQuery().count());
    assertEquals(0, repositoryService.createProcessDefinitionQuery().active().count());
    assertEquals(
        nrOfProcessDefinitions,
        repositoryService.createProcessDefinitionQuery().suspended().count());
    assertEquals(1, runtimeService.createProcessInstanceQuery().suspended().count());

    // Move time 6 hours and run job executor
    ClockUtil.setCurrentTime(new Date(startTime.getTime() + (6 * hourInMs)));
    waitForJobExecutorToProcessAllJobs(5000L);
    assertEquals(nrOfProcessDefinitions, repositoryService.createProcessDefinitionQuery().count());
    assertEquals(
        nrOfProcessDefinitions, repositoryService.createProcessDefinitionQuery().active().count());
    assertEquals(0, repositoryService.createProcessDefinitionQuery().suspended().count());
    assertEquals(1, runtimeService.createProcessInstanceQuery().active().count());

    // Clean DB
    for (org.camunda.bpm.engine.repository.Deployment deployment :
        repositoryService.createDeploymentQuery().list()) {
      repositoryService.deleteDeployment(deployment.getId(), true);
    }
  }