@Override protected Cohort getExistingObject() { return cohortService.getCohort(EXISTING_ID); } }
/** * @see CohortService#getCohortsContainingPatient(Patient) */ @Test public void getCohortsContainingPatient_shouldReturnCohortsThatHaveGivenPatient() { executeDataSet(COHORT_XML); Patient patientToAdd = new Patient(7); service.addPatientToCohort(service.getCohort(2), patientToAdd); assertTrue(service.getCohort(2).contains(patientToAdd.getPatientId())); List<Cohort> cohortsWithGivenPatient = service.getCohortsContainingPatientId(patientToAdd.getId()); assertTrue(cohortsWithGivenPatient.contains(service.getCohort(2))); }
@Test public void purgeCohortMembership_shouldRemoveMembershipFromCohort() { executeDataSet(COHORT_XML); CohortMembership toPurge = service.getCohortMembershipByUuid("v9a9m5i6-17e6-407c-9d4v-hbi8teu9lf0f"); Cohort owner = toPurge.getCohort(); service.purgeCohortMembership(toPurge); Context.flushSession(); assertNull(service.getCohortMembershipByUuid("v9a9m5i6-17e6-407c-9d4v-hbi8teu9lf0f")); assertFalse(service.getCohort(owner.getId()).contains(toPurge.getPatientId())); }
@Override protected Cohort getObjectById(Integer id) { return Context.getCohortService().getCohort(id); }
/** * @see CohortService#getCohortsContainingPatient(Patient) */ @Test public void getCohortsContainingPatient_shouldNotReturnVoidedCohorts() { executeDataSet(COHORT_XML); // make sure we have two cohorts, the first of which is voided assertTrue(service.getCohort(1).getVoided()); assertFalse(service.getCohort(2).getVoided()); // add a patient to both cohorts Patient patientToAdd = new Patient(7); service.addPatientToCohort(service.getCohort(1), patientToAdd); service.addPatientToCohort(service.getCohort(2), patientToAdd); assertTrue(service.getCohort(1).contains(patientToAdd.getPatientId())); assertTrue(service.getCohort(2).contains(patientToAdd.getPatientId())); // call the method and it should not return the voided cohort List<Cohort> cohortsWithPatientAdded = service.getCohortsContainingPatientId(patientToAdd.getId()); assertNotNull(cohortsWithPatientAdded); assertFalse(cohortsWithPatientAdded.contains(service.getCohort(1))); }
/** * @see CohortService#saveCohort(Cohort) */ @Test public void saveCohort_shouldUpdateAnExistingCohort() { executeDataSet(COHORT_XML); // get and modify a cohort in the data set String modifiedCohortDescription = "This description has been modified in a test"; Cohort cohortToModify = service.getCohort(2); cohortToModify.setDescription(modifiedCohortDescription); // save the modified cohort back to the data set, see if the modification is there service.saveCohort(cohortToModify); assertTrue(service.getCohort(2).getDescription().equals(modifiedCohortDescription)); }
/** * @verifies not return ended memberships * @see CohortService#getCohortsContainingPatient(org.openmrs.Patient) */ @Test public void getCohortsContainingPatient_shouldNotReturnEndedMemberships() throws Exception { executeDataSet(COHORT_XML); Cohort cohort = service.getCohort(2); Patient patient = new Patient(7); CohortMembership membership = new CohortMembership(patient.getPatientId()); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date startDate = dateFormat.parse("2017-01-01 00:00:00"); membership.setStartDate(startDate); cohort.addMembership(membership); assertTrue(cohort.contains(patient.getPatientId())); Date endDate = dateFormat.parse("2017-01-31 00:00:00"); membership.setEndDate(endDate); List<Cohort> cohortsWithPatientAdded = service.getCohortsContainingPatientId(patient.getId()); assertEquals(0, cohortsWithPatientAdded.size()); }
/** * @see CohortService#addPatientToCohort(Cohort,Patient) */ @Test public void addPatientToCohort_shouldAddAPatientAndSaveTheCohort() { executeDataSet(COHORT_XML); // make a patient, add it using the method Patient patientToAdd = Context.getPatientService().getPatient(3); service.addPatientToCohort(service.getCohort(2), patientToAdd); // proof of "save the cohort": see if the patient is in the cohort assertTrue(service.getCohort(2).contains(3)); }
@Test public void patientVoided_shouldVoidMemberships() { executeDataSet(COHORT_XML); Cohort cohort = Context.getCohortService().getCohort(2); Patient voidedPatient = new Patient(7); voidedPatient.setVoided(true); voidedPatient.setDateVoided(new Date()); voidedPatient.setVoidedBy(Context.getAuthenticatedUser()); voidedPatient.setVoidReason("Voided as a result of the associated patient getting voided"); CohortMembership newMemberContainingVoidedPatient = new CohortMembership(voidedPatient.getPatientId()); cohort.addMembership(newMemberContainingVoidedPatient); assertTrue(cohort.contains(voidedPatient.getPatientId())); assertEquals(1, service.getCohortsContainingPatientId(voidedPatient.getId()).size()); service.notifyPatientVoided(voidedPatient); assertTrue(newMemberContainingVoidedPatient.getVoided()); assertEquals(newMemberContainingVoidedPatient.getDateVoided(), voidedPatient.getDateVoided()); assertEquals(newMemberContainingVoidedPatient.getVoidedBy(), voidedPatient.getVoidedBy()); assertEquals(newMemberContainingVoidedPatient.getVoidReason(), voidedPatient.getVoidReason()); }
/** * @see CohortService#addPatientToCohort(Cohort,Patient) */ @Test public void addPatientToCohort_shouldNotFailIfCohortAlreadyContainsPatient() { executeDataSet(COHORT_XML); // make a patient, add it using the method Patient patientToAdd = Context.getPatientService().getPatient(3); service.addPatientToCohort(service.getCohort(2), patientToAdd); assertTrue(service.getCohort(2).contains(3)); // do it again to see if it fails try { service.addPatientToCohort(service.getCohort(2), patientToAdd); } catch (Exception e) { Assert.fail("addPatientToCohort(Cohort,Patient) fails when cohort already contains patient."); } }
/** * @verifies not get matching memberships of a cohort as of a date * @see Cohort#getActiveMemberships(Date) */ @Test public void getMemberships_shouldNotGetMatchingMembershipsAsOfADate() throws Exception { executeDataSet(COHORT_XML); Cohort cohort = Context.getCohortService().getCohort(1); CohortMembership newMember = new CohortMembership(4); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date startDate = dateFormat.parse("2017-11-01 00:00:00"); newMember.setStartDate(startDate); cohort.addMembership(newMember); service.saveCohort(cohort); Date dateToTest = dateFormat.parse("2016-11-01 00:00:00"); Collection<CohortMembership> membersAsOfDate = cohort.getActiveMemberships(dateToTest); assertFalse(membersAsOfDate.stream().anyMatch(m -> m.getStartDate().equals(dateToTest))); }
/** * @verifies {@link Cohort#getActiveMemberships(Date)} */ @Test public void getMemberships_shouldGetMembershipsAsOfADate() throws ParseException { executeDataSet(COHORT_XML); Cohort cohort = Context.getCohortService().getCohort(1); CohortMembership newMember = new CohortMembership(4); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date dateToTest = dateFormat.parse("2016-11-01 00:00:00"); newMember.setStartDate(dateToTest); cohort.addMembership(newMember); service.saveCohort(cohort); Collection<CohortMembership> membersAsOfDate = cohort.getActiveMemberships(dateToTest); assertFalse(membersAsOfDate.isEmpty()); assertTrue(membersAsOfDate.stream().anyMatch(m -> m.getStartDate().equals(dateToTest))); }
@Test public void removePatientFromCohort_shouldNotFailIfCohortDoesNotContainPatient() { executeDataSet(COHORT_XML); // make a patient Patient notInCohort = new Patient(4); // verify that the patient is not already in the Cohort assertFalse(service.getCohort(2).contains(notInCohort.getPatientId())); // try to remove it from the cohort without failing try { service.removePatientFromCohort(service.getCohort(2), notInCohort); } catch (Exception e) { Assert.fail("removePatientFromCohort(Cohort,Patient) should not fail if cohort doesn't contain patient"); } }
@Test public void endCohortMembership_shouldEndTheCohortMembership() { Date endOnDate = new Date(); executeDataSet(COHORT_XML); Cohort cohort = service.getCohort(1); CohortMembership cm = cohort.getActiveMemberships().iterator().next(); assertNull(cm.getEndDate()); service.endCohortMembership(cm, endOnDate); assertEquals(endOnDate, cm.getEndDate()); // Since TRUNK-5450 also CohortMembers with an end-date are taken into account by contains assertTrue(cohort.contains(cm.getPatientId())); }
@Test public void removePatientFromCohort_shouldSaveCohortAfterRemovingPatient() { executeDataSet(COHORT_XML); Cohort cohort = service.getCohort(2); Integer patientId = cohort.getMemberships().iterator().next().getPatientId(); Patient patient = Context.getPatientService().getPatient(patientId); service.removePatientFromCohort(cohort, patient); assertFalse(cohort.contains(patientId)); }
/** * @see CohortService#getCohort(Integer) */ @Test public void getCohort_shouldGetCohortById() { executeDataSet(COHORT_XML); Cohort cohortToGet = service.getCohort(2); assertNotNull(cohortToGet); assertTrue(cohortToGet.getCohortId() == 2); }
@Test public void voidCohortMembership_shouldVoidCohortMembership() { executeDataSet(COHORT_XML); Cohort cohort = service.getCohort(1); CohortMembership cm = cohort.getActiveMemberships().iterator().next(); final String reason = "Some reason"; service.voidCohortMembership(cm, reason); assertTrue(cm.getVoided()); assertNotNull(cm.getVoidedBy()); assertNotNull(cm.getDateVoided()); assertEquals(reason, cm.getVoidReason()); assertFalse(cohort.contains(cm.getPatientId())); }
@Before public void setUp() { validator = new CohortValidator(); executeDataSet(COHORT_XML); cohort = Context.getCohortService().getCohort(2); patient = Context.getPatientService().getPatient(7); cohortMembership = new CohortMembership(patient.getPatientId()); cohort.addMembership(cohortMembership); errors = new BindException(cohort, "cohort"); }
/** * @verifies return unvoided memberships * @see Cohort#getMemberships(boolean) */ @Test public void getMemberships_shouldReturnUnvoidedMemberships() throws Exception { executeDataSet(COHORT_XML); Cohort cohort = Context.getCohortService().getCohort(1); CohortMembership nonVoidedMembership = new CohortMembership(4); CohortMembership voidedMembership = new CohortMembership(7); voidedMembership.setVoided(true); voidedMembership.setVoidedBy(Context.getAuthenticatedUser()); voidedMembership.setDateVoided(new Date()); voidedMembership.setVoidReason("Void reason"); cohort.addMembership(nonVoidedMembership); cohort.addMembership(voidedMembership); Context.getCohortService().saveCohort(cohort); Collection<CohortMembership> unvoidedMemberships = cohort.getMemberships(false); assertEquals(2, unvoidedMemberships.size()); }
@Test public void patientUnvoided_shouldUnvoidMemberships() { executeDataSet(COHORT_XML); Cohort cohort = Context.getCohortService().getCohort(2); Patient unvoidedPatient = new Patient(7); User voidedBy = Context.getAuthenticatedUser(); Date dateVoided = new Date(); String voidReason = "Associated patient is voided"; CohortMembership voidedMembership = new CohortMembership(unvoidedPatient.getPatientId()); cohort.addMembership(voidedMembership); voidedMembership.setVoided(true); voidedMembership.setVoidedBy(voidedBy); voidedMembership.setDateVoided(dateVoided); voidedMembership.setVoidReason(voidReason); service.notifyPatientUnvoided(unvoidedPatient, voidedBy, dateVoided); assertFalse(voidedMembership.getVoided()); assertNull(voidedMembership.getVoidedBy()); assertNull(voidedMembership.getDateVoided()); assertNull(voidedMembership.getVoidReason()); }