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

This commit is contained in:
jamesagnew 2018-02-12 11:52:58 -05:00
commit d259b9a65d
2 changed files with 197 additions and 116 deletions

View File

@ -30,7 +30,6 @@ import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.*;
import org.apache.http.entity.ByteArrayEntity;
@ -132,68 +131,6 @@ public class ResourceProviderR4Test extends BaseResourceProviderR4Test {
return retVal;
}
@Test
public void testSelfReferentialInclude() {
Location loc1 = new Location();
loc1.setName("loc1");
IIdType loc1id = myClient.create().resource(loc1).execute().getId().toUnqualifiedVersionless();
Location loc2 = new Location();
loc2.setName("loc2");
IIdType loc2id = myClient.create().resource(loc2).execute().getId().toUnqualifiedVersionless();
loc1 = new Location();
loc1.setId(loc1id);
loc1.setName("loc1");
loc1.getPartOf().setReference(loc2id.getValue());
myClient.update().resource(loc1).execute().getId().toUnqualifiedVersionless();
loc2 = new Location();
loc2.setId(loc2id);
loc2.setName("loc2");
loc2.getPartOf().setReference(loc1id.getValue());
myClient.update().resource(loc2).execute().getId().toUnqualifiedVersionless();
IBaseBundle result = myClient
.search()
.forResource(Location.class)
.where(Location.NAME.matches().value("loc1"))
.include(Location.INCLUDE_PARTOF.asRecursive())
.execute();
assertThat(toUnqualifiedVersionlessIdValues(result), contains(loc1id.getValue(), loc2id.getValue()));
}
@Test
public void testSelfReferentialRevInclude() {
Location loc1 = new Location();
loc1.setName("loc1");
IIdType loc1id = myClient.create().resource(loc1).execute().getId().toUnqualifiedVersionless();
Location loc2 = new Location();
loc2.setName("loc2");
IIdType loc2id = myClient.create().resource(loc2).execute().getId().toUnqualifiedVersionless();
loc1 = new Location();
loc1.setId(loc1id);
loc1.setName("loc1");
loc1.getPartOf().setReference(loc2id.getValue());
myClient.update().resource(loc1).execute().getId().toUnqualifiedVersionless();
loc2 = new Location();
loc2.setId(loc2id);
loc2.setName("loc2");
loc2.getPartOf().setReference(loc1id.getValue());
myClient.update().resource(loc2).execute().getId().toUnqualifiedVersionless();
IBaseBundle result = myClient
.search()
.forResource(Location.class)
.where(Location.NAME.matches().value("loc1"))
.revInclude(Location.INCLUDE_PARTOF.asRecursive())
.execute();
assertThat(toUnqualifiedVersionlessIdValues(result), contains(loc1id.getValue(), loc2id.getValue()));
}
/**
* See #484
*/
@ -1745,31 +1682,6 @@ public class ResourceProviderR4Test extends BaseResourceProviderR4Test {
assertThat(ids, contains(pid0.getValue()));
}
// private void delete(String theResourceType, String theParamName, String theParamValue) {
// Bundle resources;
// do {
// IQuery<Bundle> forResource = myClient.search().forResource(theResourceType);
// if (theParamName != null) {
// forResource = forResource.where(new StringClientParam(theParamName).matches().value(theParamValue));
// }
// resources = forResource.execute();
// for (IResource next : resources.toListOfResources()) {
// ourLog.info("Deleting resource: {}", next.getId());
// myClient.delete().resource(next).execute();
// }
// } while (resources.size() > 0);
// }
//
// private void deleteToken(String theResourceType, String theParamName, String theParamSystem, String theParamValue)
// {
// Bundle resources = myClient.search().forResource(theResourceType).where(new
// TokenClientParam(theParamName).exactly().systemAndCode(theParamSystem, theParamValue)).execute();
// for (IResource next : resources.toListOfResources()) {
// ourLog.info("Deleting resource: {}", next.getId());
// myClient.delete().resource(next).execute();
// }
// }
@Test
public void testHasParameterNoResults() throws Exception {
@ -1860,6 +1772,31 @@ public class ResourceProviderR4Test extends BaseResourceProviderR4Test {
}
}
// private void delete(String theResourceType, String theParamName, String theParamValue) {
// Bundle resources;
// do {
// IQuery<Bundle> forResource = myClient.search().forResource(theResourceType);
// if (theParamName != null) {
// forResource = forResource.where(new StringClientParam(theParamName).matches().value(theParamValue));
// }
// resources = forResource.execute();
// for (IResource next : resources.toListOfResources()) {
// ourLog.info("Deleting resource: {}", next.getId());
// myClient.delete().resource(next).execute();
// }
// } while (resources.size() > 0);
// }
//
// private void deleteToken(String theResourceType, String theParamName, String theParamSystem, String theParamValue)
// {
// Bundle resources = myClient.search().forResource(theResourceType).where(new
// TokenClientParam(theParamName).exactly().systemAndCode(theParamSystem, theParamValue)).execute();
// for (IResource next : resources.toListOfResources()) {
// ourLog.info("Deleting resource: {}", next.getId());
// myClient.delete().resource(next).execute();
// }
// }
@Test
public void testIdAndVersionInBodyForCreate() throws IOException {
String methodName = "testIdAndVersionInBodyForCreate";
@ -1967,6 +1904,39 @@ public class ResourceProviderR4Test extends BaseResourceProviderR4Test {
}
/**
* See #793
*/
@Test
public void testIncludeCountDoesntIncludeIncludes() {
Organization org = new Organization();
org.setName("ORG");
IIdType orgId = myOrganizationDao.create(org).getId().toUnqualifiedVersionless();
for (int i = 0; i < 10; i++) {
Patient pt = new Patient();
pt.getManagingOrganization().setReference(orgId.getValue());
pt.addName().setFamily("FAM" + i);
myPatientDao.create(pt);
}
Bundle bundle = myClient
.search()
.forResource(Patient.class)
.include(Patient.INCLUDE_ORGANIZATION)
.count(2)
.returnBundle(Bundle.class)
.execute();
ourLog.info(myFhirCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(bundle));
assertEquals(3, bundle.getEntry().size());
assertEquals("Patient", bundle.getEntry().get(0).getResource().getIdElement().getResourceType());
assertEquals("Patient", bundle.getEntry().get(1).getResource().getIdElement().getResourceType());
assertEquals("Organization", bundle.getEntry().get(2).getResource().getIdElement().getResourceType());
assertEquals(10, bundle.getTotal());
}
@Test
public void testIncludeWithExternalReferences() {
myDaoConfig.setAllowExternalReferences(true);
@ -2413,30 +2383,6 @@ public class ResourceProviderR4Test extends BaseResourceProviderR4Test {
}
}
@Test
public void testRetrieveMissingVersionsDoesntCrashSearch() {
Patient p1 = new Patient();
p1.setActive(true);
final IIdType id1 = myClient.create().resource(p1).execute().getId();
Patient p2 = new Patient();
p2.setActive(false);
IIdType id2 = myClient.create().resource(p2).execute().getId();
new TransactionTemplate(myTxManager).execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
ResourceHistoryTable version = myResourceHistoryTableDao.findForIdAndVersion(id1.getIdPartAsLong(), 1);
myResourceHistoryTableDao.delete(version);
}
});
Bundle bundle = myClient.search().forResource("Patient").returnBundle(Bundle.class).execute();
assertEquals(2, bundle.getTotal());
assertEquals(1, bundle.getEntry().size());
assertEquals(id2.getIdPart(), bundle.getEntry().get(0).getResource().getIdElement().getIdPart());
}
@Test
public void testRetrieveMissingVersionsDoesntCrashHistory() {
Patient p1 = new Patient();
@ -2461,6 +2407,30 @@ public class ResourceProviderR4Test extends BaseResourceProviderR4Test {
assertEquals(id2.getIdPart(), bundle.getEntry().get(0).getResource().getIdElement().getIdPart());
}
@Test
public void testRetrieveMissingVersionsDoesntCrashSearch() {
Patient p1 = new Patient();
p1.setActive(true);
final IIdType id1 = myClient.create().resource(p1).execute().getId();
Patient p2 = new Patient();
p2.setActive(false);
IIdType id2 = myClient.create().resource(p2).execute().getId();
new TransactionTemplate(myTxManager).execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
ResourceHistoryTable version = myResourceHistoryTableDao.findForIdAndVersion(id1.getIdPartAsLong(), 1);
myResourceHistoryTableDao.delete(version);
}
});
Bundle bundle = myClient.search().forResource("Patient").returnBundle(Bundle.class).execute();
assertEquals(2, bundle.getTotal());
assertEquals(1, bundle.getEntry().size());
assertEquals(id2.getIdPart(), bundle.getEntry().get(0).getResource().getIdElement().getIdPart());
}
@Test
public void testSaveAndRetrieveExistingNarrativeJson() {
Patient p1 = new Patient();
@ -3757,6 +3727,68 @@ public class ResourceProviderR4Test extends BaseResourceProviderR4Test {
testSearchReturnsResults("/Observation?code%3Atext=THIS_IS_THE_disp");
}
@Test
public void testSelfReferentialInclude() {
Location loc1 = new Location();
loc1.setName("loc1");
IIdType loc1id = myClient.create().resource(loc1).execute().getId().toUnqualifiedVersionless();
Location loc2 = new Location();
loc2.setName("loc2");
IIdType loc2id = myClient.create().resource(loc2).execute().getId().toUnqualifiedVersionless();
loc1 = new Location();
loc1.setId(loc1id);
loc1.setName("loc1");
loc1.getPartOf().setReference(loc2id.getValue());
myClient.update().resource(loc1).execute().getId().toUnqualifiedVersionless();
loc2 = new Location();
loc2.setId(loc2id);
loc2.setName("loc2");
loc2.getPartOf().setReference(loc1id.getValue());
myClient.update().resource(loc2).execute().getId().toUnqualifiedVersionless();
IBaseBundle result = myClient
.search()
.forResource(Location.class)
.where(Location.NAME.matches().value("loc1"))
.include(Location.INCLUDE_PARTOF.asRecursive())
.execute();
assertThat(toUnqualifiedVersionlessIdValues(result), contains(loc1id.getValue(), loc2id.getValue()));
}
@Test
public void testSelfReferentialRevInclude() {
Location loc1 = new Location();
loc1.setName("loc1");
IIdType loc1id = myClient.create().resource(loc1).execute().getId().toUnqualifiedVersionless();
Location loc2 = new Location();
loc2.setName("loc2");
IIdType loc2id = myClient.create().resource(loc2).execute().getId().toUnqualifiedVersionless();
loc1 = new Location();
loc1.setId(loc1id);
loc1.setName("loc1");
loc1.getPartOf().setReference(loc2id.getValue());
myClient.update().resource(loc1).execute().getId().toUnqualifiedVersionless();
loc2 = new Location();
loc2.setId(loc2id);
loc2.setName("loc2");
loc2.getPartOf().setReference(loc1id.getValue());
myClient.update().resource(loc2).execute().getId().toUnqualifiedVersionless();
IBaseBundle result = myClient
.search()
.forResource(Location.class)
.where(Location.NAME.matches().value("loc1"))
.revInclude(Location.INCLUDE_PARTOF.asRecursive())
.execute();
assertThat(toUnqualifiedVersionlessIdValues(result), contains(loc1id.getValue(), loc2id.getValue()));
}
@Test
public void testSmallResultIncludes() {
Patient p = new Patient();

View File

@ -5,10 +5,7 @@ import ca.uhn.fhir.util.TestUtil;
import com.google.common.collect.Sets;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.hl7.fhir.r4.model.Bundle;
import org.hl7.fhir.r4.model.Extension;
import org.hl7.fhir.r4.model.Patient;
import org.hl7.fhir.r4.model.StringType;
import org.hl7.fhir.r4.model.*;
import org.junit.AfterClass;
import org.junit.Test;
import org.slf4j.Logger;
@ -39,6 +36,58 @@ public class JsonParserR4Test {
return b;
}
/**
* See #814
*/
@Test
public void testDuplicateContainedResourcesNotOutputtedTwice() {
MedicationDispense md = new MedicationDispense();
MedicationRequest mr = new MedicationRequest();
md.addAuthorizingPrescription().setResource(mr);
Medication med = new Medication();
md.setMedication(new Reference(med));
mr.setMedication(new Reference(med));
String encoded = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(md);
ourLog.info(encoded);
int idx = encoded.indexOf("\"Medication\"");
assertNotEquals(-1, idx);
idx = encoded.indexOf("\"Medication\"", idx + 1);
assertEquals(-1, idx);
}
/**
* See #814
*/
@Test
public void testDuplicateContainedResourcesNotOutputtedTwiceWithManualIds() {
MedicationDispense md = new MedicationDispense();
MedicationRequest mr = new MedicationRequest();
mr.setId("#MR");
md.addAuthorizingPrescription().setResource(mr);
Medication med = new Medication();
med.setId("#MED");
md.setMedication(new Reference(med));
mr.setMedication(new Reference(med));
String encoded = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(md);
ourLog.info(encoded);
int idx = encoded.indexOf("\"Medication\"");
assertNotEquals(-1, idx);
idx = encoded.indexOf("\"Medication\"", idx + 1);
assertEquals(-1, idx);
}
@Test
public void testExcludeNothing() {
IParser parser = ourCtx.newJsonParser().setPrettyPrint(true);