public void testAdvanceReportWorkflow() {
    Integer id = 10;
    Integer wfId = 5;
    String transitionToTake = "abcd";
    String loginId = "SYSTEM_ADMIN";
    ReviewStatus reviewStatus = ReviewStatus.DRAFT_INCOMPLETE;
    Report report = Fixtures.createReport("testReport");
    report.setStatus(ReportStatus.INPROCESS);
    report.setReviewComments(new ArrayList<ReportReviewComment>());
    report.setWorkflowId(wfId);
    List<String> transitions = new ArrayList<String>();

    ReportSubmittability errorMessagesMock = registerMockFor(ReportSubmittability.class);
    EasyMock.expect(errorMessagesMock.isSubmittable()).andReturn(true);
    EasyMock.expect(reportValidationService.isSubmittable(report)).andReturn(errorMessagesMock);
    EasyMock.expect(wfService.nextTransitionNames(wfId, loginId)).andReturn(transitions);
    EasyMock.expect(wfService.advanceWorkflow(wfId, transitionToTake)).andReturn(reviewStatus);
    EasyMock.expect(reportDao.getById(id)).andReturn(report);
    reportDao.save(report);
    replayMocks();
    List<String> transitionsNames = impl.advanceReportWorkflow(wfId, transitionToTake, id, loginId);

    verifyMocks();
    assertEquals(
        "A review comment for the action of advancing workflow was not added",
        1,
        report.getReviewCommentsInternal().size());
  }
  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();
  }
  @Override
  protected Object formBackingObject(HttpServletRequest request) throws Exception {

    ReviewAeReportCommand command =
        new ReviewAeReportCommand(expeditedAdverseEventReportDao, reportDao);
    String aeReportId = request.getParameter("aeReport");
    String reportId = request.getParameter("report");

    ExpeditedAdverseEventReport aeReport =
        expeditedAdverseEventReportDao.getById(Integer.parseInt(aeReportId));

    Report report = reportDao.getById(Integer.parseInt(reportId));

    if (report.isWorkflowEnabled()
        && report.getLastVersion().getReportStatus().equals(ReportStatus.COMPLETED)
        && report.getWorkflowId() != null) {
      User user = workflowService.findCoordinatingCenterReviewer(report.getWorkflowId());
      if (user != null) {
        Reporter r = new Reporter();
        r.copy(user);
        aeReport.setReviewer(r);
      }
    } else {
      aeReport.setReviewer(aeReport.getReporter());
    }

    String xml = adeersReportGenerator.generateCaaersXml(aeReport, report);
    String pngOutFile = WebUtils.getBaseFileName(aeReportId, reportId);

    // CAAERS-5904, see report as per the report definition format
    List<String> list =
        adeersReportGenerator.generateImage(xml, tempDir + File.separator + pngOutFile, report);
    command.addFiles(aeReportId, reportId, list);

    if (reportId != null && !reportId.equals("") && !reportId.equals("null"))
      command.setReportId(Integer.parseInt(reportId));
    else command.setReportId(null);

    // (CAAERS-5865)to perform sync only for ctep-esys studies,
    // set studyOutOfSync to false, so sync will not run for non-ctep-esys studies
    if (aeReport.getStudy().getCtepEsysIdentifier() == null) {
      command.setStudyOutOfSync(false);
    }

    command.setAeReport(aeReport);
    for (Report r : aeReport.getReports()) {
      if (r.getId().equals(Integer.parseInt(reportId)))
        command.setWorkflowEnabled(r.getReportDefinition().getWorkflowEnabled());
    }
    return command;
  }
  public void testAdvanceReportingPeriodWorkflow() {
    Integer id = 5;
    Integer wfId = 5;
    String loginId = "SYSTEM_ADMIN";
    String transitionToTake = "abcd";
    List<String> transitions = new ArrayList<String>();
    ReviewStatus rs = ReviewStatus.DRAFT_INCOMPLETE;
    AdverseEventReportingPeriod rp = Fixtures.createReportingPeriod();
    rp.setReviewComments(new ArrayList<ReportingPeriodReviewComment>());
    EasyMock.expect(wfService.advanceWorkflow(wfId, transitionToTake)).andReturn(rs);
    EasyMock.expect(wfService.nextTransitionNames(wfId, loginId)).andReturn(transitions);
    EasyMock.expect(rpDao.getById(id)).andReturn(rp);
    rpDao.modifyOrSaveReviewStatusAndComments(rp);
    replayMocks();
    List<String> transitionNames =
        impl.advanceReportingPeriodWorkflow(wfId, transitionToTake, id, loginId);

    verifyMocks();
    assertEquals(
        "A review comment for the action of advancing workflow was not added",
        1,
        rp.getReviewComments().size());
  }
  public void testNextTransitionsForAeReportWithCompleteReports() throws Exception {
    Report report = Fixtures.createReport("testReport");
    report.setWorkflowId(1);
    ReportSubmittability errorMessagesMock = registerMockFor(ReportSubmittability.class);
    report.setStatus(ReportStatus.PENDING);
    List<String> transitions = new ArrayList<String>();
    transitions.add("test action");
    transitions.add("Submit to Central Office Report Reviewer");

    EasyMock.expect(wfService.nextTransitionNames(1, "SYSTEM_ADMIN")).andReturn(transitions);
    EasyMock.expect(reportValidationService.isSubmittable(report)).andReturn(errorMessagesMock);
    EasyMock.expect(errorMessagesMock.isSubmittable()).andReturn(true);
    replayMocks();
    List<String> filteredTransitions =
        impl.nextTransitionNamesForReportWorkflow(report, "SYSTEM_ADMIN");
    verifyMocks();
    assertEquals(2, filteredTransitions.size());
  }
  // test the transition, were the physician can login to the system.
  public void testNextTransitionsForAeReportWithAnInvestigatorHavingLogin() throws Exception {
    Report report = Fixtures.createReport("testReport");
    Investigator investigator = Fixtures.createInvestigator("tester");
    investigator.setLoginId("hai");
    report.getPhysician().setPerson(investigator);
    report.setWorkflowId(1);
    ReportSubmittability errorMessagesMock = registerMockFor(ReportSubmittability.class);
    report.setStatus(ReportStatus.PENDING);
    List<String> transitions = new ArrayList<String>();
    transitions.add("test action");
    transitions.add("Send to Physician for Review");

    EasyMock.expect(wfService.nextTransitionNames(1, "SYSTEM_ADMIN")).andReturn(transitions);
    EasyMock.expect(reportValidationService.isSubmittable(report)).andReturn(errorMessagesMock);
    EasyMock.expect(errorMessagesMock.isSubmittable()).andReturn(true);
    replayMocks();
    List<String> filteredTransitions =
        impl.nextTransitionNamesForReportWorkflow(report, "SYSTEM_ADMIN");
    verifyMocks();
    assertEquals(1, filteredTransitions.size());
  }