From 2fb8658c97555e117f0cabec146eb9697310e1ce Mon Sep 17 00:00:00 2001 From: Amos Laboso Date: Wed, 31 Jul 2024 19:06:39 +0300 Subject: [PATCH] O3-3586 - Trying to register a patient using a duplicate identifier should block the form from submitting any data (#615) * O3-3586 - Trying to register a patient using a duplicate identifier should block the form from submitting any data * O3-3586 - Adding test for - Trying to register a patient using a duplicate identifier should block the form from submitting any data * O3-3586 - Adding a new test to ensure shouldUpdateAnExistingPatientIdentifier - Trying to register a patient using a duplicate identifier should block the form from submitting any data --------- Co-authored-by: Amos Laboso --- .../PatientIdentifierResource1_8.java | 4 +- .../PatientIdentifierController1_9Test.java | 59 +++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PatientIdentifierResource1_8.java b/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PatientIdentifierResource1_8.java index 55927a9b7..34f24c837 100644 --- a/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PatientIdentifierResource1_8.java +++ b/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PatientIdentifierResource1_8.java @@ -231,10 +231,10 @@ public PatientIdentifier save(PatientIdentifier delegate) { } if (needToAdd) { + service().savePatientIdentifier(delegate); delegate.getPatient().addIdentifier(delegate); } - - service().savePatientIdentifier(delegate); + return delegate; } diff --git a/omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/PatientIdentifierController1_9Test.java b/omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/PatientIdentifierController1_9Test.java index e76664559..cabdf9d8a 100644 --- a/omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/PatientIdentifierController1_9Test.java +++ b/omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/PatientIdentifierController1_9Test.java @@ -15,12 +15,14 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.fail; import org.apache.commons.beanutils.PropertyUtils; import org.codehaus.jackson.map.ObjectMapper; import org.junit.Before; import org.junit.Test; import org.openmrs.PatientIdentifier; +import org.openmrs.api.APIException; import org.openmrs.api.PatientService; import org.openmrs.api.context.Context; import org.openmrs.module.webservices.rest.SimpleObject; @@ -189,4 +191,61 @@ public void shouldReturnTheAuditInfoForTheFullRepresentation() throws Exception assertNotNull(PropertyUtils.getProperty(result, "auditInfo")); } + + @Test + public void shouldNotAddIdentifierInUseByAnotherPatient() throws Exception { + SimpleObject patientIdentifier = new SimpleObject(); + patientIdentifier.add("identifier", "123456789qwerty"); + patientIdentifier.add("identifierType", "2f470aa8-1d73-43b7-81b5-01f0c0dfa53c"); + patientIdentifier.add("location", RestTestConstants1_8.LOCATION_UUID); + + String json = new ObjectMapper().writeValueAsString(patientIdentifier); + + MockHttpServletRequest req = request(RequestMethod.POST, getURI()); + req.setContent(json.getBytes()); + + SimpleObject newPatientIdentifier = deserialize(handle(req)); + + assertNotNull(PropertyUtils.getProperty(newPatientIdentifier, "uuid")); + + final String OTHER_PATIENT_UUID = "5946f880-b197-400b-9caa-a3c661d23041"; + final String REQUEST_URI = "patient/" + OTHER_PATIENT_UUID + "/identifier"; + + int otherPatientActiveIdentifiersSize = service.getPatientByUuid(OTHER_PATIENT_UUID).getActiveIdentifiers() + .size(); + req = request(RequestMethod.POST, REQUEST_URI); + req.setContent(json.getBytes()); + try { + handle(req); + fail(); + } catch (Exception ex) { + assertTrue(ex instanceof APIException); + } + assertEquals(otherPatientActiveIdentifiersSize, service.getPatientByUuid(OTHER_PATIENT_UUID) + .getActiveIdentifiers().size()); + } + + @Test + public void shouldUpdateAnExistingPatientIdentifier() throws Exception { + final String patientIdentifierNewValue = "omrs12-34-00"; + PatientIdentifier patientIdentifier = service.getPatientIdentifierByUuid(getUuid()); + final String patientIdentifierUuidThatShouldNotChange = patientIdentifier.getUuid(); + + assertFalse(patientIdentifierNewValue.equals(patientIdentifier.getIdentifier())); + + SimpleObject simpleObject = new SimpleObject(); + simpleObject.add("identifier", patientIdentifierNewValue); + String json = new ObjectMapper().writeValueAsString(simpleObject); + + MockHttpServletRequest req = request(RequestMethod.POST, getURI() + "/" + getUuid()); + req.setContent(json.getBytes()); + + SimpleObject updatedPatientIdentifier = deserialize(handle(req)); + Object uuid = PropertyUtils.getProperty(updatedPatientIdentifier, "uuid"); + Object identifierValue = PropertyUtils.getProperty(updatedPatientIdentifier, "identifier"); + + assertEquals(patientIdentifierUuidThatShouldNotChange, uuid); + assertEquals(patientIdentifierNewValue, identifierValue); + assertEquals(patientIdentifierNewValue, patientIdentifier.getIdentifier()); + } }