Skip to content

Commit

Permalink
Merge pull request #259 from tmarzeion/RESTWS-466
Browse files Browse the repository at this point in the history
RESTWS-466 - Fixed custom representation depth issue
  • Loading branch information
rkorytkowski committed Dec 6, 2016
2 parents 79c5be2 + e7d2f42 commit 3a89e8a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,31 @@

import org.apache.commons.beanutils.PropertyUtils;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openmrs.Encounter;
import org.openmrs.api.context.Context;
import org.openmrs.module.webservices.rest.SimpleObject;
import org.openmrs.module.webservices.rest.test.Util;
import org.openmrs.module.webservices.rest.web.RestTestConstants1_8;
import org.openmrs.module.webservices.rest.web.response.ResourceDoesNotSupportOperationException;
import org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceControllerTest;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.web.bind.annotation.RequestMethod;

import static org.junit.Assert.assertEquals;

public class EncounterController2_0Test extends MainResourceControllerTest {

public static final String currentTimezone = Calendar.getInstance().getTimeZone().getDisplayName(true, TimeZone.SHORT);


@Before
public void setup() throws Exception {
executeDataSet("EncountersForDifferentTypesWithObservations.xml");
}

@Test
public void shouldGetEncountersByEncounterTypeAndPatient() throws Exception {
executeDataSet("EncountersForDifferentTypesWithObservations.xml");

SimpleObject result = deserialize(handle(newGetRequest(getURI(), new Parameter("s", "default"), new Parameter(
"patient", "41c6b35e-c093-11e3-be87-005056821db0"), new Parameter("encounterType",
"ff7397ea-c090-11e3-be87-005056821db0"))));
Expand All @@ -51,6 +60,29 @@ public void shouldGetAll() throws Exception {
super.shouldGetAll();
}

@Test
public void shouldReturnEncounterAsComplexCustomRepresentation() throws Exception {
final String customRep = "custom:(uuid,display,patient:(uuid,display),location:(name,tags:(display)))";

final String encounterUuid = "62967e68-96bb-11e0-8d6b-9b9415a91465";
final String encounterDisplay = "sample encounter a 01/08/2008";
final String patientUuid = "41c6b35e-c093-11e3-be87-005056821db0";
final String patientDisplay = "";
final String locationName = "Unknown Location";

SimpleObject result = deserialize(handle(newGetRequest(getURI(),
new Parameter("patient", "41c6b35e-c093-11e3-be87-005056821db0"),
new Parameter("v", customRep))));

assertEquals(2, Util.getResultsSize(result));
List<?> encounters = result.get("results");
assertEquals(encounterUuid, PropertyUtils.getProperty(encounters.get(0), "uuid"));
assertEquals(encounterDisplay, PropertyUtils.getProperty(Util.getResultsList(result).get(0), "display"));
assertEquals(patientUuid, PropertyUtils.getProperty(Util.getResultsList(result).get(0), "patient.uuid"));
assertEquals(patientDisplay, PropertyUtils.getProperty(Util.getResultsList(result).get(0), "patient.display"));
assertEquals(locationName, PropertyUtils.getProperty(Util.getResultsList(result).get(0), "location.name"));
}

/**
* @see org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceControllerTest#getURI()
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,8 @@ private DelegatingResourceDescription getCustomRepresentationDescription(CustomR
DelegatingResourceDescription desc = new DelegatingResourceDescription();

String def = representation.getRepresentation();
def = def.substring(1, def.length() - 1); //remove '(' and ')'
def = def.startsWith("(") ? def.substring(1) : def;
def = def.endsWith(")") ? def.substring(0, def.length() - 1) : def;
String[] fragments = def.split(",");
for (int i = 0; i < fragments.length; i++) {
String[] field = fragments[i].split(":"); //split into field and representation
Expand All @@ -382,21 +383,26 @@ private DelegatingResourceDescription getCustomRepresentationDescription(CustomR
if (rep.startsWith("(")) {
StringBuilder customRep = new StringBuilder();
customRep.append(rep);
int open = 1;
for (i = i + 1; i < fragments.length; i++) {
for (char fragment : fragments[i].toCharArray()) {
if (fragment == '(') {
open++;
} else if (fragment == ')') {
open--;
}
if (!rep.endsWith(")")) {
for (int j = 2; j < field.length; j++) {
customRep.append(":").append(field[j]);
}

customRep.append(",");
customRep.append(fragments[i]);

if (open == 0) {
break;
int open = 1;
for (i = i + 1; i < fragments.length; i++) {
for (char fragment : fragments[i].toCharArray()) {
if (fragment == '(') {
open++;
} else if (fragment == ')') {
open--;
}
}

customRep.append(",");
customRep.append(fragments[i]);

if (open == 0) {
break;
}
}
}
desc.addProperty(property, new CustomRepresentation(customRep.toString()));
Expand Down

0 comments on commit 3a89e8a

Please sign in to comment.