From 5006c2afa9391458789d18eb11a5a61cf269200d Mon Sep 17 00:00:00 2001 From: IamMujuziMoses Date: Thu, 29 May 2025 14:15:52 +0300 Subject: [PATCH 1/2] RESTWS-677: Failure to change preferred name, or identifier using a POST request to the REST API --- .../PatientIdentifierResource1_8.java | 5 +- .../openmrs1_8/PersonNameResource1_8.java | 17 ++- .../PersonNameController1_8Test.java | 124 ++++++++++++++++++ .../PatientIdentifierController1_9Test.java | 60 +++++++++ ...reatePatientIdentifierResource1_9Test.java | 6 +- 5 files changed, 206 insertions(+), 6 deletions(-) create mode 100644 omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_8/PersonNameController1_8Test.java diff --git a/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PatientIdentifierResource1_8.java b/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PatientIdentifierResource1_8.java index e9692afcf..4b58f2df8 100644 --- a/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PatientIdentifierResource1_8.java +++ b/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PatientIdentifierResource1_8.java @@ -221,7 +221,7 @@ public PatientIdentifier save(PatientIdentifier delegate) { } } } - + boolean needToAdd = true; for (PatientIdentifier pi : delegate.getPatient().getActiveIdentifiers()) { if (pi.equals(delegate)) { @@ -235,9 +235,10 @@ public PatientIdentifier save(PatientIdentifier delegate) { if (needToAdd) { delegate.getPatient().addIdentifier(delegate); } + // make sure that the identifier has actually been added to the patient + Context.getPatientService().savePatient(delegate.getPatient()); return delegate; - } @Override diff --git a/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PersonNameResource1_8.java b/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PersonNameResource1_8.java index 171c10cd7..7e4c1f106 100644 --- a/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PersonNameResource1_8.java +++ b/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PersonNameResource1_8.java @@ -53,6 +53,7 @@ public DelegatingResourceDescription getRepresentationDescription(Representation description.addProperty("middleName"); description.addProperty("familyName"); description.addProperty("familyName2"); + description.addProperty("preferred"); description.addProperty("voided"); description.addSelfLink(); description.addLink("full", ".?v=" + RestConstants.REPRESENTATION_FULL); @@ -120,7 +121,7 @@ public Model getGETModel(Representation rep) { } if (rep instanceof FullRepresentation) { model - .property("preferred", new BooleanProperty()) + .property("preferred", new BooleanProperty()._default(false)) .property("prefix", new StringProperty()) .property("familyNamePrefix", new StringProperty()) .property("familyNameSuffix", new StringProperty()) @@ -224,9 +225,21 @@ public PersonName save(PersonName newName) { break; } } - if (needToAdd) + if (needToAdd) { newName.getPerson().addName(newName); + } + + // if this name is marked as preferred, then we need to clear any others that are marked as preferred + if (newName.isPreferred()) { + for (PersonName pn : newName.getPerson().getNames()) { + if (!pn.equals(newName)) { + pn.setPreferred(false); + } + } + } + Context.getPersonService().savePerson(newName.getPerson()); + return newName; } diff --git a/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_8/PersonNameController1_8Test.java b/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_8/PersonNameController1_8Test.java new file mode 100644 index 000000000..921131780 --- /dev/null +++ b/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_8/PersonNameController1_8Test.java @@ -0,0 +1,124 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + * + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ +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.codehaus.jackson.map.ObjectMapper; +import org.hamcrest.Matcher; +import org.junit.Before; +import org.junit.Test; +import org.openmrs.PatientIdentifier; +import org.openmrs.Person; +import org.openmrs.PersonName; +import org.openmrs.api.PersonService; +import org.openmrs.api.context.Context; +import org.openmrs.module.webservices.rest.SimpleObject; +import org.openmrs.module.webservices.rest.web.RestTestConstants1_8; +import org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceControllerTest; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.web.bind.annotation.RequestMethod; + +public class PersonNameController1_8Test extends MainResourceControllerTest { + + private PersonService service; + + @Before + public void before() throws Exception { + executeDataSet("PersonControllerTest-otherPersonData.xml"); + this.service = Context.getPersonService(); + } + + /** + * @see org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceControllerTest#getURI() + */ + @Override + public String getURI() { + return "person" + "/" + RestTestConstants1_8.PERSON_UUID + "/name"; + } + + /** + * @see org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceControllerTest#getAllCount() + */ + @Override + public long getAllCount() { + int count = 0; + + Person person = service.getPersonByUuid(RestTestConstants1_8.PERSON_UUID); + + for (PersonName name : person.getNames()) { + if (!name.isVoided()) { + count++; + } + } + + return count; + } + + /** + * @see org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceControllerTest#getUuid() + */ + @Override + public String getUuid() { + return RestTestConstants1_8.PERSON_NAME_UUID; + } + + @Test + public void shouldSetPreferred() throws Exception { + PersonName nonPreferred = service.getPersonNameByUuid(getUuid()); + Person person = nonPreferred.getPerson(); + assertThat(nonPreferred.isPreferred(), is(true)); + + SimpleObject newName= new SimpleObject(); + newName.add("givenName", "Moses"); + newName.add("familyName", "Mujuzi"); + newName.add("preferred", "true"); + + String json = new ObjectMapper().writeValueAsString(newName); + + MockHttpServletRequest req = request(RequestMethod.POST, getURI()); + req.setContent(json.getBytes()); + + SimpleObject result = deserialize(handle(req)); + Object uuid = PropertyUtils.getProperty(result, "uuid"); + PersonName preferred = service.getPersonNameByUuid(uuid.toString()); + + // sanity check + assertThat(nonPreferred.isPreferred(), is(false)); + assertThat(preferred.isPreferred(), is(true)); + + // check before change + assertThat(person.getNames(), (Matcher) hasItem(allOf( + hasProperty("preferred", is(true)), + hasProperty("givenName", is("Moses")) + ))); + + // change non preferred to preferred + SimpleObject updateToTrue = new SimpleObject(); + updateToTrue.add("preferred", "true"); + + String updateJson = new ObjectMapper().writeValueAsString(updateToTrue); + + MockHttpServletRequest req1 = request(RequestMethod.POST, getURI() + "/" + nonPreferred.getUuid()); + req1.setContent(updateJson.getBytes()); + handle(req1); + + // check after change + assertThat(person.getNames(), (Matcher) hasItem(allOf( + hasProperty("preferred", is(false)), + hasProperty("givenName", is("Moses")) + ))); + } +} diff --git a/omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/PatientIdentifierController1_9Test.java b/omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/PatientIdentifierController1_9Test.java index c5eb3aa4e..215056aad 100644 --- a/omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/PatientIdentifierController1_9Test.java +++ b/omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/PatientIdentifierController1_9Test.java @@ -9,7 +9,12 @@ */ package org.openmrs.module.webservices.rest.web.v1_0.controller.openmrs1_9; +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.assertEquals; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; @@ -17,11 +22,17 @@ import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.fail; +import java.util.Set; + import org.apache.commons.beanutils.PropertyUtils; import org.codehaus.jackson.map.ObjectMapper; +import org.hamcrest.Matcher; import org.junit.Before; import org.junit.Test; +import org.openmrs.Patient; import org.openmrs.PatientIdentifier; +import org.openmrs.PatientIdentifierType; +import org.openmrs.PersonName; import org.openmrs.api.APIException; import org.openmrs.api.PatientService; import org.openmrs.api.context.Context; @@ -30,6 +41,7 @@ import org.openmrs.module.webservices.rest.web.RestConstants; import org.openmrs.module.webservices.rest.web.RestTestConstants1_8; import org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceControllerTest; +import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_8.PatientIdentifierResource1_8; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.web.bind.annotation.RequestMethod; @@ -248,4 +260,52 @@ public void shouldUpdateAnExistingPatientIdentifier() throws Exception { assertEquals(patientIdentifierNewValue, identifierValue); assertEquals(patientIdentifierNewValue, patientIdentifier.getIdentifier()); } + + @Test + public void shouldSetPreferred() throws Exception { + PatientIdentifier nonPreferred = service.getPatientIdentifierByUuid(getUuid()); + Patient patient = nonPreferred.getPatient(); + assertThat(nonPreferred.isPreferred(), is(true)); + + SimpleObject newIdentifier = new SimpleObject(); + newIdentifier.add("identifier", "OMRS303"); + newIdentifier.add("identifierType", "2f470aa8-1d73-43b7-81b5-01f0c0dfa53c"); + newIdentifier.add("location", RestTestConstants1_8.LOCATION_UUID); + newIdentifier.add("preferred", "true"); + + String json = new ObjectMapper().writeValueAsString(newIdentifier); + + MockHttpServletRequest req = request(RequestMethod.POST, "patient/" + patient.getUuid() + "/identifier"); + req.setContent(json.getBytes()); + + SimpleObject result = deserialize(handle(req)); + Object uuid = PropertyUtils.getProperty(result, "uuid"); + PatientIdentifier preferred = service.getPatientIdentifierByUuid(uuid.toString()); + + // sanity check + assertThat(nonPreferred.isPreferred(), is(false)); + assertThat(preferred.isPreferred(), is(true)); + + // check before change + assertThat(patient.getIdentifiers(), (Matcher) hasItem(allOf( + hasProperty("preferred", is(true)), + hasProperty("identifier", is("OMRS303")) + ))); + + // change non preferred to preferred + SimpleObject updateToTrue = new SimpleObject(); + updateToTrue.add("preferred", "true"); + + String updateJson = new ObjectMapper().writeValueAsString(updateToTrue); + + MockHttpServletRequest req1 = request(RequestMethod.POST, "patient/" + patient.getUuid() + "/identifier/" + nonPreferred.getUuid()); + req1.setContent(updateJson.getBytes()); + handle(req1); + + // check after change + assertThat(patient.getIdentifiers(), (Matcher) hasItem(allOf( + hasProperty("preferred", is(false)), + hasProperty("identifier", is("OMRS303")) + ))); + } } diff --git a/omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_9/CreatePatientIdentifierResource1_9Test.java b/omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_9/CreatePatientIdentifierResource1_9Test.java index 7f305d476..2cb36f219 100644 --- a/omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_9/CreatePatientIdentifierResource1_9Test.java +++ b/omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_9/CreatePatientIdentifierResource1_9Test.java @@ -39,7 +39,8 @@ public void shouldCreatePatientIdentifier_WhenTypeIsSpecifiedByUuid() throws Exc String personAttributeJson = "{" + " \"identifier\": \"101-6\"," + " \"identifierType\": {" + " \"uuid\" : \"1a339fe9-38bc-4ab3-b180-320988c0b968\"" + " }," + " \"location\" : {" + " \"uuid\" : \"8d6c993e-c2cc-11de-8d13-0010c6dffd0f\"" - + " }," + " \"preferred\": true" + " }"; + + " }" + + " }"; SimpleObject personAttributeSimpleObject = new SimpleObject(); personAttributeSimpleObject.putAll(new ObjectMapper().readValue(personAttributeJson, HashMap.class)); @@ -54,7 +55,8 @@ public void shouldCreatePatientIdentifier_WhenTypeIsSpecifiedByName() throws Exc String personAttributeJson = "{" + " \"identifier\": \"101-6\"," + " \"identifierType\": {" + " \"name\" : \"OpenMRS Identification Number\"" + " }," + " \"location\" : {" + " \"uuid\" : \"8d6c993e-c2cc-11de-8d13-0010c6dffd0f\"" - + " }," + " \"preferred\": true" + " }"; + + " }" + + " }"; SimpleObject personAttributeSimpleObject = new SimpleObject(); personAttributeSimpleObject.putAll(new ObjectMapper().readValue(personAttributeJson, HashMap.class)); From cb65e5aa304e7891eb857eca933231d0cbb94601 Mon Sep 17 00:00:00 2001 From: IamMujuziMoses Date: Thu, 29 May 2025 19:35:23 +0300 Subject: [PATCH 2/2] RESTWS-677: Failure to change preferred name, or identifier using a POST request to the REST API --- .../controller/openmrs1_8/PersonNameController1_8Test.java | 1 - .../openmrs1_9/PatientIdentifierController1_9Test.java | 5 ----- .../openmrs1_9/CreatePatientIdentifierResource1_9Test.java | 6 ++---- 3 files changed, 2 insertions(+), 10 deletions(-) diff --git a/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_8/PersonNameController1_8Test.java b/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_8/PersonNameController1_8Test.java index 921131780..0ce5368b9 100644 --- a/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_8/PersonNameController1_8Test.java +++ b/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_8/PersonNameController1_8Test.java @@ -20,7 +20,6 @@ import org.hamcrest.Matcher; import org.junit.Before; import org.junit.Test; -import org.openmrs.PatientIdentifier; import org.openmrs.Person; import org.openmrs.PersonName; import org.openmrs.api.PersonService; diff --git a/omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/PatientIdentifierController1_9Test.java b/omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/PatientIdentifierController1_9Test.java index 215056aad..336050050 100644 --- a/omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/PatientIdentifierController1_9Test.java +++ b/omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/PatientIdentifierController1_9Test.java @@ -22,8 +22,6 @@ import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.fail; -import java.util.Set; - import org.apache.commons.beanutils.PropertyUtils; import org.codehaus.jackson.map.ObjectMapper; import org.hamcrest.Matcher; @@ -31,8 +29,6 @@ import org.junit.Test; import org.openmrs.Patient; import org.openmrs.PatientIdentifier; -import org.openmrs.PatientIdentifierType; -import org.openmrs.PersonName; import org.openmrs.api.APIException; import org.openmrs.api.PatientService; import org.openmrs.api.context.Context; @@ -41,7 +37,6 @@ import org.openmrs.module.webservices.rest.web.RestConstants; import org.openmrs.module.webservices.rest.web.RestTestConstants1_8; import org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceControllerTest; -import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_8.PatientIdentifierResource1_8; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.web.bind.annotation.RequestMethod; diff --git a/omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_9/CreatePatientIdentifierResource1_9Test.java b/omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_9/CreatePatientIdentifierResource1_9Test.java index 2cb36f219..af7c5b9ca 100644 --- a/omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_9/CreatePatientIdentifierResource1_9Test.java +++ b/omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_9/CreatePatientIdentifierResource1_9Test.java @@ -39,8 +39,7 @@ public void shouldCreatePatientIdentifier_WhenTypeIsSpecifiedByUuid() throws Exc String personAttributeJson = "{" + " \"identifier\": \"101-6\"," + " \"identifierType\": {" + " \"uuid\" : \"1a339fe9-38bc-4ab3-b180-320988c0b968\"" + " }," + " \"location\" : {" + " \"uuid\" : \"8d6c993e-c2cc-11de-8d13-0010c6dffd0f\"" - + " }" - + " }"; + + " }}"; SimpleObject personAttributeSimpleObject = new SimpleObject(); personAttributeSimpleObject.putAll(new ObjectMapper().readValue(personAttributeJson, HashMap.class)); @@ -55,8 +54,7 @@ public void shouldCreatePatientIdentifier_WhenTypeIsSpecifiedByName() throws Exc String personAttributeJson = "{" + " \"identifier\": \"101-6\"," + " \"identifierType\": {" + " \"name\" : \"OpenMRS Identification Number\"" + " }," + " \"location\" : {" + " \"uuid\" : \"8d6c993e-c2cc-11de-8d13-0010c6dffd0f\"" - + " }" - + " }"; + + " }}"; SimpleObject personAttributeSimpleObject = new SimpleObject(); personAttributeSimpleObject.putAll(new ObjectMapper().readValue(personAttributeJson, HashMap.class));