@Override protected Cohort getObjectByUuid(String uuid) { return Context.getCohortService().getCohortByUuid(uuid); } }
/** * @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)); }
/** * @see CohortService#saveCohort(Cohort) */ @Test public void saveCohort_shouldCreateNewCohorts() { executeDataSet(COHORT_XML); // make sure we have two cohorts List<Cohort> allCohorts = service.getAllCohorts(true); assertNotNull(allCohorts); assertEquals(2, allCohorts.size()); // make and save a new one Integer[] ids = { 2, 3 }; Cohort newCohort = new Cohort("a third cohort", "a cohort to add for testing", ids); service.saveCohort(newCohort); // see if the new cohort shows up in the list of cohorts allCohorts = service.getAllCohorts(true); assertNotNull(allCohorts); assertEquals(3, allCohorts.size()); }
/** * @see CohortService#purgeCohort(Cohort) */ @Test public void purgeCohort_shouldDeleteCohortFromDatabase() { executeDataSet(COHORT_XML); List<Cohort> allCohorts = service.getAllCohorts(true); assertEquals(2, allCohorts.size()); service.purgeCohort(allCohorts.get(0)); allCohorts = service.getAllCohorts(true); assertEquals(1, allCohorts.size()); }
/** * @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))); }
/** * @see org.openmrs.api.CohortService#voidCohort(org.openmrs.Cohort, java.lang.String) */ @Override public Cohort voidCohort(Cohort cohort, String reason) { // other setters done by the save handlers return Context.getCohortService().saveCohort(cohort); }
@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())); }
@Test public void handle_shouldVoidCohortMemberships() throws Exception { // test a corner case by letting the same patient belong to the cohort for two separate periods CohortMembership membership1 = new CohortMembership(7, parseDate("2001-01-01", "yyyy-MM-dd")); membership1.setEndDate(parseDate("2001-12-31", "yyyy-MM-dd")); CohortMembership membership2 = new CohortMembership(7, parseDate("2017-01-01", "yyyy-MM-dd")); CohortMembership membership3 = new CohortMembership(8); Cohort cohort = new Cohort(); cohort.setName("Cohort"); cohort.setDescription("Description"); cohort.setUuid(COHORT_UUID); cohort.addMembership(membership1); cohort.addMembership(membership2); cohort.addMembership(membership3); cohortService.saveCohort(cohort); PatientService patientService = Context.getPatientService(); patientService.voidPatient(patientService.getPatient(7), "void reason"); Collection<CohortMembership> memberships = cohortService.getCohortByUuid(COHORT_UUID).getMemberships(false); assertEquals(1, memberships.size()); assertEquals(8, (int) memberships.iterator().next().getPatientId()); // patientId 7 was voided } }
/** * @see org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource#getByUniqueId(java.lang.String) */ @Override public Cohort getByUniqueId(String param) { Cohort result = Context.getCohortService().getCohortByUuid(param); if (result == null) return Context.getCohortService().getCohort(param); return result; }
@Override protected Cohort getExistingObject() { return cohortService.getCohort(EXISTING_ID); } }
@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())); }
@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#getCohortMembershipByUuid(String) */ @Test public void getCohortMembershipByUuid_shouldReturnNullIfNoObjectFoundWithGivenUuid() { assertNull(Context.getCohortService().getCohortMembershipByUuid("some invalid uuid")); }
/** * @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."); } }
@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"); } }
/** * @see CohortService#voidCohort(Cohort,String) */ @Test public void voidCohort_shouldVoidCohort() { executeDataSet(COHORT_XML); // make sure we have a cohort that is not voided List<Cohort> allCohorts = service.getAllCohorts(true); assertNotNull(allCohorts); assertEquals(2, allCohorts.size()); assertFalse(allCohorts.get(1).getVoided()); service.voidCohort(allCohorts.get(1), "voided for Test"); assertTrue(allCohorts.get(1).getVoided()); }
/** * @see org.openmrs.api.CohortService#getAllCohorts() */ @Override @Transactional(readOnly = true) public List<Cohort> getAllCohorts() throws APIException { return Context.getCohortService().getAllCohorts(false); }
@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()); }
@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())); }
/** * @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()); }