Skip to content

Commit

Permalink
RESTWS-677 - Failure to change preferred address using a POST request…
Browse files Browse the repository at this point in the history
… to the REST API
  • Loading branch information
djazayeri committed Aug 31, 2017
1 parent c165168 commit d23b7dd
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ public void purge(PersonAddress address, RequestContext context) throws Response
*/
@Override
public PersonAddress save(PersonAddress newAddress) {
// make sure that the name has actually been added to the person
// make sure that the address has actually been added to the person
boolean needToAdd = true;
for (PersonAddress pa : newAddress.getPerson().getAddresses()) {
if (pa.equals(newAddress)) {
Expand All @@ -248,6 +248,15 @@ public PersonAddress save(PersonAddress newAddress) {
newAddress.getPerson().addAddress(newAddress);
}

// if this address is marked preferred, then we need to clear any others that are marked as preferred
if (newAddress.isPreferred()) {
for (PersonAddress pa : newAddress.getPerson().getAddresses()) {
if (!pa.equals(newAddress)) {
pa.setPreferred(false);
}
}
}

Context.getPersonService().savePerson(newAddress.getPerson());

return newAddress;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@
*/
package org.openmrs.module.webservices.rest.web.v1_0.controller.openmrs1_8;

import static org.hamcrest.beans.HasPropertyWithValue.hasProperty;
import static org.hamcrest.core.AllOf.allOf;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsCollectionContaining.hasItem;
import static org.junit.Assert.assertThat;

import org.apache.commons.beanutils.PropertyUtils;
import org.hamcrest.Matcher;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -139,7 +146,7 @@ public void shouldAddAnAddressToAPerson() throws Exception {
}

@Test
public void shouldEditIAnAddress() throws Exception {
public void shouldEditAnAddress() throws Exception {

SimpleObject address = new SimpleObject();
address.add("address1", "new address1");
Expand Down Expand Up @@ -179,4 +186,34 @@ public void shouldPurgeAPersonAddress() throws Exception {
Assert.assertNull(address);
}

@Test
public void shouldSetPreferred() throws Exception {
PersonAddress nonPreferred = service.getPersonAddressByUuid(getUuid());
Person person = nonPreferred.getPerson();

PersonAddress preferred = new PersonAddress();
preferred.setCityVillage("Seattle");
preferred.setPreferred(true);
person.addAddress(preferred);

service.savePerson(person);

// sanity check
assertThat(nonPreferred.isPreferred(), is(false));
assertThat(preferred.isPreferred(), is(true));

SimpleObject address = new SimpleObject();
address.add("preferred", "true");

MockHttpServletRequest req = newPostRequest(getURI() + "/" + getUuid(), address);
handle(req);

PersonAddress updated = service.getPersonAddressByUuid(getUuid());
assertThat(updated.isPreferred(), is(true));
// the one that was originally preferred should now not be preferred
assertThat(updated.getPerson().getAddresses(), (Matcher) hasItem(allOf(
hasProperty("preferred", is(false)),
hasProperty("cityVillage", is("Seattle"))
)));
}
}

0 comments on commit d23b7dd

Please sign in to comment.