Skip to content

Commit

Permalink
RESTWS-955: Fix adding support for locations as person attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
mherman22 committed Sep 25, 2024
1 parent eb0b5d7 commit fac48d5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import org.openmrs.module.webservices.rest.web.response.ConversionException;
import org.openmrs.module.webservices.rest.web.response.ResponseException;
import org.openmrs.util.OpenmrsClassLoader;
import java.util.UUID;

/**
* {@link Resource} for PersonAttributes, supporting standard CRUD operations
Expand Down Expand Up @@ -104,7 +103,7 @@ public void setValue(PersonAttribute personAttribute, String value) {
if (RestUtil.isValidUuid(value)) {
Location location = Context.getLocationService().getLocationByUuid(value);
if (location != null) {
personAttribute.setValue(location.getUuid());
personAttribute.setValue(location.getId().toString());
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,16 @@ public void getDisplayString_shouldGetDisplayStringForLocation() {
assertThat(displayString, equalToIgnoringCase("Unknown Location"));
}

@Test
public void getValue_shouldGetValueForPersonAttributeWhenLocationUuidIsSet() {
Location location = locationService.getLocation(1);
PersonAttribute attribute = new PersonAttribute();

resource.setValue(attribute, location.getUuid());

Assert.assertEquals(location.getId().toString(), resource.getValue(attribute));
}

@Test
public void getDisplayString_shouldGetDisplayStringForString() {
// arrange
Expand Down Expand Up @@ -150,7 +160,7 @@ public void setValue_shouldSetProperAttributableIdIfFound() {
Assert.assertNull(attribute.getValue());

resource.setValue(attribute, location.getUuid());
Assert.assertEquals(location.getUuid(), attribute.getValue());
Assert.assertEquals(location.getId().toString(), attribute.getValue());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
import static org.junit.Assert.assertThat;

import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openmrs.Location;
import org.openmrs.PersonAttribute;
import org.openmrs.api.LocationService;
import org.openmrs.api.PersonService;
import org.openmrs.api.context.Context;
import org.openmrs.module.webservices.rest.SimpleObject;
Expand All @@ -38,10 +41,13 @@ public class PersonAttributeController1_9Test extends MainResourceControllerTest
String personUuid = RestTestConstants1_8.PERSON_UUID;

private PersonService service;

private LocationService locationService;

@Before
public void before() throws Exception {
this.service = Context.getPersonService();
this.locationService = Context.getLocationService();
}

@Test
Expand Down Expand Up @@ -141,6 +147,26 @@ public void shouldSupportLocationPersonAttribute() throws Exception {
assertThat((String) value.get("display"), is("Unknown Location"));
assertThat(value.get("links"), is(notNullValue()));
}

@Test
public void shouldSupportLocationPersonAttributeBySettingUuidAsValue() throws Exception {
String personAttributeTypeJson = "{\"name\": \"location\", \"description\": \"Points to a location\", \"format\": \"org.openmrs.Location\"}";
SimpleObject personAttributeType = deserialize(handle(newPostRequest("personattributetype", personAttributeTypeJson)));
String personAttributeTypeUuid = (String) personAttributeType.get("uuid");
assertThat(personAttributeTypeUuid, is(notNullValue()));

Location location = locationService.getLocation(1);

String personAttributeJson = "{ \"attributeType\":\"" + personAttributeTypeUuid + "\", \"value\":\"" + location.getUuid() + "\"}";
SimpleObject personAttribute = deserialize(handle(newPostRequest(getURI(), personAttributeJson)));

Map<String, Object> value = personAttribute.get("value");

assertThat(value.get("uuid"), is(notNullValue()));
Assert.assertEquals(value.get("uuid"), location.getUuid());
assertThat((String) value.get("display"), is("Unknown Location"));
assertThat(value.get("links"), is(notNullValue()));
}

/**
* @see org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceControllerTest#getURI()
Expand Down

0 comments on commit fac48d5

Please sign in to comment.