Skip to content

Commit

Permalink
O3-3586 - Trying to register a patient using a duplicate identifier s…
Browse files Browse the repository at this point in the history
…hould 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 <[email protected]>
  • Loading branch information
alaboso and Amos Laboso authored Jul 31, 2024
1 parent 70882a4 commit 2fb8658
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,10 @@ public PatientIdentifier save(PatientIdentifier delegate) {
}

if (needToAdd) {
service().savePatientIdentifier(delegate);
delegate.getPatient().addIdentifier(delegate);
}

service().savePatientIdentifier(delegate);

return delegate;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}

0 comments on commit 2fb8658

Please sign in to comment.