Search in sources :

Example 21 with PersonMergeLog

use of org.openmrs.person.PersonMergeLog in project openmrs-core by openmrs.

the class PatientServiceTest method mergePatients_shouldAuditVoidedRelationships.

/**
 * @see PatientService#mergePatients(Patient,Patient)
 */
@Test
public void mergePatients_shouldAuditVoidedRelationships() throws Exception {
    // create relationships and retrieve preferred and notPreferredPatient patient
    executeDataSet(PATIENT_RELATIONSHIPS_XML);
    Patient preferred = patientService.getPatient(999);
    Patient notPreferred = patientService.getPatient(2);
    voidOrders(Collections.singleton(notPreferred));
    // merge the two patients and retrieve the audit object
    PersonMergeLog audit = mergeAndRetrieveAudit(preferred, notPreferred);
    Assert.assertTrue("relationship voiding not audited", isValueInList(personService.getRelationship(4).getUuid(), audit.getPersonMergeLogData().getVoidedRelationships()));
}
Also used : Patient(org.openmrs.Patient) PersonMergeLog(org.openmrs.person.PersonMergeLog) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) PatientServiceImplTest(org.openmrs.api.impl.PatientServiceImplTest) Test(org.junit.Test)

Example 22 with PersonMergeLog

use of org.openmrs.person.PersonMergeLog in project openmrs-core by openmrs.

the class PatientServiceTest method mergePatients_shouldMergeVisitsFromNonPreferredToPreferredPatient.

/**
 * @see PatientService#mergePatients(Patient,Patient)
 */
@Test
public void mergePatients_shouldMergeVisitsFromNonPreferredToPreferredPatient() throws Exception {
    executeDataSet(ENCOUNTERS_FOR_VISITS_XML);
    VisitService visitService = Context.getVisitService();
    Patient notPreferred = patientService.getPatient(2);
    voidOrders(Collections.singleton(notPreferred));
    Patient preferred = patientService.getPatient(6);
    // patient 2 (not preferred) has 3 unvoided visits (id = 1, 2, 3) and 1 voided visit (id = 6)
    Visit visit1 = visitService.getVisit(1);
    Visit visit2 = visitService.getVisit(2);
    Visit visit3 = visitService.getVisit(3);
    Visit visit6 = visitService.getVisit(6);
    // patient 6 (preferred) has 2 unvoided visits (id = 4, 5) and no voided visits
    Visit visit4 = visitService.getVisit(4);
    Visit visit5 = visitService.getVisit(5);
    List<String> encounterUuidsThatShouldBeMoved = new ArrayList<>();
    encounterUuidsThatShouldBeMoved.add(Context.getEncounterService().getEncounter(6).getUuid());
    for (Visit v : Arrays.asList(visit1, visit2, visit3)) {
        for (Encounter e : v.getEncounters()) {
            encounterUuidsThatShouldBeMoved.add(e.getUuid());
        }
    }
    List<Obs> originalUnvoidedObs = Context.getObsService().getObservationsByPerson(notPreferred);
    PersonMergeLog mergeLog = mergeAndRetrieveAudit(preferred, notPreferred);
    Patient merged = patientService.getPatient(preferred.getId());
    List<Visit> mergedVisits = visitService.getVisitsByPatient(merged, true, true);
    assertThat(mergedVisits.size(), is(6));
    // in order to keep this test passing when (someday?) we copy visits instead of moving them, use matchers here:
    assertThat(mergedVisits, containsInAnyOrder(matchingVisit(visit1), matchingVisit(visit2), matchingVisit(visit3), matchingVisit(visit4), matchingVisit(visit5), matchingVisit(visit6)));
    // be sure nothing slipped through without being assigned to the right patient (probably not necessary)
    for (Visit v : mergedVisits) {
        for (Encounter e : v.getEncounters()) {
            assertThat(e.getPatient(), is(v.getPatient()));
            for (Obs o : e.getAllObs(true)) {
                if (!originalUnvoidedObs.contains(o)) {
                    assertThat(o.getPerson().getId(), is(v.getPatient().getId()));
                }
            }
        }
    }
    // now check that moving visits and their contained encounters was audited correctly
    PersonMergeLogData mergeLogData = mergeLog.getPersonMergeLogData();
    assertThat(mergeLogData.getMovedVisits().size(), is(4));
    assertThat(mergeLogData.getMovedVisits(), containsInAnyOrder(visit1.getUuid(), visit2.getUuid(), visit3.getUuid(), visit6.getUuid()));
    assertThat(mergeLogData.getMovedEncounters().size(), is(encounterUuidsThatShouldBeMoved.size()));
    assertThat(mergeLogData.getMovedEncounters(), containsInAnyOrder(encounterUuidsThatShouldBeMoved.toArray()));
}
Also used : Obs(org.openmrs.Obs) Visit(org.openmrs.Visit) ArrayList(java.util.ArrayList) Patient(org.openmrs.Patient) Encounter(org.openmrs.Encounter) PersonMergeLog(org.openmrs.person.PersonMergeLog) PersonMergeLogData(org.openmrs.person.PersonMergeLogData) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) PatientServiceImplTest(org.openmrs.api.impl.PatientServiceImplTest) Test(org.junit.Test)

Example 23 with PersonMergeLog

use of org.openmrs.person.PersonMergeLog in project openmrs-core by openmrs.

the class PatientServiceTest method mergePatients_shouldAuditCreatedPatientPrograms.

/**
 * @see PatientService#mergePatients(Patient,Patient)
 */
