@Post
  public ProcessInstanceResponse createProcessInstance(ProcessInstanceCreateRequest request) {

    if (request.getProcessDefinitionId() == null
        && request.getProcessDefinitionKey() == null
        && request.getMessage() == null) {
      throw new ActivitiIllegalArgumentException(
          "Either processDefinitionId, processDefinitionKey or message is required.");
    }

    int paramsSet =
        ((request.getProcessDefinitionId() != null) ? 1 : 0)
            + ((request.getProcessDefinitionKey() != null) ? 1 : 0)
            + ((request.getMessage() != null) ? 1 : 0);

    if (paramsSet > 1) {
      throw new ActivitiIllegalArgumentException(
          "Only one of processDefinitionId, processDefinitionKey or message should be set.");
    }

    RestResponseFactory factory =
        getApplication(ActivitiRestServicesApplication.class).getRestResponseFactory();

    Map<String, Object> startVariables = null;
    if (request.getVariables() != null) {
      startVariables = new HashMap<String, Object>();
      for (RestVariable variable : request.getVariables()) {
        startVariables.put(variable.getName(), factory.getVariableValue(variable));
      }
    }

    // Actually start the instance based on key or id
    try {
      ProcessInstance instance = null;
      if (request.getProcessDefinitionId() != null) {
        instance =
            ActivitiUtil.getRuntimeService()
                .startProcessInstanceById(
                    request.getProcessDefinitionId(), request.getBusinessKey(), startVariables);
      } else if (request.getProcessDefinitionKey() != null) {
        instance =
            ActivitiUtil.getRuntimeService()
                .startProcessInstanceByKey(
                    request.getProcessDefinitionKey(), request.getBusinessKey(), startVariables);
      } else {
        instance =
            ActivitiUtil.getRuntimeService()
                .startProcessInstanceByMessage(
                    request.getMessage(), request.getBusinessKey(), startVariables);
      }

      setStatus(Status.SUCCESS_CREATED);
      return factory.createProcessInstanceResponse(this, instance);
    } catch (ActivitiObjectNotFoundException aonfe) {
      throw new ActivitiIllegalArgumentException(aonfe.getMessage(), aonfe);
    }
  }
 public void testDeleteDeploymentCascadeNonExistentDeploymentId() {
   try {
     repositoryService.deleteDeployment("foobar", true);
     fail("ActivitiException expected");
   } catch (ActivitiObjectNotFoundException ae) {
     assertTextPresent("Could not find a deployment with id 'foobar'.", ae.getMessage());
   } catch (Throwable t) {
     fail("Unexpected exception: " + t);
   }
 }
  @Deployment(resources = {"org/activiti/engine/test/api/oneTaskProcess.bpmn20.xml"})
  public void testGetResourceAsStreamUnexistingDeployment() {

    try {
      repositoryService.getResourceAsStream(
          "unexistingdeployment", "org/activiti/engine/test/api/unexistingProcess.bpmn.xml");
      fail("ActivitiException expected");
    } catch (ActivitiObjectNotFoundException ae) {
      assertTextPresent("deployment does not exist", ae.getMessage());
      assertEquals(org.activiti.engine.repository.Deployment.class, ae.getObjectClass());
    }
  }
  @Deployment(resources = {"org/activiti/engine/test/api/oneTaskProcess.bpmn20.xml"})
  public void testGetResourceAsStreamUnexistingResourceInExistingDeployment() {
    // Get hold of the deployment id
    org.activiti.engine.repository.Deployment deployment =
        repositoryService.createDeploymentQuery().singleResult();

    try {
      repositoryService.getResourceAsStream(
          deployment.getId(), "org/activiti/engine/test/api/unexistingProcess.bpmn.xml");
      fail("ActivitiException expected");
    } catch (ActivitiObjectNotFoundException ae) {
      assertTextPresent("no resource found with name", ae.getMessage());
      assertEquals(InputStream.class, ae.getObjectClass());
    }
  }