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-955: Fix adding support for locations as person attributes #628

Merged
merged 2 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are using it in only one place, you may just directly do Context.getLocationService() from that place.


@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
Loading