Skip to content

Commit

Permalink
Test bundle logic correction (#500)
Browse files Browse the repository at this point in the history
* Correcting issue in which Bundle test files were being bundled as a transaction.

* Commenting out R5 bundle check for now (processing of R5 not fully implemented, could break things.)

* Ensuring bundle type in test file is transaction and requests are PUT

---------

Co-authored-by: Evan Chicoine <[email protected]>
Co-authored-by: JP <[email protected]>
  • Loading branch information
3 people authored Dec 2, 2023
1 parent b2a82fc commit 8a2ccc9
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public boolean accept(File dir, String name) {
try {
IBaseResource resource = IOUtils.readResource(file.getAbsolutePath(), fhirContext, true);
//ensure the resource can be posted
if (resourceIsTransactionBundle(resource)) {
if (BundleUtils.resourceIsTransactionBundle(resource)) {
BundleUtils.postBundle(encoding, fhirContext, fhirUri, resource);
}
} catch (Exception e) {
Expand All @@ -155,19 +155,6 @@ public boolean accept(File dir, String name) {
}
}

private boolean resourceIsTransactionBundle(IBaseResource inputResource) {
if (inputResource == null) return false;

if (inputResource instanceof org.hl7.fhir.dstu3.model.Bundle) {
return ((org.hl7.fhir.dstu3.model.Bundle) inputResource).getType().equals(org.hl7.fhir.dstu3.model.Bundle.BundleType.TRANSACTION);

} else if (inputResource instanceof org.hl7.fhir.r4.model.Bundle) {
return ((org.hl7.fhir.r4.model.Bundle) inputResource).getType().equals(org.hl7.fhir.r4.model.Bundle.BundleType.TRANSACTION);
}
return false;

}

@Override
protected String getSourcePath(FhirContext fhirContext, Map.Entry<String, IBaseResource> resourceEntry) {
return IOUtils.getMeasurePathMap(fhirContext).get(resourceEntry.getKey());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,17 @@ public void bundleResources(ArrayList<String> refreshedLibraryNames, String igPa
if (shouldPersist) {

String bundleDestPath = FilenameUtils.concat(FilenameUtils.concat(IGProcessor.getBundlesPath(igPath), getResourceTestGroupName()), resourceName);

persistBundle(igPath, bundleDestPath, resourceName, encoding, fhirContext, new ArrayList<IBaseResource>(resources.values()), fhirUri, addBundleTimestamp);

String possibleBundleTestMessage = bundleFiles(igPath, bundleDestPath, resourceName, binaryPaths, resourceSourcePath,
primaryLibrarySourcePath, fhirContext, encoding, includeTerminology, includeDependencies, includePatientScenarios,
includeVersion, addBundleTimestamp, translatorWarningMessages);

//Check for test files in bundleDestPath + "-files", loop through if exists,
// find all files that start with "tests-", post to fhir server following same folder structure:
persistTestFiles(bundleDestPath, resourceName, encoding, fhirContext, fhirUri);

if (cdsHooksProcessor != null) {
List<String> activityDefinitionPaths = CDSHooksProcessor.bundleActivityDefinitions(resourceSourcePath, fhirContext, resources, encoding, includeVersion, shouldPersist);
cdsHooksProcessor.addActivityDefinitionFilesToBundle(igPath, bundleDestPath, activityDefinitionPaths, fhirContext, encoding);
Expand Down Expand Up @@ -372,10 +378,6 @@ private String bundleFiles(String igPath, String bundleDestPath, String primaryL


private void persistBundle(String igPath, String bundleDestPath, String libraryName, IOUtils.Encoding encoding, FhirContext fhirContext, List<IBaseResource> resources, String fhirUri, Boolean addBundleTimestamp) {
//Check for test files in bundleDestPath + "-files", loop through if exists,
// find all files that start with "tests-", post to fhir server following same folder structure:
persistTestFiles(bundleDestPath, libraryName, encoding, fhirContext, fhirUri);

IOUtils.initializeDirectory(bundleDestPath);
Object bundle = BundleUtils.bundleArtifacts(libraryName, resources, fhirContext, addBundleTimestamp, this.getIdentifiers());
IOUtils.writeBundle(bundle, bundleDestPath, encoding, fhirContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import javax.annotation.Nullable;
import java.util.*;

import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.context.FhirVersionEnum;
import ca.uhn.fhir.model.api.IFhirVersion;

Expand Down Expand Up @@ -87,8 +86,16 @@ public void refreshTestCases(String path, IOUtils.Encoding encoding, FhirContext
}
}

Object bundle = BundleUtils.bundleArtifacts(getId(FilenameUtils.getName(testCasePath)), resources, fhirContext, false);
// If the resource is a transaction bundle then don't bundle it again otherwise do
String fileId = getId(FilenameUtils.getName(testCasePath));
Object bundle;
if ((resources.size() == 1) && (BundleUtils.resourceIsABundle(resources.get(0)))) {
bundle = processTestBundle(fileId, resources.get(0), fhirContext);
}else {
bundle = BundleUtils.bundleArtifacts(fileId, resources, fhirContext, false);
}
IOUtils.writeBundle(bundle, testArtifactPath, encoding, fhirContext);

} catch (Exception e) {
LogUtils.putException(testCasePath, e);
} finally {
Expand All @@ -106,6 +113,40 @@ public void refreshTestCases(String path, IOUtils.Encoding encoding, FhirContext
}
}

public static Object processTestBundle(String id, IBaseResource resource, FhirContext fhirContext) {
switch (fhirContext.getVersion().getVersion()) {
case DSTU3:
org.hl7.fhir.dstu3.model.Bundle dstu3Bundle = (org.hl7.fhir.dstu3.model.Bundle) resource;
ResourceUtils.setIgId(id, dstu3Bundle, false);
dstu3Bundle.setType(org.hl7.fhir.dstu3.model.Bundle.BundleType.TRANSACTION);

for (org.hl7.fhir.dstu3.model.Bundle.BundleEntryComponent entry : dstu3Bundle.getEntry()) {
org.hl7.fhir.dstu3.model.Bundle.BundleEntryRequestComponent request = new org.hl7.fhir.dstu3.model.Bundle.BundleEntryRequestComponent();
request.setMethod(org.hl7.fhir.dstu3.model.Bundle.HTTPVerb.PUT);
request.setUrl(entry.getResource().fhirType() + "/" + entry.getResource().getIdElement().getIdPart());
entry.setRequest(request);
}

return dstu3Bundle;

case R4:
org.hl7.fhir.r4.model.Bundle r4Bundle = (org.hl7.fhir.r4.model.Bundle)resource;
ResourceUtils.setIgId(id, r4Bundle, false);
r4Bundle.setType(org.hl7.fhir.r4.model.Bundle.BundleType.TRANSACTION);
for (org.hl7.fhir.r4.model.Bundle.BundleEntryComponent entry : r4Bundle.getEntry()) {
org.hl7.fhir.r4.model.Bundle.BundleEntryRequestComponent request = new org.hl7.fhir.r4.model.Bundle.BundleEntryRequestComponent();
request.setMethod(org.hl7.fhir.r4.model.Bundle.HTTPVerb.PUT); // Adjust the HTTP method as needed
request.setUrl(entry.getResource().fhirType() + "/" + entry.getResource().getIdElement().getIdPart());
entry.setRequest(request);
}

return r4Bundle;
default:
throw new IllegalArgumentException("Unknown fhir version: " + fhirContext.getVersion().getVersion().getFhirVersionString());
}
}


private void addPatientToGroupR4(Group group, org.hl7.fhir.r4.model.Patient patient) {
IdType idType = patient.getIdElement();
org.hl7.fhir.r4.model.Group.GroupMemberComponent member = group.addMember();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public static Object bundleArtifacts(String id, List<IBaseResource> resources, F
}
}



public static org.hl7.fhir.dstu3.model.Bundle bundleStu3Artifacts(String id, List<IBaseResource> resources) {
org.hl7.fhir.dstu3.model.Bundle bundle = new org.hl7.fhir.dstu3.model.Bundle();
ResourceUtils.setIgId(id, bundle, false);
Expand Down Expand Up @@ -107,11 +109,10 @@ public static org.hl7.fhir.r4.model.Bundle bundleR4Artifacts(String id, List<IBa
}

public static void postBundle(IOUtils.Encoding encoding, FhirContext fhirContext, String fhirUri, IBaseResource bundle) {
if (fhirUri != null && !fhirUri.equals("")) {
if (fhirUri != null && !fhirUri.isEmpty()) {
try {
HttpClientUtils.post(fhirUri, bundle, encoding, fhirContext);
} catch (IOException e) {
e.printStackTrace();
LogUtils.putException(bundle.getIdElement().getIdPart(), "Error posting to FHIR Server: " + fhirUri + ". Bundle not posted.");
}
}
Expand Down Expand Up @@ -251,4 +252,25 @@ public static List<org.hl7.fhir.dstu3.model.Resource> getStu3ResourcesFromBundle
return resourceArrayList;
}

public static boolean resourceIsABundle(IBaseResource resource) {
return (
(resource instanceof org.hl7.fhir.dstu3.model.Bundle)
// uncomment when R5 processing is accounted for
// || (resource instanceof org.hl7.fhir.r5.model.Bundle)
|| (resource instanceof org.hl7.fhir.r4.model.Bundle)
);
}

public static boolean resourceIsTransactionBundle(IBaseResource inputResource) {
if (inputResource == null) return false;

if (inputResource instanceof org.hl7.fhir.dstu3.model.Bundle) {
return ((org.hl7.fhir.dstu3.model.Bundle) inputResource).getType().equals(org.hl7.fhir.dstu3.model.Bundle.BundleType.TRANSACTION);

} else if (inputResource instanceof org.hl7.fhir.r4.model.Bundle) {
return ((org.hl7.fhir.r4.model.Bundle) inputResource).getType().equals(org.hl7.fhir.r4.model.Bundle.BundleType.TRANSACTION);
}
return false;

}
}

0 comments on commit 8a2ccc9

Please sign in to comment.