-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Delete Operation and Tests (#563)
Delete Operation for retired Programs
- Loading branch information
Showing
3 changed files
with
12,513 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/DeleteVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
...hir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/DeleteVisitorTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} | ||
} |
Oops, something went wrong.