Skip to content

Commit

Permalink
Add support for person attribute of type Location
Browse files Browse the repository at this point in the history
  • Loading branch information
mherman22 committed Sep 16, 2024
1 parent 4a54c44 commit ab1332c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -297,21 +297,20 @@ public String getDisplayString(PersonAttribute pa) {
String value = pa.toString();
return value == null ? "" : value;
}

/**
* Gets the hydrated object of person attribute.
*
* @param pa the person attribute.
* @return an object containing the hydrated object.
* Retrieves the value of a PersonAttribute, prioritizing the UUID.
* If the UUID is unavailable, falls back to the hydrated object.
*
* @param pa the PersonAttribute from which to retrieve the value
* @return the converted representation of the UUID or hydrated object, or null if both are unavailable
*/
@PropertyGetter("value")
public Object getValue(PersonAttribute pa) {
Object value = pa.getHydratedObject();
if (value == null) {
return null;
}

return ConversionUtil.convertToRepresentation(value, Representation.REF);
Object uuid = pa.getUuid();
Object value = (uuid == null) ? pa.getHydratedObject() : null;

Object toConvert = (uuid != null) ? uuid : value;
return (toConvert != null) ? ConversionUtil.convertToRepresentation(toConvert, Representation.REF) : null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@
import static org.hamcrest.Matchers.equalToIgnoringCase;

public class PersonAttributeResource1_8Test extends BaseModuleWebContextSensitiveTest {

public static final String PERSON_ATTRIBUTE_JSON = "{" + " \"value\": \"Bangalore\"," + " \"attributeType\": {"
+ " \"uuid\": \"54fc8400-1683-4d71-a1ac-98d40836ff7c\"" + " }" + "}";


public static final String PERSON_ATTRIBUTE_JSON = "{\"value\": \"Bangalore\", \"uuid\": \"592349ed-d012-4552-a274-d5d8e73b9401\", \"attributeType\": { \"uuid\": \"54fc8400-1683-4d71-a1ac-98d40836ff7c\" }}";

public static final String PERSON_ATTRIBUTE_JSON_WITHOUT_UUID = "{\"value\": \"Bangalore\", \"uuid\": \"\", \"attributeType\": { \"uuid\": \"54fc8400-1683-4d71-a1ac-98d40836ff7c\" }}";

private SimpleObject personAttributeSimpleObject = new SimpleObject();

private PersonAttributeResource1_8 resource;
Expand All @@ -62,6 +63,14 @@ public void beforeEachTests() throws Exception {
public void shouldCreatePersonAttribute() throws Exception {
SimpleObject created = (SimpleObject) resource.create("da7f524f-27ce-4bb2-86d6-6d1d05312bd5",
personAttributeSimpleObject, new RequestContext());
Assert.assertEquals("592349ed-d012-4552-a274-d5d8e73b9401", created.get("value"));
}

@Test
public void getValue_shouldFallBackToHydratedObjectWhenUuidIsEmpty() throws Exception {
personAttributeSimpleObject.putAll(new ObjectMapper().readValue(PERSON_ATTRIBUTE_JSON_WITHOUT_UUID, HashMap.class));
SimpleObject created = (SimpleObject) resource.create("da7f524f-27ce-4bb2-86d6-6d1d05312bd5",
personAttributeSimpleObject, new RequestContext());
Assert.assertEquals("Bangalore", created.get("value"));
}

Expand Down Expand Up @@ -138,11 +147,9 @@ public void setValue_shouldSetProperAttributableIdIfFound() {

PersonAttribute attribute = new PersonAttribute(type, null);
attribute.setAttributeType(type);

Assert.assertNull(attribute.getValue());

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

Assert.assertEquals(location.getUuid(), attribute.getValue());
}

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.controller.openmrs1_9;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
Expand Down Expand Up @@ -129,19 +130,20 @@ public void shouldPurgeAttribute() throws Exception {
public void shouldSupportLocationPersonAttribute() 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");
String personAttributeTypeUuid = personAttributeType.get("uuid");
assertThat(personAttributeTypeUuid, is(notNullValue()));

String personAttributeJson = "{ \"attributeType\":\"" + personAttributeTypeUuid + "\", \"value\":\"1\"}"; //We should be able to pass UUID, see RESTWS-398
SimpleObject personAttribute = deserialize(handle(newPostRequest(getURI(), personAttributeJson)));

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

assertThat(value.get("uuid"), is(notNullValue()));
assertThat((String) value.get("display"), is("Unknown Location"));
assertThat(value.get("links"), is(notNullValue()));

String value = personAttribute.get("value");

assertThat(value, is(notNullValue()));
assertThat(value, is(equalTo(personAttribute.get("uuid"))));
assertThat((String) personAttribute.get("display"), is("Unknown Location"));
assertThat(personAttribute.get("links"), is(notNullValue()));
}

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

0 comments on commit ab1332c

Please sign in to comment.