/**
* <strong>Serious Business:</strong> deletes all resources from the FHIR
* server used in tests.
*/
public static void cleanFhirServer() {
// Before disabling this check, please go and update your resume.
if (!FHIR_API.contains("localhost"))
throw new BadCodeMonkeyException("Saving you from a career-changing event.");
IGenericClient fhirClient = createFhirClient();
Conformance conformance = fhirClient.fetchConformance().ofType(Conformance.class).execute();
/*
* This is ugly code, but not worth making more readable. Here's what it
* does: grabs the server's conformance statement, looks at the
* supported resources, and then grabs all of the resource type names
* that support bulk conditional delete operations.
*/
List<String> resourcesToDelete = conformance.getRest().stream()
.filter(r -> r.getMode() == RestfulConformanceMode.SERVER).flatMap(r -> r.getResource().stream())
.filter(r -> r.getConditionalDelete() != null)
.filter(r -> r.getConditionalDelete() == ConditionalDeleteStatus.MULTIPLE).map(r -> r.getType())
.collect(Collectors.toList());
// Loop over each resource that can be deleted, and delete all of them.
/*
* TODO This commented-out version should work, given HAPI 1.4's
* conformance statement, but doesn't. Try again in a later version? The
* not-commented-out version below does work, but is slower.
*/
// for (String resourceTypeName : resourcesToDelete)
// fhirClient.delete().resourceConditionalByUrl(resourceTypeName).execute();
for (String resourceTypeName : resourcesToDelete) {
Bundle results = fhirClient.search().forResource(resourceTypeName).returnBundle(Bundle.class).execute();
while (true) {
for (BundleEntryComponent resourceEntry : results.getEntry())
fhirClient.delete()
.resourceById(resourceTypeName, resourceEntry.getResource().getIdElement().getIdPart())
.execute();
// Get next page of results (if there is one), or exit loop.
if (results.getLink(Bundle.LINK_NEXT) != null)
results = fhirClient.loadPage().next(results).execute();
else
break;
}
}
}