Merge branch 'master' of github.com:jamesagnew/hapi-fhir

This commit is contained in:
James 2017-10-31 20:22:59 -04:00
commit 0fb74e5d42
18 changed files with 566 additions and 278 deletions

View File

@ -20,33 +20,34 @@ package ca.uhn.fhir.util;
* #L% * #L%
*/ */
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.i18n.HapiLocalizer;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import org.slf4j.LoggerFactory;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.Arrays; import java.util.Arrays;
import java.util.Locale; import java.util.Locale;
import java.util.TimeZone; import java.util.TimeZone;
import org.slf4j.LoggerFactory; import static org.apache.commons.lang3.StringUtils.defaultString;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.i18n.HapiLocalizer;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
public class TestUtil { public class TestUtil {
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(TestUtil.class); private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(TestUtil.class);
/** /**
* <b>THIS IS FOR UNIT TESTS ONLY - DO NOT CALL THIS METHOD FROM USER CODE</b> * <b>THIS IS FOR UNIT TESTS ONLY - DO NOT CALL THIS METHOD FROM USER CODE</b>
* * <p>
* When we run the unit tests in cobertura, JUnit doesn't seem to clean up static fields which leads to * When we run the unit tests in cobertura, JUnit doesn't seem to clean up static fields which leads to
* tons of memory being used by the end and the JVM crashes in Travis. Manually clearing all of the * tons of memory being used by the end and the JVM crashes in Travis. Manually clearing all of the
* static fields seems to solve this. * static fields seems to solve this.
*/ */
public static void clearAllStaticFieldsForUnitTest() { public static void clearAllStaticFieldsForUnitTest() {
HapiLocalizer.setOurFailOnMissingMessage(true); HapiLocalizer.setOurFailOnMissingMessage(true);
Class<?> theType; Class<?> theType;
try { try {
throw new Exception(); throw new Exception();
@ -104,7 +105,7 @@ public class TestUtil {
* environment * environment
*/ */
public static void randomizeLocale() { public static void randomizeLocale() {
Locale[] availableLocales = { Locale.CANADA, Locale.GERMANY, Locale.TAIWAN }; Locale[] availableLocales = {Locale.CANADA, Locale.GERMANY, Locale.TAIWAN};
Locale.setDefault(availableLocales[(int) (Math.random() * availableLocales.length)]); Locale.setDefault(availableLocales[(int) (Math.random() * availableLocales.length)]);
ourLog.info("Tests are running in locale: " + Locale.getDefault().getDisplayName()); ourLog.info("Tests are running in locale: " + Locale.getDefault().getDisplayName());
if (Math.random() < 0.5) { if (Math.random() < 0.5) {
@ -116,10 +117,19 @@ public class TestUtil {
System.setProperty("file.encoding", "UTF-8"); System.setProperty("file.encoding", "UTF-8");
System.setProperty("line.separator", "\n"); System.setProperty("line.separator", "\n");
} }
String availableTimeZones[] = { "GMT+08:00", "GMT-05:00", "GMT+00:00", "GMT+03:30" }; String availableTimeZones[] = {"GMT+08:00", "GMT-05:00", "GMT+00:00", "GMT+03:30"};
String timeZone = availableTimeZones[(int) (Math.random() * availableTimeZones.length)]; String timeZone = availableTimeZones[(int) (Math.random() * availableTimeZones.length)];
TimeZone.setDefault(TimeZone.getTimeZone(timeZone)); TimeZone.setDefault(TimeZone.getTimeZone(timeZone));
ourLog.info("Tests are using time zone: {}", TimeZone.getDefault().getID()); ourLog.info("Tests are using time zone: {}", TimeZone.getDefault().getID());
} }
/**
* <b>THIS IS FOR UNIT TESTS ONLY - DO NOT CALL THIS METHOD FROM USER CODE</b>
* <p>
* Strip \r chars from a string to account for line ending platform differences
*/
public static String stripReturns(String theString) {
return defaultString(theString).replace("\r", "");
}
} }

View File

@ -159,6 +159,7 @@ public abstract class BackboneElement extends Element implements IBaseBackboneEl
public abstract BackboneElement copy(); public abstract BackboneElement copy();
public void copyValues(BackboneElement dst) { public void copyValues(BackboneElement dst) {
super.copyValues(dst);
if (modifierExtension != null) { if (modifierExtension != null) {
dst.modifierExtension = new ArrayList<Extension>(); dst.modifierExtension = new ArrayList<Extension>();
for (Extension i : modifierExtension) for (Extension i : modifierExtension)

View File

@ -340,6 +340,7 @@ public abstract class DomainResource extends Resource implements IBaseHasExtensi
public abstract DomainResource copy(); public abstract DomainResource copy();
public void copyValues(DomainResource dst) { public void copyValues(DomainResource dst) {
super.copyValues(dst);
dst.text = text == null ? null : text.copy(); dst.text = text == null ? null : text.copy();
if (contained != null) { if (contained != null) {
dst.contained = new ArrayList<Resource>(); dst.contained = new ArrayList<Resource>();

View File

@ -0,0 +1,26 @@
package ca.uhn.fhir.model;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.hl7.fhir.dstu2016may.model.BackboneElement;
import org.hl7.fhir.dstu2016may.model.Patient.PatientCommunicationComponent;
import org.junit.Test;
public class BackboneElementDstu2_1Test {
/**
* Ensuring that IDs of subtypes of BackboneElement get copied when
* the {@link org.hl7.fhir.dstu2016may.model.BackboneElement#copy()} method is called
*/
@Test
public void testPatientCommunicationComponentIdCopy() {
PatientCommunicationComponent pcc1 = new PatientCommunicationComponent();
pcc1.setId("1001");
PatientCommunicationComponent copiedPcc = pcc1.copy();
String copiedPccID = copiedPcc.getIdElement().getIdPart();
assertTrue(copiedPcc instanceof BackboneElement); // Just making sure this assumption still holds up, otherwise this test isn't very useful
assertEquals("1001", copiedPccID);
}
}

View File

@ -0,0 +1,30 @@
package ca.uhn.fhir.model;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.hl7.fhir.dstu2016may.model.DomainResource;
import org.hl7.fhir.dstu2016may.model.Narrative;
import org.hl7.fhir.dstu2016may.model.Patient;
import org.junit.Test;
public class DomainResourceDstu2_1Test {
/**
* Ensuring that Ensuring that fields defined in {@link org.hl7.fhir.dstu2016may.model.DomainResource} and {@link org.hl7.fhir.dstu2016may.model.Resource}
* get copied when the {@link org.hl7.fhir.dstu2016may.model.DomainResource#copy()} method is called
*/
@Test
public void testPatientCopy() {
Patient p1 = new Patient();
p1.setId("1001");
p1.setText(new Narrative().setStatus(Narrative.NarrativeStatus.ADDITIONAL));
Patient copiedPatient = p1.copy();
String copiedPatientID = copiedPatient.getIdElement().getIdPart();
Narrative.NarrativeStatus copiedPatientTextStatus = copiedPatient.getText().getStatus();
assertTrue(copiedPatient instanceof DomainResource); // Just making sure this assumption still holds up, otherwise this test isn't very useful
assertEquals("1001", copiedPatientID);
assertEquals(new Narrative().setStatus(Narrative.NarrativeStatus.ADDITIONAL).getStatus(), copiedPatientTextStatus);
}
}

View File

@ -0,0 +1,25 @@
package org.hl7.fhir.dstu3.model;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.hl7.fhir.dstu3.model.Patient.PatientCommunicationComponent;
import org.junit.Test;
public class BackboneElementDstu3Test {
/**
* Ensuring that IDs of subtypes of BackboneElement get copied when
* the {@link BackboneElement#copy()} method is called
*/
@Test
public void testPatientCommunicationComponentCopy() {
PatientCommunicationComponent pcc1 = new PatientCommunicationComponent();
pcc1.setId("1001");
PatientCommunicationComponent copiedPcc = pcc1.copy();
String copiedPccID = copiedPcc.getId();
assertTrue(copiedPcc instanceof BackboneElement); // Just making sure this assumption still holds up, otherwise this test isn't very useful
assertEquals("1001", copiedPccID);
}
}

View File

@ -0,0 +1,27 @@
package org.hl7.fhir.dstu3.model;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class DomainResourceDstu3Test {
/**
* Ensuring that fields defined in {@link DomainResource} and {@link Resource}
* get copied when the {@link DomainResource#copy()} method is called
*/
@Test
public void testPatientCopy() {
Patient p1 = new Patient();
p1.setId("1001");
p1.setText(new Narrative().setStatus(Narrative.NarrativeStatus.ADDITIONAL));
Patient copiedPatient = p1.copy();
String copiedPatientID = copiedPatient.getIdElement().getIdPart();
Narrative.NarrativeStatus copiedPatientTextStatus = copiedPatient.getText().getStatus();
assertTrue(copiedPatient instanceof DomainResource); // Just making sure this assumption still holds up, otherwise this test isn't very useful
assertEquals("1001", copiedPatientID);
assertEquals(new Narrative().setStatus(Narrative.NarrativeStatus.ADDITIONAL).getStatus(), copiedPatientTextStatus);
}
}

View File

@ -0,0 +1,29 @@
package org.hl7.fhir.dstu3.model;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class MetadataResourceDstu3Test {
/**
* Ensuring that fields defined in {@link MetadataResource}, {@link DomainResource}, and {@link Resource}
* get copied when the {@link MetadataResource#copy()} method is called
*/
@Test
public void testCodeSystemCopy() {
CodeSystem codeSystem1 = new CodeSystem();
codeSystem1.setId("1001");
codeSystem1.setName("Name123");
codeSystem1.setText(new Narrative().setStatus(Narrative.NarrativeStatus.ADDITIONAL));
CodeSystem copiedCodeSystem = codeSystem1.copy();
String copiedCodeSystemID = copiedCodeSystem.getId();
String copiedCodeSystemName = copiedCodeSystem.getName();
Narrative.NarrativeStatus copiedCodeSystemTextStatus = copiedCodeSystem.getText().getStatus();
assertTrue(copiedCodeSystem instanceof MetadataResource); // Just making sure this assumption still holds up, otherwise this test isn't very useful
assertEquals("1001", copiedCodeSystemID);
assertEquals("Name123", copiedCodeSystemName);
assertEquals(new Narrative().setStatus(Narrative.NarrativeStatus.ADDITIONAL).getStatus(), copiedCodeSystemTextStatus);
}
}

View File

@ -128,6 +128,7 @@ public abstract class BackboneElement extends Element implements IBaseBackboneEl
public abstract BackboneElement copy(); public abstract BackboneElement copy();
public void copyValues(BackboneElement dst) { public void copyValues(BackboneElement dst) {
super.copyValues(dst);
if (modifierExtension != null) { if (modifierExtension != null) {
dst.modifierExtension = new ArrayList<Extension>(); dst.modifierExtension = new ArrayList<Extension>();
for (Extension i : modifierExtension) for (Extension i : modifierExtension)

View File

@ -265,6 +265,7 @@ public abstract class DomainResource extends Resource implements IBaseHasExtensi
public abstract DomainResource copy(); public abstract DomainResource copy();
public void copyValues(DomainResource dst) { public void copyValues(DomainResource dst) {
super.copyValues(dst);
dst.text = text == null ? null : text.copy(); dst.text = text == null ? null : text.copy();
if (contained != null) { if (contained != null) {
dst.contained = new ArrayList<Resource>(); dst.contained = new ArrayList<Resource>();

View File

@ -0,0 +1,26 @@
package ca.uhn.fhir.model;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.hl7.fhir.instance.model.BackboneElement;
import org.hl7.fhir.instance.model.Patient.PatientCommunicationComponent;
import org.junit.Test;
public class BackboneElementHL7_Dstu2Test {
/**
* Ensuring that IDs of subtypes of BackboneElement get copied when
* the {@link org.hl7.fhir.instance.model.BackboneElement#copy()} method is called
*/
@Test
public void testPatientCommunicationComponentIdCopy() {
PatientCommunicationComponent pcc1 = new PatientCommunicationComponent();
pcc1.setId("1001");
PatientCommunicationComponent copiedPcc = pcc1.copy();
String copiedPccID = copiedPcc.getIdElement().getIdPart();
assertTrue(copiedPcc instanceof BackboneElement); // Just making sure this assumption still holds up, otherwise this test isn't very useful
assertEquals("1001", copiedPccID);
}
}

View File

@ -0,0 +1,30 @@
package ca.uhn.fhir.model;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.hl7.fhir.instance.model.DomainResource;
import org.hl7.fhir.instance.model.Narrative;
import org.hl7.fhir.instance.model.Patient;
import org.junit.Test;
public class DomainResourceHL7_Dstu2Test {
/**
* Ensuring that fields defined in {@link org.hl7.fhir.instance.model.DomainResource} and {@link org.hl7.fhir.instance.model.Resource}
* the {@link org.hl7.fhir.instance.model.DomainResource#copy()} method is called
*/
@Test
public void testPatientCopy() {
Patient p1 = new Patient();
p1.setId("1001");
p1.setText(new Narrative().setStatus(Narrative.NarrativeStatus.ADDITIONAL));
Patient copiedPatient = p1.copy();
String copiedPatientID = copiedPatient.getIdElement().getIdPart();
Narrative.NarrativeStatus copiedPatientTextStatus = copiedPatient.getText().getStatus();
assertTrue(copiedPatient instanceof DomainResource); // Just making sure this assumption still holds up, otherwise this test isn't very useful
assertEquals("1001", copiedPatientID);
assertEquals(new Narrative().setStatus(Narrative.NarrativeStatus.ADDITIONAL).getStatus(), copiedPatientTextStatus);
}
}

View File

@ -22,7 +22,6 @@ import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.r4.hapi.rest.server.GraphQLProvider; import org.hl7.fhir.r4.hapi.rest.server.GraphQLProvider;
import org.hl7.fhir.r4.model.*; import org.hl7.fhir.r4.model.*;
import org.hl7.fhir.r4.utils.GraphQLEngine;
import org.hl7.fhir.utilities.graphql.Argument; import org.hl7.fhir.utilities.graphql.Argument;
import org.hl7.fhir.utilities.graphql.IGraphQLStorageServices; import org.hl7.fhir.utilities.graphql.IGraphQLStorageServices;
import org.hl7.fhir.utilities.graphql.ReferenceResolution; import org.hl7.fhir.utilities.graphql.ReferenceResolution;
@ -37,8 +36,7 @@ import java.util.List;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import static org.hamcrest.Matchers.startsWith; import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.*;
import static org.junit.Assert.assertThat;
public class GraphQLR4ProviderTest { public class GraphQLR4ProviderTest {
@ -48,6 +46,121 @@ public class GraphQLR4ProviderTest {
private static int ourPort; private static int ourPort;
private static Server ourServer; private static Server ourServer;
@Before
public void before() {
//nothing
}
@Test
public void testGraphInstance() throws Exception {
String query = "{name{family,given}}";
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/123/$graphql?query=" + UrlUtil.escape(query));
CloseableHttpResponse status = ourClient.execute(httpGet);
try {
String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8);
ourLog.info(responseContent);
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals(TestUtil.stripReturns("{" +
" \"name\":[{" +
" \"family\":\"FAMILY\"," +
" \"given\":[\"GIVEN1\",\"GIVEN2\"]" +
" },{" +
" \"given\":[\"GivenOnly1\",\"GivenOnly2\"]" +
" }]" +
"}"), TestUtil.stripReturns(responseContent));
assertThat(status.getFirstHeader(Constants.HEADER_CONTENT_TYPE).getValue(), startsWith("application/json"));
} finally {
IOUtils.closeQuietly(status.getEntity().getContent());
}
}
@Test
public void testGraphInstanceWithFhirpath() throws Exception {
String query = "{name(fhirpath:\"family.exists()\"){text,given,family}}";
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/123/$graphql?query=" + UrlUtil.escape(query));
CloseableHttpResponse status = ourClient.execute(httpGet);
try {
String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8);
ourLog.info(responseContent);
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals(TestUtil.stripReturns("{\n" +
" \"name\":[{\n" +
" \"given\":[\"GIVEN1\",\"GIVEN2\"],\n" +
" \"family\":\"FAMILY\"\n" +
" }]\n" +
"}"), TestUtil.stripReturns(responseContent));
assertThat(status.getFirstHeader(Constants.HEADER_CONTENT_TYPE).getValue(), startsWith("application/json"));
} finally {
IOUtils.closeQuietly(status.getEntity().getContent());
}
}
@Test
public void testGraphSystemInstance() throws Exception {
String query = "{Patient(id:123){id,name{given,family}}}";
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/$graphql?query=" + UrlUtil.escape(query));
CloseableHttpResponse status = ourClient.execute(httpGet);
try {
String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8);
ourLog.info(responseContent);
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals(TestUtil.stripReturns("{\n" +
" \"Patient\":{\n" +
" \"name\":[{\n" +
" \"given\":[\"GIVEN1\",\"GIVEN2\"],\n" +
" \"family\":\"FAMILY\"\n" +
" },{\n" +
" \"given\":[\"GivenOnly1\",\"GivenOnly2\"]\n" +
" }]\n" +
" }\n" +
"}"), TestUtil.stripReturns(responseContent));
assertThat(status.getFirstHeader(Constants.HEADER_CONTENT_TYPE).getValue(), startsWith("application/json"));
} finally {
IOUtils.closeQuietly(status.getEntity().getContent());
}
}
@Test
public void testGraphSystemList() throws Exception {
String query = "{PatientList(name:\"pet\"){name{family,given}}}";
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/$graphql?query=" + UrlUtil.escape(query));
CloseableHttpResponse status = ourClient.execute(httpGet);
try {
String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8);
ourLog.info(responseContent);
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals(TestUtil.stripReturns("{\n" +
" \"PatientList\":[{\n" +
" \"name\":[{\n" +
" \"family\":\"pet\",\n" +
" \"given\":[\"GIVEN1\",\"GIVEN2\"]\n" +
" },{\n" +
" \"given\":[\"GivenOnly1\",\"GivenOnly2\"]\n" +
" }]\n" +
" },{\n" +
" \"name\":[{\n" +
" \"given\":[\"GivenOnlyB1\",\"GivenOnlyB2\"]\n" +
" }]\n" +
" }]\n" +
"}"), TestUtil.stripReturns(responseContent));
assertThat(status.getFirstHeader(Constants.HEADER_CONTENT_TYPE).getValue(), startsWith("application/json"));
} finally {
IOUtils.closeQuietly(status.getEntity().getContent());
}
}
@AfterClass @AfterClass
public static void afterClassClearContext() throws Exception { public static void afterClassClearContext() throws Exception {
ourServer.stop(); ourServer.stop();
@ -79,121 +192,6 @@ public class GraphQLR4ProviderTest {
} }
@Before
public void before() {
//nothing
}
@Test
public void testGraphInstance() throws Exception {
String query = "{name{family,given}}";
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/123/$graphql?query=" + UrlUtil.escape(query));
CloseableHttpResponse status = ourClient.execute(httpGet);
try {
String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8);
ourLog.info(responseContent);
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals("{" +
" \"name\":[{" +
" \"family\":\"FAMILY\"," +
" \"given\":[\"GIVEN1\",\"GIVEN2\"]" +
" },{" +
" \"given\":[\"GivenOnly1\",\"GivenOnly2\"]" +
" }]" +
"}", responseContent.replace("\n", "").replace("\r", ""));
assertThat(status.getFirstHeader(Constants.HEADER_CONTENT_TYPE).getValue(), startsWith("application/json"));
} finally {
IOUtils.closeQuietly(status.getEntity().getContent());
}
}
@Test
public void testGraphSystemInstance() throws Exception {
String query = "{Patient(id:123){id,name{given,family}}}";
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/$graphql?query=" + UrlUtil.escape(query));
CloseableHttpResponse status = ourClient.execute(httpGet);
try {
String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8);
ourLog.info(responseContent);
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals("{\n" +
" \"Patient\":{\n" +
" \"name\":[{\n" +
" \"given\":[\"GIVEN1\",\"GIVEN2\"],\n" +
" \"family\":\"FAMILY\"\n" +
" },{\n" +
" \"given\":[\"GivenOnly1\",\"GivenOnly2\"]\n" +
" }]\n" +
" }\n" +
"}", responseContent);
assertThat(status.getFirstHeader(Constants.HEADER_CONTENT_TYPE).getValue(), startsWith("application/json"));
} finally {
IOUtils.closeQuietly(status.getEntity().getContent());
}
}
@Test
public void testGraphSystemList() throws Exception {
String query = "{PatientList(name:\"pet\"){name{family,given}}}";
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/$graphql?query=" + UrlUtil.escape(query));
CloseableHttpResponse status = ourClient.execute(httpGet);
try {
String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8);
ourLog.info(responseContent);
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals("{\n" +
" \"PatientList\":[{\n" +
" \"name\":[{\n" +
" \"family\":\"pet\",\n" +
" \"given\":[\"GIVEN1\",\"GIVEN2\"]\n" +
" },{\n" +
" \"given\":[\"GivenOnly1\",\"GivenOnly2\"]\n" +
" }]\n" +
" },{\n" +
" \"name\":[{\n" +
" \"given\":[\"GivenOnlyB1\",\"GivenOnlyB2\"]\n" +
" }]\n" +
" }]\n" +
"}", responseContent);
assertThat(status.getFirstHeader(Constants.HEADER_CONTENT_TYPE).getValue(), startsWith("application/json"));
} finally {
IOUtils.closeQuietly(status.getEntity().getContent());
}
}
@Test
public void testGraphInstanceWithFhirpath() throws Exception {
String query = "{name(fhirpath:\"family.exists()\"){text,given,family}}";
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/123/$graphql?query=" + UrlUtil.escape(query));
CloseableHttpResponse status = ourClient.execute(httpGet);
try {
String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8);
ourLog.info(responseContent);
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals("{\n" +
" \"name\":[{\n" +
" \"given\":[\"GIVEN1\",\"GIVEN2\"],\n" +
" \"family\":\"FAMILY\"\n" +
" }]\n" +
"}", responseContent);
assertThat(status.getFirstHeader(Constants.HEADER_CONTENT_TYPE).getValue(), startsWith("application/json"));
} finally {
IOUtils.closeQuietly(status.getEntity().getContent());
}
}
public static class DummyPatientResourceProvider implements IResourceProvider { public static class DummyPatientResourceProvider implements IResourceProvider {
@Override @Override
@ -219,31 +217,6 @@ public class GraphQLR4ProviderTest {
} }
private static class MyStorageServices implements IGraphQLStorageServices<Resource, Reference, Bundle> { private static class MyStorageServices implements IGraphQLStorageServices<Resource, Reference, Bundle> {
@Override
public ReferenceResolution<Resource> lookup(Object theAppInfo, Resource theContext, Reference theReference) throws FHIRException {
ourLog.info("lookup from {} to {}", theContext.getIdElement().getValue(), theReference.getReference());
return null;
}
@Override
public Resource lookup(Object theAppInfo, String theType, String theId) throws FHIRException {
ourLog.info("lookup {}/{}", theType, theId);
if (theType.equals("Patient") && theId.equals("123")) {
Patient p = new Patient();
p.addName()
.setFamily("FAMILY")
.addGiven("GIVEN1")
.addGiven("GIVEN2");
p.addName()
.addGiven("GivenOnly1")
.addGiven("GivenOnly2");
return p;
}
return null;
}
@Override @Override
public void listResources(Object theAppInfo, String theType, List<Argument> theSearchParams, List<Resource> theMatches) throws FHIRException { public void listResources(Object theAppInfo, String theType, List<Argument> theSearchParams, List<Resource> theMatches) throws FHIRException {
ourLog.info("listResources of {} - {}", theType, theSearchParams); ourLog.info("listResources of {} - {}", theType, theSearchParams);
@ -271,6 +244,31 @@ public class GraphQLR4ProviderTest {
} }
} }
@Override
public Resource lookup(Object theAppInfo, String theType, String theId) throws FHIRException {
ourLog.info("lookup {}/{}", theType, theId);
if (theType.equals("Patient") && theId.equals("123")) {
Patient p = new Patient();
p.addName()
.setFamily("FAMILY")
.addGiven("GIVEN1")
.addGiven("GIVEN2");
p.addName()
.addGiven("GivenOnly1")
.addGiven("GivenOnly2");
return p;
}
return null;
}
@Override
public ReferenceResolution<Resource> lookup(Object theAppInfo, Resource theContext, Reference theReference) throws FHIRException {
ourLog.info("lookup from {} to {}", theContext.getIdElement().getValue(), theReference.getReference());
return null;
}
@Override @Override
public Bundle search(Object theAppInfo, String theType, List<Argument> theSearchParams) throws FHIRException { public Bundle search(Object theAppInfo, String theType, List<Argument> theSearchParams) throws FHIRException {
ourLog.info("search on {} - {}", theType, theSearchParams); ourLog.info("search on {} - {}", theType, theSearchParams);

View File

@ -0,0 +1,25 @@
package org.hl7.fhir.r4.model;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.hl7.fhir.r4.model.Patient.PatientCommunicationComponent;
import org.junit.Test;
public class BackboneElementR4Test {
/**
* Ensuring that IDs of subtypes of BackboneElement get copied when
* the {@link BackboneElement#copy()} method is called
*/
@Test
public void testPatientCommunicationComponentCopy() {
PatientCommunicationComponent pcc1 = new PatientCommunicationComponent();
pcc1.setId("1001");
PatientCommunicationComponent copiedPcc = pcc1.copy();
String copiedPccID = copiedPcc.getId();
assertTrue(copiedPcc instanceof BackboneElement); // Just making sure this assumption still holds up, otherwise this test isn't very useful
assertEquals("1001", copiedPccID);
}
}

View File

@ -0,0 +1,26 @@
package org.hl7.fhir.r4.model;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class DomainResourceR4Test {
/**
* Ensuring that fields defined in {@link DomainResource} and {@link Resource}
* get copied when the {@link DomainResource#copy()} method is called
*/
@Test
public void testPatientCopy() {
Patient p1 = new Patient();
p1.setId("1001");
p1.setText(new Narrative().setStatus(Narrative.NarrativeStatus.ADDITIONAL));
Patient copiedPatient = p1.copy();
String copiedPatientID = copiedPatient.getIdElement().getIdPart();
Narrative.NarrativeStatus copiedPatientTextStatus = copiedPatient.getText().getStatus();
assertTrue(copiedPatient instanceof DomainResource); // Just making sure this assumption still holds up, otherwise this test isn't very useful
assertEquals("1001", copiedPatientID);
assertEquals(new Narrative().setStatus(Narrative.NarrativeStatus.ADDITIONAL).getStatus(), copiedPatientTextStatus);
}
}

View File

@ -0,0 +1,29 @@
package org.hl7.fhir.r4.model;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class MetadataResourceR4Test {
/**
* Ensuring that fields defined in {@link MetadataResource}, {@link DomainResource}, and {@link Resource}
* get copied when the {@link MetadataResource#copy()} method is called
*/
@Test
public void testCodeSystemCopy() {
CodeSystem codeSystem1 = new CodeSystem();
codeSystem1.setId("1001");
codeSystem1.setName("Name123");
codeSystem1.setText(new Narrative().setStatus(Narrative.NarrativeStatus.ADDITIONAL));
CodeSystem copiedCodeSystem = codeSystem1.copy();
String copiedCodeSystemID = copiedCodeSystem.getId();
String copiedCodeSystemName = copiedCodeSystem.getName();
Narrative.NarrativeStatus copiedCodeSystemTextStatus = copiedCodeSystem.getText().getStatus();
assertTrue(copiedCodeSystem instanceof MetadataResource); // Just making sure this assumption still holds up, otherwise this test isn't very useful
assertEquals("1001", copiedCodeSystemID);
assertEquals("Name123", copiedCodeSystemName);
assertEquals(new Narrative().setStatus(Narrative.NarrativeStatus.ADDITIONAL).getStatus(), copiedCodeSystemTextStatus);
}
}

View File

@ -48,39 +48,6 @@ public class GraphQLDstu3ProviderTest {
private static int ourPort; private static int ourPort;
private static Server ourServer; private static Server ourServer;
@AfterClass
public static void afterClassClearContext() throws Exception {
ourServer.stop();
TestUtil.clearAllStaticFieldsForUnitTest();
}
@BeforeClass
public static void beforeClass() throws Exception {
ourPort = PortUtil.findFreePort();
ourServer = new Server(ourPort);
ServletHandler proxyHandler = new ServletHandler();
RestfulServer servlet = new RestfulServer(ourCtx);
servlet.setDefaultResponseEncoding(EncodingEnum.JSON);
servlet.setPagingProvider(new FifoMemoryPagingProvider(10));
servlet.registerProvider(new DummyPatientResourceProvider());
MyStorageServices storageServices = new MyStorageServices();
servlet.registerProvider(new GraphQLProviderDstu3(ourCtx, new DefaultProfileValidationSupport(), storageServices));
ServletHolder servletHolder = new ServletHolder(servlet);
proxyHandler.addServletWithMapping(servletHolder, "/*");
ourServer.setHandler(proxyHandler);
ourServer.start();
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS);
HttpClientBuilder builder = HttpClientBuilder.create();
builder.setConnectionManager(connectionManager);
ourClient = builder.build();
}
@Before @Before
public void before() { public void before() {
//nothing //nothing
@ -97,76 +64,14 @@ public class GraphQLDstu3ProviderTest {
ourLog.info(responseContent); ourLog.info(responseContent);
assertEquals(200, status.getStatusLine().getStatusCode()); assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals("{\n" + assertEquals(TestUtil.stripReturns("{\n" +
" \"name\":[{\n" + " \"name\":[{\n" +
" \"family\":[\"FAMILY\"],\n" + " \"family\":[\"FAMILY\"],\n" +
" \"given\":[\"GIVEN1\",\"GIVEN2\"]\n" + " \"given\":[\"GIVEN1\",\"GIVEN2\"]\n" +
" },{\n" + " },{\n" +
" \"given\":[\"GivenOnly1\",\"GivenOnly2\"]\n" + " \"given\":[\"GivenOnly1\",\"GivenOnly2\"]\n" +
" }]\n" + " }]\n" +
"}", responseContent); "}"), TestUtil.stripReturns(responseContent));
assertThat(status.getFirstHeader(Constants.HEADER_CONTENT_TYPE).getValue(), Matchers.startsWith("application/json"));
} finally {
IOUtils.closeQuietly(status.getEntity().getContent());
}
}
@Test
@org.junit.Ignore
public void testGraphSystemInstance() throws Exception {
String query = "{Patient(id:123){id,name{given,family}}}";
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/$graphql?query=" + UrlUtil.escape(query));
CloseableHttpResponse status = ourClient.execute(httpGet);
try {
String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8);
ourLog.info(responseContent);
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals("{\n" +
" \"Patient\":{\n" +
" \"name\":[{\n" +
" \"given\":[\"GIVEN1\",\"GIVEN2\"],\n" +
" \"family\":[\"FAMILY\"]\n" +
" },{\n" +
" \"given\":[\"GivenOnly1\",\"GivenOnly2\"]\n" +
" }]\n" +
" }\n" +
"}", responseContent);
assertThat(status.getFirstHeader(Constants.HEADER_CONTENT_TYPE).getValue(), Matchers.startsWith("application/json"));
} finally {
IOUtils.closeQuietly(status.getEntity().getContent());
}
}
@Test
@Ignore
public void testGraphSystemList() throws Exception {
String query = "{PatientList(name:\"pet\"){name{family,given}}}";
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/$graphql?query=" + UrlUtil.escape(query));
CloseableHttpResponse status = ourClient.execute(httpGet);
try {
String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8);
ourLog.info(responseContent);
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals("{\n" +
" \"PatientList\":[{\n" +
" \"name\":[{\n" +
" \"family\":[\"pet\"],\n" +
" \"given\":[\"GIVEN1\",\"GIVEN2\"]\n" +
" },{\n" +
" \"given\":[\"GivenOnly1\",\"GivenOnly2\"]\n" +
" }]\n" +
" },{\n" +
" \"name\":[{\n" +
" \"given\":[\"GivenOnlyB1\",\"GivenOnlyB2\"]\n" +
" }]\n" +
" }]\n" +
"}", responseContent);
assertThat(status.getFirstHeader(Constants.HEADER_CONTENT_TYPE).getValue(), Matchers.startsWith("application/json")); assertThat(status.getFirstHeader(Constants.HEADER_CONTENT_TYPE).getValue(), Matchers.startsWith("application/json"));
} finally { } finally {
@ -186,12 +91,12 @@ public class GraphQLDstu3ProviderTest {
ourLog.info(responseContent); ourLog.info(responseContent);
assertEquals(200, status.getStatusLine().getStatusCode()); assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals("{\n" + assertEquals(TestUtil.stripReturns("{\n" +
" \"name\":[{\n" + " \"name\":[{\n" +
" \"given\":[\"GIVEN1\",\"GIVEN2\"],\n" + " \"given\":[\"GIVEN1\",\"GIVEN2\"],\n" +
" \"family\":[\"FAMILY\"]\n" + " \"family\":[\"FAMILY\"]\n" +
" }]\n" + " }]\n" +
"}", responseContent); "}"), TestUtil.stripReturns(responseContent));
assertThat(status.getFirstHeader(Constants.HEADER_CONTENT_TYPE).getValue(), Matchers.startsWith("application/json")); assertThat(status.getFirstHeader(Constants.HEADER_CONTENT_TYPE).getValue(), Matchers.startsWith("application/json"));
} finally { } finally {
@ -200,6 +105,100 @@ public class GraphQLDstu3ProviderTest {
} }
@Test
@org.junit.Ignore
public void testGraphSystemInstance() throws Exception {
String query = "{Patient(id:123){id,name{given,family}}}";
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/$graphql?query=" + UrlUtil.escape(query));
CloseableHttpResponse status = ourClient.execute(httpGet);
try {
String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8);
ourLog.info(responseContent);
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals(TestUtil.stripReturns("{\n" +
" \"Patient\":{\n" +
" \"name\":[{\n" +
" \"given\":[\"GIVEN1\",\"GIVEN2\"],\n" +
" \"family\":[\"FAMILY\"]\n" +
" },{\n" +
" \"given\":[\"GivenOnly1\",\"GivenOnly2\"]\n" +
" }]\n" +
" }\n" +
"}"), TestUtil.stripReturns(responseContent));
assertThat(status.getFirstHeader(Constants.HEADER_CONTENT_TYPE).getValue(), Matchers.startsWith("application/json"));
} finally {
IOUtils.closeQuietly(status.getEntity().getContent());
}
}
@Test
@Ignore
public void testGraphSystemList() throws Exception {
String query = "{PatientList(name:\"pet\"){name{family,given}}}";
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/$graphql?query=" + UrlUtil.escape(query));
CloseableHttpResponse status = ourClient.execute(httpGet);
try {
String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8);
ourLog.info(responseContent);
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals(TestUtil.stripReturns("{\n" +
" \"PatientList\":[{\n" +
" \"name\":[{\n" +
" \"family\":[\"pet\"],\n" +
" \"given\":[\"GIVEN1\",\"GIVEN2\"]\n" +
" },{\n" +
" \"given\":[\"GivenOnly1\",\"GivenOnly2\"]\n" +
" }]\n" +
" },{\n" +
" \"name\":[{\n" +
" \"given\":[\"GivenOnlyB1\",\"GivenOnlyB2\"]\n" +
" }]\n" +
" }]\n" +
"}"), TestUtil.stripReturns(responseContent));
assertThat(status.getFirstHeader(Constants.HEADER_CONTENT_TYPE).getValue(), Matchers.startsWith("application/json"));
} finally {
IOUtils.closeQuietly(status.getEntity().getContent());
}
}
@AfterClass
public static void afterClassClearContext() throws Exception {
ourServer.stop();
TestUtil.clearAllStaticFieldsForUnitTest();
}
@BeforeClass
public static void beforeClass() throws Exception {
ourPort = PortUtil.findFreePort();
ourServer = new Server(ourPort);
ServletHandler proxyHandler = new ServletHandler();
RestfulServer servlet = new RestfulServer(ourCtx);
servlet.setDefaultResponseEncoding(EncodingEnum.JSON);
servlet.setPagingProvider(new FifoMemoryPagingProvider(10));
servlet.registerProvider(new DummyPatientResourceProvider());
MyStorageServices storageServices = new MyStorageServices();
servlet.registerProvider(new GraphQLProviderDstu3(ourCtx, new DefaultProfileValidationSupport(), storageServices));
ServletHolder servletHolder = new ServletHolder(servlet);
proxyHandler.addServletWithMapping(servletHolder, "/*");
ourServer.setHandler(proxyHandler);
ourServer.start();
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS);
HttpClientBuilder builder = HttpClientBuilder.create();
builder.setConnectionManager(connectionManager);
ourClient = builder.build();
}
public static class DummyPatientResourceProvider implements IResourceProvider { public static class DummyPatientResourceProvider implements IResourceProvider {
@Override @Override
@ -225,31 +224,6 @@ public class GraphQLDstu3ProviderTest {
} }
private static class MyStorageServices implements IGraphQLStorageServices<Resource, Reference, Bundle> { private static class MyStorageServices implements IGraphQLStorageServices<Resource, Reference, Bundle> {
@Override
public ReferenceResolution<Resource> lookup(Object theAppInfo, Resource theContext, Reference theReference) throws FHIRException {
ourLog.info("lookup from {} to {}", theContext.getIdElement().getValue(), theReference.getReference());
return null;
}
@Override
public Resource lookup(Object theAppInfo, String theType, String theId) throws FHIRException {
ourLog.info("lookup {}/{}", theType, theId);
if (theType.equals("Patient") && theId.equals("123")) {
Patient p = new Patient();
p.addName()
.setFamily("FAMILY")
.addGiven("GIVEN1")
.addGiven("GIVEN2");
p.addName()
.addGiven("GivenOnly1")
.addGiven("GivenOnly2");
return p;
}
return null;
}
@Override @Override
public void listResources(Object theAppInfo, String theType, List<Argument> theSearchParams, List<Resource> theMatches) throws FHIRException { public void listResources(Object theAppInfo, String theType, List<Argument> theSearchParams, List<Resource> theMatches) throws FHIRException {
ourLog.info("listResources of {} - {}", theType, theSearchParams); ourLog.info("listResources of {} - {}", theType, theSearchParams);
@ -277,6 +251,31 @@ public class GraphQLDstu3ProviderTest {
} }
} }
@Override
public Resource lookup(Object theAppInfo, String theType, String theId) throws FHIRException {
ourLog.info("lookup {}/{}", theType, theId);
if (theType.equals("Patient") && theId.equals("123")) {
Patient p = new Patient();
p.addName()
.setFamily("FAMILY")
.addGiven("GIVEN1")
.addGiven("GIVEN2");
p.addName()
.addGiven("GivenOnly1")
.addGiven("GivenOnly2");
return p;
}
return null;
}
@Override
public ReferenceResolution<Resource> lookup(Object theAppInfo, Resource theContext, Reference theReference) throws FHIRException {
ourLog.info("lookup from {} to {}", theContext.getIdElement().getValue(), theReference.getReference());
return null;
}
@Override @Override
public Bundle search(Object theAppInfo, String theType, List<Argument> theSearchParams) throws FHIRException { public Bundle search(Object theAppInfo, String theType, List<Argument> theSearchParams) throws FHIRException {
ourLog.info("search on {} - {}", theType, theSearchParams); ourLog.info("search on {} - {}", theType, theSearchParams);

View File

@ -166,6 +166,10 @@
being acceptable for CORS requests in CorsInterceptor, CLI, and JPA Example. being acceptable for CORS requests in CorsInterceptor, CLI, and JPA Example.
Thanks to Patrick Werner for the pull request! Thanks to Patrick Werner for the pull request!
</action> </action>
<action type="fix" issue="725">
DSTU2-hl7org and DSTU2.1 structures did not copy resource IDs when invoking
copyValues(). Thanks to Clayton Bodendein for the pull request!
</action>
</release> </release>
<release version="3.0.0" date="2017-09-27"> <release version="3.0.0" date="2017-09-27">
<action type="add"> <action type="add">