diff --git a/omod-1.10/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_10/PersonResource1_10Test.java b/omod-1.10/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_10/PersonResource1_10Test.java index a6b5c7cb2..38eb05edd 100644 --- a/omod-1.10/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_10/PersonResource1_10Test.java +++ b/omod-1.10/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_10/PersonResource1_10Test.java @@ -35,7 +35,7 @@ public void validateFullRepresentation() throws Exception { @Override public String getDisplayProperty() { - return "Mr. Horatio Test Hornblower Esq."; + return "Horatio Test Hornblower"; } @Override diff --git a/omod-1.11/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_11/PersonResource1_11Test.java b/omod-1.11/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_11/PersonResource1_11Test.java index bfe6d999e..f54fad8c5 100644 --- a/omod-1.11/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_11/PersonResource1_11Test.java +++ b/omod-1.11/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_11/PersonResource1_11Test.java @@ -37,7 +37,7 @@ public void validateFullRepresentation() throws Exception { @Override public String getDisplayProperty() { - return "Mr. Horatio Test Hornblower"; + return "Horatio Test Hornblower"; } @Override diff --git a/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PersonResource1_8.java b/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PersonResource1_8.java index 15de940cc..261ed9167 100644 --- a/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PersonResource1_8.java +++ b/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PersonResource1_8.java @@ -9,6 +9,7 @@ */ package org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_8; +import java.lang.reflect.Method; import java.util.Arrays; import java.util.Date; import java.util.List; @@ -28,6 +29,8 @@ import org.openmrs.PersonAttribute; import org.openmrs.PersonName; import org.openmrs.api.context.Context; +import org.openmrs.layout.web.name.NameSupport; +import org.openmrs.layout.web.name.NameTemplate; import org.openmrs.module.webservices.rest.SimpleObject; import org.openmrs.module.webservices.rest.web.ConversionUtil; import org.openmrs.module.webservices.rest.web.RequestContext; @@ -465,8 +468,22 @@ public String getDisplayString(Person person) { // TODO copy what is done in PatientResource to use configured name layout if (person.getPersonName() == null) return ""; - - return person.getPersonName().getFullName(); + + PersonName personName = person.getPersonName(); + try { + NameTemplate nameTemplate = NameSupport.getInstance().getDefaultLayoutTemplate(); + + if (nameTemplate != null) { + // need to use reflection since the format method was not added until later versions of openmrs + Method format = NameTemplate.class.getDeclaredMethod("format", PersonName.class); + return (String) format.invoke(nameTemplate, personName); + } + } + catch (Exception e) { + // fall through to just returning full name if no format method found or format fails + } + + return personName.getFullName(); } private static void copyNameFields(PersonName existingName, PersonName personName) { diff --git a/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PatientResource1_8Test.java b/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PatientResource1_8Test.java index 9a8f6a71d..6a8400c10 100644 --- a/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PatientResource1_8Test.java +++ b/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PatientResource1_8Test.java @@ -61,7 +61,7 @@ public void validateFullRepresentation() throws Exception { @Override public String getDisplayProperty() { - return "Mr. Horatio Test Hornblower Esq."; + return "Horatio Test Hornblower"; } @Override diff --git a/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PersonResource1_8Test.java b/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PersonResource1_8Test.java index e00c3a0e4..051fee9b2 100644 --- a/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PersonResource1_8Test.java +++ b/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PersonResource1_8Test.java @@ -70,7 +70,7 @@ public void validateFullRepresentation() throws Exception { @Override public String getDisplayProperty() { - return "Mr. Horatio Test Hornblower Esq."; + return "Horatio Test Hornblower"; } @Override diff --git a/omod-1.9/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/SessionController1_9.java b/omod-1.9/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/SessionController1_9.java index e19e6d1fd..c199c228f 100644 --- a/omod-1.9/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/SessionController1_9.java +++ b/omod-1.9/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/SessionController1_9.java @@ -154,4 +154,4 @@ private Provider getCurrentProvider() { } return currentProvider; } -} +} \ No newline at end of file diff --git a/omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/SessionController1_9Test.java b/omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/SessionController1_9Test.java index 5ff929b84..60afacb4b 100644 --- a/omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/SessionController1_9Test.java +++ b/omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/SessionController1_9Test.java @@ -16,8 +16,11 @@ import org.junit.Test; import org.openmrs.GlobalProperty; import org.openmrs.Location; +import org.openmrs.PersonName; import org.openmrs.api.APIException; import org.openmrs.api.context.Context; +import org.openmrs.layout.web.name.NameSupport; +import org.openmrs.layout.web.name.NameTemplate; import org.openmrs.util.OpenmrsConstants; import org.openmrs.web.test.BaseModuleWebContextSensitiveTest; import org.springframework.mock.web.MockHttpServletRequest; @@ -30,31 +33,33 @@ import java.util.HashMap; import java.util.List; import java.util.Locale; +import java.util.Map; +@SuppressWarnings("unchecked") public class SessionController1_9Test extends BaseModuleWebContextSensitiveTest { - + private static final String SESSION_ID = "test-session-id"; - + private static final String UNKNOWN_LOCATION_UUID = "8d6c993e-c2cc-11de-8d13-0010c6dffd0f"; // Unknown Location - + private static final String XANADU_UUID = "9356400c-a5a2-4532-8f2b-2361b3446eb8"; // Xanadu - + private SessionController1_9 controller; - + private HttpServletRequest hsr; - + @Before public void before() { controller = Context.getRegisteredComponents(SessionController1_9.class).iterator().next(); // should only be 1 MockHttpServletRequest mockHsr = new MockHttpServletRequest(); mockHsr.setSession(new MockHttpSession(new MockServletContext(), SESSION_ID)); hsr = mockHsr; - + Context.getAdministrationService().saveGlobalProperty( new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST, "en_GB, sp, fr")); Context.getUserContext().setLocation(Context.getLocationService().getLocationByUuid(UNKNOWN_LOCATION_UUID)); } - + /** * @see SessionController1_9#delete(HttpServletRequest) * @verifies log the client out @@ -66,7 +71,7 @@ public void delete_shouldLogTheClientOut() throws Exception { Assert.assertFalse(Context.isAuthenticated()); Assert.assertNull(hsr.getSession(false)); } - + /** * @see SessionController1_9#get() * @verifies return the session id if the user is authenticated @@ -85,7 +90,7 @@ public void get_shouldReturnTheUserIfTheUserIsAuthenticated() throws Exception { Assert.assertEquals(Context.getAuthenticatedUser().getPerson().getUuid(), PropertyUtils.getProperty(personProp, "uuid")); } - + @Test public void get_shouldReturnLocaleInfoIfTheUserIsAuthenticated() throws Exception { Assert.assertTrue(Context.isAuthenticated()); @@ -94,7 +99,7 @@ public void get_shouldReturnLocaleInfoIfTheUserIsAuthenticated() throws Exceptio Assert.assertArrayEquals(Context.getAdministrationService().getAllowedLocales().toArray(), ((List) PropertyUtils.getProperty(ret, "allowedLocales")).toArray()); } - + @Test public void get_shouldReturnLocationIfTheUserIsAuthenticated() throws Exception { Assert.assertTrue(Context.isAuthenticated()); @@ -125,7 +130,32 @@ public void post_shouldReturnTheCurrentSession() throws Exception{ Assert.assertNotNull(currentProvider); Assert.assertTrue(currentProvider.toString().contains("Super User")); } - + + /** + * @see SessionController1_9#post(HttpServletRequest, Map) + * @verifies return the session with user.person.display formatted by nametemplate + */ + @Test + public void post_shouldReturnSessionUserPersonDisplayFormattedByNameTemplate() throws Exception { + executeDataSet("customTestDataset1_9.xml"); + + Context.logout(); + Context.authenticate("mujuzi", "test"); + Assert.assertTrue(Context.isAuthenticated()); + + String content = "{}"; + Object ret = controller.post(hsr, new ObjectMapper().readValue(content, HashMap.class)); + Object userProp = PropertyUtils.getProperty(ret, "user"); + Object personProp = PropertyUtils.getProperty(userProp, "person"); + Object displayProp = PropertyUtils.getProperty(personProp, "display"); + + PersonName personName = Context.getAuthenticatedUser().getPersonName(); + NameTemplate nameTemplate = NameSupport.getInstance().getDefaultLayoutTemplate(); + + Assert.assertEquals(displayProp, nameTemplate.format(personName)); + Assert.assertEquals(displayProp, "Moses Tusha Mujuzi"); + } + @Test public void post_shouldSetTheUserLocale() throws Exception { Locale newLocale = new Locale("sp"); @@ -137,21 +167,21 @@ public void post_shouldSetTheUserLocale() throws Exception { Assert.assertArrayEquals(Context.getAdministrationService().getAllowedLocales().toArray(), ((List) PropertyUtils.getProperty(ret, "allowedLocales")).toArray()); } - + @Test(expected = APIException.class) public void post_shouldFailWhenSettingIllegalLocale() throws Exception { String newLocale = "fOOb@r:"; String content = "{\"locale\":\"" + newLocale + "\"}"; controller.post(hsr, new ObjectMapper().readValue(content, HashMap.class)); } - + @Test(expected = APIException.class) public void post_shouldFailWhenSettingDisallowedLocale() throws Exception { String newLocale = "km_KH"; String content = "{\"locale\":\"" + newLocale + "\"}"; controller.post(hsr, new ObjectMapper().readValue(content, HashMap.class)); } - + @Test public void post_shouldSetTheSessionLocation() throws Exception { String content = "{\"sessionLocation\":\"" + XANADU_UUID + "\"}"; @@ -163,10 +193,10 @@ public void post_shouldSetTheSessionLocation() throws Exception { Assert.assertTrue(responseLoc.toString() + " should contain 'display=Xanadu'", responseLoc.toString().contains("display=Xanadu")); } - + @Test(expected = APIException.class) public void post_shouldFailWhenSettingNonexistantLocation() throws Exception { String content = "{\"sessionLocation\":\"fake-nonexistant-uuid\"}"; controller.post(hsr, new ObjectMapper().readValue(content, HashMap.class)); } -} +} \ No newline at end of file diff --git a/omod-1.9/src/test/resources/customTestDataset1_9.xml b/omod-1.9/src/test/resources/customTestDataset1_9.xml index e64953915..fca7ec8ed 100644 --- a/omod-1.9/src/test/resources/customTestDataset1_9.xml +++ b/omod-1.9/src/test/resources/customTestDataset1_9.xml @@ -41,4 +41,8 @@ + + + + diff --git a/omod-2.0/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_0/PersonResource2_0.java b/omod-2.0/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_0/PersonResource2_0.java new file mode 100644 index 000000000..1d0a3d617 --- /dev/null +++ b/omod-2.0/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_0/PersonResource2_0.java @@ -0,0 +1,47 @@ +/** + * 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.resource.openmrs2_0; + +import java.lang.reflect.Method; + +import org.openmrs.Person; +import org.openmrs.PersonName; +import org.openmrs.layout.name.NameSupport; +import org.openmrs.layout.name.NameTemplate; +import org.openmrs.module.webservices.rest.web.RestConstants; +import org.openmrs.module.webservices.rest.web.annotation.Resource; +import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_11.PersonResource1_11; + +@Resource(name = RestConstants.VERSION_1 + "/person", order = 0, supportedClass = Person.class, supportedOpenmrsVersions = { + "2.0.* - 2.1.*" }) +public class PersonResource2_0 extends PersonResource1_11 { + + @Override + public String getDisplayString(Person person) { + if (person.getPersonName() == null) + return ""; + + PersonName personName = person.getPersonName(); + try { + NameTemplate nameTemplate = NameSupport.getInstance().getDefaultLayoutTemplate(); + + if (nameTemplate != null) { + // need to use reflection since the format method was not added until later versions of openmrs + Method format = NameTemplate.class.getDeclaredMethod("format", PersonName.class); + return (String) format.invoke(nameTemplate, personName); + } + } + catch (Exception e) { + // fall through to just returning full name if no format method found or format fails + } + + return personName.getFullName(); + } +} diff --git a/omod-2.2/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_2/PersonResource2_2.java b/omod-2.2/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_2/PersonResource2_2.java index e96e51483..cdcde441a 100644 --- a/omod-2.2/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_2/PersonResource2_2.java +++ b/omod-2.2/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_2/PersonResource2_2.java @@ -17,14 +17,14 @@ import org.openmrs.module.webservices.rest.web.representation.Representation; import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource; import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription; -import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_11.PersonResource1_11; +import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs2_0.PersonResource2_0; /** * {@link Resource} for Person, supporting standard CRUD operations */ @Resource(name = RestConstants.VERSION_1 + "/person", supportedClass = Person.class, supportedOpenmrsVersions = { "2.2.* - 9.*" }) -public class PersonResource2_2 extends PersonResource1_11 { +public class PersonResource2_2 extends PersonResource2_0 { /** * @see DelegatingCrudResource#getRepresentationDescription(Representation)