add test for restfulserverutil link generation

This commit is contained in:
Tadgh 2021-07-01 17:02:44 -04:00
parent 86519a892a
commit de84e3f98d
2 changed files with 45 additions and 1 deletions

View File

@ -272,7 +272,7 @@ public class RestfulServerUtils {
boolean first = true;
Map<String, String[]> parameters = theRequest.getParameters();
for (String nextParamName : new TreeSet<>(parameters.keySet())) {
if (excludedParameterNames != null && !excludedParameterNames.contains(nextParamName)) {
if (excludedParameterNames == null || !excludedParameterNames.contains(nextParamName)) {
for (String nextParamValue : parameters.get(nextParamName)) {
if (first) {
b.append('?');

View File

@ -3,8 +3,19 @@ package ca.uhn.fhir.rest.server;
import ca.uhn.fhir.rest.api.PreferHandlingEnum;
import ca.uhn.fhir.rest.api.PreferHeader;
import ca.uhn.fhir.rest.api.PreferReturnEnum;
import ca.uhn.fhir.rest.server.servlet.ServletRequestDetails;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static ca.uhn.fhir.rest.api.RequestTypeEnum.GET;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.*;
public class RestfulServerUtilsTest{
@ -53,4 +64,37 @@ public class RestfulServerUtilsTest{
assertFalse(header.getRespondAsync());
assertEquals(PreferHandlingEnum.LENIENT, header.getHanding());
}
@Test
public void testCreateSelfLinks() {
//Given
String baseUrl = "http://localhost:8000";
Map<String, String[]> parameters = new HashMap<>();
parameters.put("_format", new String[]{"json"});
parameters.put("_count", new String[]{"10"});
parameters.put("_offset", new String[]{"100"});
List<String> paramsToRemove = Arrays.asList("_count", "_offset");
ServletRequestDetails servletRequestDetails = new ServletRequestDetails();
servletRequestDetails.setFhirServerBase("http://localhost:8000");
servletRequestDetails.setRequestPath("$my-operation");
servletRequestDetails.setRequestType(GET);
servletRequestDetails.setParameters(parameters);
//When
String linkSelf = RestfulServerUtils.createLinkSelf(baseUrl, servletRequestDetails);
//Then
assertThat(linkSelf, is(containsString("http://localhost:8000/$my-operation?")));
assertThat(linkSelf, is(containsString("_format=json")));
assertThat(linkSelf, is(containsString("_count=10")));
assertThat(linkSelf, is(containsString("_offset=100")));
//When
String linkSelfWithoutGivenParameters = RestfulServerUtils.createLinkSelfWithoutGivenParameters(baseUrl, servletRequestDetails, paramsToRemove);
//Then
assertThat(linkSelfWithoutGivenParameters, is(containsString("http://localhost:8000/$my-operation?")));
assertThat(linkSelfWithoutGivenParameters, is(containsString("_format=json")));
}
}