Skip to content

Commit 4e1a095

Browse files
author
Amos Laboso
committed
O3-3586 - Adding test for - Trying to register a patient using a duplicate identifier should block the form from submitting any data
1 parent 5ee1534 commit 4e1a095

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/PatientIdentifierController1_9Test.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
import static org.junit.Assert.assertNotNull;
1616
import static org.junit.Assert.assertNull;
1717
import static org.junit.Assert.assertNotEquals;
18+
import static org.junit.Assert.fail;
1819

1920
import org.apache.commons.beanutils.PropertyUtils;
2021
import org.codehaus.jackson.map.ObjectMapper;
2122
import org.junit.Before;
2223
import org.junit.Test;
2324
import org.openmrs.PatientIdentifier;
25+
import org.openmrs.api.APIException;
2426
import org.openmrs.api.PatientService;
2527
import org.openmrs.api.context.Context;
2628
import org.openmrs.module.webservices.rest.SimpleObject;
@@ -189,4 +191,37 @@ public void shouldReturnTheAuditInfoForTheFullRepresentation() throws Exception
189191

190192
assertNotNull(PropertyUtils.getProperty(result, "auditInfo"));
191193
}
194+
195+
@Test
196+
public void shouldNotAddIdentifierInUseByAnotherPatient() throws Exception {
197+
SimpleObject patientIdentifier = new SimpleObject();
198+
patientIdentifier.add("identifier", "123456789qwerty");
199+
patientIdentifier.add("identifierType", "2f470aa8-1d73-43b7-81b5-01f0c0dfa53c");
200+
patientIdentifier.add("location", RestTestConstants1_8.LOCATION_UUID);
201+
202+
String json = new ObjectMapper().writeValueAsString(patientIdentifier);
203+
204+
MockHttpServletRequest req = request(RequestMethod.POST, getURI());
205+
req.setContent(json.getBytes());
206+
207+
SimpleObject newPatientIdentifier = deserialize(handle(req));
208+
209+
assertNotNull(PropertyUtils.getProperty(newPatientIdentifier, "uuid"));
210+
211+
final String OTHER_PATIENT_UUID = "5946f880-b197-400b-9caa-a3c661d23041";
212+
final String REQUEST_URI = "patient/" + OTHER_PATIENT_UUID + "/identifier";
213+
214+
int otherPatientActiveIdentifiersSize = service.getPatientByUuid(OTHER_PATIENT_UUID).getActiveIdentifiers()
215+
.size();
216+
req = request(RequestMethod.POST, REQUEST_URI);
217+
req.setContent(json.getBytes());
218+
try {
219+
handle(req);
220+
fail();
221+
} catch (Exception ex) {
222+
assertTrue(ex instanceof APIException);
223+
}
224+
assertEquals(otherPatientActiveIdentifiersSize, service.getPatientByUuid(OTHER_PATIENT_UUID)
225+
.getActiveIdentifiers().size());
226+
}
192227
}

0 commit comments

Comments
 (0)