|
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,61 @@ 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 | + } |
| 227 | + |
| 228 | + @Test |
| 229 | + public void shouldUpdateAnExistingPatientIdentifier() throws Exception { |
| 230 | + final String patientIdentifierNewValue = "omrs12-34-00"; |
| 231 | + PatientIdentifier patientIdentifier = service.getPatientIdentifierByUuid(getUuid()); |
| 232 | + final String patientIdentifierUuidThatShouldNotChange = patientIdentifier.getUuid(); |
| 233 | + |
| 234 | + assertFalse(patientIdentifierNewValue.equals(patientIdentifier.getIdentifier())); |
| 235 | + |
| 236 | + SimpleObject simpleObject = new SimpleObject(); |
| 237 | + simpleObject.add("identifier", patientIdentifierNewValue); |
| 238 | + String json = new ObjectMapper().writeValueAsString(simpleObject); |
| 239 | + |
| 240 | + MockHttpServletRequest req = request(RequestMethod.POST, getURI() + "/" + getUuid()); |
| 241 | + req.setContent(json.getBytes()); |
| 242 | + |
| 243 | + SimpleObject updatedPatientIdentifier = deserialize(handle(req)); |
| 244 | + Object uuid = PropertyUtils.getProperty(updatedPatientIdentifier, "uuid"); |
| 245 | + Object identifierValue = PropertyUtils.getProperty(updatedPatientIdentifier, "identifier"); |
| 246 | + |
| 247 | + assertEquals(patientIdentifierUuidThatShouldNotChange, uuid); |
| 248 | + assertEquals(patientIdentifierNewValue, identifierValue); |
| 249 | + assertEquals(patientIdentifierNewValue, patientIdentifier.getIdentifier()); |
| 250 | + } |
192 | 251 | }
|
0 commit comments