From d23b7dd48dc12c84d2b20e9749547d2e85e2fd63 Mon Sep 17 00:00:00 2001 From: Darius Jazayeri Date: Wed, 30 Aug 2017 23:47:41 -0700 Subject: [PATCH] RESTWS-677 - Failure to change preferred address using a POST request to the REST API --- .../openmrs1_8/PersonAddressResource1_8.java | 11 +++++- .../PersonAddressController1_8Test.java | 39 ++++++++++++++++++- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PersonAddressResource1_8.java b/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PersonAddressResource1_8.java index 9a74cd322..6648ec756 100644 --- a/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PersonAddressResource1_8.java +++ b/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PersonAddressResource1_8.java @@ -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)) { @@ -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; diff --git a/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_8/PersonAddressController1_8Test.java b/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_8/PersonAddressController1_8Test.java index 12f6f3eb1..9b2a6b129 100644 --- a/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_8/PersonAddressController1_8Test.java +++ b/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_8/PersonAddressController1_8Test.java @@ -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; @@ -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"); @@ -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")) + ))); + } }