Skip to content

Commit 0afba9e

Browse files
authored
RESTWS-955: Fix adding support for locations as person attributes (#628)
* RESTWS-955: Fix adding support for locations as person attributes * use contenxt to access location service
1 parent ff28935 commit 0afba9e

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PersonAttributeResource1_8.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import org.openmrs.module.webservices.rest.web.response.ConversionException;
3939
import org.openmrs.module.webservices.rest.web.response.ResponseException;
4040
import org.openmrs.util.OpenmrsClassLoader;
41-
import java.util.UUID;
4241

4342
/**
4443
* {@link Resource} for PersonAttributes, supporting standard CRUD operations
@@ -104,7 +103,7 @@ public void setValue(PersonAttribute personAttribute, String value) {
104103
if (RestUtil.isValidUuid(value)) {
105104
Location location = Context.getLocationService().getLocationByUuid(value);
106105
if (location != null) {
107-
personAttribute.setValue(location.getUuid());
106+
personAttribute.setValue(location.getId().toString());
108107
return;
109108
}
110109
}

omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PersonAttributeResource1_8Test.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,16 @@ public void getDisplayString_shouldGetDisplayStringForLocation() {
114114
assertThat(displayString, equalToIgnoringCase("Unknown Location"));
115115
}
116116

117+
@Test
118+
public void getValue_shouldGetValueForPersonAttributeWhenLocationUuidIsSet() {
119+
Location location = locationService.getLocation(1);
120+
PersonAttribute attribute = new PersonAttribute();
121+
122+
resource.setValue(attribute, location.getUuid());
123+
124+
Assert.assertEquals(location.getId().toString(), resource.getValue(attribute));
125+
}
126+
117127
@Test
118128
public void getDisplayString_shouldGetDisplayStringForString() {
119129
// arrange
@@ -150,7 +160,7 @@ public void setValue_shouldSetProperAttributableIdIfFound() {
150160
Assert.assertNull(attribute.getValue());
151161

152162
resource.setValue(attribute, location.getUuid());
153-
Assert.assertEquals(location.getUuid(), attribute.getValue());
163+
Assert.assertEquals(location.getId().toString(), attribute.getValue());
154164
}
155165

156166
@Test

omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/PersonAttributeController1_9Test.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
import static org.junit.Assert.assertThat;
1616

1717
import org.codehaus.jackson.map.ObjectMapper;
18+
import org.junit.Assert;
1819
import org.junit.Before;
1920
import org.junit.Test;
21+
import org.openmrs.Location;
2022
import org.openmrs.PersonAttribute;
2123
import org.openmrs.api.PersonService;
2224
import org.openmrs.api.context.Context;
@@ -38,7 +40,7 @@ public class PersonAttributeController1_9Test extends MainResourceControllerTest
3840
String personUuid = RestTestConstants1_8.PERSON_UUID;
3941

4042
private PersonService service;
41-
43+
4244
@Before
4345
public void before() throws Exception {
4446
this.service = Context.getPersonService();
@@ -141,6 +143,26 @@ public void shouldSupportLocationPersonAttribute() throws Exception {
141143
assertThat((String) value.get("display"), is("Unknown Location"));
142144
assertThat(value.get("links"), is(notNullValue()));
143145
}
146+
147+
@Test
148+
public void shouldSupportLocationPersonAttributeBySettingUuidAsValue() throws Exception {
149+
String personAttributeTypeJson = "{\"name\": \"location\", \"description\": \"Points to a location\", \"format\": \"org.openmrs.Location\"}";
150+
SimpleObject personAttributeType = deserialize(handle(newPostRequest("personattributetype", personAttributeTypeJson)));
151+
String personAttributeTypeUuid = (String) personAttributeType.get("uuid");
152+
assertThat(personAttributeTypeUuid, is(notNullValue()));
153+
154+
Location location = Context.getLocationService().getLocation(1);
155+
156+
String personAttributeJson = "{ \"attributeType\":\"" + personAttributeTypeUuid + "\", \"value\":\"" + location.getUuid() + "\"}";
157+
SimpleObject personAttribute = deserialize(handle(newPostRequest(getURI(), personAttributeJson)));
158+
159+
Map<String, Object> value = personAttribute.get("value");
160+
161+
assertThat(value.get("uuid"), is(notNullValue()));
162+
Assert.assertEquals(value.get("uuid"), location.getUuid());
163+
assertThat((String) value.get("display"), is("Unknown Location"));
164+
assertThat(value.get("links"), is(notNullValue()));
165+
}
144166

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

0 commit comments

Comments
 (0)