@Test
  public void evaluate_shouldReturnHivPatientsNotScreenedForTb() throws Exception {

    // Get HIV Program and TB screening encounter type
    Program hivProgram = MetadataUtils.getProgram(Metadata.HIV_PROGRAM);
    EncounterType screeningEncType =
        MetadataUtils.getEncounterType(Metadata.TB_SCREENING_ENCOUNTER_TYPE);

    // Enroll patients #6 and #7
    PatientService ps = Context.getPatientService();
    TestUtils.enrollInProgram(ps.getPatient(6), hivProgram, TestUtils.date(2011, 1, 1));
    TestUtils.enrollInProgram(ps.getPatient(7), hivProgram, TestUtils.date(2011, 1, 1));

    // Screen patient #6 for TB a year later
    TestUtils.saveEncounter(ps.getPatient(6), screeningEncType, TestUtils.date(2012, 1, 1));

    Context.flushSession();

    List<Integer> cohort = Arrays.asList(6, 7, 8);
    CalculationResultMap resultMap =
        Context.getService(PatientCalculationService.class)
            .evaluate(cohort, new NeverScreenedForTbCalculation());
    Assert.assertFalse((Boolean) resultMap.get(6).getValue());
    Assert.assertTrue((Boolean) resultMap.get(7).getValue());
    Assert.assertNull(resultMap.get(8)); // not in HIV program
  }
  public void controller(
      @FragmentParam("patient") Patient patient,
      FragmentModel model,
      UiUtils ui,
      @SpringBean CoreContext emr) {

    String[] page1FormUuids = {Metadata.FAMILY_HISTORY_FORM, Metadata.HIV_PROGRAM_ENROLLMENT_FORM};

    List<SimpleObject> page1AvailableForms = new ArrayList<SimpleObject>();
    List<Encounter> page1Encounters = new ArrayList<Encounter>();

    for (String page1FormUuid : page1FormUuids) {
      Form page1Form = MetadataUtils.getForm(page1FormUuid);
      List<Encounter> formEncounters = getPatientEncounterByForm(patient, page1Form);

      if (formEncounters.size() == 0) {
        page1AvailableForms.add(ui.simplifyObject(page1Form));
      } else {
        page1Encounters.addAll(formEncounters);
      }
    }

    Form moh257VisitForm = MetadataUtils.getForm(Metadata.MOH_257_VISIT_SUMMARY_FORM);
    List<Encounter> moh257VisitSummaryEncounters =
        getPatientEncounterByForm(patient, moh257VisitForm);

    model.addAttribute("page1AvailableForms", page1AvailableForms);
    model.addAttribute("page1Encounters", page1Encounters);
    model.addAttribute("page2Form", moh257VisitForm);
    model.addAttribute("page2Encounters", moh257VisitSummaryEncounters);

    Concept masterSet = emr.getRegimenManager().getMasterSetConcept("ARV");
    RegimenChangeHistory arvHistory = RegimenChangeHistory.forPatient(patient, masterSet);
    model.addAttribute("arvHistory", arvHistory);
  }
  /** @see EmrVisitAssignmentHandler#getAutoCreateVisitType(org.openmrs.Encounter) */
  @Test
  public void getAutoCreateVisitType_shouldReturnAutoCreateVisitTypeIfSpecified() {
    VisitType outpatient = MetadataUtils.getVisitType(CommonMetadata._VisitType.OUTPATIENT);

    // Check form that doesn't specify one
    Encounter hivAddendum = new Encounter();
    hivAddendum.setForm(MetadataUtils.getForm(HivMetadata._Form.CLINICAL_ENCOUNTER_HIV_ADDENDUM));

    Assert.assertThat(
        EmrVisitAssignmentHandler.getAutoCreateVisitType(hivAddendum), is(nullValue()));

    // Check form that does specify one
    Encounter moh257 = new Encounter();
    moh257.setForm(MetadataUtils.getForm(HivMetadata._Form.MOH_257_VISIT_SUMMARY));

    Assert.assertThat(EmrVisitAssignmentHandler.getAutoCreateVisitType(moh257), is(outpatient));
  }
  /**
   * @see
   *     org.openmrs.module.kenyaemr.calculation.library.hiv.MissedAppointmentsOrDefaultedCalculation#evaluate(java.util.Collection,
   *     java.util.Map, org.openmrs.calculation.patient.PatientCalculationContext)
   * @verifies determine whether patients have a Missed appointments or defaulted
   */
  @Test
  public void evaluate_shouldDetermineWhetherPatientsWhoMissedAppointmentsOrDefaulted()
      throws Exception {

    // Get HIV Program
    Program hivProgram = MetadataUtils.getProgram(Metadata.HIV_PROGRAM);

    // Enroll patients #6, #7, #8 in the HIV Program
    PatientService ps = Context.getPatientService();
    for (int i = 6; i <= 8; ++i) {
      TestUtils.enrollInProgram(ps.getPatient(i), hivProgram, TestUtils.date(2011, 1, 1));
    }

    // Give patient #7 a return visit obs of 10 days ago
    Concept returnVisit = Context.getConceptService().getConcept(5096);
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DATE, -10);
    TestUtils.saveObs(
        Context.getPatientService().getPatient(7),
        returnVisit,
        calendar.getTime(),
        calendar.getTime());

    // Give patient #8 a return visit obs of 10 days in the future
    calendar = Calendar.getInstance();
    calendar.add(Calendar.DATE, 10);
    TestUtils.saveObs(
        Context.getPatientService().getPatient(8),
        returnVisit,
        calendar.getTime(),
        calendar.getTime());

    Context.flushSession();

    List<Integer> ptIds = Arrays.asList(6, 7, 8, 9);

    CalculationResultMap resultMap =
        new MissedAppointmentsOrDefaultedCalculation()
            .evaluate(
                ptIds,
                null,
                Context.getService(PatientCalculationService.class).createCalculationContext());
    Assert.assertFalse(
        (Boolean) resultMap.get(6).getValue()); // patient in HIV program but no return visit obs
    Assert.assertTrue((Boolean) resultMap.get(7).getValue()); // patient has missed visit
    Assert.assertFalse(
        (Boolean) resultMap.get(8).getValue()); // patient has future return visit date
    Assert.assertFalse((Boolean) resultMap.get(9).getValue()); // patient not in HIV Program
  }
  /** @see EmrVisitAssignmentHandler#checkLocations(org.openmrs.Visit, org.openmrs.Encounter) */
  @Test
  public void checkLocations() {
    Patient patient = TestUtils.getPatient(7);
    Form moh257 = MetadataUtils.getForm(HivMetadata._Form.MOH_257_VISIT_SUMMARY);
    VisitType outpatient = MetadataUtils.getVisitType(CommonMetadata._VisitType.OUTPATIENT);

    // Save regular visit on Jan 1st at no specific location
    Visit visit0 = TestUtils.saveVisit(patient, outpatient, TestUtils.date(2012, 1, 1), null);

    // Save regular visit on Jan 1st at location #2
    Visit visit1 = TestUtils.saveVisit(patient, outpatient, TestUtils.date(2012, 1, 1), null);
    visit1.setLocation(Context.getLocationService().getLocation(1));

    // Save regular visit on Jan 1st at location #2
    Visit visit2 = TestUtils.saveVisit(patient, outpatient, TestUtils.date(2012, 1, 1), null);
    visit2.setLocation(Context.getLocationService().getLocation(2));

    // Save MOH257 for that day (will default to location #1)
    Encounter encounter = TestUtils.saveEncounter(patient, moh257, TestUtils.date(2012, 1, 1));

    Assert.assertThat(EmrVisitAssignmentHandler.checkLocations(visit0, encounter), is(true));
    Assert.assertThat(EmrVisitAssignmentHandler.checkLocations(visit1, encounter), is(true));
    Assert.assertThat(EmrVisitAssignmentHandler.checkLocations(visit2, encounter), is(false));
  }
 /**
  * Gets a concept by an identifier (mapping or UUID)
  *
  * @param identifier the identifier
  * @return the concept
  * @throws IllegalArgumentException if the concept could not be found
  */
 public static Concept getConcept(String identifier) {
   return MetadataUtils.getConcept(identifier);
 }