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 3, 2017
1 parent 49ed4dc commit 8600a98
Show file tree
Hide file tree
Showing 2 changed files with 58 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 @@ -113,6 +113,49 @@ public void shouldEditAPatientIdentifier() throws Exception {
assertEquals(newLocationUuid, patientIdentifierType.getLocation().getUuid());
}

@Test
public void shouldEditAPatientIdentifierWithPreferredAttibuite() throws Exception {
PatientIdentifier patientIdentifier = service.getPatientIdentifierByUuid(getUuid());
Boolean originalPreferredStatus = patientIdentifier.getPreferred();
Boolean postPreferredStatus = !originalPreferredStatus;
SimpleObject postPatientIdentifier = new SimpleObject();
postPatientIdentifier.add("preferred", postPreferredStatus);

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

MockHttpServletRequest req = request(RequestMethod.POST, getURI() + "/" + getUuid());
req.setContent(json.getBytes());
handle(req);
assertNotEquals(originalPreferredStatus, patientIdentifier.getPreferred());
}

@Test
public void shouldUnsetOtherPreferredIdentifiers() throws Exception {
PatientIdentifier nonPreferred = service.getPatientIdentifierByUuid(getUuid());
long originalCount = getAllCount();

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));

assertNotNull(PropertyUtils.getProperty(newPatientIdentifier, "uuid"));
assertEquals(originalCount + 1, getAllCount());

String preferredUuid = (String) PropertyUtils.getProperty(newPatientIdentifier, "uuid");
PatientIdentifier preferredIdentifer = service.getPatientIdentifierByUuid(preferredUuid);
assertEquals(nonPreferred.isPreferred(), false);
assertEquals(preferredIdentifer.isPreferred(), true);
}

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

0 comments on commit 8600a98

Please sign in to comment.