Skip to content

Commit

Permalink
RESTWS-955: Fix adding support for locations as person attributes (#628)
Browse files Browse the repository at this point in the history
* RESTWS-955: Fix adding support for locations as person attributes

* use contenxt to access location service
  • Loading branch information
mherman22 authored Sep 25, 2024
1 parent ff28935 commit 0afba9e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 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,8 +15,10 @@
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.PersonService;
import org.openmrs.api.context.Context;
Expand All @@ -38,7 +40,7 @@ public class PersonAttributeController1_9Test extends MainResourceControllerTest
String personUuid = RestTestConstants1_8.PERSON_UUID;

private PersonService service;

@Before
public void before() throws Exception {
this.service = Context.getPersonService();
Expand Down Expand Up @@ -141,6 +143,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 = Context.getLocationService().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 0afba9e

Please sign in to comment.