/**
   * Get dicom viewer URL for given study and patient
   *
   * @param study study for the dicom viewer url
   * @param patient patient for the dicom viewer url
   * @should return dicom viewer url given completed study and patient
   * @should return null given non completed study and patient
   */
  private String getDicomViewerUrl(Study study, Patient patient) {

    if (study.isCompleted()) {
      String studyUidUrl = "studyUID=" + study.getStudyInstanceUid();
      String patientIdUrl = "patientID=" + patient.getPatientIdentifier().getIdentifier();
      return RadiologyProperties.getDicomViewerUrl() + studyUidUrl + "&" + patientIdUrl;
    } else {
      return null;
    }
  }
  /**
   * Populate model and view given radiologyOrder and obs
   *
   * @param radiologyOrder to populate the model and view
   * @param obs to populate the model and view
   * @should populate the model and view for given radiology order with completed study and obs
   * @should populate the model and view for given radiology order without completed study and obs
   */
  private ModelAndView populateModelAndView(RadiologyOrder radiologyOrder, Obs obs) {

    ModelAndView result = new ModelAndView(RADIOLOGY_OBS_FORM_PATH);

    List<Obs> previousObs = radiologyService.getObsByOrderId(radiologyOrder.getOrderId());
    result.addObject("obs", obs);
    result.addObject("previousObs", previousObs);

    Study study = radiologyOrder.getStudy();
    result.addObject("studyUID", study.isCompleted() ? study.getStudyInstanceUid() : null);
    result.addObject("dicomViewerUrl", getDicomViewerUrl(study, radiologyOrder.getPatient()));

    return result;
  }
  /** @see RadiologyObsFormController#populateModelAndView(RadiologyOrder, Obs) */
  @Test
  @Verifies(
      value =
          "should populate the model and view for given radiology order  with completed study and obs",
      method = "populateModelAndView(RadiologyOrder, Obs)")
  public void
      populateModelAndView_ShouldPopulateModelAndViewWithObsForGivenRadiologyOrderWithCompletedStudyAndObs()
          throws Exception {

    mockStudy.setPerformedStatus(PerformedProcedureStepStatus.COMPLETED);

    Method populateModelAndViewMethod =
        radiologyObsFormController
            .getClass()
            .getDeclaredMethod(
                "populateModelAndView",
                new Class[] {
                  org.openmrs.module.radiology.RadiologyOrder.class, org.openmrs.Obs.class
                });
    populateModelAndViewMethod.setAccessible(true);

    ModelAndView modelAndView =
        (ModelAndView)
            populateModelAndViewMethod.invoke(
                radiologyObsFormController, new Object[] {mockRadiologyOrder, mockObs});

    assertThat(mockObs.getPerson(), is((Person) mockRadiologyOrder.getPatient()));

    assertThat(modelAndView.getModelMap(), hasKey("obs"));
    Obs obs = (Obs) modelAndView.getModelMap().get("obs");
    assertThat(obs, is(mockObs));

    assertThat(modelAndView.getModelMap(), hasKey("studyUID"));
    String studyUID = (String) modelAndView.getModelMap().get("studyUID");
    assertNotNull(studyUID);
    assertThat(studyUID, is(mockStudy.getStudyInstanceUid()));

    assertThat(modelAndView.getModelMap(), hasKey("previousObs"));
    List<Obs> previousObs = (List<Obs>) modelAndView.getModelMap().get("previousObs");
    assertNotNull(previousObs);
    assertThat(previousObs, is(radiologyService.getObsByOrderId(mockRadiologyOrder.getId())));

    assertThat(modelAndView.getModelMap(), hasKey("dicomViewerUrl"));
  }
  /** @see RadiologyObsFormController#getDicomViewerUrl(Study, Patient) */
  @Test
  @Verifies(
      value = "should return dicom viewer url given completed study and patient",
      method = "getDicomViewerUrl(Study, Patient)")
  public void getDicomViewerUrl_ShouldReturnDicomViewerUrlGivenCompletedStudyAndPatient()
      throws Exception {

    mockStudy.setPerformedStatus(PerformedProcedureStepStatus.COMPLETED);

    when(RadiologyProperties.getServersAddress()).thenReturn("localhost");
    when(RadiologyProperties.getServersPort()).thenReturn("8081");
    when(RadiologyProperties.getDicomViewerUrlBase()).thenReturn("/weasis/viewer?");

    Method getDicomViewerUrlMethod =
        radiologyObsFormController
            .getClass()
            .getDeclaredMethod(
                "getDicomViewerUrl",
                new Class[] {org.openmrs.module.radiology.Study.class, org.openmrs.Patient.class});
    getDicomViewerUrlMethod.setAccessible(true);

    String dicomViewerUrl =
        (String)
            getDicomViewerUrlMethod.invoke(
                radiologyObsFormController, new Object[] {mockStudy, mockPatient});

    assertNotNull(dicomViewerUrl);

    String patID = mockPatient.getPatientIdentifier().getIdentifier();
    assertThat(
        dicomViewerUrl,
        is(
            "http://localhost:8081/weasis/viewer?studyUID="
                + mockStudy.getStudyInstanceUid()
                + "&patientID="
                + patID));
  }