Skip to content

Commit

Permalink
[Bug]Failure to change preferred name, or identifier using a POST req…
Browse files Browse the repository at this point in the history
…uest to the REST API
  • Loading branch information
chibujax committed Oct 4, 2017
1 parent 49ed4dc commit f84b5e9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -214,17 +214,29 @@ public PatientIdentifier getByUniqueId(String uniqueId) {
*/
@Override
public PatientIdentifier save(PatientIdentifier delegate) {
// make sure it has already been added to the patient
if (delegate.isPreferred()) {
for (PatientIdentifier pI : delegate.getPatient().getActiveIdentifiers()) {
if (!pI.equals(delegate)) {
pI.setPreferred(false);
}
}
}

boolean needToAdd = true;
for (PatientIdentifier pi : delegate.getPatient().getActiveIdentifiers()) {
if (pi.equals(delegate)) {
needToAdd = false;
break;
}
}
if (needToAdd)

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

service().savePatientIdentifier(delegate);
return delegate;

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
package org.openmrs.module.webservices.rest.web.v1_0.controller.openmrs1_9;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
Expand Down Expand Up @@ -113,6 +114,33 @@ public void shouldEditAPatientIdentifier() throws Exception {
assertEquals(newLocationUuid, patientIdentifierType.getLocation().getUuid());
}

@Test
public void shouldUnsetOtherPreferredIdentifiers() throws Exception {
PatientIdentifier nonPreferred = service.getPatientIdentifierByUuid(getUuid());
long originalCount = getAllCount();
assertTrue(nonPreferred.isPreferred());
SimpleObject patientIdentifier = new SimpleObject();
patientIdentifier.add("identifier", "abc123ez");
patientIdentifier.add("identifierType", "2f470aa8-1d73-43b7-81b5-01f0c0dfa53c");
patientIdentifier.add("location", RestTestConstants1_8.LOCATION_UUID);
patientIdentifier.add("preferred", true);

String json = new ObjectMapper().writeValueAsString(patientIdentifier);

MockHttpServletRequest req = request(RequestMethod.POST, getURI());
req.setContent(json.getBytes());

SimpleObject newPatientIdentifier = deserialize(handle(req));
Object uuid = PropertyUtils.getProperty(newPatientIdentifier, "uuid");
assertNotNull(uuid);
assertEquals(originalCount + 1, getAllCount());

PatientIdentifier preferredIdentifer = service.getPatientIdentifierByUuid(uuid.toString());
assertFalse(nonPreferred.isPreferred());
assertTrue(preferredIdentifer.isPreferred());
assertEquals(preferredIdentifer.getIdentifier(), "abc123ez");
}

@Test
public void shouldVoidAPatientIdentifier() throws Exception {
assertEquals(false, service.getPatientIdentifierByUuid(getUuid()).isVoided());
Expand Down

0 comments on commit f84b5e9

Please sign in to comment.