/** @see RadiologyObsFormController#updateReadingPhysician(Study) */
  @Test
  @Verifies(
      value = "should not update reading physician for given study with reading physician",
      method = "updateReadingPhysician(Study)")
  public void
      updateReadingPhysician_shouldNotUpdateReadingPhysicianForGivenStudyWithReadingPhysician()
          throws Exception {

    User otherMockReadingPhysician = RadiologyTestData.getMockRadiologyReadingPhysician();
    assertThat(mockReadingPhysician, is(not(otherMockReadingPhysician)));

    mockStudy.setReadingPhysician(mockReadingPhysician);
    when(Context.getAuthenticatedUser()).thenReturn(otherMockReadingPhysician);

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

    assertThat(mockStudy.getReadingPhysician(), is(mockReadingPhysician));

    updateReadingPhysicianMethod.invoke(radiologyObsFormController, new Object[] {mockStudy});

    assertThat(mockStudy.getReadingPhysician(), is(mockReadingPhysician));
  }
  /**
   * Updates reading physician for given study
   *
   * @param study for which the reading physician should be updated
   * @should update reading physician for given study and user authenticated as reading physician
   * @should not update reading physician if user is not authenticated as reading physician
   * @should not update reading physician for given study with reading physician
   */
  private void updateReadingPhysician(Study study) {

    User user = Context.getAuthenticatedUser();
    if (user.hasRole(READING_PHYSICIAN, true) && study.getReadingPhysician() == null)
      study.setReadingPhysician(user);
    radiologyService.saveStudy(study);
  }
  /**
   * 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#getDicomViewerUrl(Study, Patient) */
  @Test
  @Verifies(
      value = "should return null given non completed study and patient",
      method = "getDicomViewerUrl(Study, Patient)")
  public void getDicomViewerUrl_ShouldReturnNullGivenNonCompletedStudyAndPatient()
      throws Exception {

    mockStudy.setPerformedStatus(PerformedProcedureStepStatus.IN_PROGRESS);

    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});

    assertThat(dicomViewerUrl, nullValue());
  }
  /** @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));
  }
  /** @see RadiologyObsFormController#updateReadingPhysician(Study) */
  @Test
  @Verifies(
      value =
          "should not update reading physician if user is not authenticated as reading physician",
      method = "updateReadingPhysician(Study)")
  public void
      updateReadingPhysician_shouldNotUpdateReadingPhysicianIfUserIsNotAuthenticatedAsReadingPhysician()
          throws Exception {

    when(Context.getAuthenticatedUser()).thenReturn(mockRadiologyScheduler);

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

    assertThat(mockStudy.getReadingPhysician(), nullValue());

    updateReadingPhysicianMethod.invoke(radiologyObsFormController, new Object[] {mockStudy});

    assertThat(mockStudy.getReadingPhysician(), nullValue());
  }