|
15 | 15 | import static org.junit.Assert.assertNotNull;
|
16 | 16 | import static org.junit.Assert.assertNull;
|
17 | 17 | import static org.junit.Assert.assertNotEquals;
|
| 18 | +import static org.junit.Assert.fail; |
18 | 19 |
|
19 | 20 | import org.apache.commons.beanutils.PropertyUtils;
|
20 | 21 | import org.codehaus.jackson.map.ObjectMapper;
|
21 | 22 | import org.junit.Before;
|
22 | 23 | import org.junit.Test;
|
23 | 24 | import org.openmrs.PatientIdentifier;
|
| 25 | +import org.openmrs.api.APIException; |
24 | 26 | import org.openmrs.api.PatientService;
|
25 | 27 | import org.openmrs.api.context.Context;
|
26 | 28 | import org.openmrs.module.webservices.rest.SimpleObject;
|
@@ -189,4 +191,37 @@ public void shouldReturnTheAuditInfoForTheFullRepresentation() throws Exception
|
189 | 191 |
|
190 | 192 | assertNotNull(PropertyUtils.getProperty(result, "auditInfo"));
|
191 | 193 | }
|
| 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 | + } |
192 | 227 | }
|
0 commit comments