Skip to content

Commit

Permalink
Add Delete Operation and Tests (#563)
Browse files Browse the repository at this point in the history
Delete Operation for retired Programs
  • Loading branch information
Chris0296 authored and taha.attari@smilecdr.com committed Oct 16, 2024
1 parent a0ca0a6 commit 517035f
Show file tree
Hide file tree
Showing 3 changed files with 12,513 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.opencds.cqf.fhir.utility.visitor;

import ca.uhn.fhir.rest.server.exceptions.PreconditionFailedException;
import java.util.ArrayList;
import org.hl7.fhir.instance.model.api.IBase;
import org.hl7.fhir.instance.model.api.IBaseParameters;
import org.hl7.fhir.instance.model.api.IDomainResource;
import org.opencds.cqf.fhir.api.Repository;
import org.opencds.cqf.fhir.utility.BundleHelper;
import org.opencds.cqf.fhir.utility.PackageHelper;
import org.opencds.cqf.fhir.utility.adapter.IKnowledgeArtifactAdapter;

public class DeleteVisitor extends AbstractKnowledgeArtifactVisitor {

public static final String RETIRED_STATUS = "retired";

@Override
public IBase visit(IKnowledgeArtifactAdapter rootAdapter, Repository repository, IBaseParameters operationParams) {
if (!rootAdapter.getStatus().equals(RETIRED_STATUS)) {
throw new PreconditionFailedException("Cannot delete an artifact that is not in retired status");
}

var fhirVersion = rootAdapter.get().getStructureFhirVersionEnum();
var transactionBundle = BundleHelper.newBundle(fhirVersion, null, "transaction");

var resToUpdate = new ArrayList<IDomainResource>();
resToUpdate.add(rootAdapter.get());

var resourcesToUpdate = getComponents(rootAdapter, repository, resToUpdate);

for (var res : resourcesToUpdate) {
var entry = PackageHelper.deleteEntry(res);
BundleHelper.addEntry(transactionBundle, entry);
}

return repository.transaction(transactionBundle);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package org.opencds.cqf.fhir.utility.visitor.r4;

import static org.junit.jupiter.api.Assertions.fail;
import static org.opencds.cqf.fhir.utility.r4.Parameters.parameters;
import static org.opencds.cqf.fhir.utility.r4.Parameters.part;

import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.parser.IParser;
import ca.uhn.fhir.rest.server.exceptions.PreconditionFailedException;
import org.hl7.fhir.r4.model.Bundle;
import org.hl7.fhir.r4.model.IdType;
import org.hl7.fhir.r4.model.Library;
import org.hl7.fhir.r4.model.Parameters;
import org.hl7.fhir.r4.model.SearchParameter;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.opencds.cqf.fhir.api.Repository;
import org.opencds.cqf.fhir.utility.adapter.ILibraryAdapter;
import org.opencds.cqf.fhir.utility.adapter.r4.AdapterFactory;
import org.opencds.cqf.fhir.utility.repository.InMemoryFhirRepository;
import org.opencds.cqf.fhir.utility.visitor.DeleteVisitor;
import org.opencds.cqf.fhir.utility.visitor.IKnowledgeArtifactVisitor;

public class DeleteVisitorTests {

private final FhirContext fhirContext = FhirContext.forR4Cached();
private Repository repo;
private final IParser jsonParser = fhirContext.newJsonParser();

@BeforeEach
void setup() {
SearchParameter sp = (SearchParameter) jsonParser.parseResource(
ReleaseVisitorTests.class.getResourceAsStream("SearchParameter-artifactAssessment.json"));
repo = new InMemoryFhirRepository(fhirContext);
repo.update(sp);
}

@Test
void library_delete_test() {
Bundle bundle =
(Bundle) jsonParser.parseResource(DeleteVisitorTests.class.getResourceAsStream("Bundle-delete.json"));
Bundle tsBundle = repo.transaction(bundle);
// Resource is uploaded using POST - need to get id like this
String id = tsBundle.getEntry().get(0).getResponse().getLocation();
String version = "1.1.0";
Library library = repo.read(Library.class, new IdType(id)).copy();
ILibraryAdapter libraryAdapter = new AdapterFactory().createLibrary(library);
IKnowledgeArtifactVisitor deleteVisitor = new DeleteVisitor();
Parameters params = parameters(part("version", version));
Bundle returnedBundle = (Bundle) libraryAdapter.accept(deleteVisitor, repo, params);

var res = returnedBundle.getEntry();

assert (res.size() == 9);
}

@Test
void library_delete_active_test() {
try {
Bundle bundle = (Bundle)
jsonParser.parseResource(DeleteVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json"));
repo.transaction(bundle);
String version = "1.0.0";
Library library = repo.read(Library.class, new IdType("Library/SpecificationLibrary"))
.copy();
ILibraryAdapter libraryAdapter = new AdapterFactory().createLibrary(library);
IKnowledgeArtifactVisitor deleteVisitor = new DeleteVisitor();
Parameters params = parameters(part("version", version));
libraryAdapter.accept(deleteVisitor, repo, params);

fail("Trying to withdraw an active Library should throw an Exception");
} catch (PreconditionFailedException e) {
assert (e.getMessage().contains("Cannot delete an artifact that is not in retired status"));
}
}

@Test
void library_delete_draft_test() {
try {
Bundle bundle = (Bundle)
jsonParser.parseResource(DeleteVisitorTests.class.getResourceAsStream("Bundle-withdraw.json"));
Bundle tsBundle = repo.transaction(bundle);
String id = tsBundle.getEntry().get(0).getResponse().getLocation();
String version = "1.1.0-draft";
Library library = repo.read(Library.class, new IdType(id)).copy();
ILibraryAdapter libraryAdapter = new AdapterFactory().createLibrary(library);
IKnowledgeArtifactVisitor deleteVisitor = new DeleteVisitor();
Parameters params = parameters(part("version", version));
libraryAdapter.accept(deleteVisitor, repo, params);

fail("Trying to withdraw a draft Library should throw an Exception");
} catch (PreconditionFailedException e) {
assert (e.getMessage().contains("Cannot delete an artifact that is not in retired status"));
}
}
}
Loading

0 comments on commit 517035f

Please sign in to comment.