@Test
public void mergePatients_shouldAuditCreatedPatientPrograms() throws Exception {
    // retrieve preferred  and notPreferredPatient patient
    Patient preferred = patientService.getPatient(999);
    Patient notPreferred = patientService.getPatient(2);
    voidOrders(Collections.singleton(notPreferred));
    // retrieve program for notProferred patient
    PatientProgram program = Context.getProgramWorkflowService().getPatientPrograms(notPreferred, null, null, null, null, null, false).get(0);
    // merge the two patients and retrieve the audit object
    PersonMergeLog audit = mergeAndRetrieveAudit(preferred, notPreferred);
    // find the UUID of the program to which the preferred patient was enrolled as a result of the merge
    String enrolledProgramUuid = null;
    List<PatientProgram> programs = Context.getProgramWorkflowService().getPatientPrograms(preferred, null, null, null, null, null, false);
    for (PatientProgram p : programs) {
        if (p.getDateCreated().equals(program.getDateCreated())) {
            enrolledProgramUuid = p.getUuid();
        }
    }
    Assert.assertNotNull("expected enrolled program was not found for the preferred patient after the merge", enrolledProgramUuid);
    Assert.assertTrue("program enrollment not audited", isValueInList(enrolledProgramUuid, audit.getPersonMergeLogData().getCreatedPrograms()));
}
Also used : Patient(org.openmrs.Patient) PersonMergeLog(org.openmrs.person.PersonMergeLog) PatientProgram(org.openmrs.PatientProgram) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) PatientServiceImplTest(org.openmrs.api.impl.PatientServiceImplTest) Test(org.junit.Test)

Example 24 with PersonMergeLog

use of org.openmrs.person.PersonMergeLog in project openmrs-core by openmrs.

the class PatientServiceTest method mergePatients_shouldAuditMovedEncounters.

/**
 * @see PatientService#mergePatients(Patient,Patient)
 */
@Test
public void mergePatients_shouldAuditMovedEncounters() throws Exception {
    // retrieve patients
    Patient preferred = patientService.getPatient(999);
    Patient notPreferred = patientService.getPatient(7);
    voidOrders(Collections.singleton(notPreferred));
    // merge the two patients and retrieve the audit object
    PersonMergeLog audit = mergeAndRetrieveAudit(preferred, notPreferred);
    Assert.assertTrue("encounter creation not audited", isValueInList(Context.getEncounterService().getEncounter(3).getUuid(), audit.getPersonMergeLogData().getMovedEncounters()));
}
Also used : Patient(org.openmrs.Patient) PersonMergeLog(org.openmrs.person.PersonMergeLog) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) PatientServiceImplTest(org.openmrs.api.impl.PatientServiceImplTest) Test(org.junit.Test)

Example 25 with PersonMergeLog

use of org.openmrs.person.PersonMergeLog in project openmrs-core by openmrs.

the class PatientServiceTest method mergePatients_shouldAuditCreatedRelationships.

/**
 * @see PatientService#mergePatients(Patient,Patient)
 */
@Test
public void mergePatients_shouldAuditCreatedRelationships() throws Exception {
    // create relationships and retrieve preferred  and notPreferredPatient patient
    executeDataSet(PATIENT_RELATIONSHIPS_XML);
    Patient preferred = patientService.getPatient(7);
    Patient notPreferred = patientService.getPatient(2);
    voidOrders(Collections.singleton(notPreferred));
    // merge the two patients and retrieve the audit object
    PersonMergeLog audit = mergeAndRetrieveAudit(preferred, notPreferred);
    // find the UUID of the created relationship as a result of the merge
    // note: since patient 2 is related to patient 1. patient 7 should now be related to patient 1
    String createdRelationshipUuid = null;
    List<Relationship> relationships = personService.getRelationshipsByPerson(preferred);
    for (Relationship r : relationships) {
        if (r.getPersonB().getId().equals(1) || r.getPersonA().getId().equals(1)) {
            createdRelationshipUuid = r.getUuid();
        }
    }
    Assert.assertNotNull("expected relationship was not found for the preferred patient after the merge", createdRelationshipUuid);
    Assert.assertTrue("relationship creation not audited", isValueInList(createdRelationshipUuid, audit.getPersonMergeLogData().getCreatedRelationships()));
}
Also used : Relationship(org.openmrs.Relationship) Patient(org.openmrs.Patient) PersonMergeLog(org.openmrs.person.PersonMergeLog) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) PatientServiceImplTest(org.openmrs.api.impl.PatientServiceImplTest) Test(org.junit.Test)

Aggregations

PersonMergeLog (org.openmrs.person.PersonMergeLog)41 Test (org.junit.Test)37 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)37 Patient (org.openmrs.Patient)18 PatientServiceImplTest (org.openmrs.api.impl.PatientServiceImplTest)18 Person (org.openmrs.Person)9 PersonName (org.openmrs.PersonName)8 PersonMergeLogData (org.openmrs.person.PersonMergeLogData)8 Date (java.util.Date)6 BindException (org.springframework.validation.BindException)6 Errors (org.springframework.validation.Errors)6 GregorianCalendar (java.util.GregorianCalendar)4 Location (org.openmrs.Location)2 Obs (org.openmrs.Obs)2 PatientIdentifier (org.openmrs.PatientIdentifier)2 PersonAddress (org.openmrs.PersonAddress)2 PersonAttribute (org.openmrs.PersonAttribute)2 ArrayList (java.util.ArrayList)1 Encounter (org.openmrs.Encounter)1 PatientIdentifierType (org.openmrs.PatientIdentifierType)1