diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/PatientTranslatorImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/PatientTranslatorImpl.java index aeeb7f813..037077fd6 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/PatientTranslatorImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/PatientTranslatorImpl.java @@ -148,7 +148,14 @@ public org.openmrs.Patient toOpenmrsType(@Nonnull org.openmrs.Patient currentPat } for (HumanName name : patient.getName()) { - currentPatient.addName(nameTranslator.toOpenmrsType(name)); + PersonName existingName = null; + if (name.hasId()) { + existingName = currentPatient.getNames().stream().filter(n -> n.getUuid().equals(name.getId())).findFirst() + .orElse(null); + } + + PersonName pn = nameTranslator.toOpenmrsType(existingName != null ? existingName : new PersonName(), name); + currentPatient.addName(pn); } if (patient.hasGender()) { diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/PatientTranslatorImplTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/PatientTranslatorImplTest.java index 73a885b5d..eae554874 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/PatientTranslatorImplTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/PatientTranslatorImplTest.java @@ -66,6 +66,8 @@ public class PatientTranslatorImplTest { private static final String PATIENT_IDENTIFIER_UUID = "654321-fedcba-654321"; + private static final String PATIENT_NAME_UUID = "1fdb5469-57c7-435f-b009-3dcceb23b0a2"; + private static final String PATIENT_GIVEN_NAME = "Jean Claude"; private static final String PATIENT_FAMILY_NAME = "van Damme"; @@ -304,10 +306,30 @@ public void shouldTranslateFhirPatientNameToOpenmrsPatientName() { PersonName personName = new PersonName(); personName.setGivenName(PATIENT_GIVEN_NAME); personName.setFamilyName(PATIENT_FAMILY_NAME); - when(nameTranslator.toOpenmrsType(any())).thenReturn(personName); + when(nameTranslator.toOpenmrsType(any(), any())).thenReturn(personName); + + Patient patient = new Patient(); + HumanName name = new HumanName(); + name.addGiven(PATIENT_GIVEN_NAME); + name.setFamily(PATIENT_FAMILY_NAME); + patient.addName(name); + + org.openmrs.Patient result = patientTranslator.toOpenmrsType(patient); + assertThat(result.getGivenName(), equalTo(PATIENT_GIVEN_NAME)); + assertThat(result.getFamilyName(), equalTo(PATIENT_FAMILY_NAME)); + } + + @Test + public void shouldTranslateFhirPatientNameToExistingOpenmrsPatientName() { + PersonName personName = new PersonName(); + personName.setUuid(PATIENT_NAME_UUID); + personName.setGivenName(PATIENT_GIVEN_NAME); + personName.setFamilyName(PATIENT_FAMILY_NAME); + when(nameTranslator.toOpenmrsType(any(), any())).thenReturn(personName); Patient patient = new Patient(); HumanName name = new HumanName(); + name.setId(PATIENT_NAME_UUID); name.addGiven(PATIENT_GIVEN_NAME); name.setFamily(PATIENT_FAMILY_NAME); patient.addName(name); @@ -315,6 +337,7 @@ public void shouldTranslateFhirPatientNameToOpenmrsPatientName() { org.openmrs.Patient result = patientTranslator.toOpenmrsType(patient); assertThat(result.getGivenName(), equalTo(PATIENT_GIVEN_NAME)); assertThat(result.getFamilyName(), equalTo(PATIENT_FAMILY_NAME)); + assertThat(result.getPersonName().getUuid(), equalTo(PATIENT_NAME_UUID)); } @Test