public void testStringVariableQuery() {
    ProcessDefinition processDefinition =
        ProcessDefinition.parseXmlString(
            "<process-definition name='variables'>"
                + "  <start-state name='start'/>"
                + "</process-definition>");
    deployProcessDefinition(processDefinition);

    ProcessInstance one = jbpmContext.newProcessInstanceForUpdate("variables");
    one.getContextInstance().setVariable("category", "overpaid");
    one.getContextInstance().setVariable("duedate", "tomorrow");

    ProcessInstance two = jbpmContext.newProcessInstanceForUpdate("variables");
    two.getContextInstance().setVariable("category", "overpaid");
    two.getContextInstance().setVariable("duedate", "yesterday");

    ProcessInstance three = jbpmContext.newProcessInstanceForUpdate("variables");
    three.getContextInstance().setVariable("category", "underpaid");
    three.getContextInstance().setVariable("duedate", "today");

    newTransaction();

    Set expectedPids = new HashSet();
    expectedPids.add(new Long(one.getId()));
    expectedPids.add(new Long(two.getId()));

    Query query =
        session.createQuery(
            "select pi.id "
                + "from org.jbpm.context.exe.variableinstance.StringInstance si "
                + "join si.processInstance pi "
                + "where si.name = 'category'"
                + "  and si.value like 'overpaid'");
    Set retrievedPids = new HashSet(query.list());

    assertEquals(expectedPids, retrievedPids);

    newTransaction();

    expectedPids.clear();
    expectedPids.add(new Long(three.getId()));

    query =
        session.createQuery(
            "select pi.id "
                + "from org.jbpm.context.exe.variableinstance.StringInstance si "
                + "join si.processInstance pi "
                + "where si.name = 'category'"
                + "  and si.value like 'underpaid'");
    retrievedPids.clear();
    retrievedPids.addAll(query.list());

    assertEquals(expectedPids, retrievedPids);
  }
  public void testTimerWithSuperState() {
    ProcessDefinition processDefinition =
        ProcessDefinition.parseXmlString(
            "<process-definition name='jbpm2812'>"
                + "  <start-state name='start'>"
                + "    <transition to='super-state/state1'/>"
                + "  </start-state>"
                + "  <super-state name='super-state'>"
                + "    <state name='state1'>"
                + "      <timer duedate='1 second' name='timeout-timer' transition='timeout'/>"
                + "      <transition to='state2' name='go'/>"
                + "    </state>"
                + "    <state name='state2'>"
                + "  	 <transition to='end' name='go'/>"
                + "    </state>"
                + "    <transition to='timed-out-end' name='timeout'/>"
                + "  </super-state>"
                + "  <end-state name='timed-out-end'/>"
                + "  <end-state name='end'/>"
                + "</process-definition>");
    deployProcessDefinition(processDefinition);

    ProcessInstance processInstance = new ProcessInstance(processDefinition);
    processInstance.signal();
    assertEquals("state1", processInstance.getRootToken().getNode().getName());

    processJobs();
    processInstance = jbpmContext.loadProcessInstance(processInstance.getId());
    assertTrue("expected " + processInstance + " to have ended", processInstance.hasEnded());
    assertEquals("timed-out-end", processInstance.getRootToken().getNode().getName());
  }
  public void testEnactReportWorkflow() {
    long processId = 5;
    StudyParticipantAssignment assignment = Fixtures.createAssignment();
    ExpeditedAdverseEventReport aeReport = Fixtures.createSavableExpeditedReport();
    Report report = Fixtures.createReport("testReport");
    aeReport.setId(55);
    AdverseEventReportingPeriod reportingPeriod = Fixtures.createReportingPeriod();
    WorkflowConfig workflowConfig = Fixtures.createWorkflowConfig("test");
    StudySite site = assignment.getStudySite();
    StudySiteWorkflowConfig ssWfCfg = new StudySiteWorkflowConfig("report", site, workflowConfig);
    site.addStudySiteWorkflowConfig(ssWfCfg);
    reportingPeriod.addAeReport(aeReport);
    aeReport.setAssignment(assignment);
    aeReport.addReport(report);

    Map<String, Object> variables = new HashMap<String, Object>();
    variables.put(WorkflowService.VAR_STUDY_ID, site.getStudy().getId());
    variables.put(WorkflowService.VAR_WF_TYPE, Report.class.getName());
    variables.put(WorkflowService.VAR_REPORT_ID, report.getId());
    variables.put(WorkflowService.VAR_EXPEDITED_REPORT_ID, aeReport.getId());
    variables.put(WorkflowService.VAR_WF_STUDY_NAME, reportingPeriod.getStudy().getDisplayName());
    variables.put(
        WorkflowService.VAR_WF_SUBJECT_NAME, reportingPeriod.getParticipant().getFullName());
    variables.put(WorkflowService.VAR_WF_COURSE_NAME, reportingPeriod.getName());

    EasyMock.expect(wfService.createProcessInstance("test", variables)).andReturn(processInstance);
    EasyMock.expect(processInstance.getId()).andReturn(processId).anyTimes();
    reportDao.save(report);
    replayMocks();
    impl.enactReportWorkflow(report);
    verifyMocks();
  }
  public void testEnactReportingPeriodWorkflow() {
    long processId = 5;
    StudyParticipantAssignment assignment = Fixtures.createAssignment();
    AdverseEventReportingPeriod reportingPeriod = Fixtures.createReportingPeriod();
    reportingPeriod.setId(44);
    WorkflowConfig workflowConfig = Fixtures.createWorkflowConfig("test");
    StudySite site = assignment.getStudySite();
    StudySiteWorkflowConfig ssWfCfg =
        new StudySiteWorkflowConfig("reportingPeriod", site, workflowConfig);
    site.addStudySiteWorkflowConfig(ssWfCfg);
    reportingPeriod.setAssignment(assignment);

    Map<String, Object> variables = new HashMap<String, Object>();
    variables.put(WorkflowService.VAR_STUDY_ID, site.getStudy().getId());
    variables.put(WorkflowService.VAR_WF_TYPE, AdverseEventReportingPeriod.class.getName());
    variables.put(WorkflowService.VAR_WF_STUDY_NAME, reportingPeriod.getStudy().getDisplayName());
    variables.put(
        WorkflowService.VAR_WF_SUBJECT_NAME, reportingPeriod.getParticipant().getFullName());
    variables.put(WorkflowService.VAR_WF_COURSE_NAME, reportingPeriod.getName());
    variables.put(WorkflowService.VAR_REPORTING_PERIOD_ID, reportingPeriod.getId());

    EasyMock.expect(wfService.createProcessInstance("test", variables)).andReturn(processInstance);
    EasyMock.expect(processInstance.getId()).andReturn(processId).anyTimes();
    rpDao.modifyOrSaveReviewStatusAndComments(reportingPeriod);
    replayMocks();
    impl.enactReportingPeriodWorkflow(reportingPeriod);
    verifyMocks();
  }
 public void testFindProcessInstance() {
   ProcessInstance pi = jbpmService.getProcessInstance(5L);
   System.out.println(pi.getId());
   assertEquals(5L, pi.getId());
 }
  public void testTimerInCombinationWithAsyncNode() throws Throwable {
    ProcessDefinition subDefinition =
        ProcessDefinition.parseXmlString(
            "<process-definition name='sub'>"
                + "  <start-state name='start'>"
                + "    <transition to='decision'/>"
                + "  </start-state>"
                + "  <decision name='decision'>"
                + "    <handler class='org.jbpm.scenarios.AsyncTimerAndSubProcessDbTest$ToTimedDecisionHandler' />"
                + "    <transition name='default' to='task' />"
                + "  </decision>"
                + "  <task-node name='task'>"
                + "    <task name='do stuff'>"
                + "      <controller>"
                + "        <variable name='a' access='read' />"
                + "      </controller>"
                + "      <assignment actor-id='victim' />"
                + "    </task>"
                + "    <transition to='end'/>"
                + "  </task-node>"
                + "  <end-state name='end' />"
                + "</process-definition>");
    jbpmContext.deployProcessDefinition(subDefinition);
    newTransaction();

    ProcessDefinition superDefinition =
        ProcessDefinition.parseXmlString(
            "<process-definition name='super'>"
                + "  <start-state name='start'>"
                + "    <transition to='decision'/>"
                + "  </start-state>"
                + "  <decision name='decision'>"
                + "    <handler class='org.jbpm.scenarios.AsyncTimerAndSubProcessDbTest$ToTimedDecisionHandler' />"
                + "    <transition name='default' to='timed' />"
                + "  </decision>"
                + "  <state name='timed'>"
                + "    <timer name='reminder' "
                + "           duedate='0 seconds' "
                + "           transition='timer fires' />"
                + "    <transition name='timer fires' to='async'/>"
                + "    <transition name='normal continuation' to='end'/>"
                + "  </state>"
                + "  <node name='async' async='true'>"
                + "    <transition to='subprocess'/>"
                + "  </node>"
                + "  <process-state name='subprocess'>"
                + "    <sub-process name='sub' />"
                + "    <variable name='a'/>"
                + "    <variable name='b'/>"
                + "    <transition to='decision' />"
                + "  </process-state>"
                + "  <end-state name='end' />"
                + "</process-definition>");
    jbpmContext.deployProcessDefinition(superDefinition);
    newTransaction();

    ProcessInstance superInstance = jbpmContext.newProcessInstanceForUpdate("super");
    ContextInstance superContext = superInstance.getContextInstance();
    superContext.setVariable("a", "value a");
    superContext.setVariable("b", "value b");
    superInstance.signal();

    processJobs(5000);

    superInstance = jbpmContext.loadProcessInstance(superInstance.getId());
    assertEquals("subprocess", superInstance.getRootToken().getNode().getName());

    List taskInstances = taskMgmtSession.findTaskInstances("victim");
    assertEquals(1, taskInstances.size());
    TaskInstance taskInstance = (TaskInstance) taskInstances.get(0);
    taskInstance.setVariable("a", "value a updated");
    taskInstance.setVariable("b", "value b updated");
    taskInstance.end();

    jbpmContext.save(taskInstance);
    long taskInstanceId = taskInstance.getId();
    long tokenId = taskInstance.getToken().getId();
    newTransaction();

    taskInstance = jbpmContext.loadTaskInstance(taskInstanceId);
    assertEquals("value a updated", taskInstance.getVariable("a"));
    assertEquals("value b updated", taskInstance.getVariable("b"));

    Token token = jbpmContext.loadToken(tokenId);
    ContextInstance subContextInstance = token.getProcessInstance().getContextInstance();
    assertEquals("value a", subContextInstance.getVariable("a"));
    assertEquals("value b updated", subContextInstance.getVariable("b"));
  }