Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RESTWS-925: Session endpoint 'user.person.display' should respect the name template. #607

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void validateFullRepresentation() throws Exception {

@Override
public String getDisplayProperty() {
return "Mr. Horatio Test Hornblower Esq.";
return "Horatio Test Hornblower";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void validateFullRepresentation() throws Exception {

@Override
public String getDisplayProperty() {
return "Mr. Horatio Test Hornblower";
return "Horatio Test Hornblower";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void validateFullRepresentation() throws Exception {

@Override
public String getDisplayProperty() {
return "Mr. Horatio Test Hornblower Esq.";
return "Horatio Test Hornblower";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void validateFullRepresentation() throws Exception {

@Override
public String getDisplayProperty() {
return "Mr. Horatio Test Hornblower Esq.";
return "Horatio Test Hornblower";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,4 @@ private Provider getCurrentProvider() {
}
return currentProvider;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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());
Expand All @@ -94,7 +99,7 @@ public void get_shouldReturnLocaleInfoIfTheUserIsAuthenticated() throws Exceptio
Assert.assertArrayEquals(Context.getAdministrationService().getAllowedLocales().toArray(),
((List<Locale>) PropertyUtils.getProperty(ret, "allowedLocales")).toArray());
}

@Test
public void get_shouldReturnLocationIfTheUserIsAuthenticated() throws Exception {
Assert.assertTrue(Context.isAuthenticated());
Expand Down Expand Up @@ -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");
Expand All @@ -137,21 +167,21 @@ public void post_shouldSetTheUserLocale() throws Exception {
Assert.assertArrayEquals(Context.getAdministrationService().getAllowedLocales().toArray(),
((List<Locale>) 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 + "\"}";
Expand All @@ -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));
}
}
}
4 changes: 4 additions & 0 deletions omod-1.9/src/test/resources/customTestDataset1_9.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@
<encounter encounter_id="2" patient_id="2" location_id="10" form_id="1" encounter_datetime="2015-09-30 10:46:44.2"
creator="1" date_created="2015-09-30 10:44:45.2" voided="false" uuid="E8B79256-213C-4E2D-B812-D09D6C220273"/>

<person person_id="5501" gender="M" dead="false" creator="1" date_created="2014-01-09 00:00:00.0" voided="false" uuid="1716432a-4bf9-436c-baad-f03236e960e1"/>
<person_name person_name_id="5501" preferred="true" person_id="5501" prefix="Mr." given_name="Moses" middle_name="Tusha" family_name="Mujuzi" family_name_suffix="Jr" creator="1" date_created="2014-01-09 00:00:00.0" voided="false" uuid="54fdf8e8-1a80-45cf-a3fd-4fabd1b8ffab"/>
<users user_id="5501" person_id="5501" system_id="4-2" username="mujuzi" password="4a1750c8607d0fa237de36c6305715c223415189" salt="c788c6ad82a157b712392ca695dfcf2eed193d7f" creator="1" date_created="2014-01-09 00:00:00.0" retired="false" uuid="06d05314-e132-11de-babe-001e37123456"/>

</dataset>
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading