From c14a1330b434de99372aaf39bf11f43a033e5c55 Mon Sep 17 00:00:00 2001 From: jamesagnew Date: Sun, 28 Feb 2016 16:07:28 -0500 Subject: [PATCH] Fix #302 - Don't crash server when adding profiles --- .../fhir/rest/server/RestfulServerUtils.java | 14 ++-- .../uhn/fhir/rest/server/ReadDstu2Test.java | 72 +++++++++++++++++-- src/changes/changes.xml | 5 ++ 3 files changed, 77 insertions(+), 14 deletions(-) diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/server/RestfulServerUtils.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/server/RestfulServerUtils.java index 10dbda5af1c..3524bdd81ec 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/server/RestfulServerUtils.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/server/RestfulServerUtils.java @@ -80,16 +80,16 @@ public class RestfulServerUtils { public static void addProfileToBundleEntry(FhirContext theContext, IBaseResource theResource, String theServerBase) { if (theResource instanceof IResource) { if (theContext.getVersion().getVersion().isNewerThan(FhirVersionEnum.DSTU1)) { - List tl = ResourceMetadataKeyEnum.PROFILES.get((IResource) theResource); - if (tl == null) { - tl = new ArrayList(); - ResourceMetadataKeyEnum.PROFILES.put((IResource) theResource, tl); - } - RuntimeResourceDefinition nextDef = theContext.getResourceDefinition(theResource); String profile = nextDef.getResourceProfile(theServerBase); if (isNotBlank(profile)) { - tl.add(new IdDt(profile)); + List newList = new ArrayList(); + List existingList = ResourceMetadataKeyEnum.PROFILES.get((IResource) theResource); + if (existingList != null) { + newList.addAll(existingList); + } + newList.add(new IdDt(profile)); + ResourceMetadataKeyEnum.PROFILES.put((IResource) theResource, newList); } } else { TagList tl = ResourceMetadataKeyEnum.TAG_LIST.get((IResource) theResource); diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/ReadDstu2Test.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/ReadDstu2Test.java index 415e742e22f..0ab9e163d9e 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/ReadDstu2Test.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/ReadDstu2Test.java @@ -4,6 +4,8 @@ import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; +import java.util.ArrayList; +import java.util.List; import java.util.concurrent.TimeUnit; import org.apache.commons.io.IOUtils; @@ -16,11 +18,13 @@ import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.ServletHandler; import org.eclipse.jetty.servlet.ServletHolder; import org.junit.AfterClass; +import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.model.api.IResource; +import ca.uhn.fhir.model.api.ResourceMetadataKeyEnum; import ca.uhn.fhir.model.api.annotation.ResourceDef; import ca.uhn.fhir.model.dstu2.resource.Patient; import ca.uhn.fhir.model.primitive.IdDt; @@ -36,12 +40,16 @@ public class ReadDstu2Test { private static int ourPort; private static Server ourServer; + private static RestfulServer ourServlet; + private static boolean ourInitializeProfileList; /** * In DSTU2+ the resource ID appears in the resource body */ @Test public void testReadJson() throws Exception { + ourServlet.setAddProfileTag(AddProfileTagEnum.ALWAYS); + HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/123?_format=json"); HttpResponse status = ourClient.execute(httpGet); String responseContent = IOUtils.toString(status.getEntity().getContent()); @@ -71,6 +79,54 @@ public class ReadDstu2Test { ourLog.info(responseContent); } + + @Before + public void before() { + ourServlet.setAddProfileTag(AddProfileTagEnum.NEVER); + ourInitializeProfileList = false; + } + + /** + * See #302 + */ + @Test + public void testAddProfile() throws Exception { + ourServlet.setAddProfileTag(AddProfileTagEnum.ALWAYS); + + HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/123&_format=xml"); + HttpResponse status = ourClient.execute(httpGet); + String responseContent = IOUtils.toString(status.getEntity().getContent()); + IOUtils.closeQuietly(status.getEntity().getContent()); + + assertEquals(200, status.getStatusLine().getStatusCode()); + assertThat(responseContent, containsString("p1ReadValue")); + assertThat(responseContent, containsString("p1ReadId")); + assertEquals("", responseContent); + + ourLog.info(responseContent); + } + + /** + * See #302 + */ + @Test + public void testAddProfileToExistingList() throws Exception { + ourServlet.setAddProfileTag(AddProfileTagEnum.ALWAYS); + ourInitializeProfileList = true; + + HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/123&_format=xml"); + HttpResponse status = ourClient.execute(httpGet); + String responseContent = IOUtils.toString(status.getEntity().getContent()); + IOUtils.closeQuietly(status.getEntity().getContent()); + + assertEquals(200, status.getStatusLine().getStatusCode()); + assertThat(responseContent, containsString("p1ReadValue")); + assertThat(responseContent, containsString("p1ReadId")); + assertEquals("", responseContent); + + ourLog.info(responseContent); + } + @AfterClass public static void afterClass() throws Exception { ourServer.stop(); @@ -85,10 +141,10 @@ public class ReadDstu2Test { DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); ServletHandler proxyHandler = new ServletHandler(); - RestfulServer servlet = new RestfulServer(ourCtx); - servlet.setFhirContext(ourCtx); - servlet.setResourceProviders(patientProvider); - ServletHolder servletHolder = new ServletHolder(servlet); + ourServlet = new RestfulServer(ourCtx); + ourServlet.setFhirContext(ourCtx); + ourServlet.setResourceProviders(patientProvider); + ServletHolder servletHolder = new ServletHolder(ourServlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); ourServer.start(); @@ -100,9 +156,6 @@ public class ReadDstu2Test { } - /** - * Created by dsotnikov on 2/25/2014. - */ public static class DummyPatientResourceProvider implements IResourceProvider { @Override @@ -115,6 +168,11 @@ public class ReadDstu2Test { Patient p1 = new MyPatient(); p1.setId("p1ReadId"); p1.addIdentifier().setValue("p1ReadValue"); + if (ourInitializeProfileList) { + List profiles = new ArrayList(); + profiles.add(new IdDt("http://foo")); + ResourceMetadataKeyEnum.PROFILES.put(p1, profiles); + } return p1; } diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 496f74f77ca..0559006d72c 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -129,6 +129,11 @@ Michael Lawley for reporting! ]]> + + Fix a server exception when trying to automatically add the profile tag + to a resource which already has one or more profiles set. Thanks to + Magnus Vinther for reporting! +