From a69a71e420e5e22093538d42da8ee566e2ef52cb Mon Sep 17 00:00:00 2001 From: Brenin Rhodes Date: Tue, 4 Jun 2024 19:05:57 -0600 Subject: [PATCH 01/38] Remove non CRMI logic. Update PackageProcessor to use visitor. --- .../RepositoryTerminologyProvider.java | 2 +- .../cqf/fhir/cr/common/PackageProcessor.java | 42 ++++ .../cqf/fhir/cr/library/LibraryProcessor.java | 61 ++++++ .../PlanDefinitionProcessor.java | 3 +- .../packages/PackageProcessor.java | 131 ------------ .../cqf/fhir/cr/helpers/GeneratedPackage.java | 23 +++ .../cr/library/LibraryProcessorTests.java | 76 +++++++ .../cqf/fhir/cr/library/TestLibrary.java | 194 ++++++++++++++++++ .../cr/plandefinition/PlanDefinition.java | 29 +-- .../PlanDefinitionProcessorTests.java | 20 +- .../dstu3/resources/Library-FHIRHelpers.json | 2 +- ...atientPriorAuthorizationPrepopulation.json | 4 +- .../resources/PlanDefinition-route-one.json | 4 +- ...ctivityDefinition-SendMessageActivity.json | 51 +++++ .../r5/resources/Library-FHIRHelpers.json | 2 +- ...atientPriorAuthorizationPrepopulation.json | 2 +- cqf-fhir-utility/pom.xml | 4 + .../opencds/cqf/fhir/utility/Constants.java | 22 +- .../opencds/cqf/fhir}/utility/ValueSets.java | 2 +- .../adapter/KnowledgeArtifactAdapter.java | 4 +- .../dstu3/KnowledgeArtifactAdapter.java | 9 - .../utility/adapter/dstu3/LibraryAdapter.java | 8 - .../adapter/dstu3/PlanDefinitionAdapter.java | 9 - .../adapter/dstu3/ValueSetAdapter.java | 8 - .../adapter/r4/KnowledgeArtifactAdapter.java | 9 - .../utility/adapter/r4/LibraryAdapter.java | 19 +- .../adapter/r4/PlanDefinitionAdapter.java | 9 - .../utility/adapter/r4/ValueSetAdapter.java | 9 - .../utility/adapter/r5/AdapterFactory.java | 7 +- .../adapter/r5/KnowledgeArtifactAdapter.java | 9 - .../utility/adapter/r5/LibraryAdapter.java | 19 +- .../adapter/r5/PlanDefinitionAdapter.java | 9 - .../utility/adapter/r5/ValueSetAdapter.java | 8 - .../KnowledgeArtifactApproveVisitor.java | 48 ++--- .../KnowledgeArtifactDraftVisitor.java | 39 +--- .../KnowledgeArtifactPackageVisitor.java | 38 +--- .../KnowledgeArtifactReleaseVisitor.java | 55 ++--- .../visitor/KnowledgeArtifactVisitor.java | 9 - .../KnowledgeArtifactPackageVisitor.java | 179 ++-------------- .../r4/KnowledgeArtifactPackageVisitor.java | 180 ++-------------- .../r5/KnowledgeArtifactPackageVisitor.java | 177 ++-------------- .../KnowledgeArtifactPackageVisitorTests.java | 5 + .../KnowledgeArtifactPackageVisitorTests.java | 7 +- .../KnowledgeArtifactPackageVisitorTests.java | 5 + 44 files changed, 636 insertions(+), 916 deletions(-) create mode 100644 cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/common/PackageProcessor.java create mode 100644 cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/library/LibraryProcessor.java delete mode 100644 cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/plandefinition/packages/PackageProcessor.java create mode 100644 cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/helpers/GeneratedPackage.java create mode 100644 cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/library/LibraryProcessorTests.java create mode 100644 cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/library/TestLibrary.java create mode 100644 cqf-fhir-cr/src/test/resources/org/opencds/cqf/fhir/cr/plandefinition/r5/resources/ActivityDefinition-SendMessageActivity.json rename {cqf-fhir-cql/src/main/java/org/opencds/cqf/fhir/cql/engine => cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir}/utility/ValueSets.java (99%) diff --git a/cqf-fhir-cql/src/main/java/org/opencds/cqf/fhir/cql/engine/terminology/RepositoryTerminologyProvider.java b/cqf-fhir-cql/src/main/java/org/opencds/cqf/fhir/cql/engine/terminology/RepositoryTerminologyProvider.java index 37eeb89e1..7d3620f1e 100644 --- a/cqf-fhir-cql/src/main/java/org/opencds/cqf/fhir/cql/engine/terminology/RepositoryTerminologyProvider.java +++ b/cqf-fhir-cql/src/main/java/org/opencds/cqf/fhir/cql/engine/terminology/RepositoryTerminologyProvider.java @@ -22,8 +22,8 @@ import org.opencds.cqf.fhir.api.Repository; import org.opencds.cqf.fhir.cql.engine.terminology.TerminologySettings.VALUESET_EXPANSION_MODE; import org.opencds.cqf.fhir.cql.engine.terminology.TerminologySettings.VALUESET_PRE_EXPANSION_MODE; -import org.opencds.cqf.fhir.cql.engine.utility.ValueSets; import org.opencds.cqf.fhir.utility.FhirPathCache; +import org.opencds.cqf.fhir.utility.ValueSets; import org.opencds.cqf.fhir.utility.search.Searches; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/common/PackageProcessor.java b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/common/PackageProcessor.java new file mode 100644 index 000000000..b39150a72 --- /dev/null +++ b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/common/PackageProcessor.java @@ -0,0 +1,42 @@ +package org.opencds.cqf.fhir.cr.common; + +import static org.opencds.cqf.fhir.utility.Parameters.newBooleanPart; +import static org.opencds.cqf.fhir.utility.Parameters.newParameters; + +import ca.uhn.fhir.context.FhirVersionEnum; +import org.hl7.fhir.instance.model.api.IBase; +import org.hl7.fhir.instance.model.api.IBaseBundle; +import org.hl7.fhir.instance.model.api.IBaseResource; +import org.hl7.fhir.instance.model.api.IDomainResource; +import org.opencds.cqf.fhir.api.Repository; +import org.opencds.cqf.fhir.utility.adapter.AdapterFactory; +import org.opencds.cqf.fhir.utility.visitor.KnowledgeArtifactPackageVisitor; + +public class PackageProcessor implements IPackageProcessor { + protected final Repository repository; + protected final FhirVersionEnum fhirVersion; + protected final KnowledgeArtifactPackageVisitor packageVisitor; + + public PackageProcessor(Repository repository) { + this.repository = repository; + this.fhirVersion = repository.fhirContext().getVersion().getVersion(); + packageVisitor = new KnowledgeArtifactPackageVisitor(); + } + + @Override + public IBaseBundle packageResource(IBaseResource resource) { + return packageResource(resource, "POST"); + } + + @Override + public IBaseBundle packageResource(IBaseResource resource, String method) { + var adapter = + AdapterFactory.forFhirVersion(fhirVersion).createKnowledgeArtifactAdapter((IDomainResource) resource); + IBase[] parts = {}; + var parameters = newParameters( + repository.fhirContext(), + "package-parameters", + newBooleanPart(repository.fhirContext(), "isPut", false, parts)); + return (IBaseBundle) adapter.accept(packageVisitor, repository, parameters); + } +} diff --git a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/library/LibraryProcessor.java b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/library/LibraryProcessor.java new file mode 100644 index 000000000..16155a15f --- /dev/null +++ b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/library/LibraryProcessor.java @@ -0,0 +1,61 @@ +package org.opencds.cqf.fhir.cr.library; + +import static java.util.Objects.requireNonNull; + +import ca.uhn.fhir.context.FhirVersionEnum; +import org.hl7.fhir.instance.model.api.IBaseBundle; +import org.hl7.fhir.instance.model.api.IBaseResource; +import org.hl7.fhir.instance.model.api.IIdType; +import org.hl7.fhir.instance.model.api.IPrimitiveType; +import org.opencds.cqf.cql.engine.model.ModelResolver; +import org.opencds.cqf.fhir.api.Repository; +import org.opencds.cqf.fhir.cql.EvaluationSettings; +import org.opencds.cqf.fhir.cql.engine.model.FhirModelResolverCache; +import org.opencds.cqf.fhir.cr.common.IPackageProcessor; +import org.opencds.cqf.fhir.cr.common.PackageProcessor; +import org.opencds.cqf.fhir.cr.common.ResourceResolver; +import org.opencds.cqf.fhir.utility.monad.Either3; + +public class LibraryProcessor { + protected final ModelResolver modelResolver; + protected final FhirVersionEnum fhirVersion; + protected final IPackageProcessor packageProcessor; + protected Repository repository; + protected EvaluationSettings evaluationSettings; + + public LibraryProcessor(Repository repository) { + this(repository, EvaluationSettings.getDefault()); + } + + public LibraryProcessor(Repository repository, EvaluationSettings evaluationSettings) { + this(repository, evaluationSettings, null); + } + + public LibraryProcessor( + Repository repository, EvaluationSettings evaluationSettings, IPackageProcessor packageProcessor) { + this.repository = requireNonNull(repository, "repository can not be null"); + this.evaluationSettings = requireNonNull(evaluationSettings, "evaluationSettings can not be null"); + fhirVersion = this.repository.fhirContext().getVersion().getVersion(); + modelResolver = FhirModelResolverCache.resolverForVersion(fhirVersion); + this.packageProcessor = packageProcessor != null ? packageProcessor : new PackageProcessor(this.repository); + } + + public EvaluationSettings evaluationSettings() { + return evaluationSettings; + } + + protected , R extends IBaseResource> R resolveLibrary( + Either3 library) { + return new ResourceResolver("Library", repository).resolve(library); + } + + public , R extends IBaseResource> IBaseBundle packageLibrary( + Either3 library) { + return packageProcessor.packageResource(resolveLibrary(library)); + } + + public , R extends IBaseResource> IBaseBundle packageLibrary( + Either3 library, boolean isPut) { + return packageProcessor.packageResource(resolveLibrary(library), isPut ? "PUT" : "POST"); + } +} diff --git a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/plandefinition/PlanDefinitionProcessor.java b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/plandefinition/PlanDefinitionProcessor.java index c3eedf88b..caf2bf029 100644 --- a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/plandefinition/PlanDefinitionProcessor.java +++ b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/plandefinition/PlanDefinitionProcessor.java @@ -18,11 +18,12 @@ import org.opencds.cqf.fhir.cql.engine.model.FhirModelResolverCache; import org.opencds.cqf.fhir.cr.activitydefinition.apply.IRequestResolverFactory; import org.opencds.cqf.fhir.cr.common.IPackageProcessor; +import org.opencds.cqf.fhir.cr.common.PackageProcessor; import org.opencds.cqf.fhir.cr.common.ResourceResolver; import org.opencds.cqf.fhir.cr.plandefinition.apply.ApplyProcessor; import org.opencds.cqf.fhir.cr.plandefinition.apply.ApplyRequest; import org.opencds.cqf.fhir.cr.plandefinition.apply.IApplyProcessor; -import org.opencds.cqf.fhir.cr.plandefinition.packages.PackageProcessor; +// import org.opencds.cqf.fhir.cr.plandefinition.packages.PackageProcessor; import org.opencds.cqf.fhir.utility.Ids; import org.opencds.cqf.fhir.utility.monad.Either3; diff --git a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/plandefinition/packages/PackageProcessor.java b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/plandefinition/packages/PackageProcessor.java deleted file mode 100644 index 788c46ed6..000000000 --- a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/plandefinition/packages/PackageProcessor.java +++ /dev/null @@ -1,131 +0,0 @@ -package org.opencds.cqf.fhir.cr.plandefinition.packages; - -import ca.uhn.fhir.context.FhirVersionEnum; -import org.hl7.fhir.instance.model.api.IBaseBundle; -import org.hl7.fhir.instance.model.api.IBaseResource; -import org.opencds.cqf.fhir.api.Repository; -import org.opencds.cqf.fhir.cr.common.IPackageProcessor; -import org.opencds.cqf.fhir.utility.BundleHelper; - -public class PackageProcessor implements IPackageProcessor { - protected final Repository repository; - protected final FhirVersionEnum fhirVersion; - - public PackageProcessor(Repository repository) { - this.repository = repository; - this.fhirVersion = repository.fhirContext().getVersion().getVersion(); - } - - @Override - public IBaseBundle packageResource(IBaseResource resource) { - return packageResource(resource, "POST"); - } - - @Override - public IBaseBundle packageResource(IBaseResource resource, String method) { - return packagePlanDefinition(resource, method.equals("PUT")); - } - - protected IBaseBundle packagePlanDefinition(IBaseResource planDefinition, boolean isPut) { - switch (fhirVersion) { - case DSTU3: - return packageDstu3((org.hl7.fhir.dstu3.model.PlanDefinition) planDefinition, isPut); - case R4: - return packageR4((org.hl7.fhir.r4.model.PlanDefinition) planDefinition, isPut); - case R5: - return packageR5((org.hl7.fhir.r5.model.PlanDefinition) planDefinition, isPut); - - default: - return null; - } - } - - protected IBaseBundle packageDstu3(org.hl7.fhir.dstu3.model.PlanDefinition planDefinition, boolean isPut) { - var packageBundle = new org.hl7.fhir.dstu3.model.Bundle(); - packageBundle.setType(org.hl7.fhir.dstu3.model.Bundle.BundleType.TRANSACTION); - BundleHelper.addEntry( - packageBundle, org.opencds.cqf.fhir.utility.dstu3.PackageHelper.createEntry(planDefinition, isPut)); - // The CPG IG specifies a main cql library for a PlanDefinition - var libraryCanonical = planDefinition.hasLibrary() - ? new org.hl7.fhir.dstu3.model.StringType( - planDefinition.getLibrary().get(0).getReference()) - : null; - if (libraryCanonical != null) { - var library = (org.hl7.fhir.dstu3.model.Library) - org.opencds.cqf.fhir.utility.dstu3.SearchHelper.searchRepositoryByCanonical( - repository, libraryCanonical); - if (library != null) { - BundleHelper.addEntry( - packageBundle, org.opencds.cqf.fhir.utility.dstu3.PackageHelper.createEntry(library, isPut)); - if (library.hasRelatedArtifact()) { - org.opencds.cqf.fhir.utility.dstu3.PackageHelper.addRelatedArtifacts( - packageBundle, library.getRelatedArtifact(), repository, isPut); - } - } - } - if (planDefinition.hasRelatedArtifact()) { - org.opencds.cqf.fhir.utility.dstu3.PackageHelper.addRelatedArtifacts( - packageBundle, planDefinition.getRelatedArtifact(), repository, isPut); - } - - return packageBundle; - } - - protected IBaseBundle packageR4(org.hl7.fhir.r4.model.PlanDefinition planDefinition, boolean isPut) { - var packageBundle = new org.hl7.fhir.r4.model.Bundle(); - packageBundle.setType(org.hl7.fhir.r4.model.Bundle.BundleType.TRANSACTION); - packageBundle.addEntry((org.hl7.fhir.r4.model.Bundle.BundleEntryComponent) - org.opencds.cqf.fhir.utility.PackageHelper.createEntry(planDefinition, isPut)); - // The CPG IG specifies a main cql library for a PlanDefinition - var libraryCanonical = - planDefinition.hasLibrary() ? planDefinition.getLibrary().get(0) : null; - if (libraryCanonical != null) { - var library = (org.hl7.fhir.r4.model.Library) - org.opencds.cqf.fhir.utility.r4.SearchHelper.searchRepositoryByCanonical( - repository, libraryCanonical); - if (library != null) { - packageBundle.addEntry((org.hl7.fhir.r4.model.Bundle.BundleEntryComponent) - org.opencds.cqf.fhir.utility.PackageHelper.createEntry(library, isPut)); - if (library.hasRelatedArtifact()) { - org.opencds.cqf.fhir.utility.r4.PackageHelper.addRelatedArtifacts( - packageBundle, library.getRelatedArtifact(), repository, isPut); - } - } - } - if (planDefinition.hasRelatedArtifact()) { - org.opencds.cqf.fhir.utility.r4.PackageHelper.addRelatedArtifacts( - packageBundle, planDefinition.getRelatedArtifact(), repository, isPut); - } - - return packageBundle; - } - - protected IBaseBundle packageR5(org.hl7.fhir.r5.model.PlanDefinition planDefinition, boolean isPut) { - var packageBundle = new org.hl7.fhir.r5.model.Bundle(); - packageBundle.setType(org.hl7.fhir.r5.model.Bundle.BundleType.TRANSACTION); - packageBundle.addEntry((org.hl7.fhir.r5.model.Bundle.BundleEntryComponent) - org.opencds.cqf.fhir.utility.PackageHelper.createEntry(planDefinition, isPut)); - // The CPG IG specifies a main cql library for a PlanDefinition - var libraryCanonical = - planDefinition.hasLibrary() ? planDefinition.getLibrary().get(0) : null; - if (libraryCanonical != null) { - var library = (org.hl7.fhir.r5.model.Library) - org.opencds.cqf.fhir.utility.r5.SearchHelper.searchRepositoryByCanonical( - repository, libraryCanonical); - if (library != null) { - packageBundle.addEntry((org.hl7.fhir.r5.model.Bundle.BundleEntryComponent) - org.opencds.cqf.fhir.utility.PackageHelper.createEntry(library, isPut)); - if (library.hasRelatedArtifact()) { - org.opencds.cqf.fhir.utility.r5.PackageHelper.addRelatedArtifacts( - packageBundle, library.getRelatedArtifact(), repository, isPut); - } - } - } - if (planDefinition.hasRelatedArtifact()) { - org.opencds.cqf.fhir.utility.r5.PackageHelper.addRelatedArtifacts( - packageBundle, planDefinition.getRelatedArtifact(), repository, isPut); - } - - return packageBundle; - } -} diff --git a/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/helpers/GeneratedPackage.java b/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/helpers/GeneratedPackage.java new file mode 100644 index 000000000..e3e41acf3 --- /dev/null +++ b/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/helpers/GeneratedPackage.java @@ -0,0 +1,23 @@ +package org.opencds.cqf.fhir.cr.helpers; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.opencds.cqf.fhir.utility.BundleHelper.getEntry; + +import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.parser.IParser; +import org.hl7.fhir.instance.model.api.IBaseBundle; + +public class GeneratedPackage { + final IBaseBundle generatedBundle; + final IParser jsonParser; + + public GeneratedPackage(IBaseBundle generatedBundle, FhirContext fhirContext) { + this.generatedBundle = generatedBundle; + jsonParser = fhirContext.newJsonParser().setPrettyPrint(true); + } + + public GeneratedPackage hasEntry(int count) { + assertEquals(count, getEntry(generatedBundle).size()); + return this; + } +} diff --git a/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/library/LibraryProcessorTests.java b/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/library/LibraryProcessorTests.java new file mode 100644 index 000000000..550a7f4b5 --- /dev/null +++ b/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/library/LibraryProcessorTests.java @@ -0,0 +1,76 @@ +package org.opencds.cqf.fhir.cr.library; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.opencds.cqf.fhir.cr.library.TestLibrary.CLASS_PATH; +import static org.opencds.cqf.fhir.cr.library.TestLibrary.given; +import static org.opencds.cqf.fhir.test.Resources.getResourcePath; + +import ca.uhn.fhir.context.FhirContext; +import java.nio.file.Paths; +import org.junit.jupiter.api.Test; +import org.opencds.cqf.fhir.cql.EvaluationSettings; +import org.opencds.cqf.fhir.cr.common.PackageProcessor; +import org.opencds.cqf.fhir.utility.Ids; +import org.opencds.cqf.fhir.utility.monad.Eithers; +import org.opencds.cqf.fhir.utility.repository.ig.IgRepository; + +public class LibraryProcessorTests { + private final FhirContext fhirContextDstu3 = FhirContext.forDstu3Cached(); + private final FhirContext fhirContextR4 = FhirContext.forR4Cached(); + private final FhirContext fhirContextR5 = FhirContext.forR5Cached(); + + @Test + void defaultSettings() { + var repository = + new IgRepository(fhirContextR4, Paths.get(getResourcePath(this.getClass()) + "/" + CLASS_PATH + "/r4")); + var processor = new LibraryProcessor(repository); + assertNotNull(processor.evaluationSettings()); + } + + @Test + void processor() { + var repository = + new IgRepository(fhirContextR5, Paths.get(getResourcePath(this.getClass()) + "/" + CLASS_PATH + "/r5")); + var packageProcessor = new PackageProcessor(repository); + var processor = new LibraryProcessor(repository, EvaluationSettings.getDefault(), packageProcessor); + assertNotNull(processor.evaluationSettings()); + var result = processor.resolveLibrary(Eithers.forMiddle3( + Ids.newId(repository.fhirContext(), "Library", "OutpatientPriorAuthorizationPrepopulation"))); + assertNotNull(result); + } + + @Test + void packageDstu3() { + given().repositoryFor(fhirContextDstu3, "dstu3") + .when() + .libraryId("OutpatientPriorAuthorizationPrepopulation") + .thenPackage() + .hasEntry(2); + } + + @Test + void packageR4() { + given().repositoryFor(fhirContextR4, "r4/pa-aslp") + .when() + .libraryId("ASLPDataElements") + .thenPackage() + .hasEntry(10); + + given().repositoryFor(fhirContextR4, "r4") + .when() + .libraryId("OutpatientPriorAuthorizationPrepopulation") + .isPut(Boolean.TRUE) + .thenPackage() + .hasEntry(2); + } + + @Test + void packageR5() { + given().repositoryFor(fhirContextR5, "r5") + .when() + .libraryId("OutpatientPriorAuthorizationPrepopulation") + .isPut(Boolean.TRUE) + .thenPackage() + .hasEntry(2); + } +} diff --git a/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/library/TestLibrary.java b/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/library/TestLibrary.java new file mode 100644 index 000000000..6ca348e8a --- /dev/null +++ b/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/library/TestLibrary.java @@ -0,0 +1,194 @@ +package org.opencds.cqf.fhir.cr.library; + +import static org.opencds.cqf.fhir.test.Resources.getResourcePath; +import static org.opencds.cqf.fhir.utility.BundleHelper.addEntry; +import static org.opencds.cqf.fhir.utility.BundleHelper.newBundle; +import static org.opencds.cqf.fhir.utility.BundleHelper.newEntryWithResource; + +import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.parser.IParser; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.nio.file.Paths; +import org.hl7.fhir.instance.model.api.IBaseBundle; +import org.hl7.fhir.instance.model.api.IBaseParameters; +import org.hl7.fhir.instance.model.api.IBaseResource; +import org.hl7.fhir.instance.model.api.IIdType; +import org.opencds.cqf.fhir.api.Repository; +import org.opencds.cqf.fhir.cql.EvaluationSettings; +import org.opencds.cqf.fhir.cql.engine.retrieve.RetrieveSettings.SEARCH_FILTER_MODE; +import org.opencds.cqf.fhir.cql.engine.retrieve.RetrieveSettings.TERMINOLOGY_FILTER_MODE; +import org.opencds.cqf.fhir.cql.engine.terminology.TerminologySettings.VALUESET_EXPANSION_MODE; +import org.opencds.cqf.fhir.cr.TestOperationProvider; +import org.opencds.cqf.fhir.cr.helpers.GeneratedPackage; +import org.opencds.cqf.fhir.cr.plandefinition.PlanDefinition; +import org.opencds.cqf.fhir.utility.Ids; +import org.opencds.cqf.fhir.utility.monad.Eithers; +import org.opencds.cqf.fhir.utility.repository.InMemoryFhirRepository; +import org.opencds.cqf.fhir.utility.repository.ig.IgRepository; + +public class TestLibrary { + // Borrowing resources from PlanDefinition + public static final String CLASS_PATH = "org/opencds/cqf/fhir/cr/plandefinition"; + + private static InputStream open(String asset) { + return PlanDefinition.class.getResourceAsStream(asset); + } + + public static String load(InputStream asset) throws IOException { + return new String(asset.readAllBytes(), StandardCharsets.UTF_8); + } + + public static String load(String asset) throws IOException { + return load(open(asset)); + } + + public static Given given() { + return new Given(); + } + + public static class Given { + private Repository repository; + private EvaluationSettings evaluationSettings; + + public Given repository(Repository repository) { + this.repository = repository; + return this; + } + + public Given repositoryFor(FhirContext fhirContext, String repositoryPath) { + this.repository = new IgRepository( + fhirContext, Paths.get(getResourcePath(this.getClass()) + "/" + CLASS_PATH + "/" + repositoryPath)); + return this; + } + + public Given evaluationSettings(EvaluationSettings evaluationSettings) { + this.evaluationSettings = evaluationSettings; + return this; + } + + public LibraryProcessor buildProcessor(Repository repository) { + if (repository instanceof IgRepository) { + ((IgRepository) repository) + .setOperationProvider(TestOperationProvider.newProvider(repository.fhirContext())); + } + if (evaluationSettings == null) { + evaluationSettings = EvaluationSettings.getDefault(); + evaluationSettings + .getRetrieveSettings() + .setSearchParameterMode(SEARCH_FILTER_MODE.FILTER_IN_MEMORY) + .setTerminologyParameterMode(TERMINOLOGY_FILTER_MODE.FILTER_IN_MEMORY); + + evaluationSettings + .getTerminologySettings() + .setValuesetExpansionMode(VALUESET_EXPANSION_MODE.PERFORM_NAIVE_EXPANSION); + } + return new LibraryProcessor(repository, evaluationSettings); + } + + public When when() { + return new When(repository, buildProcessor(repository)); + } + } + + public static class When { + private final Repository repository; + private final LibraryProcessor processor; + private final IParser jsonParser; + + private String libraryId; + + private String subjectId; + private Boolean useServerData; + private Repository dataRepository; + private Repository contentRepository; + private Repository terminologyRepository; + private IBaseBundle additionalData; + private IIdType additionalDataId; + private IBaseParameters parameters; + private Boolean isPackagePut; + + public When(Repository repository, LibraryProcessor processor) { + this.repository = repository; + this.processor = processor; + jsonParser = repository.fhirContext().newJsonParser(); + } + + public When libraryId(String id) { + libraryId = id; + return this; + } + + public When subjectId(String id) { + subjectId = id; + return this; + } + + public When useServerData(Boolean value) { + useServerData = value; + return this; + } + + public When data(String dataAssetName) { + dataRepository = new InMemoryFhirRepository( + repository.fhirContext(), (IBaseBundle) jsonParser.parseResource(open(dataAssetName))); + return this; + } + + public When content(String dataAssetName) { + contentRepository = new InMemoryFhirRepository( + repository.fhirContext(), (IBaseBundle) jsonParser.parseResource(open(dataAssetName))); + return this; + } + + public When terminology(String dataAssetName) { + terminologyRepository = new InMemoryFhirRepository( + repository.fhirContext(), (IBaseBundle) jsonParser.parseResource(open(dataAssetName))); + return this; + } + + private void loadAdditionalData(IBaseResource resource) { + var fhirVersion = repository.fhirContext().getVersion().getVersion(); + additionalData = resource.getIdElement().getResourceType().equals("Bundle") + ? (IBaseBundle) resource + : addEntry(newBundle(fhirVersion), newEntryWithResource(fhirVersion, resource)); + } + + public When additionalData(String dataAssetName) { + var data = jsonParser.parseResource(open(dataAssetName)); + loadAdditionalData(data); + return this; + } + + public When additionalDataId(IIdType id) { + additionalDataId = id; + return this; + } + + public When parameters(IBaseParameters params) { + parameters = params; + return this; + } + + public When isPut(Boolean value) { + isPackagePut = value; + return this; + } + + public GeneratedPackage thenPackage() { + if (isPackagePut == null) { + return new GeneratedPackage( + processor.packageLibrary( + Eithers.forMiddle3(Ids.newId(repository.fhirContext(), "Library", libraryId))), + repository.fhirContext()); + } else { + return new GeneratedPackage( + processor.packageLibrary( + Eithers.forMiddle3(Ids.newId(repository.fhirContext(), "Library", libraryId)), + isPackagePut), + repository.fhirContext()); + } + } + } +} diff --git a/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/plandefinition/PlanDefinition.java b/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/plandefinition/PlanDefinition.java index 322a550c3..e260cf2db 100644 --- a/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/plandefinition/PlanDefinition.java +++ b/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/plandefinition/PlanDefinition.java @@ -31,6 +31,7 @@ import org.opencds.cqf.fhir.cql.engine.retrieve.RetrieveSettings.TERMINOLOGY_FILTER_MODE; import org.opencds.cqf.fhir.cql.engine.terminology.TerminologySettings.VALUESET_EXPANSION_MODE; import org.opencds.cqf.fhir.cr.TestOperationProvider; +import org.opencds.cqf.fhir.cr.helpers.GeneratedPackage; import org.opencds.cqf.fhir.utility.Ids; import org.opencds.cqf.fhir.utility.monad.Eithers; import org.opencds.cqf.fhir.utility.repository.InMemoryFhirRepository; @@ -258,12 +259,17 @@ public GeneratedCarePlan thenApply() { public GeneratedPackage thenPackage() { if (isPackagePut == null) { - return new GeneratedPackage(processor.packagePlanDefinition( - Eithers.forMiddle3(Ids.newId(repository.fhirContext(), "PlanDefinition", planDefinitionId)))); + return new GeneratedPackage( + processor.packagePlanDefinition(Eithers.forMiddle3( + Ids.newId(repository.fhirContext(), "PlanDefinition", planDefinitionId))), + repository.fhirContext()); } else { - return new GeneratedPackage(processor.packagePlanDefinition( - Eithers.forMiddle3(Ids.newId(repository.fhirContext(), "PlanDefinition", planDefinitionId)), - isPackagePut)); + return new GeneratedPackage( + processor.packagePlanDefinition( + Eithers.forMiddle3( + Ids.newId(repository.fhirContext(), "PlanDefinition", planDefinitionId)), + isPackagePut), + repository.fhirContext()); } } } @@ -393,17 +399,4 @@ public GeneratedCarePlan hasQuestionnaire() { return this; } } - - public static class GeneratedPackage { - IBaseBundle generatedBundle; - - public GeneratedPackage(IBaseBundle generatedBundle) { - this.generatedBundle = generatedBundle; - } - - public GeneratedPackage hasEntry(int count) { - assertEquals(count, getEntry(generatedBundle).size()); - return this; - } - } } diff --git a/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/plandefinition/PlanDefinitionProcessorTests.java b/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/plandefinition/PlanDefinitionProcessorTests.java index 1235898af..42ad68af5 100644 --- a/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/plandefinition/PlanDefinitionProcessorTests.java +++ b/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/plandefinition/PlanDefinitionProcessorTests.java @@ -13,8 +13,8 @@ import org.opencds.cqf.fhir.cql.EvaluationSettings; import org.opencds.cqf.fhir.cql.engine.model.FhirModelResolverCache; import org.opencds.cqf.fhir.cr.activitydefinition.apply.IRequestResolverFactory; +import org.opencds.cqf.fhir.cr.common.PackageProcessor; import org.opencds.cqf.fhir.cr.plandefinition.apply.ApplyProcessor; -import org.opencds.cqf.fhir.cr.plandefinition.packages.PackageProcessor; import org.opencds.cqf.fhir.utility.BundleHelper; import org.opencds.cqf.fhir.utility.Ids; import org.opencds.cqf.fhir.utility.monad.Eithers; @@ -458,7 +458,7 @@ void packageDstu3() { .when() .planDefinitionId("generate-questionnaire") .thenPackage() - .hasEntry(8); + .hasEntry(9); given().repositoryFor(fhirContextDstu3, "dstu3") .when() @@ -480,22 +480,24 @@ void packageR4() { .planDefinitionId("DischargeInstructionsPlan") .isPut(Boolean.TRUE) .thenPackage() - .hasEntry(1); + .hasEntry(2); } @Test void packageR5() { - given().repositoryFor(fhirContextR5, "r5") - .when() - .planDefinitionId("generate-questionnaire") - .thenPackage() - .hasEntry(8); + // Due to StructureDefinition no longer being a MetaDataResource in R5 it will need an adapter created before + // $package will work with StructureDefinitions. + // given().repositoryFor(fhirContextR5, "r5") + // .when() + // .planDefinitionId("generate-questionnaire") + // .thenPackage() + // .hasEntry(8); given().repositoryFor(fhirContextR5, "r5") .when() .planDefinitionId("DischargeInstructionsPlan") .isPut(Boolean.TRUE) .thenPackage() - .hasEntry(1); + .hasEntry(2); } } diff --git a/cqf-fhir-cr/src/test/resources/org/opencds/cqf/fhir/cr/plandefinition/dstu3/resources/Library-FHIRHelpers.json b/cqf-fhir-cr/src/test/resources/org/opencds/cqf/fhir/cr/plandefinition/dstu3/resources/Library-FHIRHelpers.json index 6a6aaad1e..bd27b6435 100644 --- a/cqf-fhir-cr/src/test/resources/org/opencds/cqf/fhir/cr/plandefinition/dstu3/resources/Library-FHIRHelpers.json +++ b/cqf-fhir-cr/src/test/resources/org/opencds/cqf/fhir/cr/plandefinition/dstu3/resources/Library-FHIRHelpers.json @@ -2,7 +2,7 @@ "resourceType": "Library", "id": "FHIRHelpers", "url": "http://fhir.org/guides/cqf/common/Library/FHIRHelpers", - "version": "3.2.0", + "version": "3.0.1", "name": "FHIRHelpers", "title": "FHIR Helpers", "status": "draft", diff --git a/cqf-fhir-cr/src/test/resources/org/opencds/cqf/fhir/cr/plandefinition/dstu3/resources/Library-OutpatientPriorAuthorizationPrepopulation.json b/cqf-fhir-cr/src/test/resources/org/opencds/cqf/fhir/cr/plandefinition/dstu3/resources/Library-OutpatientPriorAuthorizationPrepopulation.json index 6d7881148..7504d2cef 100644 --- a/cqf-fhir-cr/src/test/resources/org/opencds/cqf/fhir/cr/plandefinition/dstu3/resources/Library-OutpatientPriorAuthorizationPrepopulation.json +++ b/cqf-fhir-cr/src/test/resources/org/opencds/cqf/fhir/cr/plandefinition/dstu3/resources/Library-OutpatientPriorAuthorizationPrepopulation.json @@ -21,7 +21,9 @@ "relatedArtifact": [ { "type": "depends-on", - "resource": "http://fhir.org/guides/cqf/common/Library/FHIRHelpers|3.2.0" + "resource": { + "reference": "http://fhir.org/guides/cqf/common/Library/FHIRHelpers|3.0.1" + } } ], "dataRequirement": [ diff --git a/cqf-fhir-cr/src/test/resources/org/opencds/cqf/fhir/cr/plandefinition/dstu3/resources/PlanDefinition-route-one.json b/cqf-fhir-cr/src/test/resources/org/opencds/cqf/fhir/cr/plandefinition/dstu3/resources/PlanDefinition-route-one.json index 2ebd3e0fc..a1d3f40d1 100644 --- a/cqf-fhir-cr/src/test/resources/org/opencds/cqf/fhir/cr/plandefinition/dstu3/resources/PlanDefinition-route-one.json +++ b/cqf-fhir-cr/src/test/resources/org/opencds/cqf/fhir/cr/plandefinition/dstu3/resources/PlanDefinition-route-one.json @@ -74,7 +74,9 @@ } ], "library": [ - "http://somewhere.org/fhir/uv/mycontentig/Library/OutpatientPriorAuthorizationPrepopulation" + { + "reference": "http://somewhere.org/fhir/uv/mycontentig/Library/OutpatientPriorAuthorizationPrepopulation" + } ], "action": [ { diff --git a/cqf-fhir-cr/src/test/resources/org/opencds/cqf/fhir/cr/plandefinition/r5/resources/ActivityDefinition-SendMessageActivity.json b/cqf-fhir-cr/src/test/resources/org/opencds/cqf/fhir/cr/plandefinition/r5/resources/ActivityDefinition-SendMessageActivity.json new file mode 100644 index 000000000..a1a4c3394 --- /dev/null +++ b/cqf-fhir-cr/src/test/resources/org/opencds/cqf/fhir/cr/plandefinition/r5/resources/ActivityDefinition-SendMessageActivity.json @@ -0,0 +1,51 @@ +{ + "resourceType": "ActivityDefinition", + "id": "SendMessageActivity", + "meta": { + "profile": [ + "http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-communicationactivity" + ] + }, + "kind": "CommunicationRequest", + "profile": "http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-communicationrequest", + "intent": "proposal", + "extension": [ + { + "url": "http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-knowledgeCapability", + "valueCode": "publishable" + }, + { + "url": "http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-knowledgeRepresentationLevel", + "valueCode": "structured" + } + ], + "url": "http://example.org/ActivityDefinition/SendMessageActivity", + "name": "SendMessageActivity", + "title": "ActivityDefinition SendMessageActivity", + "status": "draft", + "experimental": true, + "publisher": "Example", + "jurisdiction": [ + { + "coding": [ + { + "code": "001", + "system": "http://unstats.un.org/unsd/methods/m49/m49.htm", + "display": "World" + } + ] + } + ], + "version": "0.1.0", + "description": "Example Activity Definition for a recommendation to send a message", + "code": { + "coding": [ + { + "code": "send-message", + "system": "http://hl7.org/fhir/uv/cpg/CodeSystem/cpg-activity-type", + "display": "Send a message" + } + ] + }, + "doNotPerform": false +} \ No newline at end of file diff --git a/cqf-fhir-cr/src/test/resources/org/opencds/cqf/fhir/cr/plandefinition/r5/resources/Library-FHIRHelpers.json b/cqf-fhir-cr/src/test/resources/org/opencds/cqf/fhir/cr/plandefinition/r5/resources/Library-FHIRHelpers.json index 643e6e67f..36b6cadff 100644 --- a/cqf-fhir-cr/src/test/resources/org/opencds/cqf/fhir/cr/plandefinition/r5/resources/Library-FHIRHelpers.json +++ b/cqf-fhir-cr/src/test/resources/org/opencds/cqf/fhir/cr/plandefinition/r5/resources/Library-FHIRHelpers.json @@ -1,7 +1,7 @@ { "resourceType": "Library", "id": "FHIRHelpers", - "url": "http://somewhere.org/fhir/uv/mycontentig/Library/OutpatientPriorAuthorizationPrepopulation", + "url": "http://fhir.org/guides/cqf/common/Library/FHIRHelpers", "version": "4.0.001", "name": "FHIRHelpers", "title": "FHIR Helpers", diff --git a/cqf-fhir-cr/src/test/resources/org/opencds/cqf/fhir/cr/plandefinition/r5/resources/Library-OutpatientPriorAuthorizationPrepopulation.json b/cqf-fhir-cr/src/test/resources/org/opencds/cqf/fhir/cr/plandefinition/r5/resources/Library-OutpatientPriorAuthorizationPrepopulation.json index de46c31b2..8cdb7cbd1 100644 --- a/cqf-fhir-cr/src/test/resources/org/opencds/cqf/fhir/cr/plandefinition/r5/resources/Library-OutpatientPriorAuthorizationPrepopulation.json +++ b/cqf-fhir-cr/src/test/resources/org/opencds/cqf/fhir/cr/plandefinition/r5/resources/Library-OutpatientPriorAuthorizationPrepopulation.json @@ -21,7 +21,7 @@ "relatedArtifact": [ { "type": "depends-on", - "resource": "http://fhir.org/guides/cqf/common/Library/FHIRHelpers|1.0.0" + "resource": "http://fhir.org/guides/cqf/common/Library/FHIRHelpers|4.0.001" } ], "dataRequirement": [ diff --git a/cqf-fhir-utility/pom.xml b/cqf-fhir-utility/pom.xml index 1f8dc04cf..bcf59dffa 100644 --- a/cqf-fhir-utility/pom.xml +++ b/cqf-fhir-utility/pom.xml @@ -32,6 +32,10 @@ 3.7.0-SNAPSHOT test + + info.cqframework + engine + ca.uhn.hapi.fhir hapi-fhir-base diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/Constants.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/Constants.java index 0d54f15eb..088f94346 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/Constants.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/Constants.java @@ -2,9 +2,6 @@ import ca.uhn.fhir.model.api.Tag; import com.google.common.collect.ImmutableMap; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; import java.util.Map; public class Constants { @@ -55,6 +52,8 @@ private Constants() {} "http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-custom-activity-kind"; public static final String CPG_ACTIVITY_KIND = "http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-activity-kind"; + public static final String CQF_EXPANSION_PARAMETERS = + "http://hl7.org/fhir/StructureDefinition/cqf-expansionParameters"; public static final String CQF_EXPRESSION = "http://hl7.org/fhir/StructureDefinition/cqf-expression"; // This is only for dstu3 since the Expression type does not exist in that version public static final String CQF_EXPRESSION_LANGUAGE = @@ -115,29 +114,14 @@ private Constants() {} public static final String APPLY_PARAMETER_DATA_ENDPOINT = "dataEndpoint"; public static final String APPLY_PARAMETER_CONTENT_ENDPOINT = "contentEndpoint"; public static final String APPLY_PARAMETER_TERMINOLOGY_ENDPOINT = "terminologyEndpoint"; + public static final String US_PH_CONTEXT_URL = "http://hl7.org/fhir/us/ecr/CodeSystem/us-ph-usage-context"; - public static final String VALUE_SET_CONDITION_CODE = "focus"; - public static final String VALUE_SET_PRIORITY_CODE = "priority"; - public static final String VALUE_SET_CONDITION_URL = - "http://aphl.org/fhir/vsm/StructureDefinition/vsm-valueset-condition"; - public static final String VALUE_SET_PRIORITY_URL = - "http://aphl.org/fhir/vsm/StructureDefinition/vsm-valueset-priority"; public static final String LIBRARY_TYPE = "http://terminology.hl7.org/CodeSystem/library-type"; public static final String ASSET_COLLECTION = "asset-collection"; - public static final String EXPANSION_PARAMETERS_URL = - "http://hl7.org/fhir/StructureDefinition/cqf-expansionParameters"; - - // can't use List.of for Android 26 compatibility - public static final List PRESERVED_EXTENSION_URLS = - Collections.unmodifiableList(Arrays.asList(VALUE_SET_PRIORITY_URL, VALUE_SET_CONDITION_URL)); public static final String AUTHORITATIVE_SOURCE_URL = "http://hl7.org/fhir/StructureDefinition/valueset-authoritativeSource"; - public static final String VSM_WORKFLOW_CODES_CODE_SYSTEM_URL = - "http://aphl.org/fhir/vsm/CodeSystem/vsm-workflow-codes"; - public static final String VSM_VALUE_SET_TAG_VSM_AUTHORED_CODE = "vsm-authored"; public static final String VSAC_USERNAME = "vsacUsername"; - public static final String APIKEY = "apiKey"; } diff --git a/cqf-fhir-cql/src/main/java/org/opencds/cqf/fhir/cql/engine/utility/ValueSets.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/ValueSets.java similarity index 99% rename from cqf-fhir-cql/src/main/java/org/opencds/cqf/fhir/cql/engine/utility/ValueSets.java rename to cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/ValueSets.java index eea4c3e94..aa00eaaaf 100644 --- a/cqf-fhir-cql/src/main/java/org/opencds/cqf/fhir/cql/engine/utility/ValueSets.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/ValueSets.java @@ -1,4 +1,4 @@ -package org.opencds.cqf.fhir.cql.engine.utility; +package org.opencds.cqf.fhir.utility; import ca.uhn.fhir.context.BaseRuntimeChildDefinition; import ca.uhn.fhir.context.BaseRuntimeChildDefinition.IAccessor; diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/KnowledgeArtifactAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/KnowledgeArtifactAdapter.java index 0072b98b6..e4359de1f 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/KnowledgeArtifactAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/KnowledgeArtifactAdapter.java @@ -207,7 +207,9 @@ default List combineComponentsAndDependencies() { void setRelatedArtifact(List relatedArtifacts); - public IBase accept(KnowledgeArtifactVisitor visitor, Repository repository, IBaseParameters operationParameters); + default IBase accept(KnowledgeArtifactVisitor visitor, Repository repository, IBaseParameters operationParameters) { + return visitor.visit(this, repository, operationParameters); + } @SuppressWarnings("unchecked") default List getOwnedRelatedArtifacts() { diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/KnowledgeArtifactAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/KnowledgeArtifactAdapter.java index 510383d12..726a16109 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/KnowledgeArtifactAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/KnowledgeArtifactAdapter.java @@ -13,15 +13,11 @@ import org.hl7.fhir.dstu3.model.RelatedArtifact; import org.hl7.fhir.dstu3.model.RelatedArtifact.RelatedArtifactType; import org.hl7.fhir.exceptions.FHIRException; -import org.hl7.fhir.instance.model.api.IBase; import org.hl7.fhir.instance.model.api.IBaseExtension; import org.hl7.fhir.instance.model.api.IBaseHasExtensions; -import org.hl7.fhir.instance.model.api.IBaseParameters; import org.hl7.fhir.instance.model.api.ICompositeType; import org.hl7.fhir.instance.model.api.IPrimitiveType; -import org.opencds.cqf.fhir.api.Repository; import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; -import org.opencds.cqf.fhir.utility.visitor.KnowledgeArtifactVisitor; public class KnowledgeArtifactAdapter extends ResourceAdapter implements org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter { @@ -125,11 +121,6 @@ public List getDependencies() { return new ArrayList<>(); } - @Override - public IBase accept(KnowledgeArtifactVisitor visitor, Repository repository, IBaseParameters operationParameters) { - return visitor.visit(this, repository, operationParameters); - } - @Override public void setEffectivePeriod(ICompositeType effectivePeriod) { if (effectivePeriod != null && !(effectivePeriod instanceof Period)) { diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/LibraryAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/LibraryAdapter.java index d376efa1a..7962a39b7 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/LibraryAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/LibraryAdapter.java @@ -14,18 +14,14 @@ import org.hl7.fhir.dstu3.model.RelatedArtifact; import org.hl7.fhir.dstu3.model.RelatedArtifact.RelatedArtifactType; import org.hl7.fhir.exceptions.FHIRException; -import org.hl7.fhir.instance.model.api.IBase; import org.hl7.fhir.instance.model.api.IBaseExtension; import org.hl7.fhir.instance.model.api.IBaseHasExtensions; -import org.hl7.fhir.instance.model.api.IBaseParameters; import org.hl7.fhir.instance.model.api.ICompositeType; import org.hl7.fhir.instance.model.api.IDomainResource; import org.hl7.fhir.instance.model.api.IIdType; import org.hl7.fhir.instance.model.api.IPrimitiveType; -import org.opencds.cqf.fhir.api.Repository; import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; -import org.opencds.cqf.fhir.utility.visitor.KnowledgeArtifactVisitor; class LibraryAdapter extends ResourceAdapter implements org.opencds.cqf.fhir.utility.adapter.LibraryAdapter { @@ -46,10 +42,6 @@ public LibraryAdapter(Library library) { this.library = library; } - public IBase accept(KnowledgeArtifactVisitor visitor, Repository repository, IBaseParameters operationParameters) { - return visitor.visit(this, repository, operationParameters); - } - protected Library getLibrary() { return this.library; } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/PlanDefinitionAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/PlanDefinitionAdapter.java index 9bf4b6b15..6ec5e3d4e 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/PlanDefinitionAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/PlanDefinitionAdapter.java @@ -18,18 +18,14 @@ import org.hl7.fhir.dstu3.model.StringType; import org.hl7.fhir.dstu3.model.UriType; import org.hl7.fhir.exceptions.FHIRException; -import org.hl7.fhir.instance.model.api.IBase; import org.hl7.fhir.instance.model.api.IBaseExtension; import org.hl7.fhir.instance.model.api.IBaseHasExtensions; -import org.hl7.fhir.instance.model.api.IBaseParameters; import org.hl7.fhir.instance.model.api.ICompositeType; import org.hl7.fhir.instance.model.api.IDomainResource; import org.hl7.fhir.instance.model.api.IPrimitiveType; -import org.opencds.cqf.fhir.api.Repository; import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; import org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter; -import org.opencds.cqf.fhir.utility.visitor.KnowledgeArtifactVisitor; class PlanDefinitionAdapter extends ResourceAdapter implements KnowledgeArtifactAdapter { @@ -51,11 +47,6 @@ public PlanDefinitionAdapter(PlanDefinition planDefinition) { this.planDefinition = planDefinition; } - @Override - public IBase accept(KnowledgeArtifactVisitor visitor, Repository repository, IBaseParameters operationParameters) { - return visitor.visit(this, repository, operationParameters); - } - protected PlanDefinition getPlanDefinition() { return this.planDefinition; } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ValueSetAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ValueSetAdapter.java index 8e11f2d87..62a69d0d0 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ValueSetAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ValueSetAdapter.java @@ -14,18 +14,14 @@ import org.hl7.fhir.dstu3.model.RelatedArtifact.RelatedArtifactType; import org.hl7.fhir.dstu3.model.ValueSet; import org.hl7.fhir.exceptions.FHIRException; -import org.hl7.fhir.instance.model.api.IBase; import org.hl7.fhir.instance.model.api.IBaseExtension; import org.hl7.fhir.instance.model.api.IBaseHasExtensions; -import org.hl7.fhir.instance.model.api.IBaseParameters; import org.hl7.fhir.instance.model.api.ICompositeType; import org.hl7.fhir.instance.model.api.IDomainResource; import org.hl7.fhir.instance.model.api.IIdType; import org.hl7.fhir.instance.model.api.IPrimitiveType; -import org.opencds.cqf.fhir.api.Repository; import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; -import org.opencds.cqf.fhir.utility.visitor.KnowledgeArtifactVisitor; class ValueSetAdapter extends ResourceAdapter implements org.opencds.cqf.fhir.utility.adapter.ValueSetAdapter { @@ -46,10 +42,6 @@ public ValueSetAdapter(ValueSet valueSet) { this.valueSet = valueSet; } - public IBase accept(KnowledgeArtifactVisitor visitor, Repository repository, IBaseParameters operationParameters) { - return visitor.visit(this, repository, operationParameters); - } - protected ValueSet getValueSet() { return this.valueSet; } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/KnowledgeArtifactAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/KnowledgeArtifactAdapter.java index 8550f1d04..7157afd73 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/KnowledgeArtifactAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/KnowledgeArtifactAdapter.java @@ -6,10 +6,8 @@ import java.util.List; import java.util.stream.Collectors; import org.hl7.fhir.exceptions.FHIRException; -import org.hl7.fhir.instance.model.api.IBase; import org.hl7.fhir.instance.model.api.IBaseExtension; import org.hl7.fhir.instance.model.api.IBaseHasExtensions; -import org.hl7.fhir.instance.model.api.IBaseParameters; import org.hl7.fhir.instance.model.api.ICompositeType; import org.hl7.fhir.instance.model.api.IPrimitiveType; import org.hl7.fhir.r4.model.DateTimeType; @@ -19,9 +17,7 @@ import org.hl7.fhir.r4.model.Period; import org.hl7.fhir.r4.model.RelatedArtifact; import org.hl7.fhir.r4.model.RelatedArtifact.RelatedArtifactType; -import org.opencds.cqf.fhir.api.Repository; import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; -import org.opencds.cqf.fhir.utility.visitor.KnowledgeArtifactVisitor; public class KnowledgeArtifactAdapter extends ResourceAdapter implements org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter { @@ -125,11 +121,6 @@ public List getDependencies() { return new ArrayList<>(); } - @Override - public IBase accept(KnowledgeArtifactVisitor visitor, Repository repository, IBaseParameters operationParameters) { - return visitor.visit(this, repository, operationParameters); - } - @Override public void setEffectivePeriod(ICompositeType effectivePeriod) { if (effectivePeriod != null && !(effectivePeriod instanceof Period)) { diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/LibraryAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/LibraryAdapter.java index d362daf9e..fce6b3b97 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/LibraryAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/LibraryAdapter.java @@ -6,10 +6,8 @@ import java.util.List; import java.util.stream.Collectors; import org.hl7.fhir.exceptions.FHIRException; -import org.hl7.fhir.instance.model.api.IBase; import org.hl7.fhir.instance.model.api.IBaseExtension; import org.hl7.fhir.instance.model.api.IBaseHasExtensions; -import org.hl7.fhir.instance.model.api.IBaseParameters; import org.hl7.fhir.instance.model.api.ICompositeType; import org.hl7.fhir.instance.model.api.IDomainResource; import org.hl7.fhir.instance.model.api.IPrimitiveType; @@ -21,10 +19,8 @@ import org.hl7.fhir.r4.model.Period; import org.hl7.fhir.r4.model.RelatedArtifact; import org.hl7.fhir.r4.model.RelatedArtifact.RelatedArtifactType; -import org.opencds.cqf.fhir.api.Repository; import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; -import org.opencds.cqf.fhir.utility.visitor.KnowledgeArtifactVisitor; public class LibraryAdapter extends ResourceAdapter implements org.opencds.cqf.fhir.utility.adapter.LibraryAdapter { @@ -126,7 +122,7 @@ public Attachment addContent() { @Override public List getDependencies() { - List retval = new ArrayList(); + List references = new ArrayList(); final String referenceSource = this.hasVersion() ? this.getUrl() + "|" + this.getLibrary().getVersion() : this.getUrl(); @@ -134,24 +130,24 @@ public List getDependencies() { this.getRelatedArtifact().stream() .filter(ra -> ra.hasResource()) .map(ra -> DependencyInfo.convertRelatedArtifact(ra, referenceSource)) - .forEach(ra -> retval.add(ra)); + .forEach(ra -> references.add(ra)); this.getLibrary().getDataRequirement().stream().forEach(dr -> { dr.getProfile().stream() .filter(profile -> profile.hasValue()) - .forEach(profile -> retval.add(new DependencyInfo( + .forEach(profile -> references.add(new DependencyInfo( referenceSource, profile.getValue(), profile.getExtension(), (reference) -> profile.setValue(reference)))); dr.getCodeFilter().stream() .filter(cf -> cf.hasValueSet()) - .forEach(cf -> retval.add(new DependencyInfo( + .forEach(cf -> references.add(new DependencyInfo( referenceSource, cf.getValueSet(), cf.getExtension(), (reference) -> cf.setValueSet(reference)))); }); - return retval; + return references; } @Override @@ -170,11 +166,6 @@ public void setRelatedArtifact(L .collect(Collectors.toList())); } - @Override - public IBase accept(KnowledgeArtifactVisitor visitor, Repository repository, IBaseParameters operationParameters) { - return visitor.visit(this, repository, operationParameters); - } - @Override public Date getApprovalDate() { return this.getLibrary().getApprovalDate(); diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/PlanDefinitionAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/PlanDefinitionAdapter.java index 0088e07c2..652e46873 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/PlanDefinitionAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/PlanDefinitionAdapter.java @@ -7,10 +7,8 @@ import java.util.stream.Collectors; import java.util.stream.Stream; import org.hl7.fhir.exceptions.FHIRException; -import org.hl7.fhir.instance.model.api.IBase; import org.hl7.fhir.instance.model.api.IBaseExtension; import org.hl7.fhir.instance.model.api.IBaseHasExtensions; -import org.hl7.fhir.instance.model.api.IBaseParameters; import org.hl7.fhir.instance.model.api.ICompositeType; import org.hl7.fhir.instance.model.api.IDomainResource; import org.hl7.fhir.instance.model.api.IIdType; @@ -23,11 +21,9 @@ import org.hl7.fhir.r4.model.PlanDefinition; import org.hl7.fhir.r4.model.RelatedArtifact; import org.hl7.fhir.r4.model.RelatedArtifact.RelatedArtifactType; -import org.opencds.cqf.fhir.api.Repository; import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; import org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter; -import org.opencds.cqf.fhir.utility.visitor.KnowledgeArtifactVisitor; public class PlanDefinitionAdapter extends ResourceAdapter implements KnowledgeArtifactAdapter { @@ -49,11 +45,6 @@ public PlanDefinitionAdapter(PlanDefinition planDefinition) { this.planDefinition = planDefinition; } - @Override - public IBase accept(KnowledgeArtifactVisitor visitor, Repository repository, IBaseParameters operationParameters) { - return visitor.visit(this, repository, operationParameters); - } - protected PlanDefinition getPlanDefinition() { return this.planDefinition; } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/ValueSetAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/ValueSetAdapter.java index 0eea2c0d5..93ed403bf 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/ValueSetAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/ValueSetAdapter.java @@ -7,10 +7,8 @@ import java.util.stream.Collectors; import java.util.stream.Stream; import org.hl7.fhir.exceptions.FHIRException; -import org.hl7.fhir.instance.model.api.IBase; import org.hl7.fhir.instance.model.api.IBaseExtension; import org.hl7.fhir.instance.model.api.IBaseHasExtensions; -import org.hl7.fhir.instance.model.api.IBaseParameters; import org.hl7.fhir.instance.model.api.ICompositeType; import org.hl7.fhir.instance.model.api.IDomainResource; import org.hl7.fhir.instance.model.api.IIdType; @@ -22,10 +20,8 @@ import org.hl7.fhir.r4.model.RelatedArtifact; import org.hl7.fhir.r4.model.RelatedArtifact.RelatedArtifactType; import org.hl7.fhir.r4.model.ValueSet; -import org.opencds.cqf.fhir.api.Repository; import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; -import org.opencds.cqf.fhir.utility.visitor.KnowledgeArtifactVisitor; public class ValueSetAdapter extends ResourceAdapter implements org.opencds.cqf.fhir.utility.adapter.ValueSetAdapter { @@ -46,11 +42,6 @@ public ValueSetAdapter(ValueSet valueSet) { this.valueSet = valueSet; } - @Override - public IBase accept(KnowledgeArtifactVisitor visitor, Repository repository, IBaseParameters operationParameters) { - return visitor.visit(this, repository, operationParameters); - } - protected ValueSet getValueSet() { return this.valueSet; } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/AdapterFactory.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/AdapterFactory.java index 9f65a26b0..16421aacf 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/AdapterFactory.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/AdapterFactory.java @@ -1,11 +1,13 @@ package org.opencds.cqf.fhir.utility.adapter.r5; import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; +import org.apache.commons.lang3.NotImplementedException; import org.hl7.fhir.instance.model.api.IBaseBackboneElement; import org.hl7.fhir.instance.model.api.IBaseParameters; import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.instance.model.api.ICompositeType; import org.hl7.fhir.instance.model.api.IDomainResource; +import org.hl7.fhir.r5.model.CanonicalResource; import org.hl7.fhir.r5.model.Library; import org.hl7.fhir.r5.model.MetadataResource; import org.hl7.fhir.r5.model.PlanDefinition; @@ -37,9 +39,12 @@ public org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter createKnowl if (resource instanceof MetadataResource) { retval = new org.opencds.cqf.fhir.utility.adapter.r5.KnowledgeArtifactAdapter( (MetadataResource) resource); + } else if (resource instanceof CanonicalResource) { + // TODO: StructureDefinition is a CanonicalResource in R5. Other resources may be as well? + throw new NotImplementedException("CanonicalResource instances are not currently supported."); } else { throw new UnprocessableEntityException( - String.format("Resouce must be instance of %s", MetadataResource.class.getName())); + String.format("Resource must be instance of %s", MetadataResource.class.getName())); } } return retval; diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/KnowledgeArtifactAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/KnowledgeArtifactAdapter.java index 7a1ed87d1..a06557447 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/KnowledgeArtifactAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/KnowledgeArtifactAdapter.java @@ -6,10 +6,8 @@ import java.util.List; import java.util.stream.Collectors; import org.hl7.fhir.exceptions.FHIRException; -import org.hl7.fhir.instance.model.api.IBase; import org.hl7.fhir.instance.model.api.IBaseExtension; import org.hl7.fhir.instance.model.api.IBaseHasExtensions; -import org.hl7.fhir.instance.model.api.IBaseParameters; import org.hl7.fhir.instance.model.api.ICompositeType; import org.hl7.fhir.instance.model.api.IDomainResource; import org.hl7.fhir.instance.model.api.IPrimitiveType; @@ -20,9 +18,7 @@ import org.hl7.fhir.r5.model.Period; import org.hl7.fhir.r5.model.RelatedArtifact; import org.hl7.fhir.r5.model.RelatedArtifact.RelatedArtifactType; -import org.opencds.cqf.fhir.api.Repository; import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; -import org.opencds.cqf.fhir.utility.visitor.KnowledgeArtifactVisitor; public class KnowledgeArtifactAdapter extends ResourceAdapter implements org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter { @@ -134,11 +130,6 @@ public String getPurpose() { return this.get().getPurpose(); } - @Override - public IBase accept(KnowledgeArtifactVisitor visitor, Repository repository, IBaseParameters operationParameters) { - return visitor.visit(this, repository, operationParameters); - } - @Override public void setEffectivePeriod(ICompositeType effectivePeriod) { if (effectivePeriod != null && !(effectivePeriod instanceof Period)) { diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/LibraryAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/LibraryAdapter.java index be93dd368..51b0dbcbf 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/LibraryAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/LibraryAdapter.java @@ -6,10 +6,8 @@ import java.util.List; import java.util.stream.Collectors; import org.hl7.fhir.exceptions.FHIRException; -import org.hl7.fhir.instance.model.api.IBase; import org.hl7.fhir.instance.model.api.IBaseExtension; import org.hl7.fhir.instance.model.api.IBaseHasExtensions; -import org.hl7.fhir.instance.model.api.IBaseParameters; import org.hl7.fhir.instance.model.api.ICompositeType; import org.hl7.fhir.instance.model.api.IDomainResource; import org.hl7.fhir.instance.model.api.IPrimitiveType; @@ -21,10 +19,8 @@ import org.hl7.fhir.r5.model.Period; import org.hl7.fhir.r5.model.RelatedArtifact; import org.hl7.fhir.r5.model.RelatedArtifact.RelatedArtifactType; -import org.opencds.cqf.fhir.api.Repository; import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; -import org.opencds.cqf.fhir.utility.visitor.KnowledgeArtifactVisitor; public class LibraryAdapter extends ResourceAdapter implements org.opencds.cqf.fhir.utility.adapter.LibraryAdapter { @@ -122,35 +118,30 @@ public Attachment addContent() { @Override public List getDependencies() { - List retval = new ArrayList(); + List references = new ArrayList(); final String referenceSource = this.hasVersion() ? this.getUrl() + "|" + this.getLibrary().getVersion() : this.getUrl(); this.getRelatedArtifact().stream() .filter(ra -> ra.hasResource()) .map(ra -> DependencyInfo.convertRelatedArtifact(ra, referenceSource)) - .forEach(dep -> retval.add(dep)); + .forEach(dep -> references.add(dep)); this.getLibrary().getDataRequirement().stream().forEach(dr -> { dr.getProfile().stream() .filter(profile -> profile.hasValue()) - .forEach(profile -> retval.add(new DependencyInfo( + .forEach(profile -> references.add(new DependencyInfo( referenceSource, profile.getValue(), profile.getExtension(), (reference) -> profile.setValue(reference)))); dr.getCodeFilter().stream() .filter(cf -> cf.hasValueSet()) - .forEach(cf -> retval.add(new DependencyInfo( + .forEach(cf -> references.add(new DependencyInfo( referenceSource, cf.getValueSet(), cf.getExtension(), (reference) -> cf.setValueSet(reference)))); }); - return retval; - } - - @Override - public IBase accept(KnowledgeArtifactVisitor visitor, Repository repository, IBaseParameters operationParameters) { - return visitor.visit(this, repository, operationParameters); + return references; } @Override diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/PlanDefinitionAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/PlanDefinitionAdapter.java index 11772d385..cddea87d3 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/PlanDefinitionAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/PlanDefinitionAdapter.java @@ -7,10 +7,8 @@ import java.util.stream.Collectors; import java.util.stream.Stream; import org.hl7.fhir.exceptions.FHIRException; -import org.hl7.fhir.instance.model.api.IBase; import org.hl7.fhir.instance.model.api.IBaseExtension; import org.hl7.fhir.instance.model.api.IBaseHasExtensions; -import org.hl7.fhir.instance.model.api.IBaseParameters; import org.hl7.fhir.instance.model.api.ICompositeType; import org.hl7.fhir.instance.model.api.IDomainResource; import org.hl7.fhir.instance.model.api.IIdType; @@ -23,11 +21,9 @@ import org.hl7.fhir.r5.model.PlanDefinition; import org.hl7.fhir.r5.model.RelatedArtifact; import org.hl7.fhir.r5.model.RelatedArtifact.RelatedArtifactType; -import org.opencds.cqf.fhir.api.Repository; import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; import org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter; -import org.opencds.cqf.fhir.utility.visitor.KnowledgeArtifactVisitor; public class PlanDefinitionAdapter extends ResourceAdapter implements KnowledgeArtifactAdapter { @@ -44,11 +40,6 @@ public PlanDefinitionAdapter(IDomainResource planDefinition) { this.planDefinition = (PlanDefinition) planDefinition; } - @Override - public IBase accept(KnowledgeArtifactVisitor visitor, Repository repository, IBaseParameters operationParameters) { - return visitor.visit(this, repository, operationParameters); - } - protected PlanDefinition getPlanDefinition() { return this.planDefinition; } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/ValueSetAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/ValueSetAdapter.java index 06bc4e55a..abbd57b14 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/ValueSetAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/ValueSetAdapter.java @@ -7,10 +7,8 @@ import java.util.stream.Collectors; import java.util.stream.Stream; import org.hl7.fhir.exceptions.FHIRException; -import org.hl7.fhir.instance.model.api.IBase; import org.hl7.fhir.instance.model.api.IBaseExtension; import org.hl7.fhir.instance.model.api.IBaseHasExtensions; -import org.hl7.fhir.instance.model.api.IBaseParameters; import org.hl7.fhir.instance.model.api.ICompositeType; import org.hl7.fhir.instance.model.api.IPrimitiveType; import org.hl7.fhir.r5.model.DateTimeType; @@ -20,10 +18,8 @@ import org.hl7.fhir.r5.model.RelatedArtifact; import org.hl7.fhir.r5.model.RelatedArtifact.RelatedArtifactType; import org.hl7.fhir.r5.model.ValueSet; -import org.opencds.cqf.fhir.api.Repository; import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; -import org.opencds.cqf.fhir.utility.visitor.KnowledgeArtifactVisitor; class ValueSetAdapter extends ResourceAdapter implements org.opencds.cqf.fhir.utility.adapter.ValueSetAdapter { @@ -39,10 +35,6 @@ public ValueSetAdapter(ValueSet valueSet) { this.valueSet = valueSet; } - public IBase accept(KnowledgeArtifactVisitor visitor, Repository repository, IBaseParameters operationParameters) { - return visitor.visit(this, repository, operationParameters); - } - protected ValueSet getValueSet() { return this.valueSet; } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/KnowledgeArtifactApproveVisitor.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/KnowledgeArtifactApproveVisitor.java index ad4eebfd2..61fe179fc 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/KnowledgeArtifactApproveVisitor.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/KnowledgeArtifactApproveVisitor.java @@ -2,7 +2,6 @@ import ca.uhn.fhir.context.FhirVersionEnum; import ca.uhn.fhir.model.api.TemporalPrecisionEnum; -import ca.uhn.fhir.rest.server.exceptions.NotImplementedOperationException; import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; import java.util.Date; import java.util.Optional; @@ -17,15 +16,12 @@ import org.opencds.cqf.fhir.utility.Canonicals; import org.opencds.cqf.fhir.utility.PackageHelper; import org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter; -import org.opencds.cqf.fhir.utility.adapter.LibraryAdapter; -import org.opencds.cqf.fhir.utility.adapter.PlanDefinitionAdapter; -import org.opencds.cqf.fhir.utility.adapter.ValueSetAdapter; public class KnowledgeArtifactApproveVisitor implements KnowledgeArtifactVisitor { @Override - public IBaseResource visit(LibraryAdapter library, Repository repository, IBaseParameters approveParameters) { + public IBase visit(KnowledgeArtifactAdapter adapter, Repository repository, IBaseParameters approveParameters) { Date currentDate = new Date(); - var fhirVersion = library.get().getStructureFhirVersionEnum(); + var fhirVersion = adapter.get().getStructureFhirVersionEnum(); Date approvalDate = VisitorHelper.getParameter("approvalDate", approveParameters, IPrimitiveType.class) .map(d -> (Date) d.getValue()) .orElse(currentDate); @@ -40,12 +36,12 @@ public IBaseResource visit(LibraryAdapter library, Repository repository, IBaseP "artifactAssessmentTarget", approveParameters, IPrimitiveType.class) .map(t -> (String) t.getValue()); if (artifactAssessmentTarget.isPresent()) { - if (!Canonicals.getUrl(artifactAssessmentTarget.get()).equals(library.getUrl())) { + if (!Canonicals.getUrl(artifactAssessmentTarget.get()).equals(adapter.getUrl())) { throw new UnprocessableEntityException( "ArtifactCommentTarget URL does not match URL of resource being approved."); } - if (library.hasVersion()) { - if (!Canonicals.getVersion(artifactAssessmentTarget.get()).equals(library.getVersion())) { + if (adapter.hasVersion()) { + if (!Canonicals.getVersion(artifactAssessmentTarget.get()).equals(adapter.getVersion())) { throw new UnprocessableEntityException( "ArtifactCommentTarget version does not match version of resource being approved."); } @@ -59,18 +55,18 @@ public IBaseResource visit(LibraryAdapter library, Repository repository, IBaseP var returnBundle = BundleHelper.newBundle(fhirVersion, null, "transaction"); var assessment = createApprovalAssessment( - library.getId(), + adapter.getId(), artifactAssessmentType, artifactAssessmentSummary, artifactAssessmentTarget, artifactAssessmentRelatedArtifact, artifactAssessmentAuthor, - library.get().getIdElement(), + adapter.get().getIdElement(), fhirVersion); - library.setApprovalDate(approvalDate); - setDateElement(library, currentDate, fhirVersion); + adapter.setApprovalDate(approvalDate); + setDateElement(adapter, currentDate, fhirVersion); BundleHelper.addEntry(returnBundle, PackageHelper.createEntry(assessment, false)); - BundleHelper.addEntry(returnBundle, PackageHelper.createEntry(library.get(), true)); + BundleHelper.addEntry(returnBundle, PackageHelper.createEntry(adapter.get(), true)); return repository.transaction(returnBundle); } @@ -122,17 +118,17 @@ private IBaseResource createApprovalAssessment( } } - private void setDateElement(LibraryAdapter library, Date currentDate, FhirVersionEnum fhirVersion) { + private void setDateElement(KnowledgeArtifactAdapter adapter, Date currentDate, FhirVersionEnum fhirVersion) { switch (fhirVersion) { case DSTU3: - library.setDateElement( + adapter.setDateElement( new org.hl7.fhir.dstu3.model.DateTimeType(currentDate, TemporalPrecisionEnum.DAY)); break; case R4: - library.setDateElement(new org.hl7.fhir.r4.model.DateTimeType(currentDate, TemporalPrecisionEnum.DAY)); + adapter.setDateElement(new org.hl7.fhir.r4.model.DateTimeType(currentDate, TemporalPrecisionEnum.DAY)); break; case R5: - library.setDateElement(new org.hl7.fhir.r5.model.DateTimeType(currentDate, TemporalPrecisionEnum.DAY)); + adapter.setDateElement(new org.hl7.fhir.r5.model.DateTimeType(currentDate, TemporalPrecisionEnum.DAY)); break; case DSTU2: case DSTU2_1: @@ -142,20 +138,4 @@ private void setDateElement(LibraryAdapter library, Date currentDate, FhirVersio String.format("Unsupported version of FHIR: %s", fhirVersion.getFhirVersionString())); } } - - @Override - public IBase visit(KnowledgeArtifactAdapter library, Repository repository, IBaseParameters draftParameters) { - throw new NotImplementedOperationException("Not implemented"); - } - - @Override - public IBase visit( - PlanDefinitionAdapter planDefinition, Repository repository, IBaseParameters operationParameters) { - throw new NotImplementedOperationException("Not implemented"); - } - - @Override - public IBase visit(ValueSetAdapter valueSet, Repository repository, IBaseParameters operationParameters) { - throw new NotImplementedOperationException("Not implemented"); - } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/KnowledgeArtifactDraftVisitor.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/KnowledgeArtifactDraftVisitor.java index 48524dd84..cefe8d3c7 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/KnowledgeArtifactDraftVisitor.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/KnowledgeArtifactDraftVisitor.java @@ -1,7 +1,6 @@ package org.opencds.cqf.fhir.utility.visitor; import ca.uhn.fhir.context.FhirVersionEnum; -import ca.uhn.fhir.rest.server.exceptions.NotImplementedOperationException; import ca.uhn.fhir.rest.server.exceptions.PreconditionFailedException; import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; import java.util.ArrayList; @@ -13,7 +12,6 @@ import java.util.regex.Pattern; import java.util.stream.Collectors; import org.hl7.fhir.instance.model.api.IBase; -import org.hl7.fhir.instance.model.api.IBaseBundle; import org.hl7.fhir.instance.model.api.IBaseExtension; import org.hl7.fhir.instance.model.api.IBaseParameters; import org.hl7.fhir.instance.model.api.ICompositeType; @@ -26,19 +24,16 @@ import org.opencds.cqf.fhir.utility.adapter.AdapterFactory; import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; import org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter; -import org.opencds.cqf.fhir.utility.adapter.LibraryAdapter; -import org.opencds.cqf.fhir.utility.adapter.PlanDefinitionAdapter; -import org.opencds.cqf.fhir.utility.adapter.ValueSetAdapter; import org.opencds.cqf.fhir.utility.r4.PackageHelper; public class KnowledgeArtifactDraftVisitor implements KnowledgeArtifactVisitor { @Override - public IBaseBundle visit(LibraryAdapter library, Repository repository, IBaseParameters draftParameters) { - var fhirVersion = library.get().getStructureFhirVersionEnum(); + public IBase visit(KnowledgeArtifactAdapter adapter, Repository repository, IBaseParameters draftParameters) { + var fhirVersion = adapter.get().getStructureFhirVersionEnum(); String version = VisitorHelper.getParameter("version", draftParameters, IPrimitiveType.class) .map(r -> (String) r.getValue()) .orElseThrow(() -> new UnprocessableEntityException("The version argument is required")); - var libRes = library.get(); + var libRes = adapter.get(); // check valid semverversion checkVersionValidSemver(version); @@ -47,28 +42,28 @@ public IBaseBundle visit(LibraryAdapter library, Repository repository, IBasePar .filter(ext -> !ext.getUrl().equals(KnowledgeArtifactAdapter.releaseDescriptionUrl) && !ext.getUrl().equals(KnowledgeArtifactAdapter.releaseLabelUrl)) .collect(Collectors.toList()); - library.setExtension(removeReleaseLabelAndDescription); + adapter.setExtension(removeReleaseLabelAndDescription); // remove approval date - library.setApprovalDate(null); + adapter.setApprovalDate(null); // new draft version String draftVersion = version + "-draft"; - String draftVersionUrl = Canonicals.getUrl(library.getUrl()) + "|" + draftVersion; + String draftVersionUrl = Canonicals.getUrl(adapter.getUrl()) + "|" + draftVersion; // Root artifact must NOT have status of 'Active'. Existing drafts of // reference artifacts with the right verison number will be adopted. // This check is performed here to facilitate that different treatment // for the root artifact and those referenced by it. - if (library.getStatus() != "active") { + if (adapter.getStatus() != "active") { throw new PreconditionFailedException(String.format( "Drafts can only be created from artifacts with status of 'active'. Resource '%s' has a status of: '%s'", - library.getUrl(), library.getStatus())); + adapter.getUrl(), adapter.getStatus())); } // Ensure only one resource exists with this URL var existingArtifactsForUrl = SearchHelper.searchRepositoryByCanonicalWithPaging(repository, draftVersionUrl); if (BundleHelper.getEntry(existingArtifactsForUrl).size() != 0) { throw new PreconditionFailedException(String.format( "A draft of Program '%s' already exists with version: '%s'. Only one draft of a program version can exist at a time.", - library.getUrl(), draftVersionUrl)); + adapter.getUrl(), draftVersionUrl)); } // create draft resources List resourcesToCreate = @@ -95,22 +90,6 @@ public IBaseBundle visit(LibraryAdapter library, Repository repository, IBasePar // what is dependency, where did it originate? potentially the package? } - @Override - public IBase visit(KnowledgeArtifactAdapter library, Repository repository, IBaseParameters draftParameters) { - throw new NotImplementedOperationException("Not implemented"); - } - - @Override - public IBase visit( - PlanDefinitionAdapter planDefinition, Repository repository, IBaseParameters operationParameters) { - throw new NotImplementedOperationException("Not implemented"); - } - - @Override - public IBase visit(ValueSetAdapter valueSet, Repository repository, IBaseParameters operationParameters) { - throw new NotImplementedOperationException("Not implemented"); - } - private List createDraftsOfArtifactAndRelated( IDomainResource resource, Repository repository, diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/KnowledgeArtifactPackageVisitor.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/KnowledgeArtifactPackageVisitor.java index 7565a28e2..f85bac3c0 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/KnowledgeArtifactPackageVisitor.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/KnowledgeArtifactPackageVisitor.java @@ -24,19 +24,17 @@ import org.opencds.cqf.fhir.api.Repository; import org.opencds.cqf.fhir.utility.BundleHelper; import org.opencds.cqf.fhir.utility.Canonicals; +import org.opencds.cqf.fhir.utility.PackageHelper; import org.opencds.cqf.fhir.utility.SearchHelper; import org.opencds.cqf.fhir.utility.adapter.AdapterFactory; import org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter; -import org.opencds.cqf.fhir.utility.adapter.LibraryAdapter; -import org.opencds.cqf.fhir.utility.adapter.PlanDefinitionAdapter; -import org.opencds.cqf.fhir.utility.adapter.ValueSetAdapter; -import org.opencds.cqf.fhir.utility.r4.PackageHelper; public class KnowledgeArtifactPackageVisitor implements KnowledgeArtifactVisitor { @Override - public IBaseResource visit(LibraryAdapter library, Repository repository, IBaseParameters packageParameters) { - var fhirVersion = library.get().getStructureFhirVersionEnum(); + public IBase visit(KnowledgeArtifactAdapter adapter, Repository repository, IBaseParameters packageParameters) { + var fhirVersion = adapter.get().getStructureFhirVersionEnum(); + Optional artifactRoute = VisitorHelper.getParameter( "artifactRoute", packageParameters, IPrimitiveType.class) .map(r -> (String) r.getValue()); @@ -89,13 +87,13 @@ public IBaseResource visit(LibraryAdapter library, Repository repository, IBaseP if (count.isPresent() && count.get() < 0) { throw new UnprocessableEntityException("'count' must be non-negative"); } - var resource = library.get(); + var resource = adapter.get(); // TODO: In the case of a released (active) root Library we can depend on the relatedArtifacts as a // comprehensive manifest var packagedBundle = BundleHelper.newBundle(fhirVersion); if (include.size() == 1 && include.stream().anyMatch((includedType) -> includedType.equals("artifact"))) { - findUnsupportedCapability(library, capability); - processCanonicals(library, artifactVersion, checkArtifactVersion, forceArtifactVersion); + findUnsupportedCapability(adapter, capability); + processCanonicals(adapter, artifactVersion, checkArtifactVersion, forceArtifactVersion); var entry = PackageHelper.createEntry(resource, false); BundleHelper.addEntry(packagedBundle, entry); } else { @@ -130,7 +128,7 @@ protected void handleValueSetReferenceExtensions( case DSTU3: org.opencds.cqf.fhir.utility.visitor.dstu3.KnowledgeArtifactPackageVisitor packageVisitorDstu3 = new org.opencds.cqf.fhir.utility.visitor.dstu3.KnowledgeArtifactPackageVisitor(); - packageVisitorDstu3.handleValueSetReferenceExtensions( + packageVisitorDstu3.handleValueSets( (org.hl7.fhir.dstu3.model.MetadataResource) resource, ((org.hl7.fhir.dstu3.model.Bundle) packagedBundle).getEntry(), repository, @@ -179,9 +177,9 @@ void recursivePackage( boolean entryExists = BundleHelper.getEntryResources(bundle).stream() .map(e -> AdapterFactory.forFhirVersion(fhirVersion) .createKnowledgeArtifactAdapter((IDomainResource) e)) - .filter(mr -> mr.getUrl() != null && mr.getVersion() != null) + .filter(mr -> mr.getUrl() != null) .anyMatch(mr -> mr.getUrl().equals(adapter.getUrl()) - && mr.getVersion().equals(adapter.getVersion())); + && (!mr.hasVersion() || mr.getVersion().equals(adapter.getVersion()))); if (!entryExists) { var entry = PackageHelper.createEntry(resource, false); BundleHelper.addEntry(bundle, entry); @@ -218,22 +216,6 @@ void recursivePackage( } } - @Override - public IBase visit(KnowledgeArtifactAdapter library, Repository repository, IBaseParameters draftParameters) { - throw new NotImplementedOperationException("Not implemented"); - } - - @Override - public IBase visit( - PlanDefinitionAdapter planDefinition, Repository repository, IBaseParameters operationParameters) { - throw new NotImplementedOperationException("Not implemented"); - } - - @Override - public IBase visit(ValueSetAdapter valueSet, Repository repository, IBaseParameters operationParameters) { - throw new NotImplementedOperationException("Not implemented"); - } - private void findUnsupportedCapability(KnowledgeArtifactAdapter resource, List capability) throws PreconditionFailedException { if (capability != null && !capability.isEmpty()) { diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/KnowledgeArtifactReleaseVisitor.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/KnowledgeArtifactReleaseVisitor.java index 9011c43aa..da905cb0d 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/KnowledgeArtifactReleaseVisitor.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/KnowledgeArtifactReleaseVisitor.java @@ -32,9 +32,6 @@ import org.opencds.cqf.fhir.utility.adapter.AdapterFactory; import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; import org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter; -import org.opencds.cqf.fhir.utility.adapter.LibraryAdapter; -import org.opencds.cqf.fhir.utility.adapter.PlanDefinitionAdapter; -import org.opencds.cqf.fhir.utility.adapter.ValueSetAdapter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -43,7 +40,8 @@ public class KnowledgeArtifactReleaseVisitor implements KnowledgeArtifactVisitor @SuppressWarnings("unchecked") @Override - public IBase visit(LibraryAdapter rootLibraryAdapter, Repository repository, IBaseParameters operationParameters) { + public IBase visit( + KnowledgeArtifactAdapter rootAdapter, Repository repository, IBaseParameters operationParameters) { // boolean latestFromTxServer = operationParameters.getParameterBool("latestFromTxServer"); Optional latestFromTxServer = VisitorHelper.getParameter( "latestFromTxServer", operationParameters, IPrimitiveType.class) @@ -65,38 +63,37 @@ public IBase visit(LibraryAdapter rootLibraryAdapter, Repository repository, IBa "requireNonExperimental", operationParameters, IPrimitiveType.class) .map(t -> (String) t.getValue()); checkReleaseVersion(version, versionBehavior); - var rootLibrary = rootLibraryAdapter.get(); + var rootLibrary = rootAdapter.get(); var fhirVersion = rootLibrary.getStructureFhirVersionEnum(); - var currentApprovalDate = rootLibraryAdapter.getApprovalDate(); - checkReleasePreconditions(rootLibraryAdapter, currentApprovalDate); + var currentApprovalDate = rootAdapter.getApprovalDate(); + checkReleasePreconditions(rootAdapter, currentApprovalDate); // Determine which version should be used. - var existingVersion = rootLibraryAdapter.hasVersion() - ? rootLibraryAdapter.getVersion().replace("-draft", "") - : null; + var existingVersion = + rootAdapter.hasVersion() ? rootAdapter.getVersion().replace("-draft", "") : null; var releaseVersion = getReleaseVersion(version, versionBehavior, existingVersion, fhirVersion) .orElseThrow( () -> new UnprocessableEntityException("Could not resolve a version for the root artifact.")); - var rootEffectivePeriod = rootLibraryAdapter.getEffectivePeriod(); + var rootEffectivePeriod = rootAdapter.getEffectivePeriod(); // if the root artifact is experimental then we don't need to check for experimental children - if (rootLibraryAdapter.getExperimental()) { + if (rootAdapter.getExperimental()) { requireNonExpermimental = Optional.of("none"); } var releasedResources = internalRelease( - rootLibraryAdapter, + rootAdapter, releaseVersion, rootEffectivePeriod, latestFromTxServer.orElse(false), requireNonExpermimental, repository); updateReleaseLabel(rootLibrary, releaseLabel); - var rootArtifactOriginalDependencies = new ArrayList(rootLibraryAdapter.getDependencies()); + var rootArtifactOriginalDependencies = new ArrayList(rootAdapter.getDependencies()); // Get list of extensions which need to be preserved var originalDependenciesWithExtensions = rootArtifactOriginalDependencies.stream() .filter(dep -> dep.getExtension() != null && dep.getExtension().size() > 0) .collect(Collectors.toList()); // once iteration is complete, delete all depends-on RAs in the root artifact - rootLibraryAdapter.getRelatedArtifact().removeIf(ra -> KnowledgeArtifactAdapter.getRelatedArtifactType(ra) + rootAdapter.getRelatedArtifact().removeIf(ra -> KnowledgeArtifactAdapter.getRelatedArtifactType(ra) .equalsIgnoreCase("depends-on")); var transactionBundle = BundleHelper.newBundle(fhirVersion, null, "transaction"); @@ -128,7 +125,7 @@ public IBase visit(LibraryAdapter rootLibraryAdapter, Repository repository, IBa } var componentToDependency = KnowledgeArtifactAdapter.newRelatedArtifact( fhirVersion, "depends-on", KnowledgeArtifactAdapter.getRelatedArtifactReference(component)); - rootLibraryAdapter.getRelatedArtifact().add(componentToDependency); + rootAdapter.getRelatedArtifact().add(componentToDependency); } var dependencies = artifactAdapter.getDependencies(); @@ -149,15 +146,15 @@ public IBase visit(LibraryAdapter rootLibraryAdapter, Repository repository, IBa dependency.setReference(updatedReference); } // only add the dependency to the manifest if it is from a leaf artifact - if (!artifactAdapter.getUrl().equals(rootLibraryAdapter.getUrl())) { + if (!artifactAdapter.getUrl().equals(rootAdapter.getUrl())) { var newDep = KnowledgeArtifactAdapter.newRelatedArtifact( fhirVersion, "depends-on", dependency.getReference()); - rootLibraryAdapter.getRelatedArtifact().add(newDep); + rootAdapter.getRelatedArtifact().add(newDep); } } } // removed duplicates and add - var relatedArtifacts = rootLibraryAdapter.getRelatedArtifact(); + var relatedArtifacts = rootAdapter.getRelatedArtifact(); var distinctResolvedRelatedArtifacts = new ArrayList<>(relatedArtifacts); distinctResolvedRelatedArtifacts.clear(); for (var resolvedRelatedArtifact : relatedArtifacts) { @@ -191,7 +188,7 @@ public IBase visit(LibraryAdapter rootLibraryAdapter, Repository repository, IBa findArtifactCommentsToUpdate(rootLibrary, releaseVersion, repository).forEach(entry -> { BundleHelper.addEntry(transactionBundle, entry); }); - rootLibraryAdapter.setRelatedArtifact(distinctResolvedRelatedArtifacts); + rootAdapter.setRelatedArtifact(distinctResolvedRelatedArtifacts); return repository.transaction(transactionBundle); } @@ -485,22 +482,4 @@ private void checkVersionValidSemver(String version) throws UnprocessableEntityE "The version must be in the format MAJOR.MINOR.PATCH or MAJOR.MINOR.PATCH.REVISION"); } } - - @Override - public IBase visit(PlanDefinitionAdapter valueSet, Repository repository, IBaseParameters operationParameters) { - throw new NotImplementedOperationException("Not implemented"); - } - - @Override - public IBase visit(ValueSetAdapter valueSet, Repository repository, IBaseParameters operationParameters) { - throw new NotImplementedOperationException("Not implemented"); - } - - @Override - public IBase visit( - KnowledgeArtifactAdapter knowledgeArtifactAdapter, - Repository repository, - IBaseParameters operationParameters) { - throw new NotImplementedOperationException("Not implemented"); - } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/KnowledgeArtifactVisitor.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/KnowledgeArtifactVisitor.java index 4496ef6ca..256608680 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/KnowledgeArtifactVisitor.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/KnowledgeArtifactVisitor.java @@ -4,16 +4,7 @@ import org.hl7.fhir.instance.model.api.IBaseParameters; import org.opencds.cqf.fhir.api.Repository; import org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter; -import org.opencds.cqf.fhir.utility.adapter.LibraryAdapter; -import org.opencds.cqf.fhir.utility.adapter.PlanDefinitionAdapter; -import org.opencds.cqf.fhir.utility.adapter.ValueSetAdapter; public interface KnowledgeArtifactVisitor { IBase visit(KnowledgeArtifactAdapter knowledgeArtifact, Repository repository, IBaseParameters draftParameters); - - IBase visit(LibraryAdapter library, Repository repository, IBaseParameters draftParameters); - - IBase visit(PlanDefinitionAdapter planDefinition, Repository repository, IBaseParameters draftParameters); - - IBase visit(ValueSetAdapter valueSet, Repository repository, IBaseParameters draftParameters); } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactPackageVisitor.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactPackageVisitor.java index ba384fe0c..b7c02039f 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactPackageVisitor.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactPackageVisitor.java @@ -1,5 +1,7 @@ package org.opencds.cqf.fhir.utility.visitor.dstu3; +import static org.opencds.cqf.fhir.utility.ValueSets.getCodesInCompose; + import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; import java.time.Instant; @@ -14,7 +16,6 @@ import org.hl7.fhir.dstu3.model.Bundle; import org.hl7.fhir.dstu3.model.Bundle.BundleEntryComponent; import org.hl7.fhir.dstu3.model.Bundle.BundleType; -import org.hl7.fhir.dstu3.model.CodeableConcept; import org.hl7.fhir.dstu3.model.Coding; import org.hl7.fhir.dstu3.model.Endpoint; import org.hl7.fhir.dstu3.model.Extension; @@ -22,24 +23,22 @@ import org.hl7.fhir.dstu3.model.MetadataResource; import org.hl7.fhir.dstu3.model.Parameters; import org.hl7.fhir.dstu3.model.Reference; -import org.hl7.fhir.dstu3.model.RelatedArtifact; import org.hl7.fhir.dstu3.model.Resource; import org.hl7.fhir.dstu3.model.ResourceType; import org.hl7.fhir.dstu3.model.StructureDefinition; import org.hl7.fhir.dstu3.model.UsageContext; import org.hl7.fhir.dstu3.model.ValueSet; -import org.hl7.fhir.instance.model.api.IBaseResource; import org.opencds.cqf.fhir.api.Repository; -import org.opencds.cqf.fhir.utility.Canonicals; import org.opencds.cqf.fhir.utility.Constants; -import org.opencds.cqf.fhir.utility.adapter.AdapterFactory; import org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter; import org.opencds.cqf.fhir.utility.client.TerminologyServerClient; public class KnowledgeArtifactPackageVisitor { + private final FhirContext fhirContext = FhirContext.forDstu3Cached(); + public KnowledgeArtifactPackageVisitor() { - this.terminologyServerClient = new TerminologyServerClient(FhirContext.forDstu3()); + this.terminologyServerClient = new TerminologyServerClient(fhirContext); } private TerminologyServerClient terminologyServerClient; @@ -209,21 +208,17 @@ public static List findUnsupportedInclude( * @param manifest the resource containing all RelatedArtifact references * @param bundleEntries the list of packaged resources to modify according to the extensions on the manifest relatedArtifact references */ - public void handleValueSetReferenceExtensions( + public void handleValueSets( MetadataResource manifest, List bundleEntries, Repository repository, Optional terminologyEndpoint) throws UnprocessableEntityException, IllegalArgumentException { - KnowledgeArtifactAdapter adapter = AdapterFactory.forFhirVersion(manifest.getStructureFhirVersionEnum()) - .createKnowledgeArtifactAdapter(manifest); - List relatedArtifactsWithPreservedExtension = - getRelatedArtifactsWithPreservedExtensions(adapter.getRelatedArtifact()); Parameters expansionParams = new Parameters(); Library rootSpecificationLibrary = getRootSpecificationLibrary(bundleEntries); if (rootSpecificationLibrary != null) { Extension expansionParamsExtension = - rootSpecificationLibrary.getExtensionByUrl(Constants.EXPANSION_PARAMETERS_URL); + rootSpecificationLibrary.getExtensionByUrl(Constants.CQF_EXPANSION_PARAMETERS); if (expansionParamsExtension != null && expansionParamsExtension.hasValue()) { Reference expansionReference = (Reference) expansionParamsExtension.getValue(); expansionParams = getExpansionParams(rootSpecificationLibrary, expansionReference.getReference()); @@ -233,48 +228,7 @@ public void handleValueSetReferenceExtensions( bundleEntries.stream().forEach(entry -> { if (entry.getResource().getResourceType().equals(ResourceType.ValueSet)) { ValueSet valueSet = (ValueSet) entry.getResource(); - // remove any existing Priority and Conditions - List usageContexts = removeExistingReferenceExtensionData(valueSet.getUseContext()); - valueSet.setUseContext(usageContexts); - Optional maybeVSRelatedArtifact = relatedArtifactsWithPreservedExtension.stream() - .filter(ra -> Canonicals.getUrl(ra.getResource().getReference()) - .equals(valueSet.getUrl())) - .findFirst(); - checkIfValueSetNeedsCondition(valueSet, maybeVSRelatedArtifact.orElse(null)); - // If leaf valueset - if (!valueSet.hasCompose() - || (valueSet.hasCompose() - && valueSet.getCompose() - .getIncludeFirstRep() - .getValueSet() - .isEmpty())) { - expandValueSet(valueSet, params, terminologyEndpoint); - // If Condition extension is present - maybeVSRelatedArtifact - .map(ra -> ra.getExtension()) - .ifPresent( - // add Conditions - exts -> { - exts.stream() - .filter(ext -> ext.getUrl() - .equalsIgnoreCase(Constants.VALUE_SET_CONDITION_URL)) - .forEach(ext -> tryAddCondition( - usageContexts, (CodeableConcept) ext.getValue())); - }); - } - // update Priority - UsageContext priority = getOrCreateUsageContext( - usageContexts, KnowledgeArtifactAdapter.usPhContextTypeUrl, Constants.VALUE_SET_PRIORITY_CODE); - Optional ext = - maybeVSRelatedArtifact.map(ra -> ra.getExtensionByUrl(Constants.VALUE_SET_PRIORITY_URL)); - if (ext.isPresent()) { - priority.setValue(ext.get().getValue()); - } else { - CodeableConcept routine = new CodeableConcept( - new Coding(KnowledgeArtifactAdapter.contextUrl, "routine", null)) - .setText("Routine"); - priority.setValue(routine); - } + expandValueSet(valueSet, params, terminologyEndpoint); } }); } @@ -288,7 +242,7 @@ public void expandValueSet( : valueSet.getUrl(); ValueSet expandedValueSet; - if (isVSMAuthoredValueSet(valueSet) && hasSimpleCompose(valueSet)) { + if (hasSimpleCompose(valueSet)) { // Perform naive expansion independent of terminology servers. Copy all codes from compose into expansion. ValueSet.ValueSetExpansionComponent expansion = new ValueSet.ValueSetExpansionComponent(); expansion.setTimestamp(Date.from(Instant.now())); @@ -301,15 +255,13 @@ public void expandValueSet( expansionParams.add(parameterNaive); expansion.setParameter(expansionParams); - for (ValueSet.ConceptSetComponent csc : valueSet.getCompose().getInclude()) { - for (ValueSet.ConceptReferenceComponent crc : csc.getConcept()) { - expansion - .addContains() - .setCode(crc.getCode()) - .setSystem(csc.getSystem()) - .setVersion(csc.getVersion()) - .setDisplay(crc.getDisplay()); - } + for (var code : getCodesInCompose(fhirContext, valueSet)) { + expansion + .addContains() + .setCode(code.getCode()) + .setSystem(code.getSystem()) + .setVersion(code.getVersion()) + .setDisplay(code.getDisplay()); } valueSet.setExpansion(expansion); } else { @@ -328,7 +280,7 @@ public void expandValueSet( "Cannot expand ValueSet without VSAC API Key: " + valueSet.getId())); } else { throw new UnprocessableEntityException( - "Cannot expand ValueSet without credentials: " + valueSet.getId()); + "Cannot expand ValueSet without a terminology server: " + valueSet.getId()); } try { @@ -342,32 +294,14 @@ public void expandValueSet( } } - protected boolean isVSMAuthoredValueSet(ValueSet valueSet) { - return valueSet.hasMeta() - && valueSet.getMeta().hasTag() - && valueSet.getMeta() - .getTag( - Constants.VSM_WORKFLOW_CODES_CODE_SYSTEM_URL, - Constants.VSM_VALUE_SET_TAG_VSM_AUTHORED_CODE) - != null; - } - - // A simple compose element of a ValueSet must have a compose without an exclude element. Each - // element of the include cannot reference a value set or have a filter, and must have a system - // and enumerate concepts + // A simple compose element of a ValueSet must have a compose without an exclude element. Each element of the + // include cannot have a filter, and must reference a ValueSet or have a system and enumerate concepts protected boolean hasSimpleCompose(ValueSet valueSet) { return valueSet.hasCompose() && !valueSet.getCompose().hasExclude() && valueSet.getCompose().getInclude().stream() - .noneMatch( - csc -> csc.hasValueSet() || csc.hasFilter() || !csc.hasSystem() || !csc.hasConcept()); - } - - protected List getRelatedArtifactsWithPreservedExtensions(List deps) { - return deps.stream() - .filter(ra -> Constants.PRESERVED_EXTENSION_URLS.stream().anyMatch(url -> ra.getExtension().stream() - .anyMatch(ext -> ext.getUrl().equalsIgnoreCase(url)))) - .collect(Collectors.toList()); + .noneMatch(csc -> + csc.hasFilter() || (!csc.hasValueSet() && (!csc.hasSystem() && !csc.hasConcept()))); } protected static Library getRootSpecificationLibrary(List bundleEntries) { @@ -409,77 +343,6 @@ protected static Parameters getExpansionParams(Library rootSpecificationLibrary, return (Parameters) expansionParamResource.orElse(null); } - protected void checkIfValueSetNeedsCondition(IBaseResource resource, RelatedArtifact relatedArtifact) - throws UnprocessableEntityException { - if (resource != null && resource.fhirType().equals(ResourceType.ValueSet.name())) { - ValueSet valueSet = (ValueSet) resource; - boolean isLeaf = !valueSet.hasCompose() - || (valueSet.hasCompose() - && valueSet.getCompose() - .getIncludeFirstRep() - .getValueSet() - .isEmpty()); - Optional maybeConditionExtension = Optional.ofNullable(relatedArtifact) - .map(RelatedArtifact::getExtension) - .map(list -> { - return list.stream() - .filter(ext -> ext.getUrl().equalsIgnoreCase(Constants.VALUE_SET_CONDITION_URL)) - .findFirst() - .orElse(null); - }); - Optional maybePriorityExtension = Optional.ofNullable(relatedArtifact) - .map(RelatedArtifact::getExtension) - .map(list -> { - return list.stream() - .filter(ext -> ext.getUrl().equalsIgnoreCase(Constants.VALUE_SET_PRIORITY_URL)) - .findFirst() - .orElse(null); - }); - if (isLeaf && (!maybeConditionExtension.isPresent() || !maybePriorityExtension.isPresent())) { - if (!maybeConditionExtension.isPresent() && !maybePriorityExtension.isPresent()) { - throw new UnprocessableEntityException( - "Missing condition and priority references on ValueSet : " + valueSet.getUrl()); - } else if (!maybeConditionExtension.isPresent()) { - throw new UnprocessableEntityException( - "Missing condition reference on ValueSet : " + valueSet.getUrl()); - } else { - throw new UnprocessableEntityException( - "Missing priority reference on ValueSet : " + valueSet.getUrl()); - } - } - } - } - - /** - * Removes any existing UsageContexts corresponding to the VSM specific extensions - * @param usageContexts the list of usage contexts to modify - */ - protected List removeExistingReferenceExtensionData(List usageContexts) { - List useContextCodesToReplace = Collections.unmodifiableList( - Arrays.asList(Constants.VALUE_SET_CONDITION_CODE, Constants.VALUE_SET_PRIORITY_CODE)); - return usageContexts.stream() - // remove any useContexts which need to be replaced - .filter(useContext -> !useContextCodesToReplace.stream() - .anyMatch(code -> useContext.getCode().getCode().equals(code))) - .collect(Collectors.toList()); - } - - protected void tryAddCondition(List usageContexts, CodeableConcept condition) { - boolean focusAlreadyExists = usageContexts.stream() - .anyMatch(u -> u.getCode().getSystem().equals(KnowledgeArtifactAdapter.contextTypeUrl) - && u.getCode().getCode().equals(Constants.VALUE_SET_CONDITION_CODE) - && u.getValueCodeableConcept() - .hasCoding( - condition.getCoding().get(0).getSystem(), - condition.getCoding().get(0).getCode())); - if (!focusAlreadyExists) { - UsageContext newFocus = new UsageContext( - new Coding(KnowledgeArtifactAdapter.contextUrl, Constants.VALUE_SET_CONDITION_CODE, null), - condition); - newFocus.setValue(condition); - usageContexts.add(newFocus); - } - } /** * * Either finds a usageContext with the same system and code or creates an empty one diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactPackageVisitor.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactPackageVisitor.java index c2f5af21b..9cfb79d49 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactPackageVisitor.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactPackageVisitor.java @@ -1,5 +1,7 @@ package org.opencds.cqf.fhir.utility.visitor.r4; +import static org.opencds.cqf.fhir.utility.ValueSets.getCodesInCompose; + import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; import java.time.Instant; @@ -10,12 +12,10 @@ import java.util.List; import java.util.Optional; import java.util.stream.Collectors; -import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.r4.model.BooleanType; import org.hl7.fhir.r4.model.Bundle; import org.hl7.fhir.r4.model.Bundle.BundleEntryComponent; import org.hl7.fhir.r4.model.Bundle.BundleType; -import org.hl7.fhir.r4.model.CodeableConcept; import org.hl7.fhir.r4.model.Coding; import org.hl7.fhir.r4.model.Endpoint; import org.hl7.fhir.r4.model.Extension; @@ -23,23 +23,23 @@ import org.hl7.fhir.r4.model.MetadataResource; import org.hl7.fhir.r4.model.Parameters; import org.hl7.fhir.r4.model.Reference; -import org.hl7.fhir.r4.model.RelatedArtifact; import org.hl7.fhir.r4.model.Resource; import org.hl7.fhir.r4.model.ResourceType; import org.hl7.fhir.r4.model.StructureDefinition; import org.hl7.fhir.r4.model.UsageContext; import org.hl7.fhir.r4.model.ValueSet; import org.opencds.cqf.fhir.api.Repository; -import org.opencds.cqf.fhir.utility.Canonicals; import org.opencds.cqf.fhir.utility.Constants; -import org.opencds.cqf.fhir.utility.adapter.AdapterFactory; import org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter; import org.opencds.cqf.fhir.utility.client.TerminologyServerClient; public class KnowledgeArtifactPackageVisitor { + // private Logger logger = LoggerFactory.getLogger(KnowledgeArtifactPackageVisitor.class); + + private final FhirContext fhirContext = FhirContext.forR4Cached(); public KnowledgeArtifactPackageVisitor() { - this.terminologyServerClient = new TerminologyServerClient(FhirContext.forR4()); + this.terminologyServerClient = new TerminologyServerClient(fhirContext); } private TerminologyServerClient terminologyServerClient; @@ -227,15 +227,11 @@ public void handleValueSetReferenceExtensions( Repository repository, Optional terminologyEndpoint) throws UnprocessableEntityException, IllegalArgumentException { - KnowledgeArtifactAdapter adapter = AdapterFactory.forFhirVersion(manifest.getStructureFhirVersionEnum()) - .createKnowledgeArtifactAdapter(manifest); - List relatedArtifactsWithPreservedExtension = - getRelatedArtifactsWithPreservedExtensions(adapter.getRelatedArtifact()); Parameters expansionParams = new Parameters(); Library rootSpecificationLibrary = getRootSpecificationLibrary(bundleEntries); if (rootSpecificationLibrary != null) { Extension expansionParamsExtension = - rootSpecificationLibrary.getExtensionByUrl(Constants.EXPANSION_PARAMETERS_URL); + rootSpecificationLibrary.getExtensionByUrl(Constants.CQF_EXPANSION_PARAMETERS); if (expansionParamsExtension != null && expansionParamsExtension.hasValue()) { Reference expansionReference = (Reference) expansionParamsExtension.getValue(); expansionParams = getExpansionParams(rootSpecificationLibrary, expansionReference.getReference()); @@ -245,48 +241,7 @@ public void handleValueSetReferenceExtensions( bundleEntries.stream().forEach(entry -> { if (entry.getResource().getResourceType().equals(ResourceType.ValueSet)) { ValueSet valueSet = (ValueSet) entry.getResource(); - // remove any existing Priority and Conditions - List usageContexts = removeExistingReferenceExtensionData(valueSet.getUseContext()); - valueSet.setUseContext(usageContexts); - Optional maybeVSRelatedArtifact = relatedArtifactsWithPreservedExtension.stream() - .filter(ra -> Canonicals.getUrl(ra.getResource()).equals(valueSet.getUrl())) - .findFirst(); - checkIfValueSetNeedsCondition(valueSet, maybeVSRelatedArtifact.orElse(null)); - // If leaf valueset - if (!valueSet.hasCompose() - || (valueSet.hasCompose() - && valueSet.getCompose() - .getIncludeFirstRep() - .getValueSet() - .isEmpty())) { - expandValueSet(valueSet, params, terminologyEndpoint); - // If Condition extension is present - maybeVSRelatedArtifact - .map(ra -> ra.getExtension()) - .ifPresent( - // add Conditions - exts -> { - exts.stream() - .filter(ext -> ext.getUrl() - .equalsIgnoreCase(Constants.VALUE_SET_CONDITION_URL)) - .forEach(ext -> tryAddCondition( - usageContexts, (CodeableConcept) ext.getValue())); - }); - } - // update Priority - UsageContext priority = getOrCreateUsageContext( - usageContexts, KnowledgeArtifactAdapter.usPhContextTypeUrl, Constants.VALUE_SET_PRIORITY_CODE); - Optional ext = - maybeVSRelatedArtifact.map(ra -> ra.getExtensionByUrl(Constants.VALUE_SET_PRIORITY_URL)); - - if (ext.isPresent()) { - priority.setValue(ext.get().getValue()); - } else { - CodeableConcept routine = new CodeableConcept( - new Coding(KnowledgeArtifactAdapter.contextUrl, "routine", null)) - .setText("Routine"); - priority.setValue(routine); - } + expandValueSet(valueSet, params, terminologyEndpoint); } }); } @@ -300,7 +255,7 @@ public void expandValueSet( : valueSet.getUrl(); ValueSet expandedValueSet; - if (isVSMAuthoredValueSet(valueSet) && hasSimpleCompose(valueSet)) { + if (hasSimpleCompose(valueSet)) { // Perform naive expansion independent of terminology servers. Copy all codes from compose into expansion. ValueSet.ValueSetExpansionComponent expansion = new ValueSet.ValueSetExpansionComponent(); expansion.setTimestamp(Date.from(Instant.now())); @@ -313,15 +268,13 @@ public void expandValueSet( expansionParams.add(parameterNaive); expansion.setParameter(expansionParams); - for (ValueSet.ConceptSetComponent csc : valueSet.getCompose().getInclude()) { - for (ValueSet.ConceptReferenceComponent crc : csc.getConcept()) { - expansion - .addContains() - .setCode(crc.getCode()) - .setSystem(csc.getSystem()) - .setVersion(csc.getVersion()) - .setDisplay(crc.getDisplay()); - } + for (var code : getCodesInCompose(fhirContext, valueSet)) { + expansion + .addContains() + .setCode(code.getCode()) + .setSystem(code.getSystem()) + .setVersion(code.getVersion()) + .setDisplay(code.getDisplay()); } valueSet.setExpansion(expansion); } else { @@ -340,7 +293,7 @@ public void expandValueSet( "Cannot expand ValueSet without VSAC API Key: " + valueSet.getId())); } else { throw new UnprocessableEntityException( - "Cannot expand ValueSet without credentials: " + valueSet.getId()); + "Cannot expand ValueSet without a terminology server: " + valueSet.getId()); } try { @@ -354,32 +307,14 @@ public void expandValueSet( } } - protected boolean isVSMAuthoredValueSet(ValueSet valueSet) { - return valueSet.hasMeta() - && valueSet.getMeta().hasTag() - && valueSet.getMeta() - .getTag( - Constants.VSM_WORKFLOW_CODES_CODE_SYSTEM_URL, - Constants.VSM_VALUE_SET_TAG_VSM_AUTHORED_CODE) - != null; - } - - // A simple compose element of a ValueSet must have a compose without an exclude element. Each - // element of the include cannot reference a value set or have a filter, and must have a system - // and enumerate concepts + // A simple compose element of a ValueSet must have a compose without an exclude element. Each element of the + // include cannot have a filter, and must reference a ValueSet or have a system and enumerate concepts protected boolean hasSimpleCompose(ValueSet valueSet) { return valueSet.hasCompose() && !valueSet.getCompose().hasExclude() && valueSet.getCompose().getInclude().stream() - .noneMatch( - csc -> csc.hasValueSet() || csc.hasFilter() || !csc.hasSystem() || !csc.hasConcept()); - } - - protected List getRelatedArtifactsWithPreservedExtensions(List deps) { - return deps.stream() - .filter(ra -> Constants.PRESERVED_EXTENSION_URLS.stream().anyMatch(url -> ra.getExtension().stream() - .anyMatch(ext -> ext.getUrl().equalsIgnoreCase(url)))) - .collect(Collectors.toList()); + .noneMatch(csc -> + csc.hasFilter() || (!csc.hasValueSet() && (!csc.hasSystem() && !csc.hasConcept()))); } protected static Library getRootSpecificationLibrary(List bundleEntries) { @@ -421,79 +356,6 @@ protected static Parameters getExpansionParams(Library rootSpecificationLibrary, return (Parameters) expansionParamResource.orElse(null); } - protected void checkIfValueSetNeedsCondition(IBaseResource resource, RelatedArtifact relatedArtifact) - throws UnprocessableEntityException { - if (resource != null && resource.fhirType().equals(ResourceType.ValueSet.name())) { - ValueSet valueSet = (ValueSet) resource; - // TODO:: do we need to update the definition of a leaf? - boolean isLeaf = !valueSet.hasCompose() - || (valueSet.hasCompose() - && valueSet.getCompose() - .getIncludeFirstRep() - .getValueSet() - .isEmpty()); - Optional maybeConditionExtension = Optional.ofNullable(relatedArtifact) - .map(RelatedArtifact::getExtension) - .map(list -> { - return list.stream() - .filter(ext -> ext.getUrl().equalsIgnoreCase(Constants.VALUE_SET_CONDITION_URL)) - .findFirst() - .orElse(null); - }); - Optional maybePriorityExtension = Optional.ofNullable(relatedArtifact) - .map(RelatedArtifact::getExtension) - .map(list -> { - return list.stream() - .filter(ext -> ext.getUrl().equalsIgnoreCase(Constants.VALUE_SET_PRIORITY_URL)) - .findFirst() - .orElse(null); - }); - if (isLeaf && (!maybeConditionExtension.isPresent() || !maybePriorityExtension.isPresent())) { - if (!maybeConditionExtension.isPresent() && !maybePriorityExtension.isPresent()) { - throw new UnprocessableEntityException( - "Missing condition and priority references on ValueSet : " + valueSet.getUrl()); - } else if (!maybeConditionExtension.isPresent()) { - throw new UnprocessableEntityException( - "Missing condition reference on ValueSet : " + valueSet.getUrl()); - } else { - throw new UnprocessableEntityException( - "Missing priority reference on ValueSet : " + valueSet.getUrl()); - } - } - } - } - - /** - * Removes any existing UsageContexts corresponding to the VSM specific extensions - * @param usageContexts the list of usage contexts to modify - */ - protected List removeExistingReferenceExtensionData(List usageContexts) { - List useContextCodesToReplace = Collections.unmodifiableList( - Arrays.asList(Constants.VALUE_SET_CONDITION_CODE, Constants.VALUE_SET_PRIORITY_CODE)); - return usageContexts.stream() - // remove any useContexts which need to be replaced - .filter(useContext -> !useContextCodesToReplace.stream() - .anyMatch(code -> useContext.getCode().getCode().equals(code))) - .collect(Collectors.toList()); - } - - protected void tryAddCondition(List usageContexts, CodeableConcept condition) { - boolean focusAlreadyExists = usageContexts.stream() - .anyMatch(u -> u.getCode().getSystem().equals(KnowledgeArtifactAdapter.contextTypeUrl) - && u.getCode().getCode().equals(Constants.VALUE_SET_CONDITION_CODE) - && u.getValueCodeableConcept() - .hasCoding( - condition.getCoding().get(0).getSystem(), - condition.getCoding().get(0).getCode())); - if (!focusAlreadyExists) { - UsageContext newFocus = new UsageContext( - new Coding(KnowledgeArtifactAdapter.contextUrl, Constants.VALUE_SET_CONDITION_CODE, null), - condition); - newFocus.setValue(condition); - usageContexts.add(newFocus); - } - } - /** * * Either finds a usageContext with the same system and code or creates an empty one diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactPackageVisitor.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactPackageVisitor.java index 07eab24af..9b7f3cefa 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactPackageVisitor.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactPackageVisitor.java @@ -1,5 +1,7 @@ package org.opencds.cqf.fhir.utility.visitor.r5; +import static org.opencds.cqf.fhir.utility.ValueSets.getCodesInCompose; + import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; import java.time.Instant; @@ -10,12 +12,10 @@ import java.util.List; import java.util.Optional; import java.util.stream.Collectors; -import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.r5.model.BooleanType; import org.hl7.fhir.r5.model.Bundle; import org.hl7.fhir.r5.model.Bundle.BundleEntryComponent; import org.hl7.fhir.r5.model.Bundle.BundleType; -import org.hl7.fhir.r5.model.CodeableConcept; import org.hl7.fhir.r5.model.Coding; import org.hl7.fhir.r5.model.Endpoint; import org.hl7.fhir.r5.model.Extension; @@ -23,23 +23,22 @@ import org.hl7.fhir.r5.model.MetadataResource; import org.hl7.fhir.r5.model.Parameters; import org.hl7.fhir.r5.model.Reference; -import org.hl7.fhir.r5.model.RelatedArtifact; import org.hl7.fhir.r5.model.Resource; import org.hl7.fhir.r5.model.ResourceType; import org.hl7.fhir.r5.model.StructureDefinition; import org.hl7.fhir.r5.model.UsageContext; import org.hl7.fhir.r5.model.ValueSet; import org.opencds.cqf.fhir.api.Repository; -import org.opencds.cqf.fhir.utility.Canonicals; import org.opencds.cqf.fhir.utility.Constants; -import org.opencds.cqf.fhir.utility.adapter.AdapterFactory; import org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter; import org.opencds.cqf.fhir.utility.client.TerminologyServerClient; public class KnowledgeArtifactPackageVisitor { + private final FhirContext fhirContext = FhirContext.forR5Cached(); + public KnowledgeArtifactPackageVisitor() { - this.terminologyServerClient = new TerminologyServerClient(FhirContext.forR5()); + this.terminologyServerClient = new TerminologyServerClient(fhirContext); } private TerminologyServerClient terminologyServerClient; @@ -223,15 +222,11 @@ public void handleValueSetReferenceExtensions( Repository repository, Optional terminologyEndpoint) throws UnprocessableEntityException, IllegalArgumentException { - KnowledgeArtifactAdapter adapter = AdapterFactory.forFhirVersion(manifest.getStructureFhirVersionEnum()) - .createKnowledgeArtifactAdapter(manifest); - List relatedArtifactsWithPreservedExtension = - getRelatedArtifactsWithPreservedExtensions(adapter.getRelatedArtifact()); Parameters expansionParams = new Parameters(); Library rootSpecificationLibrary = getRootSpecificationLibrary(bundleEntries); if (rootSpecificationLibrary != null) { Extension expansionParamsExtension = - rootSpecificationLibrary.getExtensionByUrl(Constants.EXPANSION_PARAMETERS_URL); + rootSpecificationLibrary.getExtensionByUrl(Constants.CQF_EXPANSION_PARAMETERS); if (expansionParamsExtension != null && expansionParamsExtension.hasValue()) { Reference expansionReference = (Reference) expansionParamsExtension.getValue(); expansionParams = getExpansionParams(rootSpecificationLibrary, expansionReference.getReference()); @@ -241,48 +236,7 @@ public void handleValueSetReferenceExtensions( bundleEntries.stream().forEach(entry -> { if (entry.getResource().getResourceType().equals(ResourceType.ValueSet)) { ValueSet valueSet = (ValueSet) entry.getResource(); - // remove any existing Priority and Conditions - List usageContexts = removeExistingReferenceExtensionData(valueSet.getUseContext()); - valueSet.setUseContext(usageContexts); - Optional maybeVSRelatedArtifact = relatedArtifactsWithPreservedExtension.stream() - .filter(ra -> Canonicals.getUrl(ra.getResource()).equals(valueSet.getUrl())) - .findFirst(); - checkIfValueSetNeedsCondition(valueSet, maybeVSRelatedArtifact.orElse(null)); - // If leaf valueset - if (!valueSet.hasCompose() - || (valueSet.hasCompose() - && valueSet.getCompose() - .getIncludeFirstRep() - .getValueSet() - .isEmpty())) { - expandValueSet(valueSet, params, terminologyEndpoint); - // If Condition extension is present - maybeVSRelatedArtifact - .map(ra -> ra.getExtension()) - .ifPresent( - // add Conditions - exts -> { - exts.stream() - .filter(ext -> ext.getUrl() - .equalsIgnoreCase(Constants.VALUE_SET_CONDITION_URL)) - .forEach(ext -> tryAddCondition( - usageContexts, (CodeableConcept) ext.getValue())); - }); - } - // update Priority - UsageContext priority = getOrCreateUsageContext( - usageContexts, KnowledgeArtifactAdapter.usPhContextTypeUrl, Constants.VALUE_SET_PRIORITY_CODE); - Optional ext = - maybeVSRelatedArtifact.map(ra -> ra.getExtensionByUrl(Constants.VALUE_SET_PRIORITY_URL)); - - if (ext.isPresent()) { - priority.setValue(ext.get().getValue()); - } else { - CodeableConcept routine = new CodeableConcept( - new Coding(KnowledgeArtifactAdapter.contextUrl, "routine", null)) - .setText("Routine"); - priority.setValue(routine); - } + expandValueSet(valueSet, params, terminologyEndpoint); } }); } @@ -296,7 +250,7 @@ public void expandValueSet( : valueSet.getUrl(); ValueSet expandedValueSet; - if (isVSMAuthoredValueSet(valueSet) && hasSimpleCompose(valueSet)) { + if (hasSimpleCompose(valueSet)) { // Perform naive expansion independent of terminology servers. Copy all codes from compose into expansion. ValueSet.ValueSetExpansionComponent expansion = new ValueSet.ValueSetExpansionComponent(); expansion.setTimestamp(Date.from(Instant.now())); @@ -309,15 +263,13 @@ public void expandValueSet( expansionParams.add(parameterNaive); expansion.setParameter(expansionParams); - for (ValueSet.ConceptSetComponent csc : valueSet.getCompose().getInclude()) { - for (ValueSet.ConceptReferenceComponent crc : csc.getConcept()) { - expansion - .addContains() - .setCode(crc.getCode()) - .setSystem(csc.getSystem()) - .setVersion(csc.getVersion()) - .setDisplay(crc.getDisplay()); - } + for (var code : getCodesInCompose(fhirContext, valueSet)) { + expansion + .addContains() + .setCode(code.getCode()) + .setSystem(code.getSystem()) + .setVersion(code.getVersion()) + .setDisplay(code.getDisplay()); } valueSet.setExpansion(expansion); } else { @@ -336,7 +288,7 @@ public void expandValueSet( "Cannot expand ValueSet without VSAC API Key: " + valueSet.getId())); } else { throw new UnprocessableEntityException( - "Cannot expand ValueSet without credentials: " + valueSet.getId()); + "Cannot expand ValueSet without a terminology server: " + valueSet.getId()); } try { @@ -350,32 +302,14 @@ public void expandValueSet( } } - protected boolean isVSMAuthoredValueSet(ValueSet valueSet) { - return valueSet.hasMeta() - && valueSet.getMeta().hasTag() - && valueSet.getMeta() - .getTag( - Constants.VSM_WORKFLOW_CODES_CODE_SYSTEM_URL, - Constants.VSM_VALUE_SET_TAG_VSM_AUTHORED_CODE) - != null; - } - - // A simple compose element of a ValueSet must have a compose without an exclude element. Each - // element of the include cannot reference a value set or have a filter, and must have a system - // and enumerate concepts + // A simple compose element of a ValueSet must have a compose without an exclude element. Each element of the + // include cannot have a filter, and must reference a ValueSet or have a system and enumerate concepts protected boolean hasSimpleCompose(ValueSet valueSet) { return valueSet.hasCompose() && !valueSet.getCompose().hasExclude() && valueSet.getCompose().getInclude().stream() - .noneMatch( - csc -> csc.hasValueSet() || csc.hasFilter() || !csc.hasSystem() || !csc.hasConcept()); - } - - protected List getRelatedArtifactsWithPreservedExtensions(List deps) { - return deps.stream() - .filter(ra -> Constants.PRESERVED_EXTENSION_URLS.stream().anyMatch(url -> ra.getExtension().stream() - .anyMatch(ext -> ext.getUrl().equalsIgnoreCase(url)))) - .collect(Collectors.toList()); + .noneMatch(csc -> + csc.hasFilter() || (!csc.hasValueSet() && (!csc.hasSystem() && !csc.hasConcept()))); } protected static Library getRootSpecificationLibrary(List bundleEntries) { @@ -417,77 +351,6 @@ protected static Parameters getExpansionParams(Library rootSpecificationLibrary, return (Parameters) expansionParamResource.orElse(null); } - protected void checkIfValueSetNeedsCondition(IBaseResource resource, RelatedArtifact relatedArtifact) - throws UnprocessableEntityException { - if (resource != null && resource.fhirType().equals(ResourceType.ValueSet.name())) { - ValueSet valueSet = (ValueSet) resource; - boolean isLeaf = !valueSet.hasCompose() - || (valueSet.hasCompose() - && valueSet.getCompose() - .getIncludeFirstRep() - .getValueSet() - .isEmpty()); - Optional maybeConditionExtension = Optional.ofNullable(relatedArtifact) - .map(RelatedArtifact::getExtension) - .map(list -> { - return list.stream() - .filter(ext -> ext.getUrl().equalsIgnoreCase(Constants.VALUE_SET_CONDITION_URL)) - .findFirst() - .orElse(null); - }); - Optional maybePriorityExtension = Optional.ofNullable(relatedArtifact) - .map(RelatedArtifact::getExtension) - .map(list -> { - return list.stream() - .filter(ext -> ext.getUrl().equalsIgnoreCase(Constants.VALUE_SET_PRIORITY_URL)) - .findFirst() - .orElse(null); - }); - if (isLeaf && (!maybeConditionExtension.isPresent() || !maybePriorityExtension.isPresent())) { - if (!maybeConditionExtension.isPresent() && !maybePriorityExtension.isPresent()) { - throw new UnprocessableEntityException( - "Missing condition and priority references on ValueSet : " + valueSet.getUrl()); - } else if (!maybeConditionExtension.isPresent()) { - throw new UnprocessableEntityException( - "Missing condition reference on ValueSet : " + valueSet.getUrl()); - } else { - throw new UnprocessableEntityException( - "Missing priority reference on ValueSet : " + valueSet.getUrl()); - } - } - } - } - - /** - * Removes any existing UsageContexts corresponding to the VSM specific extensions - * @param usageContexts the list of usage contexts to modify - */ - protected List removeExistingReferenceExtensionData(List usageContexts) { - List useContextCodesToReplace = Collections.unmodifiableList( - Arrays.asList(Constants.VALUE_SET_CONDITION_CODE, Constants.VALUE_SET_PRIORITY_CODE)); - return usageContexts.stream() - // remove any useContexts which need to be replaced - .filter(useContext -> !useContextCodesToReplace.stream() - .anyMatch(code -> useContext.getCode().getCode().equals(code))) - .collect(Collectors.toList()); - } - - protected void tryAddCondition(List usageContexts, CodeableConcept condition) { - boolean focusAlreadyExists = usageContexts.stream() - .anyMatch(u -> u.getCode().getSystem().equals(KnowledgeArtifactAdapter.contextTypeUrl) - && u.getCode().getCode().equals(Constants.VALUE_SET_CONDITION_CODE) - && u.getValueCodeableConcept() - .hasCoding( - condition.getCoding().get(0).getSystem(), - condition.getCoding().get(0).getCode())); - if (!focusAlreadyExists) { - UsageContext newFocus = new UsageContext( - new Coding(KnowledgeArtifactAdapter.contextUrl, Constants.VALUE_SET_CONDITION_CODE, null), - condition); - newFocus.setValue(condition); - usageContexts.add(newFocus); - } - } /** * * Either finds a usageContext with the same system and code or creates an empty one diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactPackageVisitorTests.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactPackageVisitorTests.java index 0b30ddf2a..2ce224bb5 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactPackageVisitorTests.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactPackageVisitorTests.java @@ -36,6 +36,7 @@ import org.hl7.fhir.dstu3.model.UriType; import org.hl7.fhir.dstu3.model.ValueSet; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; @@ -98,6 +99,7 @@ void visitLibraryTest() { } @Test + @Disabled("This test needs a ValueSet that cannot be naively expanded") void packageOperation_should_fail_no_credentials() { Bundle loadedBundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); @@ -119,6 +121,7 @@ void packageOperation_should_fail_no_credentials() { } @Test + @Disabled("This test needs a ValueSet that cannot be naively expanded") void packageOperation_should_fail_credentials_missing_username() { Bundle loadedBundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); @@ -144,6 +147,7 @@ void packageOperation_should_fail_credentials_missing_username() { } @Test + @Disabled("This test needs a ValueSet that cannot be naively expanded") void packageOperation_should_fail_credentials_missing_apikey() { Bundle loadedBundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); @@ -169,6 +173,7 @@ void packageOperation_should_fail_credentials_missing_apikey() { } @Test + @Disabled("This test needs a ValueSet that cannot be naively expanded") void packageOperation_should_fail_credentials_invalid() { Bundle loadedBundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactPackageVisitorTests.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactPackageVisitorTests.java index efb13d6e6..d21ff0ac6 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactPackageVisitorTests.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactPackageVisitorTests.java @@ -36,6 +36,7 @@ import org.hl7.fhir.r4.model.StringType; import org.hl7.fhir.r4.model.ValueSet; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; @@ -98,6 +99,7 @@ void visitLibraryTest() { } @Test + @Disabled("This test needs a ValueSet that cannot be naively expanded") void packageOperation_should_fail_no_credentials() { Bundle loadedBundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); @@ -115,10 +117,11 @@ void packageOperation_should_fail_no_credentials() { } catch (UnprocessableEntityException e) { maybeException = e; } - assertTrue(maybeException.getMessage().contains("Cannot expand ValueSet without credentials: ")); + assertTrue(maybeException.getMessage().contains("Cannot expand ValueSet without a terminology server: ")); } @Test + @Disabled("This test needs a ValueSet that cannot be naively expanded") void packageOperation_should_fail_credentials_missing_username() { Bundle loadedBundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); @@ -144,6 +147,7 @@ void packageOperation_should_fail_credentials_missing_username() { } @Test + @Disabled("This test needs a ValueSet that cannot be naively expanded") void packageOperation_should_fail_credentials_missing_apikey() { Bundle loadedBundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); @@ -169,6 +173,7 @@ void packageOperation_should_fail_credentials_missing_apikey() { } @Test + @Disabled("This test needs a ValueSet that cannot be naively expanded") void packageOperation_should_fail_credentials_invalid() { Bundle loadedBundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactPackageVisitorTests.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactPackageVisitorTests.java index f8545d981..59dbe2de1 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactPackageVisitorTests.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactPackageVisitorTests.java @@ -36,6 +36,7 @@ import org.hl7.fhir.r5.model.StringType; import org.hl7.fhir.r5.model.ValueSet; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; @@ -98,6 +99,7 @@ void visitLibraryTest() { } @Test + @Disabled("This test needs a ValueSet that cannot be naively expanded") void packageOperation_should_fail_no_credentials() { Bundle loadedBundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); @@ -119,6 +121,7 @@ void packageOperation_should_fail_no_credentials() { } @Test + @Disabled("This test needs a ValueSet that cannot be naively expanded") void packageOperation_should_fail_credentials_missing_username() { Bundle loadedBundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); @@ -144,6 +147,7 @@ void packageOperation_should_fail_credentials_missing_username() { } @Test + @Disabled("This test needs a ValueSet that cannot be naively expanded") void packageOperation_should_fail_credentials_missing_apikey() { Bundle loadedBundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); @@ -169,6 +173,7 @@ void packageOperation_should_fail_credentials_missing_apikey() { } @Test + @Disabled("This test needs a ValueSet that cannot be naively expanded") void packageOperation_should_fail_credentials_invalid() { Bundle loadedBundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); From 9661b2d69670ee79a31d2fbb7bf0162792a6762d Mon Sep 17 00:00:00 2001 From: Brenin Rhodes Date: Wed, 5 Jun 2024 11:03:47 -0600 Subject: [PATCH 02/38] Add adapters for Questionnaire and StructureDefinition --- .../cqf/fhir/cr/common/IPackageProcessor.java | 5 + .../cqf/fhir/cr/common/PackageProcessor.java | 22 +- .../cqf/fhir/cr/library/LibraryProcessor.java | 23 +- .../PlanDefinitionProcessor.java | 23 +- .../questionnaire/QuestionnaireProcessor.java | 26 +- .../packages/PackageProcessor.java | 119 --------- .../cqf/fhir/cr/helpers/GeneratedPackage.java | 11 + .../PlanDefinitionProcessorTests.java | 12 +- .../QuestionnaireProcessorTests.java | 43 ++-- .../cr/questionnaire/TestQuestionnaire.java | 9 +- .../utility/adapter/dstu3/AdapterFactory.java | 21 +- .../adapter/dstu3/QuestionnaireAdapter.java | 64 +++++ .../dstu3/StructureDefinitionAdapter.java | 58 +++++ .../utility/adapter/r4/AdapterFactory.java | 21 +- .../adapter/r4/QuestionnaireAdapter.java | 64 +++++ .../r4/StructureDefinitionAdapter.java | 58 +++++ .../utility/adapter/r5/AdapterFactory.java | 24 +- .../adapter/r5/QuestionnaireAdapter.java | 64 +++++ .../r5/StructureDefinitionAdapter.java | 226 ++++++++++++++++++ .../KnowledgeArtifactPackageVisitor.java | 2 + 20 files changed, 698 insertions(+), 197 deletions(-) delete mode 100644 cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/questionnaire/packages/PackageProcessor.java create mode 100644 cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/QuestionnaireAdapter.java create mode 100644 cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/StructureDefinitionAdapter.java create mode 100644 cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/QuestionnaireAdapter.java create mode 100644 cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/StructureDefinitionAdapter.java create mode 100644 cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapter.java create mode 100644 cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapter.java diff --git a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/common/IPackageProcessor.java b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/common/IPackageProcessor.java index 215b05dd5..cd7330390 100644 --- a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/common/IPackageProcessor.java +++ b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/common/IPackageProcessor.java @@ -1,10 +1,15 @@ package org.opencds.cqf.fhir.cr.common; import org.hl7.fhir.instance.model.api.IBaseBundle; +import org.hl7.fhir.instance.model.api.IBaseParameters; import org.hl7.fhir.instance.model.api.IBaseResource; public interface IPackageProcessor { + @Deprecated IBaseBundle packageResource(IBaseResource resource); + @Deprecated IBaseBundle packageResource(IBaseResource resource, String method); + + IBaseBundle packageResource(IBaseResource resource, IBaseParameters parameters); } diff --git a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/common/PackageProcessor.java b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/common/PackageProcessor.java index b39150a72..ca4633160 100644 --- a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/common/PackageProcessor.java +++ b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/common/PackageProcessor.java @@ -6,6 +6,7 @@ import ca.uhn.fhir.context.FhirVersionEnum; import org.hl7.fhir.instance.model.api.IBase; import org.hl7.fhir.instance.model.api.IBaseBundle; +import org.hl7.fhir.instance.model.api.IBaseParameters; import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.instance.model.api.IDomainResource; import org.opencds.cqf.fhir.api.Repository; @@ -30,13 +31,20 @@ public IBaseBundle packageResource(IBaseResource resource) { @Override public IBaseBundle packageResource(IBaseResource resource, String method) { - var adapter = - AdapterFactory.forFhirVersion(fhirVersion).createKnowledgeArtifactAdapter((IDomainResource) resource); IBase[] parts = {}; - var parameters = newParameters( - repository.fhirContext(), - "package-parameters", - newBooleanPart(repository.fhirContext(), "isPut", false, parts)); - return (IBaseBundle) adapter.accept(packageVisitor, repository, parameters); + return packageResource( + resource, + newParameters( + repository.fhirContext(), + "package-parameters", + newBooleanPart(repository.fhirContext(), "isPut", method.equals("PUT"), parts))); + } + + @Override + public IBaseBundle packageResource(IBaseResource resource, IBaseParameters parameters) { + return (IBaseBundle) packageVisitor.visit( + AdapterFactory.forFhirVersion(fhirVersion).createKnowledgeArtifactAdapter((IDomainResource) resource), + repository, + parameters); } } diff --git a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/library/LibraryProcessor.java b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/library/LibraryProcessor.java index 16155a15f..074fa3d18 100644 --- a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/library/LibraryProcessor.java +++ b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/library/LibraryProcessor.java @@ -1,9 +1,13 @@ package org.opencds.cqf.fhir.cr.library; import static java.util.Objects.requireNonNull; +import static org.opencds.cqf.fhir.utility.Parameters.newBooleanPart; +import static org.opencds.cqf.fhir.utility.Parameters.newParameters; import ca.uhn.fhir.context.FhirVersionEnum; +import org.hl7.fhir.instance.model.api.IBase; import org.hl7.fhir.instance.model.api.IBaseBundle; +import org.hl7.fhir.instance.model.api.IBaseParameters; import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.instance.model.api.IIdType; import org.hl7.fhir.instance.model.api.IPrimitiveType; @@ -51,11 +55,26 @@ protected , R extends IBaseResource> R resolveL public , R extends IBaseResource> IBaseBundle packageLibrary( Either3 library) { - return packageProcessor.packageResource(resolveLibrary(library)); + return packageLibrary(library, false); } public , R extends IBaseResource> IBaseBundle packageLibrary( Either3 library, boolean isPut) { - return packageProcessor.packageResource(resolveLibrary(library), isPut ? "PUT" : "POST"); + IBase[] parts = {}; + return packageLibrary( + library, + newParameters( + repository.fhirContext(), + "package-parameters", + newBooleanPart(repository.fhirContext(), "isPut", isPut, parts))); + } + + public , R extends IBaseResource> IBaseBundle packageLibrary( + Either3 library, IBaseParameters parameters) { + return packageLibrary(resolveLibrary(library), parameters); + } + + public IBaseBundle packageLibrary(IBaseResource questionnaire, IBaseParameters parameters) { + return packageProcessor.packageResource(questionnaire, parameters); } } diff --git a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/plandefinition/PlanDefinitionProcessor.java b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/plandefinition/PlanDefinitionProcessor.java index caf2bf029..1d9a0ba96 100644 --- a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/plandefinition/PlanDefinitionProcessor.java +++ b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/plandefinition/PlanDefinitionProcessor.java @@ -1,10 +1,13 @@ package org.opencds.cqf.fhir.cr.plandefinition; import static java.util.Objects.requireNonNull; +import static org.opencds.cqf.fhir.utility.Parameters.newBooleanPart; +import static org.opencds.cqf.fhir.utility.Parameters.newParameters; import static org.opencds.cqf.fhir.utility.repository.Repositories.createRestRepository; import static org.opencds.cqf.fhir.utility.repository.Repositories.proxy; import ca.uhn.fhir.context.FhirVersionEnum; +import org.hl7.fhir.instance.model.api.IBase; import org.hl7.fhir.instance.model.api.IBaseBundle; import org.hl7.fhir.instance.model.api.IBaseDatatype; import org.hl7.fhir.instance.model.api.IBaseParameters; @@ -23,7 +26,6 @@ import org.opencds.cqf.fhir.cr.plandefinition.apply.ApplyProcessor; import org.opencds.cqf.fhir.cr.plandefinition.apply.ApplyRequest; import org.opencds.cqf.fhir.cr.plandefinition.apply.IApplyProcessor; -// import org.opencds.cqf.fhir.cr.plandefinition.packages.PackageProcessor; import org.opencds.cqf.fhir.utility.Ids; import org.opencds.cqf.fhir.utility.monad.Either3; @@ -83,12 +85,27 @@ protected , R extends IBaseResource> R resolveP public , R extends IBaseResource> IBaseBundle packagePlanDefinition( Either3 planDefinition) { - return packageProcessor.packageResource(resolvePlanDefinition(planDefinition)); + return packagePlanDefinition(planDefinition, false); } public , R extends IBaseResource> IBaseBundle packagePlanDefinition( Either3 planDefinition, boolean isPut) { - return packageProcessor.packageResource(resolvePlanDefinition(planDefinition), isPut ? "PUT" : "POST"); + IBase[] parts = {}; + return packagePlanDefinition( + planDefinition, + newParameters( + repository.fhirContext(), + "package-parameters", + newBooleanPart(repository.fhirContext(), "isPut", isPut, parts))); + } + + public , R extends IBaseResource> IBaseBundle packagePlanDefinition( + Either3 planDefinition, IBaseParameters parameters) { + return packagePlanDefinition(resolvePlanDefinition(planDefinition), parameters); + } + + public IBaseBundle packagePlanDefinition(IBaseResource planDefinition, IBaseParameters parameters) { + return packageProcessor.packageResource(planDefinition, parameters); } protected , R extends IBaseResource> ApplyRequest buildApplyRequest( diff --git a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/questionnaire/QuestionnaireProcessor.java b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/questionnaire/QuestionnaireProcessor.java index 7074c47ef..f1aa5036b 100644 --- a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/questionnaire/QuestionnaireProcessor.java +++ b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/questionnaire/QuestionnaireProcessor.java @@ -1,10 +1,13 @@ package org.opencds.cqf.fhir.cr.questionnaire; import static java.util.Objects.requireNonNull; +import static org.opencds.cqf.fhir.utility.Parameters.newBooleanPart; +import static org.opencds.cqf.fhir.utility.Parameters.newParameters; import static org.opencds.cqf.fhir.utility.repository.Repositories.createRestRepository; import static org.opencds.cqf.fhir.utility.repository.Repositories.proxy; import ca.uhn.fhir.context.FhirVersionEnum; +import org.hl7.fhir.instance.model.api.IBase; import org.hl7.fhir.instance.model.api.IBaseBundle; import org.hl7.fhir.instance.model.api.IBaseParameters; import org.hl7.fhir.instance.model.api.IBaseResource; @@ -16,11 +19,11 @@ import org.opencds.cqf.fhir.cql.LibraryEngine; import org.opencds.cqf.fhir.cql.engine.model.FhirModelResolverCache; import org.opencds.cqf.fhir.cr.common.IPackageProcessor; +import org.opencds.cqf.fhir.cr.common.PackageProcessor; import org.opencds.cqf.fhir.cr.common.ResourceResolver; import org.opencds.cqf.fhir.cr.questionnaire.generate.GenerateProcessor; import org.opencds.cqf.fhir.cr.questionnaire.generate.GenerateRequest; import org.opencds.cqf.fhir.cr.questionnaire.generate.IGenerateProcessor; -import org.opencds.cqf.fhir.cr.questionnaire.packages.PackageProcessor; import org.opencds.cqf.fhir.cr.questionnaire.populate.IPopulateProcessor; import org.opencds.cqf.fhir.cr.questionnaire.populate.PopulateProcessor; import org.opencds.cqf.fhir.cr.questionnaire.populate.PopulateRequest; @@ -167,20 +170,27 @@ public IBaseResource generateQuestionnaire(GenerateRequest request, String id) { public > IBaseBundle packageQuestionnaire( Either3 questionnaire) { - return packageQuestionnaire(resolveQuestionnaire(questionnaire)); + return packageQuestionnaire(questionnaire, false); } - public IBaseBundle packageQuestionnaire(IBaseResource questionnaire) { - return packageProcessor.packageResource(questionnaire); + public > IBaseBundle packageQuestionnaire( + Either3 questionnaire, boolean isPut) { + IBase[] parts = {}; + return packageQuestionnaire( + questionnaire, + newParameters( + repository.fhirContext(), + "package-parameters", + newBooleanPart(repository.fhirContext(), "isPut", isPut, parts))); } public > IBaseBundle packageQuestionnaire( - Either3 questionnaire, boolean isPut) { - return packageQuestionnaire(resolveQuestionnaire(questionnaire), isPut); + Either3 questionnaire, IBaseParameters parameters) { + return packageQuestionnaire(resolveQuestionnaire(questionnaire), parameters); } - public IBaseBundle packageQuestionnaire(IBaseResource questionnaire, boolean isPut) { - return packageProcessor.packageResource(questionnaire, isPut ? "PUT" : "POST"); + public IBaseBundle packageQuestionnaire(IBaseResource questionnaire, IBaseParameters parameters) { + return packageProcessor.packageResource(questionnaire, parameters); } public PopulateRequest buildPopulateRequest( diff --git a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/questionnaire/packages/PackageProcessor.java b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/questionnaire/packages/PackageProcessor.java deleted file mode 100644 index e7a199156..000000000 --- a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/questionnaire/packages/PackageProcessor.java +++ /dev/null @@ -1,119 +0,0 @@ -package org.opencds.cqf.fhir.cr.questionnaire.packages; - -import ca.uhn.fhir.context.FhirVersionEnum; -import org.hl7.fhir.instance.model.api.IBaseBundle; -import org.hl7.fhir.instance.model.api.IBaseResource; -import org.opencds.cqf.fhir.api.Repository; -import org.opencds.cqf.fhir.cr.common.IPackageProcessor; -import org.opencds.cqf.fhir.utility.BundleHelper; -import org.opencds.cqf.fhir.utility.Constants; - -public class PackageProcessor implements IPackageProcessor { - protected final Repository repository; - protected final FhirVersionEnum fhirVersion; - - public PackageProcessor(Repository repository) { - this.repository = repository; - this.fhirVersion = repository.fhirContext().getVersion().getVersion(); - } - - @Override - public IBaseBundle packageResource(IBaseResource resource) { - return packageResource(resource, "POST"); - } - - @Override - public IBaseBundle packageResource(IBaseResource resource, String method) { - return packageQuestionnaire(resource, method.equals("PUT")); - } - - protected IBaseBundle packageQuestionnaire(IBaseResource questionnaire, boolean isPut) { - switch (fhirVersion) { - case DSTU3: - return packageDstu3(questionnaire, isPut); - case R4: - return packageR4(questionnaire, isPut); - case R5: - return packageR5(questionnaire, isPut); - - default: - return null; - } - } - - protected IBaseBundle packageDstu3(IBaseResource questionnaire, Boolean isPut) { - var bundle = new org.hl7.fhir.dstu3.model.Bundle(); - bundle.setType(org.hl7.fhir.dstu3.model.Bundle.BundleType.TRANSACTION); - BundleHelper.addEntry( - bundle, - org.opencds.cqf.fhir.utility.dstu3.PackageHelper.createEntry( - (org.hl7.fhir.dstu3.model.Resource) questionnaire, isPut)); - var libraryExtension = - ((org.hl7.fhir.dstu3.model.Questionnaire) questionnaire).getExtensionByUrl(Constants.CQF_LIBRARY); - if (libraryExtension != null) { - var libraryCanonical = (org.hl7.fhir.dstu3.model.UriType) libraryExtension.getValue(); - var library = (org.hl7.fhir.dstu3.model.Library) - org.opencds.cqf.fhir.utility.dstu3.SearchHelper.searchRepositoryByCanonical( - repository, libraryCanonical); - if (library != null) { - BundleHelper.addEntry( - bundle, org.opencds.cqf.fhir.utility.dstu3.PackageHelper.createEntry(library, isPut)); - if (library.hasRelatedArtifact()) { - org.opencds.cqf.fhir.utility.dstu3.PackageHelper.addRelatedArtifacts( - bundle, library.getRelatedArtifact(), repository, isPut); - } - } - } - return bundle; - } - - protected IBaseBundle packageR4(IBaseResource questionnaire, Boolean isPut) { - var bundle = new org.hl7.fhir.r4.model.Bundle(); - bundle.setType(org.hl7.fhir.r4.model.Bundle.BundleType.TRANSACTION); - bundle.addEntry((org.hl7.fhir.r4.model.Bundle.BundleEntryComponent) - org.opencds.cqf.fhir.utility.PackageHelper.createEntry( - (org.hl7.fhir.r4.model.Resource) questionnaire, isPut)); - var libraryExtension = - ((org.hl7.fhir.r4.model.Questionnaire) questionnaire).getExtensionByUrl(Constants.CQF_LIBRARY); - if (libraryExtension != null) { - var libraryCanonical = (org.hl7.fhir.r4.model.CanonicalType) libraryExtension.getValue(); - var library = (org.hl7.fhir.r4.model.Library) - org.opencds.cqf.fhir.utility.r4.SearchHelper.searchRepositoryByCanonical( - repository, libraryCanonical); - if (library != null) { - bundle.addEntry((org.hl7.fhir.r4.model.Bundle.BundleEntryComponent) - org.opencds.cqf.fhir.utility.PackageHelper.createEntry(library, isPut)); - if (library.hasRelatedArtifact()) { - org.opencds.cqf.fhir.utility.r4.PackageHelper.addRelatedArtifacts( - bundle, library.getRelatedArtifact(), repository, isPut); - } - } - } - return bundle; - } - - protected IBaseBundle packageR5(IBaseResource questionnaire, Boolean isPut) { - var bundle = new org.hl7.fhir.r5.model.Bundle(); - bundle.setType(org.hl7.fhir.r5.model.Bundle.BundleType.TRANSACTION); - bundle.addEntry((org.hl7.fhir.r5.model.Bundle.BundleEntryComponent) - org.opencds.cqf.fhir.utility.PackageHelper.createEntry( - (org.hl7.fhir.r5.model.Resource) questionnaire, isPut)); - var libraryExtension = - ((org.hl7.fhir.r5.model.Questionnaire) questionnaire).getExtensionByUrl(Constants.CQF_LIBRARY); - if (libraryExtension != null) { - var libraryCanonical = (org.hl7.fhir.r5.model.CanonicalType) libraryExtension.getValue(); - var library = (org.hl7.fhir.r5.model.Library) - org.opencds.cqf.fhir.utility.r5.SearchHelper.searchRepositoryByCanonical( - repository, libraryCanonical); - if (library != null) { - bundle.addEntry((org.hl7.fhir.r5.model.Bundle.BundleEntryComponent) - org.opencds.cqf.fhir.utility.PackageHelper.createEntry(library, isPut)); - if (library.hasRelatedArtifact()) { - org.opencds.cqf.fhir.utility.r5.PackageHelper.addRelatedArtifacts( - bundle, library.getRelatedArtifact(), repository, isPut); - } - } - } - return bundle; - } -} diff --git a/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/helpers/GeneratedPackage.java b/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/helpers/GeneratedPackage.java index e3e41acf3..34b7df8ae 100644 --- a/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/helpers/GeneratedPackage.java +++ b/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/helpers/GeneratedPackage.java @@ -2,10 +2,12 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.opencds.cqf.fhir.utility.BundleHelper.getEntry; +import static org.opencds.cqf.fhir.utility.BundleHelper.getEntryResourceFirstRep; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.parser.IParser; import org.hl7.fhir.instance.model.api.IBaseBundle; +import org.hl7.fhir.instance.model.api.IBaseResource; public class GeneratedPackage { final IBaseBundle generatedBundle; @@ -20,4 +22,13 @@ public GeneratedPackage hasEntry(int count) { assertEquals(count, getEntry(generatedBundle).size()); return this; } + + public GeneratedPackage firstEntryIsType(Class resourceType) { + assertEquals(resourceType, getEntryResourceFirstRep(generatedBundle).getClass()); + return this; + } + + public IBaseBundle getBundle() { + return generatedBundle; + } } diff --git a/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/plandefinition/PlanDefinitionProcessorTests.java b/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/plandefinition/PlanDefinitionProcessorTests.java index 42ad68af5..72f0c2fc1 100644 --- a/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/plandefinition/PlanDefinitionProcessorTests.java +++ b/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/plandefinition/PlanDefinitionProcessorTests.java @@ -485,13 +485,11 @@ void packageR4() { @Test void packageR5() { - // Due to StructureDefinition no longer being a MetaDataResource in R5 it will need an adapter created before - // $package will work with StructureDefinitions. - // given().repositoryFor(fhirContextR5, "r5") - // .when() - // .planDefinitionId("generate-questionnaire") - // .thenPackage() - // .hasEntry(8); + given().repositoryFor(fhirContextR5, "r5") + .when() + .planDefinitionId("generate-questionnaire") + .thenPackage() + .hasEntry(9); given().repositoryFor(fhirContextR5, "r5") .when() diff --git a/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/questionnaire/QuestionnaireProcessorTests.java b/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/questionnaire/QuestionnaireProcessorTests.java index d3275da4d..00cf2df7f 100644 --- a/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/questionnaire/QuestionnaireProcessorTests.java +++ b/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/questionnaire/QuestionnaireProcessorTests.java @@ -1,7 +1,5 @@ package org.opencds.cqf.fhir.cr.questionnaire; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.opencds.cqf.fhir.cr.questionnaire.TestQuestionnaire.CLASS_PATH; @@ -15,8 +13,8 @@ import java.nio.file.Paths; import org.junit.jupiter.api.Test; import org.opencds.cqf.fhir.api.Repository; +import org.opencds.cqf.fhir.cr.common.PackageProcessor; import org.opencds.cqf.fhir.cr.questionnaire.generate.GenerateProcessor; -import org.opencds.cqf.fhir.cr.questionnaire.packages.PackageProcessor; import org.opencds.cqf.fhir.cr.questionnaire.populate.PopulateProcessor; import org.opencds.cqf.fhir.utility.Ids; import org.opencds.cqf.fhir.utility.repository.ig.IgRepository; @@ -174,35 +172,32 @@ void populateNoQuestionnaireThrowsException() { @Test void questionnairePackageDstu3() { - var bundle = (org.hl7.fhir.dstu3.model.Bundle) given().repository(repositoryDstu3) + given().repository(repositoryDstu3) .when() .questionnaireId(Ids.newId(fhirContextDstu3, "Questionnaire", "OutpatientPriorAuthorizationRequest")) - .thenPackage(); - - assertEquals(3, bundle.getEntry().size()); - assertEquals("Questionnaire", bundle.getEntry().get(0).getResource().fhirType()); + .thenPackage() + .hasEntry(3) + .firstEntryIsType(org.hl7.fhir.dstu3.model.Questionnaire.class); } @Test void questionnairePackageR4() { - var bundle = (org.hl7.fhir.r4.model.Bundle) given().repository(repositoryR4) + given().repository(repositoryR4) .when() .questionnaireId(Ids.newId(fhirContextR4, "Questionnaire", "OutpatientPriorAuthorizationRequest")) - .thenPackage(); - - assertEquals(3, bundle.getEntry().size()); - assertEquals("Questionnaire", bundle.getEntry().get(0).getResource().fhirType()); + .thenPackage() + .hasEntry(3) + .firstEntryIsType(org.hl7.fhir.r4.model.Questionnaire.class); } @Test void questionnairePackageR5() { - var bundle = (org.hl7.fhir.r5.model.Bundle) given().repository(repositoryR5) + given().repository(repositoryR5) .when() .questionnaireId(Ids.newId(fhirContextR5, "Questionnaire", "OutpatientPriorAuthorizationRequest")) - .thenPackage(); - - assertEquals(3, bundle.getEntry().size()); - assertEquals("Questionnaire", bundle.getEntry().get(0).getResource().fhirType()); + .thenPackage() + .hasEntry(3) + .firstEntryIsType(org.hl7.fhir.r5.model.Questionnaire.class); } @Test @@ -241,14 +236,13 @@ void pa_aslp_Populate() { @Test void pa_aslp_Package() { - var bundle = (org.hl7.fhir.r4.model.Bundle) given().repositoryFor(fhirContextR4, "r4/pa-aslp") + given().repositoryFor(fhirContextR4, "r4/pa-aslp") .when() .questionnaireId(Ids.newId(fhirContextR4, "Questionnaire", "ASLPA1")) .isPut(Boolean.TRUE) - .thenPackage(); - - assertFalse(bundle.getEntry().isEmpty()); - assertEquals(11, bundle.getEntry().size()); + .thenPackage() + .hasEntry(11) + .firstEntryIsType(org.hl7.fhir.r4.model.Questionnaire.class); } @Test @@ -260,7 +254,8 @@ void processors() { .when() .questionnaireId(Ids.newId(fhirContextR4, "Questionnaire", "OutpatientPriorAuthorizationRequest")) .isPut(Boolean.FALSE) - .thenPackage(); + .thenPackage() + .getBundle(); assertNotNull(bundle); } } diff --git a/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/questionnaire/TestQuestionnaire.java b/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/questionnaire/TestQuestionnaire.java index 349f89bc5..fbbfe8913 100644 --- a/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/questionnaire/TestQuestionnaire.java +++ b/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/questionnaire/TestQuestionnaire.java @@ -27,6 +27,7 @@ import org.opencds.cqf.fhir.cql.engine.retrieve.RetrieveSettings.TERMINOLOGY_FILTER_MODE; import org.opencds.cqf.fhir.cql.engine.terminology.TerminologySettings.VALUESET_EXPANSION_MODE; import org.opencds.cqf.fhir.cr.common.IPackageProcessor; +import org.opencds.cqf.fhir.cr.helpers.GeneratedPackage; import org.opencds.cqf.fhir.cr.questionnaire.generate.IGenerateProcessor; import org.opencds.cqf.fhir.cr.questionnaire.populate.IPopulateProcessor; import org.opencds.cqf.fhir.cr.questionnaire.populate.PopulateRequest; @@ -211,9 +212,13 @@ public GeneratedQuestionnaireResponse thenPopulate(Boolean buildRequest) { } } - public IBaseBundle thenPackage() { + public GeneratedPackage thenPackage() { var param = Eithers.for3(questionnaireUrl, questionnaireId, questionnaire); - return isPut == null ? processor.packageQuestionnaire(param) : processor.packageQuestionnaire(param, isPut); + return new GeneratedPackage( + isPut == null + ? processor.packageQuestionnaire(param) + : processor.packageQuestionnaire(param, isPut), + fhirContext()); } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/AdapterFactory.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/AdapterFactory.java index 5ca999c64..17924e898 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/AdapterFactory.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/AdapterFactory.java @@ -4,6 +4,8 @@ import org.hl7.fhir.dstu3.model.Library; import org.hl7.fhir.dstu3.model.MetadataResource; import org.hl7.fhir.dstu3.model.PlanDefinition; +import org.hl7.fhir.dstu3.model.Questionnaire; +import org.hl7.fhir.dstu3.model.StructureDefinition; import org.hl7.fhir.dstu3.model.ValueSet; import org.hl7.fhir.instance.model.api.IBaseBackboneElement; import org.hl7.fhir.instance.model.api.IBaseParameters; @@ -26,23 +28,28 @@ public org.opencds.cqf.fhir.utility.adapter.ResourceAdapter createResource(IBase @Override public org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter createKnowledgeArtifactAdapter( IDomainResource resource) { - org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter retval; + org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter adapter; if (resource instanceof Library) { - retval = createLibrary(resource); + adapter = createLibrary(resource); } else if (resource instanceof PlanDefinition) { - retval = new org.opencds.cqf.fhir.utility.adapter.dstu3.PlanDefinitionAdapter((PlanDefinition) resource); + adapter = new org.opencds.cqf.fhir.utility.adapter.dstu3.PlanDefinitionAdapter((PlanDefinition) resource); + } else if (resource instanceof Questionnaire) { + adapter = new org.opencds.cqf.fhir.utility.adapter.dstu3.QuestionnaireAdapter((Questionnaire) resource); + } else if (resource instanceof StructureDefinition) { + adapter = new org.opencds.cqf.fhir.utility.adapter.dstu3.StructureDefinitionAdapter( + (StructureDefinition) resource); } else if (resource instanceof ValueSet) { - retval = new org.opencds.cqf.fhir.utility.adapter.dstu3.ValueSetAdapter((ValueSet) resource); + adapter = new org.opencds.cqf.fhir.utility.adapter.dstu3.ValueSetAdapter((ValueSet) resource); } else { if (resource instanceof MetadataResource) { - retval = new org.opencds.cqf.fhir.utility.adapter.dstu3.KnowledgeArtifactAdapter( + adapter = new org.opencds.cqf.fhir.utility.adapter.dstu3.KnowledgeArtifactAdapter( (MetadataResource) resource); } else { throw new UnprocessableEntityException( - String.format("Resouce must be instance of %s", MetadataResource.class.getName())); + String.format("Resource must be instance of %s", MetadataResource.class.getName())); } } - return retval; + return adapter; } @Override diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/QuestionnaireAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/QuestionnaireAdapter.java new file mode 100644 index 000000000..51db90e7d --- /dev/null +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/QuestionnaireAdapter.java @@ -0,0 +1,64 @@ +package org.opencds.cqf.fhir.utility.adapter.dstu3; + +import java.util.ArrayList; +import java.util.List; +import org.hl7.fhir.dstu3.model.Questionnaire; +import org.hl7.fhir.dstu3.model.UriType; +import org.opencds.cqf.fhir.utility.Constants; +import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; +import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; + +public class QuestionnaireAdapter extends KnowledgeArtifactAdapter { + + private Questionnaire questionnaire; + + public QuestionnaireAdapter(Questionnaire questionnaire) { + super(questionnaire); + this.questionnaire = questionnaire; + } + + protected Questionnaire getQuestionnaire() { + return this.questionnaire; + } + + @Override + public List getDependencies() { + List references = new ArrayList<>(); + final String referenceSource = this.getQuestionnaire().hasVersion() + ? this.getQuestionnaire().getUrl() + "|" + + this.getQuestionnaire().getVersion() + : this.getQuestionnaire().getUrl(); + /* + derivedFrom + item[]..definition // NOTE: This is not a simple canonical, it will have a fragment to identify the specific element + item[]..answerValueSet + item[]..extension[itemMedia] + item[]..extension[itemAnswerMedia] + item[]..extension[unitValueSet] + item[]..extension[referenceProfile] + item[]..extension[candidateExpression].reference + item[]..extension[lookupQuestionnaire] + extension[cqf-library] + extension[launchContext] + extension[variable].reference + item[]..extension[variable].reference + item[]..extension[initialExpression].reference + item[]..extension[calculatedExpression].reference + item[]..extension[cqf-calculatedValue].reference + item[]..extension[cqf-expression].reference + item[]..extension[sdc-questionnaire-subQuestionnaire] + */ + + var libraryExtensions = questionnaire.getExtensionsByUrl(Constants.CQF_LIBRARY); + for (var libraryExt : libraryExtensions) { + DependencyInfo dependency = new DependencyInfo( + referenceSource, + ((UriType) libraryExt.getValue()).asStringValue(), + libraryExt.getExtension(), + (reference) -> libraryExt.setValue(new UriType(reference))); + references.add(dependency); + } + + return references; + } +} diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/StructureDefinitionAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/StructureDefinitionAdapter.java new file mode 100644 index 000000000..8bf2803e8 --- /dev/null +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/StructureDefinitionAdapter.java @@ -0,0 +1,58 @@ +package org.opencds.cqf.fhir.utility.adapter.dstu3; + +import java.util.ArrayList; +import java.util.List; +import org.hl7.fhir.dstu3.model.StructureDefinition; +import org.hl7.fhir.dstu3.model.UriType; +import org.opencds.cqf.fhir.utility.Constants; +import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; +import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; + +public class StructureDefinitionAdapter extends KnowledgeArtifactAdapter { + + private StructureDefinition structureDefinition; + + public StructureDefinitionAdapter(StructureDefinition structureDefinition) { + super(structureDefinition); + this.structureDefinition = structureDefinition; + } + + protected StructureDefinition getStructureDefinition() { + return this.structureDefinition; + } + + @Override + public List getDependencies() { + List references = new ArrayList<>(); + final String referenceSource = this.getStructureDefinition().hasVersion() + ? this.getStructureDefinition().getUrl() + "|" + + this.getStructureDefinition().getVersion() + : this.getStructureDefinition().getUrl(); + /* + extension[].url + modifierExtension[].url + baseDefinition + differential.element[].type.code + differential.element[].type.profile[] + differential.element[].type.targetProfile[] + differential.element[].binding.valueSet + differential.element[].extension[].url + differential.element[].modifierExtension[].url + extension[cpg-inferenceExpression].reference + extension[cpg-assertionExpression].reference + extension[cpg-featureExpression].reference + */ + + var libraryExtensions = structureDefinition.getExtensionsByUrl(Constants.CQF_LIBRARY); + for (var libraryExt : libraryExtensions) { + DependencyInfo dependency = new DependencyInfo( + referenceSource, + ((UriType) libraryExt.getValue()).asStringValue(), + libraryExt.getExtension(), + (reference) -> libraryExt.setValue(new UriType(reference))); + references.add(dependency); + } + + return references; + } +} diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/AdapterFactory.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/AdapterFactory.java index 892aa20f8..56c4983f5 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/AdapterFactory.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/AdapterFactory.java @@ -9,6 +9,8 @@ import org.hl7.fhir.r4.model.Library; import org.hl7.fhir.r4.model.MetadataResource; import org.hl7.fhir.r4.model.PlanDefinition; +import org.hl7.fhir.r4.model.Questionnaire; +import org.hl7.fhir.r4.model.StructureDefinition; import org.hl7.fhir.r4.model.ValueSet; import org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter; import org.opencds.cqf.fhir.utility.adapter.LibraryAdapter; @@ -26,23 +28,28 @@ public org.opencds.cqf.fhir.utility.adapter.ResourceAdapter createResource(IBase @Override public KnowledgeArtifactAdapter createKnowledgeArtifactAdapter(IDomainResource resource) { - KnowledgeArtifactAdapter retval; + KnowledgeArtifactAdapter adapter; if (resource instanceof Library) { - retval = createLibrary(resource); + adapter = createLibrary(resource); } else if (resource instanceof PlanDefinition) { - retval = new org.opencds.cqf.fhir.utility.adapter.r4.PlanDefinitionAdapter((PlanDefinition) resource); + adapter = new org.opencds.cqf.fhir.utility.adapter.r4.PlanDefinitionAdapter((PlanDefinition) resource); + } else if (resource instanceof Questionnaire) { + adapter = new org.opencds.cqf.fhir.utility.adapter.r4.QuestionnaireAdapter((Questionnaire) resource); + } else if (resource instanceof StructureDefinition) { + adapter = new org.opencds.cqf.fhir.utility.adapter.r4.StructureDefinitionAdapter( + (StructureDefinition) resource); } else if (resource instanceof ValueSet) { - retval = new org.opencds.cqf.fhir.utility.adapter.r4.ValueSetAdapter((ValueSet) resource); + adapter = new org.opencds.cqf.fhir.utility.adapter.r4.ValueSetAdapter((ValueSet) resource); } else { if (resource != null && resource instanceof MetadataResource) { - retval = new org.opencds.cqf.fhir.utility.adapter.r4.KnowledgeArtifactAdapter( + adapter = new org.opencds.cqf.fhir.utility.adapter.r4.KnowledgeArtifactAdapter( (MetadataResource) resource); } else { throw new UnprocessableEntityException( - String.format("Resouce must be instance of %s", MetadataResource.class.getName())); + String.format("Resource must be instance of %s", MetadataResource.class.getName())); } } - return retval; + return adapter; } @Override diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/QuestionnaireAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/QuestionnaireAdapter.java new file mode 100644 index 000000000..a18ebe301 --- /dev/null +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/QuestionnaireAdapter.java @@ -0,0 +1,64 @@ +package org.opencds.cqf.fhir.utility.adapter.r4; + +import java.util.ArrayList; +import java.util.List; +import org.hl7.fhir.r4.model.CanonicalType; +import org.hl7.fhir.r4.model.Questionnaire; +import org.opencds.cqf.fhir.utility.Constants; +import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; +import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; + +public class QuestionnaireAdapter extends KnowledgeArtifactAdapter { + + private Questionnaire questionnaire; + + public QuestionnaireAdapter(Questionnaire questionnaire) { + super(questionnaire); + this.questionnaire = questionnaire; + } + + protected Questionnaire getQuestionnaire() { + return this.questionnaire; + } + + @Override + public List getDependencies() { + List references = new ArrayList<>(); + final String referenceSource = this.getQuestionnaire().hasVersion() + ? this.getQuestionnaire().getUrl() + "|" + + this.getQuestionnaire().getVersion() + : this.getQuestionnaire().getUrl(); + /* + derivedFrom + item[]..definition // NOTE: This is not a simple canonical, it will have a fragment to identify the specific element + item[]..answerValueSet + item[]..extension[itemMedia] + item[]..extension[itemAnswerMedia] + item[]..extension[unitValueSet] + item[]..extension[referenceProfile] + item[]..extension[candidateExpression].reference + item[]..extension[lookupQuestionnaire] + extension[cqf-library] + extension[launchContext] + extension[variable].reference + item[]..extension[variable].reference + item[]..extension[initialExpression].reference + item[]..extension[calculatedExpression].reference + item[]..extension[cqf-calculatedValue].reference + item[]..extension[cqf-expression].reference + item[]..extension[sdc-questionnaire-subQuestionnaire] + */ + + var libraryExtensions = questionnaire.getExtensionsByUrl(Constants.CQF_LIBRARY); + for (var libraryExt : libraryExtensions) { + DependencyInfo dependency = new DependencyInfo( + referenceSource, + ((CanonicalType) libraryExt.getValue()).asStringValue(), + libraryExt.getExtension(), + (reference) -> libraryExt.setValue(new CanonicalType(reference))); + references.add(dependency); + } + + return references; + } +} diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/StructureDefinitionAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/StructureDefinitionAdapter.java new file mode 100644 index 000000000..5267e6c88 --- /dev/null +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/StructureDefinitionAdapter.java @@ -0,0 +1,58 @@ +package org.opencds.cqf.fhir.utility.adapter.r4; + +import java.util.ArrayList; +import java.util.List; +import org.hl7.fhir.r4.model.CanonicalType; +import org.hl7.fhir.r4.model.StructureDefinition; +import org.opencds.cqf.fhir.utility.Constants; +import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; +import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; + +public class StructureDefinitionAdapter extends KnowledgeArtifactAdapter { + + private StructureDefinition structureDefinition; + + public StructureDefinitionAdapter(StructureDefinition structureDefinition) { + super(structureDefinition); + this.structureDefinition = structureDefinition; + } + + protected StructureDefinition getStructureDefinition() { + return this.structureDefinition; + } + + @Override + public List getDependencies() { + List references = new ArrayList<>(); + final String referenceSource = this.getStructureDefinition().hasVersion() + ? this.getStructureDefinition().getUrl() + "|" + + this.getStructureDefinition().getVersion() + : this.getStructureDefinition().getUrl(); + /* + extension[].url + modifierExtension[].url + baseDefinition + differential.element[].type.code + differential.element[].type.profile[] + differential.element[].type.targetProfile[] + differential.element[].binding.valueSet + differential.element[].extension[].url + differential.element[].modifierExtension[].url + extension[cpg-inferenceExpression].reference + extension[cpg-assertionExpression].reference + extension[cpg-featureExpression].reference + */ + + var libraryExtensions = structureDefinition.getExtensionsByUrl(Constants.CQF_LIBRARY); + for (var libraryExt : libraryExtensions) { + DependencyInfo dependency = new DependencyInfo( + referenceSource, + ((CanonicalType) libraryExt.getValue()).asStringValue(), + libraryExt.getExtension(), + (reference) -> libraryExt.setValue(new CanonicalType(reference))); + references.add(dependency); + } + + return references; + } +} diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/AdapterFactory.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/AdapterFactory.java index 16421aacf..1fb8ed51e 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/AdapterFactory.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/AdapterFactory.java @@ -1,16 +1,16 @@ package org.opencds.cqf.fhir.utility.adapter.r5; import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; -import org.apache.commons.lang3.NotImplementedException; import org.hl7.fhir.instance.model.api.IBaseBackboneElement; import org.hl7.fhir.instance.model.api.IBaseParameters; import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.instance.model.api.ICompositeType; import org.hl7.fhir.instance.model.api.IDomainResource; -import org.hl7.fhir.r5.model.CanonicalResource; import org.hl7.fhir.r5.model.Library; import org.hl7.fhir.r5.model.MetadataResource; import org.hl7.fhir.r5.model.PlanDefinition; +import org.hl7.fhir.r5.model.Questionnaire; +import org.hl7.fhir.r5.model.StructureDefinition; import org.hl7.fhir.r5.model.ValueSet; import org.opencds.cqf.fhir.utility.adapter.LibraryAdapter; @@ -28,26 +28,28 @@ public org.opencds.cqf.fhir.utility.adapter.ResourceAdapter createResource(IBase @Override public org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter createKnowledgeArtifactAdapter( IDomainResource resource) { - org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter retval; + org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter adapter; if (resource instanceof Library) { - retval = createLibrary(resource); + adapter = createLibrary(resource); } else if (resource instanceof PlanDefinition) { - retval = new org.opencds.cqf.fhir.utility.adapter.r5.PlanDefinitionAdapter((PlanDefinition) resource); + adapter = new org.opencds.cqf.fhir.utility.adapter.r5.PlanDefinitionAdapter((PlanDefinition) resource); + } else if (resource instanceof Questionnaire) { + adapter = new org.opencds.cqf.fhir.utility.adapter.r5.QuestionnaireAdapter((Questionnaire) resource); + } else if (resource instanceof StructureDefinition) { + adapter = new org.opencds.cqf.fhir.utility.adapter.r5.StructureDefinitionAdapter( + (StructureDefinition) resource); } else if (resource instanceof ValueSet) { - retval = new org.opencds.cqf.fhir.utility.adapter.r5.ValueSetAdapter((ValueSet) resource); + adapter = new org.opencds.cqf.fhir.utility.adapter.r5.ValueSetAdapter((ValueSet) resource); } else { if (resource instanceof MetadataResource) { - retval = new org.opencds.cqf.fhir.utility.adapter.r5.KnowledgeArtifactAdapter( + adapter = new org.opencds.cqf.fhir.utility.adapter.r5.KnowledgeArtifactAdapter( (MetadataResource) resource); - } else if (resource instanceof CanonicalResource) { - // TODO: StructureDefinition is a CanonicalResource in R5. Other resources may be as well? - throw new NotImplementedException("CanonicalResource instances are not currently supported."); } else { throw new UnprocessableEntityException( String.format("Resource must be instance of %s", MetadataResource.class.getName())); } } - return retval; + return adapter; } @Override diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapter.java new file mode 100644 index 000000000..9403bc95f --- /dev/null +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapter.java @@ -0,0 +1,64 @@ +package org.opencds.cqf.fhir.utility.adapter.r5; + +import java.util.ArrayList; +import java.util.List; +import org.hl7.fhir.r5.model.CanonicalType; +import org.hl7.fhir.r5.model.Questionnaire; +import org.opencds.cqf.fhir.utility.Constants; +import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; +import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; + +public class QuestionnaireAdapter extends KnowledgeArtifactAdapter { + + private Questionnaire questionnaire; + + public QuestionnaireAdapter(Questionnaire questionnaire) { + super(questionnaire); + this.questionnaire = questionnaire; + } + + protected Questionnaire getQuestionnaire() { + return this.questionnaire; + } + + @Override + public List getDependencies() { + List references = new ArrayList<>(); + final String referenceSource = this.getQuestionnaire().hasVersion() + ? this.getQuestionnaire().getUrl() + "|" + + this.getQuestionnaire().getVersion() + : this.getQuestionnaire().getUrl(); + /* + derivedFrom + item[]..definition // NOTE: This is not a simple canonical, it will have a fragment to identify the specific element + item[]..answerValueSet + item[]..extension[itemMedia] + item[]..extension[itemAnswerMedia] + item[]..extension[unitValueSet] + item[]..extension[referenceProfile] + item[]..extension[candidateExpression].reference + item[]..extension[lookupQuestionnaire] + extension[cqf-library] + extension[launchContext] + extension[variable].reference + item[]..extension[variable].reference + item[]..extension[initialExpression].reference + item[]..extension[calculatedExpression].reference + item[]..extension[cqf-calculatedValue].reference + item[]..extension[cqf-expression].reference + item[]..extension[sdc-questionnaire-subQuestionnaire] + */ + + var libraryExtensions = questionnaire.getExtensionsByUrl(Constants.CQF_LIBRARY); + for (var libraryExt : libraryExtensions) { + DependencyInfo dependency = new DependencyInfo( + referenceSource, + ((CanonicalType) libraryExt.getValue()).asStringValue(), + libraryExt.getExtension(), + (reference) -> libraryExt.setValue(new CanonicalType(reference))); + references.add(dependency); + } + + return references; + } +} diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapter.java new file mode 100644 index 000000000..f47a5e59b --- /dev/null +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapter.java @@ -0,0 +1,226 @@ +package org.opencds.cqf.fhir.utility.adapter.r5; + +import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.stream.Collectors; +import org.hl7.fhir.exceptions.FHIRException; +import org.hl7.fhir.instance.model.api.IBaseExtension; +import org.hl7.fhir.instance.model.api.IBaseHasExtensions; +import org.hl7.fhir.instance.model.api.ICompositeType; +import org.hl7.fhir.instance.model.api.IPrimitiveType; +import org.hl7.fhir.r5.model.CanonicalType; +import org.hl7.fhir.r5.model.DateTimeType; +import org.hl7.fhir.r5.model.Enumerations.PublicationStatus; +import org.hl7.fhir.r5.model.Extension; +import org.hl7.fhir.r5.model.Period; +import org.hl7.fhir.r5.model.RelatedArtifact; +import org.hl7.fhir.r5.model.RelatedArtifact.RelatedArtifactType; +import org.hl7.fhir.r5.model.StructureDefinition; +import org.opencds.cqf.fhir.utility.Constants; +import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; +import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; +import org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter; + +public class StructureDefinitionAdapter extends ResourceAdapter implements KnowledgeArtifactAdapter { + + private StructureDefinition structureDefinition; + + public StructureDefinitionAdapter(StructureDefinition structureDefinition) { + super(structureDefinition); + this.structureDefinition = structureDefinition; + } + + protected StructureDefinition getStructureDefinition() { + return this.structureDefinition; + } + + @Override + public List getDependencies() { + List references = new ArrayList<>(); + final String referenceSource = this.getStructureDefinition().hasVersion() + ? this.getStructureDefinition().getUrl() + "|" + + this.getStructureDefinition().getVersion() + : this.getStructureDefinition().getUrl(); + /* + extension[].url + modifierExtension[].url + baseDefinition + differential.element[].type.code + differential.element[].type.profile[] + differential.element[].type.targetProfile[] + differential.element[].binding.valueSet + differential.element[].extension[].url + differential.element[].modifierExtension[].url + extension[cpg-inferenceExpression].reference + extension[cpg-assertionExpression].reference + extension[cpg-featureExpression].reference + */ + + var libraryExtensions = structureDefinition.getExtensionsByUrl(Constants.CQF_LIBRARY); + for (var libraryExt : libraryExtensions) { + DependencyInfo dependency = new DependencyInfo( + referenceSource, + ((CanonicalType) libraryExt.getValue()).asStringValue(), + libraryExt.getExtension(), + (reference) -> libraryExt.setValue(new CanonicalType(reference))); + references.add(dependency); + } + + return references; + } + + @Override + public StructureDefinition get() { + return this.structureDefinition; + } + + @Override + public StructureDefinition copy() { + return this.get().copy(); + } + + @Override + public String getUrl() { + return this.get().getUrl(); + } + + @Override + public boolean hasUrl() { + return this.get().hasUrl(); + } + + @Override + public void setUrl(String url) { + this.get().setUrl(url); + } + + @Override + public void setVersion(String version) { + this.get().setVersion(version); + } + + @Override + public String getVersion() { + return this.get().getVersion(); + } + + @Override + public boolean hasVersion() { + return this.get().hasVersion(); + } + + @Override + public String getName() { + return this.get().getName(); + } + + @Override + public void setName(String name) { + this.get().setName(name); + } + + @Override + public Date getApprovalDate() { + return null; + } + + @Override + public Date getDate() { + return this.get().getDate(); + } + + @Override + public void setDate(Date approvalDate) { + this.get().setDate(approvalDate); + } + + @Override + public void setDateElement(IPrimitiveType date) { + if (date != null && !(date instanceof DateTimeType)) { + throw new UnprocessableEntityException("Date must be " + DateTimeType.class.getName()); + } + this.get().setDateElement((DateTimeType) date); + } + + @Override + public void setApprovalDate(Date approvalDate) { + // do nothing + } + + @Override + public Period getEffectivePeriod() { + return null; + } + + @Override + public String getPurpose() { + return this.get().getPurpose(); + } + + @Override + public void setEffectivePeriod(ICompositeType effectivePeriod) { + if (effectivePeriod != null && !(effectivePeriod instanceof Period)) { + throw new UnprocessableEntityException("EffectivePeriod must be a valid " + Period.class.getName()); + } + // do nothing + } + + @Override + public boolean hasRelatedArtifact() { + return false; + } + + @SuppressWarnings("unchecked") + @Override + public List getRelatedArtifact() { + return new ArrayList<>(); + } + + @SuppressWarnings("unchecked") + @Override + public List getRelatedArtifactsOfType(String codeString) { + RelatedArtifactType type; + try { + type = RelatedArtifactType.fromCode(codeString); + } catch (FHIRException e) { + throw new UnprocessableEntityException("Invalid related artifact code"); + } + return this.getRelatedArtifact().stream() + .filter(ra -> ra.getType() == type) + .collect(Collectors.toList()); + } + + @Override + public void setRelatedArtifact(List relatedArtifacts) + throws UnprocessableEntityException { + // do nothing + } + + @Override + public void setStatus(String statusCodeString) { + PublicationStatus status; + try { + status = PublicationStatus.fromCode(statusCodeString); + } catch (FHIRException e) { + throw new UnprocessableEntityException("Invalid status code"); + } + this.get().setStatus(status); + } + + @Override + public String getStatus() { + return this.get().getStatus() == null ? null : this.get().getStatus().toCode(); + } + + @Override + public boolean getExperimental() { + return this.get().getExperimental(); + } + + @Override + public void setExtension(List> extensions) { + this.get().setExtension(extensions.stream().map(e -> (Extension) e).collect(Collectors.toList())); + } +} diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/KnowledgeArtifactPackageVisitor.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/KnowledgeArtifactPackageVisitor.java index f85bac3c0..99d7327b1 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/KnowledgeArtifactPackageVisitor.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/KnowledgeArtifactPackageVisitor.java @@ -70,6 +70,8 @@ public IBase visit(KnowledgeArtifactAdapter adapter, Repository repository, IBas "forceArtifactVersion", packageParameters, IPrimitiveType.class) .map(l -> l.stream().map(t -> (String) t.getValue()).collect(Collectors.toList())) .orElseGet(() -> new ArrayList<>()); + Optional isPut = VisitorHelper.getParameter("isPut", packageParameters, IBaseBooleanDatatype.class) + .map(r -> r.getValue()); if ((artifactRoute.isPresent() && !StringUtils.isBlank(artifactRoute.get()) From 4e9cb635b9ffaaae71abd5251781c342fcbdd619 Mon Sep 17 00:00:00 2001 From: Brenin Rhodes Date: Wed, 5 Jun 2024 15:14:15 -0600 Subject: [PATCH 03/38] Shortening some class names --- .../cqf/fhir/cr/common/PackageProcessor.java | 6 +- ...pproveVisitor.java => ApproveVisitor.java} | 23 ++- .../visitor/DataRequirementsVisitor.java | 137 +++++++++++++++ ...actDraftVisitor.java => DraftVisitor.java} | 62 +++---- ...ackageVisitor.java => PackageVisitor.java} | 164 +++++++++--------- ...eleaseVisitor.java => ReleaseVisitor.java} | 42 ++--- .../fhir/utility/visitor/VisitorHelper.java | 61 +++++++ ...pproveVisitor.java => ApproveVisitor.java} | 2 +- ...actDraftVisitor.java => DraftVisitor.java} | 2 +- ...ackageVisitor.java => PackageVisitor.java} | 4 +- ...eleaseVisitor.java => ReleaseVisitor.java} | 2 +- ...pproveVisitor.java => ApproveVisitor.java} | 2 +- ...actDraftVisitor.java => DraftVisitor.java} | 2 +- ...ackageVisitor.java => PackageVisitor.java} | 8 +- ...eleaseVisitor.java => ReleaseVisitor.java} | 2 +- ...pproveVisitor.java => ApproveVisitor.java} | 2 +- ...actDraftVisitor.java => DraftVisitor.java} | 2 +- ...ackageVisitor.java => PackageVisitor.java} | 6 +- ...eleaseVisitor.java => ReleaseVisitor.java} | 2 +- .../dstu3/PlanDefinitionAdapterTest.java | 4 +- .../adapter/r4/PlanDefinitionAdapterTest.java | 4 +- .../adapter/r5/PlanDefinitionAdapterTest.java | 4 +- .../KnowledgeArtifactApproveVisitorTest.java | 8 +- .../KnowledgeArtifactDraftVisitorTests.java | 12 +- .../KnowledgeArtifactPackageVisitorTests.java | 24 +-- .../KnowledgeArtifactReleaseVisitorTests.java | 24 +-- .../KnowledgeArtifactApproveVisitorTest.java | 8 +- .../KnowledgeArtifactDraftVisitorTests.java | 12 +- .../KnowledgeArtifactPackageVisitorTests.java | 24 +-- .../KnowledgeArtifactReleaseVisitorTests.java | 26 +-- .../KnowledgeArtifactApproveVisitorTest.java | 8 +- .../KnowledgeArtifactDraftVisitorTests.java | 12 +- .../KnowledgeArtifactPackageVisitorTests.java | 24 +-- .../KnowledgeArtifactReleaseVisitorTests.java | 24 +-- 34 files changed, 469 insertions(+), 280 deletions(-) rename cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/{KnowledgeArtifactApproveVisitor.java => ApproveVisitor.java} (86%) create mode 100644 cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/DataRequirementsVisitor.java rename cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/{KnowledgeArtifactDraftVisitor.java => DraftVisitor.java} (83%) rename cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/{KnowledgeArtifactPackageVisitor.java => PackageVisitor.java} (73%) rename cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/{KnowledgeArtifactReleaseVisitor.java => ReleaseVisitor.java} (93%) rename cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/dstu3/{KnowledgeArtifactApproveVisitor.java => ApproveVisitor.java} (97%) rename cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/dstu3/{KnowledgeArtifactDraftVisitor.java => DraftVisitor.java} (98%) rename cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/dstu3/{KnowledgeArtifactPackageVisitor.java => PackageVisitor.java} (99%) rename cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/dstu3/{KnowledgeArtifactReleaseVisitor.java => ReleaseVisitor.java} (99%) rename cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r4/{KnowledgeArtifactApproveVisitor.java => ApproveVisitor.java} (97%) rename cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r4/{KnowledgeArtifactDraftVisitor.java => DraftVisitor.java} (98%) rename cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r4/{KnowledgeArtifactPackageVisitor.java => PackageVisitor.java} (99%) rename cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r4/{KnowledgeArtifactReleaseVisitor.java => ReleaseVisitor.java} (99%) rename cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r5/{KnowledgeArtifactApproveVisitor.java => ApproveVisitor.java} (98%) rename cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r5/{KnowledgeArtifactDraftVisitor.java => DraftVisitor.java} (98%) rename cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r5/{KnowledgeArtifactPackageVisitor.java => PackageVisitor.java} (99%) rename cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r5/{KnowledgeArtifactReleaseVisitor.java => ReleaseVisitor.java} (99%) diff --git a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/common/PackageProcessor.java b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/common/PackageProcessor.java index ca4633160..7f7a5b0c1 100644 --- a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/common/PackageProcessor.java +++ b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/common/PackageProcessor.java @@ -11,17 +11,17 @@ import org.hl7.fhir.instance.model.api.IDomainResource; import org.opencds.cqf.fhir.api.Repository; import org.opencds.cqf.fhir.utility.adapter.AdapterFactory; -import org.opencds.cqf.fhir.utility.visitor.KnowledgeArtifactPackageVisitor; +import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; public class PackageProcessor implements IPackageProcessor { protected final Repository repository; protected final FhirVersionEnum fhirVersion; - protected final KnowledgeArtifactPackageVisitor packageVisitor; + protected final PackageVisitor packageVisitor; public PackageProcessor(Repository repository) { this.repository = repository; this.fhirVersion = repository.fhirContext().getVersion().getVersion(); - packageVisitor = new KnowledgeArtifactPackageVisitor(); + packageVisitor = new PackageVisitor(); } @Override diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/KnowledgeArtifactApproveVisitor.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/ApproveVisitor.java similarity index 86% rename from cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/KnowledgeArtifactApproveVisitor.java rename to cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/ApproveVisitor.java index 61fe179fc..1851ba156 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/KnowledgeArtifactApproveVisitor.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/ApproveVisitor.java @@ -17,7 +17,7 @@ import org.opencds.cqf.fhir.utility.PackageHelper; import org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter; -public class KnowledgeArtifactApproveVisitor implements KnowledgeArtifactVisitor { +public class ApproveVisitor implements KnowledgeArtifactVisitor { @Override public IBase visit(KnowledgeArtifactAdapter adapter, Repository repository, IBaseParameters approveParameters) { Date currentDate = new Date(); @@ -82,17 +82,16 @@ private IBaseResource createApprovalAssessment( throws UnprocessableEntityException { switch (fhirVersion) { case DSTU3: - return org.opencds.cqf.fhir.utility.visitor.dstu3.KnowledgeArtifactApproveVisitor - .createApprovalAssessment( - id, - new org.hl7.fhir.dstu3.model.CodeType(artifactAssessmentType), - artifactAssessmentSummary.map(t -> new org.hl7.fhir.dstu3.model.MarkdownType(t)), - artifactAssessmentTargetCanonical.map(t -> new org.hl7.fhir.dstu3.model.UriType(t)), - artifactAssessmentRelatedArtifact.map(t -> new org.hl7.fhir.dstu3.model.UriType(t)), - artifactAssessmentAuthor.map(t -> (org.hl7.fhir.dstu3.model.Reference) t), - new org.hl7.fhir.dstu3.model.Reference(artifactTargetReference)); + return org.opencds.cqf.fhir.utility.visitor.dstu3.ApproveVisitor.createApprovalAssessment( + id, + new org.hl7.fhir.dstu3.model.CodeType(artifactAssessmentType), + artifactAssessmentSummary.map(t -> new org.hl7.fhir.dstu3.model.MarkdownType(t)), + artifactAssessmentTargetCanonical.map(t -> new org.hl7.fhir.dstu3.model.UriType(t)), + artifactAssessmentRelatedArtifact.map(t -> new org.hl7.fhir.dstu3.model.UriType(t)), + artifactAssessmentAuthor.map(t -> (org.hl7.fhir.dstu3.model.Reference) t), + new org.hl7.fhir.dstu3.model.Reference(artifactTargetReference)); case R4: - return org.opencds.cqf.fhir.utility.visitor.r4.KnowledgeArtifactApproveVisitor.createApprovalAssessment( + return org.opencds.cqf.fhir.utility.visitor.r4.ApproveVisitor.createApprovalAssessment( id, new org.hl7.fhir.r4.model.CodeType(artifactAssessmentType), artifactAssessmentSummary.map(t -> new org.hl7.fhir.r4.model.MarkdownType(t)), @@ -101,7 +100,7 @@ private IBaseResource createApprovalAssessment( artifactAssessmentAuthor.map(t -> (org.hl7.fhir.r4.model.Reference) t), new org.hl7.fhir.r4.model.Reference(artifactTargetReference)); case R5: - return org.opencds.cqf.fhir.utility.visitor.r5.KnowledgeArtifactApproveVisitor.createApprovalAssessment( + return org.opencds.cqf.fhir.utility.visitor.r5.ApproveVisitor.createApprovalAssessment( id, artifactAssessmentType, artifactAssessmentSummary.map(t -> new org.hl7.fhir.r5.model.MarkdownType(t)), diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/DataRequirementsVisitor.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/DataRequirementsVisitor.java new file mode 100644 index 000000000..18a69d1bc --- /dev/null +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/DataRequirementsVisitor.java @@ -0,0 +1,137 @@ +package org.opencds.cqf.fhir.utility.visitor; + +import static org.opencds.cqf.fhir.utility.visitor.VisitorHelper.findUnsupportedCapability; +import static org.opencds.cqf.fhir.utility.visitor.VisitorHelper.processCanonicals; + +import ca.uhn.fhir.parser.DataFormatException; +import ca.uhn.fhir.rest.server.exceptions.NotImplementedOperationException; +import ca.uhn.fhir.rest.server.exceptions.PreconditionFailedException; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; +import org.apache.commons.lang3.StringUtils; +import org.hl7.fhir.instance.model.api.IBase; +import org.hl7.fhir.instance.model.api.IBaseParameters; +import org.hl7.fhir.instance.model.api.IBaseResource; +import org.hl7.fhir.instance.model.api.ICompositeType; +import org.hl7.fhir.instance.model.api.IDomainResource; +import org.hl7.fhir.instance.model.api.IPrimitiveType; +import org.opencds.cqf.fhir.api.Repository; +import org.opencds.cqf.fhir.utility.BundleHelper; +import org.opencds.cqf.fhir.utility.Canonicals; +import org.opencds.cqf.fhir.utility.Resources; +import org.opencds.cqf.fhir.utility.SearchHelper; +import org.opencds.cqf.fhir.utility.adapter.AdapterFactory; +import org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter; +import org.opencds.cqf.fhir.utility.adapter.LibraryAdapter; + +public class DataRequirementsVisitor implements KnowledgeArtifactVisitor { + + @Override + public IBase visit(KnowledgeArtifactAdapter adapter, Repository repository, IBaseParameters parameters) { + var fhirVersion = adapter.get().getStructureFhirVersionEnum(); + Optional artifactRoute = VisitorHelper.getParameter("artifactRoute", parameters, IPrimitiveType.class) + .map(r -> (String) r.getValue()); + Optional endpointUri = VisitorHelper.getParameter("endpointUri", parameters, IPrimitiveType.class) + .map(r -> (String) r.getValue()); + Optional endpoint = + VisitorHelper.getResourceParameter("endpoint", parameters, IBaseResource.class); + Optional terminologyEndpoint = + VisitorHelper.getResourceParameter("terminologyEndpoint", parameters, IBaseResource.class); + List artifactVersion = VisitorHelper.getListParameter( + "artifactVersion", parameters, IPrimitiveType.class) + .map(l -> l.stream().map(t -> (String) t.getValue()).collect(Collectors.toList())) + .orElseGet(() -> new ArrayList<>()); + List checkArtifactVersion = VisitorHelper.getListParameter( + "checkArtifactVersion", parameters, IPrimitiveType.class) + .map(l -> l.stream().map(t -> (String) t.getValue()).collect(Collectors.toList())) + .orElseGet(() -> new ArrayList<>()); + List forceArtifactVersion = VisitorHelper.getListParameter( + "forceArtifactVersion", parameters, IPrimitiveType.class) + .map(l -> l.stream().map(t -> (String) t.getValue()).collect(Collectors.toList())) + .orElseGet(() -> new ArrayList<>()); + + if ((artifactRoute.isPresent() + && !StringUtils.isBlank(artifactRoute.get()) + && !artifactRoute.get().isEmpty()) + || (endpointUri.isPresent() + && !StringUtils.isBlank(endpointUri.get()) + && !endpointUri.get().isEmpty()) + || endpoint.isPresent()) { + throw new NotImplementedOperationException( + "This repository is not implementing custom Content and endpoints at this time"); + } + + var resource = adapter.get(); + var library = Resources.newResource( + repository.fhirContext().getResourceDefinition("Library").getImplementingClass()); + var libraryAdapter = AdapterFactory.forFhirVersion(fhirVersion).createLibrary(library); + libraryAdapter.setType("module-definition"); + recursiveGather( + resource, + libraryAdapter, + repository, + forceArtifactVersion, + forceArtifactVersion, + artifactVersion, + checkArtifactVersion, + forceArtifactVersion); + return libraryAdapter.get(); + } + + protected void recursiveGather( + IDomainResource resource, + LibraryAdapter libraryAdapter, + Repository repository, + List capability, + List include, + List artifactVersion, + List checkArtifactVersion, + List forceArtifactVersion) + throws PreconditionFailedException { + if (resource != null) { + var fhirVersion = resource.getStructureFhirVersionEnum(); + var adapter = AdapterFactory.forFhirVersion(fhirVersion).createKnowledgeArtifactAdapter(resource); + findUnsupportedCapability(adapter, capability); + processCanonicals(adapter, artifactVersion, checkArtifactVersion, forceArtifactVersion); + + // TODO: Handle duplicates + gatherRequirements(adapter).forEach(dr -> libraryAdapter.addDataRequirement(dr)); + + adapter.combineComponentsAndDependencies().stream() + // sometimes VS dependencies aren't FHIR resources + .filter(ra -> !StringUtils.isBlank(Canonicals.getResourceType(ra.getReference()))) + .filter(ra -> { + try { + var resourceDef = repository + .fhirContext() + .getResourceDefinition(Canonicals.getResourceType(ra.getReference())); + return resourceDef != null; + } catch (DataFormatException e) { + if (e.getMessage().contains("1684")) { + return false; + } else { + throw new DataFormatException(e.getMessage()); + } + } + }) + .map(ra -> SearchHelper.searchRepositoryByCanonicalWithPaging(repository, ra.getReference())) + .map(searchBundle -> (IDomainResource) BundleHelper.getEntryResourceFirstRep(searchBundle)) + .forEach(component -> recursiveGather( + component, + libraryAdapter, + repository, + capability, + include, + artifactVersion, + checkArtifactVersion, + forceArtifactVersion)); + } + } + + protected List gatherRequirements(KnowledgeArtifactAdapter adapter) { + // TODO: Refactor R4MeasureDataRequirementsService + return new ArrayList<>(); + } +} diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/KnowledgeArtifactDraftVisitor.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/DraftVisitor.java similarity index 83% rename from cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/KnowledgeArtifactDraftVisitor.java rename to cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/DraftVisitor.java index cefe8d3c7..b3449dacd 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/KnowledgeArtifactDraftVisitor.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/DraftVisitor.java @@ -26,7 +26,7 @@ import org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter; import org.opencds.cqf.fhir.utility.r4.PackageHelper; -public class KnowledgeArtifactDraftVisitor implements KnowledgeArtifactVisitor { +public class DraftVisitor implements KnowledgeArtifactVisitor { @Override public IBase visit(KnowledgeArtifactAdapter adapter, Repository repository, IBaseParameters draftParameters) { var fhirVersion = adapter.get().getStructureFhirVersionEnum(); @@ -157,22 +157,19 @@ private void processReferencedResourceForDraft( switch (fhirVersion) { case DSTU3: referencedResource = - org.opencds.cqf.fhir.utility.visitor.dstu3.KnowledgeArtifactDraftVisitor - .processReferencedResourceForDraft( + org.opencds.cqf.fhir.utility.visitor.dstu3.DraftVisitor.processReferencedResourceForDraft( repository, (org.hl7.fhir.dstu3.model.RelatedArtifact) ra, version) .map(r -> (IDomainResource) r); break; case R4: referencedResource = - org.opencds.cqf.fhir.utility.visitor.r4.KnowledgeArtifactDraftVisitor - .processReferencedResourceForDraft( + org.opencds.cqf.fhir.utility.visitor.r4.DraftVisitor.processReferencedResourceForDraft( repository, (org.hl7.fhir.r4.model.RelatedArtifact) ra, version) .map(r -> (IDomainResource) r); break; case R5: referencedResource = - org.opencds.cqf.fhir.utility.visitor.r5.KnowledgeArtifactDraftVisitor - .processReferencedResourceForDraft( + org.opencds.cqf.fhir.utility.visitor.r5.DraftVisitor.processReferencedResourceForDraft( repository, (org.hl7.fhir.r5.model.RelatedArtifact) ra, version) .map(r -> (IDomainResource) r); break; @@ -226,37 +223,34 @@ private void updateUsageContextReferencesWithUrns( FhirVersionEnum fhirVersion) { switch (fhirVersion) { case DSTU3: - org.opencds.cqf.fhir.utility.visitor.dstu3.KnowledgeArtifactDraftVisitor - .updateUsageContextReferencesWithUrns( - (org.hl7.fhir.dstu3.model.MetadataResource) newResource, - resourceListWithOriginalIds.stream() - .map(ra -> (org.hl7.fhir.dstu3.model.MetadataResource) ra) - .collect(Collectors.toList()), - idListForTransactionBundle.stream() - .map(id -> new org.hl7.fhir.dstu3.model.IdType(id)) - .collect(Collectors.toList())); + org.opencds.cqf.fhir.utility.visitor.dstu3.DraftVisitor.updateUsageContextReferencesWithUrns( + (org.hl7.fhir.dstu3.model.MetadataResource) newResource, + resourceListWithOriginalIds.stream() + .map(ra -> (org.hl7.fhir.dstu3.model.MetadataResource) ra) + .collect(Collectors.toList()), + idListForTransactionBundle.stream() + .map(id -> new org.hl7.fhir.dstu3.model.IdType(id)) + .collect(Collectors.toList())); break; case R4: - org.opencds.cqf.fhir.utility.visitor.r4.KnowledgeArtifactDraftVisitor - .updateUsageContextReferencesWithUrns( - (org.hl7.fhir.r4.model.MetadataResource) newResource, - resourceListWithOriginalIds.stream() - .map(ra -> (org.hl7.fhir.r4.model.MetadataResource) ra) - .collect(Collectors.toList()), - idListForTransactionBundle.stream() - .map(id -> new org.hl7.fhir.r4.model.IdType(id)) - .collect(Collectors.toList())); + org.opencds.cqf.fhir.utility.visitor.r4.DraftVisitor.updateUsageContextReferencesWithUrns( + (org.hl7.fhir.r4.model.MetadataResource) newResource, + resourceListWithOriginalIds.stream() + .map(ra -> (org.hl7.fhir.r4.model.MetadataResource) ra) + .collect(Collectors.toList()), + idListForTransactionBundle.stream() + .map(id -> new org.hl7.fhir.r4.model.IdType(id)) + .collect(Collectors.toList())); break; case R5: - org.opencds.cqf.fhir.utility.visitor.r5.KnowledgeArtifactDraftVisitor - .updateUsageContextReferencesWithUrns( - (org.hl7.fhir.r5.model.MetadataResource) newResource, - resourceListWithOriginalIds.stream() - .map(ra -> (org.hl7.fhir.r5.model.MetadataResource) ra) - .collect(Collectors.toList()), - idListForTransactionBundle.stream() - .map(id -> new org.hl7.fhir.r5.model.IdType(id)) - .collect(Collectors.toList())); + org.opencds.cqf.fhir.utility.visitor.r5.DraftVisitor.updateUsageContextReferencesWithUrns( + (org.hl7.fhir.r5.model.MetadataResource) newResource, + resourceListWithOriginalIds.stream() + .map(ra -> (org.hl7.fhir.r5.model.MetadataResource) ra) + .collect(Collectors.toList()), + idListForTransactionBundle.stream() + .map(id -> new org.hl7.fhir.r5.model.IdType(id)) + .collect(Collectors.toList())); break; case DSTU2: diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/KnowledgeArtifactPackageVisitor.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/PackageVisitor.java similarity index 73% rename from cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/KnowledgeArtifactPackageVisitor.java rename to cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/PackageVisitor.java index 99d7327b1..6df93e09e 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/KnowledgeArtifactPackageVisitor.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/PackageVisitor.java @@ -1,5 +1,8 @@ package org.opencds.cqf.fhir.utility.visitor; +import static org.opencds.cqf.fhir.utility.visitor.VisitorHelper.findUnsupportedCapability; +import static org.opencds.cqf.fhir.utility.visitor.VisitorHelper.processCanonicals; + import ca.uhn.fhir.context.FhirVersionEnum; import ca.uhn.fhir.parser.DataFormatException; import ca.uhn.fhir.rest.server.exceptions.NotImplementedOperationException; @@ -15,7 +18,6 @@ import org.hl7.fhir.instance.model.api.IBaseBackboneElement; import org.hl7.fhir.instance.model.api.IBaseBooleanDatatype; import org.hl7.fhir.instance.model.api.IBaseBundle; -import org.hl7.fhir.instance.model.api.IBaseExtension; import org.hl7.fhir.instance.model.api.IBaseIntegerDatatype; import org.hl7.fhir.instance.model.api.IBaseParameters; import org.hl7.fhir.instance.model.api.IBaseResource; @@ -29,7 +31,7 @@ import org.opencds.cqf.fhir.utility.adapter.AdapterFactory; import org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter; -public class KnowledgeArtifactPackageVisitor implements KnowledgeArtifactVisitor { +public class PackageVisitor implements KnowledgeArtifactVisitor { @Override public IBase visit(KnowledgeArtifactAdapter adapter, Repository repository, IBaseParameters packageParameters) { @@ -70,8 +72,9 @@ public IBase visit(KnowledgeArtifactAdapter adapter, Repository repository, IBas "forceArtifactVersion", packageParameters, IPrimitiveType.class) .map(l -> l.stream().map(t -> (String) t.getValue()).collect(Collectors.toList())) .orElseGet(() -> new ArrayList<>()); - Optional isPut = VisitorHelper.getParameter("isPut", packageParameters, IBaseBooleanDatatype.class) - .map(r -> r.getValue()); + boolean isPut = VisitorHelper.getParameter("isPut", packageParameters, IBaseBooleanDatatype.class) + .map(r -> r.getValue()) + .orElse(false); if ((artifactRoute.isPresent() && !StringUtils.isBlank(artifactRoute.get()) @@ -96,7 +99,7 @@ public IBase visit(KnowledgeArtifactAdapter adapter, Repository repository, IBas if (include.size() == 1 && include.stream().anyMatch((includedType) -> includedType.equals("artifact"))) { findUnsupportedCapability(adapter, capability); processCanonicals(adapter, artifactVersion, checkArtifactVersion, forceArtifactVersion); - var entry = PackageHelper.createEntry(resource, false); + var entry = PackageHelper.createEntry(resource, isPut); BundleHelper.addEntry(packagedBundle, entry); } else { recursivePackage( @@ -107,13 +110,14 @@ public IBase visit(KnowledgeArtifactAdapter adapter, Repository repository, IBas include, artifactVersion, checkArtifactVersion, - forceArtifactVersion); + forceArtifactVersion, + isPut); var included = findUnsupportedInclude(BundleHelper.getEntry(packagedBundle), include, fhirVersion); BundleHelper.setEntry(packagedBundle, included); } setCorrectBundleType(count, offset, packagedBundle, fhirVersion); pageBundleBasedOnCountAndOffset(count, offset, packagedBundle); - handleValueSetReferenceExtensions(resource, packagedBundle, repository, terminologyEndpoint); + handleValueSets(resource, packagedBundle, repository, terminologyEndpoint); return packagedBundle; // DependencyInfo --document here that there is a need for figuring out how to determine which package the @@ -121,15 +125,15 @@ public IBase visit(KnowledgeArtifactAdapter adapter, Repository repository, IBas // what is dependency, where did it originate? potentially the package? } - protected void handleValueSetReferenceExtensions( + protected void handleValueSets( IDomainResource resource, IBaseBundle packagedBundle, Repository repository, Optional terminologyEndpoint) { switch (resource.getStructureFhirVersionEnum()) { case DSTU3: - org.opencds.cqf.fhir.utility.visitor.dstu3.KnowledgeArtifactPackageVisitor packageVisitorDstu3 = - new org.opencds.cqf.fhir.utility.visitor.dstu3.KnowledgeArtifactPackageVisitor(); + org.opencds.cqf.fhir.utility.visitor.dstu3.PackageVisitor packageVisitorDstu3 = + new org.opencds.cqf.fhir.utility.visitor.dstu3.PackageVisitor(); packageVisitorDstu3.handleValueSets( (org.hl7.fhir.dstu3.model.MetadataResource) resource, ((org.hl7.fhir.dstu3.model.Bundle) packagedBundle).getEntry(), @@ -137,18 +141,18 @@ protected void handleValueSetReferenceExtensions( terminologyEndpoint.map(te -> (org.hl7.fhir.dstu3.model.Endpoint) te)); break; case R4: - org.opencds.cqf.fhir.utility.visitor.r4.KnowledgeArtifactPackageVisitor packageVisitorR4 = - new org.opencds.cqf.fhir.utility.visitor.r4.KnowledgeArtifactPackageVisitor(); - packageVisitorR4.handleValueSetReferenceExtensions( + org.opencds.cqf.fhir.utility.visitor.r4.PackageVisitor packageVisitorR4 = + new org.opencds.cqf.fhir.utility.visitor.r4.PackageVisitor(); + packageVisitorR4.handleValueSets( (org.hl7.fhir.r4.model.MetadataResource) resource, ((org.hl7.fhir.r4.model.Bundle) packagedBundle).getEntry(), repository, terminologyEndpoint.map(te -> (org.hl7.fhir.r4.model.Endpoint) te)); break; case R5: - org.opencds.cqf.fhir.utility.visitor.r5.KnowledgeArtifactPackageVisitor packageVisitorR5 = - new org.opencds.cqf.fhir.utility.visitor.r5.KnowledgeArtifactPackageVisitor(); - packageVisitorR5.handleValueSetReferenceExtensions( + org.opencds.cqf.fhir.utility.visitor.r5.PackageVisitor packageVisitorR5 = + new org.opencds.cqf.fhir.utility.visitor.r5.PackageVisitor(); + packageVisitorR5.handleValueSets( (org.hl7.fhir.r5.model.MetadataResource) resource, ((org.hl7.fhir.r5.model.Bundle) packagedBundle).getEntry(), repository, @@ -169,7 +173,8 @@ void recursivePackage( List include, List artifactVersion, List checkArtifactVersion, - List forceArtifactVersion) + List forceArtifactVersion, + boolean isPut) throws PreconditionFailedException { if (resource != null) { var fhirVersion = resource.getStructureFhirVersionEnum(); @@ -183,7 +188,7 @@ void recursivePackage( .anyMatch(mr -> mr.getUrl().equals(adapter.getUrl()) && (!mr.hasVersion() || mr.getVersion().equals(adapter.getVersion()))); if (!entryExists) { - var entry = PackageHelper.createEntry(resource, false); + var entry = PackageHelper.createEntry(resource, isPut); BundleHelper.addEntry(bundle, entry); } @@ -214,78 +219,80 @@ void recursivePackage( include, artifactVersion, checkArtifactVersion, - forceArtifactVersion)); + forceArtifactVersion, + isPut)); } } - private void findUnsupportedCapability(KnowledgeArtifactAdapter resource, List capability) - throws PreconditionFailedException { - if (capability != null && !capability.isEmpty()) { - List> knowledgeCapabilityExtension = resource.get().getExtension().stream() - .filter(ext -> ext.getUrl().contains("cqf-knowledgeCapability")) - .collect(Collectors.toList()); - if (knowledgeCapabilityExtension.isEmpty()) { - // consider resource unsupported if it's knowledgeCapability is undefined - throw new PreconditionFailedException( - String.format("Resource with url: '%s' does not specify capability.", resource.getUrl())); - } - knowledgeCapabilityExtension.stream() - .filter(ext -> !capability.contains(((IPrimitiveType) ext.getValue()).getValue())) - .findAny() - .ifPresent((ext) -> { - throw new PreconditionFailedException(String.format( - "Resource with url: '%s' is not one of '%s'.", - resource.getUrl(), String.join(", ", capability))); - }); - } - } + // private void findUnsupportedCapability(KnowledgeArtifactAdapter resource, List capability) + // throws PreconditionFailedException { + // if (capability != null && !capability.isEmpty()) { + // List> knowledgeCapabilityExtension = resource.get().getExtension().stream() + // .filter(ext -> ext.getUrl().contains("cqf-knowledgeCapability")) + // .collect(Collectors.toList()); + // if (knowledgeCapabilityExtension.isEmpty()) { + // // consider resource unsupported if it's knowledgeCapability is undefined + // throw new PreconditionFailedException( + // String.format("Resource with url: '%s' does not specify capability.", resource.getUrl())); + // } + // knowledgeCapabilityExtension.stream() + // .filter(ext -> !capability.contains(((IPrimitiveType) ext.getValue()).getValue())) + // .findAny() + // .ifPresent((ext) -> { + // throw new PreconditionFailedException(String.format( + // "Resource with url: '%s' is not one of '%s'.", + // resource.getUrl(), String.join(", ", capability))); + // }); + // } + // } - private void processCanonicals( - KnowledgeArtifactAdapter resource, - List canonicalVersion, - List checkArtifactVersion, - List forceArtifactVersion) - throws PreconditionFailedException { - if (checkArtifactVersion != null && !checkArtifactVersion.isEmpty()) { - // check throws an error - findVersionInListMatchingResource(checkArtifactVersion, resource).ifPresent((version) -> { - if (!resource.getVersion().equals(version)) { - throw new PreconditionFailedException(String.format( - "Resource with url '%s' has version '%s' but checkVersion specifies '%s'", - resource.getUrl(), resource.getVersion(), version)); - } - }); - } else if (forceArtifactVersion != null && !forceArtifactVersion.isEmpty()) { - // force just does a silent override - findVersionInListMatchingResource(forceArtifactVersion, resource) - .ifPresent((version) -> resource.setVersion(version)); - } else if (canonicalVersion != null && !canonicalVersion.isEmpty() && !resource.hasVersion()) { - // canonicalVersion adds a version if it's missing - findVersionInListMatchingResource(canonicalVersion, resource) - .ifPresent((version) -> resource.setVersion(version)); - } - } + // private void processCanonicals( + // KnowledgeArtifactAdapter resource, + // List canonicalVersion, + // List checkArtifactVersion, + // List forceArtifactVersion) + // throws PreconditionFailedException { + // if (checkArtifactVersion != null && !checkArtifactVersion.isEmpty()) { + // // check throws an error + // findVersionInListMatchingResource(checkArtifactVersion, resource).ifPresent((version) -> { + // if (!resource.getVersion().equals(version)) { + // throw new PreconditionFailedException(String.format( + // "Resource with url '%s' has version '%s' but checkVersion specifies '%s'", + // resource.getUrl(), resource.getVersion(), version)); + // } + // }); + // } else if (forceArtifactVersion != null && !forceArtifactVersion.isEmpty()) { + // // force just does a silent override + // findVersionInListMatchingResource(forceArtifactVersion, resource) + // .ifPresent((version) -> resource.setVersion(version)); + // } else if (canonicalVersion != null && !canonicalVersion.isEmpty() && !resource.hasVersion()) { + // // canonicalVersion adds a version if it's missing + // findVersionInListMatchingResource(canonicalVersion, resource) + // .ifPresent((version) -> resource.setVersion(version)); + // } + // } - private Optional findVersionInListMatchingResource(List list, KnowledgeArtifactAdapter resource) { - return list.stream() - .filter((canonical) -> Canonicals.getUrl(canonical).equals(resource.getUrl())) - .map((canonical) -> Canonicals.getVersion(canonical)) - .findAny(); - } + // private Optional findVersionInListMatchingResource(List list, KnowledgeArtifactAdapter resource) + // { + // return list.stream() + // .filter((canonical) -> Canonicals.getUrl(canonical).equals(resource.getUrl())) + // .map((canonical) -> Canonicals.getVersion(canonical)) + // .findAny(); + // } private void setCorrectBundleType( Optional count, Optional offset, IBaseBundle bundle, FhirVersionEnum fhirVersion) { switch (fhirVersion) { case DSTU3: - org.opencds.cqf.fhir.utility.visitor.dstu3.KnowledgeArtifactPackageVisitor.setCorrectBundleType( + org.opencds.cqf.fhir.utility.visitor.dstu3.PackageVisitor.setCorrectBundleType( count, offset, (org.hl7.fhir.dstu3.model.Bundle) bundle); break; case R4: - org.opencds.cqf.fhir.utility.visitor.r4.KnowledgeArtifactPackageVisitor.setCorrectBundleType( + org.opencds.cqf.fhir.utility.visitor.r4.PackageVisitor.setCorrectBundleType( count, offset, (org.hl7.fhir.r4.model.Bundle) bundle); break; case R5: - org.opencds.cqf.fhir.utility.visitor.r5.KnowledgeArtifactPackageVisitor.setCorrectBundleType( + org.opencds.cqf.fhir.utility.visitor.r5.PackageVisitor.setCorrectBundleType( count, offset, (org.hl7.fhir.r5.model.Bundle) bundle); break; case DSTU2: @@ -331,14 +338,13 @@ private List findUnsupportedInclude( List entries, List include, FhirVersionEnum fhirVersion) { switch (fhirVersion) { case DSTU3: - return org.opencds.cqf.fhir.utility.visitor.dstu3.KnowledgeArtifactPackageVisitor - .findUnsupportedInclude( - (List) entries, include); + return org.opencds.cqf.fhir.utility.visitor.dstu3.PackageVisitor.findUnsupportedInclude( + (List) entries, include); case R4: - return org.opencds.cqf.fhir.utility.visitor.r4.KnowledgeArtifactPackageVisitor.findUnsupportedInclude( + return org.opencds.cqf.fhir.utility.visitor.r4.PackageVisitor.findUnsupportedInclude( (List) entries, include); case R5: - return org.opencds.cqf.fhir.utility.visitor.r5.KnowledgeArtifactPackageVisitor.findUnsupportedInclude( + return org.opencds.cqf.fhir.utility.visitor.r5.PackageVisitor.findUnsupportedInclude( (List) entries, include); case DSTU2: case DSTU2_1: diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/KnowledgeArtifactReleaseVisitor.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/ReleaseVisitor.java similarity index 93% rename from cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/KnowledgeArtifactReleaseVisitor.java rename to cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/ReleaseVisitor.java index da905cb0d..d9da86189 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/KnowledgeArtifactReleaseVisitor.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/ReleaseVisitor.java @@ -35,8 +35,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class KnowledgeArtifactReleaseVisitor implements KnowledgeArtifactVisitor { - private Logger log = LoggerFactory.getLogger(KnowledgeArtifactReleaseVisitor.class); +public class ReleaseVisitor implements KnowledgeArtifactVisitor { + private Logger log = LoggerFactory.getLogger(ReleaseVisitor.class); @SuppressWarnings("unchecked") @Override @@ -266,7 +266,7 @@ private void checkNonExperimental( .CRMIReleaseExperimentalBehaviorCodes.fromCode(experimentalBehavior.get()) : org.opencds.cqf.fhir.utility.dstu3.CRMIReleaseExperimentalBehavior .CRMIReleaseExperimentalBehaviorCodes.NULL; - org.opencds.cqf.fhir.utility.visitor.dstu3.KnowledgeArtifactReleaseVisitor.checkNonExperimental( + org.opencds.cqf.fhir.utility.visitor.dstu3.ReleaseVisitor.checkNonExperimental( (org.hl7.fhir.dstu3.model.MetadataResource) resource, code, repository, log); } else if (resource instanceof org.hl7.fhir.r4.model.MetadataResource) { var code = experimentalBehavior.isPresent() @@ -274,7 +274,7 @@ private void checkNonExperimental( .CRMIReleaseExperimentalBehaviorCodes.fromCode(experimentalBehavior.get()) : org.opencds.cqf.fhir.utility.r4.CRMIReleaseExperimentalBehavior .CRMIReleaseExperimentalBehaviorCodes.NULL; - org.opencds.cqf.fhir.utility.visitor.r4.KnowledgeArtifactReleaseVisitor.checkNonExperimental( + org.opencds.cqf.fhir.utility.visitor.r4.ReleaseVisitor.checkNonExperimental( (org.hl7.fhir.r4.model.MetadataResource) resource, code, repository, log); } else if (resource instanceof org.hl7.fhir.r5.model.MetadataResource) { var code = experimentalBehavior.isPresent() @@ -282,7 +282,7 @@ private void checkNonExperimental( .CRMIReleaseExperimentalBehaviorCodes.fromCode(experimentalBehavior.get()) : org.opencds.cqf.fhir.utility.r5.CRMIReleaseExperimentalBehavior .CRMIReleaseExperimentalBehaviorCodes.NULL; - org.opencds.cqf.fhir.utility.visitor.r5.KnowledgeArtifactReleaseVisitor.checkNonExperimental( + org.opencds.cqf.fhir.utility.visitor.r5.ReleaseVisitor.checkNonExperimental( (org.hl7.fhir.r5.model.MetadataResource) resource, code, repository, log); } else { throw new UnprocessableEntityException(resource.getClass().getName() + " not supported"); @@ -292,13 +292,13 @@ private void checkNonExperimental( private void propagageEffectivePeriod( ICompositeType rootEffectivePeriod, KnowledgeArtifactAdapter artifactAdapter) { if (rootEffectivePeriod instanceof org.hl7.fhir.dstu3.model.Period) { - org.opencds.cqf.fhir.utility.visitor.dstu3.KnowledgeArtifactReleaseVisitor.propagageEffectivePeriod( + org.opencds.cqf.fhir.utility.visitor.dstu3.ReleaseVisitor.propagageEffectivePeriod( (org.hl7.fhir.dstu3.model.Period) rootEffectivePeriod, artifactAdapter); } else if (rootEffectivePeriod instanceof org.hl7.fhir.r4.model.Period) { - org.opencds.cqf.fhir.utility.visitor.r4.KnowledgeArtifactReleaseVisitor.propagageEffectivePeriod( + org.opencds.cqf.fhir.utility.visitor.r4.ReleaseVisitor.propagageEffectivePeriod( (org.hl7.fhir.r4.model.Period) rootEffectivePeriod, artifactAdapter); } else if (rootEffectivePeriod instanceof org.hl7.fhir.r5.model.Period) { - org.opencds.cqf.fhir.utility.visitor.r5.KnowledgeArtifactReleaseVisitor.propagageEffectivePeriod( + org.opencds.cqf.fhir.utility.visitor.r5.ReleaseVisitor.propagageEffectivePeriod( (org.hl7.fhir.r5.model.Period) rootEffectivePeriod, artifactAdapter); } else { throw new UnprocessableEntityException( @@ -335,13 +335,13 @@ private Optional getReleaseVersion( throws UnprocessableEntityException { switch (fhirVersion) { case DSTU3: - return org.opencds.cqf.fhir.utility.visitor.dstu3.KnowledgeArtifactReleaseVisitor.getReleaseVersion( + return org.opencds.cqf.fhir.utility.visitor.dstu3.ReleaseVisitor.getReleaseVersion( version, versionBehavior, existingVersion); case R4: - return org.opencds.cqf.fhir.utility.visitor.r4.KnowledgeArtifactReleaseVisitor.getReleaseVersion( + return org.opencds.cqf.fhir.utility.visitor.r4.ReleaseVisitor.getReleaseVersion( version, versionBehavior, existingVersion); case R5: - return org.opencds.cqf.fhir.utility.visitor.r5.KnowledgeArtifactReleaseVisitor.getReleaseVersion( + return org.opencds.cqf.fhir.utility.visitor.r5.ReleaseVisitor.getReleaseVersion( version, versionBehavior, existingVersion); case DSTU2: case DSTU2_1: @@ -354,13 +354,13 @@ private Optional getReleaseVersion( private void updateReleaseLabel(IBaseResource artifact, String releaseLabel) throws IllegalArgumentException { if (artifact instanceof org.hl7.fhir.dstu3.model.MetadataResource) { - org.opencds.cqf.fhir.utility.visitor.dstu3.KnowledgeArtifactReleaseVisitor.updateReleaseLabel( + org.opencds.cqf.fhir.utility.visitor.dstu3.ReleaseVisitor.updateReleaseLabel( (org.hl7.fhir.dstu3.model.MetadataResource) artifact, releaseLabel); } else if (artifact instanceof org.hl7.fhir.r4.model.MetadataResource) { - org.opencds.cqf.fhir.utility.visitor.r4.KnowledgeArtifactReleaseVisitor.updateReleaseLabel( + org.opencds.cqf.fhir.utility.visitor.r4.ReleaseVisitor.updateReleaseLabel( (org.hl7.fhir.r4.model.MetadataResource) artifact, releaseLabel); } else if (artifact instanceof org.hl7.fhir.r5.model.MetadataResource) { - org.opencds.cqf.fhir.utility.visitor.r5.KnowledgeArtifactReleaseVisitor.updateReleaseLabel( + org.opencds.cqf.fhir.utility.visitor.r5.ReleaseVisitor.updateReleaseLabel( (org.hl7.fhir.r5.model.MetadataResource) artifact, releaseLabel); } else { throw new UnprocessableEntityException(artifact.getClass().getName() + " not supported"); @@ -425,27 +425,19 @@ private void checkReleasePreconditions(KnowledgeArtifactAdapter artifact, Date a private List findArtifactCommentsToUpdate( IBaseResource artifact, String releaseVersion, Repository repository) { if (artifact instanceof org.hl7.fhir.dstu3.model.MetadataResource) { - return org - .opencds - .cqf - .fhir - .utility - .visitor - .dstu3 - .KnowledgeArtifactReleaseVisitor - .findArtifactCommentsToUpdate( + return org.opencds.cqf.fhir.utility.visitor.dstu3.ReleaseVisitor.findArtifactCommentsToUpdate( (org.hl7.fhir.dstu3.model.MetadataResource) artifact, releaseVersion, repository) .stream() .map(r -> (IBaseBackboneElement) r) .collect(Collectors.toList()); } else if (artifact instanceof org.hl7.fhir.r4.model.MetadataResource) { - return org.opencds.cqf.fhir.utility.visitor.r4.KnowledgeArtifactReleaseVisitor.findArtifactCommentsToUpdate( + return org.opencds.cqf.fhir.utility.visitor.r4.ReleaseVisitor.findArtifactCommentsToUpdate( (org.hl7.fhir.r4.model.MetadataResource) artifact, releaseVersion, repository) .stream() .map(r -> (IBaseBackboneElement) r) .collect(Collectors.toList()); } else if (artifact instanceof org.hl7.fhir.r5.model.MetadataResource) { - return org.opencds.cqf.fhir.utility.visitor.r5.KnowledgeArtifactReleaseVisitor.findArtifactCommentsToUpdate( + return org.opencds.cqf.fhir.utility.visitor.r5.ReleaseVisitor.findArtifactCommentsToUpdate( (org.hl7.fhir.r5.model.MetadataResource) artifact, releaseVersion, repository) .stream() .map(r -> (IBaseBackboneElement) r) diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/VisitorHelper.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/VisitorHelper.java index d29032141..9709baa8b 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/VisitorHelper.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/VisitorHelper.java @@ -1,15 +1,20 @@ package org.opencds.cqf.fhir.utility.visitor; +import ca.uhn.fhir.rest.server.exceptions.PreconditionFailedException; import java.util.ArrayList; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; import org.hl7.fhir.instance.model.api.IBaseBundle; import org.hl7.fhir.instance.model.api.IBaseDatatype; +import org.hl7.fhir.instance.model.api.IBaseExtension; import org.hl7.fhir.instance.model.api.IBaseParameters; import org.hl7.fhir.instance.model.api.IBaseResource; +import org.hl7.fhir.instance.model.api.IPrimitiveType; import org.opencds.cqf.fhir.utility.BundleHelper; +import org.opencds.cqf.fhir.utility.Canonicals; import org.opencds.cqf.fhir.utility.adapter.AdapterFactory; +import org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter; public class VisitorHelper { @@ -77,4 +82,60 @@ public static List getMetadataResourcesFromBundle(IBaseBundle bun return resourceList; } + + public static void findUnsupportedCapability(KnowledgeArtifactAdapter resource, List capability) + throws PreconditionFailedException { + if (capability != null && !capability.isEmpty()) { + List> knowledgeCapabilityExtension = resource.get().getExtension().stream() + .filter(ext -> ext.getUrl().contains("cqf-knowledgeCapability")) + .collect(Collectors.toList()); + if (knowledgeCapabilityExtension.isEmpty()) { + // consider resource unsupported if it's knowledgeCapability is undefined + throw new PreconditionFailedException( + String.format("Resource with url: '%s' does not specify capability.", resource.getUrl())); + } + knowledgeCapabilityExtension.stream() + .filter(ext -> !capability.contains(((IPrimitiveType) ext.getValue()).getValue())) + .findAny() + .ifPresent((ext) -> { + throw new PreconditionFailedException(String.format( + "Resource with url: '%s' is not one of '%s'.", + resource.getUrl(), String.join(", ", capability))); + }); + } + } + + public static void processCanonicals( + KnowledgeArtifactAdapter resource, + List canonicalVersion, + List checkArtifactVersion, + List forceArtifactVersion) + throws PreconditionFailedException { + if (checkArtifactVersion != null && !checkArtifactVersion.isEmpty()) { + // check throws an error + findVersionInListMatchingResource(checkArtifactVersion, resource).ifPresent((version) -> { + if (!resource.getVersion().equals(version)) { + throw new PreconditionFailedException(String.format( + "Resource with url '%s' has version '%s' but checkVersion specifies '%s'", + resource.getUrl(), resource.getVersion(), version)); + } + }); + } else if (forceArtifactVersion != null && !forceArtifactVersion.isEmpty()) { + // force just does a silent override + findVersionInListMatchingResource(forceArtifactVersion, resource) + .ifPresent((version) -> resource.setVersion(version)); + } else if (canonicalVersion != null && !canonicalVersion.isEmpty() && !resource.hasVersion()) { + // canonicalVersion adds a version if it's missing + findVersionInListMatchingResource(canonicalVersion, resource) + .ifPresent((version) -> resource.setVersion(version)); + } + } + + private static Optional findVersionInListMatchingResource( + List list, KnowledgeArtifactAdapter resource) { + return list.stream() + .filter((canonical) -> Canonicals.getUrl(canonical).equals(resource.getUrl())) + .map((canonical) -> Canonicals.getVersion(canonical)) + .findAny(); + } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactApproveVisitor.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/dstu3/ApproveVisitor.java similarity index 97% rename from cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactApproveVisitor.java rename to cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/dstu3/ApproveVisitor.java index eb6ffece5..5b3e953fb 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactApproveVisitor.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/dstu3/ApproveVisitor.java @@ -10,7 +10,7 @@ import org.hl7.fhir.instance.model.api.IIdType; import org.opencds.cqf.fhir.utility.dstu3.ArtifactAssessment; -public class KnowledgeArtifactApproveVisitor { +public class ApproveVisitor { public static ArtifactAssessment createApprovalAssessment( IIdType id, diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactDraftVisitor.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/dstu3/DraftVisitor.java similarity index 98% rename from cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactDraftVisitor.java rename to cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/dstu3/DraftVisitor.java index 8bed77201..104d78e28 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactDraftVisitor.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/dstu3/DraftVisitor.java @@ -11,7 +11,7 @@ import org.opencds.cqf.fhir.api.Repository; import org.opencds.cqf.fhir.utility.SearchHelper; -public class KnowledgeArtifactDraftVisitor { +public class DraftVisitor { public static Optional processReferencedResourceForDraft( Repository repository, RelatedArtifact ra, String version) { diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactPackageVisitor.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/dstu3/PackageVisitor.java similarity index 99% rename from cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactPackageVisitor.java rename to cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/dstu3/PackageVisitor.java index b7c02039f..864cf0a50 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactPackageVisitor.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/dstu3/PackageVisitor.java @@ -33,11 +33,11 @@ import org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter; import org.opencds.cqf.fhir.utility.client.TerminologyServerClient; -public class KnowledgeArtifactPackageVisitor { +public class PackageVisitor { private final FhirContext fhirContext = FhirContext.forDstu3Cached(); - public KnowledgeArtifactPackageVisitor() { + public PackageVisitor() { this.terminologyServerClient = new TerminologyServerClient(fhirContext); } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactReleaseVisitor.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/dstu3/ReleaseVisitor.java similarity index 99% rename from cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactReleaseVisitor.java rename to cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/dstu3/ReleaseVisitor.java index 944a91ee3..e9b7b3e2a 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactReleaseVisitor.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/dstu3/ReleaseVisitor.java @@ -30,7 +30,7 @@ import org.opencds.cqf.fhir.utility.dstu3.CRMIReleaseVersionBehavior.CRMIReleaseVersionBehaviorCodes; import org.slf4j.Logger; -public class KnowledgeArtifactReleaseVisitor { +public class ReleaseVisitor { public static void checkNonExperimental( MetadataResource resource, diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactApproveVisitor.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r4/ApproveVisitor.java similarity index 97% rename from cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactApproveVisitor.java rename to cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r4/ApproveVisitor.java index a8d6bd35b..4a7a80d7a 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactApproveVisitor.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r4/ApproveVisitor.java @@ -10,7 +10,7 @@ import org.hl7.fhir.r4.model.Reference; import org.opencds.cqf.fhir.utility.r4.ArtifactAssessment; -public class KnowledgeArtifactApproveVisitor { +public class ApproveVisitor { public static ArtifactAssessment createApprovalAssessment( IIdType id, diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactDraftVisitor.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r4/DraftVisitor.java similarity index 98% rename from cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactDraftVisitor.java rename to cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r4/DraftVisitor.java index ec9274f97..2b86d1c51 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactDraftVisitor.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r4/DraftVisitor.java @@ -12,7 +12,7 @@ import org.opencds.cqf.fhir.api.Repository; import org.opencds.cqf.fhir.utility.SearchHelper; -public class KnowledgeArtifactDraftVisitor { +public class DraftVisitor { public static Optional processReferencedResourceForDraft( Repository repository, RelatedArtifact ra, String version) { diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactPackageVisitor.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r4/PackageVisitor.java similarity index 99% rename from cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactPackageVisitor.java rename to cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r4/PackageVisitor.java index 9cfb79d49..d03586e33 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactPackageVisitor.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r4/PackageVisitor.java @@ -33,12 +33,12 @@ import org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter; import org.opencds.cqf.fhir.utility.client.TerminologyServerClient; -public class KnowledgeArtifactPackageVisitor { +public class PackageVisitor { // private Logger logger = LoggerFactory.getLogger(KnowledgeArtifactPackageVisitor.class); private final FhirContext fhirContext = FhirContext.forR4Cached(); - public KnowledgeArtifactPackageVisitor() { + public PackageVisitor() { this.terminologyServerClient = new TerminologyServerClient(fhirContext); } @@ -221,7 +221,7 @@ public static List findUnsupportedInclude( * @param manifest the resource containing all RelatedArtifact references * @param bundleEntries the list of packaged resources to modify according to the extensions on the manifest relatedArtifact references */ - public void handleValueSetReferenceExtensions( + public void handleValueSets( MetadataResource manifest, List bundleEntries, Repository repository, @@ -255,7 +255,7 @@ public void expandValueSet( : valueSet.getUrl(); ValueSet expandedValueSet; - if (hasSimpleCompose(valueSet)) { + if (!terminologyEndpoint.isPresent() && hasSimpleCompose(valueSet)) { // Perform naive expansion independent of terminology servers. Copy all codes from compose into expansion. ValueSet.ValueSetExpansionComponent expansion = new ValueSet.ValueSetExpansionComponent(); expansion.setTimestamp(Date.from(Instant.now())); diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactReleaseVisitor.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r4/ReleaseVisitor.java similarity index 99% rename from cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactReleaseVisitor.java rename to cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r4/ReleaseVisitor.java index 4771cd6d9..4de30c828 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactReleaseVisitor.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r4/ReleaseVisitor.java @@ -30,7 +30,7 @@ import org.opencds.cqf.fhir.utility.r4.CRMIReleaseVersionBehavior.CRMIReleaseVersionBehaviorCodes; import org.slf4j.Logger; -public class KnowledgeArtifactReleaseVisitor { +public class ReleaseVisitor { public static void checkNonExperimental( MetadataResource resource, diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactApproveVisitor.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r5/ApproveVisitor.java similarity index 98% rename from cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactApproveVisitor.java rename to cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r5/ApproveVisitor.java index 119f15450..b425a4c9e 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactApproveVisitor.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r5/ApproveVisitor.java @@ -12,7 +12,7 @@ import org.hl7.fhir.r5.model.Reference; import org.hl7.fhir.r5.model.RelatedArtifact.RelatedArtifactType; -public class KnowledgeArtifactApproveVisitor { +public class ApproveVisitor { public static ArtifactAssessment createApprovalAssessment( IIdType id, diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactDraftVisitor.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r5/DraftVisitor.java similarity index 98% rename from cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactDraftVisitor.java rename to cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r5/DraftVisitor.java index 077939263..5ee6210ea 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactDraftVisitor.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r5/DraftVisitor.java @@ -11,7 +11,7 @@ import org.opencds.cqf.fhir.api.Repository; import org.opencds.cqf.fhir.utility.SearchHelper; -public class KnowledgeArtifactDraftVisitor { +public class DraftVisitor { public static Optional processReferencedResourceForDraft( Repository repository, RelatedArtifact ra, String version) { diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactPackageVisitor.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r5/PackageVisitor.java similarity index 99% rename from cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactPackageVisitor.java rename to cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r5/PackageVisitor.java index 9b7f3cefa..15a0d849f 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactPackageVisitor.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r5/PackageVisitor.java @@ -33,11 +33,11 @@ import org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter; import org.opencds.cqf.fhir.utility.client.TerminologyServerClient; -public class KnowledgeArtifactPackageVisitor { +public class PackageVisitor { private final FhirContext fhirContext = FhirContext.forR5Cached(); - public KnowledgeArtifactPackageVisitor() { + public PackageVisitor() { this.terminologyServerClient = new TerminologyServerClient(fhirContext); } @@ -216,7 +216,7 @@ public static List findUnsupportedInclude( * @param manifest the resource containing all RelatedArtifact references * @param bundleEntries the list of packaged resources to modify according to the extensions on the manifest relatedArtifact references */ - public void handleValueSetReferenceExtensions( + public void handleValueSets( MetadataResource manifest, List bundleEntries, Repository repository, diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactReleaseVisitor.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r5/ReleaseVisitor.java similarity index 99% rename from cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactReleaseVisitor.java rename to cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r5/ReleaseVisitor.java index 6fda8a093..5c2d97122 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactReleaseVisitor.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r5/ReleaseVisitor.java @@ -30,7 +30,7 @@ import org.opencds.cqf.fhir.utility.r5.CRMIReleaseVersionBehavior.CRMIReleaseVersionBehaviorCodes; import org.slf4j.Logger; -public class KnowledgeArtifactReleaseVisitor { +public class ReleaseVisitor { public static void checkNonExperimental( MetadataResource resource, diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/PlanDefinitionAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/PlanDefinitionAdapterTest.java index 72ec340aa..be41f896a 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/PlanDefinitionAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/PlanDefinitionAdapterTest.java @@ -19,12 +19,12 @@ import org.hl7.fhir.dstu3.model.UriType; import org.hl7.fhir.instance.model.api.IDomainResource; import org.junit.jupiter.api.Test; -import org.opencds.cqf.fhir.utility.visitor.KnowledgeArtifactPackageVisitor; +import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; public class PlanDefinitionAdapterTest { @Test void adapter_accepts_visitor() { - var spyVisitor = spy(new KnowledgeArtifactPackageVisitor()); + var spyVisitor = spy(new PackageVisitor()); doReturn(new Bundle()).when(spyVisitor).visit(any(PlanDefinitionAdapter.class), any(), any()); IDomainResource planDef = new PlanDefinition(); var adapter = new PlanDefinitionAdapter(planDef); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/PlanDefinitionAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/PlanDefinitionAdapterTest.java index 8a3bb55ce..dc5090534 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/PlanDefinitionAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/PlanDefinitionAdapterTest.java @@ -17,12 +17,12 @@ import org.hl7.fhir.r4.model.PlanDefinition; import org.hl7.fhir.r4.model.RelatedArtifact; import org.junit.jupiter.api.Test; -import org.opencds.cqf.fhir.utility.visitor.KnowledgeArtifactPackageVisitor; +import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; public class PlanDefinitionAdapterTest { @Test void adapter_accepts_visitor() { - var spyVisitor = spy(new KnowledgeArtifactPackageVisitor()); + var spyVisitor = spy(new PackageVisitor()); doReturn(new Bundle()).when(spyVisitor).visit(any(PlanDefinitionAdapter.class), any(), any()); IDomainResource planDef = new PlanDefinition(); var adapter = new PlanDefinitionAdapter(planDef); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/PlanDefinitionAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/PlanDefinitionAdapterTest.java index d03a64c4b..f7cd39125 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/PlanDefinitionAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/PlanDefinitionAdapterTest.java @@ -17,12 +17,12 @@ import org.hl7.fhir.r5.model.PlanDefinition; import org.hl7.fhir.r5.model.RelatedArtifact; import org.junit.jupiter.api.Test; -import org.opencds.cqf.fhir.utility.visitor.KnowledgeArtifactPackageVisitor; +import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; public class PlanDefinitionAdapterTest { @Test void adapter_accepts_visitor() { - var spyVisitor = spy(new KnowledgeArtifactPackageVisitor()); + var spyVisitor = spy(new PackageVisitor()); doReturn(new Bundle()).when(spyVisitor).visit(any(PlanDefinitionAdapter.class), any(), any()); IDomainResource planDef = new PlanDefinition(); var adapter = new PlanDefinitionAdapter(planDef); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactApproveVisitorTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactApproveVisitorTest.java index 8a99686be..763d1d44c 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactApproveVisitorTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactApproveVisitorTest.java @@ -37,7 +37,7 @@ import org.opencds.cqf.fhir.utility.adapter.dstu3.AdapterFactory; import org.opencds.cqf.fhir.utility.dstu3.ArtifactAssessment; import org.opencds.cqf.fhir.utility.repository.InMemoryFhirRepository; -import org.opencds.cqf.fhir.utility.visitor.KnowledgeArtifactApproveVisitor; +import org.opencds.cqf.fhir.utility.visitor.ApproveVisitor; class KnowledgeArtifactApproveVisitorTest { private final FhirContext fhirContext = FhirContext.forDstu3Cached(); @@ -66,7 +66,7 @@ void approveOperation_endpoint_id_should_match_target_parameter() { var artifactAssessmentTarget = "Library/This-Library-Does-Not-Exist|1.0.0"; var params = parameters(part("artifactAssessmentTarget", new UriType(artifactAssessmentTarget))); UnprocessableEntityException maybeException = null; - var releaseVisitor = new KnowledgeArtifactApproveVisitor(); + var releaseVisitor = new ApproveVisitor(); var lib = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -96,7 +96,7 @@ void approveOperation_should_respect_artifactAssessment_information_type_binding String artifactAssessmentType = "this-type-does-not-exist"; Parameters params = parameters(part("artifactAssessmentType", new CodeType(artifactAssessmentType))); UnprocessableEntityException maybeException = null; - KnowledgeArtifactApproveVisitor releaseVisitor = new KnowledgeArtifactApproveVisitor(); + ApproveVisitor releaseVisitor = new ApproveVisitor(); Library lib = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -130,7 +130,7 @@ void approveOperation_test() { part("artifactAssessmentTarget", new UriType(artifactAssessmentTarget)), part("artifactAssessmentRelatedArtifact", new UriType(artifactAssessmentRelatedArtifact)), part("artifactAssessmentAuthor", new Reference(artifactAssessmentAuthor))); - KnowledgeArtifactApproveVisitor approveVisitor = new KnowledgeArtifactApproveVisitor(); + ApproveVisitor approveVisitor = new ApproveVisitor(); Library lib = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactDraftVisitorTests.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactDraftVisitorTests.java index b6c203ecb..40c511d09 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactDraftVisitorTests.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactDraftVisitorTests.java @@ -41,7 +41,7 @@ import org.opencds.cqf.fhir.utility.adapter.dstu3.AdapterFactory; import org.opencds.cqf.fhir.utility.dstu3.MetadataResourceHelper; import org.opencds.cqf.fhir.utility.repository.InMemoryFhirRepository; -import org.opencds.cqf.fhir.utility.visitor.KnowledgeArtifactDraftVisitor; +import org.opencds.cqf.fhir.utility.visitor.DraftVisitor; import org.opencds.cqf.fhir.utility.visitor.KnowledgeArtifactVisitor; class KnowledgeArtifactDraftVisitorTests { @@ -85,7 +85,7 @@ void library_draft_test() { Bundle bundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactDraftVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); spyRepository.transaction(bundle); - KnowledgeArtifactVisitor draftVisitor = new KnowledgeArtifactDraftVisitor(); + KnowledgeArtifactVisitor draftVisitor = new DraftVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -148,7 +148,7 @@ void draftOperation_no_effectivePeriod_test() { .copy(); assertTrue(baseLib.hasEffectivePeriod()); LibraryAdapter libraryAdapter = new AdapterFactory().createLibrary(baseLib); - KnowledgeArtifactVisitor draftVisitor = new KnowledgeArtifactDraftVisitor(); + KnowledgeArtifactVisitor draftVisitor = new DraftVisitor(); PlanDefinition planDef = spyRepository .read(PlanDefinition.class, new IdType("PlanDefinition/plandefinition-ersd-instance-example")) .copy(); @@ -181,7 +181,7 @@ void draftOperation_version_conflict_test() { .read(Library.class, new IdType(specificationLibReference)) .copy(); LibraryAdapter libraryAdapter = new AdapterFactory().createLibrary(baseLib); - KnowledgeArtifactVisitor draftVisitor = new KnowledgeArtifactDraftVisitor(); + KnowledgeArtifactVisitor draftVisitor = new DraftVisitor(); try { libraryAdapter.accept(draftVisitor, spyRepository, params); @@ -204,7 +204,7 @@ void draftOperation_cannot_create_draft_of_draft_test() { .read(Library.class, new IdType("Library/SpecificationLibraryDraftVersion-1-0-0-23")) .copy(); LibraryAdapter libraryAdapter = new AdapterFactory().createLibrary(baseLib); - KnowledgeArtifactVisitor draftVisitor = new KnowledgeArtifactDraftVisitor(); + KnowledgeArtifactVisitor draftVisitor = new DraftVisitor(); try { libraryAdapter.accept(draftVisitor, spyRepository, params); } catch (PreconditionFailedException e) { @@ -223,7 +223,7 @@ void draftOperation_version_format_test() { .read(Library.class, new IdType("Library/SpecificationLibraryDraftVersion-1-0-0-23")) .copy(); LibraryAdapter libraryAdapter = new AdapterFactory().createLibrary(baseLib); - KnowledgeArtifactVisitor draftVisitor = new KnowledgeArtifactDraftVisitor(); + KnowledgeArtifactVisitor draftVisitor = new DraftVisitor(); for (String version : badVersionList) { UnprocessableEntityException maybeException = null; diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactPackageVisitorTests.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactPackageVisitorTests.java index 2ce224bb5..5826cf9f8 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactPackageVisitorTests.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactPackageVisitorTests.java @@ -45,7 +45,7 @@ import org.opencds.cqf.fhir.utility.adapter.LibraryAdapter; import org.opencds.cqf.fhir.utility.adapter.dstu3.AdapterFactory; import org.opencds.cqf.fhir.utility.repository.InMemoryFhirRepository; -import org.opencds.cqf.fhir.utility.visitor.KnowledgeArtifactPackageVisitor; +import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; class KnowledgeArtifactPackageVisitorTests { private final FhirContext fhirContext = FhirContext.forDstu3Cached(); @@ -71,7 +71,7 @@ void visitLibraryTest() { Bundle loadedBundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example-naive.json")); spyRepository.transaction(loadedBundle); - KnowledgeArtifactPackageVisitor packageVisitor = new KnowledgeArtifactPackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -104,7 +104,7 @@ void packageOperation_should_fail_no_credentials() { Bundle loadedBundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); spyRepository.transaction(loadedBundle); - KnowledgeArtifactPackageVisitor packageVisitor = new KnowledgeArtifactPackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -126,7 +126,7 @@ void packageOperation_should_fail_credentials_missing_username() { Bundle loadedBundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); spyRepository.transaction(loadedBundle); - KnowledgeArtifactPackageVisitor packageVisitor = new KnowledgeArtifactPackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -152,7 +152,7 @@ void packageOperation_should_fail_credentials_missing_apikey() { Bundle loadedBundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); spyRepository.transaction(loadedBundle); - KnowledgeArtifactPackageVisitor packageVisitor = new KnowledgeArtifactPackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -178,7 +178,7 @@ void packageOperation_should_fail_credentials_invalid() { Bundle loadedBundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); spyRepository.transaction(loadedBundle); - KnowledgeArtifactPackageVisitor packageVisitor = new KnowledgeArtifactPackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -206,7 +206,7 @@ void packageOperation_should_fail_non_matching_capability() { "Bundle-ersd-package-capabilities.json")); spyRepository.transaction(bundle); List capabilities = Arrays.asList("computable", "publishable", "executable"); - KnowledgeArtifactPackageVisitor packageVisitor = new KnowledgeArtifactPackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -238,7 +238,7 @@ void packageOperation_should_apply_check_force_canonicalVersions() { Bundle bundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-active-no-versions.json")); spyRepository.transaction(bundle); - KnowledgeArtifactPackageVisitor packageVisitor = new KnowledgeArtifactPackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -299,7 +299,7 @@ void packageOperation_should_respect_count_offset() { Bundle bundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); spyRepository.transaction(bundle); - KnowledgeArtifactPackageVisitor packageVisitor = new KnowledgeArtifactPackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -337,7 +337,7 @@ void packageOperation_different_bundle_types() { Bundle bundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); spyRepository.transaction(bundle); - KnowledgeArtifactPackageVisitor packageVisitor = new KnowledgeArtifactPackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -376,7 +376,7 @@ void packageOperation_should_conditionally_create() { Bundle bundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); spyRepository.transaction(bundle); - KnowledgeArtifactPackageVisitor packageVisitor = new KnowledgeArtifactPackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -396,7 +396,7 @@ void packageOperation_should_respect_include() { Bundle bundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); spyRepository.transaction(bundle); - KnowledgeArtifactPackageVisitor packageVisitor = new KnowledgeArtifactPackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactReleaseVisitorTests.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactReleaseVisitorTests.java index 9da783efa..f70a5feea 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactReleaseVisitorTests.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactReleaseVisitorTests.java @@ -48,7 +48,7 @@ import org.opencds.cqf.fhir.utility.adapter.dstu3.AdapterFactory; import org.opencds.cqf.fhir.utility.dstu3.MetadataResourceHelper; import org.opencds.cqf.fhir.utility.repository.InMemoryFhirRepository; -import org.opencds.cqf.fhir.utility.visitor.KnowledgeArtifactReleaseVisitor; +import org.opencds.cqf.fhir.utility.visitor.ReleaseVisitor; import org.slf4j.LoggerFactory; class KnowledgeArtifactReleaseVisitorTests { @@ -95,7 +95,7 @@ void visitLibraryTest() { Bundle bundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream("Bundle-ersd-release-bundle.json")); spyRepository.transaction(bundle); - KnowledgeArtifactReleaseVisitor releaseVisitor = new KnowledgeArtifactReleaseVisitor(); + ReleaseVisitor releaseVisitor = new ReleaseVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/ReleaseSpecificationLibrary")) .copy(); @@ -201,7 +201,7 @@ void releaseResource_force_version() { spyRepository.transaction(bundle); // Existing version should be "1.2.3"; String newVersionToForce = "1.2.7.23"; - KnowledgeArtifactReleaseVisitor releaseVisitor = new KnowledgeArtifactReleaseVisitor(); + ReleaseVisitor releaseVisitor = new ReleaseVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -239,7 +239,7 @@ void releaseResource_require_non_experimental_error() { part("requireNonExperimental", new CodeType("error"))); Exception notExpectingAnyException = null; // no Exception if root is experimental - KnowledgeArtifactReleaseVisitor releaseVisitor = new KnowledgeArtifactReleaseVisitor(); + ReleaseVisitor releaseVisitor = new ReleaseVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -278,7 +278,7 @@ void releaseResource_require_non_experimental_warn() { "Bundle-small-approved-draft-experimental-children.json")); spyRepository.transaction(bundle2); - KnowledgeArtifactReleaseVisitor releaseVisitor = new KnowledgeArtifactReleaseVisitor(); + ReleaseVisitor releaseVisitor = new ReleaseVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -336,7 +336,7 @@ void releaseResource_propagate_effective_period() { Parameters params = parameters(part("version", new StringType("1.2.7")), part("versionBehavior", new CodeType("default"))); - KnowledgeArtifactReleaseVisitor releaseVisitor = new KnowledgeArtifactReleaseVisitor(); + ReleaseVisitor releaseVisitor = new ReleaseVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -375,7 +375,7 @@ void releaseResource_latestFromTx_NotSupported_test() { part("version", "1.2.3"), part("versionBehavior", new CodeType("default")), part("latestFromTxServer", new BooleanType(true))); - KnowledgeArtifactReleaseVisitor releaseVisitor = new KnowledgeArtifactReleaseVisitor(); + ReleaseVisitor releaseVisitor = new ReleaseVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -400,7 +400,7 @@ void release_missing_approvalDate_validation_test() { String actualErrorMessage = ""; Parameters params1 = parameters(part("version", versionData), part("versionBehavior", new CodeType("default"))); - KnowledgeArtifactReleaseVisitor releaseVisitor = new KnowledgeArtifactReleaseVisitor(); + ReleaseVisitor releaseVisitor = new ReleaseVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/ReleaseSpecificationLibrary")) .copy(); @@ -418,7 +418,7 @@ void release_version_format_test() { Bundle bundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft.json")); spyRepository.transaction(bundle); - KnowledgeArtifactReleaseVisitor releaseVisitor = new KnowledgeArtifactReleaseVisitor(); + ReleaseVisitor releaseVisitor = new ReleaseVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -443,7 +443,7 @@ void release_releaseLabel_test() { KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft.json")); spyRepository.transaction(bundle); String releaseLabel = "release label test"; - KnowledgeArtifactReleaseVisitor releaseVisitor = new KnowledgeArtifactReleaseVisitor(); + ReleaseVisitor releaseVisitor = new ReleaseVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -472,7 +472,7 @@ void release_version_active_test() { Bundle bundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); spyRepository.transaction(bundle); - KnowledgeArtifactReleaseVisitor releaseVisitor = new KnowledgeArtifactReleaseVisitor(); + ReleaseVisitor releaseVisitor = new ReleaseVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -494,7 +494,7 @@ void release_versionBehaviour_format_test() { Bundle bundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft.json")); spyRepository.transaction(bundle); - KnowledgeArtifactReleaseVisitor releaseVisitor = new KnowledgeArtifactReleaseVisitor(); + ReleaseVisitor releaseVisitor = new ReleaseVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactApproveVisitorTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactApproveVisitorTest.java index 4e4e60856..10805cc06 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactApproveVisitorTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactApproveVisitorTest.java @@ -37,7 +37,7 @@ import org.opencds.cqf.fhir.utility.adapter.r4.AdapterFactory; import org.opencds.cqf.fhir.utility.r4.ArtifactAssessment; import org.opencds.cqf.fhir.utility.repository.InMemoryFhirRepository; -import org.opencds.cqf.fhir.utility.visitor.KnowledgeArtifactApproveVisitor; +import org.opencds.cqf.fhir.utility.visitor.ApproveVisitor; class KnowledgeArtifactApproveVisitorTest { private final FhirContext fhirContext = FhirContext.forR4Cached(); @@ -66,7 +66,7 @@ void approveOperation_endpoint_id_should_match_target_parameter() { var artifactAssessmentTarget = "Library/This-Library-Does-Not-Exist|1.0.0"; var params = parameters(part("artifactAssessmentTarget", new CanonicalType(artifactAssessmentTarget))); UnprocessableEntityException maybeException = null; - var releaseVisitor = new KnowledgeArtifactApproveVisitor(); + var releaseVisitor = new ApproveVisitor(); var lib = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -96,7 +96,7 @@ void approveOperation_should_respect_artifactAssessment_information_type_binding String artifactAssessmentType = "this-type-does-not-exist"; Parameters params = parameters(part("artifactAssessmentType", new CodeType(artifactAssessmentType))); UnprocessableEntityException maybeException = null; - KnowledgeArtifactApproveVisitor releaseVisitor = new KnowledgeArtifactApproveVisitor(); + ApproveVisitor releaseVisitor = new ApproveVisitor(); Library lib = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -130,7 +130,7 @@ void approveOperation_test() { part("artifactAssessmentTarget", new CanonicalType(artifactAssessmentTarget)), part("artifactAssessmentRelatedArtifact", new CanonicalType(artifactAssessmentRelatedArtifact)), part("artifactAssessmentAuthor", new Reference(artifactAssessmentAuthor))); - KnowledgeArtifactApproveVisitor approveVisitor = new KnowledgeArtifactApproveVisitor(); + ApproveVisitor approveVisitor = new ApproveVisitor(); Library lib = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactDraftVisitorTests.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactDraftVisitorTests.java index ebeaae3b0..2790c3bc7 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactDraftVisitorTests.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactDraftVisitorTests.java @@ -41,7 +41,7 @@ import org.opencds.cqf.fhir.utility.adapter.r4.AdapterFactory; import org.opencds.cqf.fhir.utility.r4.MetadataResourceHelper; import org.opencds.cqf.fhir.utility.repository.InMemoryFhirRepository; -import org.opencds.cqf.fhir.utility.visitor.KnowledgeArtifactDraftVisitor; +import org.opencds.cqf.fhir.utility.visitor.DraftVisitor; import org.opencds.cqf.fhir.utility.visitor.KnowledgeArtifactVisitor; class KnowledgeArtifactDraftVisitorTests { @@ -85,7 +85,7 @@ void library_draft_test() { Bundle bundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactDraftVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); spyRepository.transaction(bundle); - KnowledgeArtifactVisitor draftVisitor = new KnowledgeArtifactDraftVisitor(); + KnowledgeArtifactVisitor draftVisitor = new DraftVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -144,7 +144,7 @@ void draftOperation_no_effectivePeriod_test() { .copy(); assertTrue(baseLib.hasEffectivePeriod()); LibraryAdapter libraryAdapter = new AdapterFactory().createLibrary(baseLib); - KnowledgeArtifactVisitor draftVisitor = new KnowledgeArtifactDraftVisitor(); + KnowledgeArtifactVisitor draftVisitor = new DraftVisitor(); PlanDefinition planDef = spyRepository .read(PlanDefinition.class, new IdType("PlanDefinition/plandefinition-ersd-instance-example")) .copy(); @@ -177,7 +177,7 @@ void draftOperation_version_conflict_test() { .read(Library.class, new IdType(specificationLibReference)) .copy(); LibraryAdapter libraryAdapter = new AdapterFactory().createLibrary(baseLib); - KnowledgeArtifactVisitor draftVisitor = new KnowledgeArtifactDraftVisitor(); + KnowledgeArtifactVisitor draftVisitor = new DraftVisitor(); try { libraryAdapter.accept(draftVisitor, spyRepository, params); @@ -200,7 +200,7 @@ void draftOperation_cannot_create_draft_of_draft_test() { .read(Library.class, new IdType("Library/SpecificationLibraryDraftVersion-1-0-0-23")) .copy(); LibraryAdapter libraryAdapter = new AdapterFactory().createLibrary(baseLib); - KnowledgeArtifactVisitor draftVisitor = new KnowledgeArtifactDraftVisitor(); + KnowledgeArtifactVisitor draftVisitor = new DraftVisitor(); try { libraryAdapter.accept(draftVisitor, spyRepository, params); } catch (PreconditionFailedException e) { @@ -219,7 +219,7 @@ void draftOperation_version_format_test() { .read(Library.class, new IdType("Library/SpecificationLibraryDraftVersion-1-0-0-23")) .copy(); LibraryAdapter libraryAdapter = new AdapterFactory().createLibrary(baseLib); - KnowledgeArtifactVisitor draftVisitor = new KnowledgeArtifactDraftVisitor(); + KnowledgeArtifactVisitor draftVisitor = new DraftVisitor(); for (String version : badVersionList) { UnprocessableEntityException maybeException = null; diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactPackageVisitorTests.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactPackageVisitorTests.java index d21ff0ac6..beff495df 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactPackageVisitorTests.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactPackageVisitorTests.java @@ -45,7 +45,7 @@ import org.opencds.cqf.fhir.utility.adapter.LibraryAdapter; import org.opencds.cqf.fhir.utility.adapter.r4.AdapterFactory; import org.opencds.cqf.fhir.utility.repository.InMemoryFhirRepository; -import org.opencds.cqf.fhir.utility.visitor.KnowledgeArtifactPackageVisitor; +import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; class KnowledgeArtifactPackageVisitorTests { private final FhirContext fhirContext = FhirContext.forR4Cached(); @@ -71,7 +71,7 @@ void visitLibraryTest() { Bundle loadedBundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example-naive.json")); spyRepository.transaction(loadedBundle); - KnowledgeArtifactPackageVisitor packageVisitor = new KnowledgeArtifactPackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -104,7 +104,7 @@ void packageOperation_should_fail_no_credentials() { Bundle loadedBundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); spyRepository.transaction(loadedBundle); - KnowledgeArtifactPackageVisitor packageVisitor = new KnowledgeArtifactPackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -126,7 +126,7 @@ void packageOperation_should_fail_credentials_missing_username() { Bundle loadedBundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); spyRepository.transaction(loadedBundle); - KnowledgeArtifactPackageVisitor packageVisitor = new KnowledgeArtifactPackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -152,7 +152,7 @@ void packageOperation_should_fail_credentials_missing_apikey() { Bundle loadedBundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); spyRepository.transaction(loadedBundle); - KnowledgeArtifactPackageVisitor packageVisitor = new KnowledgeArtifactPackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -178,7 +178,7 @@ void packageOperation_should_fail_credentials_invalid() { Bundle loadedBundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); spyRepository.transaction(loadedBundle); - KnowledgeArtifactPackageVisitor packageVisitor = new KnowledgeArtifactPackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -206,7 +206,7 @@ void packageOperation_should_fail_non_matching_capability() { "Bundle-ersd-package-capabilities.json")); spyRepository.transaction(bundle); List capabilities = Arrays.asList("computable", "publishable", "executable"); - KnowledgeArtifactPackageVisitor packageVisitor = new KnowledgeArtifactPackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -238,7 +238,7 @@ void packageOperation_should_apply_check_force_canonicalVersions() { Bundle bundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-active-no-versions.json")); spyRepository.transaction(bundle); - KnowledgeArtifactPackageVisitor packageVisitor = new KnowledgeArtifactPackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -300,7 +300,7 @@ void packageOperation_should_respect_count_offset() { Bundle bundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); spyRepository.transaction(bundle); - KnowledgeArtifactPackageVisitor packageVisitor = new KnowledgeArtifactPackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -338,7 +338,7 @@ void packageOperation_different_bundle_types() { Bundle bundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); spyRepository.transaction(bundle); - KnowledgeArtifactPackageVisitor packageVisitor = new KnowledgeArtifactPackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -377,7 +377,7 @@ void packageOperation_should_conditionally_create() { Bundle bundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); spyRepository.transaction(bundle); - KnowledgeArtifactPackageVisitor packageVisitor = new KnowledgeArtifactPackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -397,7 +397,7 @@ void packageOperation_should_respect_include() { Bundle bundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); spyRepository.transaction(bundle); - KnowledgeArtifactPackageVisitor packageVisitor = new KnowledgeArtifactPackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactReleaseVisitorTests.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactReleaseVisitorTests.java index c178d1bc7..0bab8a302 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactReleaseVisitorTests.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactReleaseVisitorTests.java @@ -49,7 +49,7 @@ import org.opencds.cqf.fhir.utility.adapter.r4.AdapterFactory; import org.opencds.cqf.fhir.utility.r4.MetadataResourceHelper; import org.opencds.cqf.fhir.utility.repository.InMemoryFhirRepository; -import org.opencds.cqf.fhir.utility.visitor.KnowledgeArtifactReleaseVisitor; +import org.opencds.cqf.fhir.utility.visitor.ReleaseVisitor; import org.slf4j.LoggerFactory; class KnowledgeArtifactReleaseVisitorTests { @@ -96,7 +96,7 @@ void visitLibraryTest() { Bundle bundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream("Bundle-ersd-release-bundle.json")); spyRepository.transaction(bundle); - KnowledgeArtifactReleaseVisitor releaseVisitor = new KnowledgeArtifactReleaseVisitor(); + ReleaseVisitor releaseVisitor = new ReleaseVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/ReleaseSpecificationLibrary")) .copy(); @@ -202,7 +202,7 @@ void releaseResource_force_version() { spyRepository.transaction(bundle); // Existing version should be "1.2.3"; String newVersionToForce = "1.2.7.23"; - KnowledgeArtifactReleaseVisitor releaseVisitor = new KnowledgeArtifactReleaseVisitor(); + ReleaseVisitor releaseVisitor = new ReleaseVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -240,7 +240,7 @@ void releaseResource_require_non_experimental_error() { part("requireNonExperimental", new CodeType("error"))); Exception notExpectingAnyException = null; // no Exception if root is experimental - KnowledgeArtifactReleaseVisitor releaseVisitor = new KnowledgeArtifactReleaseVisitor(); + ReleaseVisitor releaseVisitor = new ReleaseVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -279,7 +279,7 @@ void releaseResource_require_non_experimental_warn() { "Bundle-small-approved-draft-experimental-children.json")); spyRepository.transaction(bundle2); - KnowledgeArtifactReleaseVisitor releaseVisitor = new KnowledgeArtifactReleaseVisitor(); + ReleaseVisitor releaseVisitor = new ReleaseVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -337,7 +337,7 @@ void releaseResource_propagate_effective_period() { Parameters params = parameters(part("version", new StringType("1.2.7")), part("versionBehavior", new CodeType("default"))); - KnowledgeArtifactReleaseVisitor releaseVisitor = new KnowledgeArtifactReleaseVisitor(); + ReleaseVisitor releaseVisitor = new ReleaseVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -376,7 +376,7 @@ void releaseResource_latestFromTx_NotSupported_test() { part("version", "1.2.3"), part("versionBehavior", new CodeType("default")), part("latestFromTxServer", new BooleanType(true))); - KnowledgeArtifactReleaseVisitor releaseVisitor = new KnowledgeArtifactReleaseVisitor(); + ReleaseVisitor releaseVisitor = new ReleaseVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -401,7 +401,7 @@ void release_missing_approvalDate_validation_test() { String actualErrorMessage = ""; Parameters params1 = parameters(part("version", versionData), part("versionBehavior", new CodeType("default"))); - KnowledgeArtifactReleaseVisitor releaseVisitor = new KnowledgeArtifactReleaseVisitor(); + ReleaseVisitor releaseVisitor = new ReleaseVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/ReleaseSpecificationLibrary")) .copy(); @@ -419,7 +419,7 @@ void release_version_format_test() { Bundle bundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft.json")); spyRepository.transaction(bundle); - KnowledgeArtifactReleaseVisitor releaseVisitor = new KnowledgeArtifactReleaseVisitor(); + ReleaseVisitor releaseVisitor = new ReleaseVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -444,7 +444,7 @@ void release_releaseLabel_test() { KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft.json")); spyRepository.transaction(bundle); String releaseLabel = "release label test"; - KnowledgeArtifactReleaseVisitor releaseVisitor = new KnowledgeArtifactReleaseVisitor(); + ReleaseVisitor releaseVisitor = new ReleaseVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -473,7 +473,7 @@ void release_version_active_test() { Bundle bundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); spyRepository.transaction(bundle); - KnowledgeArtifactReleaseVisitor releaseVisitor = new KnowledgeArtifactReleaseVisitor(); + ReleaseVisitor releaseVisitor = new ReleaseVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -495,7 +495,7 @@ void release_versionBehaviour_format_test() { Bundle bundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft.json")); spyRepository.transaction(bundle); - KnowledgeArtifactReleaseVisitor releaseVisitor = new KnowledgeArtifactReleaseVisitor(); + ReleaseVisitor releaseVisitor = new ReleaseVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -521,7 +521,7 @@ void release_preserves_extensions() { var bundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft.json")); spyRepository.transaction(bundle); - var releaseVisitor = new KnowledgeArtifactReleaseVisitor(); + var releaseVisitor = new ReleaseVisitor(); var orginalLibrary = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactApproveVisitorTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactApproveVisitorTest.java index 0d4e8b0ed..80aa7281e 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactApproveVisitorTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactApproveVisitorTest.java @@ -37,7 +37,7 @@ import org.opencds.cqf.fhir.utility.adapter.LibraryAdapter; import org.opencds.cqf.fhir.utility.adapter.r5.AdapterFactory; import org.opencds.cqf.fhir.utility.repository.InMemoryFhirRepository; -import org.opencds.cqf.fhir.utility.visitor.KnowledgeArtifactApproveVisitor; +import org.opencds.cqf.fhir.utility.visitor.ApproveVisitor; class KnowledgeArtifactApproveVisitorTest { private final FhirContext fhirContext = FhirContext.forR5Cached(); @@ -66,7 +66,7 @@ void approveOperation_endpoint_id_should_match_target_parameter() { var artifactAssessmentTarget = "Library/This-Library-Does-Not-Exist|1.0.0"; var params = parameters(part("artifactAssessmentTarget", new CanonicalType(artifactAssessmentTarget))); UnprocessableEntityException maybeException = null; - var releaseVisitor = new KnowledgeArtifactApproveVisitor(); + var releaseVisitor = new ApproveVisitor(); var lib = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -96,7 +96,7 @@ void approveOperation_should_respect_artifactAssessment_information_type_binding String artifactAssessmentType = "this-type-does-not-exist"; Parameters params = parameters(part("artifactAssessmentType", new CodeType(artifactAssessmentType))); UnprocessableEntityException maybeException = null; - KnowledgeArtifactApproveVisitor releaseVisitor = new KnowledgeArtifactApproveVisitor(); + ApproveVisitor releaseVisitor = new ApproveVisitor(); Library lib = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -130,7 +130,7 @@ void approveOperation_test() { part("artifactAssessmentTarget", new CanonicalType(artifactAssessmentTarget)), part("artifactAssessmentRelatedArtifact", new CanonicalType(artifactAssessmentRelatedArtifact)), part("artifactAssessmentAuthor", new Reference(artifactAssessmentAuthor))); - KnowledgeArtifactApproveVisitor approveVisitor = new KnowledgeArtifactApproveVisitor(); + ApproveVisitor approveVisitor = new ApproveVisitor(); Library lib = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactDraftVisitorTests.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactDraftVisitorTests.java index 2f1247403..5cfea0b8f 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactDraftVisitorTests.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactDraftVisitorTests.java @@ -41,7 +41,7 @@ import org.opencds.cqf.fhir.utility.adapter.r5.AdapterFactory; import org.opencds.cqf.fhir.utility.r5.MetadataResourceHelper; import org.opencds.cqf.fhir.utility.repository.InMemoryFhirRepository; -import org.opencds.cqf.fhir.utility.visitor.KnowledgeArtifactDraftVisitor; +import org.opencds.cqf.fhir.utility.visitor.DraftVisitor; import org.opencds.cqf.fhir.utility.visitor.KnowledgeArtifactVisitor; class KnowledgeArtifactDraftVisitorTests { @@ -85,7 +85,7 @@ void library_draft_test() { Bundle bundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactDraftVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); spyRepository.transaction(bundle); - KnowledgeArtifactVisitor draftVisitor = new KnowledgeArtifactDraftVisitor(); + KnowledgeArtifactVisitor draftVisitor = new DraftVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -144,7 +144,7 @@ void draftOperation_no_effectivePeriod_test() { .copy(); assertTrue(baseLib.hasEffectivePeriod()); LibraryAdapter libraryAdapter = new AdapterFactory().createLibrary(baseLib); - KnowledgeArtifactVisitor draftVisitor = new KnowledgeArtifactDraftVisitor(); + KnowledgeArtifactVisitor draftVisitor = new DraftVisitor(); PlanDefinition planDef = spyRepository .read(PlanDefinition.class, new IdType("PlanDefinition/plandefinition-ersd-instance-example")) .copy(); @@ -177,7 +177,7 @@ void draftOperation_version_conflict_test() { .read(Library.class, new IdType(specificationLibReference)) .copy(); LibraryAdapter libraryAdapter = new AdapterFactory().createLibrary(baseLib); - KnowledgeArtifactVisitor draftVisitor = new KnowledgeArtifactDraftVisitor(); + KnowledgeArtifactVisitor draftVisitor = new DraftVisitor(); try { libraryAdapter.accept(draftVisitor, spyRepository, params); @@ -200,7 +200,7 @@ void draftOperation_cannot_create_draft_of_draft_test() { .read(Library.class, new IdType("Library/SpecificationLibraryDraftVersion-1-0-0-23")) .copy(); LibraryAdapter libraryAdapter = new AdapterFactory().createLibrary(baseLib); - KnowledgeArtifactVisitor draftVisitor = new KnowledgeArtifactDraftVisitor(); + KnowledgeArtifactVisitor draftVisitor = new DraftVisitor(); try { libraryAdapter.accept(draftVisitor, spyRepository, params); } catch (PreconditionFailedException e) { @@ -219,7 +219,7 @@ void draftOperation_version_format_test() { .read(Library.class, new IdType("Library/SpecificationLibraryDraftVersion-1-0-0-23")) .copy(); LibraryAdapter libraryAdapter = new AdapterFactory().createLibrary(baseLib); - KnowledgeArtifactVisitor draftVisitor = new KnowledgeArtifactDraftVisitor(); + KnowledgeArtifactVisitor draftVisitor = new DraftVisitor(); for (String version : badVersionList) { UnprocessableEntityException maybeException = null; diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactPackageVisitorTests.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactPackageVisitorTests.java index 59dbe2de1..326addcd7 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactPackageVisitorTests.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactPackageVisitorTests.java @@ -45,7 +45,7 @@ import org.opencds.cqf.fhir.utility.adapter.LibraryAdapter; import org.opencds.cqf.fhir.utility.adapter.r5.AdapterFactory; import org.opencds.cqf.fhir.utility.repository.InMemoryFhirRepository; -import org.opencds.cqf.fhir.utility.visitor.KnowledgeArtifactPackageVisitor; +import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; class KnowledgeArtifactPackageVisitorTests { private final FhirContext fhirContext = FhirContext.forR5Cached(); @@ -71,7 +71,7 @@ void visitLibraryTest() { Bundle loadedBundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example-naive.json")); spyRepository.transaction(loadedBundle); - KnowledgeArtifactPackageVisitor packageVisitor = new KnowledgeArtifactPackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -104,7 +104,7 @@ void packageOperation_should_fail_no_credentials() { Bundle loadedBundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); spyRepository.transaction(loadedBundle); - KnowledgeArtifactPackageVisitor packageVisitor = new KnowledgeArtifactPackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -126,7 +126,7 @@ void packageOperation_should_fail_credentials_missing_username() { Bundle loadedBundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); spyRepository.transaction(loadedBundle); - KnowledgeArtifactPackageVisitor packageVisitor = new KnowledgeArtifactPackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -152,7 +152,7 @@ void packageOperation_should_fail_credentials_missing_apikey() { Bundle loadedBundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); spyRepository.transaction(loadedBundle); - KnowledgeArtifactPackageVisitor packageVisitor = new KnowledgeArtifactPackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -178,7 +178,7 @@ void packageOperation_should_fail_credentials_invalid() { Bundle loadedBundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); spyRepository.transaction(loadedBundle); - KnowledgeArtifactPackageVisitor packageVisitor = new KnowledgeArtifactPackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -206,7 +206,7 @@ void packageOperation_should_fail_non_matching_capability() { "Bundle-ersd-package-capabilities.json")); spyRepository.transaction(bundle); List capabilities = Arrays.asList("computable", "publishable", "executable"); - KnowledgeArtifactPackageVisitor packageVisitor = new KnowledgeArtifactPackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -238,7 +238,7 @@ void packageOperation_should_apply_check_force_canonicalVersions() { Bundle bundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-active-no-versions.json")); spyRepository.transaction(bundle); - KnowledgeArtifactPackageVisitor packageVisitor = new KnowledgeArtifactPackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -300,7 +300,7 @@ void packageOperation_should_respect_count_offset() { Bundle bundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); spyRepository.transaction(bundle); - KnowledgeArtifactPackageVisitor packageVisitor = new KnowledgeArtifactPackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -338,7 +338,7 @@ void packageOperation_different_bundle_types() { Bundle bundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); spyRepository.transaction(bundle); - KnowledgeArtifactPackageVisitor packageVisitor = new KnowledgeArtifactPackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -377,7 +377,7 @@ void packageOperation_should_conditionally_create() { Bundle bundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); spyRepository.transaction(bundle); - KnowledgeArtifactPackageVisitor packageVisitor = new KnowledgeArtifactPackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -397,7 +397,7 @@ void packageOperation_should_respect_include() { Bundle bundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); spyRepository.transaction(bundle); - KnowledgeArtifactPackageVisitor packageVisitor = new KnowledgeArtifactPackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactReleaseVisitorTests.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactReleaseVisitorTests.java index b01a4b0c4..a08379eba 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactReleaseVisitorTests.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactReleaseVisitorTests.java @@ -48,7 +48,7 @@ import org.opencds.cqf.fhir.utility.adapter.r5.AdapterFactory; import org.opencds.cqf.fhir.utility.r5.MetadataResourceHelper; import org.opencds.cqf.fhir.utility.repository.InMemoryFhirRepository; -import org.opencds.cqf.fhir.utility.visitor.KnowledgeArtifactReleaseVisitor; +import org.opencds.cqf.fhir.utility.visitor.ReleaseVisitor; import org.slf4j.LoggerFactory; class KnowledgeArtifactReleaseVisitorTests { @@ -95,7 +95,7 @@ void visitLibraryTest() { Bundle bundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream("Bundle-ersd-release-bundle.json")); spyRepository.transaction(bundle); - KnowledgeArtifactReleaseVisitor releaseVisitor = new KnowledgeArtifactReleaseVisitor(); + ReleaseVisitor releaseVisitor = new ReleaseVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/ReleaseSpecificationLibrary")) .copy(); @@ -199,7 +199,7 @@ void releaseResource_force_version() { spyRepository.transaction(bundle); // Existing version should be "1.2.3"; String newVersionToForce = "1.2.7.23"; - KnowledgeArtifactReleaseVisitor releaseVisitor = new KnowledgeArtifactReleaseVisitor(); + ReleaseVisitor releaseVisitor = new ReleaseVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -237,7 +237,7 @@ void releaseResource_require_non_experimental_error() { part("requireNonExperimental", new CodeType("error"))); Exception notExpectingAnyException = null; // no Exception if root is experimental - KnowledgeArtifactReleaseVisitor releaseVisitor = new KnowledgeArtifactReleaseVisitor(); + ReleaseVisitor releaseVisitor = new ReleaseVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -276,7 +276,7 @@ void releaseResource_require_non_experimental_warn() { "Bundle-small-approved-draft-experimental-children.json")); spyRepository.transaction(bundle2); - KnowledgeArtifactReleaseVisitor releaseVisitor = new KnowledgeArtifactReleaseVisitor(); + ReleaseVisitor releaseVisitor = new ReleaseVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -334,7 +334,7 @@ void releaseResource_propagate_effective_period() { Parameters params = parameters(part("version", new StringType("1.2.7")), part("versionBehavior", new CodeType("default"))); - KnowledgeArtifactReleaseVisitor releaseVisitor = new KnowledgeArtifactReleaseVisitor(); + ReleaseVisitor releaseVisitor = new ReleaseVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -373,7 +373,7 @@ void releaseResource_latestFromTx_NotSupported_test() { part("version", "1.2.3"), part("versionBehavior", new CodeType("default")), part("latestFromTxServer", new BooleanType(true))); - KnowledgeArtifactReleaseVisitor releaseVisitor = new KnowledgeArtifactReleaseVisitor(); + ReleaseVisitor releaseVisitor = new ReleaseVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -398,7 +398,7 @@ void release_missing_approvalDate_validation_test() { String actualErrorMessage = ""; Parameters params1 = parameters(part("version", versionData), part("versionBehavior", new CodeType("default"))); - KnowledgeArtifactReleaseVisitor releaseVisitor = new KnowledgeArtifactReleaseVisitor(); + ReleaseVisitor releaseVisitor = new ReleaseVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/ReleaseSpecificationLibrary")) .copy(); @@ -416,7 +416,7 @@ void release_version_format_test() { Bundle bundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft.json")); spyRepository.transaction(bundle); - KnowledgeArtifactReleaseVisitor releaseVisitor = new KnowledgeArtifactReleaseVisitor(); + ReleaseVisitor releaseVisitor = new ReleaseVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -441,7 +441,7 @@ void release_releaseLabel_test() { KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft.json")); spyRepository.transaction(bundle); String releaseLabel = "release label test"; - KnowledgeArtifactReleaseVisitor releaseVisitor = new KnowledgeArtifactReleaseVisitor(); + ReleaseVisitor releaseVisitor = new ReleaseVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -470,7 +470,7 @@ void release_version_active_test() { Bundle bundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); spyRepository.transaction(bundle); - KnowledgeArtifactReleaseVisitor releaseVisitor = new KnowledgeArtifactReleaseVisitor(); + ReleaseVisitor releaseVisitor = new ReleaseVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -492,7 +492,7 @@ void release_versionBehaviour_format_test() { Bundle bundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft.json")); spyRepository.transaction(bundle); - KnowledgeArtifactReleaseVisitor releaseVisitor = new KnowledgeArtifactReleaseVisitor(); + ReleaseVisitor releaseVisitor = new ReleaseVisitor(); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); From 163748ae891b509d5127f4e3a0faec9dbbaceafc Mon Sep 17 00:00:00 2001 From: Brenin Rhodes Date: Wed, 5 Jun 2024 15:14:45 -0600 Subject: [PATCH 04/38] Add type and dataRequirement --- .../fhir/utility/adapter/LibraryAdapter.java | 14 ++++++++ .../utility/adapter/dstu3/LibraryAdapter.java | 36 +++++++++++++++++++ .../utility/adapter/r4/LibraryAdapter.java | 36 +++++++++++++++++++ .../utility/adapter/r5/LibraryAdapter.java | 36 +++++++++++++++++++ 4 files changed, 122 insertions(+) diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/LibraryAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/LibraryAdapter.java index 00b108b4f..5013f6ab8 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/LibraryAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/LibraryAdapter.java @@ -1,5 +1,6 @@ package org.opencds.cqf.fhir.utility.adapter; +import java.util.Arrays; import java.util.List; import org.hl7.fhir.instance.model.api.ICompositeType; @@ -8,6 +9,9 @@ */ public interface LibraryAdapter extends KnowledgeArtifactAdapter { + List LIBRARY_TYPES = + Arrays.asList("logic-library", "model-definition", "asset-collection", "module-definition"); + boolean hasContent(); List getContent(); @@ -15,4 +19,14 @@ public interface LibraryAdapter extends KnowledgeArtifactAdapter { void setContent(List attachments); ICompositeType addContent(); + + ICompositeType getType(); + + LibraryAdapter setType(String type); + + List getDataRequirement(); + + LibraryAdapter addDataRequirement(ICompositeType dataRequirement); + + LibraryAdapter setDataRequirements(List dataRequirements); } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/LibraryAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/LibraryAdapter.java index 7962a39b7..2ca8f957c 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/LibraryAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/LibraryAdapter.java @@ -6,6 +6,9 @@ import java.util.List; import java.util.stream.Collectors; import org.hl7.fhir.dstu3.model.Attachment; +import org.hl7.fhir.dstu3.model.CodeableConcept; +import org.hl7.fhir.dstu3.model.Coding; +import org.hl7.fhir.dstu3.model.DataRequirement; import org.hl7.fhir.dstu3.model.DateTimeType; import org.hl7.fhir.dstu3.model.Enumerations.PublicationStatus; import org.hl7.fhir.dstu3.model.Extension; @@ -274,4 +277,37 @@ public boolean getExperimental() { public void setExtension(List> extensions) { this.get().setExtension(extensions.stream().map(e -> (Extension) e).collect(Collectors.toList())); } + + @Override + public ICompositeType getType() { + return library.getType(); + } + + @Override + public LibraryAdapter setType(String type) { + if (LIBRARY_TYPES.contains(type)) { + library.setType(new CodeableConcept(new Coding("http://hl7.org/fhir/ValueSet/library-type", type, ""))); + } else { + throw new UnprocessableEntityException("Invalid type: {}", type); + } + return this; + } + + @Override + public List getDataRequirement() { + return library.getDataRequirement(); + } + + @Override + public LibraryAdapter addDataRequirement(ICompositeType dataRequirement) { + library.addDataRequirement((DataRequirement) dataRequirement); + return this; + } + + @Override + public LibraryAdapter setDataRequirements(List dataRequirements) { + library.setDataRequirement( + dataRequirements.stream().map(dr -> (DataRequirement) dr).collect(Collectors.toList())); + return this; + } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/LibraryAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/LibraryAdapter.java index fce6b3b97..8627b92ae 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/LibraryAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/LibraryAdapter.java @@ -12,6 +12,9 @@ import org.hl7.fhir.instance.model.api.IDomainResource; import org.hl7.fhir.instance.model.api.IPrimitiveType; import org.hl7.fhir.r4.model.Attachment; +import org.hl7.fhir.r4.model.CodeableConcept; +import org.hl7.fhir.r4.model.Coding; +import org.hl7.fhir.r4.model.DataRequirement; import org.hl7.fhir.r4.model.DateTimeType; import org.hl7.fhir.r4.model.Enumerations.PublicationStatus; import org.hl7.fhir.r4.model.Extension; @@ -265,4 +268,37 @@ public boolean getExperimental() { public void setExtension(List> extensions) { this.get().setExtension(extensions.stream().map(e -> (Extension) e).collect(Collectors.toList())); } + + @Override + public ICompositeType getType() { + return library.getType(); + } + + @Override + public LibraryAdapter setType(String type) { + if (LIBRARY_TYPES.contains(type)) { + library.setType(new CodeableConcept(new Coding("http://hl7.org/fhir/ValueSet/library-type", type, ""))); + } else { + throw new UnprocessableEntityException("Invalid type: {}", type); + } + return this; + } + + @Override + public List getDataRequirement() { + return library.getDataRequirement(); + } + + @Override + public LibraryAdapter addDataRequirement(ICompositeType dataRequirement) { + library.addDataRequirement((DataRequirement) dataRequirement); + return this; + } + + @Override + public LibraryAdapter setDataRequirements(List dataRequirements) { + library.setDataRequirement( + dataRequirements.stream().map(dr -> (DataRequirement) dr).collect(Collectors.toList())); + return this; + } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/LibraryAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/LibraryAdapter.java index 51b0dbcbf..ffbd7690b 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/LibraryAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/LibraryAdapter.java @@ -12,6 +12,9 @@ import org.hl7.fhir.instance.model.api.IDomainResource; import org.hl7.fhir.instance.model.api.IPrimitiveType; import org.hl7.fhir.r5.model.Attachment; +import org.hl7.fhir.r5.model.CodeableConcept; +import org.hl7.fhir.r5.model.Coding; +import org.hl7.fhir.r5.model.DataRequirement; import org.hl7.fhir.r5.model.DateTimeType; import org.hl7.fhir.r5.model.Enumerations.PublicationStatus; import org.hl7.fhir.r5.model.Extension; @@ -259,4 +262,37 @@ public boolean getExperimental() { public void setExtension(List> extensions) { this.get().setExtension(extensions.stream().map(e -> (Extension) e).collect(Collectors.toList())); } + + @Override + public ICompositeType getType() { + return library.getType(); + } + + @Override + public LibraryAdapter setType(String type) { + if (LIBRARY_TYPES.contains(type)) { + library.setType(new CodeableConcept(new Coding("http://hl7.org/fhir/ValueSet/library-type", type, ""))); + } else { + throw new UnprocessableEntityException("Invalid type: {}", type); + } + return this; + } + + @Override + public List getDataRequirement() { + return library.getDataRequirement(); + } + + @Override + public LibraryAdapter addDataRequirement(ICompositeType dataRequirement) { + library.addDataRequirement((DataRequirement) dataRequirement); + return this; + } + + @Override + public LibraryAdapter setDataRequirements(List dataRequirements) { + library.setDataRequirement( + dataRequirements.stream().map(dr -> (DataRequirement) dr).collect(Collectors.toList())); + return this; + } } From 39cb3def7b5b72222bc9a6fe3f3174fb1863bf66 Mon Sep 17 00:00:00 2001 From: Brenin Rhodes Date: Thu, 6 Jun 2024 16:37:24 -0600 Subject: [PATCH 05/38] Refactor ValueSet expansion --- .../cqf/fhir/cr/common/PackageProcessor.java | 4 +- .../cqf/fhir/cr/library/LibraryProcessor.java | 4 +- .../PlanDefinitionProcessor.java | 4 +- .../questionnaire/QuestionnaireProcessor.java | 4 +- .../fhir/utility/visitor/PackageVisitor.java | 89 ++------- .../utility/visitor/r4/PackageVisitor.java | 175 +++++++++++++----- .../KnowledgeArtifactPackageVisitorTests.java | 15 +- 7 files changed, 150 insertions(+), 145 deletions(-) diff --git a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/common/PackageProcessor.java b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/common/PackageProcessor.java index 7f7a5b0c1..8ef54d44f 100644 --- a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/common/PackageProcessor.java +++ b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/common/PackageProcessor.java @@ -4,7 +4,6 @@ import static org.opencds.cqf.fhir.utility.Parameters.newParameters; import ca.uhn.fhir.context.FhirVersionEnum; -import org.hl7.fhir.instance.model.api.IBase; import org.hl7.fhir.instance.model.api.IBaseBundle; import org.hl7.fhir.instance.model.api.IBaseParameters; import org.hl7.fhir.instance.model.api.IBaseResource; @@ -31,13 +30,12 @@ public IBaseBundle packageResource(IBaseResource resource) { @Override public IBaseBundle packageResource(IBaseResource resource, String method) { - IBase[] parts = {}; return packageResource( resource, newParameters( repository.fhirContext(), "package-parameters", - newBooleanPart(repository.fhirContext(), "isPut", method.equals("PUT"), parts))); + newBooleanPart(repository.fhirContext(), "isPut", method.equals("PUT")))); } @Override diff --git a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/library/LibraryProcessor.java b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/library/LibraryProcessor.java index 074fa3d18..1d8136a2a 100644 --- a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/library/LibraryProcessor.java +++ b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/library/LibraryProcessor.java @@ -5,7 +5,6 @@ import static org.opencds.cqf.fhir.utility.Parameters.newParameters; import ca.uhn.fhir.context.FhirVersionEnum; -import org.hl7.fhir.instance.model.api.IBase; import org.hl7.fhir.instance.model.api.IBaseBundle; import org.hl7.fhir.instance.model.api.IBaseParameters; import org.hl7.fhir.instance.model.api.IBaseResource; @@ -60,13 +59,12 @@ public , R extends IBaseResource> IBaseBundle p public , R extends IBaseResource> IBaseBundle packageLibrary( Either3 library, boolean isPut) { - IBase[] parts = {}; return packageLibrary( library, newParameters( repository.fhirContext(), "package-parameters", - newBooleanPart(repository.fhirContext(), "isPut", isPut, parts))); + newBooleanPart(repository.fhirContext(), "isPut", isPut))); } public , R extends IBaseResource> IBaseBundle packageLibrary( diff --git a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/plandefinition/PlanDefinitionProcessor.java b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/plandefinition/PlanDefinitionProcessor.java index 1d9a0ba96..74df063a7 100644 --- a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/plandefinition/PlanDefinitionProcessor.java +++ b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/plandefinition/PlanDefinitionProcessor.java @@ -7,7 +7,6 @@ import static org.opencds.cqf.fhir.utility.repository.Repositories.proxy; import ca.uhn.fhir.context.FhirVersionEnum; -import org.hl7.fhir.instance.model.api.IBase; import org.hl7.fhir.instance.model.api.IBaseBundle; import org.hl7.fhir.instance.model.api.IBaseDatatype; import org.hl7.fhir.instance.model.api.IBaseParameters; @@ -90,13 +89,12 @@ public , R extends IBaseResource> IBaseBundle p public , R extends IBaseResource> IBaseBundle packagePlanDefinition( Either3 planDefinition, boolean isPut) { - IBase[] parts = {}; return packagePlanDefinition( planDefinition, newParameters( repository.fhirContext(), "package-parameters", - newBooleanPart(repository.fhirContext(), "isPut", isPut, parts))); + newBooleanPart(repository.fhirContext(), "isPut", isPut))); } public , R extends IBaseResource> IBaseBundle packagePlanDefinition( diff --git a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/questionnaire/QuestionnaireProcessor.java b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/questionnaire/QuestionnaireProcessor.java index f1aa5036b..f96182626 100644 --- a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/questionnaire/QuestionnaireProcessor.java +++ b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/questionnaire/QuestionnaireProcessor.java @@ -7,7 +7,6 @@ import static org.opencds.cqf.fhir.utility.repository.Repositories.proxy; import ca.uhn.fhir.context.FhirVersionEnum; -import org.hl7.fhir.instance.model.api.IBase; import org.hl7.fhir.instance.model.api.IBaseBundle; import org.hl7.fhir.instance.model.api.IBaseParameters; import org.hl7.fhir.instance.model.api.IBaseResource; @@ -175,13 +174,12 @@ public > IBaseBundle packageQuestionnaire( public > IBaseBundle packageQuestionnaire( Either3 questionnaire, boolean isPut) { - IBase[] parts = {}; return packageQuestionnaire( questionnaire, newParameters( repository.fhirContext(), "package-parameters", - newBooleanPart(repository.fhirContext(), "isPut", isPut, parts))); + newBooleanPart(repository.fhirContext(), "isPut", isPut))); } public > IBaseBundle packageQuestionnaire( diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/PackageVisitor.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/PackageVisitor.java index 6df93e09e..5dc45e037 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/PackageVisitor.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/PackageVisitor.java @@ -4,7 +4,6 @@ import static org.opencds.cqf.fhir.utility.visitor.VisitorHelper.processCanonicals; import ca.uhn.fhir.context.FhirVersionEnum; -import ca.uhn.fhir.parser.DataFormatException; import ca.uhn.fhir.rest.server.exceptions.NotImplementedOperationException; import ca.uhn.fhir.rest.server.exceptions.PreconditionFailedException; import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; @@ -115,9 +114,9 @@ public IBase visit(KnowledgeArtifactAdapter adapter, Repository repository, IBas var included = findUnsupportedInclude(BundleHelper.getEntry(packagedBundle), include, fhirVersion); BundleHelper.setEntry(packagedBundle, included); } + handleValueSets(resource, packagedBundle, repository, terminologyEndpoint); setCorrectBundleType(count, offset, packagedBundle, fhirVersion); pageBundleBasedOnCountAndOffset(count, offset, packagedBundle); - handleValueSets(resource, packagedBundle, repository, terminologyEndpoint); return packagedBundle; // DependencyInfo --document here that there is a need for figuring out how to determine which package the @@ -165,7 +164,7 @@ protected void handleValueSets( } } - void recursivePackage( + protected void recursivePackage( IDomainResource resource, IBaseBundle bundle, Repository repository, @@ -192,21 +191,17 @@ void recursivePackage( BundleHelper.addEntry(bundle, entry); } - adapter.combineComponentsAndDependencies().stream() - // sometimes VS dependencies aren't FHIR resources - .filter(ra -> !StringUtils.isBlank(Canonicals.getResourceType(ra.getReference()))) + var dependencies = adapter.combineComponentsAndDependencies(); + dependencies.stream() + // sometimes VS dependencies aren't FHIR resources, only include references that are .filter(ra -> { try { - var resourceDef = repository - .fhirContext() - .getResourceDefinition(Canonicals.getResourceType(ra.getReference())); - return resourceDef != null; - } catch (DataFormatException e) { - if (e.getMessage().contains("1684")) { - return false; - } else { - throw new DataFormatException(e.getMessage()); - } + return null + != repository + .fhirContext() + .getResourceDefinition(Canonicals.getResourceType(ra.getReference())); + } catch (Exception e) { + return false; } }) .map(ra -> SearchHelper.searchRepositoryByCanonicalWithPaging(repository, ra.getReference())) @@ -224,63 +219,7 @@ void recursivePackage( } } - // private void findUnsupportedCapability(KnowledgeArtifactAdapter resource, List capability) - // throws PreconditionFailedException { - // if (capability != null && !capability.isEmpty()) { - // List> knowledgeCapabilityExtension = resource.get().getExtension().stream() - // .filter(ext -> ext.getUrl().contains("cqf-knowledgeCapability")) - // .collect(Collectors.toList()); - // if (knowledgeCapabilityExtension.isEmpty()) { - // // consider resource unsupported if it's knowledgeCapability is undefined - // throw new PreconditionFailedException( - // String.format("Resource with url: '%s' does not specify capability.", resource.getUrl())); - // } - // knowledgeCapabilityExtension.stream() - // .filter(ext -> !capability.contains(((IPrimitiveType) ext.getValue()).getValue())) - // .findAny() - // .ifPresent((ext) -> { - // throw new PreconditionFailedException(String.format( - // "Resource with url: '%s' is not one of '%s'.", - // resource.getUrl(), String.join(", ", capability))); - // }); - // } - // } - - // private void processCanonicals( - // KnowledgeArtifactAdapter resource, - // List canonicalVersion, - // List checkArtifactVersion, - // List forceArtifactVersion) - // throws PreconditionFailedException { - // if (checkArtifactVersion != null && !checkArtifactVersion.isEmpty()) { - // // check throws an error - // findVersionInListMatchingResource(checkArtifactVersion, resource).ifPresent((version) -> { - // if (!resource.getVersion().equals(version)) { - // throw new PreconditionFailedException(String.format( - // "Resource with url '%s' has version '%s' but checkVersion specifies '%s'", - // resource.getUrl(), resource.getVersion(), version)); - // } - // }); - // } else if (forceArtifactVersion != null && !forceArtifactVersion.isEmpty()) { - // // force just does a silent override - // findVersionInListMatchingResource(forceArtifactVersion, resource) - // .ifPresent((version) -> resource.setVersion(version)); - // } else if (canonicalVersion != null && !canonicalVersion.isEmpty() && !resource.hasVersion()) { - // // canonicalVersion adds a version if it's missing - // findVersionInListMatchingResource(canonicalVersion, resource) - // .ifPresent((version) -> resource.setVersion(version)); - // } - // } - - // private Optional findVersionInListMatchingResource(List list, KnowledgeArtifactAdapter resource) - // { - // return list.stream() - // .filter((canonical) -> Canonicals.getUrl(canonical).equals(resource.getUrl())) - // .map((canonical) -> Canonicals.getVersion(canonical)) - // .findAny(); - // } - - private void setCorrectBundleType( + protected void setCorrectBundleType( Optional count, Optional offset, IBaseBundle bundle, FhirVersionEnum fhirVersion) { switch (fhirVersion) { case DSTU3: @@ -310,7 +249,7 @@ private void setCorrectBundleType( * @param offset the number of resources to skip beginning from the start of the bundle (starts from 1) * @param bundle the bundle to page */ - private void pageBundleBasedOnCountAndOffset( + protected void pageBundleBasedOnCountAndOffset( Optional count, Optional offset, IBaseBundle bundle) { if (offset.isPresent()) { var entries = BundleHelper.getEntry(bundle); @@ -334,7 +273,7 @@ private void pageBundleBasedOnCountAndOffset( } @SuppressWarnings("unchecked") - private List findUnsupportedInclude( + protected List findUnsupportedInclude( List entries, List include, FhirVersionEnum fhirVersion) { switch (fhirVersion) { case DSTU3: diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r4/PackageVisitor.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r4/PackageVisitor.java index d03586e33..ac1ab2994 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r4/PackageVisitor.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r4/PackageVisitor.java @@ -7,16 +7,20 @@ import java.time.Instant; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.Collections; import java.util.Date; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; +import org.hl7.fhir.instance.model.api.IBaseHasExtensions; +import org.hl7.fhir.instance.model.api.IPrimitiveType; import org.hl7.fhir.r4.model.BooleanType; import org.hl7.fhir.r4.model.Bundle; import org.hl7.fhir.r4.model.Bundle.BundleEntryComponent; import org.hl7.fhir.r4.model.Bundle.BundleType; import org.hl7.fhir.r4.model.Coding; +import org.hl7.fhir.r4.model.DateTimeType; import org.hl7.fhir.r4.model.Endpoint; import org.hl7.fhir.r4.model.Extension; import org.hl7.fhir.r4.model.Library; @@ -237,34 +241,81 @@ public void handleValueSets( expansionParams = getExpansionParams(rootSpecificationLibrary, expansionReference.getReference()); } } - Parameters params = expansionParams; - bundleEntries.stream().forEach(entry -> { - if (entry.getResource().getResourceType().equals(ResourceType.ValueSet)) { - ValueSet valueSet = (ValueSet) entry.getResource(); - expandValueSet(valueSet, params, terminologyEndpoint); - } + var params = expansionParams; + var expandedList = new ArrayList(); + + var valueSets = bundleEntries.stream() + .filter(e -> e.hasResource() && e.getResource().fhirType().equals("ValueSet")) + .map(e -> (ValueSet) e.getResource()) + .collect(Collectors.toList()); + + valueSets.stream().forEach(valueSet -> { + expandValueSet(valueSet, params, terminologyEndpoint, valueSets, expandedList); }); } public void expandValueSet( - ValueSet valueSet, Parameters expansionParameters, Optional terminologyEndpoint) { + ValueSet valueSet, + Parameters expansionParameters, + Optional terminologyEndpoint, + List valueSets, + List expandedList) { + // Have we already expanded this ValueSet? + if (expandedList.contains(valueSet.getUrl())) { + // Nothing to do here + return; + } + // Gather the Terminology Service from the valueSet's authoritativeSourceUrl. - Extension authoritativeSource = valueSet.getExtensionByUrl(Constants.AUTHORITATIVE_SOURCE_URL); - String authoritativeSourceUrl = authoritativeSource != null && authoritativeSource.hasValue() - ? authoritativeSource.getValue().primitiveValue() - : valueSet.getUrl(); + var authoritativeSource = ((IBaseHasExtensions) valueSet) + .getExtension().stream() + .filter(e -> e.getUrl().equals(Constants.AUTHORITATIVE_SOURCE_URL)) + .findFirst() + .orElse(null); + @SuppressWarnings("unchecked") + var authoritativeSourceUrl = authoritativeSource == null + ? null + : ((IPrimitiveType) authoritativeSource.getValue()).getValueAsString(); + + // If terminologyEndpoint exists and we have no authoritativeSourceUrl or the authoritativeSourceUrl matches the + // terminologyEndpoint address then we will use the terminologyEndpoint for expansion + if (terminologyEndpoint.isPresent() + && (authoritativeSourceUrl == null + || authoritativeSourceUrl.equals( + terminologyEndpoint.get().getAddressElement().asStringValue()))) { + var username = terminologyEndpoint.get().getExtensionsByUrl(Constants.VSAC_USERNAME).stream() + .findFirst() + .map(ext -> ext.getValue().toString()) + .orElseThrow(() -> new UnprocessableEntityException( + "Cannot expand ValueSet without VSAC Username: " + valueSet.getId())); + var apiKey = terminologyEndpoint.get().getExtensionsByUrl(Constants.APIKEY).stream() + .findFirst() + .map(ext -> ext.getValue().toString()) + .orElseThrow(() -> new UnprocessableEntityException( + "Cannot expand ValueSet without VSAC API Key: " + valueSet.getId())); - ValueSet expandedValueSet; - if (!terminologyEndpoint.isPresent() && hasSimpleCompose(valueSet)) { + try { + var expandedValueSet = terminologyServerClient.expand( + valueSet, + terminologyEndpoint.get().getAddressElement().asStringValue(), + expansionParameters, + username, + apiKey); + valueSet.setExpansion(expandedValueSet.getExpansion()); + } catch (Exception ex) { + throw new UnprocessableEntityException( + "Terminology Server expansion failed for: " + valueSet.getId(), ex.getMessage()); + } + } + // Else if the ValueSet has a simple compose then we will perform naive expansion. + else if (hasSimpleCompose(valueSet)) { // Perform naive expansion independent of terminology servers. Copy all codes from compose into expansion. - ValueSet.ValueSetExpansionComponent expansion = new ValueSet.ValueSetExpansionComponent(); - expansion.setTimestamp(Date.from(Instant.now())); + var expansion = new ValueSet.ValueSetExpansionComponent(new DateTimeType(Date.from(Instant.now()))); ArrayList expansionParams = new ArrayList<>(); - ValueSet.ValueSetExpansionParameterComponent parameterNaive = - new ValueSet.ValueSetExpansionParameterComponent(); - parameterNaive.setName("naive"); - parameterNaive.setValue(new BooleanType(true)); + var parameterNaive = new ValueSet.ValueSetExpansionParameterComponent() + .setName("naive") + .setValue(new BooleanType(true)); expansionParams.add(parameterNaive); expansion.setParameter(expansionParams); @@ -277,44 +328,74 @@ public void expandValueSet( .setDisplay(code.getDisplay()); } valueSet.setExpansion(expansion); - } else { - String username; - String apiKey; - if (terminologyEndpoint.isPresent()) { - username = terminologyEndpoint.get().getExtensionsByUrl(Constants.VSAC_USERNAME).stream() + } + // Else if the ValueSet has a grouping compose then we will attempt to group. + else if (hasGroupingCompose(valueSet)) { + var expansion = new ValueSet.ValueSetExpansionComponent(new DateTimeType(Date.from(Instant.now()))); + var includes = valueSet.getCompose().getInclude().stream() + .map(i -> i.getValueSet()) + .flatMap(Collection::stream) + .map(c -> c.asStringValue()) + .distinct() + .collect(Collectors.toList()); + includes.forEach(reference -> { + // Grab the ValueSet + var split = reference.split("\\|"); + var url = split.length == 1 ? reference : split[0]; + var version = split.length == 1 ? null : split[1]; + var vs = valueSets.stream() + .filter(v -> v.getUrl().equals(url) + && (version == null || v.getVersion().equals(version))) .findFirst() - .map(ext -> ext.getValue().toString()) - .orElseThrow(() -> new UnprocessableEntityException( - "Cannot expand ValueSet without VSAC Username: " + valueSet.getId())); - apiKey = terminologyEndpoint.get().getExtensionsByUrl(Constants.APIKEY).stream() + .orElse(null); + // Expand the ValueSet if we haven't already + if (!expandedList.contains(url)) { + expandValueSet(vs, expansionParameters, terminologyEndpoint, valueSets, expandedList); + } + vs.getExpansion().getContains().forEach(code -> { + // Add the code if not already present + if (expansion.getContains().stream() + .noneMatch(expandedCode -> code.getSystem().equals(expandedCode.getSystem()) + && code.getCode().equals(expandedCode.getCode()) + && (!code.hasVersion() || code.getVersion().equals(expandedCode.getVersion())))) { + expansion.addContains(code); + } + }); + // If any included expansion is naive it makes the expansion naive + var naiveParam = vs.getExpansion().getParameter().stream() + .filter(p -> p.getName().equals("naive")) .findFirst() - .map(ext -> ext.getValue().toString()) - .orElseThrow(() -> new UnprocessableEntityException( - "Cannot expand ValueSet without VSAC API Key: " + valueSet.getId())); - } else { - throw new UnprocessableEntityException( - "Cannot expand ValueSet without a terminology server: " + valueSet.getId()); - } - - try { - expandedValueSet = terminologyServerClient.expand( - valueSet, authoritativeSourceUrl, expansionParameters, username, apiKey); - valueSet.setExpansion(expandedValueSet.getExpansion()); - } catch (Exception ex) { - throw new UnprocessableEntityException( - "Terminology Server expansion failed for: " + valueSet.getId(), ex.getMessage()); - } + .orElse(null); + if (naiveParam != null + && valueSet.getExpansion().getParameter().stream() + .noneMatch(p -> p.getName().equals("naive"))) { + expansion.addParameter(naiveParam); + } + }); + valueSet.setExpansion(expansion); + } else { + throw new UnprocessableEntityException( + "Cannot expand ValueSet without a terminology server: " + valueSet.getId()); } + expandedList.add(valueSet.getUrl()); } // A simple compose element of a ValueSet must have a compose without an exclude element. Each element of the - // include cannot have a filter, and must reference a ValueSet or have a system and enumerate concepts + // include cannot have a filter or reference a ValueSet and must have a system and enumerate concepts. protected boolean hasSimpleCompose(ValueSet valueSet) { return valueSet.hasCompose() && !valueSet.getCompose().hasExclude() && valueSet.getCompose().getInclude().stream() - .noneMatch(csc -> - csc.hasFilter() || (!csc.hasValueSet() && (!csc.hasSystem() && !csc.hasConcept()))); + .noneMatch( + csc -> csc.hasFilter() || csc.hasValueSet() || !csc.hasSystem() || !csc.hasConcept()); + } + + // A grouping compose element of a ValueSet must have a compose without an exclude element and each element of the + // include must reference a ValueSet. + protected boolean hasGroupingCompose(ValueSet valueSet) { + return valueSet.hasCompose() + && !valueSet.getCompose().hasExclude() + && valueSet.getCompose().getInclude().stream().noneMatch(csc -> !csc.hasValueSet() || csc.hasFilter()); } protected static Library getRootSpecificationLibrary(List bundleEntries) { diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactPackageVisitorTests.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactPackageVisitorTests.java index beff495df..54ffc5fca 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactPackageVisitorTests.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactPackageVisitorTests.java @@ -109,7 +109,7 @@ void packageOperation_should_fail_no_credentials() { .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); LibraryAdapter libraryAdapter = new AdapterFactory().createLibrary(library); - Parameters params = new Parameters(); + Parameters params = parameters(); UnprocessableEntityException maybeException = null; try { @@ -121,7 +121,6 @@ void packageOperation_should_fail_no_credentials() { } @Test - @Disabled("This test needs a ValueSet that cannot be naively expanded") void packageOperation_should_fail_credentials_missing_username() { Bundle loadedBundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); @@ -131,11 +130,10 @@ void packageOperation_should_fail_credentials_missing_username() { .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); LibraryAdapter libraryAdapter = new AdapterFactory().createLibrary(library); - Parameters params = new Parameters(); Endpoint terminologyEndpoint = new Endpoint(); terminologyEndpoint.addExtension(Constants.VSAC_USERNAME, new StringType(null)); terminologyEndpoint.addExtension(Constants.APIKEY, new StringType("some-api-key")); - params.addParameter().setName("terminologyEndpoint").setResource(terminologyEndpoint); + Parameters params = parameters(part("terminologyEndpoint", terminologyEndpoint)); UnprocessableEntityException maybeException = null; try { @@ -147,7 +145,6 @@ void packageOperation_should_fail_credentials_missing_username() { } @Test - @Disabled("This test needs a ValueSet that cannot be naively expanded") void packageOperation_should_fail_credentials_missing_apikey() { Bundle loadedBundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); @@ -157,11 +154,10 @@ void packageOperation_should_fail_credentials_missing_apikey() { .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); LibraryAdapter libraryAdapter = new AdapterFactory().createLibrary(library); - Parameters params = new Parameters(); Endpoint terminologyEndpoint = new Endpoint(); terminologyEndpoint.addExtension(Constants.VSAC_USERNAME, new StringType("someUsername")); terminologyEndpoint.addExtension(Constants.APIKEY, new StringType(null)); - params.addParameter().setName("terminologyEndpoint").setResource(terminologyEndpoint); + Parameters params = parameters(part("terminologyEndpoint", terminologyEndpoint)); UnprocessableEntityException maybeException = null; try { @@ -173,7 +169,6 @@ void packageOperation_should_fail_credentials_missing_apikey() { } @Test - @Disabled("This test needs a ValueSet that cannot be naively expanded") void packageOperation_should_fail_credentials_invalid() { Bundle loadedBundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); @@ -183,11 +178,10 @@ void packageOperation_should_fail_credentials_invalid() { .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); LibraryAdapter libraryAdapter = new AdapterFactory().createLibrary(library); - Parameters params = new Parameters(); Endpoint terminologyEndpoint = new Endpoint(); terminologyEndpoint.addExtension(Constants.VSAC_USERNAME, new StringType("someUsername")); terminologyEndpoint.addExtension(Constants.APIKEY, new StringType("some-api-key")); - params.addParameter().setName("terminologyEndpoint").setResource(terminologyEndpoint); + Parameters params = parameters(part("terminologyEndpoint", terminologyEndpoint)); UnprocessableEntityException maybeException = null; try { @@ -196,7 +190,6 @@ void packageOperation_should_fail_credentials_invalid() { maybeException = e; } assertTrue(maybeException.getMessage().contains("Terminology Server expansion failed for:")); - assertTrue(maybeException.getAdditionalMessages().stream().allMatch(msg -> msg.contains("HTTP 401"))); } @Test From c0c114d5bda7ec7475e7c5c4316b613cdbbe6b49 Mon Sep 17 00:00:00 2001 From: Brenin Rhodes Date: Thu, 27 Jun 2024 18:56:36 -0600 Subject: [PATCH 06/38] Extract expansion logic out of PackageVisitor --- .../cqf/fhir/cr/common/PackageProcessor.java | 4 +- .../cqf/fhir/utility/ExpandHelper.java | 118 +++++++++ .../opencds/cqf/fhir/utility/ValueSets.java | 88 ++++++- .../fhir/utility/adapter/AdapterFactory.java | 12 + .../fhir/utility/adapter/EndpointAdapter.java | 7 + .../adapter/KnowledgeArtifactAdapter.java | 3 - .../fhir/utility/adapter/LibraryAdapter.java | 2 + .../utility/adapter/ParametersAdapter.java | 5 + .../fhir/utility/adapter/ResourceAdapter.java | 14 + .../fhir/utility/adapter/ValueSetAdapter.java | 38 ++- .../utility/adapter/dstu3/AdapterFactory.java | 28 +- .../adapter/dstu3/EndpointAdapter.java | 36 +++ .../utility/adapter/dstu3/LibraryAdapter.java | 6 + .../adapter/dstu3/ParametersAdapter.java | 20 ++ .../adapter/dstu3/ResourceAdapter.java | 59 +++++ .../adapter/dstu3/ValueSetAdapter.java | 86 +++++- .../utility/adapter/r4/AdapterFactory.java | 34 ++- .../utility/adapter/r4/EndpointAdapter.java | 36 +++ .../utility/adapter/r4/LibraryAdapter.java | 6 + .../utility/adapter/r4/ParametersAdapter.java | 20 ++ .../utility/adapter/r4/ResourceAdapter.java | 59 +++++ .../utility/adapter/r4/ValueSetAdapter.java | 88 ++++++- .../utility/adapter/r5/AdapterFactory.java | 28 +- .../utility/adapter/r5/EndpointAdapter.java | 36 +++ .../adapter/r5/KnowledgeArtifactAdapter.java | 7 - .../utility/adapter/r5/LibraryAdapter.java | 6 + .../utility/adapter/r5/ParametersAdapter.java | 20 ++ .../utility/adapter/r5/ResourceAdapter.java | 59 +++++ .../utility/adapter/r5/ValueSetAdapter.java | 86 +++++- .../client/TerminologyServerClient.java | 132 +++------- .../fhir/utility/visitor/PackageVisitor.java | 121 ++++++--- .../fhir/utility/visitor/VisitorHelper.java | 5 + .../utility/visitor/dstu3/PackageVisitor.java | 154 ----------- .../utility/visitor/r4/PackageVisitor.java | 247 +----------------- .../utility/visitor/r5/PackageVisitor.java | 154 ----------- .../dstu3/PlanDefinitionAdapterTest.java | 5 +- .../adapter/r4/PlanDefinitionAdapterTest.java | 5 +- .../adapter/r5/PlanDefinitionAdapterTest.java | 5 +- .../client/TerminologyServerClientTest.java | 89 +++++-- .../KnowledgeArtifactPackageVisitorTests.java | 22 +- .../KnowledgeArtifactPackageVisitorTests.java | 28 +- .../KnowledgeArtifactPackageVisitorTests.java | 22 +- 42 files changed, 1199 insertions(+), 801 deletions(-) create mode 100644 cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/ExpandHelper.java create mode 100644 cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/EndpointAdapter.java create mode 100644 cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/EndpointAdapter.java create mode 100644 cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/EndpointAdapter.java create mode 100644 cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/EndpointAdapter.java diff --git a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/common/PackageProcessor.java b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/common/PackageProcessor.java index 8ef54d44f..dc191e03a 100644 --- a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/common/PackageProcessor.java +++ b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/common/PackageProcessor.java @@ -19,8 +19,8 @@ public class PackageProcessor implements IPackageProcessor { public PackageProcessor(Repository repository) { this.repository = repository; - this.fhirVersion = repository.fhirContext().getVersion().getVersion(); - packageVisitor = new PackageVisitor(); + this.fhirVersion = this.repository.fhirContext().getVersion().getVersion(); + packageVisitor = new PackageVisitor(this.repository.fhirContext()); } @Override diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/ExpandHelper.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/ExpandHelper.java new file mode 100644 index 000000000..b4c7ee298 --- /dev/null +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/ExpandHelper.java @@ -0,0 +1,118 @@ +package org.opencds.cqf.fhir.utility; + +import static org.opencds.cqf.fhir.utility.ValueSets.addCodeToExpansion; +import static org.opencds.cqf.fhir.utility.ValueSets.addParameterToExpansion; +import static org.opencds.cqf.fhir.utility.ValueSets.getCodesInExpansion; +import static org.opencds.cqf.fhir.utility.adapter.AdapterFactory.createAdapterForResource; + +import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; +import java.util.List; +import java.util.Optional; +import org.apache.commons.lang3.StringUtils; +import org.hl7.fhir.instance.model.api.IPrimitiveType; +import org.opencds.cqf.fhir.utility.adapter.EndpointAdapter; +import org.opencds.cqf.fhir.utility.adapter.ParametersAdapter; +import org.opencds.cqf.fhir.utility.adapter.ValueSetAdapter; +import org.opencds.cqf.fhir.utility.client.TerminologyServerClient; + +public class ExpandHelper { + private final FhirContext fhirContext; + private final TerminologyServerClient terminologyServerClient; + + public ExpandHelper(FhirContext fhirContext, TerminologyServerClient server) { + this.fhirContext = fhirContext; + terminologyServerClient = server; + } + + public void expandValueSet( + ValueSetAdapter valueSet, + ParametersAdapter expansionParameters, + Optional terminologyEndpoint, + List valueSets, + List expandedList) { + // Have we already expanded this ValueSet? + if (expandedList.contains(valueSet.getUrl())) { + // Nothing to do here + return; + } + + // Gather the Terminology Service from the valueSet's authoritativeSourceUrl. + var authoritativeSource = valueSet.getExtension().stream() + .filter(e -> e.getUrl().equals(Constants.AUTHORITATIVE_SOURCE_URL)) + .findFirst() + .orElse(null); + @SuppressWarnings("unchecked") + var authoritativeSourceUrl = authoritativeSource == null + ? null + : ((IPrimitiveType) authoritativeSource.getValue()).getValueAsString(); + + // If terminologyEndpoint exists and we have no authoritativeSourceUrl or the authoritativeSourceUrl matches the + // terminologyEndpoint address then we will use the terminologyEndpoint for expansion + if (terminologyEndpoint.isPresent() + && (authoritativeSourceUrl == null + || authoritativeSourceUrl.equals( + terminologyEndpoint.get().getAddress()))) { + try { + var expandedValueSet = (ValueSetAdapter) createAdapterForResource( + terminologyServerClient.expand(valueSet, terminologyEndpoint.get(), expansionParameters)); + valueSet.setExpansion(expandedValueSet.getExpansion()); + } catch (Exception ex) { + throw new UnprocessableEntityException(String.format( + "Terminology Server expansion failed for ValueSet (%s): %s", + valueSet.getId(), ex.getMessage())); + } + } + // Else if the ValueSet has a simple compose then we will perform naive expansion. + else if (valueSet.hasSimpleCompose()) { + valueSet.naiveExpand(); + } + // Else if the ValueSet has a grouping compose then we will attempt to group. + else if (valueSet.hasGroupingCompose()) { + var expansion = valueSet.newExpansion(); + var includes = valueSet.getValueSetIncludes(); + includes.forEach(reference -> { + // Grab the ValueSet + var split = reference.split("\\|"); + var url = split.length == 1 ? reference : split[0]; + var version = split.length == 1 ? null : split[1]; + var vs = valueSets.stream() + .filter(v -> v.getUrl().equals(url) + && (version == null || v.getVersion().equals(version))) + .findFirst() + .orElse(null); + // Expand the ValueSet if we haven't already + if (!expandedList.contains(url)) { + expandValueSet(vs, expansionParameters, terminologyEndpoint, valueSets, expandedList); + } + getCodesInExpansion(fhirContext, vs.get()).forEach(code -> { + // Add the code if not already present + var existingCodes = getCodesInExpansion(fhirContext, expansion); + if (existingCodes != null + && existingCodes.stream() + .noneMatch(expandedCode -> code.getSystem().equals(expandedCode.getSystem()) + && code.getCode().equals(expandedCode.getCode()) + && (StringUtils.isEmpty(code.getVersion()) + || code.getVersion().equals(expandedCode.getVersion())))) { + try { + addCodeToExpansion(fhirContext, expansion, code); + } catch (Exception ex) { + throw new UnprocessableEntityException(String.format( + "Encountered exception attempting to expand ValueSet %s: %s", + vs.get().getId(), ex.getMessage())); + } + } + }); + // If any included expansion is naive it makes the expansion naive + if (vs.hasNaiveParameter() && !valueSet.hasNaiveParameter()) { + addParameterToExpansion(fhirContext, expansion, valueSet.createNaiveParameter()); + } + }); + valueSet.setExpansion(expansion); + } else { + throw new UnprocessableEntityException( + "Cannot expand ValueSet without a terminology server: " + valueSet.getId()); + } + expandedList.add(valueSet.getUrl()); + } +} diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/ValueSets.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/ValueSets.java index aa00eaaaf..d7558ade9 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/ValueSets.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/ValueSets.java @@ -1,5 +1,7 @@ package org.opencds.cqf.fhir.utility; +import static org.opencds.cqf.fhir.utility.Resources.newBackboneElement; + import ca.uhn.fhir.context.BaseRuntimeChildDefinition; import ca.uhn.fhir.context.BaseRuntimeChildDefinition.IAccessor; import ca.uhn.fhir.context.FhirContext; @@ -7,9 +9,11 @@ import ca.uhn.fhir.context.RuntimeResourceBlockDefinition; import ca.uhn.fhir.context.RuntimeResourceDefinition; import ca.uhn.fhir.fhirpath.IFhirPath; +import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.List; import org.hl7.fhir.instance.model.api.IBase; +import org.hl7.fhir.instance.model.api.IBaseBackboneElement; import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.instance.model.api.IPrimitiveType; import org.opencds.cqf.cql.engine.runtime.Code; @@ -172,9 +176,7 @@ public static IBase getExpansion(FhirContext fhirContext, IBaseResource valueSet return expansion.get(0); } - public static List getContains(FhirContext fhirContext, IBaseResource valueSet) { - IBase expansion = getExpansion(fhirContext, valueSet); - + public static List getContainsInExpansion(FhirContext fhirContext, IBase expansion) { if (expansion == null) { return null; } @@ -190,6 +192,10 @@ public static List getContains(FhirContext fhirContext, IBaseResource val return contains; } + public static List getContains(FhirContext fhirContext, IBaseResource valueSet) { + return getContainsInExpansion(fhirContext, getExpansion(fhirContext, valueSet)); + } + public static List getCodesInCompose(FhirContext fhirContext, IBaseResource valueSet) { List includes = getIncludes(fhirContext, valueSet); @@ -228,9 +234,7 @@ public static List getCodesInCompose(FhirContext fhirContext, IBaseResourc return codes; } - public static List getCodesInExpansion(FhirContext fhirContext, IBaseResource valueSet) { - List contains = getContains(fhirContext, valueSet); - + public static List getCodesInContains(FhirContext fhirContext, List contains) { if (contains == null) { return null; } @@ -258,6 +262,67 @@ public static List getCodesInExpansion(FhirContext fhirContext, IBaseResou return codes; } + public static List getCodesInExpansion(FhirContext fhirContext, IBase expansion) { + return getCodesInContains(fhirContext, getContainsInExpansion(fhirContext, expansion)); + } + + public static List getCodesInExpansion(FhirContext fhirContext, IBaseResource valueSet) { + return getCodesInContains(fhirContext, getContains(fhirContext, valueSet)); + } + + @SuppressWarnings("unchecked") + public static void addCodeToExpansion(FhirContext fhirContext, IBase expansion, Code code) + throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, + NoSuchMethodException, SecurityException { + var containsDef = getContainsDefinition(fhirContext); + var systemDef = getSystemDefinition(fhirContext); + var codeDef = getCodeDefinition(fhirContext); + var displayDef = getDisplayDefinition(fhirContext); + var versionDef = getVersionDefinition(fhirContext); + var newCode = newBackboneElement((Class) + containsDef.getChildByName("contains").getImplementingClass()); + systemDef + .getMutator() + .addValue( + newCode, + systemDef + .getChildByName("system") + .getImplementingClass() + .getConstructor(String.class) + .newInstance(code.getSystem())); + codeDef.getMutator() + .addValue( + newCode, + codeDef.getChildByName("code") + .getImplementingClass() + .getConstructor(String.class) + .newInstance(code.getCode())); + displayDef + .getMutator() + .addValue( + newCode, + displayDef + .getChildByName("display") + .getImplementingClass() + .getConstructor(String.class) + .newInstance(code.getDisplay())); + versionDef + .getMutator() + .addValue( + newCode, + versionDef + .getChildByName("version") + .getImplementingClass() + .getConstructor(String.class) + .newInstance(code.getVersion())); + containsDef.getMutator().addValue(expansion, newCode); + } + + public static void addParameterToExpansion( + FhirContext fhirContext, IBase expansion, IBaseBackboneElement parameter) { + getParameterDefinition(fhirContext).getMutator().addValue(expansion, parameter); + } + public static String getUrl(FhirContext fhirContext, IBaseResource valueSet) { BaseRuntimeChildDefinition urlDef = getUrlDefinition(fhirContext); return getStringValueFromPrimitiveAccessor(valueSet, urlDef.getAccessor()); @@ -370,6 +435,17 @@ private static BaseRuntimeChildDefinition getExpansionDefinition(FhirContext fhi return def.getChildByName("expansion"); } + private static BaseRuntimeChildDefinition getParameterDefinition(FhirContext fhirContext) { + BaseRuntimeChildDefinition expansionChild = getExpansionDefinition(fhirContext); + RuntimeResourceBlockDefinition expansionBlockChild = + (RuntimeResourceBlockDefinition) expansionChild.getChildByName("expansion"); + return getParameterDefinition(expansionBlockChild); + } + + private static BaseRuntimeChildDefinition getParameterDefinition(RuntimeResourceBlockDefinition expansionChild) { + return expansionChild.getChildByName("parameter"); + } + private static BaseRuntimeChildDefinition getContainsDefinition(FhirContext fhirContext) { BaseRuntimeChildDefinition expansionChild = getExpansionDefinition(fhirContext); RuntimeResourceBlockDefinition expansionBlockChild = diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/AdapterFactory.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/AdapterFactory.java index 6bf1ab554..7e7a23aeb 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/AdapterFactory.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/AdapterFactory.java @@ -24,6 +24,10 @@ public static AdapterFactory forFhirVersion(FhirVersionEnum fhirVersion) { } } + public static ResourceAdapter createAdapterForResource(IBaseResource resource) { + return forFhirVersion(resource.getStructureFhirVersionEnum()).createResource(resource); + } + /** * Creates an adapter that exposes common resource operations across multiple versions of FHIR * @@ -73,4 +77,12 @@ public static AdapterFactory forFhirVersion(FhirVersionEnum fhirVersion) { */ public ParametersParameterComponentAdapter createParametersParameters( IBaseBackboneElement parametersParametersComponent); + + /** + * Creates an adapter that exposes common Attachment operations across multiple versions of FHIR + * + * @param endpoint a FHIR Endpoint Resource + * @return an adapter exposing common api calls + */ + public EndpointAdapter createEndpoint(IBaseResource endpoint); } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/EndpointAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/EndpointAdapter.java new file mode 100644 index 000000000..db6098131 --- /dev/null +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/EndpointAdapter.java @@ -0,0 +1,7 @@ +package org.opencds.cqf.fhir.utility.adapter; + +public interface EndpointAdapter extends ResourceAdapter { + public String getAddress(); + + public EndpointAdapter setAddress(String address); +} diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/KnowledgeArtifactAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/KnowledgeArtifactAdapter.java index e4359de1f..a08d12f7f 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/KnowledgeArtifactAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/KnowledgeArtifactAdapter.java @@ -10,7 +10,6 @@ import org.hl7.fhir.dstu3.model.Reference; import org.hl7.fhir.instance.model.api.IBase; import org.hl7.fhir.instance.model.api.IBaseBundle; -import org.hl7.fhir.instance.model.api.IBaseExtension; import org.hl7.fhir.instance.model.api.IBaseHasExtensions; import org.hl7.fhir.instance.model.api.IBaseParameters; import org.hl7.fhir.instance.model.api.IBaseResource; @@ -74,8 +73,6 @@ default void setId(IIdType id) { boolean getExperimental(); - void setExtension(List> extensions); - @SuppressWarnings("unchecked") static T newRelatedArtifact( FhirVersionEnum version, String type, String reference) { diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/LibraryAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/LibraryAdapter.java index 5013f6ab8..ce66042d3 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/LibraryAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/LibraryAdapter.java @@ -29,4 +29,6 @@ public interface LibraryAdapter extends KnowledgeArtifactAdapter { LibraryAdapter addDataRequirement(ICompositeType dataRequirement); LibraryAdapter setDataRequirements(List dataRequirements); + + List getUseContext(); } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/ParametersAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/ParametersAdapter.java index c83090ffc..b2fe2ae57 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/ParametersAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/ParametersAdapter.java @@ -1,6 +1,7 @@ package org.opencds.cqf.fhir.utility.adapter; import java.util.List; +import org.hl7.fhir.instance.model.api.IBase; import org.hl7.fhir.instance.model.api.IBaseBackboneElement; import org.hl7.fhir.instance.model.api.IBaseDatatype; import org.hl7.fhir.instance.model.api.IBaseResource; @@ -17,5 +18,9 @@ public interface ParametersAdapter extends ResourceAdapter { public void setParameter(List parametersParameterComponents); + public void addParameter(IBase parameter); + + public void addParameter(String name, IBase value); + public IBaseBackboneElement addParameter(); } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/ResourceAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/ResourceAdapter.java index c2e6f2b13..0f13e232a 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/ResourceAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/ResourceAdapter.java @@ -1,7 +1,9 @@ package org.opencds.cqf.fhir.utility.adapter; +import java.util.List; import org.hl7.fhir.exceptions.FHIRException; import org.hl7.fhir.instance.model.api.IBase; +import org.hl7.fhir.instance.model.api.IBaseExtension; import org.hl7.fhir.instance.model.api.IBaseResource; public interface ResourceAdapter extends Adapter { @@ -29,4 +31,16 @@ public interface ResourceAdapter extends Adapter { public boolean equalsDeep(IBase other); public boolean equalsShallow(IBase other); + + public void setExtension(List> extensions); + + public > void addExtension(T extension); + + public List> getExtension(); + + public > T getExtensionByUrl(String url); + + public List> getExtensionsByUrl(String url); + + public List getContained(); } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/ValueSetAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/ValueSetAdapter.java index 4197ff380..366d81a8f 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/ValueSetAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/ValueSetAdapter.java @@ -1,6 +1,42 @@ package org.opencds.cqf.fhir.utility.adapter; +import java.util.List; +import org.hl7.fhir.instance.model.api.IBaseBackboneElement; + /** * This interface exposes common functionality across all FHIR ValueSet versions. */ -public interface ValueSetAdapter extends KnowledgeArtifactAdapter {} +public interface ValueSetAdapter extends KnowledgeArtifactAdapter { + public void setExpansion(T expansion); + + public T getExpansion(); + + public T newExpansion(); + + public List getValueSetIncludes(); + + /** + * A simple compose element of a ValueSet must have a compose without an exclude element. Each element of the + * include cannot have a filter or reference a ValueSet and must have a system and enumerate concepts. + * + * @return boolean + */ + public boolean hasSimpleCompose(); + + /** + * A grouping compose element of a ValueSet must have a compose without an exclude element and each element of the + * include must reference a ValueSet. + * + * @return boolean + */ + public boolean hasGroupingCompose(); + + /** + * Performs a naive expansion on the ValueSet by collecting all codes within the compose. Can only be performed on a ValueSet with a simple compose. + */ + public void naiveExpand(); + + public boolean hasNaiveParameter(); + + public T createNaiveParameter(); +} diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/AdapterFactory.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/AdapterFactory.java index 17924e898..5eb2718e5 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/AdapterFactory.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/AdapterFactory.java @@ -1,8 +1,10 @@ package org.opencds.cqf.fhir.utility.adapter.dstu3; import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; +import org.hl7.fhir.dstu3.model.Endpoint; import org.hl7.fhir.dstu3.model.Library; import org.hl7.fhir.dstu3.model.MetadataResource; +import org.hl7.fhir.dstu3.model.Parameters; import org.hl7.fhir.dstu3.model.PlanDefinition; import org.hl7.fhir.dstu3.model.Questionnaire; import org.hl7.fhir.dstu3.model.StructureDefinition; @@ -12,7 +14,6 @@ import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.instance.model.api.ICompositeType; import org.hl7.fhir.instance.model.api.IDomainResource; -import org.opencds.cqf.fhir.utility.adapter.LibraryAdapter; public class AdapterFactory implements org.opencds.cqf.fhir.utility.adapter.AdapterFactory { @@ -20,6 +21,10 @@ public class AdapterFactory implements org.opencds.cqf.fhir.utility.adapter.Adap public org.opencds.cqf.fhir.utility.adapter.ResourceAdapter createResource(IBaseResource resource) { if (resource instanceof MetadataResource) { return createKnowledgeArtifactAdapter((MetadataResource) resource); + } else if (resource instanceof Endpoint) { + return createEndpoint(resource); + } else if (resource instanceof Parameters) { + return createParameters((Parameters) resource); } else { return new ResourceAdapter(resource); } @@ -32,18 +37,16 @@ public org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter createKnowl if (resource instanceof Library) { adapter = createLibrary(resource); } else if (resource instanceof PlanDefinition) { - adapter = new org.opencds.cqf.fhir.utility.adapter.dstu3.PlanDefinitionAdapter((PlanDefinition) resource); + adapter = new PlanDefinitionAdapter((PlanDefinition) resource); } else if (resource instanceof Questionnaire) { - adapter = new org.opencds.cqf.fhir.utility.adapter.dstu3.QuestionnaireAdapter((Questionnaire) resource); + adapter = new QuestionnaireAdapter((Questionnaire) resource); } else if (resource instanceof StructureDefinition) { - adapter = new org.opencds.cqf.fhir.utility.adapter.dstu3.StructureDefinitionAdapter( - (StructureDefinition) resource); + adapter = new StructureDefinitionAdapter((StructureDefinition) resource); } else if (resource instanceof ValueSet) { - adapter = new org.opencds.cqf.fhir.utility.adapter.dstu3.ValueSetAdapter((ValueSet) resource); + adapter = new ValueSetAdapter((ValueSet) resource); } else { if (resource instanceof MetadataResource) { - adapter = new org.opencds.cqf.fhir.utility.adapter.dstu3.KnowledgeArtifactAdapter( - (MetadataResource) resource); + adapter = new KnowledgeArtifactAdapter((MetadataResource) resource); } else { throw new UnprocessableEntityException( String.format("Resource must be instance of %s", MetadataResource.class.getName())); @@ -53,8 +56,8 @@ public org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter createKnowl } @Override - public LibraryAdapter createLibrary(IBaseResource library) { - return new org.opencds.cqf.fhir.utility.adapter.dstu3.LibraryAdapter((IDomainResource) library); + public org.opencds.cqf.fhir.utility.adapter.LibraryAdapter createLibrary(IBaseResource library) { + return new LibraryAdapter((IDomainResource) library); } @Override @@ -72,4 +75,9 @@ public org.opencds.cqf.fhir.utility.adapter.ParametersParameterComponentAdapter IBaseBackboneElement parametersParametersComponent) { return new ParametersParameterComponentAdapter(parametersParametersComponent); } + + @Override + public org.opencds.cqf.fhir.utility.adapter.EndpointAdapter createEndpoint(IBaseResource endpoint) { + return new EndpointAdapter(endpoint); + } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/EndpointAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/EndpointAdapter.java new file mode 100644 index 000000000..09f267327 --- /dev/null +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/EndpointAdapter.java @@ -0,0 +1,36 @@ +package org.opencds.cqf.fhir.utility.adapter.dstu3; + +import org.hl7.fhir.dstu3.model.Endpoint; +import org.hl7.fhir.instance.model.api.IBaseResource; + +public class EndpointAdapter extends ResourceAdapter implements org.opencds.cqf.fhir.utility.adapter.EndpointAdapter { + private final Endpoint endpoint; + + public EndpointAdapter(IBaseResource endpoint) { + super(endpoint); + if (!(endpoint instanceof Endpoint)) { + throw new IllegalArgumentException("resource passed as endpoint argument is not an Endpoint resource"); + } + this.endpoint = (Endpoint) endpoint; + } + + public EndpointAdapter(Endpoint endpoint) { + super(endpoint); + this.endpoint = endpoint; + } + + public Endpoint getEndpoint() { + return endpoint; + } + + @Override + public String getAddress() { + return endpoint.getAddress(); + } + + @Override + public EndpointAdapter setAddress(String address) { + endpoint.setAddress(address); + return this; + } +} diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/LibraryAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/LibraryAdapter.java index 2ca8f957c..eb19dc898 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/LibraryAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/LibraryAdapter.java @@ -16,6 +16,7 @@ import org.hl7.fhir.dstu3.model.Period; import org.hl7.fhir.dstu3.model.RelatedArtifact; import org.hl7.fhir.dstu3.model.RelatedArtifact.RelatedArtifactType; +import org.hl7.fhir.dstu3.model.UsageContext; import org.hl7.fhir.exceptions.FHIRException; import org.hl7.fhir.instance.model.api.IBaseExtension; import org.hl7.fhir.instance.model.api.IBaseHasExtensions; @@ -310,4 +311,9 @@ public LibraryAdapter setDataRequirements(List dat dataRequirements.stream().map(dr -> (DataRequirement) dr).collect(Collectors.toList())); return this; } + + @Override + public List getUseContext() { + return library.getUseContext(); + } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ParametersAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ParametersAdapter.java index 4af59fa6c..e55e85374 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ParametersAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ParametersAdapter.java @@ -5,6 +5,7 @@ import org.hl7.fhir.dstu3.model.Parameters; import org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent; import org.hl7.fhir.dstu3.model.Type; +import org.hl7.fhir.instance.model.api.IBase; import org.hl7.fhir.instance.model.api.IBaseBackboneElement; import org.hl7.fhir.instance.model.api.IBaseResource; @@ -39,6 +40,25 @@ public void setParameter(List parametersParameterComponent .collect(Collectors.toList())); } + @Override + public void addParameter(String name, IBase value) { + if (value instanceof Type) { + getParameters().addParameter(addParameter().setName(name).setValue((Type) value)); + } else { + throw new IllegalArgumentException("element passed as value argument is not a valid type"); + } + } + + @Override + public void addParameter(IBase parameter) { + if (parameter instanceof ParametersParameterComponent) { + getParameters().addParameter((ParametersParameterComponent) parameter); + } else { + throw new IllegalArgumentException( + "element passed as parameter argument is not a valid parameter component"); + } + } + @Override public ParametersParameterComponent addParameter() { return this.getParameters().addParameter(); diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapter.java index a6d75fa60..6047ea620 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapter.java @@ -1,10 +1,19 @@ package org.opencds.cqf.fhir.utility.adapter.dstu3; +import static java.util.Optional.ofNullable; + import ca.uhn.fhir.context.FhirVersionEnum; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; import org.hl7.fhir.dstu3.model.Base; +import org.hl7.fhir.dstu3.model.DomainResource; +import org.hl7.fhir.dstu3.model.Extension; import org.hl7.fhir.dstu3.model.Resource; import org.hl7.fhir.exceptions.FHIRException; import org.hl7.fhir.instance.model.api.IBase; +import org.hl7.fhir.instance.model.api.IBaseExtension; import org.hl7.fhir.instance.model.api.IBaseResource; class ResourceAdapter implements org.opencds.cqf.fhir.utility.adapter.ResourceAdapter { @@ -27,6 +36,14 @@ protected Resource getResource() { return this.resource; } + protected boolean isDomainResource() { + return getDomainResource().isPresent(); + } + + protected Optional getDomainResource() { + return ofNullable(resource instanceof DomainResource ? (DomainResource) resource : null); + } + public IBaseResource get() { return this.resource; } @@ -95,4 +112,46 @@ public boolean equalsDeep(IBase other) { public boolean equalsShallow(IBase other) { return this.getResource().equalsShallow((Base) other); } + + @Override + public void setExtension(List> extensions) { + getDomainResource() + .ifPresent(r -> r.setExtension( + extensions.stream().map(e -> (Extension) e).collect(Collectors.toList()))); + } + + @Override + public > void addExtension(T extension) { + getDomainResource().ifPresent(r -> r.addExtension((Extension) extension)); + } + + @Override + public List getExtension() { + return isDomainResource() ? getDomainResource().get().getExtension() : new ArrayList<>(); + } + + @SuppressWarnings("unchecked") + @Override + public Extension getExtensionByUrl(String url) { + return isDomainResource() + ? getDomainResource().get().getExtension().stream() + .filter(e -> e.getUrl().equals(url)) + .findFirst() + .orElse(null) + : null; + } + + @Override + public List getExtensionsByUrl(String url) { + return isDomainResource() + ? getDomainResource().get().getExtension().stream() + .filter(e -> e.getUrl().equals(url)) + .collect(Collectors.toList()) + : new ArrayList<>(); + } + + @Override + public List getContained() { + return isDomainResource() ? getDomainResource().get().getContained() : new ArrayList<>(); + } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ValueSetAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ValueSetAdapter.java index 62a69d0d0..fc6763e26 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ValueSetAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ValueSetAdapter.java @@ -1,20 +1,26 @@ package org.opencds.cqf.fhir.utility.adapter.dstu3; +import static org.opencds.cqf.fhir.utility.ValueSets.getCodesInCompose; + +import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; +import java.time.Instant; import java.util.ArrayList; +import java.util.Collection; import java.util.Date; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; +import org.hl7.fhir.dstu3.model.BooleanType; import org.hl7.fhir.dstu3.model.DateTimeType; import org.hl7.fhir.dstu3.model.Enumerations.PublicationStatus; -import org.hl7.fhir.dstu3.model.Extension; import org.hl7.fhir.dstu3.model.Period; import org.hl7.fhir.dstu3.model.RelatedArtifact; import org.hl7.fhir.dstu3.model.RelatedArtifact.RelatedArtifactType; import org.hl7.fhir.dstu3.model.ValueSet; +import org.hl7.fhir.dstu3.model.ValueSet.ValueSetExpansionComponent; import org.hl7.fhir.exceptions.FHIRException; -import org.hl7.fhir.instance.model.api.IBaseExtension; +import org.hl7.fhir.instance.model.api.IBaseBackboneElement; import org.hl7.fhir.instance.model.api.IBaseHasExtensions; import org.hl7.fhir.instance.model.api.ICompositeType; import org.hl7.fhir.instance.model.api.IDomainResource; @@ -252,8 +258,80 @@ public boolean getExperimental() { return this.getValueSet().getExperimental(); } + // @Override + // public void setExtension(List> extensions) { + // this.get().setExtension(extensions.stream().map(e -> (Extension) e).collect(Collectors.toList())); + // } + + @Override + public void setExpansion(T expansion) { + valueSet.setExpansion((ValueSetExpansionComponent) expansion); + } + + @SuppressWarnings("unchecked") + @Override + public ValueSetExpansionComponent getExpansion() { + return valueSet.getExpansion(); + } + + @SuppressWarnings("unchecked") + @Override + public ValueSetExpansionComponent newExpansion() { + return new ValueSetExpansionComponent().setTimestamp(Date.from(Instant.now())); + } + @Override - public void setExtension(List> extensions) { - this.get().setExtension(extensions.stream().map(e -> (Extension) e).collect(Collectors.toList())); + public List getValueSetIncludes() { + return valueSet.getCompose().getInclude().stream() + .map(i -> i.getValueSet()) + .flatMap(Collection::stream) + .map(c -> c.asStringValue()) + .distinct() + .collect(Collectors.toList()); + } + + @Override + public boolean hasSimpleCompose() { + return valueSet.hasCompose() + && !valueSet.getCompose().hasExclude() + && valueSet.getCompose().getInclude().stream() + .noneMatch( + csc -> csc.hasFilter() || csc.hasValueSet() || !csc.hasSystem() || !csc.hasConcept()); + } + + @Override + public boolean hasGroupingCompose() { + return valueSet.hasCompose() + && !valueSet.getCompose().hasExclude() + && valueSet.getCompose().getInclude().stream().noneMatch(csc -> !csc.hasValueSet() || csc.hasFilter()); + } + + @Override + public boolean hasNaiveParameter() { + return valueSet.getExpansion().getParameter().stream() + .anyMatch(p -> p.getName().equals("naive")); + } + + @SuppressWarnings("unchecked") + @Override + public ValueSet.ValueSetExpansionParameterComponent createNaiveParameter() { + return new ValueSet.ValueSetExpansionParameterComponent() + .setName("naive") + .setValue(new BooleanType(true)); + } + + @Override + public void naiveExpand() { + var expansion = newExpansion().addParameter(createNaiveParameter()); + + for (var code : getCodesInCompose(FhirContext.forDstu3Cached(), valueSet)) { + expansion + .addContains() + .setCode(code.getCode()) + .setSystem(code.getSystem()) + .setVersion(code.getVersion()) + .setDisplay(code.getDisplay()); + } + valueSet.setExpansion(expansion); } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/AdapterFactory.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/AdapterFactory.java index 56c4983f5..43607c983 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/AdapterFactory.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/AdapterFactory.java @@ -6,14 +6,14 @@ import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.instance.model.api.ICompositeType; import org.hl7.fhir.instance.model.api.IDomainResource; +import org.hl7.fhir.r4.model.Endpoint; import org.hl7.fhir.r4.model.Library; import org.hl7.fhir.r4.model.MetadataResource; +import org.hl7.fhir.r4.model.Parameters; import org.hl7.fhir.r4.model.PlanDefinition; import org.hl7.fhir.r4.model.Questionnaire; import org.hl7.fhir.r4.model.StructureDefinition; import org.hl7.fhir.r4.model.ValueSet; -import org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter; -import org.opencds.cqf.fhir.utility.adapter.LibraryAdapter; public class AdapterFactory implements org.opencds.cqf.fhir.utility.adapter.AdapterFactory { @@ -21,29 +21,32 @@ public class AdapterFactory implements org.opencds.cqf.fhir.utility.adapter.Adap public org.opencds.cqf.fhir.utility.adapter.ResourceAdapter createResource(IBaseResource resource) { if (resource instanceof MetadataResource) { return createKnowledgeArtifactAdapter((IDomainResource) resource); + } else if (resource instanceof Endpoint) { + return createEndpoint(resource); + } else if (resource instanceof Parameters) { + return createParameters((Parameters) resource); } else { return new ResourceAdapter(resource); } } @Override - public KnowledgeArtifactAdapter createKnowledgeArtifactAdapter(IDomainResource resource) { - KnowledgeArtifactAdapter adapter; + public org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter createKnowledgeArtifactAdapter( + IDomainResource resource) { + org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter adapter; if (resource instanceof Library) { adapter = createLibrary(resource); } else if (resource instanceof PlanDefinition) { - adapter = new org.opencds.cqf.fhir.utility.adapter.r4.PlanDefinitionAdapter((PlanDefinition) resource); + adapter = new PlanDefinitionAdapter((PlanDefinition) resource); } else if (resource instanceof Questionnaire) { - adapter = new org.opencds.cqf.fhir.utility.adapter.r4.QuestionnaireAdapter((Questionnaire) resource); + adapter = new QuestionnaireAdapter((Questionnaire) resource); } else if (resource instanceof StructureDefinition) { - adapter = new org.opencds.cqf.fhir.utility.adapter.r4.StructureDefinitionAdapter( - (StructureDefinition) resource); + adapter = new StructureDefinitionAdapter((StructureDefinition) resource); } else if (resource instanceof ValueSet) { - adapter = new org.opencds.cqf.fhir.utility.adapter.r4.ValueSetAdapter((ValueSet) resource); + adapter = new ValueSetAdapter((ValueSet) resource); } else { if (resource != null && resource instanceof MetadataResource) { - adapter = new org.opencds.cqf.fhir.utility.adapter.r4.KnowledgeArtifactAdapter( - (MetadataResource) resource); + adapter = new KnowledgeArtifactAdapter((MetadataResource) resource); } else { throw new UnprocessableEntityException( String.format("Resource must be instance of %s", MetadataResource.class.getName())); @@ -53,8 +56,8 @@ public KnowledgeArtifactAdapter createKnowledgeArtifactAdapter(IDomainResource r } @Override - public LibraryAdapter createLibrary(IBaseResource library) { - return new org.opencds.cqf.fhir.utility.adapter.r4.LibraryAdapter((IDomainResource) library); + public org.opencds.cqf.fhir.utility.adapter.LibraryAdapter createLibrary(IBaseResource library) { + return new LibraryAdapter((IDomainResource) library); } @Override @@ -72,4 +75,9 @@ public org.opencds.cqf.fhir.utility.adapter.ParametersParameterComponentAdapter IBaseBackboneElement parametersParametersComponent) { return new ParametersParameterComponentAdapter(parametersParametersComponent); } + + @Override + public org.opencds.cqf.fhir.utility.adapter.EndpointAdapter createEndpoint(IBaseResource endpoint) { + return new EndpointAdapter(endpoint); + } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/EndpointAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/EndpointAdapter.java new file mode 100644 index 000000000..520b73d17 --- /dev/null +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/EndpointAdapter.java @@ -0,0 +1,36 @@ +package org.opencds.cqf.fhir.utility.adapter.r4; + +import org.hl7.fhir.instance.model.api.IBaseResource; +import org.hl7.fhir.r4.model.Endpoint; + +public class EndpointAdapter extends ResourceAdapter implements org.opencds.cqf.fhir.utility.adapter.EndpointAdapter { + private final Endpoint endpoint; + + public EndpointAdapter(IBaseResource endpoint) { + super(endpoint); + if (!(endpoint instanceof Endpoint)) { + throw new IllegalArgumentException("resource passed as endpoint argument is not an Endpoint resource"); + } + this.endpoint = (Endpoint) endpoint; + } + + public EndpointAdapter(Endpoint endpoint) { + super(endpoint); + this.endpoint = endpoint; + } + + public Endpoint getEndpoint() { + return endpoint; + } + + @Override + public String getAddress() { + return endpoint.getAddress(); + } + + @Override + public EndpointAdapter setAddress(String address) { + endpoint.setAddress(address); + return this; + } +} diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/LibraryAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/LibraryAdapter.java index 8627b92ae..08647dbf0 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/LibraryAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/LibraryAdapter.java @@ -22,6 +22,7 @@ import org.hl7.fhir.r4.model.Period; import org.hl7.fhir.r4.model.RelatedArtifact; import org.hl7.fhir.r4.model.RelatedArtifact.RelatedArtifactType; +import org.hl7.fhir.r4.model.UsageContext; import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; @@ -301,4 +302,9 @@ public LibraryAdapter setDataRequirements(List dat dataRequirements.stream().map(dr -> (DataRequirement) dr).collect(Collectors.toList())); return this; } + + @Override + public List getUseContext() { + return library.getUseContext(); + } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/ParametersAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/ParametersAdapter.java index 58cffbd16..63cd6b506 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/ParametersAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/ParametersAdapter.java @@ -2,6 +2,7 @@ import java.util.List; import java.util.stream.Collectors; +import org.hl7.fhir.instance.model.api.IBase; import org.hl7.fhir.instance.model.api.IBaseBackboneElement; import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.r4.model.Parameters; @@ -49,6 +50,25 @@ public void setParameter(List parametersParameterComponent .collect(Collectors.toList())); } + @Override + public void addParameter(String name, IBase value) { + if (value instanceof Type) { + getParameters().addParameter(name, (Type) value); + } else { + throw new IllegalArgumentException("element passed as value argument is not a valid type"); + } + } + + @Override + public void addParameter(IBase parameter) { + if (parameter instanceof ParametersParameterComponent) { + getParameters().addParameter((ParametersParameterComponent) parameter); + } else { + throw new IllegalArgumentException( + "element passed as parameter argument is not a valid parameter component"); + } + } + @Override public ParametersParameterComponent addParameter() { return this.getParameters().addParameter(); diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/ResourceAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/ResourceAdapter.java index aa5ba049e..9c745907d 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/ResourceAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/ResourceAdapter.java @@ -1,10 +1,19 @@ package org.opencds.cqf.fhir.utility.adapter.r4; +import static java.util.Optional.ofNullable; + import ca.uhn.fhir.context.FhirVersionEnum; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; import org.hl7.fhir.exceptions.FHIRException; import org.hl7.fhir.instance.model.api.IBase; +import org.hl7.fhir.instance.model.api.IBaseExtension; import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.r4.model.Base; +import org.hl7.fhir.r4.model.DomainResource; +import org.hl7.fhir.r4.model.Extension; import org.hl7.fhir.r4.model.Resource; class ResourceAdapter implements org.opencds.cqf.fhir.utility.adapter.ResourceAdapter { @@ -27,6 +36,14 @@ protected Resource getResource() { return this.resource; } + protected boolean isDomainResource() { + return getDomainResource().isPresent(); + } + + protected Optional getDomainResource() { + return ofNullable(resource instanceof DomainResource ? (DomainResource) resource : null); + } + public IBaseResource get() { return this.resource; } @@ -95,4 +112,46 @@ public boolean equalsDeep(IBase other) { public boolean equalsShallow(IBase other) { return this.getResource().equalsShallow((Base) other); } + + @Override + public void setExtension(List> extensions) { + getDomainResource() + .ifPresent(r -> r.setExtension( + extensions.stream().map(e -> (Extension) e).collect(Collectors.toList()))); + } + + @Override + public > void addExtension(T extension) { + getDomainResource().ifPresent(r -> r.addExtension((Extension) extension)); + } + + @Override + public List getExtension() { + return isDomainResource() ? getDomainResource().get().getExtension() : new ArrayList<>(); + } + + @SuppressWarnings("unchecked") + @Override + public Extension getExtensionByUrl(String url) { + return isDomainResource() + ? getDomainResource().get().getExtension().stream() + .filter(e -> e.getUrl().equals(url)) + .findFirst() + .orElse(null) + : null; + } + + @Override + public List getExtensionsByUrl(String url) { + return isDomainResource() + ? getDomainResource().get().getExtension().stream() + .filter(e -> e.getUrl().equals(url)) + .collect(Collectors.toList()) + : new ArrayList<>(); + } + + @Override + public List getContained() { + return isDomainResource() ? getDomainResource().get().getContained() : new ArrayList<>(); + } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/ValueSetAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/ValueSetAdapter.java index 93ed403bf..575131e32 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/ValueSetAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/ValueSetAdapter.java @@ -1,25 +1,31 @@ package org.opencds.cqf.fhir.utility.adapter.r4; +import static org.opencds.cqf.fhir.utility.ValueSets.getCodesInCompose; + +import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; +import java.time.Instant; import java.util.ArrayList; +import java.util.Collection; import java.util.Date; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; import org.hl7.fhir.exceptions.FHIRException; -import org.hl7.fhir.instance.model.api.IBaseExtension; +import org.hl7.fhir.instance.model.api.IBaseBackboneElement; import org.hl7.fhir.instance.model.api.IBaseHasExtensions; import org.hl7.fhir.instance.model.api.ICompositeType; import org.hl7.fhir.instance.model.api.IDomainResource; import org.hl7.fhir.instance.model.api.IIdType; import org.hl7.fhir.instance.model.api.IPrimitiveType; +import org.hl7.fhir.r4.model.BooleanType; import org.hl7.fhir.r4.model.DateTimeType; import org.hl7.fhir.r4.model.Enumerations.PublicationStatus; -import org.hl7.fhir.r4.model.Extension; import org.hl7.fhir.r4.model.Period; import org.hl7.fhir.r4.model.RelatedArtifact; import org.hl7.fhir.r4.model.RelatedArtifact.RelatedArtifactType; import org.hl7.fhir.r4.model.ValueSet; +import org.hl7.fhir.r4.model.ValueSet.ValueSetExpansionComponent; import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; @@ -244,8 +250,82 @@ public void setEffectivePeriod(ICompositeType effectivePeriod) { // do nothing } + // @Override + // public void setExtension(List> extensions) { + // this.get().setExtension(extensions.stream().map(e -> (Extension) e).collect(Collectors.toList())); + // } + + @Override + public void setExpansion(T expansion) { + valueSet.setExpansion((ValueSetExpansionComponent) expansion); + } + + @SuppressWarnings("unchecked") + @Override + public ValueSetExpansionComponent getExpansion() { + return valueSet.getExpansion(); + } + + @SuppressWarnings("unchecked") + @Override + public ValueSetExpansionComponent newExpansion() { + var expansion = new ValueSet.ValueSetExpansionComponent(new DateTimeType(Date.from(Instant.now()))); + expansion.getContains(); + return expansion; + } + @Override - public void setExtension(List> extensions) { - this.get().setExtension(extensions.stream().map(e -> (Extension) e).collect(Collectors.toList())); + public List getValueSetIncludes() { + return valueSet.getCompose().getInclude().stream() + .map(i -> i.getValueSet()) + .flatMap(Collection::stream) + .map(c -> c.asStringValue()) + .distinct() + .collect(Collectors.toList()); + } + + @Override + public boolean hasSimpleCompose() { + return valueSet.hasCompose() + && !valueSet.getCompose().hasExclude() + && valueSet.getCompose().getInclude().stream() + .noneMatch( + csc -> csc.hasFilter() || csc.hasValueSet() || !csc.hasSystem() || !csc.hasConcept()); + } + + @Override + public boolean hasGroupingCompose() { + return valueSet.hasCompose() + && !valueSet.getCompose().hasExclude() + && valueSet.getCompose().getInclude().stream().noneMatch(csc -> !csc.hasValueSet() || csc.hasFilter()); + } + + @Override + public boolean hasNaiveParameter() { + return valueSet.getExpansion().getParameter().stream() + .anyMatch(p -> p.getName().equals("naive")); + } + + @SuppressWarnings("unchecked") + @Override + public ValueSet.ValueSetExpansionParameterComponent createNaiveParameter() { + return new ValueSet.ValueSetExpansionParameterComponent() + .setName("naive") + .setValue(new BooleanType(true)); + } + + @Override + public void naiveExpand() { + var expansion = newExpansion().addParameter(createNaiveParameter()); + + for (var code : getCodesInCompose(FhirContext.forR4Cached(), valueSet)) { + expansion + .addContains() + .setCode(code.getCode()) + .setSystem(code.getSystem()) + .setVersion(code.getVersion()) + .setDisplay(code.getDisplay()); + } + valueSet.setExpansion(expansion); } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/AdapterFactory.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/AdapterFactory.java index 1fb8ed51e..cc510618e 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/AdapterFactory.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/AdapterFactory.java @@ -6,13 +6,14 @@ import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.instance.model.api.ICompositeType; import org.hl7.fhir.instance.model.api.IDomainResource; +import org.hl7.fhir.r5.model.Endpoint; import org.hl7.fhir.r5.model.Library; import org.hl7.fhir.r5.model.MetadataResource; +import org.hl7.fhir.r5.model.Parameters; import org.hl7.fhir.r5.model.PlanDefinition; import org.hl7.fhir.r5.model.Questionnaire; import org.hl7.fhir.r5.model.StructureDefinition; import org.hl7.fhir.r5.model.ValueSet; -import org.opencds.cqf.fhir.utility.adapter.LibraryAdapter; public class AdapterFactory implements org.opencds.cqf.fhir.utility.adapter.AdapterFactory { @@ -20,6 +21,10 @@ public class AdapterFactory implements org.opencds.cqf.fhir.utility.adapter.Adap public org.opencds.cqf.fhir.utility.adapter.ResourceAdapter createResource(IBaseResource resource) { if (resource instanceof MetadataResource) { return createKnowledgeArtifactAdapter((MetadataResource) resource); + } else if (resource instanceof Endpoint) { + return createEndpoint(resource); + } else if (resource instanceof Parameters) { + return createParameters((Parameters) resource); } else { return new ResourceAdapter(resource); } @@ -32,18 +37,16 @@ public org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter createKnowl if (resource instanceof Library) { adapter = createLibrary(resource); } else if (resource instanceof PlanDefinition) { - adapter = new org.opencds.cqf.fhir.utility.adapter.r5.PlanDefinitionAdapter((PlanDefinition) resource); + adapter = new PlanDefinitionAdapter((PlanDefinition) resource); } else if (resource instanceof Questionnaire) { - adapter = new org.opencds.cqf.fhir.utility.adapter.r5.QuestionnaireAdapter((Questionnaire) resource); + adapter = new QuestionnaireAdapter((Questionnaire) resource); } else if (resource instanceof StructureDefinition) { - adapter = new org.opencds.cqf.fhir.utility.adapter.r5.StructureDefinitionAdapter( - (StructureDefinition) resource); + adapter = new StructureDefinitionAdapter((StructureDefinition) resource); } else if (resource instanceof ValueSet) { - adapter = new org.opencds.cqf.fhir.utility.adapter.r5.ValueSetAdapter((ValueSet) resource); + adapter = new ValueSetAdapter((ValueSet) resource); } else { if (resource instanceof MetadataResource) { - adapter = new org.opencds.cqf.fhir.utility.adapter.r5.KnowledgeArtifactAdapter( - (MetadataResource) resource); + adapter = new KnowledgeArtifactAdapter((MetadataResource) resource); } else { throw new UnprocessableEntityException( String.format("Resource must be instance of %s", MetadataResource.class.getName())); @@ -53,8 +56,8 @@ public org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter createKnowl } @Override - public LibraryAdapter createLibrary(IBaseResource library) { - return new org.opencds.cqf.fhir.utility.adapter.r5.LibraryAdapter((IDomainResource) library); + public org.opencds.cqf.fhir.utility.adapter.LibraryAdapter createLibrary(IBaseResource library) { + return new LibraryAdapter((IDomainResource) library); } @Override @@ -72,4 +75,9 @@ public org.opencds.cqf.fhir.utility.adapter.ParametersParameterComponentAdapter IBaseBackboneElement parametersParametersComponent) { return new ParametersParameterComponentAdapter(parametersParametersComponent); } + + @Override + public org.opencds.cqf.fhir.utility.adapter.EndpointAdapter createEndpoint(IBaseResource endpoint) { + return new EndpointAdapter(endpoint); + } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/EndpointAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/EndpointAdapter.java new file mode 100644 index 000000000..35c14f4ff --- /dev/null +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/EndpointAdapter.java @@ -0,0 +1,36 @@ +package org.opencds.cqf.fhir.utility.adapter.r5; + +import org.hl7.fhir.instance.model.api.IBaseResource; +import org.hl7.fhir.r5.model.Endpoint; + +public class EndpointAdapter extends ResourceAdapter implements org.opencds.cqf.fhir.utility.adapter.EndpointAdapter { + private final Endpoint endpoint; + + public EndpointAdapter(IBaseResource endpoint) { + super(endpoint); + if (!(endpoint instanceof Endpoint)) { + throw new IllegalArgumentException("resource passed as endpoint argument is not an Endpoint resource"); + } + this.endpoint = (Endpoint) endpoint; + } + + public EndpointAdapter(Endpoint endpoint) { + super(endpoint); + this.endpoint = endpoint; + } + + public Endpoint getEndpoint() { + return endpoint; + } + + @Override + public String getAddress() { + return endpoint.getAddress(); + } + + @Override + public EndpointAdapter setAddress(String address) { + endpoint.setAddress(address); + return this; + } +} diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/KnowledgeArtifactAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/KnowledgeArtifactAdapter.java index a06557447..deee1feff 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/KnowledgeArtifactAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/KnowledgeArtifactAdapter.java @@ -6,14 +6,12 @@ import java.util.List; import java.util.stream.Collectors; import org.hl7.fhir.exceptions.FHIRException; -import org.hl7.fhir.instance.model.api.IBaseExtension; import org.hl7.fhir.instance.model.api.IBaseHasExtensions; import org.hl7.fhir.instance.model.api.ICompositeType; import org.hl7.fhir.instance.model.api.IDomainResource; import org.hl7.fhir.instance.model.api.IPrimitiveType; import org.hl7.fhir.r5.model.DateTimeType; import org.hl7.fhir.r5.model.Enumerations.PublicationStatus; -import org.hl7.fhir.r5.model.Extension; import org.hl7.fhir.r5.model.MetadataResource; import org.hl7.fhir.r5.model.Period; import org.hl7.fhir.r5.model.RelatedArtifact; @@ -199,9 +197,4 @@ public String getStatus() { public boolean getExperimental() { return this.get().getExperimental(); } - - @Override - public void setExtension(List> extensions) { - this.get().setExtension(extensions.stream().map(e -> (Extension) e).collect(Collectors.toList())); - } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/LibraryAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/LibraryAdapter.java index ffbd7690b..4d9c6a722 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/LibraryAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/LibraryAdapter.java @@ -22,6 +22,7 @@ import org.hl7.fhir.r5.model.Period; import org.hl7.fhir.r5.model.RelatedArtifact; import org.hl7.fhir.r5.model.RelatedArtifact.RelatedArtifactType; +import org.hl7.fhir.r5.model.UsageContext; import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; @@ -295,4 +296,9 @@ public LibraryAdapter setDataRequirements(List dat dataRequirements.stream().map(dr -> (DataRequirement) dr).collect(Collectors.toList())); return this; } + + @Override + public List getUseContext() { + return library.getUseContext(); + } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/ParametersAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/ParametersAdapter.java index ad34ed36c..08403986f 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/ParametersAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/ParametersAdapter.java @@ -2,6 +2,7 @@ import java.util.List; import java.util.stream.Collectors; +import org.hl7.fhir.instance.model.api.IBase; import org.hl7.fhir.instance.model.api.IBaseBackboneElement; import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.r5.model.DataType; @@ -49,6 +50,25 @@ public void setParameter(List parametersParameterComponent .collect(Collectors.toList())); } + @Override + public void addParameter(String name, IBase value) { + if (value instanceof DataType) { + getParameters().addParameter(name, (DataType) value); + } else { + throw new IllegalArgumentException("element passed as value argument is not a valid data type"); + } + } + + @Override + public void addParameter(IBase parameter) { + if (parameter instanceof ParametersParameterComponent) { + getParameters().addParameter((ParametersParameterComponent) parameter); + } else { + throw new IllegalArgumentException( + "element passed as parameter argument is not a valid parameter component"); + } + } + @Override public ParametersParameterComponent addParameter() { return this.getParameters().addParameter(); diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapter.java index a7f957a87..1e14d8653 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapter.java @@ -1,10 +1,19 @@ package org.opencds.cqf.fhir.utility.adapter.r5; +import static java.util.Optional.ofNullable; + import ca.uhn.fhir.context.FhirVersionEnum; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; import org.hl7.fhir.exceptions.FHIRException; import org.hl7.fhir.instance.model.api.IBase; +import org.hl7.fhir.instance.model.api.IBaseExtension; import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.r5.model.Base; +import org.hl7.fhir.r5.model.DomainResource; +import org.hl7.fhir.r5.model.Extension; import org.hl7.fhir.r5.model.Resource; class ResourceAdapter implements org.opencds.cqf.fhir.utility.adapter.ResourceAdapter { @@ -27,6 +36,14 @@ protected Resource getResource() { return this.resource; } + protected boolean isDomainResource() { + return getDomainResource().isPresent(); + } + + protected Optional getDomainResource() { + return ofNullable(resource instanceof DomainResource ? (DomainResource) resource : null); + } + public IBaseResource get() { return this.resource; } @@ -95,4 +112,46 @@ public boolean equalsDeep(IBase other) { public boolean equalsShallow(IBase other) { return this.getResource().equalsShallow((Base) other); } + + @Override + public void setExtension(List> extensions) { + getDomainResource() + .ifPresent(r -> r.setExtension( + extensions.stream().map(e -> (Extension) e).collect(Collectors.toList()))); + } + + @Override + public > void addExtension(T extension) { + getDomainResource().ifPresent(r -> r.addExtension((Extension) extension)); + } + + @Override + public List getExtension() { + return isDomainResource() ? getDomainResource().get().getExtension() : new ArrayList<>(); + } + + @SuppressWarnings("unchecked") + @Override + public Extension getExtensionByUrl(String url) { + return isDomainResource() + ? getDomainResource().get().getExtension().stream() + .filter(e -> e.getUrl().equals(url)) + .findFirst() + .orElse(null) + : null; + } + + @Override + public List getExtensionsByUrl(String url) { + return isDomainResource() + ? getDomainResource().get().getExtension().stream() + .filter(e -> e.getUrl().equals(url)) + .collect(Collectors.toList()) + : new ArrayList<>(); + } + + @Override + public List getContained() { + return isDomainResource() ? getDomainResource().get().getContained() : new ArrayList<>(); + } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/ValueSetAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/ValueSetAdapter.java index abbd57b14..ce719d531 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/ValueSetAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/ValueSetAdapter.java @@ -1,23 +1,29 @@ package org.opencds.cqf.fhir.utility.adapter.r5; +import static org.opencds.cqf.fhir.utility.ValueSets.getCodesInCompose; + +import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; +import java.time.Instant; import java.util.ArrayList; +import java.util.Collection; import java.util.Date; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; import org.hl7.fhir.exceptions.FHIRException; -import org.hl7.fhir.instance.model.api.IBaseExtension; +import org.hl7.fhir.instance.model.api.IBaseBackboneElement; import org.hl7.fhir.instance.model.api.IBaseHasExtensions; import org.hl7.fhir.instance.model.api.ICompositeType; import org.hl7.fhir.instance.model.api.IPrimitiveType; +import org.hl7.fhir.r5.model.BooleanType; import org.hl7.fhir.r5.model.DateTimeType; import org.hl7.fhir.r5.model.Enumerations.PublicationStatus; -import org.hl7.fhir.r5.model.Extension; import org.hl7.fhir.r5.model.Period; import org.hl7.fhir.r5.model.RelatedArtifact; import org.hl7.fhir.r5.model.RelatedArtifact.RelatedArtifactType; import org.hl7.fhir.r5.model.ValueSet; +import org.hl7.fhir.r5.model.ValueSet.ValueSetExpansionComponent; import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; @@ -236,8 +242,80 @@ public boolean getExperimental() { return this.getValueSet().getExperimental(); } + // @Override + // public void setExtension(List> extensions) { + // this.get().setExtension(extensions.stream().map(e -> (Extension) e).collect(Collectors.toList())); + // } + + @Override + public void setExpansion(T expansion) { + valueSet.setExpansion((ValueSetExpansionComponent) expansion); + } + + @SuppressWarnings("unchecked") + @Override + public ValueSetExpansionComponent getExpansion() { + return valueSet.getExpansion(); + } + + @SuppressWarnings("unchecked") + @Override + public ValueSetExpansionComponent newExpansion() { + return new ValueSetExpansionComponent(Date.from(Instant.now())); + } + @Override - public void setExtension(List> extensions) { - this.get().setExtension(extensions.stream().map(e -> (Extension) e).collect(Collectors.toList())); + public List getValueSetIncludes() { + return valueSet.getCompose().getInclude().stream() + .map(i -> i.getValueSet()) + .flatMap(Collection::stream) + .map(c -> c.asStringValue()) + .distinct() + .collect(Collectors.toList()); + } + + @Override + public boolean hasSimpleCompose() { + return valueSet.hasCompose() + && !valueSet.getCompose().hasExclude() + && valueSet.getCompose().getInclude().stream() + .noneMatch( + csc -> csc.hasFilter() || csc.hasValueSet() || !csc.hasSystem() || !csc.hasConcept()); + } + + @Override + public boolean hasGroupingCompose() { + return valueSet.hasCompose() + && !valueSet.getCompose().hasExclude() + && valueSet.getCompose().getInclude().stream().noneMatch(csc -> !csc.hasValueSet() || csc.hasFilter()); + } + + @Override + public boolean hasNaiveParameter() { + return valueSet.getExpansion().getParameter().stream() + .anyMatch(p -> p.getName().equals("naive")); + } + + @SuppressWarnings("unchecked") + @Override + public ValueSet.ValueSetExpansionParameterComponent createNaiveParameter() { + return new ValueSet.ValueSetExpansionParameterComponent() + .setName("naive") + .setValue(new BooleanType(true)); + } + + @Override + public void naiveExpand() { + var expansion = newExpansion().addParameter(createNaiveParameter()); + + for (var code : getCodesInCompose(FhirContext.forR5Cached(), valueSet)) { + expansion + .addContains() + .setCode(code.getCode()) + .setSystem(code.getSystem()) + .setVersion(code.getVersion()) + .setDisplay(code.getDisplay()); + } + valueSet.setExpansion(expansion); } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/client/TerminologyServerClient.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/client/TerminologyServerClient.java index 25087ac14..ed6dd3c21 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/client/TerminologyServerClient.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/client/TerminologyServerClient.java @@ -1,10 +1,25 @@ package org.opencds.cqf.fhir.utility.client; +import static com.google.common.base.Preconditions.checkNotNull; + import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.context.FhirVersionEnum; import ca.uhn.fhir.rest.client.api.IGenericClient; +import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; import java.util.Objects; +import org.hl7.fhir.instance.model.api.IBaseParameters; +import org.hl7.fhir.instance.model.api.IBaseResource; import org.opencds.cqf.fhir.utility.Canonicals; +import org.opencds.cqf.fhir.utility.Constants; +import org.opencds.cqf.fhir.utility.Parameters; +import org.opencds.cqf.fhir.utility.adapter.EndpointAdapter; +import org.opencds.cqf.fhir.utility.adapter.ParametersAdapter; +import org.opencds.cqf.fhir.utility.adapter.ValueSetAdapter; +/** + * This class currently serves as a VSAC Terminology Server client as it expects the Endpoint provided to contain a VSAC username and api key. + * Future enhancements include adding support for multiple endpoints + */ public class TerminologyServerClient { private final FhirContext ctx; @@ -13,108 +28,47 @@ public TerminologyServerClient(FhirContext ctx) { this.ctx = ctx; } - public org.hl7.fhir.dstu3.model.ValueSet expand( - org.hl7.fhir.dstu3.model.ValueSet valueSet, - String authoritativeSource, - org.hl7.fhir.dstu3.model.Parameters expansionParameters, - String username, - String apiKey) { - IGenericClient fhirClient = ctx.newRestfulGenericClient(getAuthoritativeSourceBase(authoritativeSource)); - Clients.registerAdditionalRequestHeadersAuth(fhirClient, username, apiKey); - if (expansionParameters == null) { - expansionParameters = new org.hl7.fhir.dstu3.model.Parameters(); - } - if (!expansionParameters.getParameter().stream() - .anyMatch(p -> p.getName().equals("url"))) { - expansionParameters - .addParameter() - .setName("url") - .setValue(new org.hl7.fhir.dstu3.model.UriType(valueSet.getUrl())); - } - if (valueSet.hasVersion() - && !expansionParameters.getParameter().stream() - .anyMatch(p -> p.getName().equals("valueSetVersion"))) { - expansionParameters - .addParameter() - .setName("valueSetVersion") - .setValue(new org.hl7.fhir.dstu3.model.StringType(valueSet.getVersion())); - } - // Invoke on the type using the url parameter - return fhirClient - .operation() - .onType("ValueSet") - .named("$expand") - .withParameters(expansionParameters) - .returnResourceType(org.hl7.fhir.dstu3.model.ValueSet.class) - .execute(); - } - - public org.hl7.fhir.r4.model.ValueSet expand( - org.hl7.fhir.r4.model.ValueSet valueSet, - String authoritativeSource, - org.hl7.fhir.r4.model.Parameters expansionParameters, - String username, - String apiKey) { - IGenericClient fhirClient = ctx.newRestfulGenericClient(getAuthoritativeSourceBase(authoritativeSource)); + public IBaseResource expand(ValueSetAdapter valueSet, EndpointAdapter endpoint, ParametersAdapter parameters) { + checkNotNull(valueSet, "expected non-null value for valueSet"); + checkNotNull(endpoint, "expected non-null value for endpoint"); + checkNotNull(parameters, "expected non-null value for parameters"); + var username = endpoint.getExtensionsByUrl(Constants.VSAC_USERNAME).stream() + .findFirst() + .map(ext -> ext.getValue().toString()) + .orElseThrow(() -> new UnprocessableEntityException("Cannot expand ValueSet without VSAC Username.")); + var apiKey = endpoint.getExtensionsByUrl(Constants.APIKEY).stream() + .findFirst() + .map(ext -> ext.getValue().toString()) + .orElseThrow(() -> new UnprocessableEntityException("Cannot expand ValueSet without VSAC API Key.")); + IGenericClient fhirClient = ctx.newRestfulGenericClient(getAddressBase(endpoint.getAddress())); Clients.registerAdditionalRequestHeadersAuth(fhirClient, username, apiKey); - if (expansionParameters == null) { - expansionParameters = new org.hl7.fhir.r4.model.Parameters(); - } - if (!expansionParameters.hasParameter("url")) { - expansionParameters.addParameter("url", new org.hl7.fhir.r4.model.UriType(valueSet.getUrl())); - } - if (valueSet.hasVersion() && !expansionParameters.hasParameter("valueSetVersion")) { - expansionParameters.addParameter( - "valueSetVersion", new org.hl7.fhir.r4.model.StringType(valueSet.getVersion())); - } - // Invoke on the type using the url parameter - return fhirClient - .operation() - .onType("ValueSet") - .named("$expand") - .withParameters(expansionParameters) - .returnResourceType(org.hl7.fhir.r4.model.ValueSet.class) - .execute(); - } - public org.hl7.fhir.r5.model.ValueSet expand( - org.hl7.fhir.r5.model.ValueSet valueSet, - String authoritativeSource, - org.hl7.fhir.r5.model.Parameters expansionParameters, - String username, - String apiKey) { - IGenericClient fhirClient = ctx.newRestfulGenericClient(getAuthoritativeSourceBase(authoritativeSource)); - Clients.registerAdditionalRequestHeadersAuth(fhirClient, username, apiKey); - - if (expansionParameters == null) { - expansionParameters = new org.hl7.fhir.r5.model.Parameters(); - } - if (!expansionParameters.hasParameter("url")) { - expansionParameters.addParameter("url", new org.hl7.fhir.r5.model.UriType(valueSet.getUrl())); + if (parameters.getParameter("url") == null) { + parameters.addParameter( + valueSet.get().getStructureFhirVersionEnum().equals(FhirVersionEnum.DSTU3) + ? Parameters.newUriPart(ctx, "url", valueSet.getUrl()) + : Parameters.newUrlPart(ctx, "url", valueSet.getUrl())); } - if (valueSet.hasVersion() && !expansionParameters.hasParameter("valueSetVersion")) { - expansionParameters.addParameter( - "valueSetVersion", new org.hl7.fhir.r5.model.StringType(valueSet.getVersion())); + if (valueSet.hasVersion() && parameters.getParameter("valueSetVersion") == null) { + parameters.addParameter(Parameters.newStringPart(ctx, "valueSetVersion", valueSet.getVersion())); } // Invoke on the type using the url parameter return fhirClient .operation() .onType("ValueSet") .named("$expand") - .withParameters(expansionParameters) - .returnResourceType(org.hl7.fhir.r5.model.ValueSet.class) + .withParameters((IBaseParameters) parameters.get()) + .returnResourceType(valueSet.get().getClass()) .execute(); } - // Strips resource and id from the authoritative source URL, these are not needed as the client constructs the URL. + // Strips resource and id from the endpoint address URL, these are not needed as the client constructs the URL. // Converts http URLs to https - private String getAuthoritativeSourceBase(String authoritativeSource) { - authoritativeSource = authoritativeSource.substring( - 0, - authoritativeSource.indexOf(Objects.requireNonNull(Canonicals.getResourceType(authoritativeSource)))); - if (authoritativeSource.startsWith("http://")) { - authoritativeSource = authoritativeSource.replaceFirst("http://", "https://"); + private String getAddressBase(String address) { + address = address.substring(0, address.indexOf(Objects.requireNonNull(Canonicals.getResourceType(address)))); + if (address.startsWith("http://")) { + address = address.replaceFirst("http://", "https://"); } - return authoritativeSource; + return address; } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/PackageVisitor.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/PackageVisitor.java index 5dc45e037..2e937088c 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/PackageVisitor.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/PackageVisitor.java @@ -1,8 +1,11 @@ package org.opencds.cqf.fhir.utility.visitor; +import static org.opencds.cqf.fhir.utility.Parameters.newParameters; +import static org.opencds.cqf.fhir.utility.adapter.AdapterFactory.createAdapterForResource; import static org.opencds.cqf.fhir.utility.visitor.VisitorHelper.findUnsupportedCapability; import static org.opencds.cqf.fhir.utility.visitor.VisitorHelper.processCanonicals; +import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.context.FhirVersionEnum; import ca.uhn.fhir.rest.server.exceptions.NotImplementedOperationException; import ca.uhn.fhir.rest.server.exceptions.PreconditionFailedException; @@ -19,18 +22,35 @@ import org.hl7.fhir.instance.model.api.IBaseBundle; import org.hl7.fhir.instance.model.api.IBaseIntegerDatatype; import org.hl7.fhir.instance.model.api.IBaseParameters; +import org.hl7.fhir.instance.model.api.IBaseReference; import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.instance.model.api.IDomainResource; import org.hl7.fhir.instance.model.api.IPrimitiveType; import org.opencds.cqf.fhir.api.Repository; import org.opencds.cqf.fhir.utility.BundleHelper; import org.opencds.cqf.fhir.utility.Canonicals; +import org.opencds.cqf.fhir.utility.Constants; +import org.opencds.cqf.fhir.utility.ExpandHelper; import org.opencds.cqf.fhir.utility.PackageHelper; import org.opencds.cqf.fhir.utility.SearchHelper; import org.opencds.cqf.fhir.utility.adapter.AdapterFactory; +import org.opencds.cqf.fhir.utility.adapter.EndpointAdapter; import org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter; +import org.opencds.cqf.fhir.utility.adapter.LibraryAdapter; +import org.opencds.cqf.fhir.utility.adapter.ParametersAdapter; +import org.opencds.cqf.fhir.utility.adapter.ValueSetAdapter; +import org.opencds.cqf.fhir.utility.client.TerminologyServerClient; public class PackageVisitor implements KnowledgeArtifactVisitor { + protected final FhirContext fhirContext; + protected final TerminologyServerClient terminologyServerClient; + protected final ExpandHelper expandHelper; + + public PackageVisitor(FhirContext fhirContext) { + this.fhirContext = fhirContext; + this.terminologyServerClient = new TerminologyServerClient(this.fhirContext); + expandHelper = new ExpandHelper(fhirContext, terminologyServerClient); + } @Override public IBase visit(KnowledgeArtifactAdapter adapter, Repository repository, IBaseParameters packageParameters) { @@ -129,39 +149,36 @@ protected void handleValueSets( IBaseBundle packagedBundle, Repository repository, Optional terminologyEndpoint) { - switch (resource.getStructureFhirVersionEnum()) { - case DSTU3: - org.opencds.cqf.fhir.utility.visitor.dstu3.PackageVisitor packageVisitorDstu3 = - new org.opencds.cqf.fhir.utility.visitor.dstu3.PackageVisitor(); - packageVisitorDstu3.handleValueSets( - (org.hl7.fhir.dstu3.model.MetadataResource) resource, - ((org.hl7.fhir.dstu3.model.Bundle) packagedBundle).getEntry(), - repository, - terminologyEndpoint.map(te -> (org.hl7.fhir.dstu3.model.Endpoint) te)); - break; - case R4: - org.opencds.cqf.fhir.utility.visitor.r4.PackageVisitor packageVisitorR4 = - new org.opencds.cqf.fhir.utility.visitor.r4.PackageVisitor(); - packageVisitorR4.handleValueSets( - (org.hl7.fhir.r4.model.MetadataResource) resource, - ((org.hl7.fhir.r4.model.Bundle) packagedBundle).getEntry(), - repository, - terminologyEndpoint.map(te -> (org.hl7.fhir.r4.model.Endpoint) te)); - break; - case R5: - org.opencds.cqf.fhir.utility.visitor.r5.PackageVisitor packageVisitorR5 = - new org.opencds.cqf.fhir.utility.visitor.r5.PackageVisitor(); - packageVisitorR5.handleValueSets( - (org.hl7.fhir.r5.model.MetadataResource) resource, - ((org.hl7.fhir.r5.model.Bundle) packagedBundle).getEntry(), - repository, - terminologyEndpoint.map(te -> (org.hl7.fhir.r5.model.Endpoint) te)); - break; - default: - throw new IllegalArgumentException(String.format( - "Unsupported FHIR version: %s", - resource.getStructureFhirVersionEnum().getFhirVersionString())); + var expansionParams = newParameters(repository.fhirContext()); + var rootSpecificationLibrary = getRootSpecificationLibrary(packagedBundle); + if (rootSpecificationLibrary != null) { + var expansionParamsExtension = + rootSpecificationLibrary.getExtensionByUrl(Constants.CQF_EXPANSION_PARAMETERS); + if (expansionParamsExtension != null && expansionParamsExtension.getValue() != null) { + // Reference expansionReference = (Reference) expansionParamsExtension.getValue(); + expansionParams = getExpansionParams( + rootSpecificationLibrary, + ((IBaseReference) expansionParamsExtension.getValue()) + .getReferenceElement() + .getValueAsString()); + } } + var params = (ParametersAdapter) createAdapterForResource(expansionParams); + var expandedList = new ArrayList(); + + var valueSets = BundleHelper.getEntryResources(packagedBundle).stream() + .filter(r -> r.fhirType().equals("ValueSet")) + .map(v -> (ValueSetAdapter) createAdapterForResource(v)) + .collect(Collectors.toList()); + + valueSets.stream().forEach(valueSet -> { + expandHelper.expandValueSet( + valueSet, + params, + terminologyEndpoint.map(e -> (EndpointAdapter) createAdapterForResource(e)), + valueSets, + expandedList); + }); } protected void recursivePackage( @@ -293,4 +310,44 @@ protected List findUnsupportedInclude( String.format("Unsupported version of FHIR: %s", fhirVersion.getFhirVersionString())); } } + + protected static LibraryAdapter getRootSpecificationLibrary(IBaseBundle bundle) { + Optional rootSpecLibrary = BundleHelper.getEntryResources(bundle).stream() + .filter(r -> r.fhirType().equals("Library")) + .map(r -> AdapterFactory.forFhirVersion(r.getStructureFhirVersionEnum()) + .createLibrary(r)) + // .filter(a -> a.getType().hasCoding(Constants.LIBRARY_TYPE, Constants.ASSET_COLLECTION) + // && a.getUseContext().stream() + // .allMatch(useContext -> (useContext + // .getCode() + // .getSystem() + // .equals(KnowledgeArtifactAdapter.usPhContextTypeUrl) + // && useContext + // .getCode() + // .getCode() + // .equals("reporting") + // && useContext + // .getValueCodeableConcept() + // .hasCoding(Constants.US_PH_CONTEXT_URL, "triggering")) + // || (useContext + // .getCode() + // .getSystem() + // .equals(KnowledgeArtifactAdapter.usPhContextTypeUrl) + // && useContext + // .getCode() + // .getCode() + // .equals("specification-type") + // && useContext + // .getValueCodeableConcept() + // .hasCoding(Constants.US_PH_CONTEXT_URL, "program")))) + .findFirst(); + return rootSpecLibrary.orElse(null); + } + + protected static IBaseParameters getExpansionParams(LibraryAdapter rootSpecificationLibrary, String reference) { + Optional expansionParamResource = rootSpecificationLibrary.getContained().stream() + .filter(contained -> contained.getIdElement().getValue().equals(reference)) + .findFirst(); + return (IBaseParameters) expansionParamResource.orElse(null); + } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/VisitorHelper.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/VisitorHelper.java index 9709baa8b..cc54beddc 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/VisitorHelper.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/VisitorHelper.java @@ -10,6 +10,7 @@ import org.hl7.fhir.instance.model.api.IBaseExtension; import org.hl7.fhir.instance.model.api.IBaseParameters; import org.hl7.fhir.instance.model.api.IBaseResource; +import org.hl7.fhir.instance.model.api.ICompositeType; import org.hl7.fhir.instance.model.api.IPrimitiveType; import org.opencds.cqf.fhir.utility.BundleHelper; import org.opencds.cqf.fhir.utility.Canonicals; @@ -138,4 +139,8 @@ private static Optional findVersionInListMatchingResource( .map((canonical) -> Canonicals.getVersion(canonical)) .findAny(); } + + public static boolean typeHasCoding(ICompositeType type, String system, String code) { + return false; + } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/dstu3/PackageVisitor.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/dstu3/PackageVisitor.java index 864cf0a50..a912e9142 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/dstu3/PackageVisitor.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/dstu3/PackageVisitor.java @@ -1,14 +1,9 @@ package org.opencds.cqf.fhir.utility.visitor.dstu3; -import static org.opencds.cqf.fhir.utility.ValueSets.getCodesInCompose; - import ca.uhn.fhir.context.FhirContext; -import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; -import java.time.Instant; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; -import java.util.Date; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; @@ -17,20 +12,11 @@ import org.hl7.fhir.dstu3.model.Bundle.BundleEntryComponent; import org.hl7.fhir.dstu3.model.Bundle.BundleType; import org.hl7.fhir.dstu3.model.Coding; -import org.hl7.fhir.dstu3.model.Endpoint; -import org.hl7.fhir.dstu3.model.Extension; import org.hl7.fhir.dstu3.model.Library; import org.hl7.fhir.dstu3.model.MetadataResource; -import org.hl7.fhir.dstu3.model.Parameters; -import org.hl7.fhir.dstu3.model.Reference; -import org.hl7.fhir.dstu3.model.Resource; import org.hl7.fhir.dstu3.model.ResourceType; import org.hl7.fhir.dstu3.model.StructureDefinition; import org.hl7.fhir.dstu3.model.UsageContext; -import org.hl7.fhir.dstu3.model.ValueSet; -import org.opencds.cqf.fhir.api.Repository; -import org.opencds.cqf.fhir.utility.Constants; -import org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter; import org.opencds.cqf.fhir.utility.client.TerminologyServerClient; public class PackageVisitor { @@ -203,146 +189,6 @@ public static List findUnsupportedInclude( return distinctFilteredEntries; } - /** - * ValueSets can be part of multiple artifacts at the same time. Certain properties are tracked/managed in the manifest to avoid conflicts with other artifacts. This function sets those properties on the ValueSets themselves at export / $package time - * @param manifest the resource containing all RelatedArtifact references - * @param bundleEntries the list of packaged resources to modify according to the extensions on the manifest relatedArtifact references - */ - public void handleValueSets( - MetadataResource manifest, - List bundleEntries, - Repository repository, - Optional terminologyEndpoint) - throws UnprocessableEntityException, IllegalArgumentException { - Parameters expansionParams = new Parameters(); - Library rootSpecificationLibrary = getRootSpecificationLibrary(bundleEntries); - if (rootSpecificationLibrary != null) { - Extension expansionParamsExtension = - rootSpecificationLibrary.getExtensionByUrl(Constants.CQF_EXPANSION_PARAMETERS); - if (expansionParamsExtension != null && expansionParamsExtension.hasValue()) { - Reference expansionReference = (Reference) expansionParamsExtension.getValue(); - expansionParams = getExpansionParams(rootSpecificationLibrary, expansionReference.getReference()); - } - } - Parameters params = expansionParams; - bundleEntries.stream().forEach(entry -> { - if (entry.getResource().getResourceType().equals(ResourceType.ValueSet)) { - ValueSet valueSet = (ValueSet) entry.getResource(); - expandValueSet(valueSet, params, terminologyEndpoint); - } - }); - } - - public void expandValueSet( - ValueSet valueSet, Parameters expansionParameters, Optional terminologyEndpoint) { - // Gather the Terminology Service from the valueSet's authoritativeSourceUrl. - Extension authoritativeSource = valueSet.getExtensionByUrl(Constants.AUTHORITATIVE_SOURCE_URL); - String authoritativeSourceUrl = authoritativeSource != null && authoritativeSource.hasValue() - ? authoritativeSource.getValue().primitiveValue() - : valueSet.getUrl(); - - ValueSet expandedValueSet; - if (hasSimpleCompose(valueSet)) { - // Perform naive expansion independent of terminology servers. Copy all codes from compose into expansion. - ValueSet.ValueSetExpansionComponent expansion = new ValueSet.ValueSetExpansionComponent(); - expansion.setTimestamp(Date.from(Instant.now())); - - ArrayList expansionParams = new ArrayList<>(); - ValueSet.ValueSetExpansionParameterComponent parameterNaive = - new ValueSet.ValueSetExpansionParameterComponent(); - parameterNaive.setName("naive"); - parameterNaive.setValue(new BooleanType(true)); - expansionParams.add(parameterNaive); - expansion.setParameter(expansionParams); - - for (var code : getCodesInCompose(fhirContext, valueSet)) { - expansion - .addContains() - .setCode(code.getCode()) - .setSystem(code.getSystem()) - .setVersion(code.getVersion()) - .setDisplay(code.getDisplay()); - } - valueSet.setExpansion(expansion); - } else { - String username; - String apiKey; - if (terminologyEndpoint.isPresent()) { - username = terminologyEndpoint.get().getExtensionsByUrl(Constants.VSAC_USERNAME).stream() - .findFirst() - .map(ext -> ext.getValue().toString()) - .orElseThrow(() -> new UnprocessableEntityException( - "Cannot expand ValueSet without VSAC Username: " + valueSet.getId())); - apiKey = terminologyEndpoint.get().getExtensionsByUrl(Constants.APIKEY).stream() - .findFirst() - .map(ext -> ext.getValue().toString()) - .orElseThrow(() -> new UnprocessableEntityException( - "Cannot expand ValueSet without VSAC API Key: " + valueSet.getId())); - } else { - throw new UnprocessableEntityException( - "Cannot expand ValueSet without a terminology server: " + valueSet.getId()); - } - - try { - expandedValueSet = terminologyServerClient.expand( - valueSet, authoritativeSourceUrl, expansionParameters, username, apiKey); - valueSet.setExpansion(expandedValueSet.getExpansion()); - } catch (Exception ex) { - throw new UnprocessableEntityException( - "Terminology Server expansion failed for: " + valueSet.getId(), ex.getMessage()); - } - } - } - - // A simple compose element of a ValueSet must have a compose without an exclude element. Each element of the - // include cannot have a filter, and must reference a ValueSet or have a system and enumerate concepts - protected boolean hasSimpleCompose(ValueSet valueSet) { - return valueSet.hasCompose() - && !valueSet.getCompose().hasExclude() - && valueSet.getCompose().getInclude().stream() - .noneMatch(csc -> - csc.hasFilter() || (!csc.hasValueSet() && (!csc.hasSystem() && !csc.hasConcept()))); - } - - protected static Library getRootSpecificationLibrary(List bundleEntries) { - Optional rootSpecLibrary = bundleEntries.stream() - .filter(entry -> entry.getResource().getResourceType() == ResourceType.Library) - .map(entry -> ((Library) entry.getResource())) - .filter(entry -> entry.getType().hasCoding(Constants.LIBRARY_TYPE, Constants.ASSET_COLLECTION) - && entry.getUseContext().stream() - .allMatch(useContext -> (useContext - .getCode() - .getSystem() - .equals(KnowledgeArtifactAdapter.usPhContextTypeUrl) - && useContext - .getCode() - .getCode() - .equals("reporting") - && useContext - .getValueCodeableConcept() - .hasCoding(Constants.US_PH_CONTEXT_URL, "triggering")) - || (useContext - .getCode() - .getSystem() - .equals(KnowledgeArtifactAdapter.usPhContextTypeUrl) - && useContext - .getCode() - .getCode() - .equals("specification-type") - && useContext - .getValueCodeableConcept() - .hasCoding(Constants.US_PH_CONTEXT_URL, "program")))) - .findFirst(); - return rootSpecLibrary.orElse(null); - } - - protected static Parameters getExpansionParams(Library rootSpecificationLibrary, String reference) { - Optional expansionParamResource = rootSpecificationLibrary.getContained().stream() - .filter(contained -> contained.getId().equals(reference)) - .findFirst(); - return (Parameters) expansionParamResource.orElse(null); - } - /** * * Either finds a usageContext with the same system and code or creates an empty one diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r4/PackageVisitor.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r4/PackageVisitor.java index ac1ab2994..6d7745ae9 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r4/PackageVisitor.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r4/PackageVisitor.java @@ -1,52 +1,32 @@ package org.opencds.cqf.fhir.utility.visitor.r4; -import static org.opencds.cqf.fhir.utility.ValueSets.getCodesInCompose; - -import ca.uhn.fhir.context.FhirContext; -import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; -import java.time.Instant; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collection; import java.util.Collections; -import java.util.Date; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; -import org.hl7.fhir.instance.model.api.IBaseHasExtensions; -import org.hl7.fhir.instance.model.api.IPrimitiveType; import org.hl7.fhir.r4.model.BooleanType; import org.hl7.fhir.r4.model.Bundle; import org.hl7.fhir.r4.model.Bundle.BundleEntryComponent; import org.hl7.fhir.r4.model.Bundle.BundleType; import org.hl7.fhir.r4.model.Coding; -import org.hl7.fhir.r4.model.DateTimeType; -import org.hl7.fhir.r4.model.Endpoint; -import org.hl7.fhir.r4.model.Extension; import org.hl7.fhir.r4.model.Library; import org.hl7.fhir.r4.model.MetadataResource; -import org.hl7.fhir.r4.model.Parameters; -import org.hl7.fhir.r4.model.Reference; -import org.hl7.fhir.r4.model.Resource; import org.hl7.fhir.r4.model.ResourceType; import org.hl7.fhir.r4.model.StructureDefinition; import org.hl7.fhir.r4.model.UsageContext; -import org.hl7.fhir.r4.model.ValueSet; -import org.opencds.cqf.fhir.api.Repository; -import org.opencds.cqf.fhir.utility.Constants; -import org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter; -import org.opencds.cqf.fhir.utility.client.TerminologyServerClient; public class PackageVisitor { // private Logger logger = LoggerFactory.getLogger(KnowledgeArtifactPackageVisitor.class); - private final FhirContext fhirContext = FhirContext.forR4Cached(); + // private final FhirContext fhirContext = FhirContext.forR4Cached(); - public PackageVisitor() { - this.terminologyServerClient = new TerminologyServerClient(fhirContext); - } + // public PackageVisitor() { + // this.terminologyServerClient = new TerminologyServerClient(fhirContext); + // } - private TerminologyServerClient terminologyServerClient; + // private TerminologyServerClient terminologyServerClient; // as per http://hl7.org/fhir/R4/resource.html#canonical public static final List canonicalResourceTypes = @@ -220,223 +200,6 @@ public static List findUnsupportedInclude( return distinctFilteredEntries; } - /** - * ValueSets can be part of multiple artifacts at the same time. Certain properties are tracked/managed in the manifest to avoid conflicts with other artifacts. This function sets those properties on the ValueSets themselves at export / $package time - * @param manifest the resource containing all RelatedArtifact references - * @param bundleEntries the list of packaged resources to modify according to the extensions on the manifest relatedArtifact references - */ - public void handleValueSets( - MetadataResource manifest, - List bundleEntries, - Repository repository, - Optional terminologyEndpoint) - throws UnprocessableEntityException, IllegalArgumentException { - Parameters expansionParams = new Parameters(); - Library rootSpecificationLibrary = getRootSpecificationLibrary(bundleEntries); - if (rootSpecificationLibrary != null) { - Extension expansionParamsExtension = - rootSpecificationLibrary.getExtensionByUrl(Constants.CQF_EXPANSION_PARAMETERS); - if (expansionParamsExtension != null && expansionParamsExtension.hasValue()) { - Reference expansionReference = (Reference) expansionParamsExtension.getValue(); - expansionParams = getExpansionParams(rootSpecificationLibrary, expansionReference.getReference()); - } - } - var params = expansionParams; - var expandedList = new ArrayList(); - - var valueSets = bundleEntries.stream() - .filter(e -> e.hasResource() && e.getResource().fhirType().equals("ValueSet")) - .map(e -> (ValueSet) e.getResource()) - .collect(Collectors.toList()); - - valueSets.stream().forEach(valueSet -> { - expandValueSet(valueSet, params, terminologyEndpoint, valueSets, expandedList); - }); - } - - public void expandValueSet( - ValueSet valueSet, - Parameters expansionParameters, - Optional terminologyEndpoint, - List valueSets, - List expandedList) { - // Have we already expanded this ValueSet? - if (expandedList.contains(valueSet.getUrl())) { - // Nothing to do here - return; - } - - // Gather the Terminology Service from the valueSet's authoritativeSourceUrl. - var authoritativeSource = ((IBaseHasExtensions) valueSet) - .getExtension().stream() - .filter(e -> e.getUrl().equals(Constants.AUTHORITATIVE_SOURCE_URL)) - .findFirst() - .orElse(null); - @SuppressWarnings("unchecked") - var authoritativeSourceUrl = authoritativeSource == null - ? null - : ((IPrimitiveType) authoritativeSource.getValue()).getValueAsString(); - - // If terminologyEndpoint exists and we have no authoritativeSourceUrl or the authoritativeSourceUrl matches the - // terminologyEndpoint address then we will use the terminologyEndpoint for expansion - if (terminologyEndpoint.isPresent() - && (authoritativeSourceUrl == null - || authoritativeSourceUrl.equals( - terminologyEndpoint.get().getAddressElement().asStringValue()))) { - var username = terminologyEndpoint.get().getExtensionsByUrl(Constants.VSAC_USERNAME).stream() - .findFirst() - .map(ext -> ext.getValue().toString()) - .orElseThrow(() -> new UnprocessableEntityException( - "Cannot expand ValueSet without VSAC Username: " + valueSet.getId())); - var apiKey = terminologyEndpoint.get().getExtensionsByUrl(Constants.APIKEY).stream() - .findFirst() - .map(ext -> ext.getValue().toString()) - .orElseThrow(() -> new UnprocessableEntityException( - "Cannot expand ValueSet without VSAC API Key: " + valueSet.getId())); - - try { - var expandedValueSet = terminologyServerClient.expand( - valueSet, - terminologyEndpoint.get().getAddressElement().asStringValue(), - expansionParameters, - username, - apiKey); - valueSet.setExpansion(expandedValueSet.getExpansion()); - } catch (Exception ex) { - throw new UnprocessableEntityException( - "Terminology Server expansion failed for: " + valueSet.getId(), ex.getMessage()); - } - } - // Else if the ValueSet has a simple compose then we will perform naive expansion. - else if (hasSimpleCompose(valueSet)) { - // Perform naive expansion independent of terminology servers. Copy all codes from compose into expansion. - var expansion = new ValueSet.ValueSetExpansionComponent(new DateTimeType(Date.from(Instant.now()))); - - ArrayList expansionParams = new ArrayList<>(); - var parameterNaive = new ValueSet.ValueSetExpansionParameterComponent() - .setName("naive") - .setValue(new BooleanType(true)); - expansionParams.add(parameterNaive); - expansion.setParameter(expansionParams); - - for (var code : getCodesInCompose(fhirContext, valueSet)) { - expansion - .addContains() - .setCode(code.getCode()) - .setSystem(code.getSystem()) - .setVersion(code.getVersion()) - .setDisplay(code.getDisplay()); - } - valueSet.setExpansion(expansion); - } - // Else if the ValueSet has a grouping compose then we will attempt to group. - else if (hasGroupingCompose(valueSet)) { - var expansion = new ValueSet.ValueSetExpansionComponent(new DateTimeType(Date.from(Instant.now()))); - var includes = valueSet.getCompose().getInclude().stream() - .map(i -> i.getValueSet()) - .flatMap(Collection::stream) - .map(c -> c.asStringValue()) - .distinct() - .collect(Collectors.toList()); - includes.forEach(reference -> { - // Grab the ValueSet - var split = reference.split("\\|"); - var url = split.length == 1 ? reference : split[0]; - var version = split.length == 1 ? null : split[1]; - var vs = valueSets.stream() - .filter(v -> v.getUrl().equals(url) - && (version == null || v.getVersion().equals(version))) - .findFirst() - .orElse(null); - // Expand the ValueSet if we haven't already - if (!expandedList.contains(url)) { - expandValueSet(vs, expansionParameters, terminologyEndpoint, valueSets, expandedList); - } - vs.getExpansion().getContains().forEach(code -> { - // Add the code if not already present - if (expansion.getContains().stream() - .noneMatch(expandedCode -> code.getSystem().equals(expandedCode.getSystem()) - && code.getCode().equals(expandedCode.getCode()) - && (!code.hasVersion() || code.getVersion().equals(expandedCode.getVersion())))) { - expansion.addContains(code); - } - }); - // If any included expansion is naive it makes the expansion naive - var naiveParam = vs.getExpansion().getParameter().stream() - .filter(p -> p.getName().equals("naive")) - .findFirst() - .orElse(null); - if (naiveParam != null - && valueSet.getExpansion().getParameter().stream() - .noneMatch(p -> p.getName().equals("naive"))) { - expansion.addParameter(naiveParam); - } - }); - valueSet.setExpansion(expansion); - } else { - throw new UnprocessableEntityException( - "Cannot expand ValueSet without a terminology server: " + valueSet.getId()); - } - expandedList.add(valueSet.getUrl()); - } - - // A simple compose element of a ValueSet must have a compose without an exclude element. Each element of the - // include cannot have a filter or reference a ValueSet and must have a system and enumerate concepts. - protected boolean hasSimpleCompose(ValueSet valueSet) { - return valueSet.hasCompose() - && !valueSet.getCompose().hasExclude() - && valueSet.getCompose().getInclude().stream() - .noneMatch( - csc -> csc.hasFilter() || csc.hasValueSet() || !csc.hasSystem() || !csc.hasConcept()); - } - - // A grouping compose element of a ValueSet must have a compose without an exclude element and each element of the - // include must reference a ValueSet. - protected boolean hasGroupingCompose(ValueSet valueSet) { - return valueSet.hasCompose() - && !valueSet.getCompose().hasExclude() - && valueSet.getCompose().getInclude().stream().noneMatch(csc -> !csc.hasValueSet() || csc.hasFilter()); - } - - protected static Library getRootSpecificationLibrary(List bundleEntries) { - Optional rootSpecLibrary = bundleEntries.stream() - .filter(entry -> entry.getResource().getResourceType() == ResourceType.Library) - .map(entry -> ((Library) entry.getResource())) - .filter(entry -> entry.getType().hasCoding(Constants.LIBRARY_TYPE, Constants.ASSET_COLLECTION) - && entry.getUseContext().stream() - .allMatch(useContext -> (useContext - .getCode() - .getSystem() - .equals(KnowledgeArtifactAdapter.usPhContextTypeUrl) - && useContext - .getCode() - .getCode() - .equals("reporting") - && useContext - .getValueCodeableConcept() - .hasCoding(Constants.US_PH_CONTEXT_URL, "triggering")) - || (useContext - .getCode() - .getSystem() - .equals(KnowledgeArtifactAdapter.usPhContextTypeUrl) - && useContext - .getCode() - .getCode() - .equals("specification-type") - && useContext - .getValueCodeableConcept() - .hasCoding(Constants.US_PH_CONTEXT_URL, "program")))) - .findFirst(); - return rootSpecLibrary.orElse(null); - } - - protected static Parameters getExpansionParams(Library rootSpecificationLibrary, String reference) { - Optional expansionParamResource = rootSpecificationLibrary.getContained().stream() - .filter(contained -> contained.getId().equals(reference)) - .findFirst(); - return (Parameters) expansionParamResource.orElse(null); - } - /** * * Either finds a usageContext with the same system and code or creates an empty one diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r5/PackageVisitor.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r5/PackageVisitor.java index 15a0d849f..c62f951d4 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r5/PackageVisitor.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r5/PackageVisitor.java @@ -1,14 +1,9 @@ package org.opencds.cqf.fhir.utility.visitor.r5; -import static org.opencds.cqf.fhir.utility.ValueSets.getCodesInCompose; - import ca.uhn.fhir.context.FhirContext; -import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; -import java.time.Instant; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; -import java.util.Date; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; @@ -17,20 +12,11 @@ import org.hl7.fhir.r5.model.Bundle.BundleEntryComponent; import org.hl7.fhir.r5.model.Bundle.BundleType; import org.hl7.fhir.r5.model.Coding; -import org.hl7.fhir.r5.model.Endpoint; -import org.hl7.fhir.r5.model.Extension; import org.hl7.fhir.r5.model.Library; import org.hl7.fhir.r5.model.MetadataResource; -import org.hl7.fhir.r5.model.Parameters; -import org.hl7.fhir.r5.model.Reference; -import org.hl7.fhir.r5.model.Resource; import org.hl7.fhir.r5.model.ResourceType; import org.hl7.fhir.r5.model.StructureDefinition; import org.hl7.fhir.r5.model.UsageContext; -import org.hl7.fhir.r5.model.ValueSet; -import org.opencds.cqf.fhir.api.Repository; -import org.opencds.cqf.fhir.utility.Constants; -import org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter; import org.opencds.cqf.fhir.utility.client.TerminologyServerClient; public class PackageVisitor { @@ -211,146 +197,6 @@ public static List findUnsupportedInclude( return distinctFilteredEntries; } - /** - * ValueSets can be part of multiple artifacts at the same time. Certain properties are tracked/managed in the manifest to avoid conflicts with other artifacts. This function sets those properties on the ValueSets themselves at export / $package time - * @param manifest the resource containing all RelatedArtifact references - * @param bundleEntries the list of packaged resources to modify according to the extensions on the manifest relatedArtifact references - */ - public void handleValueSets( - MetadataResource manifest, - List bundleEntries, - Repository repository, - Optional terminologyEndpoint) - throws UnprocessableEntityException, IllegalArgumentException { - Parameters expansionParams = new Parameters(); - Library rootSpecificationLibrary = getRootSpecificationLibrary(bundleEntries); - if (rootSpecificationLibrary != null) { - Extension expansionParamsExtension = - rootSpecificationLibrary.getExtensionByUrl(Constants.CQF_EXPANSION_PARAMETERS); - if (expansionParamsExtension != null && expansionParamsExtension.hasValue()) { - Reference expansionReference = (Reference) expansionParamsExtension.getValue(); - expansionParams = getExpansionParams(rootSpecificationLibrary, expansionReference.getReference()); - } - } - Parameters params = expansionParams; - bundleEntries.stream().forEach(entry -> { - if (entry.getResource().getResourceType().equals(ResourceType.ValueSet)) { - ValueSet valueSet = (ValueSet) entry.getResource(); - expandValueSet(valueSet, params, terminologyEndpoint); - } - }); - } - - public void expandValueSet( - ValueSet valueSet, Parameters expansionParameters, Optional terminologyEndpoint) { - // Gather the Terminology Service from the valueSet's authoritativeSourceUrl. - Extension authoritativeSource = valueSet.getExtensionByUrl(Constants.AUTHORITATIVE_SOURCE_URL); - String authoritativeSourceUrl = authoritativeSource != null && authoritativeSource.hasValue() - ? authoritativeSource.getValue().primitiveValue() - : valueSet.getUrl(); - - ValueSet expandedValueSet; - if (hasSimpleCompose(valueSet)) { - // Perform naive expansion independent of terminology servers. Copy all codes from compose into expansion. - ValueSet.ValueSetExpansionComponent expansion = new ValueSet.ValueSetExpansionComponent(); - expansion.setTimestamp(Date.from(Instant.now())); - - ArrayList expansionParams = new ArrayList<>(); - ValueSet.ValueSetExpansionParameterComponent parameterNaive = - new ValueSet.ValueSetExpansionParameterComponent(); - parameterNaive.setName("naive"); - parameterNaive.setValue(new BooleanType(true)); - expansionParams.add(parameterNaive); - expansion.setParameter(expansionParams); - - for (var code : getCodesInCompose(fhirContext, valueSet)) { - expansion - .addContains() - .setCode(code.getCode()) - .setSystem(code.getSystem()) - .setVersion(code.getVersion()) - .setDisplay(code.getDisplay()); - } - valueSet.setExpansion(expansion); - } else { - String username; - String apiKey; - if (terminologyEndpoint.isPresent()) { - username = terminologyEndpoint.get().getExtensionsByUrl(Constants.VSAC_USERNAME).stream() - .findFirst() - .map(ext -> ext.getValue().toString()) - .orElseThrow(() -> new UnprocessableEntityException( - "Cannot expand ValueSet without VSAC Username: " + valueSet.getId())); - apiKey = terminologyEndpoint.get().getExtensionsByUrl(Constants.APIKEY).stream() - .findFirst() - .map(ext -> ext.getValue().toString()) - .orElseThrow(() -> new UnprocessableEntityException( - "Cannot expand ValueSet without VSAC API Key: " + valueSet.getId())); - } else { - throw new UnprocessableEntityException( - "Cannot expand ValueSet without a terminology server: " + valueSet.getId()); - } - - try { - expandedValueSet = terminologyServerClient.expand( - valueSet, authoritativeSourceUrl, expansionParameters, username, apiKey); - valueSet.setExpansion(expandedValueSet.getExpansion()); - } catch (Exception ex) { - throw new UnprocessableEntityException( - "Terminology Server expansion failed for: " + valueSet.getId(), ex.getMessage()); - } - } - } - - // A simple compose element of a ValueSet must have a compose without an exclude element. Each element of the - // include cannot have a filter, and must reference a ValueSet or have a system and enumerate concepts - protected boolean hasSimpleCompose(ValueSet valueSet) { - return valueSet.hasCompose() - && !valueSet.getCompose().hasExclude() - && valueSet.getCompose().getInclude().stream() - .noneMatch(csc -> - csc.hasFilter() || (!csc.hasValueSet() && (!csc.hasSystem() && !csc.hasConcept()))); - } - - protected static Library getRootSpecificationLibrary(List bundleEntries) { - Optional rootSpecLibrary = bundleEntries.stream() - .filter(entry -> entry.getResource().getResourceType() == ResourceType.Library) - .map(entry -> ((Library) entry.getResource())) - .filter(entry -> entry.getType().hasCoding(Constants.LIBRARY_TYPE, Constants.ASSET_COLLECTION) - && entry.getUseContext().stream() - .allMatch(useContext -> (useContext - .getCode() - .getSystem() - .equals(KnowledgeArtifactAdapter.usPhContextTypeUrl) - && useContext - .getCode() - .getCode() - .equals("reporting") - && useContext - .getValueCodeableConcept() - .hasCoding(Constants.US_PH_CONTEXT_URL, "triggering")) - || (useContext - .getCode() - .getSystem() - .equals(KnowledgeArtifactAdapter.usPhContextTypeUrl) - && useContext - .getCode() - .getCode() - .equals("specification-type") - && useContext - .getValueCodeableConcept() - .hasCoding(Constants.US_PH_CONTEXT_URL, "program")))) - .findFirst(); - return rootSpecLibrary.orElse(null); - } - - protected static Parameters getExpansionParams(Library rootSpecificationLibrary, String reference) { - Optional expansionParamResource = rootSpecificationLibrary.getContained().stream() - .filter(contained -> contained.getId().equals(reference)) - .findFirst(); - return (Parameters) expansionParamResource.orElse(null); - } - /** * * Either finds a usageContext with the same system and code or creates an empty one diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/PlanDefinitionAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/PlanDefinitionAdapterTest.java index be41f896a..15262ace0 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/PlanDefinitionAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/PlanDefinitionAdapterTest.java @@ -8,6 +8,7 @@ import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import ca.uhn.fhir.context.FhirContext; import java.util.Date; import java.util.List; import org.hl7.fhir.dstu3.model.Bundle; @@ -22,9 +23,11 @@ import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; public class PlanDefinitionAdapterTest { + private final FhirContext fhirContext = FhirContext.forDstu3Cached(); + @Test void adapter_accepts_visitor() { - var spyVisitor = spy(new PackageVisitor()); + var spyVisitor = spy(new PackageVisitor(fhirContext)); doReturn(new Bundle()).when(spyVisitor).visit(any(PlanDefinitionAdapter.class), any(), any()); IDomainResource planDef = new PlanDefinition(); var adapter = new PlanDefinitionAdapter(planDef); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/PlanDefinitionAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/PlanDefinitionAdapterTest.java index dc5090534..862115f3f 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/PlanDefinitionAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/PlanDefinitionAdapterTest.java @@ -8,6 +8,7 @@ import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import ca.uhn.fhir.context.FhirContext; import java.util.Date; import java.util.List; import org.hl7.fhir.instance.model.api.IDomainResource; @@ -20,9 +21,11 @@ import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; public class PlanDefinitionAdapterTest { + private final FhirContext fhirContext = FhirContext.forR4Cached(); + @Test void adapter_accepts_visitor() { - var spyVisitor = spy(new PackageVisitor()); + var spyVisitor = spy(new PackageVisitor(fhirContext)); doReturn(new Bundle()).when(spyVisitor).visit(any(PlanDefinitionAdapter.class), any(), any()); IDomainResource planDef = new PlanDefinition(); var adapter = new PlanDefinitionAdapter(planDef); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/PlanDefinitionAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/PlanDefinitionAdapterTest.java index f7cd39125..73c49adc7 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/PlanDefinitionAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/PlanDefinitionAdapterTest.java @@ -8,6 +8,7 @@ import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import ca.uhn.fhir.context.FhirContext; import java.util.Date; import java.util.List; import org.hl7.fhir.instance.model.api.IDomainResource; @@ -20,9 +21,11 @@ import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; public class PlanDefinitionAdapterTest { + private final FhirContext fhirContext = FhirContext.forR5Cached(); + @Test void adapter_accepts_visitor() { - var spyVisitor = spy(new PackageVisitor()); + var spyVisitor = spy(new PackageVisitor(fhirContext)); doReturn(new Bundle()).when(spyVisitor).visit(any(PlanDefinitionAdapter.class), any(), any()); IDomainResource planDef = new PlanDefinition(); var adapter = new PlanDefinitionAdapter(planDef); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/client/TerminologyServerClientTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/client/TerminologyServerClientTest.java index 565a67392..46448e90b 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/client/TerminologyServerClientTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/client/TerminologyServerClientTest.java @@ -6,14 +6,20 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.context.FhirVersionEnum; import ca.uhn.fhir.rest.client.impl.GenericClient; import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; import org.mockito.internal.stubbing.defaultanswers.ReturnsDeepStubs; +import org.opencds.cqf.fhir.utility.Constants; +import org.opencds.cqf.fhir.utility.adapter.AdapterFactory; +import org.opencds.cqf.fhir.utility.adapter.ValueSetAdapter; public class TerminologyServerClientTest { private static final String url = "www.test.com"; @@ -24,14 +30,23 @@ public class TerminologyServerClientTest { private static final String urlParamName = "url"; private static final String versionParamName = "valueSetVersion"; + private FhirContext fhirContextDstu3 = FhirContext.forDstu3Cached(); + private FhirContext fhirContextR4 = FhirContext.forR4Cached(); + private FhirContext fhirContextR5 = FhirContext.forR5Cached(); + @Test void testR4UrlAndVersion() { - var vs = new org.hl7.fhir.r4.model.ValueSet(); - vs.setUrl(url); + var factory = AdapterFactory.forFhirVersion(FhirVersionEnum.R4); + var valueSet = (ValueSetAdapter) factory.createKnowledgeArtifactAdapter(new org.hl7.fhir.r4.model.ValueSet()); + valueSet.setUrl(url); + var endpoint = factory.createEndpoint(new org.hl7.fhir.r4.model.Endpoint()); + endpoint.setAddress(authoritativeSource); + endpoint.addExtension(new org.hl7.fhir.r4.model.Extension( + Constants.VSAC_USERNAME, new org.hl7.fhir.r4.model.StringType(username))); + endpoint.addExtension( + new org.hl7.fhir.r4.model.Extension(Constants.APIKEY, new org.hl7.fhir.r4.model.StringType(password))); var capt = ArgumentCaptor.forClass(org.hl7.fhir.r4.model.Parameters.class); var clientMock = mock(GenericClient.class, new ReturnsDeepStubs()); - var contextMock = mock(FhirContext.class); - when(contextMock.newRestfulGenericClient(any())).thenReturn(clientMock); when(clientMock .operation() .onType(anyString()) @@ -39,10 +54,13 @@ void testR4UrlAndVersion() { .withParameters(capt.capture()) .returnResourceType(any()) .execute()) - .thenReturn(vs); + .thenReturn(valueSet.get()); + var contextSpy = spy(fhirContextR4); + doReturn(clientMock).when(contextSpy).newRestfulGenericClient(any()); - var client = new TerminologyServerClient(contextMock); - client.expand(vs, authoritativeSource, new org.hl7.fhir.r4.model.Parameters(), username, password); + var client = new TerminologyServerClient(contextSpy); + var parameters = factory.createParameters(new org.hl7.fhir.r4.model.Parameters()); + client.expand(valueSet, endpoint, parameters); assertNotNull(capt.getValue()); var params = capt.getValue(); assertEquals( @@ -53,8 +71,8 @@ void testR4UrlAndVersion() { assertNull(params.getParameter(versionParamName)); // when the valueset has a version it should be in params - vs.setVersion(version); - client.expand(vs, authoritativeSource, new org.hl7.fhir.r4.model.Parameters(), username, password); + valueSet.setVersion(version); + client.expand(valueSet, endpoint, parameters); params = capt.getAllValues().get(1); assertEquals( version, @@ -65,12 +83,17 @@ void testR4UrlAndVersion() { @Test void testR5UrlAndVersion() { - var vs = new org.hl7.fhir.r5.model.ValueSet(); - vs.setUrl(url); + var factory = AdapterFactory.forFhirVersion(FhirVersionEnum.R5); + var valueSet = (ValueSetAdapter) factory.createKnowledgeArtifactAdapter(new org.hl7.fhir.r5.model.ValueSet()); + valueSet.setUrl(url); + var endpoint = factory.createEndpoint(new org.hl7.fhir.r5.model.Endpoint()); + endpoint.setAddress(authoritativeSource); + endpoint.addExtension(new org.hl7.fhir.r5.model.Extension( + Constants.VSAC_USERNAME, new org.hl7.fhir.r5.model.StringType(username))); + endpoint.addExtension( + new org.hl7.fhir.r5.model.Extension(Constants.APIKEY, new org.hl7.fhir.r5.model.StringType(password))); var capt = ArgumentCaptor.forClass(org.hl7.fhir.r5.model.Parameters.class); var clientMock = mock(GenericClient.class, new ReturnsDeepStubs()); - var contextMock = mock(FhirContext.class); - when(contextMock.newRestfulGenericClient(any())).thenReturn(clientMock); when(clientMock .operation() .onType(anyString()) @@ -78,10 +101,13 @@ void testR5UrlAndVersion() { .withParameters(capt.capture()) .returnResourceType(any()) .execute()) - .thenReturn(vs); + .thenReturn(valueSet.get()); + var contextSpy = spy(fhirContextR5); + doReturn(clientMock).when(contextSpy).newRestfulGenericClient(any()); - var client = new TerminologyServerClient(contextMock); - client.expand(vs, authoritativeSource, new org.hl7.fhir.r5.model.Parameters(), username, password); + var client = new TerminologyServerClient(contextSpy); + var parameters = factory.createParameters(new org.hl7.fhir.r5.model.Parameters()); + client.expand(valueSet, endpoint, parameters); assertNotNull(capt.getValue()); var params = capt.getValue(); assertEquals( @@ -92,8 +118,8 @@ void testR5UrlAndVersion() { assertNull(params.getParameter(versionParamName)); // when the valueset has a version it should be in params - vs.setVersion(version); - client.expand(vs, authoritativeSource, new org.hl7.fhir.r5.model.Parameters(), username, password); + valueSet.setVersion(version); + client.expand(valueSet, endpoint, parameters); params = capt.getAllValues().get(1); assertEquals( version, @@ -104,12 +130,18 @@ void testR5UrlAndVersion() { @Test void testDstu3UrlAndVersion() { - var vs = new org.hl7.fhir.dstu3.model.ValueSet(); - vs.setUrl(url); + var factory = AdapterFactory.forFhirVersion(FhirVersionEnum.DSTU3); + var valueSet = + (ValueSetAdapter) factory.createKnowledgeArtifactAdapter(new org.hl7.fhir.dstu3.model.ValueSet()); + valueSet.setUrl(url); + var endpoint = factory.createEndpoint(new org.hl7.fhir.dstu3.model.Endpoint()); + endpoint.setAddress(authoritativeSource); + endpoint.addExtension(new org.hl7.fhir.dstu3.model.Extension( + Constants.VSAC_USERNAME, new org.hl7.fhir.dstu3.model.StringType(username))); + endpoint.addExtension(new org.hl7.fhir.dstu3.model.Extension( + Constants.APIKEY, new org.hl7.fhir.dstu3.model.StringType(password))); var capt = ArgumentCaptor.forClass(org.hl7.fhir.dstu3.model.Parameters.class); var clientMock = mock(GenericClient.class, new ReturnsDeepStubs()); - var contextMock = mock(FhirContext.class); - when(contextMock.newRestfulGenericClient(any())).thenReturn(clientMock); when(clientMock .operation() .onType(anyString()) @@ -117,10 +149,13 @@ void testDstu3UrlAndVersion() { .withParameters(capt.capture()) .returnResourceType(any()) .execute()) - .thenReturn(vs); + .thenReturn(valueSet.get()); + var contextSpy = spy(fhirContextDstu3); + doReturn(clientMock).when(contextSpy).newRestfulGenericClient(any()); - var client = new TerminologyServerClient(contextMock); - client.expand(vs, authoritativeSource, new org.hl7.fhir.dstu3.model.Parameters(), username, password); + var client = new TerminologyServerClient(contextSpy); + var parameters = factory.createParameters(new org.hl7.fhir.dstu3.model.Parameters()); + client.expand(valueSet, endpoint, parameters); assertNotNull(capt.getValue()); var params = capt.getValue(); assertEquals( @@ -134,8 +169,8 @@ void testDstu3UrlAndVersion() { assertTrue(!params.getParameter().stream().anyMatch(p -> p.getName().equals(versionParamName))); // when the valueset has a version it should be in params - vs.setVersion(version); - client.expand(vs, authoritativeSource, new org.hl7.fhir.dstu3.model.Parameters(), username, password); + valueSet.setVersion(version); + client.expand(valueSet, endpoint, parameters); params = capt.getAllValues().get(1); assertEquals( version, diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactPackageVisitorTests.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactPackageVisitorTests.java index 5826cf9f8..f3f473f0e 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactPackageVisitorTests.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactPackageVisitorTests.java @@ -71,7 +71,7 @@ void visitLibraryTest() { Bundle loadedBundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example-naive.json")); spyRepository.transaction(loadedBundle); - PackageVisitor packageVisitor = new PackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -104,7 +104,7 @@ void packageOperation_should_fail_no_credentials() { Bundle loadedBundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); spyRepository.transaction(loadedBundle); - PackageVisitor packageVisitor = new PackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -126,7 +126,7 @@ void packageOperation_should_fail_credentials_missing_username() { Bundle loadedBundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); spyRepository.transaction(loadedBundle); - PackageVisitor packageVisitor = new PackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -152,7 +152,7 @@ void packageOperation_should_fail_credentials_missing_apikey() { Bundle loadedBundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); spyRepository.transaction(loadedBundle); - PackageVisitor packageVisitor = new PackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -178,7 +178,7 @@ void packageOperation_should_fail_credentials_invalid() { Bundle loadedBundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); spyRepository.transaction(loadedBundle); - PackageVisitor packageVisitor = new PackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -206,7 +206,7 @@ void packageOperation_should_fail_non_matching_capability() { "Bundle-ersd-package-capabilities.json")); spyRepository.transaction(bundle); List capabilities = Arrays.asList("computable", "publishable", "executable"); - PackageVisitor packageVisitor = new PackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -238,7 +238,7 @@ void packageOperation_should_apply_check_force_canonicalVersions() { Bundle bundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-active-no-versions.json")); spyRepository.transaction(bundle); - PackageVisitor packageVisitor = new PackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -299,7 +299,7 @@ void packageOperation_should_respect_count_offset() { Bundle bundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); spyRepository.transaction(bundle); - PackageVisitor packageVisitor = new PackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -337,7 +337,7 @@ void packageOperation_different_bundle_types() { Bundle bundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); spyRepository.transaction(bundle); - PackageVisitor packageVisitor = new PackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -376,7 +376,7 @@ void packageOperation_should_conditionally_create() { Bundle bundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); spyRepository.transaction(bundle); - PackageVisitor packageVisitor = new PackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -396,7 +396,7 @@ void packageOperation_should_respect_include() { Bundle bundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); spyRepository.transaction(bundle); - PackageVisitor packageVisitor = new PackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactPackageVisitorTests.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactPackageVisitorTests.java index 54ffc5fca..9bf5a89be 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactPackageVisitorTests.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactPackageVisitorTests.java @@ -71,7 +71,7 @@ void visitLibraryTest() { Bundle loadedBundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example-naive.json")); spyRepository.transaction(loadedBundle); - PackageVisitor packageVisitor = new PackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -104,7 +104,7 @@ void packageOperation_should_fail_no_credentials() { Bundle loadedBundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); spyRepository.transaction(loadedBundle); - PackageVisitor packageVisitor = new PackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -125,7 +125,7 @@ void packageOperation_should_fail_credentials_missing_username() { Bundle loadedBundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); spyRepository.transaction(loadedBundle); - PackageVisitor packageVisitor = new PackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -141,7 +141,7 @@ void packageOperation_should_fail_credentials_missing_username() { } catch (UnprocessableEntityException e) { maybeException = e; } - assertTrue(maybeException.getMessage().contains("Cannot expand ValueSet without VSAC Username: ")); + assertTrue(maybeException.getMessage().contains("Cannot expand ValueSet without VSAC Username.")); } @Test @@ -149,7 +149,7 @@ void packageOperation_should_fail_credentials_missing_apikey() { Bundle loadedBundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); spyRepository.transaction(loadedBundle); - PackageVisitor packageVisitor = new PackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -165,7 +165,7 @@ void packageOperation_should_fail_credentials_missing_apikey() { } catch (UnprocessableEntityException e) { maybeException = e; } - assertTrue(maybeException.getMessage().contains("Cannot expand ValueSet without VSAC API Key: ")); + assertTrue(maybeException.getMessage().contains("Cannot expand ValueSet without VSAC API Key.")); } @Test @@ -173,7 +173,7 @@ void packageOperation_should_fail_credentials_invalid() { Bundle loadedBundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); spyRepository.transaction(loadedBundle); - PackageVisitor packageVisitor = new PackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -189,7 +189,7 @@ void packageOperation_should_fail_credentials_invalid() { } catch (UnprocessableEntityException e) { maybeException = e; } - assertTrue(maybeException.getMessage().contains("Terminology Server expansion failed for:")); + assertTrue(maybeException.getMessage().contains("Terminology Server expansion failed for ValueSet ")); } @Test @@ -199,7 +199,7 @@ void packageOperation_should_fail_non_matching_capability() { "Bundle-ersd-package-capabilities.json")); spyRepository.transaction(bundle); List capabilities = Arrays.asList("computable", "publishable", "executable"); - PackageVisitor packageVisitor = new PackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -231,7 +231,7 @@ void packageOperation_should_apply_check_force_canonicalVersions() { Bundle bundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-active-no-versions.json")); spyRepository.transaction(bundle); - PackageVisitor packageVisitor = new PackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -293,7 +293,7 @@ void packageOperation_should_respect_count_offset() { Bundle bundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); spyRepository.transaction(bundle); - PackageVisitor packageVisitor = new PackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -331,7 +331,7 @@ void packageOperation_different_bundle_types() { Bundle bundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); spyRepository.transaction(bundle); - PackageVisitor packageVisitor = new PackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -370,7 +370,7 @@ void packageOperation_should_conditionally_create() { Bundle bundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); spyRepository.transaction(bundle); - PackageVisitor packageVisitor = new PackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -390,7 +390,7 @@ void packageOperation_should_respect_include() { Bundle bundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); spyRepository.transaction(bundle); - PackageVisitor packageVisitor = new PackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactPackageVisitorTests.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactPackageVisitorTests.java index 326addcd7..cf9da2723 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactPackageVisitorTests.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactPackageVisitorTests.java @@ -71,7 +71,7 @@ void visitLibraryTest() { Bundle loadedBundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example-naive.json")); spyRepository.transaction(loadedBundle); - PackageVisitor packageVisitor = new PackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -104,7 +104,7 @@ void packageOperation_should_fail_no_credentials() { Bundle loadedBundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); spyRepository.transaction(loadedBundle); - PackageVisitor packageVisitor = new PackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -126,7 +126,7 @@ void packageOperation_should_fail_credentials_missing_username() { Bundle loadedBundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); spyRepository.transaction(loadedBundle); - PackageVisitor packageVisitor = new PackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -152,7 +152,7 @@ void packageOperation_should_fail_credentials_missing_apikey() { Bundle loadedBundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); spyRepository.transaction(loadedBundle); - PackageVisitor packageVisitor = new PackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -178,7 +178,7 @@ void packageOperation_should_fail_credentials_invalid() { Bundle loadedBundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); spyRepository.transaction(loadedBundle); - PackageVisitor packageVisitor = new PackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -206,7 +206,7 @@ void packageOperation_should_fail_non_matching_capability() { "Bundle-ersd-package-capabilities.json")); spyRepository.transaction(bundle); List capabilities = Arrays.asList("computable", "publishable", "executable"); - PackageVisitor packageVisitor = new PackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -238,7 +238,7 @@ void packageOperation_should_apply_check_force_canonicalVersions() { Bundle bundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-active-no-versions.json")); spyRepository.transaction(bundle); - PackageVisitor packageVisitor = new PackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -300,7 +300,7 @@ void packageOperation_should_respect_count_offset() { Bundle bundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); spyRepository.transaction(bundle); - PackageVisitor packageVisitor = new PackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -338,7 +338,7 @@ void packageOperation_different_bundle_types() { Bundle bundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); spyRepository.transaction(bundle); - PackageVisitor packageVisitor = new PackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -377,7 +377,7 @@ void packageOperation_should_conditionally_create() { Bundle bundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); spyRepository.transaction(bundle); - PackageVisitor packageVisitor = new PackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); @@ -397,7 +397,7 @@ void packageOperation_should_respect_include() { Bundle bundle = (Bundle) jsonParser.parseResource( KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); spyRepository.transaction(bundle); - PackageVisitor packageVisitor = new PackageVisitor(); + PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); From aff70bae272f33a8bd2719880295ee2e109b838c Mon Sep 17 00:00:00 2001 From: Brenin Rhodes Date: Fri, 28 Jun 2024 12:45:20 -0600 Subject: [PATCH 07/38] Add tests --- .../org/opencds/cqf/fhir/cql/Engines.java | 18 +- .../fhir/utility/adapter/AdapterFactory.java | 5 + .../dstu3/KnowledgeArtifactAdapter.java | 10 + .../adapter/dstu3/QuestionnaireAdapter.java | 12 ++ .../dstu3/StructureDefinitionAdapter.java | 178 ++++++++++++++++- .../adapter/r4/KnowledgeArtifactAdapter.java | 17 +- .../adapter/r4/QuestionnaireAdapter.java | 12 ++ .../r4/StructureDefinitionAdapter.java | 188 +++++++++++++++++- .../adapter/r5/QuestionnaireAdapter.java | 12 ++ .../r5/StructureDefinitionAdapter.java | 10 + .../utility/visitor/dstu3/PackageVisitor.java | 38 ---- .../utility/visitor/r4/PackageVisitor.java | 37 ---- .../utility/visitor/r5/PackageVisitor.java | 38 ---- .../dstu3/QuestionnaireAdapaterTest.java | 65 ++++++ .../dstu3/StructureDefinitionAdapterTest.java | 65 ++++++ .../adapter/r4/QuestionnaireAdapaterTest.java | 65 ++++++ .../r4/StructureDefinitionAdapterTest.java | 65 ++++++ .../adapter/r5/QuestionnaireAdapaterTest.java | 65 ++++++ .../r5/StructureDefinitionAdapterTest.java | 65 ++++++ ...sitorTest.java => ApproveVisitorTest.java} | 10 +- ...sitorTests.java => DraftVisitorTests.java} | 26 +-- ...torTests.java => PackageVisitorTests.java} | 35 ++-- ...torTests.java => ReleaseVisitorTests.java} | 51 ++--- ...sitorTest.java => ApproveVisitorTest.java} | 10 +- ...sitorTests.java => DraftVisitorTests.java} | 26 +-- ...torTests.java => PackageVisitorTests.java} | 35 ++-- ...torTests.java => ReleaseVisitorTests.java} | 53 +++-- ...sitorTest.java => ApproveVisitorTest.java} | 10 +- ...sitorTests.java => DraftVisitorTests.java} | 26 +-- ...torTests.java => PackageVisitorTests.java} | 35 ++-- ...torTests.java => ReleaseVisitorTests.java} | 51 ++--- 31 files changed, 995 insertions(+), 338 deletions(-) create mode 100644 cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/QuestionnaireAdapaterTest.java create mode 100644 cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/StructureDefinitionAdapterTest.java create mode 100644 cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/QuestionnaireAdapaterTest.java create mode 100644 cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/StructureDefinitionAdapterTest.java create mode 100644 cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapaterTest.java create mode 100644 cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapterTest.java rename cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/{KnowledgeArtifactApproveVisitorTest.java => ApproveVisitorTest.java} (95%) rename cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/{KnowledgeArtifactDraftVisitorTests.java => DraftVisitorTests.java} (90%) rename cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/{KnowledgeArtifactPackageVisitorTests.java => PackageVisitorTests.java} (93%) rename cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/{KnowledgeArtifactReleaseVisitorTests.java => ReleaseVisitorTests.java} (92%) rename cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/{KnowledgeArtifactApproveVisitorTest.java => ApproveVisitorTest.java} (95%) rename cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/{KnowledgeArtifactDraftVisitorTests.java => DraftVisitorTests.java} (90%) rename cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/{KnowledgeArtifactPackageVisitorTests.java => PackageVisitorTests.java} (93%) rename cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/{KnowledgeArtifactReleaseVisitorTests.java => ReleaseVisitorTests.java} (91%) rename cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/{KnowledgeArtifactApproveVisitorTest.java => ApproveVisitorTest.java} (96%) rename cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/{KnowledgeArtifactDraftVisitorTests.java => DraftVisitorTests.java} (90%) rename cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/{KnowledgeArtifactPackageVisitorTests.java => PackageVisitorTests.java} (93%) rename cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/{KnowledgeArtifactReleaseVisitorTests.java => ReleaseVisitorTests.java} (92%) diff --git a/cqf-fhir-cql/src/main/java/org/opencds/cqf/fhir/cql/Engines.java b/cqf-fhir-cql/src/main/java/org/opencds/cqf/fhir/cql/Engines.java index f54d322a5..739797df2 100644 --- a/cqf-fhir-cql/src/main/java/org/opencds/cqf/fhir/cql/Engines.java +++ b/cqf-fhir-cql/src/main/java/org/opencds/cqf/fhir/cql/Engines.java @@ -106,7 +106,7 @@ public static CqlEngine forRepositoryAndSettings( } private static LibrarySourceProvider buildLibrarySource(Repository repository) { - AdapterFactory adapterFactory = getAdapterFactory(repository.fhirContext()); + var adapterFactory = AdapterFactory.forFhirContext(repository.fhirContext()); return new RepositoryFhirLibrarySourceProvider( repository, adapterFactory, new LibraryVersionSelector(adapterFactory)); } @@ -186,22 +186,10 @@ private static Map buildDataProviders( return dataProviders; } - public static AdapterFactory getAdapterFactory(FhirContext fhirContext) { - switch (fhirContext.getVersion().getVersion()) { - case DSTU3: - return new org.opencds.cqf.fhir.utility.adapter.dstu3.AdapterFactory(); - case R4: - return new org.opencds.cqf.fhir.utility.adapter.r4.AdapterFactory(); - case R5: - return new org.opencds.cqf.fhir.utility.adapter.r5.AdapterFactory(); - default: - throw new IllegalArgumentException(String.format("unsupported FHIR version: %s", fhirContext)); - } - } - public static CqlFhirParametersConverter getCqlFhirParametersConverter(FhirContext fhirContext) { var fhirTypeConverter = new FhirTypeConverterFactory().create(fhirContext.getVersion().getVersion()); - return new CqlFhirParametersConverter(fhirContext, getAdapterFactory(fhirContext), fhirTypeConverter); + return new CqlFhirParametersConverter( + fhirContext, AdapterFactory.forFhirContext(fhirContext), fhirTypeConverter); } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/AdapterFactory.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/AdapterFactory.java index 7e7a23aeb..5566c10bc 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/AdapterFactory.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/AdapterFactory.java @@ -1,5 +1,6 @@ package org.opencds.cqf.fhir.utility.adapter; +import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.context.FhirVersionEnum; import org.hl7.fhir.instance.model.api.IBaseBackboneElement; import org.hl7.fhir.instance.model.api.IBaseParameters; @@ -9,6 +10,10 @@ public interface AdapterFactory { + public static AdapterFactory forFhirContext(FhirContext fhirContext) { + return forFhirVersion(fhirContext.getVersion().getVersion()); + } + public static AdapterFactory forFhirVersion(FhirVersionEnum fhirVersion) { switch (fhirVersion) { case DSTU3: diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/KnowledgeArtifactAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/KnowledgeArtifactAdapter.java index 726a16109..32c8587b2 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/KnowledgeArtifactAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/KnowledgeArtifactAdapter.java @@ -16,6 +16,7 @@ import org.hl7.fhir.instance.model.api.IBaseExtension; import org.hl7.fhir.instance.model.api.IBaseHasExtensions; import org.hl7.fhir.instance.model.api.ICompositeType; +import org.hl7.fhir.instance.model.api.IDomainResource; import org.hl7.fhir.instance.model.api.IPrimitiveType; import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; @@ -23,6 +24,15 @@ public class KnowledgeArtifactAdapter extends ResourceAdapter implements org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter { MetadataResource adaptedResource; + public KnowledgeArtifactAdapter(IDomainResource resource) { + super(resource); + if (!(resource instanceof MetadataResource)) { + throw new IllegalArgumentException( + "resource passed as resource argument is not a MetadataResource resource"); + } + this.adaptedResource = (MetadataResource) resource; + } + public KnowledgeArtifactAdapter(MetadataResource resource) { super(resource); this.adaptedResource = resource; diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/QuestionnaireAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/QuestionnaireAdapter.java index 51db90e7d..4d54f847f 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/QuestionnaireAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/QuestionnaireAdapter.java @@ -4,6 +4,7 @@ import java.util.List; import org.hl7.fhir.dstu3.model.Questionnaire; import org.hl7.fhir.dstu3.model.UriType; +import org.hl7.fhir.instance.model.api.IDomainResource; import org.opencds.cqf.fhir.utility.Constants; import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; @@ -12,6 +13,17 @@ public class QuestionnaireAdapter extends KnowledgeArtifactAdapter { private Questionnaire questionnaire; + public QuestionnaireAdapter(IDomainResource questionnaire) { + super(questionnaire); + + if (!(questionnaire instanceof Questionnaire)) { + throw new IllegalArgumentException( + "resource passed as questionnaire argument is not a Questionnaire resource"); + } + + this.questionnaire = (Questionnaire) questionnaire; + } + public QuestionnaireAdapter(Questionnaire questionnaire) { super(questionnaire); this.questionnaire = questionnaire; diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/StructureDefinitionAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/StructureDefinitionAdapter.java index 8bf2803e8..0f04e9a61 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/StructureDefinitionAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/StructureDefinitionAdapter.java @@ -1,17 +1,42 @@ package org.opencds.cqf.fhir.utility.adapter.dstu3; +import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; import java.util.ArrayList; +import java.util.Date; import java.util.List; +import java.util.stream.Collectors; +import org.hl7.fhir.dstu3.model.DateTimeType; +import org.hl7.fhir.dstu3.model.Enumerations.PublicationStatus; +import org.hl7.fhir.dstu3.model.Extension; +import org.hl7.fhir.dstu3.model.Period; +import org.hl7.fhir.dstu3.model.RelatedArtifact; +import org.hl7.fhir.dstu3.model.RelatedArtifact.RelatedArtifactType; import org.hl7.fhir.dstu3.model.StructureDefinition; import org.hl7.fhir.dstu3.model.UriType; +import org.hl7.fhir.exceptions.FHIRException; +import org.hl7.fhir.instance.model.api.IBaseExtension; +import org.hl7.fhir.instance.model.api.IBaseHasExtensions; +import org.hl7.fhir.instance.model.api.ICompositeType; +import org.hl7.fhir.instance.model.api.IDomainResource; +import org.hl7.fhir.instance.model.api.IPrimitiveType; import org.opencds.cqf.fhir.utility.Constants; import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; +import org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter; -public class StructureDefinitionAdapter extends KnowledgeArtifactAdapter { +public class StructureDefinitionAdapter extends ResourceAdapter implements KnowledgeArtifactAdapter { private StructureDefinition structureDefinition; + public StructureDefinitionAdapter(IDomainResource structureDefinition) { + super(structureDefinition); + if (!(structureDefinition instanceof StructureDefinition)) { + throw new IllegalArgumentException( + "resource passed as planDefinition argument is not a StructureDefinition resource"); + } + this.structureDefinition = (StructureDefinition) structureDefinition; + } + public StructureDefinitionAdapter(StructureDefinition structureDefinition) { super(structureDefinition); this.structureDefinition = structureDefinition; @@ -55,4 +80,155 @@ public List getDependencies() { return references; } + + @Override + public StructureDefinition get() { + return structureDefinition; + } + + @Override + public StructureDefinition copy() { + return get().copy(); + } + + @Override + public String getUrl() { + return get().getUrl(); + } + + @Override + public boolean hasUrl() { + return get().hasUrl(); + } + + @Override + public void setUrl(String url) { + get().setUrl(url); + } + + @Override + public void setVersion(String version) { + get().setVersion(version); + } + + @Override + public String getVersion() { + return get().getVersion(); + } + + @Override + public boolean hasVersion() { + return get().hasVersion(); + } + + @Override + public String getName() { + return get().getName(); + } + + @Override + public void setName(String name) { + get().setName(name); + } + + @Override + public Date getApprovalDate() { + return null; + } + + @Override + public Date getDate() { + return get().getDate(); + } + + @Override + public void setDate(Date approvalDate) { + get().setDate(approvalDate); + } + + @Override + public void setDateElement(IPrimitiveType date) { + if (date != null && !(date instanceof DateTimeType)) { + throw new UnprocessableEntityException("Date must be " + DateTimeType.class.getName()); + } + get().setDateElement((DateTimeType) date); + } + + @Override + public void setApprovalDate(Date approvalDate) { + // do nothing + } + + @Override + public Period getEffectivePeriod() { + return null; + } + + @Override + public String getPurpose() { + return get().getPurpose(); + } + + @Override + public void setEffectivePeriod(ICompositeType effectivePeriod) { + if (effectivePeriod != null && !(effectivePeriod instanceof Period)) { + throw new UnprocessableEntityException("EffectivePeriod must be a valid " + Period.class.getName()); + } + // do nothing + } + + @Override + public boolean hasRelatedArtifact() { + return false; + } + + @SuppressWarnings("unchecked") + @Override + public List getRelatedArtifact() { + return new ArrayList<>(); + } + + @SuppressWarnings("unchecked") + @Override + public List getRelatedArtifactsOfType(String codeString) { + RelatedArtifactType type; + try { + type = RelatedArtifactType.fromCode(codeString); + } catch (FHIRException e) { + throw new UnprocessableEntityException("Invalid related artifact code"); + } + return getRelatedArtifact().stream().filter(ra -> ra.getType() == type).collect(Collectors.toList()); + } + + @Override + public void setRelatedArtifact(List relatedArtifacts) + throws UnprocessableEntityException { + // do nothing + } + + @Override + public void setStatus(String statusCodeString) { + PublicationStatus status; + try { + status = PublicationStatus.fromCode(statusCodeString); + } catch (FHIRException e) { + throw new UnprocessableEntityException("Invalid status code"); + } + get().setStatus(status); + } + + @Override + public String getStatus() { + return get().getStatus() == null ? null : get().getStatus().toCode(); + } + + @Override + public boolean getExperimental() { + return get().getExperimental(); + } + + @Override + public void setExtension(List> extensions) { + get().setExtension(extensions.stream().map(e -> (Extension) e).collect(Collectors.toList())); + } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/KnowledgeArtifactAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/KnowledgeArtifactAdapter.java index 7157afd73..a6e9d9824 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/KnowledgeArtifactAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/KnowledgeArtifactAdapter.java @@ -6,13 +6,12 @@ import java.util.List; import java.util.stream.Collectors; import org.hl7.fhir.exceptions.FHIRException; -import org.hl7.fhir.instance.model.api.IBaseExtension; import org.hl7.fhir.instance.model.api.IBaseHasExtensions; import org.hl7.fhir.instance.model.api.ICompositeType; +import org.hl7.fhir.instance.model.api.IDomainResource; import org.hl7.fhir.instance.model.api.IPrimitiveType; import org.hl7.fhir.r4.model.DateTimeType; import org.hl7.fhir.r4.model.Enumerations.PublicationStatus; -import org.hl7.fhir.r4.model.Extension; import org.hl7.fhir.r4.model.MetadataResource; import org.hl7.fhir.r4.model.Period; import org.hl7.fhir.r4.model.RelatedArtifact; @@ -23,6 +22,15 @@ public class KnowledgeArtifactAdapter extends ResourceAdapter implements org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter { MetadataResource adaptedResource; + public KnowledgeArtifactAdapter(IDomainResource resource) { + super(resource); + if (!(resource instanceof MetadataResource)) { + throw new IllegalArgumentException( + "resource passed as resource argument is not a MetadataResource resource"); + } + this.adaptedResource = (MetadataResource) resource; + } + public KnowledgeArtifactAdapter(MetadataResource resource) { super(resource); this.adaptedResource = resource; @@ -179,9 +187,4 @@ public String getStatus() { public boolean getExperimental() { return this.get().getExperimental(); } - - @Override - public void setExtension(List> extensions) { - this.get().setExtension(extensions.stream().map(e -> (Extension) e).collect(Collectors.toList())); - } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/QuestionnaireAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/QuestionnaireAdapter.java index a18ebe301..59b2c41f8 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/QuestionnaireAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/QuestionnaireAdapter.java @@ -2,6 +2,7 @@ import java.util.ArrayList; import java.util.List; +import org.hl7.fhir.instance.model.api.IDomainResource; import org.hl7.fhir.r4.model.CanonicalType; import org.hl7.fhir.r4.model.Questionnaire; import org.opencds.cqf.fhir.utility.Constants; @@ -12,6 +13,17 @@ public class QuestionnaireAdapter extends KnowledgeArtifactAdapter { private Questionnaire questionnaire; + public QuestionnaireAdapter(IDomainResource questionnaire) { + super(questionnaire); + + if (!(questionnaire instanceof Questionnaire)) { + throw new IllegalArgumentException( + "resource passed as questionnaire argument is not a Questionnaire resource"); + } + + this.questionnaire = (Questionnaire) questionnaire; + } + public QuestionnaireAdapter(Questionnaire questionnaire) { super(questionnaire); this.questionnaire = questionnaire; diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/StructureDefinitionAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/StructureDefinitionAdapter.java index 5267e6c88..6b641fe42 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/StructureDefinitionAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/StructureDefinitionAdapter.java @@ -1,33 +1,58 @@ package org.opencds.cqf.fhir.utility.adapter.r4; +import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; import java.util.ArrayList; +import java.util.Date; import java.util.List; +import java.util.stream.Collectors; +import org.hl7.fhir.exceptions.FHIRException; +import org.hl7.fhir.instance.model.api.IBaseExtension; +import org.hl7.fhir.instance.model.api.IBaseHasExtensions; +import org.hl7.fhir.instance.model.api.ICompositeType; +import org.hl7.fhir.instance.model.api.IDomainResource; +import org.hl7.fhir.instance.model.api.IPrimitiveType; import org.hl7.fhir.r4.model.CanonicalType; +import org.hl7.fhir.r4.model.DateTimeType; +import org.hl7.fhir.r4.model.Enumerations.PublicationStatus; +import org.hl7.fhir.r4.model.Extension; +import org.hl7.fhir.r4.model.Period; +import org.hl7.fhir.r4.model.RelatedArtifact; +import org.hl7.fhir.r4.model.RelatedArtifact.RelatedArtifactType; import org.hl7.fhir.r4.model.StructureDefinition; import org.opencds.cqf.fhir.utility.Constants; import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; +import org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter; -public class StructureDefinitionAdapter extends KnowledgeArtifactAdapter { +public class StructureDefinitionAdapter extends ResourceAdapter implements KnowledgeArtifactAdapter { private StructureDefinition structureDefinition; + public StructureDefinitionAdapter(IDomainResource structureDefinition) { + super(structureDefinition); + if (!(structureDefinition instanceof StructureDefinition)) { + throw new IllegalArgumentException( + "resource passed as planDefinition argument is not a StructureDefinition resource"); + } + this.structureDefinition = (StructureDefinition) structureDefinition; + } + public StructureDefinitionAdapter(StructureDefinition structureDefinition) { super(structureDefinition); this.structureDefinition = structureDefinition; } protected StructureDefinition getStructureDefinition() { - return this.structureDefinition; + return structureDefinition; } @Override public List getDependencies() { List references = new ArrayList<>(); - final String referenceSource = this.getStructureDefinition().hasVersion() - ? this.getStructureDefinition().getUrl() + "|" - + this.getStructureDefinition().getVersion() - : this.getStructureDefinition().getUrl(); + final String referenceSource = getStructureDefinition().hasVersion() + ? getStructureDefinition().getUrl() + "|" + + getStructureDefinition().getVersion() + : getStructureDefinition().getUrl(); /* extension[].url modifierExtension[].url @@ -55,4 +80,155 @@ public List getDependencies() { return references; } + + @Override + public StructureDefinition get() { + return structureDefinition; + } + + @Override + public StructureDefinition copy() { + return get().copy(); + } + + @Override + public String getUrl() { + return get().getUrl(); + } + + @Override + public boolean hasUrl() { + return get().hasUrl(); + } + + @Override + public void setUrl(String url) { + get().setUrl(url); + } + + @Override + public void setVersion(String version) { + get().setVersion(version); + } + + @Override + public String getVersion() { + return get().getVersion(); + } + + @Override + public boolean hasVersion() { + return get().hasVersion(); + } + + @Override + public String getName() { + return get().getName(); + } + + @Override + public void setName(String name) { + get().setName(name); + } + + @Override + public Date getApprovalDate() { + return null; + } + + @Override + public Date getDate() { + return get().getDate(); + } + + @Override + public void setDate(Date approvalDate) { + get().setDate(approvalDate); + } + + @Override + public void setDateElement(IPrimitiveType date) { + if (date != null && !(date instanceof DateTimeType)) { + throw new UnprocessableEntityException("Date must be " + DateTimeType.class.getName()); + } + get().setDateElement((DateTimeType) date); + } + + @Override + public void setApprovalDate(Date approvalDate) { + // do nothing + } + + @Override + public Period getEffectivePeriod() { + return null; + } + + @Override + public String getPurpose() { + return get().getPurpose(); + } + + @Override + public void setEffectivePeriod(ICompositeType effectivePeriod) { + if (effectivePeriod != null && !(effectivePeriod instanceof Period)) { + throw new UnprocessableEntityException("EffectivePeriod must be a valid " + Period.class.getName()); + } + // do nothing + } + + @Override + public boolean hasRelatedArtifact() { + return false; + } + + @SuppressWarnings("unchecked") + @Override + public List getRelatedArtifact() { + return new ArrayList<>(); + } + + @SuppressWarnings("unchecked") + @Override + public List getRelatedArtifactsOfType(String codeString) { + RelatedArtifactType type; + try { + type = RelatedArtifactType.fromCode(codeString); + } catch (FHIRException e) { + throw new UnprocessableEntityException("Invalid related artifact code"); + } + return getRelatedArtifact().stream().filter(ra -> ra.getType() == type).collect(Collectors.toList()); + } + + @Override + public void setRelatedArtifact(List relatedArtifacts) + throws UnprocessableEntityException { + // do nothing + } + + @Override + public void setStatus(String statusCodeString) { + PublicationStatus status; + try { + status = PublicationStatus.fromCode(statusCodeString); + } catch (FHIRException e) { + throw new UnprocessableEntityException("Invalid status code"); + } + get().setStatus(status); + } + + @Override + public String getStatus() { + return get().getStatus() == null ? null : get().getStatus().toCode(); + } + + @Override + public boolean getExperimental() { + return get().getExperimental(); + } + + @Override + public void setExtension(List> extensions) { + get().setExtension(extensions.stream().map(e -> (Extension) e).collect(Collectors.toList())); + } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapter.java index 9403bc95f..274025d44 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapter.java @@ -2,6 +2,7 @@ import java.util.ArrayList; import java.util.List; +import org.hl7.fhir.instance.model.api.IDomainResource; import org.hl7.fhir.r5.model.CanonicalType; import org.hl7.fhir.r5.model.Questionnaire; import org.opencds.cqf.fhir.utility.Constants; @@ -12,6 +13,17 @@ public class QuestionnaireAdapter extends KnowledgeArtifactAdapter { private Questionnaire questionnaire; + public QuestionnaireAdapter(IDomainResource questionnaire) { + super(questionnaire); + + if (!(questionnaire instanceof Questionnaire)) { + throw new IllegalArgumentException( + "resource passed as questionnaire argument is not a Questionnaire resource"); + } + + this.questionnaire = (Questionnaire) questionnaire; + } + public QuestionnaireAdapter(Questionnaire questionnaire) { super(questionnaire); this.questionnaire = questionnaire; diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapter.java index f47a5e59b..b4573587a 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapter.java @@ -9,6 +9,7 @@ import org.hl7.fhir.instance.model.api.IBaseExtension; import org.hl7.fhir.instance.model.api.IBaseHasExtensions; import org.hl7.fhir.instance.model.api.ICompositeType; +import org.hl7.fhir.instance.model.api.IDomainResource; import org.hl7.fhir.instance.model.api.IPrimitiveType; import org.hl7.fhir.r5.model.CanonicalType; import org.hl7.fhir.r5.model.DateTimeType; @@ -27,6 +28,15 @@ public class StructureDefinitionAdapter extends ResourceAdapter implements Knowl private StructureDefinition structureDefinition; + public StructureDefinitionAdapter(IDomainResource structureDefinition) { + super(structureDefinition); + if (!(structureDefinition instanceof StructureDefinition)) { + throw new IllegalArgumentException( + "resource passed as planDefinition argument is not a StructureDefinition resource"); + } + this.structureDefinition = (StructureDefinition) structureDefinition; + } + public StructureDefinitionAdapter(StructureDefinition structureDefinition) { super(structureDefinition); this.structureDefinition = structureDefinition; diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/dstu3/PackageVisitor.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/dstu3/PackageVisitor.java index a912e9142..4e01a8e68 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/dstu3/PackageVisitor.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/dstu3/PackageVisitor.java @@ -1,6 +1,5 @@ package org.opencds.cqf.fhir.utility.visitor.dstu3; -import ca.uhn.fhir.context.FhirContext; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -11,24 +10,12 @@ import org.hl7.fhir.dstu3.model.Bundle; import org.hl7.fhir.dstu3.model.Bundle.BundleEntryComponent; import org.hl7.fhir.dstu3.model.Bundle.BundleType; -import org.hl7.fhir.dstu3.model.Coding; import org.hl7.fhir.dstu3.model.Library; import org.hl7.fhir.dstu3.model.MetadataResource; import org.hl7.fhir.dstu3.model.ResourceType; import org.hl7.fhir.dstu3.model.StructureDefinition; -import org.hl7.fhir.dstu3.model.UsageContext; -import org.opencds.cqf.fhir.utility.client.TerminologyServerClient; public class PackageVisitor { - - private final FhirContext fhirContext = FhirContext.forDstu3Cached(); - - public PackageVisitor() { - this.terminologyServerClient = new TerminologyServerClient(fhirContext); - } - - private TerminologyServerClient terminologyServerClient; - // as per http://hl7.org/fhir/dstu3/resource.html#canonical public static final List canonicalResourceTypes = // can't use List.of for Android 26 compatibility @@ -188,29 +175,4 @@ public static List findUnsupportedInclude( } return distinctFilteredEntries; } - - /** - * - * Either finds a usageContext with the same system and code or creates an empty one - * and appends it - * - * @param usageContexts the list of usageContexts to search and/or append to - * @param system the usageContext.code.system to find / create - * @param code the usage.code.code to find / create - * @return the found / created usageContext - */ - protected UsageContext getOrCreateUsageContext(List usageContexts, String system, String code) { - return usageContexts.stream() - .filter(useContext -> useContext.getCode().getSystem().equals(system) - && useContext.getCode().getCode().equals(code)) - .findFirst() - .orElseGet(() -> { - // create the UseContext if it doesn't exist - Coding c = new Coding(system, code, null); - UsageContext n = new UsageContext(c, null); - // add it to the ValueSet before returning - usageContexts.add(n); - return n; - }); - } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r4/PackageVisitor.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r4/PackageVisitor.java index 6d7745ae9..5afdc19fd 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r4/PackageVisitor.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r4/PackageVisitor.java @@ -10,24 +10,12 @@ import org.hl7.fhir.r4.model.Bundle; import org.hl7.fhir.r4.model.Bundle.BundleEntryComponent; import org.hl7.fhir.r4.model.Bundle.BundleType; -import org.hl7.fhir.r4.model.Coding; import org.hl7.fhir.r4.model.Library; import org.hl7.fhir.r4.model.MetadataResource; import org.hl7.fhir.r4.model.ResourceType; import org.hl7.fhir.r4.model.StructureDefinition; -import org.hl7.fhir.r4.model.UsageContext; public class PackageVisitor { - // private Logger logger = LoggerFactory.getLogger(KnowledgeArtifactPackageVisitor.class); - - // private final FhirContext fhirContext = FhirContext.forR4Cached(); - - // public PackageVisitor() { - // this.terminologyServerClient = new TerminologyServerClient(fhirContext); - // } - - // private TerminologyServerClient terminologyServerClient; - // as per http://hl7.org/fhir/R4/resource.html#canonical public static final List canonicalResourceTypes = // can't use List.of for Android 26 compatibility @@ -199,29 +187,4 @@ public static List findUnsupportedInclude( } return distinctFilteredEntries; } - - /** - * - * Either finds a usageContext with the same system and code or creates an empty one - * and appends it - * - * @param usageContexts the list of usageContexts to search and/or append to - * @param system the usageContext.code.system to find / create - * @param code the usage.code.code to find / create - * @return the found / created usageContext - */ - protected UsageContext getOrCreateUsageContext(List usageContexts, String system, String code) { - return usageContexts.stream() - .filter(useContext -> useContext.getCode().getSystem().equals(system) - && useContext.getCode().getCode().equals(code)) - .findFirst() - .orElseGet(() -> { - // create the UseContext if it doesn't exist - Coding c = new Coding(system, code, null); - UsageContext n = new UsageContext(c, null); - // add it to the ValueSet before returning - usageContexts.add(n); - return n; - }); - } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r5/PackageVisitor.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r5/PackageVisitor.java index c62f951d4..0b51bb51c 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r5/PackageVisitor.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r5/PackageVisitor.java @@ -1,6 +1,5 @@ package org.opencds.cqf.fhir.utility.visitor.r5; -import ca.uhn.fhir.context.FhirContext; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -11,24 +10,12 @@ import org.hl7.fhir.r5.model.Bundle; import org.hl7.fhir.r5.model.Bundle.BundleEntryComponent; import org.hl7.fhir.r5.model.Bundle.BundleType; -import org.hl7.fhir.r5.model.Coding; import org.hl7.fhir.r5.model.Library; import org.hl7.fhir.r5.model.MetadataResource; import org.hl7.fhir.r5.model.ResourceType; import org.hl7.fhir.r5.model.StructureDefinition; -import org.hl7.fhir.r5.model.UsageContext; -import org.opencds.cqf.fhir.utility.client.TerminologyServerClient; public class PackageVisitor { - - private final FhirContext fhirContext = FhirContext.forR5Cached(); - - public PackageVisitor() { - this.terminologyServerClient = new TerminologyServerClient(fhirContext); - } - - private TerminologyServerClient terminologyServerClient; - // as per http://hl7.org/fhir/r5/resource.html#canonical public static final List canonicalResourceTypes = // can't use List.of for Android 26 compatibility @@ -196,29 +183,4 @@ public static List findUnsupportedInclude( } return distinctFilteredEntries; } - - /** - * - * Either finds a usageContext with the same system and code or creates an empty one - * and appends it - * - * @param usageContexts the list of usageContexts to search and/or append to - * @param system the usageContext.code.system to find / create - * @param code the usage.code.code to find / create - * @return the found / created usageContext - */ - protected UsageContext getOrCreateUsageContext(List usageContexts, String system, String code) { - return usageContexts.stream() - .filter(useContext -> useContext.getCode().getSystem().equals(system) - && useContext.getCode().getCode().equals(code)) - .findFirst() - .orElseGet(() -> { - // create the UseContext if it doesn't exist - Coding c = new Coding(system, code, null); - UsageContext n = new UsageContext(c, null); - // add it to the ValueSet before returning - usageContexts.add(n); - return n; - }); - } } diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/QuestionnaireAdapaterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/QuestionnaireAdapaterTest.java new file mode 100644 index 000000000..27ee83d01 --- /dev/null +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/QuestionnaireAdapaterTest.java @@ -0,0 +1,65 @@ +package org.opencds.cqf.fhir.utility.adapter.dstu3; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import ca.uhn.fhir.context.FhirContext; +import org.hl7.fhir.dstu3.model.Bundle; +import org.hl7.fhir.dstu3.model.Questionnaire; +import org.hl7.fhir.instance.model.api.IDomainResource; +import org.junit.jupiter.api.Test; +import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; + +public class QuestionnaireAdapaterTest { + private final FhirContext fhirContext = FhirContext.forR4Cached(); + + @Test + void adapter_accepts_visitor() { + var spyVisitor = spy(new PackageVisitor(fhirContext)); + doReturn(new Bundle()).when(spyVisitor).visit(any(QuestionnaireAdapter.class), any(), any()); + IDomainResource questionnaire = new Questionnaire(); + var adapter = new QuestionnaireAdapter(questionnaire); + adapter.accept(spyVisitor, null, null); + verify(spyVisitor, times(1)).visit(any(QuestionnaireAdapter.class), any(), any()); + } + + @Test + void adapter_get_and_set_name() { + var questionnaire = new Questionnaire(); + var name = "name"; + questionnaire.setName(name); + var adapter = new QuestionnaireAdapter(questionnaire); + assertEquals(name, adapter.getName()); + var newName = "name2"; + adapter.setName(newName); + assertEquals(newName, questionnaire.getName()); + } + + @Test + void adapter_get_and_set_url() { + var questionnaire = new Questionnaire(); + var url = "www.url.com"; + questionnaire.setUrl(url); + var adapter = new QuestionnaireAdapter(questionnaire); + assertEquals(url, adapter.getUrl()); + var newUrl = "www.url2.com"; + adapter.setUrl(newUrl); + assertEquals(newUrl, questionnaire.getUrl()); + } + + @Test + void adapter_get_experimental() { + var questionnaire = new Questionnaire(); + var experimental = true; + questionnaire.setExperimental(experimental); + var adapter = new QuestionnaireAdapter(questionnaire); + assertEquals(experimental, adapter.getExperimental()); + } + + @Test + void adapter_get_all_dependencies() {} +} diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/StructureDefinitionAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/StructureDefinitionAdapterTest.java new file mode 100644 index 000000000..05e1beb3b --- /dev/null +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/StructureDefinitionAdapterTest.java @@ -0,0 +1,65 @@ +package org.opencds.cqf.fhir.utility.adapter.dstu3; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import ca.uhn.fhir.context.FhirContext; +import org.hl7.fhir.dstu3.model.Bundle; +import org.hl7.fhir.dstu3.model.StructureDefinition; +import org.hl7.fhir.instance.model.api.IDomainResource; +import org.junit.jupiter.api.Test; +import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; + +public class StructureDefinitionAdapterTest { + private final FhirContext fhirContext = FhirContext.forR4Cached(); + + @Test + void adapter_accepts_visitor() { + var spyVisitor = spy(new PackageVisitor(fhirContext)); + doReturn(new Bundle()).when(spyVisitor).visit(any(StructureDefinitionAdapter.class), any(), any()); + IDomainResource structureDef = new StructureDefinition(); + var adapter = new StructureDefinitionAdapter(structureDef); + adapter.accept(spyVisitor, null, null); + verify(spyVisitor, times(1)).visit(any(StructureDefinitionAdapter.class), any(), any()); + } + + @Test + void adapter_get_and_set_name() { + var structureDef = new StructureDefinition(); + var name = "name"; + structureDef.setName(name); + var adapter = new StructureDefinitionAdapter(structureDef); + assertEquals(name, adapter.getName()); + var newName = "name2"; + adapter.setName(newName); + assertEquals(newName, structureDef.getName()); + } + + @Test + void adapter_get_and_set_url() { + var structureDef = new StructureDefinition(); + var url = "www.url.com"; + structureDef.setUrl(url); + var adapter = new StructureDefinitionAdapter(structureDef); + assertEquals(url, adapter.getUrl()); + var newUrl = "www.url2.com"; + adapter.setUrl(newUrl); + assertEquals(newUrl, structureDef.getUrl()); + } + + @Test + void adapter_get_experimental() { + var structureDef = new StructureDefinition(); + var experimental = true; + structureDef.setExperimental(experimental); + var adapter = new StructureDefinitionAdapter(structureDef); + assertEquals(experimental, adapter.getExperimental()); + } + + @Test + void adapter_get_all_dependencies() {} +} diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/QuestionnaireAdapaterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/QuestionnaireAdapaterTest.java new file mode 100644 index 000000000..65be52b57 --- /dev/null +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/QuestionnaireAdapaterTest.java @@ -0,0 +1,65 @@ +package org.opencds.cqf.fhir.utility.adapter.r4; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import ca.uhn.fhir.context.FhirContext; +import org.hl7.fhir.instance.model.api.IDomainResource; +import org.hl7.fhir.r4.model.Bundle; +import org.hl7.fhir.r4.model.Questionnaire; +import org.junit.jupiter.api.Test; +import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; + +public class QuestionnaireAdapaterTest { + private final FhirContext fhirContext = FhirContext.forR4Cached(); + + @Test + void adapter_accepts_visitor() { + var spyVisitor = spy(new PackageVisitor(fhirContext)); + doReturn(new Bundle()).when(spyVisitor).visit(any(QuestionnaireAdapter.class), any(), any()); + IDomainResource questionnaire = new Questionnaire(); + var adapter = new QuestionnaireAdapter(questionnaire); + adapter.accept(spyVisitor, null, null); + verify(spyVisitor, times(1)).visit(any(QuestionnaireAdapter.class), any(), any()); + } + + @Test + void adapter_get_and_set_name() { + var questionnaire = new Questionnaire(); + var name = "name"; + questionnaire.setName(name); + var adapter = new QuestionnaireAdapter(questionnaire); + assertEquals(name, adapter.getName()); + var newName = "name2"; + adapter.setName(newName); + assertEquals(newName, questionnaire.getName()); + } + + @Test + void adapter_get_and_set_url() { + var questionnaire = new Questionnaire(); + var url = "www.url.com"; + questionnaire.setUrl(url); + var adapter = new QuestionnaireAdapter(questionnaire); + assertEquals(url, adapter.getUrl()); + var newUrl = "www.url2.com"; + adapter.setUrl(newUrl); + assertEquals(newUrl, questionnaire.getUrl()); + } + + @Test + void adapter_get_experimental() { + var questionnaire = new Questionnaire(); + var experimental = true; + questionnaire.setExperimental(experimental); + var adapter = new QuestionnaireAdapter(questionnaire); + assertEquals(experimental, adapter.getExperimental()); + } + + @Test + void adapter_get_all_dependencies() {} +} diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/StructureDefinitionAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/StructureDefinitionAdapterTest.java new file mode 100644 index 000000000..bf6fa45f9 --- /dev/null +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/StructureDefinitionAdapterTest.java @@ -0,0 +1,65 @@ +package org.opencds.cqf.fhir.utility.adapter.r4; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import ca.uhn.fhir.context.FhirContext; +import org.hl7.fhir.instance.model.api.IDomainResource; +import org.hl7.fhir.r4.model.Bundle; +import org.hl7.fhir.r4.model.StructureDefinition; +import org.junit.jupiter.api.Test; +import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; + +public class StructureDefinitionAdapterTest { + private final FhirContext fhirContext = FhirContext.forR4Cached(); + + @Test + void adapter_accepts_visitor() { + var spyVisitor = spy(new PackageVisitor(fhirContext)); + doReturn(new Bundle()).when(spyVisitor).visit(any(StructureDefinitionAdapter.class), any(), any()); + IDomainResource structureDef = new StructureDefinition(); + var adapter = new StructureDefinitionAdapter(structureDef); + adapter.accept(spyVisitor, null, null); + verify(spyVisitor, times(1)).visit(any(StructureDefinitionAdapter.class), any(), any()); + } + + @Test + void adapter_get_and_set_name() { + var structureDef = new StructureDefinition(); + var name = "name"; + structureDef.setName(name); + var adapter = new StructureDefinitionAdapter(structureDef); + assertEquals(name, adapter.getName()); + var newName = "name2"; + adapter.setName(newName); + assertEquals(newName, structureDef.getName()); + } + + @Test + void adapter_get_and_set_url() { + var structureDef = new StructureDefinition(); + var url = "www.url.com"; + structureDef.setUrl(url); + var adapter = new StructureDefinitionAdapter(structureDef); + assertEquals(url, adapter.getUrl()); + var newUrl = "www.url2.com"; + adapter.setUrl(newUrl); + assertEquals(newUrl, structureDef.getUrl()); + } + + @Test + void adapter_get_experimental() { + var structureDef = new StructureDefinition(); + var experimental = true; + structureDef.setExperimental(experimental); + var adapter = new StructureDefinitionAdapter(structureDef); + assertEquals(experimental, adapter.getExperimental()); + } + + @Test + void adapter_get_all_dependencies() {} +} diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapaterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapaterTest.java new file mode 100644 index 000000000..151b5ef91 --- /dev/null +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapaterTest.java @@ -0,0 +1,65 @@ +package org.opencds.cqf.fhir.utility.adapter.r5; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import ca.uhn.fhir.context.FhirContext; +import org.hl7.fhir.instance.model.api.IDomainResource; +import org.hl7.fhir.r5.model.Bundle; +import org.hl7.fhir.r5.model.Questionnaire; +import org.junit.jupiter.api.Test; +import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; + +public class QuestionnaireAdapaterTest { + private final FhirContext fhirContext = FhirContext.forR4Cached(); + + @Test + void adapter_accepts_visitor() { + var spyVisitor = spy(new PackageVisitor(fhirContext)); + doReturn(new Bundle()).when(spyVisitor).visit(any(QuestionnaireAdapter.class), any(), any()); + IDomainResource questionnaire = new Questionnaire(); + var adapter = new QuestionnaireAdapter(questionnaire); + adapter.accept(spyVisitor, null, null); + verify(spyVisitor, times(1)).visit(any(QuestionnaireAdapter.class), any(), any()); + } + + @Test + void adapter_get_and_set_name() { + var questionnaire = new Questionnaire(); + var name = "name"; + questionnaire.setName(name); + var adapter = new QuestionnaireAdapter(questionnaire); + assertEquals(name, adapter.getName()); + var newName = "name2"; + adapter.setName(newName); + assertEquals(newName, questionnaire.getName()); + } + + @Test + void adapter_get_and_set_url() { + var questionnaire = new Questionnaire(); + var url = "www.url.com"; + questionnaire.setUrl(url); + var adapter = new QuestionnaireAdapter(questionnaire); + assertEquals(url, adapter.getUrl()); + var newUrl = "www.url2.com"; + adapter.setUrl(newUrl); + assertEquals(newUrl, questionnaire.getUrl()); + } + + @Test + void adapter_get_experimental() { + var questionnaire = new Questionnaire(); + var experimental = true; + questionnaire.setExperimental(experimental); + var adapter = new QuestionnaireAdapter(questionnaire); + assertEquals(experimental, adapter.getExperimental()); + } + + @Test + void adapter_get_all_dependencies() {} +} diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapterTest.java new file mode 100644 index 000000000..0b711199e --- /dev/null +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapterTest.java @@ -0,0 +1,65 @@ +package org.opencds.cqf.fhir.utility.adapter.r5; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import ca.uhn.fhir.context.FhirContext; +import org.hl7.fhir.instance.model.api.IDomainResource; +import org.hl7.fhir.r5.model.Bundle; +import org.hl7.fhir.r5.model.StructureDefinition; +import org.junit.jupiter.api.Test; +import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; + +public class StructureDefinitionAdapterTest { + private final FhirContext fhirContext = FhirContext.forR4Cached(); + + @Test + void adapter_accepts_visitor() { + var spyVisitor = spy(new PackageVisitor(fhirContext)); + doReturn(new Bundle()).when(spyVisitor).visit(any(StructureDefinitionAdapter.class), any(), any()); + IDomainResource structureDef = new StructureDefinition(); + var adapter = new StructureDefinitionAdapter(structureDef); + adapter.accept(spyVisitor, null, null); + verify(spyVisitor, times(1)).visit(any(StructureDefinitionAdapter.class), any(), any()); + } + + @Test + void adapter_get_and_set_name() { + var structureDef = new StructureDefinition(); + var name = "name"; + structureDef.setName(name); + var adapter = new StructureDefinitionAdapter(structureDef); + assertEquals(name, adapter.getName()); + var newName = "name2"; + adapter.setName(newName); + assertEquals(newName, structureDef.getName()); + } + + @Test + void adapter_get_and_set_url() { + var structureDef = new StructureDefinition(); + var url = "www.url.com"; + structureDef.setUrl(url); + var adapter = new StructureDefinitionAdapter(structureDef); + assertEquals(url, adapter.getUrl()); + var newUrl = "www.url2.com"; + adapter.setUrl(newUrl); + assertEquals(newUrl, structureDef.getUrl()); + } + + @Test + void adapter_get_experimental() { + var structureDef = new StructureDefinition(); + var experimental = true; + structureDef.setExperimental(experimental); + var adapter = new StructureDefinitionAdapter(structureDef); + assertEquals(experimental, adapter.getExperimental()); + } + + @Test + void adapter_get_all_dependencies() {} +} diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactApproveVisitorTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/ApproveVisitorTest.java similarity index 95% rename from cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactApproveVisitorTest.java rename to cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/ApproveVisitorTest.java index 763d1d44c..9aad392e9 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactApproveVisitorTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/ApproveVisitorTest.java @@ -39,15 +39,15 @@ import org.opencds.cqf.fhir.utility.repository.InMemoryFhirRepository; import org.opencds.cqf.fhir.utility.visitor.ApproveVisitor; -class KnowledgeArtifactApproveVisitorTest { +class ApproveVisitorTest { private final FhirContext fhirContext = FhirContext.forDstu3Cached(); private Repository spyRepository; private final IParser jsonParser = fhirContext.newJsonParser(); @BeforeEach void setup() { - var lib = (Library) jsonParser.parseResource( - KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream("Library-ersd-active.json")); + var lib = (Library) + jsonParser.parseResource(ReleaseVisitorTests.class.getResourceAsStream("Library-ersd-active.json")); spyRepository = spy(new InMemoryFhirRepository(fhirContext)); spyRepository.update(lib); doAnswer(new Answer() { @@ -112,8 +112,8 @@ void approveOperation_should_respect_artifactAssessment_information_type_binding @Test void approveOperation_test() { - var practitioner = (Practitioner) jsonParser.parseResource( - KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream("Practitioner-minimal.json")); + var practitioner = (Practitioner) + jsonParser.parseResource(ReleaseVisitorTests.class.getResourceAsStream("Practitioner-minimal.json")); spyRepository.update(practitioner); Date today = new Date(); // get today's date in the form "2023-05-11" diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactDraftVisitorTests.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/DraftVisitorTests.java similarity index 90% rename from cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactDraftVisitorTests.java rename to cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/DraftVisitorTests.java index dec14c723..7cef08354 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactDraftVisitorTests.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/DraftVisitorTests.java @@ -44,7 +44,7 @@ import org.opencds.cqf.fhir.utility.visitor.DraftVisitor; import org.opencds.cqf.fhir.utility.visitor.KnowledgeArtifactVisitor; -class KnowledgeArtifactDraftVisitorTests { +class DraftVisitorTests { private final FhirContext fhirContext = FhirContext.forDstu3Cached(); private Repository spyRepository; private final IParser jsonParser = fhirContext.newJsonParser(); @@ -83,8 +83,8 @@ public Bundle answer(InvocationOnMock a) throws Throwable { @Test void library_draft_test() { - Bundle bundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactDraftVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); + Bundle bundle = (Bundle) + jsonParser.parseResource(DraftVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); spyRepository.transaction(bundle); KnowledgeArtifactVisitor draftVisitor = new DraftVisitor(); Library library = spyRepository @@ -141,8 +141,8 @@ void library_draft_test() { @Test void draftOperation_no_effectivePeriod_test() { - Bundle bundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactDraftVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); + Bundle bundle = (Bundle) + jsonParser.parseResource(DraftVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); spyRepository.transaction(bundle); Library baseLib = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) @@ -170,10 +170,10 @@ void draftOperation_no_effectivePeriod_test() { @Test void draftOperation_version_conflict_test() { - Bundle bundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactDraftVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); - Library versionConflictLibrary = (Library) jsonParser.parseResource( - KnowledgeArtifactDraftVisitorTests.class.getResourceAsStream("Library-version-conflict.json")); + Bundle bundle = (Bundle) + jsonParser.parseResource(DraftVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); + Library versionConflictLibrary = (Library) + jsonParser.parseResource(DraftVisitorTests.class.getResourceAsStream("Library-version-conflict.json")); spyRepository.transaction(bundle); spyRepository.update(versionConflictLibrary); Parameters params = parameters(part("version", "1.0.0")); @@ -196,8 +196,8 @@ void draftOperation_version_conflict_test() { @Test void draftOperation_cannot_create_draft_of_draft_test() { - Library versionConflictLibrary = (Library) jsonParser.parseResource( - KnowledgeArtifactDraftVisitorTests.class.getResourceAsStream("Library-version-conflict.json")); + Library versionConflictLibrary = (Library) + jsonParser.parseResource(DraftVisitorTests.class.getResourceAsStream("Library-version-conflict.json")); spyRepository.update(versionConflictLibrary); Parameters params = parameters(part("version", "1.2.1")); String maybeException = ""; @@ -217,8 +217,8 @@ void draftOperation_cannot_create_draft_of_draft_test() { @Test void draftOperation_version_format_test() { - Library versionConflictLibrary = (Library) jsonParser.parseResource( - KnowledgeArtifactDraftVisitorTests.class.getResourceAsStream("Library-version-conflict.json")); + Library versionConflictLibrary = (Library) + jsonParser.parseResource(DraftVisitorTests.class.getResourceAsStream("Library-version-conflict.json")); spyRepository.update(versionConflictLibrary); Library baseLib = spyRepository .read(Library.class, new IdType("Library/SpecificationLibraryDraftVersion-1-0-0")) diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactPackageVisitorTests.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/PackageVisitorTests.java similarity index 93% rename from cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactPackageVisitorTests.java rename to cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/PackageVisitorTests.java index f3f473f0e..69dbf6f1b 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactPackageVisitorTests.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/PackageVisitorTests.java @@ -47,7 +47,7 @@ import org.opencds.cqf.fhir.utility.repository.InMemoryFhirRepository; import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; -class KnowledgeArtifactPackageVisitorTests { +class PackageVisitorTests { private final FhirContext fhirContext = FhirContext.forDstu3Cached(); private final IParser jsonParser = fhirContext.newJsonParser(); private Repository spyRepository; @@ -69,7 +69,7 @@ public Bundle answer(InvocationOnMock a) throws Throwable { @Test void visitLibraryTest() { Bundle loadedBundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example-naive.json")); + PackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example-naive.json")); spyRepository.transaction(loadedBundle); PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository @@ -101,8 +101,8 @@ void visitLibraryTest() { @Test @Disabled("This test needs a ValueSet that cannot be naively expanded") void packageOperation_should_fail_no_credentials() { - Bundle loadedBundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); + Bundle loadedBundle = (Bundle) + jsonParser.parseResource(PackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); spyRepository.transaction(loadedBundle); PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository @@ -123,8 +123,8 @@ void packageOperation_should_fail_no_credentials() { @Test @Disabled("This test needs a ValueSet that cannot be naively expanded") void packageOperation_should_fail_credentials_missing_username() { - Bundle loadedBundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); + Bundle loadedBundle = (Bundle) + jsonParser.parseResource(PackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); spyRepository.transaction(loadedBundle); PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository @@ -149,8 +149,8 @@ void packageOperation_should_fail_credentials_missing_username() { @Test @Disabled("This test needs a ValueSet that cannot be naively expanded") void packageOperation_should_fail_credentials_missing_apikey() { - Bundle loadedBundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); + Bundle loadedBundle = (Bundle) + jsonParser.parseResource(PackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); spyRepository.transaction(loadedBundle); PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository @@ -175,8 +175,8 @@ void packageOperation_should_fail_credentials_missing_apikey() { @Test @Disabled("This test needs a ValueSet that cannot be naively expanded") void packageOperation_should_fail_credentials_invalid() { - Bundle loadedBundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); + Bundle loadedBundle = (Bundle) + jsonParser.parseResource(PackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); spyRepository.transaction(loadedBundle); PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository @@ -201,9 +201,8 @@ void packageOperation_should_fail_credentials_invalid() { @Test void packageOperation_should_fail_non_matching_capability() { - Bundle bundle = - (Bundle) jsonParser.parseResource(KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream( - "Bundle-ersd-package-capabilities.json")); + Bundle bundle = (Bundle) jsonParser.parseResource( + PackageVisitorTests.class.getResourceAsStream("Bundle-ersd-package-capabilities.json")); spyRepository.transaction(bundle); List capabilities = Arrays.asList("computable", "publishable", "executable"); PackageVisitor packageVisitor = new PackageVisitor(fhirContext); @@ -236,7 +235,7 @@ void packageOperation_should_fail_non_matching_capability() { @Test void packageOperation_should_apply_check_force_canonicalVersions() { Bundle bundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-active-no-versions.json")); + PackageVisitorTests.class.getResourceAsStream("Bundle-active-no-versions.json")); spyRepository.transaction(bundle); PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository @@ -297,7 +296,7 @@ void packageOperation_should_apply_check_force_canonicalVersions() { @Test void packageOperation_should_respect_count_offset() { Bundle bundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); + PackageVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); spyRepository.transaction(bundle); PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository @@ -335,7 +334,7 @@ void packageOperation_should_respect_count_offset() { @Test void packageOperation_different_bundle_types() { Bundle bundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); + PackageVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); spyRepository.transaction(bundle); PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository @@ -374,7 +373,7 @@ void packageOperation_different_bundle_types() { @Test void packageOperation_should_conditionally_create() { Bundle bundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); + PackageVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); spyRepository.transaction(bundle); PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository @@ -394,7 +393,7 @@ void packageOperation_should_conditionally_create() { @Test void packageOperation_should_respect_include() { Bundle bundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); + PackageVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); spyRepository.transaction(bundle); PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactReleaseVisitorTests.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/ReleaseVisitorTests.java similarity index 92% rename from cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactReleaseVisitorTests.java rename to cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/ReleaseVisitorTests.java index d47c634e1..80faf3e9c 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/KnowledgeArtifactReleaseVisitorTests.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/ReleaseVisitorTests.java @@ -51,7 +51,7 @@ import org.opencds.cqf.fhir.utility.visitor.ReleaseVisitor; import org.slf4j.LoggerFactory; -class KnowledgeArtifactReleaseVisitorTests { +class ReleaseVisitorTests { private final FhirContext fhirContext = FhirContext.forDstu3Cached(); private Repository spyRepository; private final IParser jsonParser = fhirContext.newJsonParser(); @@ -75,9 +75,8 @@ class KnowledgeArtifactReleaseVisitorTests { @BeforeEach void setup() { - SearchParameter sp = (SearchParameter) - jsonParser.parseResource(KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream( - "SearchParameter-artifactAssessment.json")); + SearchParameter sp = (SearchParameter) jsonParser.parseResource( + ReleaseVisitorTests.class.getResourceAsStream("SearchParameter-artifactAssessment.json")); spyRepository = spy(new InMemoryFhirRepository(fhirContext)); spyRepository.update(sp); doAnswer(new Answer() { @@ -94,7 +93,7 @@ public Bundle answer(InvocationOnMock a) throws Throwable { @Test void visitLibraryTest() { Bundle bundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream("Bundle-ersd-release-bundle.json")); + ReleaseVisitorTests.class.getResourceAsStream("Bundle-ersd-release-bundle.json")); spyRepository.transaction(bundle); ReleaseVisitor releaseVisitor = new ReleaseVisitor(); Library library = spyRepository @@ -198,7 +197,7 @@ void visitLibraryTest() { @Test void releaseResource_force_version() { Bundle bundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft.json")); + ReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft.json")); spyRepository.transaction(bundle); // Existing version should be "1.2.3"; String newVersionToForce = "1.2.7"; @@ -225,14 +224,12 @@ void releaseResource_force_version() { @Test void releaseResource_require_non_experimental_error() { // SpecificationLibrary - root is experimentalbut HAS experimental children - Bundle bundle = - (Bundle) jsonParser.parseResource(KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream( - "Bundle-small-approved-draft-experimental.json")); + Bundle bundle = (Bundle) jsonParser.parseResource( + ReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft-experimental.json")); spyRepository.transaction(bundle); // SpecificationLibrary2 - root is NOT experimental but HAS experimental children - Bundle bundle2 = - (Bundle) jsonParser.parseResource(KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream( - "Bundle-small-approved-draft-experimental-children.json")); + Bundle bundle2 = (Bundle) jsonParser.parseResource(ReleaseVisitorTests.class.getResourceAsStream( + "Bundle-small-approved-draft-experimental-children.json")); spyRepository.transaction(bundle2); Parameters params = parameters( part("version", new StringType("1.2.3")), @@ -269,14 +266,12 @@ void releaseResource_require_non_experimental_error() { @Test void releaseResource_require_non_experimental_warn() { // SpecificationLibrary - root is experimentalbut HAS experimental children - Bundle bundle = - (Bundle) jsonParser.parseResource(KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream( - "Bundle-small-approved-draft-experimental.json")); + Bundle bundle = (Bundle) jsonParser.parseResource( + ReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft-experimental.json")); spyRepository.transaction(bundle); // SpecificationLibrary2 - root is NOT experimental but HAS experimental children - Bundle bundle2 = - (Bundle) jsonParser.parseResource(KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream( - "Bundle-small-approved-draft-experimental-children.json")); + Bundle bundle2 = (Bundle) jsonParser.parseResource(ReleaseVisitorTests.class.getResourceAsStream( + "Bundle-small-approved-draft-experimental-children.json")); spyRepository.transaction(bundle2); ReleaseVisitor releaseVisitor = new ReleaseVisitor(); @@ -329,9 +324,8 @@ void releaseResource_require_non_experimental_warn() { @Test void releaseResource_propagate_effective_period() { - Bundle bundle = - (Bundle) jsonParser.parseResource(KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream( - "Bundle-ersd-no-child-effective-period.json")); + Bundle bundle = (Bundle) jsonParser.parseResource( + ReleaseVisitorTests.class.getResourceAsStream("Bundle-ersd-no-child-effective-period.json")); spyRepository.transaction(bundle); String effectivePeriodToPropagate = "2020-12-11"; @@ -367,7 +361,7 @@ void releaseResource_propagate_effective_period() { @Test void releaseResource_latestFromTx_NotSupported_test() { Bundle bundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft.json")); + ReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft.json")); spyRepository.transaction(bundle); String actualErrorMessage = ""; @@ -392,9 +386,8 @@ void releaseResource_latestFromTx_NotSupported_test() { @Test void release_missing_approvalDate_validation_test() { - Bundle bundle = - (Bundle) jsonParser.parseResource(KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream( - "Bundle-release-missing-approvalDate.json")); + Bundle bundle = (Bundle) jsonParser.parseResource( + ReleaseVisitorTests.class.getResourceAsStream("Bundle-release-missing-approvalDate.json")); spyRepository.transaction(bundle); String versionData = "1.2.3"; @@ -417,7 +410,7 @@ void release_missing_approvalDate_validation_test() { @Test void release_version_format_test() { Bundle bundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft.json")); + ReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft.json")); spyRepository.transaction(bundle); ReleaseVisitor releaseVisitor = new ReleaseVisitor(); Library library = spyRepository @@ -441,7 +434,7 @@ void release_version_format_test() { @Test void release_releaseLabel_test() { Bundle bundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft.json")); + ReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft.json")); spyRepository.transaction(bundle); String releaseLabel = "release label test"; ReleaseVisitor releaseVisitor = new ReleaseVisitor(); @@ -471,7 +464,7 @@ void release_releaseLabel_test() { @Test void release_version_active_test() { Bundle bundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); + ReleaseVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); spyRepository.transaction(bundle); ReleaseVisitor releaseVisitor = new ReleaseVisitor(); Library library = spyRepository @@ -493,7 +486,7 @@ void release_version_active_test() { @Test void release_versionBehaviour_format_test() { Bundle bundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft.json")); + ReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft.json")); spyRepository.transaction(bundle); ReleaseVisitor releaseVisitor = new ReleaseVisitor(); Library library = spyRepository diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactApproveVisitorTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/ApproveVisitorTest.java similarity index 95% rename from cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactApproveVisitorTest.java rename to cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/ApproveVisitorTest.java index 10805cc06..9e620e8c0 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactApproveVisitorTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/ApproveVisitorTest.java @@ -39,15 +39,15 @@ import org.opencds.cqf.fhir.utility.repository.InMemoryFhirRepository; import org.opencds.cqf.fhir.utility.visitor.ApproveVisitor; -class KnowledgeArtifactApproveVisitorTest { +class ApproveVisitorTest { private final FhirContext fhirContext = FhirContext.forR4Cached(); private Repository spyRepository; private final IParser jsonParser = fhirContext.newJsonParser(); @BeforeEach void setup() { - var lib = (Library) jsonParser.parseResource( - KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream("Library-ersd-active.json")); + var lib = (Library) + jsonParser.parseResource(ReleaseVisitorTests.class.getResourceAsStream("Library-ersd-active.json")); spyRepository = spy(new InMemoryFhirRepository(fhirContext)); spyRepository.update(lib); doAnswer(new Answer() { @@ -112,8 +112,8 @@ void approveOperation_should_respect_artifactAssessment_information_type_binding @Test void approveOperation_test() { - var practitioner = (Practitioner) jsonParser.parseResource( - KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream("Practitioner-minimal.json")); + var practitioner = (Practitioner) + jsonParser.parseResource(ReleaseVisitorTests.class.getResourceAsStream("Practitioner-minimal.json")); spyRepository.update(practitioner); Date today = new Date(); // get today's date in the form "2023-05-11" diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactDraftVisitorTests.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/DraftVisitorTests.java similarity index 90% rename from cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactDraftVisitorTests.java rename to cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/DraftVisitorTests.java index dd0a1710f..645460e37 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactDraftVisitorTests.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/DraftVisitorTests.java @@ -44,7 +44,7 @@ import org.opencds.cqf.fhir.utility.visitor.DraftVisitor; import org.opencds.cqf.fhir.utility.visitor.KnowledgeArtifactVisitor; -class KnowledgeArtifactDraftVisitorTests { +class DraftVisitorTests { private final FhirContext fhirContext = FhirContext.forR4Cached(); private Repository spyRepository; private final IParser jsonParser = fhirContext.newJsonParser(); @@ -83,8 +83,8 @@ public Bundle answer(InvocationOnMock a) throws Throwable { @Test void library_draft_test() { - Bundle bundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactDraftVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); + Bundle bundle = (Bundle) + jsonParser.parseResource(DraftVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); spyRepository.transaction(bundle); KnowledgeArtifactVisitor draftVisitor = new DraftVisitor(); Library library = spyRepository @@ -137,8 +137,8 @@ void library_draft_test() { @Test void draftOperation_no_effectivePeriod_test() { - Bundle bundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactDraftVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); + Bundle bundle = (Bundle) + jsonParser.parseResource(DraftVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); spyRepository.transaction(bundle); Library baseLib = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) @@ -166,10 +166,10 @@ void draftOperation_no_effectivePeriod_test() { @Test void draftOperation_version_conflict_test() { - Bundle bundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactDraftVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); - Library versionConflictLibrary = (Library) jsonParser.parseResource( - KnowledgeArtifactDraftVisitorTests.class.getResourceAsStream("Library-version-conflict.json")); + Bundle bundle = (Bundle) + jsonParser.parseResource(DraftVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); + Library versionConflictLibrary = (Library) + jsonParser.parseResource(DraftVisitorTests.class.getResourceAsStream("Library-version-conflict.json")); spyRepository.transaction(bundle); spyRepository.update(versionConflictLibrary); Parameters params = parameters(part("version", "1.0.0")); @@ -192,8 +192,8 @@ void draftOperation_version_conflict_test() { @Test void draftOperation_cannot_create_draft_of_draft_test() { - Library versionConflictLibrary = (Library) jsonParser.parseResource( - KnowledgeArtifactDraftVisitorTests.class.getResourceAsStream("Library-version-conflict.json")); + Library versionConflictLibrary = (Library) + jsonParser.parseResource(DraftVisitorTests.class.getResourceAsStream("Library-version-conflict.json")); spyRepository.update(versionConflictLibrary); Parameters params = parameters(part("version", "1.2.1")); String maybeException = ""; @@ -213,8 +213,8 @@ void draftOperation_cannot_create_draft_of_draft_test() { @Test void draftOperation_version_format_test() { - Library versionConflictLibrary = (Library) jsonParser.parseResource( - KnowledgeArtifactDraftVisitorTests.class.getResourceAsStream("Library-version-conflict.json")); + Library versionConflictLibrary = (Library) + jsonParser.parseResource(DraftVisitorTests.class.getResourceAsStream("Library-version-conflict.json")); spyRepository.update(versionConflictLibrary); Library baseLib = spyRepository .read(Library.class, new IdType("Library/SpecificationLibraryDraftVersion-1-0-0")) diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactPackageVisitorTests.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/PackageVisitorTests.java similarity index 93% rename from cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactPackageVisitorTests.java rename to cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/PackageVisitorTests.java index 9bf5a89be..8b29714b7 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactPackageVisitorTests.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/PackageVisitorTests.java @@ -47,7 +47,7 @@ import org.opencds.cqf.fhir.utility.repository.InMemoryFhirRepository; import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; -class KnowledgeArtifactPackageVisitorTests { +class PackageVisitorTests { private final FhirContext fhirContext = FhirContext.forR4Cached(); private final IParser jsonParser = fhirContext.newJsonParser(); private Repository spyRepository; @@ -69,7 +69,7 @@ public Bundle answer(InvocationOnMock a) throws Throwable { @Test void visitLibraryTest() { Bundle loadedBundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example-naive.json")); + PackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example-naive.json")); spyRepository.transaction(loadedBundle); PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository @@ -101,8 +101,8 @@ void visitLibraryTest() { @Test @Disabled("This test needs a ValueSet that cannot be naively expanded") void packageOperation_should_fail_no_credentials() { - Bundle loadedBundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); + Bundle loadedBundle = (Bundle) + jsonParser.parseResource(PackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); spyRepository.transaction(loadedBundle); PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository @@ -122,8 +122,8 @@ void packageOperation_should_fail_no_credentials() { @Test void packageOperation_should_fail_credentials_missing_username() { - Bundle loadedBundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); + Bundle loadedBundle = (Bundle) + jsonParser.parseResource(PackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); spyRepository.transaction(loadedBundle); PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository @@ -146,8 +146,8 @@ void packageOperation_should_fail_credentials_missing_username() { @Test void packageOperation_should_fail_credentials_missing_apikey() { - Bundle loadedBundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); + Bundle loadedBundle = (Bundle) + jsonParser.parseResource(PackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); spyRepository.transaction(loadedBundle); PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository @@ -170,8 +170,8 @@ void packageOperation_should_fail_credentials_missing_apikey() { @Test void packageOperation_should_fail_credentials_invalid() { - Bundle loadedBundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); + Bundle loadedBundle = (Bundle) + jsonParser.parseResource(PackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); spyRepository.transaction(loadedBundle); PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository @@ -194,9 +194,8 @@ void packageOperation_should_fail_credentials_invalid() { @Test void packageOperation_should_fail_non_matching_capability() { - Bundle bundle = - (Bundle) jsonParser.parseResource(KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream( - "Bundle-ersd-package-capabilities.json")); + Bundle bundle = (Bundle) jsonParser.parseResource( + PackageVisitorTests.class.getResourceAsStream("Bundle-ersd-package-capabilities.json")); spyRepository.transaction(bundle); List capabilities = Arrays.asList("computable", "publishable", "executable"); PackageVisitor packageVisitor = new PackageVisitor(fhirContext); @@ -229,7 +228,7 @@ void packageOperation_should_fail_non_matching_capability() { @Test void packageOperation_should_apply_check_force_canonicalVersions() { Bundle bundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-active-no-versions.json")); + PackageVisitorTests.class.getResourceAsStream("Bundle-active-no-versions.json")); spyRepository.transaction(bundle); PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository @@ -291,7 +290,7 @@ void packageOperation_should_apply_check_force_canonicalVersions() { @Test void packageOperation_should_respect_count_offset() { Bundle bundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); + PackageVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); spyRepository.transaction(bundle); PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository @@ -329,7 +328,7 @@ void packageOperation_should_respect_count_offset() { @Test void packageOperation_different_bundle_types() { Bundle bundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); + PackageVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); spyRepository.transaction(bundle); PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository @@ -368,7 +367,7 @@ void packageOperation_different_bundle_types() { @Test void packageOperation_should_conditionally_create() { Bundle bundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); + PackageVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); spyRepository.transaction(bundle); PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository @@ -388,7 +387,7 @@ void packageOperation_should_conditionally_create() { @Test void packageOperation_should_respect_include() { Bundle bundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); + PackageVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); spyRepository.transaction(bundle); PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactReleaseVisitorTests.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/ReleaseVisitorTests.java similarity index 91% rename from cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactReleaseVisitorTests.java rename to cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/ReleaseVisitorTests.java index be0b0c135..8ec53d011 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/KnowledgeArtifactReleaseVisitorTests.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/ReleaseVisitorTests.java @@ -52,7 +52,7 @@ import org.opencds.cqf.fhir.utility.visitor.ReleaseVisitor; import org.slf4j.LoggerFactory; -class KnowledgeArtifactReleaseVisitorTests { +class ReleaseVisitorTests { private final FhirContext fhirContext = FhirContext.forR4Cached(); private Repository spyRepository; private final IParser jsonParser = fhirContext.newJsonParser(); @@ -76,9 +76,8 @@ class KnowledgeArtifactReleaseVisitorTests { @BeforeEach void setup() { - SearchParameter sp = (SearchParameter) - jsonParser.parseResource(KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream( - "SearchParameter-artifactAssessment.json")); + SearchParameter sp = (SearchParameter) jsonParser.parseResource( + ReleaseVisitorTests.class.getResourceAsStream("SearchParameter-artifactAssessment.json")); spyRepository = spy(new InMemoryFhirRepository(fhirContext)); spyRepository.update(sp); doAnswer(new Answer() { @@ -95,7 +94,7 @@ public Bundle answer(InvocationOnMock a) throws Throwable { @Test void visitLibraryTest() { Bundle bundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream("Bundle-ersd-release-bundle.json")); + ReleaseVisitorTests.class.getResourceAsStream("Bundle-ersd-release-bundle.json")); spyRepository.transaction(bundle); ReleaseVisitor releaseVisitor = new ReleaseVisitor(); Library library = spyRepository @@ -199,7 +198,7 @@ void visitLibraryTest() { @Test void releaseResource_force_version() { Bundle bundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft.json")); + ReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft.json")); spyRepository.transaction(bundle); // Existing version should be "1.2.3"; String newVersionToForce = "1.2.7"; @@ -226,14 +225,12 @@ void releaseResource_force_version() { @Test void releaseResource_require_non_experimental_error() { // SpecificationLibrary - root is experimentalbut HAS experimental children - Bundle bundle = - (Bundle) jsonParser.parseResource(KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream( - "Bundle-small-approved-draft-experimental.json")); + Bundle bundle = (Bundle) jsonParser.parseResource( + ReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft-experimental.json")); spyRepository.transaction(bundle); // SpecificationLibrary2 - root is NOT experimental but HAS experimental children - Bundle bundle2 = - (Bundle) jsonParser.parseResource(KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream( - "Bundle-small-approved-draft-experimental-children.json")); + Bundle bundle2 = (Bundle) jsonParser.parseResource(ReleaseVisitorTests.class.getResourceAsStream( + "Bundle-small-approved-draft-experimental-children.json")); spyRepository.transaction(bundle2); Parameters params = parameters( part("version", new StringType("1.2.3")), @@ -270,14 +267,12 @@ void releaseResource_require_non_experimental_error() { @Test void releaseResource_require_non_experimental_warn() { // SpecificationLibrary - root is experimentalbut HAS experimental children - Bundle bundle = - (Bundle) jsonParser.parseResource(KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream( - "Bundle-small-approved-draft-experimental.json")); + Bundle bundle = (Bundle) jsonParser.parseResource( + ReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft-experimental.json")); spyRepository.transaction(bundle); // SpecificationLibrary2 - root is NOT experimental but HAS experimental children - Bundle bundle2 = - (Bundle) jsonParser.parseResource(KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream( - "Bundle-small-approved-draft-experimental-children.json")); + Bundle bundle2 = (Bundle) jsonParser.parseResource(ReleaseVisitorTests.class.getResourceAsStream( + "Bundle-small-approved-draft-experimental-children.json")); spyRepository.transaction(bundle2); ReleaseVisitor releaseVisitor = new ReleaseVisitor(); @@ -330,9 +325,8 @@ void releaseResource_require_non_experimental_warn() { @Test void releaseResource_propagate_effective_period() { - Bundle bundle = - (Bundle) jsonParser.parseResource(KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream( - "Bundle-ersd-no-child-effective-period.json")); + Bundle bundle = (Bundle) jsonParser.parseResource( + ReleaseVisitorTests.class.getResourceAsStream("Bundle-ersd-no-child-effective-period.json")); spyRepository.transaction(bundle); String effectivePeriodToPropagate = "2020-12-11"; @@ -368,7 +362,7 @@ void releaseResource_propagate_effective_period() { @Test void releaseResource_latestFromTx_NotSupported_test() { Bundle bundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft.json")); + ReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft.json")); spyRepository.transaction(bundle); String actualErrorMessage = ""; @@ -393,9 +387,8 @@ void releaseResource_latestFromTx_NotSupported_test() { @Test void release_missing_approvalDate_validation_test() { - Bundle bundle = - (Bundle) jsonParser.parseResource(KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream( - "Bundle-release-missing-approvalDate.json")); + Bundle bundle = (Bundle) jsonParser.parseResource( + ReleaseVisitorTests.class.getResourceAsStream("Bundle-release-missing-approvalDate.json")); spyRepository.transaction(bundle); String versionData = "1.2.3"; @@ -418,7 +411,7 @@ void release_missing_approvalDate_validation_test() { @Test void release_version_format_test() { Bundle bundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft.json")); + ReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft.json")); spyRepository.transaction(bundle); ReleaseVisitor releaseVisitor = new ReleaseVisitor(); Library library = spyRepository @@ -442,7 +435,7 @@ void release_version_format_test() { @Test void release_releaseLabel_test() { Bundle bundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft.json")); + ReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft.json")); spyRepository.transaction(bundle); String releaseLabel = "release label test"; ReleaseVisitor releaseVisitor = new ReleaseVisitor(); @@ -472,7 +465,7 @@ void release_releaseLabel_test() { @Test void release_version_active_test() { Bundle bundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); + ReleaseVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); spyRepository.transaction(bundle); ReleaseVisitor releaseVisitor = new ReleaseVisitor(); Library library = spyRepository @@ -494,7 +487,7 @@ void release_version_active_test() { @Test void release_versionBehaviour_format_test() { Bundle bundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft.json")); + ReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft.json")); spyRepository.transaction(bundle); ReleaseVisitor releaseVisitor = new ReleaseVisitor(); Library library = spyRepository @@ -520,7 +513,7 @@ void release_versionBehaviour_format_test() { @Test void release_preserves_extensions() { var bundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft.json")); + ReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft.json")); spyRepository.transaction(bundle); var releaseVisitor = new ReleaseVisitor(); var orginalLibrary = spyRepository diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactApproveVisitorTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/ApproveVisitorTest.java similarity index 96% rename from cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactApproveVisitorTest.java rename to cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/ApproveVisitorTest.java index 80aa7281e..629badd0e 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactApproveVisitorTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/ApproveVisitorTest.java @@ -39,15 +39,15 @@ import org.opencds.cqf.fhir.utility.repository.InMemoryFhirRepository; import org.opencds.cqf.fhir.utility.visitor.ApproveVisitor; -class KnowledgeArtifactApproveVisitorTest { +class ApproveVisitorTest { private final FhirContext fhirContext = FhirContext.forR5Cached(); private Repository spyRepository; private final IParser jsonParser = fhirContext.newJsonParser(); @BeforeEach void setup() { - var lib = (Library) jsonParser.parseResource( - KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream("Library-ersd-active.json")); + var lib = (Library) + jsonParser.parseResource(ReleaseVisitorTests.class.getResourceAsStream("Library-ersd-active.json")); spyRepository = spy(new InMemoryFhirRepository(fhirContext)); spyRepository.update(lib); doAnswer(new Answer() { @@ -112,8 +112,8 @@ void approveOperation_should_respect_artifactAssessment_information_type_binding @Test void approveOperation_test() { - var practitioner = (Practitioner) jsonParser.parseResource( - KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream("Practitioner-minimal.json")); + var practitioner = (Practitioner) + jsonParser.parseResource(ReleaseVisitorTests.class.getResourceAsStream("Practitioner-minimal.json")); spyRepository.update(practitioner); Date today = new Date(); // get today's date in the form "2023-05-11" diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactDraftVisitorTests.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/DraftVisitorTests.java similarity index 90% rename from cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactDraftVisitorTests.java rename to cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/DraftVisitorTests.java index 06a5837ec..02c62c5e7 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactDraftVisitorTests.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/DraftVisitorTests.java @@ -44,7 +44,7 @@ import org.opencds.cqf.fhir.utility.visitor.DraftVisitor; import org.opencds.cqf.fhir.utility.visitor.KnowledgeArtifactVisitor; -class KnowledgeArtifactDraftVisitorTests { +class DraftVisitorTests { private final FhirContext fhirContext = FhirContext.forR5Cached(); private Repository spyRepository; private final IParser jsonParser = fhirContext.newJsonParser(); @@ -83,8 +83,8 @@ public Bundle answer(InvocationOnMock a) throws Throwable { @Test void library_draft_test() { - Bundle bundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactDraftVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); + Bundle bundle = (Bundle) + jsonParser.parseResource(DraftVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); spyRepository.transaction(bundle); KnowledgeArtifactVisitor draftVisitor = new DraftVisitor(); Library library = spyRepository @@ -137,8 +137,8 @@ void library_draft_test() { @Test void draftOperation_no_effectivePeriod_test() { - Bundle bundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactDraftVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); + Bundle bundle = (Bundle) + jsonParser.parseResource(DraftVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); spyRepository.transaction(bundle); Library baseLib = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) @@ -166,10 +166,10 @@ void draftOperation_no_effectivePeriod_test() { @Test void draftOperation_version_conflict_test() { - Bundle bundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactDraftVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); - Library versionConflictLibrary = (Library) jsonParser.parseResource( - KnowledgeArtifactDraftVisitorTests.class.getResourceAsStream("Library-version-conflict.json")); + Bundle bundle = (Bundle) + jsonParser.parseResource(DraftVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); + Library versionConflictLibrary = (Library) + jsonParser.parseResource(DraftVisitorTests.class.getResourceAsStream("Library-version-conflict.json")); spyRepository.transaction(bundle); spyRepository.update(versionConflictLibrary); Parameters params = parameters(part("version", "1.0.0")); @@ -192,8 +192,8 @@ void draftOperation_version_conflict_test() { @Test void draftOperation_cannot_create_draft_of_draft_test() { - Library versionConflictLibrary = (Library) jsonParser.parseResource( - KnowledgeArtifactDraftVisitorTests.class.getResourceAsStream("Library-version-conflict.json")); + Library versionConflictLibrary = (Library) + jsonParser.parseResource(DraftVisitorTests.class.getResourceAsStream("Library-version-conflict.json")); spyRepository.update(versionConflictLibrary); Parameters params = parameters(part("version", "1.2.1")); String maybeException = ""; @@ -213,8 +213,8 @@ void draftOperation_cannot_create_draft_of_draft_test() { @Test void draftOperation_version_format_test() { - Library versionConflictLibrary = (Library) jsonParser.parseResource( - KnowledgeArtifactDraftVisitorTests.class.getResourceAsStream("Library-version-conflict.json")); + Library versionConflictLibrary = (Library) + jsonParser.parseResource(DraftVisitorTests.class.getResourceAsStream("Library-version-conflict.json")); spyRepository.update(versionConflictLibrary); Library baseLib = spyRepository .read(Library.class, new IdType("Library/SpecificationLibraryDraftVersion-1-0-0-23")) diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactPackageVisitorTests.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/PackageVisitorTests.java similarity index 93% rename from cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactPackageVisitorTests.java rename to cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/PackageVisitorTests.java index cf9da2723..15f0f7fe0 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactPackageVisitorTests.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/PackageVisitorTests.java @@ -47,7 +47,7 @@ import org.opencds.cqf.fhir.utility.repository.InMemoryFhirRepository; import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; -class KnowledgeArtifactPackageVisitorTests { +class PackageVisitorTests { private final FhirContext fhirContext = FhirContext.forR5Cached(); private final IParser jsonParser = fhirContext.newJsonParser(); private Repository spyRepository; @@ -69,7 +69,7 @@ public Bundle answer(InvocationOnMock a) throws Throwable { @Test void visitLibraryTest() { Bundle loadedBundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example-naive.json")); + PackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example-naive.json")); spyRepository.transaction(loadedBundle); PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository @@ -101,8 +101,8 @@ void visitLibraryTest() { @Test @Disabled("This test needs a ValueSet that cannot be naively expanded") void packageOperation_should_fail_no_credentials() { - Bundle loadedBundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); + Bundle loadedBundle = (Bundle) + jsonParser.parseResource(PackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); spyRepository.transaction(loadedBundle); PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository @@ -123,8 +123,8 @@ void packageOperation_should_fail_no_credentials() { @Test @Disabled("This test needs a ValueSet that cannot be naively expanded") void packageOperation_should_fail_credentials_missing_username() { - Bundle loadedBundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); + Bundle loadedBundle = (Bundle) + jsonParser.parseResource(PackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); spyRepository.transaction(loadedBundle); PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository @@ -149,8 +149,8 @@ void packageOperation_should_fail_credentials_missing_username() { @Test @Disabled("This test needs a ValueSet that cannot be naively expanded") void packageOperation_should_fail_credentials_missing_apikey() { - Bundle loadedBundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); + Bundle loadedBundle = (Bundle) + jsonParser.parseResource(PackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); spyRepository.transaction(loadedBundle); PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository @@ -175,8 +175,8 @@ void packageOperation_should_fail_credentials_missing_apikey() { @Test @Disabled("This test needs a ValueSet that cannot be naively expanded") void packageOperation_should_fail_credentials_invalid() { - Bundle loadedBundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); + Bundle loadedBundle = (Bundle) + jsonParser.parseResource(PackageVisitorTests.class.getResourceAsStream("Bundle-ersd-example.json")); spyRepository.transaction(loadedBundle); PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository @@ -201,9 +201,8 @@ void packageOperation_should_fail_credentials_invalid() { @Test void packageOperation_should_fail_non_matching_capability() { - Bundle bundle = - (Bundle) jsonParser.parseResource(KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream( - "Bundle-ersd-package-capabilities.json")); + Bundle bundle = (Bundle) jsonParser.parseResource( + PackageVisitorTests.class.getResourceAsStream("Bundle-ersd-package-capabilities.json")); spyRepository.transaction(bundle); List capabilities = Arrays.asList("computable", "publishable", "executable"); PackageVisitor packageVisitor = new PackageVisitor(fhirContext); @@ -236,7 +235,7 @@ void packageOperation_should_fail_non_matching_capability() { @Test void packageOperation_should_apply_check_force_canonicalVersions() { Bundle bundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-active-no-versions.json")); + PackageVisitorTests.class.getResourceAsStream("Bundle-active-no-versions.json")); spyRepository.transaction(bundle); PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository @@ -298,7 +297,7 @@ void packageOperation_should_apply_check_force_canonicalVersions() { @Test void packageOperation_should_respect_count_offset() { Bundle bundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); + PackageVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); spyRepository.transaction(bundle); PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository @@ -336,7 +335,7 @@ void packageOperation_should_respect_count_offset() { @Test void packageOperation_different_bundle_types() { Bundle bundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); + PackageVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); spyRepository.transaction(bundle); PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository @@ -375,7 +374,7 @@ void packageOperation_different_bundle_types() { @Test void packageOperation_should_conditionally_create() { Bundle bundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); + PackageVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); spyRepository.transaction(bundle); PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository @@ -395,7 +394,7 @@ void packageOperation_should_conditionally_create() { @Test void packageOperation_should_respect_include() { Bundle bundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactPackageVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); + PackageVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); spyRepository.transaction(bundle); PackageVisitor packageVisitor = new PackageVisitor(fhirContext); Library library = spyRepository diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactReleaseVisitorTests.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/ReleaseVisitorTests.java similarity index 92% rename from cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactReleaseVisitorTests.java rename to cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/ReleaseVisitorTests.java index 89042b71b..0e9d060fd 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/KnowledgeArtifactReleaseVisitorTests.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/ReleaseVisitorTests.java @@ -51,7 +51,7 @@ import org.opencds.cqf.fhir.utility.visitor.ReleaseVisitor; import org.slf4j.LoggerFactory; -class KnowledgeArtifactReleaseVisitorTests { +class ReleaseVisitorTests { private final FhirContext fhirContext = FhirContext.forR5Cached(); private Repository spyRepository; private final IParser jsonParser = fhirContext.newJsonParser(); @@ -75,9 +75,8 @@ class KnowledgeArtifactReleaseVisitorTests { @BeforeEach void setup() { - SearchParameter sp = (SearchParameter) - jsonParser.parseResource(KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream( - "SearchParameter-artifactAssessment.json")); + SearchParameter sp = (SearchParameter) jsonParser.parseResource( + ReleaseVisitorTests.class.getResourceAsStream("SearchParameter-artifactAssessment.json")); spyRepository = spy(new InMemoryFhirRepository(fhirContext)); spyRepository.update(sp); doAnswer(new Answer() { @@ -94,7 +93,7 @@ public Bundle answer(InvocationOnMock a) throws Throwable { @Test void visitLibraryTest() { Bundle bundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream("Bundle-ersd-release-bundle.json")); + ReleaseVisitorTests.class.getResourceAsStream("Bundle-ersd-release-bundle.json")); spyRepository.transaction(bundle); ReleaseVisitor releaseVisitor = new ReleaseVisitor(); Library library = spyRepository @@ -196,7 +195,7 @@ void visitLibraryTest() { @Test void releaseResource_force_version() { Bundle bundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft.json")); + ReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft.json")); spyRepository.transaction(bundle); // Existing version should be "1.2.3"; String newVersionToForce = "1.2.7"; @@ -223,14 +222,12 @@ void releaseResource_force_version() { @Test void releaseResource_require_non_experimental_error() { // SpecificationLibrary - root is experimentalbut HAS experimental children - Bundle bundle = - (Bundle) jsonParser.parseResource(KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream( - "Bundle-small-approved-draft-experimental.json")); + Bundle bundle = (Bundle) jsonParser.parseResource( + ReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft-experimental.json")); spyRepository.transaction(bundle); // SpecificationLibrary2 - root is NOT experimental but HAS experimental children - Bundle bundle2 = - (Bundle) jsonParser.parseResource(KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream( - "Bundle-small-approved-draft-experimental-children.json")); + Bundle bundle2 = (Bundle) jsonParser.parseResource(ReleaseVisitorTests.class.getResourceAsStream( + "Bundle-small-approved-draft-experimental-children.json")); spyRepository.transaction(bundle2); Parameters params = parameters( part("version", new StringType("1.2.3")), @@ -267,14 +264,12 @@ void releaseResource_require_non_experimental_error() { @Test void releaseResource_require_non_experimental_warn() { // SpecificationLibrary - root is experimentalbut HAS experimental children - Bundle bundle = - (Bundle) jsonParser.parseResource(KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream( - "Bundle-small-approved-draft-experimental.json")); + Bundle bundle = (Bundle) jsonParser.parseResource( + ReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft-experimental.json")); spyRepository.transaction(bundle); // SpecificationLibrary2 - root is NOT experimental but HAS experimental children - Bundle bundle2 = - (Bundle) jsonParser.parseResource(KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream( - "Bundle-small-approved-draft-experimental-children.json")); + Bundle bundle2 = (Bundle) jsonParser.parseResource(ReleaseVisitorTests.class.getResourceAsStream( + "Bundle-small-approved-draft-experimental-children.json")); spyRepository.transaction(bundle2); ReleaseVisitor releaseVisitor = new ReleaseVisitor(); @@ -327,9 +322,8 @@ void releaseResource_require_non_experimental_warn() { @Test void releaseResource_propagate_effective_period() { - Bundle bundle = - (Bundle) jsonParser.parseResource(KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream( - "Bundle-ersd-no-child-effective-period.json")); + Bundle bundle = (Bundle) jsonParser.parseResource( + ReleaseVisitorTests.class.getResourceAsStream("Bundle-ersd-no-child-effective-period.json")); spyRepository.transaction(bundle); String effectivePeriodToPropagate = "2020-12-11"; @@ -365,7 +359,7 @@ void releaseResource_propagate_effective_period() { @Test void releaseResource_latestFromTx_NotSupported_test() { Bundle bundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft.json")); + ReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft.json")); spyRepository.transaction(bundle); String actualErrorMessage = ""; @@ -390,9 +384,8 @@ void releaseResource_latestFromTx_NotSupported_test() { @Test void release_missing_approvalDate_validation_test() { - Bundle bundle = - (Bundle) jsonParser.parseResource(KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream( - "Bundle-release-missing-approvalDate.json")); + Bundle bundle = (Bundle) jsonParser.parseResource( + ReleaseVisitorTests.class.getResourceAsStream("Bundle-release-missing-approvalDate.json")); spyRepository.transaction(bundle); String versionData = "1.2.3"; @@ -415,7 +408,7 @@ void release_missing_approvalDate_validation_test() { @Test void release_version_format_test() { Bundle bundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft.json")); + ReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft.json")); spyRepository.transaction(bundle); ReleaseVisitor releaseVisitor = new ReleaseVisitor(); Library library = spyRepository @@ -439,7 +432,7 @@ void release_version_format_test() { @Test void release_releaseLabel_test() { Bundle bundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft.json")); + ReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft.json")); spyRepository.transaction(bundle); String releaseLabel = "release label test"; ReleaseVisitor releaseVisitor = new ReleaseVisitor(); @@ -469,7 +462,7 @@ void release_releaseLabel_test() { @Test void release_version_active_test() { Bundle bundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); + ReleaseVisitorTests.class.getResourceAsStream("Bundle-ersd-small-active.json")); spyRepository.transaction(bundle); ReleaseVisitor releaseVisitor = new ReleaseVisitor(); Library library = spyRepository @@ -491,7 +484,7 @@ void release_version_active_test() { @Test void release_versionBehaviour_format_test() { Bundle bundle = (Bundle) jsonParser.parseResource( - KnowledgeArtifactReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft.json")); + ReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft.json")); spyRepository.transaction(bundle); ReleaseVisitor releaseVisitor = new ReleaseVisitor(); Library library = spyRepository From 59c517dbab71da774f950ff0515b5d8c58f1a5ac Mon Sep 17 00:00:00 2001 From: Brenin Rhodes Date: Mon, 1 Jul 2024 08:36:36 -0600 Subject: [PATCH 08/38] Fix class name --- ...tionnaireAdapaterTest.java => QuestionnaireAdapterTest.java} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/{QuestionnaireAdapaterTest.java => QuestionnaireAdapterTest.java} (98%) diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapaterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapterTest.java similarity index 98% rename from cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapaterTest.java rename to cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapterTest.java index 151b5ef91..a4ac32c4f 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapaterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapterTest.java @@ -14,7 +14,7 @@ import org.junit.jupiter.api.Test; import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; -public class QuestionnaireAdapaterTest { +public class QuestionnaireAdapterTest { private final FhirContext fhirContext = FhirContext.forR4Cached(); @Test From 5a70e75e60a6e4328d5c651ff72807ec497a0084 Mon Sep 17 00:00:00 2001 From: Brenin Rhodes Date: Tue, 2 Jul 2024 09:05:04 -0600 Subject: [PATCH 09/38] clean up adapters --- .../org/opencds/cqf/fhir/cql/Engines.java | 2 +- .../CqlFhirParametersConverter.java | 2 +- .../ActivityDefinitionProcessor.java | 2 +- .../cqf/fhir/cr/library/LibraryProcessor.java | 2 +- .../measure/r4/R4DataRequirementsService.java | 2 +- .../PlanDefinitionProcessor.java | 2 +- .../questionnaire/QuestionnaireProcessor.java | 2 +- .../QuestionnaireResponseProcessor.java | 2 +- .../cqf/fhir/cr/helpers/RequestHelpers.java | 2 +- .../cr/plandefinition/PlanDefinition.java | 2 +- .../PlanDefinitionProcessorTests.java | 2 +- .../cr/questionnaire/TestItemGenerator.java | 2 +- .../TestQuestionnaireResponse.java | 2 +- cqf-fhir-utility/pom.xml | 4 + .../utility/adapter/BaseResourceAdapter.java | 30 +++ .../adapter/KnowledgeArtifactAdapter.java | 249 +++++++++++++----- .../fhir/utility/adapter/ResourceAdapter.java | 83 +++++- .../dstu3/KnowledgeArtifactAdapter.java | 138 ++-------- .../utility/adapter/dstu3/LibraryAdapter.java | 226 ++-------------- .../adapter/dstu3/PlanDefinitionAdapter.java | 203 +------------- .../adapter/dstu3/QuestionnaireAdapter.java | 26 +- .../adapter/dstu3/ResourceAdapter.java | 65 ++--- .../dstu3/StructureDefinitionAdapter.java | 182 +------------ .../adapter/dstu3/ValueSetAdapter.java | 236 +++-------------- .../adapter/r4/KnowledgeArtifactAdapter.java | 131 ++------- .../utility/adapter/r4/LibraryAdapter.java | 213 ++------------- .../adapter/r4/PlanDefinitionAdapter.java | 201 +------------- .../adapter/r4/QuestionnaireAdapter.java | 27 +- .../utility/adapter/r4/ResourceAdapter.java | 67 ++--- .../r4/StructureDefinitionAdapter.java | 168 +----------- .../utility/adapter/r4/ValueSetAdapter.java | 215 ++------------- .../adapter/r5/KnowledgeArtifactAdapter.java | 139 ++-------- .../utility/adapter/r5/LibraryAdapter.java | 223 +++------------- .../adapter/r5/PlanDefinitionAdapter.java | 202 +------------- .../adapter/r5/QuestionnaireAdapter.java | 27 +- .../utility/adapter/r5/ResourceAdapter.java | 65 ++--- .../r5/StructureDefinitionAdapter.java | 61 ++--- .../utility/adapter/r5/ValueSetAdapter.java | 222 +++------------- .../utility}/model/DynamicModelResolver.java | 2 +- .../model/FhirModelResolverCache.java | 2 +- .../fhir/utility/visitor/ReleaseVisitor.java | 22 +- .../utility/visitor/dstu3/ReleaseVisitor.java | 2 +- .../utility/visitor/r4/ReleaseVisitor.java | 2 +- .../utility/visitor/r5/ReleaseVisitor.java | 2 +- .../cqf/fhir/utility/ValueSetsTest.java | 3 + .../dstu3/StructureDefinitionAdapterTest.java | 29 ++ .../visitor/dstu3/ReleaseVisitorTests.java | 2 + .../visitor/r4/ReleaseVisitorTests.java | 10 +- .../visitor/r5/ReleaseVisitorTests.java | 2 + 49 files changed, 774 insertions(+), 2733 deletions(-) create mode 100644 cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/BaseResourceAdapter.java rename {cqf-fhir-cql/src/main/java/org/opencds/cqf/fhir/cql/engine => cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility}/model/DynamicModelResolver.java (99%) rename {cqf-fhir-cql/src/main/java/org/opencds/cqf/fhir/cql/engine => cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility}/model/FhirModelResolverCache.java (97%) create mode 100644 cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/ValueSetsTest.java diff --git a/cqf-fhir-cql/src/main/java/org/opencds/cqf/fhir/cql/Engines.java b/cqf-fhir-cql/src/main/java/org/opencds/cqf/fhir/cql/Engines.java index 739797df2..2abed3146 100644 --- a/cqf-fhir-cql/src/main/java/org/opencds/cqf/fhir/cql/Engines.java +++ b/cqf-fhir-cql/src/main/java/org/opencds/cqf/fhir/cql/Engines.java @@ -27,7 +27,6 @@ import org.opencds.cqf.fhir.api.Repository; import org.opencds.cqf.fhir.cql.cql2elm.content.RepositoryFhirLibrarySourceProvider; import org.opencds.cqf.fhir.cql.cql2elm.util.LibraryVersionSelector; -import org.opencds.cqf.fhir.cql.engine.model.FhirModelResolverCache; import org.opencds.cqf.fhir.cql.engine.parameters.CqlFhirParametersConverter; import org.opencds.cqf.fhir.cql.engine.retrieve.FederatedDataProvider; import org.opencds.cqf.fhir.cql.engine.retrieve.RepositoryRetrieveProvider; @@ -35,6 +34,7 @@ import org.opencds.cqf.fhir.cql.engine.terminology.RepositoryTerminologyProvider; import org.opencds.cqf.fhir.utility.Constants; import org.opencds.cqf.fhir.utility.adapter.AdapterFactory; +import org.opencds.cqf.fhir.utility.model.FhirModelResolverCache; import org.opencds.cqf.fhir.utility.repository.InMemoryFhirRepository; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/cqf-fhir-cql/src/main/java/org/opencds/cqf/fhir/cql/engine/parameters/CqlFhirParametersConverter.java b/cqf-fhir-cql/src/main/java/org/opencds/cqf/fhir/cql/engine/parameters/CqlFhirParametersConverter.java index 87f49aee4..6c37d1d93 100644 --- a/cqf-fhir-cql/src/main/java/org/opencds/cqf/fhir/cql/engine/parameters/CqlFhirParametersConverter.java +++ b/cqf-fhir-cql/src/main/java/org/opencds/cqf/fhir/cql/engine/parameters/CqlFhirParametersConverter.java @@ -20,10 +20,10 @@ import org.opencds.cqf.cql.engine.execution.ExpressionResult; import org.opencds.cqf.cql.engine.fhir.converter.FhirTypeConverter; import org.opencds.cqf.cql.engine.model.ModelResolver; -import org.opencds.cqf.fhir.cql.engine.model.FhirModelResolverCache; import org.opencds.cqf.fhir.utility.adapter.AdapterFactory; import org.opencds.cqf.fhir.utility.adapter.ParametersAdapter; import org.opencds.cqf.fhir.utility.adapter.ParametersParameterComponentAdapter; +import org.opencds.cqf.fhir.utility.model.FhirModelResolverCache; import org.slf4j.LoggerFactory; public class CqlFhirParametersConverter { diff --git a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/activitydefinition/ActivityDefinitionProcessor.java b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/activitydefinition/ActivityDefinitionProcessor.java index 854ae7b30..27aed1d8b 100644 --- a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/activitydefinition/ActivityDefinitionProcessor.java +++ b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/activitydefinition/ActivityDefinitionProcessor.java @@ -18,13 +18,13 @@ import org.opencds.cqf.fhir.cql.EvaluationSettings; import org.opencds.cqf.fhir.cql.ExtensionResolver; import org.opencds.cqf.fhir.cql.LibraryEngine; -import org.opencds.cqf.fhir.cql.engine.model.FhirModelResolverCache; import org.opencds.cqf.fhir.cr.activitydefinition.apply.ApplyProcessor; import org.opencds.cqf.fhir.cr.activitydefinition.apply.ApplyRequest; import org.opencds.cqf.fhir.cr.activitydefinition.apply.IApplyProcessor; import org.opencds.cqf.fhir.cr.activitydefinition.apply.IRequestResolverFactory; import org.opencds.cqf.fhir.cr.common.ResourceResolver; import org.opencds.cqf.fhir.utility.Ids; +import org.opencds.cqf.fhir.utility.model.FhirModelResolverCache; import org.opencds.cqf.fhir.utility.monad.Either3; import org.opencds.cqf.fhir.utility.repository.operations.IActivityDefinitionProcessor; diff --git a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/library/LibraryProcessor.java b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/library/LibraryProcessor.java index 1d8136a2a..06c82e427 100644 --- a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/library/LibraryProcessor.java +++ b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/library/LibraryProcessor.java @@ -13,10 +13,10 @@ import org.opencds.cqf.cql.engine.model.ModelResolver; import org.opencds.cqf.fhir.api.Repository; import org.opencds.cqf.fhir.cql.EvaluationSettings; -import org.opencds.cqf.fhir.cql.engine.model.FhirModelResolverCache; import org.opencds.cqf.fhir.cr.common.IPackageProcessor; import org.opencds.cqf.fhir.cr.common.PackageProcessor; import org.opencds.cqf.fhir.cr.common.ResourceResolver; +import org.opencds.cqf.fhir.utility.model.FhirModelResolverCache; import org.opencds.cqf.fhir.utility.monad.Either3; public class LibraryProcessor { diff --git a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/measure/r4/R4DataRequirementsService.java b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/measure/r4/R4DataRequirementsService.java index 1cfbd2bfc..56ccf055d 100644 --- a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/measure/r4/R4DataRequirementsService.java +++ b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/measure/r4/R4DataRequirementsService.java @@ -46,7 +46,6 @@ import org.opencds.cqf.fhir.api.Repository; import org.opencds.cqf.fhir.cql.cql2elm.content.RepositoryFhirLibrarySourceProvider; import org.opencds.cqf.fhir.cql.cql2elm.util.LibraryVersionSelector; -import org.opencds.cqf.fhir.cql.engine.model.FhirModelResolverCache; import org.opencds.cqf.fhir.cql.engine.terminology.RepositoryTerminologyProvider; import org.opencds.cqf.fhir.cr.measure.MeasureEvaluationOptions; import org.opencds.cqf.fhir.cr.measure.helper.DateHelper; @@ -56,6 +55,7 @@ import org.opencds.cqf.fhir.utility.Libraries; import org.opencds.cqf.fhir.utility.adapter.LibraryAdapter; import org.opencds.cqf.fhir.utility.adapter.r4.AdapterFactory; +import org.opencds.cqf.fhir.utility.model.FhirModelResolverCache; import org.opencds.cqf.fhir.utility.search.Searches; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/plandefinition/PlanDefinitionProcessor.java b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/plandefinition/PlanDefinitionProcessor.java index 7147ca4e9..873cdcfba 100644 --- a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/plandefinition/PlanDefinitionProcessor.java +++ b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/plandefinition/PlanDefinitionProcessor.java @@ -18,7 +18,6 @@ import org.opencds.cqf.fhir.api.Repository; import org.opencds.cqf.fhir.cql.EvaluationSettings; import org.opencds.cqf.fhir.cql.LibraryEngine; -import org.opencds.cqf.fhir.cql.engine.model.FhirModelResolverCache; import org.opencds.cqf.fhir.cr.activitydefinition.apply.IRequestResolverFactory; import org.opencds.cqf.fhir.cr.common.IPackageProcessor; import org.opencds.cqf.fhir.cr.common.PackageProcessor; @@ -27,6 +26,7 @@ import org.opencds.cqf.fhir.cr.plandefinition.apply.ApplyRequest; import org.opencds.cqf.fhir.cr.plandefinition.apply.IApplyProcessor; import org.opencds.cqf.fhir.utility.Ids; +import org.opencds.cqf.fhir.utility.model.FhirModelResolverCache; import org.opencds.cqf.fhir.utility.monad.Either3; @SuppressWarnings({"unused", "squid:S107", "squid:S1172"}) diff --git a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/questionnaire/QuestionnaireProcessor.java b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/questionnaire/QuestionnaireProcessor.java index 06c11e4a3..de15c5e34 100644 --- a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/questionnaire/QuestionnaireProcessor.java +++ b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/questionnaire/QuestionnaireProcessor.java @@ -17,7 +17,6 @@ import org.opencds.cqf.fhir.api.Repository; import org.opencds.cqf.fhir.cql.EvaluationSettings; import org.opencds.cqf.fhir.cql.LibraryEngine; -import org.opencds.cqf.fhir.cql.engine.model.FhirModelResolverCache; import org.opencds.cqf.fhir.cr.common.IPackageProcessor; import org.opencds.cqf.fhir.cr.common.PackageProcessor; import org.opencds.cqf.fhir.cr.common.ResourceResolver; @@ -28,6 +27,7 @@ import org.opencds.cqf.fhir.cr.questionnaire.populate.PopulateProcessor; import org.opencds.cqf.fhir.cr.questionnaire.populate.PopulateRequest; import org.opencds.cqf.fhir.utility.Ids; +import org.opencds.cqf.fhir.utility.model.FhirModelResolverCache; import org.opencds.cqf.fhir.utility.monad.Either3; public class QuestionnaireProcessor { diff --git a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/questionnaireresponse/QuestionnaireResponseProcessor.java b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/questionnaireresponse/QuestionnaireResponseProcessor.java index 6a9f07667..56bfabcad 100644 --- a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/questionnaireresponse/QuestionnaireResponseProcessor.java +++ b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/questionnaireresponse/QuestionnaireResponseProcessor.java @@ -16,12 +16,12 @@ import org.opencds.cqf.fhir.api.Repository; import org.opencds.cqf.fhir.cql.EvaluationSettings; import org.opencds.cqf.fhir.cql.LibraryEngine; -import org.opencds.cqf.fhir.cql.engine.model.FhirModelResolverCache; import org.opencds.cqf.fhir.cr.common.ResourceResolver; import org.opencds.cqf.fhir.cr.questionnaireresponse.extract.ExtractProcessor; import org.opencds.cqf.fhir.cr.questionnaireresponse.extract.ExtractRequest; import org.opencds.cqf.fhir.cr.questionnaireresponse.extract.IExtractProcessor; import org.opencds.cqf.fhir.utility.SearchHelper; +import org.opencds.cqf.fhir.utility.model.FhirModelResolverCache; import org.opencds.cqf.fhir.utility.monad.Either; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/helpers/RequestHelpers.java b/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/helpers/RequestHelpers.java index 98c4945a1..5c760e623 100644 --- a/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/helpers/RequestHelpers.java +++ b/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/helpers/RequestHelpers.java @@ -6,12 +6,12 @@ import org.hl7.fhir.instance.model.api.IBaseResource; import org.opencds.cqf.cql.engine.model.ModelResolver; import org.opencds.cqf.fhir.cql.LibraryEngine; -import org.opencds.cqf.fhir.cql.engine.model.FhirModelResolverCache; import org.opencds.cqf.fhir.cr.inputparameters.IInputParameterResolver; import org.opencds.cqf.fhir.cr.plandefinition.apply.ApplyRequest; import org.opencds.cqf.fhir.cr.questionnaire.generate.GenerateRequest; import org.opencds.cqf.fhir.cr.questionnaire.populate.PopulateRequest; import org.opencds.cqf.fhir.utility.Ids; +import org.opencds.cqf.fhir.utility.model.FhirModelResolverCache; public class RequestHelpers { public static final String PATIENT_ID = "patientId"; diff --git a/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/plandefinition/PlanDefinition.java b/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/plandefinition/PlanDefinition.java index e260cf2db..723acb2c2 100644 --- a/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/plandefinition/PlanDefinition.java +++ b/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/plandefinition/PlanDefinition.java @@ -26,13 +26,13 @@ import org.opencds.cqf.cql.engine.model.ModelResolver; import org.opencds.cqf.fhir.api.Repository; import org.opencds.cqf.fhir.cql.EvaluationSettings; -import org.opencds.cqf.fhir.cql.engine.model.FhirModelResolverCache; import org.opencds.cqf.fhir.cql.engine.retrieve.RetrieveSettings.SEARCH_FILTER_MODE; import org.opencds.cqf.fhir.cql.engine.retrieve.RetrieveSettings.TERMINOLOGY_FILTER_MODE; import org.opencds.cqf.fhir.cql.engine.terminology.TerminologySettings.VALUESET_EXPANSION_MODE; import org.opencds.cqf.fhir.cr.TestOperationProvider; import org.opencds.cqf.fhir.cr.helpers.GeneratedPackage; import org.opencds.cqf.fhir.utility.Ids; +import org.opencds.cqf.fhir.utility.model.FhirModelResolverCache; import org.opencds.cqf.fhir.utility.monad.Eithers; import org.opencds.cqf.fhir.utility.repository.InMemoryFhirRepository; import org.opencds.cqf.fhir.utility.repository.ig.IgRepository; diff --git a/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/plandefinition/PlanDefinitionProcessorTests.java b/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/plandefinition/PlanDefinitionProcessorTests.java index 3adad0e59..7c8451c21 100644 --- a/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/plandefinition/PlanDefinitionProcessorTests.java +++ b/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/plandefinition/PlanDefinitionProcessorTests.java @@ -12,12 +12,12 @@ import java.nio.file.Paths; import org.junit.jupiter.api.Test; import org.opencds.cqf.fhir.cql.EvaluationSettings; -import org.opencds.cqf.fhir.cql.engine.model.FhirModelResolverCache; import org.opencds.cqf.fhir.cr.activitydefinition.apply.IRequestResolverFactory; import org.opencds.cqf.fhir.cr.common.PackageProcessor; import org.opencds.cqf.fhir.cr.plandefinition.apply.ApplyProcessor; import org.opencds.cqf.fhir.utility.BundleHelper; import org.opencds.cqf.fhir.utility.Ids; +import org.opencds.cqf.fhir.utility.model.FhirModelResolverCache; import org.opencds.cqf.fhir.utility.monad.Eithers; import org.opencds.cqf.fhir.utility.repository.ig.IgRepository; diff --git a/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/questionnaire/TestItemGenerator.java b/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/questionnaire/TestItemGenerator.java index 551234559..9292eaf06 100644 --- a/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/questionnaire/TestItemGenerator.java +++ b/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/questionnaire/TestItemGenerator.java @@ -23,7 +23,7 @@ import org.json.JSONException; import org.opencds.cqf.cql.engine.model.ModelResolver; import org.opencds.cqf.fhir.api.Repository; -import org.opencds.cqf.fhir.cql.engine.model.FhirModelResolverCache; +import org.opencds.cqf.fhir.utility.model.FhirModelResolverCache; import org.opencds.cqf.fhir.utility.monad.Eithers; import org.opencds.cqf.fhir.utility.repository.ig.IgRepository; import org.skyscreamer.jsonassert.JSONAssert; diff --git a/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/questionnaireresponse/TestQuestionnaireResponse.java b/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/questionnaireresponse/TestQuestionnaireResponse.java index d444feb1c..3016a5b2d 100644 --- a/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/questionnaireresponse/TestQuestionnaireResponse.java +++ b/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/questionnaireresponse/TestQuestionnaireResponse.java @@ -16,8 +16,8 @@ import org.hl7.fhir.instance.model.api.IIdType; import org.opencds.cqf.cql.engine.model.ModelResolver; import org.opencds.cqf.fhir.api.Repository; -import org.opencds.cqf.fhir.cql.engine.model.FhirModelResolverCache; import org.opencds.cqf.fhir.utility.Ids; +import org.opencds.cqf.fhir.utility.model.FhirModelResolverCache; import org.opencds.cqf.fhir.utility.monad.Eithers; import org.opencds.cqf.fhir.utility.repository.ig.IgRepository; import org.skyscreamer.jsonassert.JSONAssert; diff --git a/cqf-fhir-utility/pom.xml b/cqf-fhir-utility/pom.xml index a2df8988a..73ec90092 100644 --- a/cqf-fhir-utility/pom.xml +++ b/cqf-fhir-utility/pom.xml @@ -36,6 +36,10 @@ info.cqframework engine + + info.cqframework + engine-fhir + ca.uhn.hapi.fhir hapi-fhir-base diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/BaseResourceAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/BaseResourceAdapter.java new file mode 100644 index 000000000..65b11e9a9 --- /dev/null +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/BaseResourceAdapter.java @@ -0,0 +1,30 @@ +package org.opencds.cqf.fhir.utility.adapter; + +import ca.uhn.fhir.context.BaseRuntimeElementDefinition; +import ca.uhn.fhir.context.FhirContext; +import org.hl7.fhir.instance.model.api.IBaseResource; +import org.opencds.cqf.cql.engine.model.ModelResolver; +import org.opencds.cqf.fhir.utility.model.FhirModelResolverCache; + +public abstract class BaseResourceAdapter implements ResourceAdapter { + protected final FhirContext fhirContext; + protected final BaseRuntimeElementDefinition elementDefinition; + protected final IBaseResource resource; + protected final ModelResolver modelResolver; + + public BaseResourceAdapter(IBaseResource resource) { + this.resource = resource; + fhirContext = FhirContext.forCached(resource.getStructureFhirVersionEnum()); + elementDefinition = fhirContext.getElementDefinition(this.resource.getClass()); + modelResolver = FhirModelResolverCache.resolverForVersion( + fhirContext.getVersion().getVersion()); + } + + public ModelResolver getModelResolver() { + return modelResolver; + } + + public IBaseResource get() { + return resource; + } +} diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/KnowledgeArtifactAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/KnowledgeArtifactAdapter.java index a08d12f7f..5d8068881 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/KnowledgeArtifactAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/KnowledgeArtifactAdapter.java @@ -7,6 +7,7 @@ import java.util.Optional; import java.util.stream.Collectors; import java.util.stream.Stream; +import org.apache.commons.lang3.StringUtils; import org.hl7.fhir.dstu3.model.Reference; import org.hl7.fhir.instance.model.api.IBase; import org.hl7.fhir.instance.model.api.IBaseBundle; @@ -28,50 +29,168 @@ public interface KnowledgeArtifactAdapter extends ResourceAdapter { IDomainResource copy(); default IIdType getId() { - return this.get().getIdElement(); + return get().getIdElement(); } default void setId(IIdType id) { - this.get().setId(id); + get().setId(id); } - String getName(); + default String getName() { + return resolvePathString(get(), "name"); + } - void setName(String name); + default void setName(String name) { + getModelResolver().setValue(get(), "name", newStringType(get().getStructureFhirVersionEnum(), name)); + } - boolean hasUrl(); + default boolean hasUrl() { + return StringUtils.isNotBlank(getUrl()); + } - String getUrl(); + default String getUrl() { + return resolvePathString(get(), "url"); + } - void setUrl(String url); + default void setUrl(String url) { + getModelResolver().setValue(get(), "url", newUriType(get().getStructureFhirVersionEnum(), url)); + } - boolean hasVersion(); + default boolean hasVersion() { + return StringUtils.isNotBlank(getVersion()); + } - String getVersion(); + default String getVersion() { + return resolvePathString(get(), "version"); + } - void setVersion(String version); + default void setVersion(String version) { + getModelResolver().setValue(get(), "version", newStringType(get().getStructureFhirVersionEnum(), version)); + } List getDependencies(); - Date getApprovalDate(); + @SuppressWarnings("unchecked") + default Date getApprovalDate() { + IPrimitiveType approvalDate = resolvePath(get(), "approvalDate", IPrimitiveType.class); + return approvalDate == null ? null : approvalDate.getValue(); + } + + default void setApprovalDate(Date approvalDate) { + getModelResolver() + .setValue(get(), "approvalDate", newDateType(get().getStructureFhirVersionEnum(), approvalDate)); + } - void setApprovalDate(Date approvalDate); + @SuppressWarnings("unchecked") + default Date getDate() { + IPrimitiveType date = resolvePath(get(), "date", IPrimitiveType.class); + return date == null ? null : date.getValue(); + } - Date getDate(); + default void setDate(Date date) { + getModelResolver().setValue(get(), "date", newDateTimeType(get().getStructureFhirVersionEnum(), date)); + } - void setDate(Date date); + default void setDateElement(IPrimitiveType date) { + getModelResolver().setValue(get(), "date", date); + } - void setDateElement(IPrimitiveType approvalDate); + default String getPurpose() { + return resolvePathString(get(), "purpose"); + } String getStatus(); - String getPurpose(); - void setStatus(String status); - ICompositeType getEffectivePeriod(); + default ICompositeType getEffectivePeriod() { + var effectivePeriod = resolvePath(get(), "effectivePeriod", ICompositeType.class); + return effectivePeriod == null ? newPeriod(get().getStructureFhirVersionEnum()) : effectivePeriod; + } + + default void setEffectivePeriod(ICompositeType period) { + try { + getModelResolver().setValue(get(), "effectivePeriod", period); + } catch (Exception e) { + // Do nothing + } + } + + @SuppressWarnings("unchecked") + default boolean getExperimental() { + var experimental = (IPrimitiveType) resolvePath(get(), "experimental", IPrimitiveType.class); + return experimental == null ? false : experimental.getValue(); + } + + @SuppressWarnings("unchecked") + static T newPeriod(FhirVersionEnum version) { + switch (version) { + case DSTU3: + return (T) new org.hl7.fhir.dstu3.model.Period(); + case R4: + return (T) new org.hl7.fhir.r4.model.Period(); + case R5: + return (T) new org.hl7.fhir.r5.model.Period(); + default: + throw new UnprocessableEntityException("Unsupported version: " + version.toString()); + } + } + + @SuppressWarnings("unchecked") + static > T newStringType(FhirVersionEnum version, String string) { + switch (version) { + case DSTU3: + return (T) new org.hl7.fhir.dstu3.model.StringType(string); + case R4: + return (T) new org.hl7.fhir.r4.model.StringType(string); + case R5: + return (T) new org.hl7.fhir.r5.model.StringType(string); + default: + throw new UnprocessableEntityException("Unsupported version: " + version.toString()); + } + } + + @SuppressWarnings("unchecked") + static > T newUriType(FhirVersionEnum version, String string) { + switch (version) { + case DSTU3: + return (T) new org.hl7.fhir.dstu3.model.UriType(string); + case R4: + return (T) new org.hl7.fhir.r4.model.UriType(string); + case R5: + return (T) new org.hl7.fhir.r5.model.UriType(string); + default: + throw new UnprocessableEntityException("Unsupported version: " + version.toString()); + } + } + + @SuppressWarnings("unchecked") + static > T newDateType(FhirVersionEnum version, Date date) { + switch (version) { + case DSTU3: + return (T) new org.hl7.fhir.dstu3.model.DateType(date); + case R4: + return (T) new org.hl7.fhir.r4.model.DateType(date); + case R5: + return (T) new org.hl7.fhir.r5.model.DateType(date); + default: + throw new UnprocessableEntityException("Unsupported version: " + version.toString()); + } + } - boolean getExperimental(); + @SuppressWarnings("unchecked") + static > T newDateTimeType(FhirVersionEnum version, Date date) { + switch (version) { + case DSTU3: + return (T) new org.hl7.fhir.dstu3.model.DateTimeType(date); + case R4: + return (T) new org.hl7.fhir.r4.model.DateTimeType(date); + case R5: + return (T) new org.hl7.fhir.r5.model.DateTimeType(date); + default: + throw new UnprocessableEntityException("Unsupported version: " + version.toString()); + } + } @SuppressWarnings("unchecked") static T newRelatedArtifact( @@ -94,97 +213,83 @@ static T newRelatedArtifact( return (T) r5; default: - throw new UnprocessableEntityException("Upsupported version: " + version.toString()); + throw new UnprocessableEntityException("Unsupported version: " + version.toString()); } } static String getRelatedArtifactReference(T relatedArtifact) { if (relatedArtifact instanceof org.hl7.fhir.dstu3.model.RelatedArtifact) { - return getRelatedArtifactReference((org.hl7.fhir.dstu3.model.RelatedArtifact) relatedArtifact); + return ((org.hl7.fhir.dstu3.model.RelatedArtifact) relatedArtifact) + .getResource() + .getReference(); } else if (relatedArtifact instanceof org.hl7.fhir.r4.model.RelatedArtifact) { - return getRelatedArtifactReference((org.hl7.fhir.r4.model.RelatedArtifact) relatedArtifact); + return ((org.hl7.fhir.r4.model.RelatedArtifact) relatedArtifact).getResource(); } else if (relatedArtifact instanceof org.hl7.fhir.r5.model.RelatedArtifact) { - return getRelatedArtifactReference((org.hl7.fhir.r5.model.RelatedArtifact) relatedArtifact); + return ((org.hl7.fhir.r5.model.RelatedArtifact) relatedArtifact).getResource(); } else { throw new UnprocessableEntityException("Must be a valid RelatedArtifact"); } } - private static String getRelatedArtifactReference(org.hl7.fhir.dstu3.model.RelatedArtifact relatedArtifact) { - return relatedArtifact.getResource().getReference(); - } - - private static String getRelatedArtifactReference(org.hl7.fhir.r4.model.RelatedArtifact relatedArtifact) { - return relatedArtifact.getResource(); - } - - private static String getRelatedArtifactReference(org.hl7.fhir.r5.model.RelatedArtifact relatedArtifact) { - return relatedArtifact.getResource(); - } - static String getRelatedArtifactType(T relatedArtifact) { if (relatedArtifact instanceof org.hl7.fhir.dstu3.model.RelatedArtifact) { - return getRelatedArtifactType((org.hl7.fhir.dstu3.model.RelatedArtifact) relatedArtifact); + return ((org.hl7.fhir.dstu3.model.RelatedArtifact) relatedArtifact) + .getType() + .toCode(); } else if (relatedArtifact instanceof org.hl7.fhir.r4.model.RelatedArtifact) { - return getRelatedArtifactType((org.hl7.fhir.r4.model.RelatedArtifact) relatedArtifact); + return ((org.hl7.fhir.r4.model.RelatedArtifact) relatedArtifact) + .getType() + .toCode(); } else if (relatedArtifact instanceof org.hl7.fhir.r5.model.RelatedArtifact) { - return getRelatedArtifactType((org.hl7.fhir.r5.model.RelatedArtifact) relatedArtifact); + return ((org.hl7.fhir.r5.model.RelatedArtifact) relatedArtifact) + .getType() + .toCode(); } else { throw new UnprocessableEntityException("Must be a valid RelatedArtifact"); } } - private static String getRelatedArtifactType(org.hl7.fhir.dstu3.model.RelatedArtifact relatedArtifact) { - return relatedArtifact.getType().toCode(); - } - - private static String getRelatedArtifactType(org.hl7.fhir.r4.model.RelatedArtifact relatedArtifact) { - return relatedArtifact.getType().toCode(); - } - - private static String getRelatedArtifactType(org.hl7.fhir.r5.model.RelatedArtifact relatedArtifact) { - return relatedArtifact.getType().toCode(); - } - static void setRelatedArtifactReference( T relatedArtifact, String reference) { if (relatedArtifact instanceof org.hl7.fhir.dstu3.model.RelatedArtifact) { - setRelatedArtifactReference((org.hl7.fhir.dstu3.model.RelatedArtifact) relatedArtifact, reference); + ((org.hl7.fhir.dstu3.model.RelatedArtifact) relatedArtifact) + .getResource() + .setReference(reference); } else if (relatedArtifact instanceof org.hl7.fhir.r4.model.RelatedArtifact) { - setRelatedArtifactReference((org.hl7.fhir.r4.model.RelatedArtifact) relatedArtifact, reference); + ((org.hl7.fhir.r4.model.RelatedArtifact) relatedArtifact).setResource(reference); } else if (relatedArtifact instanceof org.hl7.fhir.r5.model.RelatedArtifact) { - setRelatedArtifactReference((org.hl7.fhir.r5.model.RelatedArtifact) relatedArtifact, reference); + ((org.hl7.fhir.r5.model.RelatedArtifact) relatedArtifact).setResource(reference); } else { throw new UnprocessableEntityException("Must be a valid RelatedArtifact"); } } - ; - private static void setRelatedArtifactReference( - org.hl7.fhir.dstu3.model.RelatedArtifact relatedArtifact, String reference) { - relatedArtifact.getResource().setReference(reference); + default boolean hasRelatedArtifact() { + return !getRelatedArtifact().isEmpty(); } - private static void setRelatedArtifactReference( - org.hl7.fhir.r4.model.RelatedArtifact relatedArtifact, String reference) { - relatedArtifact.setResource(reference); + @SuppressWarnings("unchecked") + default List getRelatedArtifact() { + return resolvePathList(get(), "relatedArtifact").stream() + .map(r -> (T) r) + .collect(Collectors.toList()); } - private static void setRelatedArtifactReference( - org.hl7.fhir.r5.model.RelatedArtifact relatedArtifact, String reference) { - relatedArtifact.setResource(reference); + default void setRelatedArtifact(List relatedArtifacts) { + try { + getModelResolver().setValue(get(), "relatedArtifact", null); + getModelResolver().setValue(get(), "relatedArtifact", relatedArtifacts); + } catch (Exception e) { + // Do nothing + throw new IllegalAccessError( + "Encountered error trying to access field 'relatedArtifact': " + e.getMessage()); + } } - void setEffectivePeriod(ICompositeType effectivePeriod); - - boolean hasRelatedArtifact(); - - List getRelatedArtifact(); - List getRelatedArtifactsOfType(String codeString); default List getComponents() { - return this.getRelatedArtifactsOfType("composed-of"); + return getRelatedArtifactsOfType("composed-of"); } static boolean checkIfRelatedArtifactIsOwned(T relatedArtifact) { @@ -193,7 +298,7 @@ static boolean checkIfRelatedArt } default List combineComponentsAndDependencies() { - final String referenceSource = this.hasVersion() ? getUrl() + "|" + getVersion() : getUrl(); + final String referenceSource = hasVersion() ? getUrl() + "|" + getVersion() : getUrl(); return Stream.concat( getComponents().stream() .filter(ra -> ra != null) @@ -202,8 +307,6 @@ default List combineComponentsAndDependencies() { .collect(Collectors.toList()); } - void setRelatedArtifact(List relatedArtifacts); - default IBase accept(KnowledgeArtifactVisitor visitor, Repository repository, IBaseParameters operationParameters) { return visitor.visit(this, repository, operationParameters); } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/ResourceAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/ResourceAdapter.java index 0f13e232a..4774fb74b 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/ResourceAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/ResourceAdapter.java @@ -1,10 +1,14 @@ package org.opencds.cqf.fhir.utility.adapter; +import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; import org.hl7.fhir.exceptions.FHIRException; import org.hl7.fhir.instance.model.api.IBase; import org.hl7.fhir.instance.model.api.IBaseExtension; import org.hl7.fhir.instance.model.api.IBaseResource; +import org.hl7.fhir.instance.model.api.IPrimitiveType; +import org.opencds.cqf.cql.engine.model.ModelResolver; public interface ResourceAdapter extends Adapter { @@ -36,11 +40,76 @@ public interface ResourceAdapter extends Adapter { public > void addExtension(T extension); - public List> getExtension(); - - public > T getExtensionByUrl(String url); - - public List> getExtensionsByUrl(String url); - - public List getContained(); + public default List> getExtension() { + return getExtension(get()); + } + + public default IBaseExtension getExtensionByUrl(String url) { + return getExtensionByUrl(get(), url); + } + + public default List> getExtensionsByUrl(String url) { + return getExtensionsByUrl(get(), url); + } + + public default List getContained() { + return getContained(get()); + } + + public default boolean hasContained() { + return hasContained(get()); + } + + public ModelResolver getModelResolver(); + + @SuppressWarnings("unchecked") + public default > List getExtension(IBase base) { + return resolvePathList(base, "extension").stream().map(e -> (E) e).collect(Collectors.toList()); + } + + public default List> getExtensionsByUrl(IBase base, String url) { + return getExtension(base).stream().filter(e -> e.getUrl().equals(url)).collect(Collectors.toList()); + } + + public default IBaseExtension getExtensionByUrl(IBase base, String url) { + return getExtensionsByUrl(base, url).stream().findFirst().orElse(null); + } + + public default Boolean hasExtension(IBase base, String url) { + return getExtension(base).stream().anyMatch(e -> e.getUrl().equals(url)); + } + + public default List getContained(IBaseResource base) { + return resolvePathList(base, "contained", IBaseResource.class); + } + + public default Boolean hasContained(IBaseResource base) { + return !getContained(base).isEmpty(); + } + + @SuppressWarnings("unchecked") + public default List resolvePathList(IBase base, String path) { + var pathResult = getModelResolver().resolvePath(base, path); + return pathResult instanceof List ? (List) pathResult : new ArrayList<>(); + } + + @SuppressWarnings("unchecked") + public default List resolvePathList(IBase base, String path, Class clazz) { + return resolvePathList(base, path).stream().map(i -> (T) i).collect(Collectors.toList()); + } + + @SuppressWarnings("unchecked") + public default String resolvePathString(IBase base, String path) { + var result = (IPrimitiveType) resolvePath(base, path); + return result == null ? null : result.getValue(); + } + + public default IBase resolvePath(IBase base, String path) { + return (IBase) getModelResolver().resolvePath(base, path); + } + + @SuppressWarnings("unchecked") + public default T resolvePath(IBase base, String path, Class clazz) { + return (T) resolvePath(base, path); + } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/KnowledgeArtifactAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/KnowledgeArtifactAdapter.java index 32c8587b2..3f909c23e 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/KnowledgeArtifactAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/KnowledgeArtifactAdapter.java @@ -7,13 +7,11 @@ import java.util.stream.Collectors; import org.hl7.fhir.dstu3.model.DateTimeType; import org.hl7.fhir.dstu3.model.Enumerations.PublicationStatus; -import org.hl7.fhir.dstu3.model.Extension; import org.hl7.fhir.dstu3.model.MetadataResource; import org.hl7.fhir.dstu3.model.Period; import org.hl7.fhir.dstu3.model.RelatedArtifact; import org.hl7.fhir.dstu3.model.RelatedArtifact.RelatedArtifactType; import org.hl7.fhir.exceptions.FHIRException; -import org.hl7.fhir.instance.model.api.IBaseExtension; import org.hl7.fhir.instance.model.api.IBaseHasExtensions; import org.hl7.fhir.instance.model.api.ICompositeType; import org.hl7.fhir.instance.model.api.IDomainResource; @@ -30,82 +28,22 @@ public KnowledgeArtifactAdapter(IDomainResource resource) { throw new IllegalArgumentException( "resource passed as resource argument is not a MetadataResource resource"); } - this.adaptedResource = (MetadataResource) resource; + adaptedResource = (MetadataResource) resource; } public KnowledgeArtifactAdapter(MetadataResource resource) { super(resource); - this.adaptedResource = resource; + adaptedResource = resource; } @Override public MetadataResource get() { - return this.adaptedResource; + return adaptedResource; } @Override public MetadataResource copy() { - return this.get().copy(); - } - - @Override - public boolean hasUrl() { - return this.get().hasUrl(); - } - - @Override - public String getUrl() { - return this.get().getUrl(); - } - - @Override - public void setUrl(String url) { - this.get().setUrl(url); - } - - @Override - public void setVersion(String version) { - this.get().setVersion(version); - } - - @Override - public String getVersion() { - return this.get().getVersion(); - } - - @Override - public boolean hasVersion() { - return this.get().hasVersion(); - } - - @Override - public String getName() { - return this.get().getName(); - } - - @Override - public String getPurpose() { - return null; - } - - @Override - public void setName(String name) { - this.get().setName(name); - } - - @Override - public Date getApprovalDate() { - return null; - } - - @Override - public Date getDate() { - return this.get().getDate(); - } - - @Override - public void setDate(Date date) { - this.get().setDate(date); + return get().copy(); } @Override @@ -113,22 +51,7 @@ public void setDateElement(IPrimitiveType date) { if (date != null && !(date instanceof DateTimeType)) { throw new UnprocessableEntityException("Date must be " + DateTimeType.class.getName()); } - this.get().setDateElement((DateTimeType) date); - } - - @Override - public void setApprovalDate(Date approvalDate) { - // do nothing - } - - @Override - public Period getEffectivePeriod() { - return new Period(); - } - - @Override - public List getDependencies() { - return new ArrayList<>(); + org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter.super.setDateElement(date); } @Override @@ -136,18 +59,12 @@ public void setEffectivePeriod(ICompositeType effectivePeriod) { if (effectivePeriod != null && !(effectivePeriod instanceof Period)) { throw new UnprocessableEntityException("EffectivePeriod must be a valid " + Period.class.getName()); } - // does nothing - } - - @Override - public boolean hasRelatedArtifact() { - return false; + org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter.super.setEffectivePeriod(effectivePeriod); } - @SuppressWarnings("unchecked") @Override - public List getRelatedArtifact() { - return new ArrayList(); + public List getDependencies() { + return new ArrayList<>(); } @SuppressWarnings("unchecked") @@ -159,14 +76,15 @@ public List getRelatedArtifactsOfType(String codeString) { } catch (FHIRException e) { throw new UnprocessableEntityException("Invalid related artifact code"); } - return this.getRelatedArtifact().stream() + return getRelatedArtifact().stream() + .map(ra -> (RelatedArtifact) ra) .filter(ra -> ra.getType() == type) .collect(Collectors.toList()); } @Override - public void setRelatedArtifact(List relatedArtifacts) { - // does nothing + public String getStatus() { + return get().getStatus() == null ? null : get().getStatus().toCode(); } @Override @@ -177,21 +95,21 @@ public void setStatus(String statusCodeString) { } catch (FHIRException e) { throw new UnprocessableEntityException("Invalid status code"); } - this.get().setStatus(status); - } - - @Override - public String getStatus() { - return this.get().getStatus() == null ? null : this.get().getStatus().toCode(); - } - - @Override - public boolean getExperimental() { - return this.get().getExperimental(); - } - - @Override - public void setExtension(List> extensions) { - this.get().setExtension(extensions.stream().map(e -> (Extension) e).collect(Collectors.toList())); + get().setStatus(status); + } + + @Override + public void setRelatedArtifact(List relatedArtifacts) + throws UnprocessableEntityException { + org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter.super.setRelatedArtifact(relatedArtifacts.stream() + .map(ra -> { + try { + return (RelatedArtifact) ra; + } catch (ClassCastException e) { + throw new UnprocessableEntityException( + "All related artifacts must be of type " + RelatedArtifact.class.getName()); + } + }) + .collect(Collectors.toList())); } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/LibraryAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/LibraryAdapter.java index eb19dc898..2b1bec4b9 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/LibraryAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/LibraryAdapter.java @@ -2,155 +2,82 @@ import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; import java.util.ArrayList; -import java.util.Date; import java.util.List; import java.util.stream.Collectors; import org.hl7.fhir.dstu3.model.Attachment; import org.hl7.fhir.dstu3.model.CodeableConcept; import org.hl7.fhir.dstu3.model.Coding; import org.hl7.fhir.dstu3.model.DataRequirement; -import org.hl7.fhir.dstu3.model.DateTimeType; -import org.hl7.fhir.dstu3.model.Enumerations.PublicationStatus; -import org.hl7.fhir.dstu3.model.Extension; import org.hl7.fhir.dstu3.model.Library; -import org.hl7.fhir.dstu3.model.Period; import org.hl7.fhir.dstu3.model.RelatedArtifact; -import org.hl7.fhir.dstu3.model.RelatedArtifact.RelatedArtifactType; import org.hl7.fhir.dstu3.model.UsageContext; -import org.hl7.fhir.exceptions.FHIRException; -import org.hl7.fhir.instance.model.api.IBaseExtension; -import org.hl7.fhir.instance.model.api.IBaseHasExtensions; import org.hl7.fhir.instance.model.api.ICompositeType; import org.hl7.fhir.instance.model.api.IDomainResource; -import org.hl7.fhir.instance.model.api.IIdType; -import org.hl7.fhir.instance.model.api.IPrimitiveType; import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; -class LibraryAdapter extends ResourceAdapter implements org.opencds.cqf.fhir.utility.adapter.LibraryAdapter { - - private Library library; - +public class LibraryAdapter extends KnowledgeArtifactAdapter + implements org.opencds.cqf.fhir.utility.adapter.LibraryAdapter { public LibraryAdapter(IDomainResource library) { super(library); - if (!(library instanceof Library)) { throw new IllegalArgumentException("resource passed as library argument is not a Library resource"); } - - this.library = (Library) library; } public LibraryAdapter(Library library) { super(library); - this.library = library; } protected Library getLibrary() { - return this.library; + return (Library) resource; } @Override public Library get() { - return this.library; + return (Library) resource; } @Override public Library copy() { - return this.get().copy(); - } - - @Override - public IIdType getId() { - return this.getLibrary().getIdElement(); - } - - @Override - public void setId(IIdType id) { - this.getLibrary().setId(id); - } - - @Override - public String getName() { - return this.getLibrary().getName(); - } - - @Override - public String getPurpose() { - return this.getLibrary().getPurpose(); - } - - @Override - public void setName(String name) { - this.getLibrary().setName(name); - } - - @Override - public String getUrl() { - return this.getLibrary().getUrl(); - } - - @Override - public boolean hasUrl() { - return this.getLibrary().hasUrl(); - } - - @Override - public void setUrl(String url) { - this.getLibrary().setUrl(url); - } - - @Override - public boolean hasVersion() { - return this.getLibrary().hasVersion(); - } - - @Override - public String getVersion() { - return this.getLibrary().getVersion(); - } - - @Override - public void setVersion(String version) { - this.getLibrary().setVersion(version); + return get().copy(); } @Override public boolean hasContent() { - return this.getLibrary().hasContent(); + return getLibrary().hasContent(); } @Override public List getContent() { - return this.getLibrary().getContent().stream().collect(Collectors.toList()); + return getLibrary().getContent().stream().collect(Collectors.toList()); } @Override public void setContent(List attachments) { List castAttachments = attachments.stream().map(x -> (Attachment) x).collect(Collectors.toList()); - this.getLibrary().setContent(castAttachments); + getLibrary().setContent(castAttachments); } @Override public Attachment addContent() { - return this.getLibrary().addContent(); + return getLibrary().addContent(); } @Override public List getDependencies() { - List references = new ArrayList<>(); + List references = new ArrayList(); final String referenceSource = - this.hasVersion() ? this.getUrl() + "|" + this.getLibrary().getVersion() : this.getUrl(); + hasVersion() ? getUrl() + "|" + getLibrary().getVersion() : getUrl(); // relatedArtifact[].resource - this.getRelatedArtifact().stream() + getRelatedArtifact().stream() + .map(ra -> (RelatedArtifact) ra) .filter(ra -> ra.hasResource()) .map(ra -> DependencyInfo.convertRelatedArtifact(ra, referenceSource)) .forEach(ra -> references.add(ra)); - - // dataRequirement - this.getLibrary().getDataRequirement().stream().forEach(dr -> { + getLibrary().getDataRequirement().stream().forEach(dr -> { dr.getProfile().stream() .filter(profile -> profile.hasValue()) .forEach(profile -> references.add(new DependencyInfo( @@ -169,125 +96,22 @@ public List getDependencies() { return references; } - @Override - public Date getApprovalDate() { - return this.getLibrary().getApprovalDate(); - } - - @Override - public void setApprovalDate(Date approvalDate) { - this.getLibrary().setApprovalDate(approvalDate); - } - - @Override - public Date getDate() { - return this.getLibrary().getDate(); - } - - @Override - public void setDate(Date date) { - this.getLibrary().setDate(date); - } - - @Override - public void setDateElement(IPrimitiveType date) { - if (date != null && !(date instanceof DateTimeType)) { - throw new UnprocessableEntityException("Date must be " + DateTimeType.class.getName()); - } - this.getLibrary().setDateElement((DateTimeType) date); - } - - @Override - public Period getEffectivePeriod() { - return this.getLibrary().getEffectivePeriod(); - } - - @Override - public boolean hasRelatedArtifact() { - return this.getLibrary().hasRelatedArtifact(); - } - @SuppressWarnings("unchecked") @Override - public List getRelatedArtifact() { - return this.getLibrary().getRelatedArtifact(); - } - - @SuppressWarnings("unchecked") - @Override - public List getRelatedArtifactsOfType(String codeString) { - RelatedArtifactType type; - try { - type = RelatedArtifactType.fromCode(codeString); - } catch (FHIRException e) { - throw new UnprocessableEntityException("Invalid related artifact code"); - } - return this.getRelatedArtifact().stream() - .filter(ra -> ra.getType() == type) - .collect(Collectors.toList()); - } - - @Override - public void setRelatedArtifact(List relatedArtifacts) - throws UnprocessableEntityException { - this.getLibrary() - .setRelatedArtifact(relatedArtifacts.stream() - .map(ra -> { - try { - return (RelatedArtifact) ra; - } catch (ClassCastException e) { - throw new UnprocessableEntityException( - "All related artifacts must be of type " + RelatedArtifact.class.getName()); - } - }) - .collect(Collectors.toList())); - } - - @Override - public void setEffectivePeriod(ICompositeType effectivePeriod) { - if (effectivePeriod != null && !(effectivePeriod instanceof Period)) { - throw new UnprocessableEntityException("EffectivePeriod must be " + Period.class.getName()); - } - this.getLibrary().setEffectivePeriod((Period) effectivePeriod); - } - - @Override - public void setStatus(String statusCodeString) { - PublicationStatus status; - try { - status = PublicationStatus.fromCode(statusCodeString); - } catch (FHIRException e) { - throw new UnprocessableEntityException("Invalid status code"); - } - this.getLibrary().setStatus(status); - } - - @Override - public String getStatus() { - return this.getLibrary().getStatus() == null - ? null - : this.getLibrary().getStatus().toCode(); - } - - @Override - public boolean getExperimental() { - return this.getLibrary().getExperimental(); - } - - @Override - public void setExtension(List> extensions) { - this.get().setExtension(extensions.stream().map(e -> (Extension) e).collect(Collectors.toList())); + public List getComponents() { + return getRelatedArtifactsOfType("composed-of"); } @Override public ICompositeType getType() { - return library.getType(); + return getLibrary().getType(); } @Override public LibraryAdapter setType(String type) { if (LIBRARY_TYPES.contains(type)) { - library.setType(new CodeableConcept(new Coding("http://hl7.org/fhir/ValueSet/library-type", type, ""))); + getLibrary() + .setType(new CodeableConcept(new Coding("http://hl7.org/fhir/ValueSet/library-type", type, ""))); } else { throw new UnprocessableEntityException("Invalid type: {}", type); } @@ -296,24 +120,26 @@ public LibraryAdapter setType(String type) { @Override public List getDataRequirement() { - return library.getDataRequirement(); + return getLibrary().getDataRequirement(); } @Override public LibraryAdapter addDataRequirement(ICompositeType dataRequirement) { - library.addDataRequirement((DataRequirement) dataRequirement); + getLibrary().addDataRequirement((DataRequirement) dataRequirement); return this; } @Override public LibraryAdapter setDataRequirements(List dataRequirements) { - library.setDataRequirement( - dataRequirements.stream().map(dr -> (DataRequirement) dr).collect(Collectors.toList())); + getLibrary() + .setDataRequirement(dataRequirements.stream() + .map(dr -> (DataRequirement) dr) + .collect(Collectors.toList())); return this; } @Override public List getUseContext() { - return library.getUseContext(); + return getLibrary().getUseContext(); } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/PlanDefinitionAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/PlanDefinitionAdapter.java index 6ec5e3d4e..eac86d410 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/PlanDefinitionAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/PlanDefinitionAdapter.java @@ -1,118 +1,51 @@ package org.opencds.cqf.fhir.utility.adapter.dstu3; -import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; import java.util.ArrayList; -import java.util.Date; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; import org.hl7.fhir.dstu3.model.DataRequirement.DataRequirementCodeFilterComponent; -import org.hl7.fhir.dstu3.model.DateTimeType; -import org.hl7.fhir.dstu3.model.Enumerations.PublicationStatus; -import org.hl7.fhir.dstu3.model.Extension; -import org.hl7.fhir.dstu3.model.Period; import org.hl7.fhir.dstu3.model.PlanDefinition; import org.hl7.fhir.dstu3.model.Reference; -import org.hl7.fhir.dstu3.model.RelatedArtifact; -import org.hl7.fhir.dstu3.model.RelatedArtifact.RelatedArtifactType; import org.hl7.fhir.dstu3.model.StringType; import org.hl7.fhir.dstu3.model.UriType; -import org.hl7.fhir.exceptions.FHIRException; -import org.hl7.fhir.instance.model.api.IBaseExtension; -import org.hl7.fhir.instance.model.api.IBaseHasExtensions; -import org.hl7.fhir.instance.model.api.ICompositeType; import org.hl7.fhir.instance.model.api.IDomainResource; -import org.hl7.fhir.instance.model.api.IPrimitiveType; import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; -import org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter; - -class PlanDefinitionAdapter extends ResourceAdapter implements KnowledgeArtifactAdapter { - - private PlanDefinition planDefinition; +class PlanDefinitionAdapter extends KnowledgeArtifactAdapter { public PlanDefinitionAdapter(IDomainResource planDefinition) { super(planDefinition); - if (!(planDefinition instanceof PlanDefinition)) { throw new IllegalArgumentException( "resource passed as planDefinition argument is not a PlanDefinition resource"); } - - this.planDefinition = (PlanDefinition) planDefinition; } public PlanDefinitionAdapter(PlanDefinition planDefinition) { super(planDefinition); - this.planDefinition = planDefinition; } protected PlanDefinition getPlanDefinition() { - return this.planDefinition; + return (PlanDefinition) resource; } @Override public PlanDefinition get() { - return this.planDefinition; + return getPlanDefinition(); } @Override public PlanDefinition copy() { - return this.get().copy(); - } - - @Override - public String getName() { - return this.getPlanDefinition().getName(); - } - - @Override - public String getPurpose() { - return this.getPlanDefinition().getPurpose(); - } - - @Override - public void setName(String name) { - this.getPlanDefinition().setName(name); - } - - @Override - public String getUrl() { - return this.getPlanDefinition().getUrl(); - } - - @Override - public boolean hasUrl() { - return this.getPlanDefinition().hasUrl(); - } - - @Override - public void setUrl(String url) { - this.getPlanDefinition().setUrl(url); - } - - @Override - public String getVersion() { - return this.getPlanDefinition().getVersion(); - } - - @Override - public boolean hasVersion() { - return this.getPlanDefinition().hasVersion(); - } - - @Override - public void setVersion(String version) { - this.getPlanDefinition().setVersion(version); + return get().copy(); } @Override public List getDependencies() { List references = new ArrayList<>(); - final String referenceSource = this.hasVersion() - ? this.getPlanDefinition().getUrl() + "|" - + this.getPlanDefinition().getVersion() - : this.getPlanDefinition().getUrl(); + final String referenceSource = hasVersion() + ? getPlanDefinition().getUrl() + "|" + getPlanDefinition().getVersion() + : getPlanDefinition().getUrl(); /* https://build.fhir.org/ig/HL7/crmi-ig/distribution.html#package-and-data-requirements relatedArtifact[].resource @@ -130,12 +63,12 @@ public List getDependencies() { */ // relatedArtifact[].resource - references.addAll(this.getRelatedArtifact().stream() + references.addAll(getRelatedArtifact().stream() .map(ra -> DependencyInfo.convertRelatedArtifact(ra, referenceSource)) .collect(Collectors.toList())); // library[] - List libraries = this.planDefinition.getLibrary(); + List libraries = getPlanDefinition().getLibrary(); for (Reference ref : libraries) { // TODO: Account for reference.identifier? DependencyInfo dependency = new DependencyInfo( @@ -146,8 +79,8 @@ public List getDependencies() { references.add(dependency); } // action[] - this.planDefinition.getAction().forEach(action -> getDependenciesOfAction(action, references, referenceSource)); - this.getPlanDefinition().getExtension().stream() + getPlanDefinition().getAction().forEach(action -> getDependenciesOfAction(action, references, referenceSource)); + getPlanDefinition().getExtension().stream() .filter(ext -> ext.getUrl().contains("cpg-partOf")) .filter(ext -> ext.hasValue()) .findAny() @@ -215,127 +148,17 @@ private DependencyInfo dependencyFromDataRequirementCodeFilter(DataRequirementCo var vs = cf.getValueSet(); if (vs instanceof StringType) { return new DependencyInfo( - this.planDefinition.getUrl(), + getPlanDefinition().getUrl(), ((StringType) vs).getValue(), vs.getExtension(), (reference) -> ((StringType) vs).setValue(reference)); } else if (vs instanceof Reference) { return new DependencyInfo( - this.planDefinition.getUrl(), + getPlanDefinition().getUrl(), ((Reference) vs).getReference(), vs.getExtension(), (reference) -> ((Reference) vs).setReference(reference)); } return null; } - - @Override - public Date getApprovalDate() { - return this.getPlanDefinition().getApprovalDate(); - } - - @Override - public void setApprovalDate(Date approvalDate) { - this.getPlanDefinition().setApprovalDate(approvalDate); - } - - @Override - public Date getDate() { - return this.getPlanDefinition().getDate(); - } - - @Override - public void setDate(Date date) { - this.getPlanDefinition().setDate(date); - } - - @Override - public void setDateElement(IPrimitiveType date) { - if (date != null && !(date instanceof DateTimeType)) { - throw new UnprocessableEntityException("Date must be " + DateTimeType.class.getName()); - } - this.getPlanDefinition().setDateElement((DateTimeType) date); - } - - @Override - public Period getEffectivePeriod() { - return this.getPlanDefinition().getEffectivePeriod(); - } - - @Override - public boolean hasRelatedArtifact() { - return this.getPlanDefinition().hasRelatedArtifact(); - } - - @SuppressWarnings("unchecked") - @Override - public List getRelatedArtifact() { - return this.getPlanDefinition().getRelatedArtifact(); - } - - @SuppressWarnings("unchecked") - @Override - public List getRelatedArtifactsOfType(String codeString) { - RelatedArtifactType type; - try { - type = RelatedArtifactType.fromCode(codeString); - } catch (FHIRException e) { - throw new UnprocessableEntityException("Invalid related artifact code"); - } - return this.getRelatedArtifact().stream() - .filter(ra -> ra.getType() == type) - .collect(Collectors.toList()); - } - - @Override - public void setRelatedArtifact(List relatedArtifacts) - throws UnprocessableEntityException { - this.getPlanDefinition() - .setRelatedArtifact(relatedArtifacts.stream() - .map(ra -> { - try { - return (RelatedArtifact) ra; - } catch (ClassCastException e) { - throw new UnprocessableEntityException( - "All related artifacts must be of type " + RelatedArtifact.class.getName()); - } - }) - .collect(Collectors.toList())); - } - - @Override - public void setEffectivePeriod(ICompositeType effectivePeriod) { - if (effectivePeriod != null && !(effectivePeriod instanceof Period)) { - throw new UnprocessableEntityException("EffectivePeriod must be " + Period.class.getName()); - } - this.getPlanDefinition().setEffectivePeriod((Period) effectivePeriod); - } - - @Override - public void setStatus(String statusCodeString) { - PublicationStatus status; - try { - status = PublicationStatus.fromCode(statusCodeString); - } catch (FHIRException e) { - throw new UnprocessableEntityException("Invalid status code"); - } - this.getPlanDefinition().setStatus(status); - } - - @Override - public String getStatus() { - return this.getPlanDefinition().getStatus() == null - ? null - : this.getPlanDefinition().getStatus().toCode(); - } - - @Override - public boolean getExperimental() { - return this.getPlanDefinition().getExperimental(); - } - - @Override - public void setExtension(List> extensions) { - this.get().setExtension(extensions.stream().map(e -> (Extension) e).collect(Collectors.toList())); - } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/QuestionnaireAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/QuestionnaireAdapter.java index 4d54f847f..2b48f5990 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/QuestionnaireAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/QuestionnaireAdapter.java @@ -11,8 +11,6 @@ public class QuestionnaireAdapter extends KnowledgeArtifactAdapter { - private Questionnaire questionnaire; - public QuestionnaireAdapter(IDomainResource questionnaire) { super(questionnaire); @@ -20,26 +18,32 @@ public QuestionnaireAdapter(IDomainResource questionnaire) { throw new IllegalArgumentException( "resource passed as questionnaire argument is not a Questionnaire resource"); } - - this.questionnaire = (Questionnaire) questionnaire; } public QuestionnaireAdapter(Questionnaire questionnaire) { super(questionnaire); - this.questionnaire = questionnaire; } protected Questionnaire getQuestionnaire() { - return this.questionnaire; + return (Questionnaire) resource; + } + + @Override + public Questionnaire get() { + return (Questionnaire) resource; + } + + @Override + public Questionnaire copy() { + return get().copy(); } @Override public List getDependencies() { List references = new ArrayList<>(); - final String referenceSource = this.getQuestionnaire().hasVersion() - ? this.getQuestionnaire().getUrl() + "|" - + this.getQuestionnaire().getVersion() - : this.getQuestionnaire().getUrl(); + final String referenceSource = getQuestionnaire().hasVersion() + ? getQuestionnaire().getUrl() + "|" + getQuestionnaire().getVersion() + : getQuestionnaire().getUrl(); /* derivedFrom item[]..definition // NOTE: This is not a simple canonical, it will have a fragment to identify the specific element @@ -61,7 +65,7 @@ public List getDependencies() { item[]..extension[sdc-questionnaire-subQuestionnaire] */ - var libraryExtensions = questionnaire.getExtensionsByUrl(Constants.CQF_LIBRARY); + var libraryExtensions = getQuestionnaire().getExtensionsByUrl(Constants.CQF_LIBRARY); for (var libraryExt : libraryExtensions) { DependencyInfo dependency = new DependencyInfo( referenceSource, diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapter.java index 6047ea620..01ae388ed 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapter.java @@ -3,7 +3,6 @@ import static java.util.Optional.ofNullable; import ca.uhn.fhir.context.FhirVersionEnum; -import java.util.ArrayList; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; @@ -15,10 +14,12 @@ import org.hl7.fhir.instance.model.api.IBase; import org.hl7.fhir.instance.model.api.IBaseExtension; import org.hl7.fhir.instance.model.api.IBaseResource; +import org.opencds.cqf.fhir.utility.adapter.BaseResourceAdapter; -class ResourceAdapter implements org.opencds.cqf.fhir.utility.adapter.ResourceAdapter { +class ResourceAdapter extends BaseResourceAdapter { public ResourceAdapter(IBaseResource resource) { + super(resource); if (resource == null) { throw new IllegalArgumentException("resource can not be null"); } @@ -26,14 +27,10 @@ public ResourceAdapter(IBaseResource resource) { if (!resource.getStructureFhirVersionEnum().equals(FhirVersionEnum.DSTU3)) { throw new IllegalArgumentException("resource is incorrect fhir version for this adapter"); } - - this.resource = (Resource) resource; } - private Resource resource; - protected Resource getResource() { - return this.resource; + return (Resource) resource; } protected boolean isDomainResource() { @@ -45,22 +42,22 @@ protected Optional getDomainResource() { } public IBaseResource get() { - return this.resource; + return resource; } @Override public IBase setProperty(String name, IBase value) throws FHIRException { - return this.getResource().setProperty(name, (Base) value); + return getResource().setProperty(name, (Base) value); } @Override public IBase addChild(String name) throws FHIRException { - return this.getResource().addChild(name); + return getResource().addChild(name); } @Override public IBase getSingleProperty(String name) throws FHIRException { - IBase[] values = this.getProperty(name, true); + IBase[] values = getProperty(name, true); if (values == null || values.length == 0) { return null; @@ -75,42 +72,42 @@ public IBase getSingleProperty(String name) throws FHIRException { @Override public IBase[] getProperty(String name) throws FHIRException { - return this.getProperty(name, true); + return getProperty(name, true); } @Override public IBase[] getProperty(String name, boolean checkValid) throws FHIRException { - return this.getResource().getProperty(name.hashCode(), name, checkValid); + return getResource().getProperty(name.hashCode(), name, checkValid); } @Override public IBase makeProperty(String name) throws FHIRException { - return this.getResource().makeProperty(name.hashCode(), name); + return getResource().makeProperty(name.hashCode(), name); } @Override public String[] getTypesForProperty(String name) throws FHIRException { - return this.getResource().getTypesForProperty(name.hashCode(), name); + return getResource().getTypesForProperty(name.hashCode(), name); } @Override public IBaseResource copy() { - return this.getResource().copy(); + return getResource().copy(); } @Override public void copyValues(IBaseResource dst) { - this.getResource().copyValues((Resource) dst); + getResource().copyValues((Resource) dst); } @Override public boolean equalsDeep(IBase other) { - return this.getResource().equalsDeep((Base) other); + return getResource().equalsDeep((Base) other); } @Override public boolean equalsShallow(IBase other) { - return this.getResource().equalsShallow((Base) other); + return getResource().equalsShallow((Base) other); } @Override @@ -124,34 +121,4 @@ public void setExtension(List> extensions) { public > void addExtension(T extension) { getDomainResource().ifPresent(r -> r.addExtension((Extension) extension)); } - - @Override - public List getExtension() { - return isDomainResource() ? getDomainResource().get().getExtension() : new ArrayList<>(); - } - - @SuppressWarnings("unchecked") - @Override - public Extension getExtensionByUrl(String url) { - return isDomainResource() - ? getDomainResource().get().getExtension().stream() - .filter(e -> e.getUrl().equals(url)) - .findFirst() - .orElse(null) - : null; - } - - @Override - public List getExtensionsByUrl(String url) { - return isDomainResource() - ? getDomainResource().get().getExtension().stream() - .filter(e -> e.getUrl().equals(url)) - .collect(Collectors.toList()) - : new ArrayList<>(); - } - - @Override - public List getContained() { - return isDomainResource() ? getDomainResource().get().getContained() : new ArrayList<>(); - } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/StructureDefinitionAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/StructureDefinitionAdapter.java index 0f04e9a61..ce9c0b9f1 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/StructureDefinitionAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/StructureDefinitionAdapter.java @@ -1,32 +1,15 @@ package org.opencds.cqf.fhir.utility.adapter.dstu3; -import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; import java.util.ArrayList; -import java.util.Date; import java.util.List; -import java.util.stream.Collectors; -import org.hl7.fhir.dstu3.model.DateTimeType; -import org.hl7.fhir.dstu3.model.Enumerations.PublicationStatus; -import org.hl7.fhir.dstu3.model.Extension; -import org.hl7.fhir.dstu3.model.Period; -import org.hl7.fhir.dstu3.model.RelatedArtifact; -import org.hl7.fhir.dstu3.model.RelatedArtifact.RelatedArtifactType; +import org.hl7.fhir.dstu3.model.StringType; import org.hl7.fhir.dstu3.model.StructureDefinition; -import org.hl7.fhir.dstu3.model.UriType; -import org.hl7.fhir.exceptions.FHIRException; -import org.hl7.fhir.instance.model.api.IBaseExtension; -import org.hl7.fhir.instance.model.api.IBaseHasExtensions; -import org.hl7.fhir.instance.model.api.ICompositeType; import org.hl7.fhir.instance.model.api.IDomainResource; -import org.hl7.fhir.instance.model.api.IPrimitiveType; import org.opencds.cqf.fhir.utility.Constants; import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; -import org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter; -public class StructureDefinitionAdapter extends ResourceAdapter implements KnowledgeArtifactAdapter { - - private StructureDefinition structureDefinition; +public class StructureDefinitionAdapter extends KnowledgeArtifactAdapter { public StructureDefinitionAdapter(IDomainResource structureDefinition) { super(structureDefinition); @@ -34,25 +17,23 @@ public StructureDefinitionAdapter(IDomainResource structureDefinition) { throw new IllegalArgumentException( "resource passed as planDefinition argument is not a StructureDefinition resource"); } - this.structureDefinition = (StructureDefinition) structureDefinition; } public StructureDefinitionAdapter(StructureDefinition structureDefinition) { super(structureDefinition); - this.structureDefinition = structureDefinition; } protected StructureDefinition getStructureDefinition() { - return this.structureDefinition; + return (StructureDefinition) resource; } @Override public List getDependencies() { List references = new ArrayList<>(); - final String referenceSource = this.getStructureDefinition().hasVersion() - ? this.getStructureDefinition().getUrl() + "|" - + this.getStructureDefinition().getVersion() - : this.getStructureDefinition().getUrl(); + final String referenceSource = getStructureDefinition().hasVersion() + ? getStructureDefinition().getUrl() + "|" + + getStructureDefinition().getVersion() + : getStructureDefinition().getUrl(); /* extension[].url modifierExtension[].url @@ -68,13 +49,13 @@ public List getDependencies() { extension[cpg-featureExpression].reference */ - var libraryExtensions = structureDefinition.getExtensionsByUrl(Constants.CQF_LIBRARY); + var libraryExtensions = getStructureDefinition().getExtensionsByUrl(Constants.CQF_LIBRARY); for (var libraryExt : libraryExtensions) { DependencyInfo dependency = new DependencyInfo( referenceSource, - ((UriType) libraryExt.getValue()).asStringValue(), + ((StringType) libraryExt.getValue()).asStringValue(), libraryExt.getExtension(), - (reference) -> libraryExt.setValue(new UriType(reference))); + (reference) -> libraryExt.setValue(new StringType(reference))); references.add(dependency); } @@ -83,152 +64,11 @@ public List getDependencies() { @Override public StructureDefinition get() { - return structureDefinition; + return getStructureDefinition(); } @Override public StructureDefinition copy() { return get().copy(); } - - @Override - public String getUrl() { - return get().getUrl(); - } - - @Override - public boolean hasUrl() { - return get().hasUrl(); - } - - @Override - public void setUrl(String url) { - get().setUrl(url); - } - - @Override - public void setVersion(String version) { - get().setVersion(version); - } - - @Override - public String getVersion() { - return get().getVersion(); - } - - @Override - public boolean hasVersion() { - return get().hasVersion(); - } - - @Override - public String getName() { - return get().getName(); - } - - @Override - public void setName(String name) { - get().setName(name); - } - - @Override - public Date getApprovalDate() { - return null; - } - - @Override - public Date getDate() { - return get().getDate(); - } - - @Override - public void setDate(Date approvalDate) { - get().setDate(approvalDate); - } - - @Override - public void setDateElement(IPrimitiveType date) { - if (date != null && !(date instanceof DateTimeType)) { - throw new UnprocessableEntityException("Date must be " + DateTimeType.class.getName()); - } - get().setDateElement((DateTimeType) date); - } - - @Override - public void setApprovalDate(Date approvalDate) { - // do nothing - } - - @Override - public Period getEffectivePeriod() { - return null; - } - - @Override - public String getPurpose() { - return get().getPurpose(); - } - - @Override - public void setEffectivePeriod(ICompositeType effectivePeriod) { - if (effectivePeriod != null && !(effectivePeriod instanceof Period)) { - throw new UnprocessableEntityException("EffectivePeriod must be a valid " + Period.class.getName()); - } - // do nothing - } - - @Override - public boolean hasRelatedArtifact() { - return false; - } - - @SuppressWarnings("unchecked") - @Override - public List getRelatedArtifact() { - return new ArrayList<>(); - } - - @SuppressWarnings("unchecked") - @Override - public List getRelatedArtifactsOfType(String codeString) { - RelatedArtifactType type; - try { - type = RelatedArtifactType.fromCode(codeString); - } catch (FHIRException e) { - throw new UnprocessableEntityException("Invalid related artifact code"); - } - return getRelatedArtifact().stream().filter(ra -> ra.getType() == type).collect(Collectors.toList()); - } - - @Override - public void setRelatedArtifact(List relatedArtifacts) - throws UnprocessableEntityException { - // do nothing - } - - @Override - public void setStatus(String statusCodeString) { - PublicationStatus status; - try { - status = PublicationStatus.fromCode(statusCodeString); - } catch (FHIRException e) { - throw new UnprocessableEntityException("Invalid status code"); - } - get().setStatus(status); - } - - @Override - public String getStatus() { - return get().getStatus() == null ? null : get().getStatus().toCode(); - } - - @Override - public boolean getExperimental() { - return get().getExperimental(); - } - - @Override - public void setExtension(List> extensions) { - get().setExtension(extensions.stream().map(e -> (Extension) e).collect(Collectors.toList())); - } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ValueSetAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ValueSetAdapter.java index fc6763e26..333db035d 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ValueSetAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ValueSetAdapter.java @@ -2,8 +2,6 @@ import static org.opencds.cqf.fhir.utility.ValueSets.getCodesInCompose; -import ca.uhn.fhir.context.FhirContext; -import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; import java.time.Instant; import java.util.ArrayList; import java.util.Collection; @@ -12,112 +10,47 @@ import java.util.stream.Collectors; import java.util.stream.Stream; import org.hl7.fhir.dstu3.model.BooleanType; -import org.hl7.fhir.dstu3.model.DateTimeType; -import org.hl7.fhir.dstu3.model.Enumerations.PublicationStatus; -import org.hl7.fhir.dstu3.model.Period; -import org.hl7.fhir.dstu3.model.RelatedArtifact; -import org.hl7.fhir.dstu3.model.RelatedArtifact.RelatedArtifactType; import org.hl7.fhir.dstu3.model.ValueSet; import org.hl7.fhir.dstu3.model.ValueSet.ValueSetExpansionComponent; -import org.hl7.fhir.exceptions.FHIRException; import org.hl7.fhir.instance.model.api.IBaseBackboneElement; -import org.hl7.fhir.instance.model.api.IBaseHasExtensions; -import org.hl7.fhir.instance.model.api.ICompositeType; import org.hl7.fhir.instance.model.api.IDomainResource; -import org.hl7.fhir.instance.model.api.IIdType; -import org.hl7.fhir.instance.model.api.IPrimitiveType; import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; -class ValueSetAdapter extends ResourceAdapter implements org.opencds.cqf.fhir.utility.adapter.ValueSetAdapter { - - private ValueSet valueSet; +public class ValueSetAdapter extends KnowledgeArtifactAdapter + implements org.opencds.cqf.fhir.utility.adapter.ValueSetAdapter { public ValueSetAdapter(IDomainResource valueSet) { super(valueSet); - if (!(valueSet instanceof ValueSet)) { throw new IllegalArgumentException("resource passed as valueSet argument is not a ValueSet resource"); } - - this.valueSet = (ValueSet) valueSet; } public ValueSetAdapter(ValueSet valueSet) { super(valueSet); - this.valueSet = valueSet; } protected ValueSet getValueSet() { - return this.valueSet; + return (ValueSet) resource; } @Override public ValueSet get() { - return this.valueSet; + return (ValueSet) resource; } @Override public ValueSet copy() { - return this.get().copy(); - } - - @Override - public void setId(IIdType id) { - this.getValueSet().setId(id); - } - - @Override - public String getName() { - return this.getValueSet().getName(); - } - - @Override - public String getPurpose() { - return this.getValueSet().getPurpose(); - } - - @Override - public void setName(String name) { - this.getValueSet().setName(name); - } - - @Override - public String getUrl() { - return this.getValueSet().getUrl(); - } - - @Override - public boolean hasUrl() { - return this.getValueSet().hasUrl(); - } - - @Override - public void setUrl(String url) { - this.getValueSet().setUrl(url); - } - - @Override - public String getVersion() { - return this.getValueSet().getVersion(); - } - - @Override - public boolean hasVersion() { - return this.getValueSet().hasVersion(); - } - - @Override - public void setVersion(String version) { - this.getValueSet().setVersion(version); + return get().copy(); } @Override public List getDependencies() { List references = new ArrayList<>(); - final String referenceSource = this.getValueSet().hasVersion() - ? this.getValueSet().getUrl() + "|" + this.getValueSet().getVersion() - : this.getValueSet().getUrl(); + final String referenceSource = getValueSet().hasVersion() + ? getValueSet().getUrl() + "|" + getValueSet().getVersion() + : getValueSet().getUrl(); /* compose.include[].valueSet @@ -126,16 +59,16 @@ public List getDependencies() { compose.exclude[].system */ Stream.concat( - this.valueSet.getCompose().getInclude().stream(), - this.valueSet.getCompose().getExclude().stream()) + getValueSet().getCompose().getInclude().stream(), + getValueSet().getCompose().getExclude().stream()) .forEach(component -> { if (component.hasValueSet()) { - component.getValueSet().forEach(uri -> { + component.getValueSet().forEach(ct -> { references.add(new DependencyInfo( referenceSource, - uri.getValue(), - uri.getExtension(), - (reference) -> uri.setValue(reference))); + ct.getValue(), + ct.getExtension(), + (reference) -> ct.setValue(reference))); }); } if (component.hasSystem()) { @@ -151,138 +84,28 @@ public List getDependencies() { return references; } - @Override - public Date getApprovalDate() { - return null; - } - - @Override - public void setApprovalDate(Date date) { - // do nothing; - } - - @Override - public Date getDate() { - return this.getValueSet().getDate(); - } - - @Override - public void setDate(Date date) { - this.getValueSet().setDate(date); - } - - @Override - public void setDateElement(IPrimitiveType date) { - if (date != null && !(date instanceof DateTimeType)) { - throw new UnprocessableEntityException("Date must be " + DateTimeType.class.getName()); - } - this.getValueSet().setDateElement((DateTimeType) date); - } - - @Override - public Period getEffectivePeriod() { - return new Period(); - } - - @Override - public boolean hasRelatedArtifact() { - return false; - } - - @SuppressWarnings("unchecked") - @Override - public List getRelatedArtifact() { - return new ArrayList<>(); - } - - @SuppressWarnings("unchecked") - @Override - public List getComponents() { - return this.getRelatedArtifactsOfType("composed-of"); - } - - @SuppressWarnings("unchecked") - @Override - public List getRelatedArtifactsOfType(String codeString) { - RelatedArtifactType type; - try { - type = RelatedArtifactType.fromCode(codeString); - } catch (FHIRException e) { - throw new UnprocessableEntityException("Invalid related artifact code"); - } - return this.getRelatedArtifact().stream() - .filter(ra -> ra.getType() == type) - .collect(Collectors.toList()); - } - - @Override - public void setRelatedArtifact(List relatedArtifacts) - throws UnprocessableEntityException { - relatedArtifacts.stream().forEach(ra -> { - if (ra != null && !(ra instanceof RelatedArtifact)) { - throw new UnprocessableEntityException( - "All related artifacts must be of type " + RelatedArtifact.class.getName()); - } - }); - // do nothing - } - - @Override - public void setEffectivePeriod(ICompositeType effectivePeriod) { - if (effectivePeriod != null && !(effectivePeriod instanceof Period)) { - throw new UnprocessableEntityException("EffectivePeriod must be a valid " + Period.class.getName()); - } - // do nothing - } - - @Override - public void setStatus(String statusCodeString) { - PublicationStatus status; - try { - status = PublicationStatus.fromCode(statusCodeString); - } catch (FHIRException e) { - throw new UnprocessableEntityException("Invalid status code"); - } - this.getValueSet().setStatus(status); - } - - @Override - public String getStatus() { - return this.getValueSet().getStatus() == null - ? null - : this.getValueSet().getStatus().toCode(); - } - - @Override - public boolean getExperimental() { - return this.getValueSet().getExperimental(); - } - - // @Override - // public void setExtension(List> extensions) { - // this.get().setExtension(extensions.stream().map(e -> (Extension) e).collect(Collectors.toList())); - // } - @Override public void setExpansion(T expansion) { - valueSet.setExpansion((ValueSetExpansionComponent) expansion); + getValueSet().setExpansion((ValueSetExpansionComponent) expansion); } @SuppressWarnings("unchecked") @Override public ValueSetExpansionComponent getExpansion() { - return valueSet.getExpansion(); + return getValueSet().getExpansion(); } @SuppressWarnings("unchecked") @Override public ValueSetExpansionComponent newExpansion() { - return new ValueSetExpansionComponent().setTimestamp(Date.from(Instant.now())); + var expansion = new ValueSet.ValueSetExpansionComponent().setTimestamp(Date.from(Instant.now())); + expansion.getContains(); + return expansion; } @Override public List getValueSetIncludes() { - return valueSet.getCompose().getInclude().stream() + return getValueSet().getCompose().getInclude().stream() .map(i -> i.getValueSet()) .flatMap(Collection::stream) .map(c -> c.asStringValue()) @@ -292,23 +115,24 @@ public List getValueSetIncludes() { @Override public boolean hasSimpleCompose() { - return valueSet.hasCompose() - && !valueSet.getCompose().hasExclude() - && valueSet.getCompose().getInclude().stream() + return getValueSet().hasCompose() + && !getValueSet().getCompose().hasExclude() + && getValueSet().getCompose().getInclude().stream() .noneMatch( csc -> csc.hasFilter() || csc.hasValueSet() || !csc.hasSystem() || !csc.hasConcept()); } @Override public boolean hasGroupingCompose() { - return valueSet.hasCompose() - && !valueSet.getCompose().hasExclude() - && valueSet.getCompose().getInclude().stream().noneMatch(csc -> !csc.hasValueSet() || csc.hasFilter()); + return getValueSet().hasCompose() + && !getValueSet().getCompose().hasExclude() + && getValueSet().getCompose().getInclude().stream() + .noneMatch(csc -> !csc.hasValueSet() || csc.hasFilter()); } @Override public boolean hasNaiveParameter() { - return valueSet.getExpansion().getParameter().stream() + return getValueSet().getExpansion().getParameter().stream() .anyMatch(p -> p.getName().equals("naive")); } @@ -324,7 +148,7 @@ public ValueSet.ValueSetExpansionParameterComponent createNaiveParameter() { public void naiveExpand() { var expansion = newExpansion().addParameter(createNaiveParameter()); - for (var code : getCodesInCompose(FhirContext.forDstu3Cached(), valueSet)) { + for (var code : getCodesInCompose(fhirContext, getValueSet())) { expansion .addContains() .setCode(code.getCode()) @@ -332,6 +156,6 @@ public void naiveExpand() { .setVersion(code.getVersion()) .setDisplay(code.getDisplay()); } - valueSet.setExpansion(expansion); + getValueSet().setExpansion(expansion); } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/KnowledgeArtifactAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/KnowledgeArtifactAdapter.java index a6e9d9824..74901c195 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/KnowledgeArtifactAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/KnowledgeArtifactAdapter.java @@ -28,82 +28,22 @@ public KnowledgeArtifactAdapter(IDomainResource resource) { throw new IllegalArgumentException( "resource passed as resource argument is not a MetadataResource resource"); } - this.adaptedResource = (MetadataResource) resource; + adaptedResource = (MetadataResource) resource; } public KnowledgeArtifactAdapter(MetadataResource resource) { super(resource); - this.adaptedResource = resource; + adaptedResource = resource; } @Override public MetadataResource get() { - return this.adaptedResource; + return adaptedResource; } @Override public MetadataResource copy() { - return this.get().copy(); - } - - @Override - public String getUrl() { - return this.get().getUrl(); - } - - @Override - public boolean hasUrl() { - return this.get().hasUrl(); - } - - @Override - public void setUrl(String url) { - this.get().setUrl(url); - } - - @Override - public void setVersion(String version) { - this.get().setVersion(version); - } - - @Override - public String getVersion() { - return this.get().getVersion(); - } - - @Override - public boolean hasVersion() { - return this.get().hasVersion(); - } - - @Override - public String getName() { - return this.get().getName(); - } - - @Override - public String getPurpose() { - return null; - } - - @Override - public void setName(String name) { - this.get().setName(name); - } - - @Override - public Date getApprovalDate() { - return null; - } - - @Override - public Date getDate() { - return this.get().getDate(); - } - - @Override - public void setDate(Date approvalDate) { - this.get().setDate(approvalDate); + return get().copy(); } @Override @@ -111,22 +51,7 @@ public void setDateElement(IPrimitiveType date) { if (date != null && !(date instanceof DateTimeType)) { throw new UnprocessableEntityException("Date must be " + DateTimeType.class.getName()); } - this.get().setDateElement((DateTimeType) date); - } - - @Override - public void setApprovalDate(Date approvalDate) { - // do nothing - } - - @Override - public Period getEffectivePeriod() { - return new Period(); - } - - @Override - public List getDependencies() { - return new ArrayList<>(); + org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter.super.setDateElement(date); } @Override @@ -134,18 +59,12 @@ public void setEffectivePeriod(ICompositeType effectivePeriod) { if (effectivePeriod != null && !(effectivePeriod instanceof Period)) { throw new UnprocessableEntityException("EffectivePeriod must be a valid " + Period.class.getName()); } - // does nothing - } - - @Override - public boolean hasRelatedArtifact() { - return false; + org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter.super.setEffectivePeriod(effectivePeriod); } - @SuppressWarnings("unchecked") @Override - public List getRelatedArtifact() { - return new ArrayList(); + public List getDependencies() { + return new ArrayList<>(); } @SuppressWarnings("unchecked") @@ -157,14 +76,15 @@ public List getRelatedArtifactsOfType(String codeString) { } catch (FHIRException e) { throw new UnprocessableEntityException("Invalid related artifact code"); } - return this.getRelatedArtifact().stream() + return getRelatedArtifact().stream() + .map(ra -> (RelatedArtifact) ra) .filter(ra -> ra.getType() == type) .collect(Collectors.toList()); } @Override - public void setRelatedArtifact(List relatedArtifacts) { - // does nothing + public String getStatus() { + return get().getStatus() == null ? null : get().getStatus().toCode(); } @Override @@ -175,16 +95,21 @@ public void setStatus(String statusCodeString) { } catch (FHIRException e) { throw new UnprocessableEntityException("Invalid status code"); } - this.get().setStatus(status); - } - - @Override - public String getStatus() { - return this.get().getStatus() == null ? null : this.get().getStatus().toCode(); - } - - @Override - public boolean getExperimental() { - return this.get().getExperimental(); + get().setStatus(status); + } + + @Override + public void setRelatedArtifact(List relatedArtifacts) + throws UnprocessableEntityException { + org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter.super.setRelatedArtifact(relatedArtifacts.stream() + .map(ra -> { + try { + return (RelatedArtifact) ra; + } catch (ClassCastException e) { + throw new UnprocessableEntityException( + "All related artifacts must be of type " + RelatedArtifact.class.getName()); + } + }) + .collect(Collectors.toList())); } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/LibraryAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/LibraryAdapter.java index 08647dbf0..3c238a391 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/LibraryAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/LibraryAdapter.java @@ -2,140 +2,82 @@ import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; import java.util.ArrayList; -import java.util.Date; import java.util.List; import java.util.stream.Collectors; -import org.hl7.fhir.exceptions.FHIRException; -import org.hl7.fhir.instance.model.api.IBaseExtension; -import org.hl7.fhir.instance.model.api.IBaseHasExtensions; import org.hl7.fhir.instance.model.api.ICompositeType; import org.hl7.fhir.instance.model.api.IDomainResource; -import org.hl7.fhir.instance.model.api.IPrimitiveType; import org.hl7.fhir.r4.model.Attachment; import org.hl7.fhir.r4.model.CodeableConcept; import org.hl7.fhir.r4.model.Coding; import org.hl7.fhir.r4.model.DataRequirement; -import org.hl7.fhir.r4.model.DateTimeType; -import org.hl7.fhir.r4.model.Enumerations.PublicationStatus; -import org.hl7.fhir.r4.model.Extension; import org.hl7.fhir.r4.model.Library; -import org.hl7.fhir.r4.model.Period; import org.hl7.fhir.r4.model.RelatedArtifact; -import org.hl7.fhir.r4.model.RelatedArtifact.RelatedArtifactType; import org.hl7.fhir.r4.model.UsageContext; import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; -public class LibraryAdapter extends ResourceAdapter implements org.opencds.cqf.fhir.utility.adapter.LibraryAdapter { - - private Library library; - +public class LibraryAdapter extends KnowledgeArtifactAdapter + implements org.opencds.cqf.fhir.utility.adapter.LibraryAdapter { public LibraryAdapter(IDomainResource library) { super(library); if (!(library instanceof Library)) { throw new IllegalArgumentException("resource passed as library argument is not a Library resource"); } - this.library = (Library) library; } public LibraryAdapter(Library library) { super(library); - this.library = library; } protected Library getLibrary() { - return this.library; + return (Library) resource; } @Override public Library get() { - return this.library; + return (Library) resource; } @Override public Library copy() { - return this.get().copy(); - } - - @Override - public String getName() { - return this.getLibrary().getName(); - } - - @Override - public String getPurpose() { - return this.getLibrary().getPurpose(); - } - - @Override - public void setName(String name) { - this.getLibrary().setName(name); - } - - @Override - public boolean hasUrl() { - return this.getLibrary().hasUrl(); - } - - @Override - public String getUrl() { - return this.getLibrary().getUrl(); - } - - @Override - public void setUrl(String url) { - this.getLibrary().setUrl(url); - } - - @Override - public String getVersion() { - return this.getLibrary().getVersion(); - } - - @Override - public boolean hasVersion() { - return this.getLibrary().hasVersion(); - } - - @Override - public void setVersion(String version) { - this.getLibrary().setVersion(version); + return get().copy(); } @Override public boolean hasContent() { - return this.getLibrary().hasContent(); + return getLibrary().hasContent(); } @Override public List getContent() { - return this.getLibrary().getContent().stream().collect(Collectors.toList()); + return getLibrary().getContent().stream().collect(Collectors.toList()); } @Override public void setContent(List attachments) { List castAttachments = attachments.stream().map(x -> (Attachment) x).collect(Collectors.toList()); - this.getLibrary().setContent(castAttachments); + getLibrary().setContent(castAttachments); } @Override public Attachment addContent() { - return this.getLibrary().addContent(); + return getLibrary().addContent(); } @Override public List getDependencies() { List references = new ArrayList(); final String referenceSource = - this.hasVersion() ? this.getUrl() + "|" + this.getLibrary().getVersion() : this.getUrl(); + hasVersion() ? getUrl() + "|" + getLibrary().getVersion() : getUrl(); // relatedArtifact[].resource - this.getRelatedArtifact().stream() + getRelatedArtifact().stream() + .map(ra -> (RelatedArtifact) ra) .filter(ra -> ra.hasResource()) .map(ra -> DependencyInfo.convertRelatedArtifact(ra, referenceSource)) .forEach(ra -> references.add(ra)); - this.getLibrary().getDataRequirement().stream().forEach(dr -> { + getLibrary().getDataRequirement().stream().forEach(dr -> { dr.getProfile().stream() .filter(profile -> profile.hasValue()) .forEach(profile -> references.add(new DependencyInfo( @@ -154,131 +96,22 @@ public List getDependencies() { return references; } - @Override - public void setRelatedArtifact(List relatedArtifacts) - throws UnprocessableEntityException { - this.getLibrary() - .setRelatedArtifact(relatedArtifacts.stream() - .map(ra -> { - try { - return (RelatedArtifact) ra; - } catch (ClassCastException e) { - throw new UnprocessableEntityException( - "All related artifacts must be of type " + RelatedArtifact.class.getName()); - } - }) - .collect(Collectors.toList())); - } - - @Override - public Date getApprovalDate() { - return this.getLibrary().getApprovalDate(); - } - - @Override - public Date getDate() { - return this.getLibrary().getDate(); - } - - @Override - public void setDate(Date date) { - this.getLibrary().setDate(date); - } - - @Override - public void setDateElement(IPrimitiveType date) { - if (date != null && !(date instanceof DateTimeType)) { - throw new UnprocessableEntityException("Date must be " + DateTimeType.class.getName()); - } - this.getLibrary().setDateElement((DateTimeType) date); - } - - @Override - public Period getEffectivePeriod() { - return this.getLibrary().getEffectivePeriod(); - } - - @Override - public void setEffectivePeriod(ICompositeType effectivePeriod) { - if (effectivePeriod != null && !(effectivePeriod instanceof Period)) { - throw new UnprocessableEntityException("EffectivePeriod must " + Period.class.getName()); - } - this.getLibrary().setEffectivePeriod((Period) effectivePeriod); - } - - @Override - public boolean hasRelatedArtifact() { - return this.getLibrary().hasRelatedArtifact(); - } - - @SuppressWarnings("unchecked") - @Override - public List getRelatedArtifact() { - return this.getLibrary().getRelatedArtifact(); - } - - @Override - public void setApprovalDate(Date approvalDate) { - this.getLibrary().setApprovalDate(approvalDate); - } - - @SuppressWarnings("unchecked") - @Override - public List getRelatedArtifactsOfType(String codeString) { - RelatedArtifactType type; - try { - type = RelatedArtifactType.fromCode(codeString); - } catch (FHIRException e) { - throw new UnprocessableEntityException("Invalid related artifact code"); - } - return this.getRelatedArtifact().stream() - .filter(ra -> ra.getType() == type) - .collect(Collectors.toList()); - } - @SuppressWarnings("unchecked") @Override public List getComponents() { - return this.getRelatedArtifactsOfType("composed-of"); - } - - @Override - public String getStatus() { - return this.getLibrary().getStatus() == null - ? null - : this.getLibrary().getStatus().toCode(); - } - - @Override - public void setStatus(String statusCodeString) { - PublicationStatus status; - try { - status = PublicationStatus.fromCode(statusCodeString); - } catch (FHIRException e) { - throw new UnprocessableEntityException("Invalid status code: " + statusCodeString); - } - this.getLibrary().setStatus(status); - } - - @Override - public boolean getExperimental() { - return this.getLibrary().getExperimental(); - } - - @Override - public void setExtension(List> extensions) { - this.get().setExtension(extensions.stream().map(e -> (Extension) e).collect(Collectors.toList())); + return getRelatedArtifactsOfType("composed-of"); } @Override public ICompositeType getType() { - return library.getType(); + return getLibrary().getType(); } @Override public LibraryAdapter setType(String type) { if (LIBRARY_TYPES.contains(type)) { - library.setType(new CodeableConcept(new Coding("http://hl7.org/fhir/ValueSet/library-type", type, ""))); + getLibrary() + .setType(new CodeableConcept(new Coding("http://hl7.org/fhir/ValueSet/library-type", type, ""))); } else { throw new UnprocessableEntityException("Invalid type: {}", type); } @@ -287,24 +120,26 @@ public LibraryAdapter setType(String type) { @Override public List getDataRequirement() { - return library.getDataRequirement(); + return getLibrary().getDataRequirement(); } @Override public LibraryAdapter addDataRequirement(ICompositeType dataRequirement) { - library.addDataRequirement((DataRequirement) dataRequirement); + getLibrary().addDataRequirement((DataRequirement) dataRequirement); return this; } @Override public LibraryAdapter setDataRequirements(List dataRequirements) { - library.setDataRequirement( - dataRequirements.stream().map(dr -> (DataRequirement) dr).collect(Collectors.toList())); + getLibrary() + .setDataRequirement(dataRequirements.stream() + .map(dr -> (DataRequirement) dr) + .collect(Collectors.toList())); return this; } @Override public List getUseContext() { - return library.getUseContext(); + return getLibrary().getUseContext(); } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/PlanDefinitionAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/PlanDefinitionAdapter.java index 652e46873..1f50dfe8a 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/PlanDefinitionAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/PlanDefinitionAdapter.java @@ -1,126 +1,49 @@ package org.opencds.cqf.fhir.utility.adapter.r4; -import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; import java.util.ArrayList; -import java.util.Date; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; -import org.hl7.fhir.exceptions.FHIRException; -import org.hl7.fhir.instance.model.api.IBaseExtension; -import org.hl7.fhir.instance.model.api.IBaseHasExtensions; -import org.hl7.fhir.instance.model.api.ICompositeType; import org.hl7.fhir.instance.model.api.IDomainResource; -import org.hl7.fhir.instance.model.api.IIdType; -import org.hl7.fhir.instance.model.api.IPrimitiveType; import org.hl7.fhir.r4.model.CanonicalType; -import org.hl7.fhir.r4.model.DateTimeType; -import org.hl7.fhir.r4.model.Enumerations.PublicationStatus; -import org.hl7.fhir.r4.model.Extension; -import org.hl7.fhir.r4.model.Period; import org.hl7.fhir.r4.model.PlanDefinition; -import org.hl7.fhir.r4.model.RelatedArtifact; -import org.hl7.fhir.r4.model.RelatedArtifact.RelatedArtifactType; import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; -import org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter; -public class PlanDefinitionAdapter extends ResourceAdapter implements KnowledgeArtifactAdapter { - - private PlanDefinition planDefinition; +public class PlanDefinitionAdapter extends KnowledgeArtifactAdapter { public PlanDefinitionAdapter(IDomainResource planDefinition) { super(planDefinition); - if (!(planDefinition instanceof PlanDefinition)) { throw new IllegalArgumentException( "resource passed as planDefinition argument is not a PlanDefinition resource"); } - - this.planDefinition = (PlanDefinition) planDefinition; } public PlanDefinitionAdapter(PlanDefinition planDefinition) { super(planDefinition); - this.planDefinition = planDefinition; } protected PlanDefinition getPlanDefinition() { - return this.planDefinition; + return (PlanDefinition) resource; } @Override public PlanDefinition get() { - return this.planDefinition; + return getPlanDefinition(); } @Override public PlanDefinition copy() { - return this.get().copy(); - } - - @Override - public IIdType getId() { - return this.getPlanDefinition().getIdElement(); - } - - @Override - public void setId(IIdType id) { - this.getPlanDefinition().setId(id); - } - - @Override - public String getName() { - return this.getPlanDefinition().getName(); - } - - @Override - public String getPurpose() { - return this.getPlanDefinition().getPurpose(); - } - - @Override - public void setName(String name) { - this.getPlanDefinition().setName(name); - } - - @Override - public String getUrl() { - return this.getPlanDefinition().getUrl(); - } - - @Override - public boolean hasUrl() { - return this.getPlanDefinition().hasUrl(); - } - - @Override - public void setUrl(String url) { - this.getPlanDefinition().setUrl(url); - } - - @Override - public String getVersion() { - return this.getPlanDefinition().getVersion(); - } - - @Override - public boolean hasVersion() { - return this.getPlanDefinition().hasVersion(); - } - - @Override - public void setVersion(String version) { - this.getPlanDefinition().setVersion(version); + return get().copy(); } @Override public List getDependencies() { List references = new ArrayList<>(); - final String referenceSource = this.getPlanDefinition().hasVersion() - ? this.getPlanDefinition().getUrl() + "|" - + this.getPlanDefinition().getVersion() - : this.getPlanDefinition().getUrl(); + final String referenceSource = getPlanDefinition().hasVersion() + ? getPlanDefinition().getUrl() + "|" + getPlanDefinition().getVersion() + : getPlanDefinition().getUrl(); /* relatedArtifact[].resource library[] @@ -137,20 +60,20 @@ public List getDependencies() { */ // relatedArtifact[].resource - references.addAll(this.getRelatedArtifact().stream() + references.addAll(getRelatedArtifact().stream() .map(ra -> DependencyInfo.convertRelatedArtifact(ra, referenceSource)) .collect(Collectors.toList())); // library[] - List libraries = this.getPlanDefinition().getLibrary(); + List libraries = getPlanDefinition().getLibrary(); for (CanonicalType ct : libraries) { DependencyInfo dependency = new DependencyInfo( referenceSource, ct.getValue(), ct.getExtension(), (reference) -> ct.setValue(reference)); references.add(dependency); } // action[] - this.planDefinition.getAction().forEach(action -> getDependenciesOfAction(action, references, referenceSource)); - this.getPlanDefinition().getExtension().stream() + getPlanDefinition().getAction().forEach(action -> getDependenciesOfAction(action, references, referenceSource)); + getPlanDefinition().getExtension().stream() .filter(ext -> ext.getUrl().contains("cpg-partOf")) .filter(ext -> ext.hasValue()) .findAny() @@ -249,106 +172,4 @@ private void getDependenciesOfAction( } action.getAction().forEach(nestedAction -> getDependenciesOfAction(nestedAction, references, referenceSource)); } - - @Override - public Date getApprovalDate() { - return this.getPlanDefinition().getApprovalDate(); - } - - @Override - public Date getDate() { - return this.getPlanDefinition().getDate(); - } - - @Override - public void setDate(Date date) { - this.getPlanDefinition().setDate(date); - } - - @Override - public void setDateElement(IPrimitiveType date) { - if (date != null && !(date instanceof DateTimeType)) { - throw new UnprocessableEntityException("Date must be " + DateTimeType.class.getName()); - } - this.getPlanDefinition().setDateElement((DateTimeType) date); - } - - @Override - public Period getEffectivePeriod() { - return this.getPlanDefinition().getEffectivePeriod(); - } - - @Override - public void setApprovalDate(Date date) { - this.getPlanDefinition().setApprovalDate(date); - } - - @Override - public boolean hasRelatedArtifact() { - return this.getPlanDefinition().hasRelatedArtifact(); - } - - @SuppressWarnings("unchecked") - @Override - public List getRelatedArtifact() { - return this.getPlanDefinition().getRelatedArtifact(); - } - - @Override - public void setRelatedArtifact(List relatedArtifacts) { - this.getPlanDefinition() - .setRelatedArtifact(relatedArtifacts.stream() - .map(ra -> (RelatedArtifact) ra) - .collect(Collectors.toList())); - } - - @SuppressWarnings("unchecked") - @Override - public List getRelatedArtifactsOfType(String codeString) { - RelatedArtifactType type; - try { - type = RelatedArtifactType.fromCode(codeString); - } catch (FHIRException e) { - throw new UnprocessableEntityException("Invalid related artifact code"); - } - return this.getRelatedArtifact().stream() - .filter(ra -> ra.getType() == type) - .collect(Collectors.toList()); - } - - @Override - public void setEffectivePeriod(ICompositeType effectivePeriod) { - if (effectivePeriod != null && !(effectivePeriod instanceof Period)) { - throw new UnprocessableEntityException("EffectivePeriod must be " + Period.class.getName()); - } - this.getPlanDefinition().setEffectivePeriod((Period) effectivePeriod); - } - - @Override - public void setStatus(String statusCodeString) { - PublicationStatus status; - try { - status = PublicationStatus.fromCode(statusCodeString); - } catch (FHIRException e) { - throw new UnprocessableEntityException("Invalid status code"); - } - this.getPlanDefinition().setStatus(status); - } - - @Override - public String getStatus() { - return this.getPlanDefinition().getStatus() == null - ? null - : this.getPlanDefinition().getStatus().toCode(); - } - - @Override - public boolean getExperimental() { - return this.getPlanDefinition().getExperimental(); - } - - @Override - public void setExtension(List> extensions) { - this.get().setExtension(extensions.stream().map(e -> (Extension) e).collect(Collectors.toList())); - } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/QuestionnaireAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/QuestionnaireAdapter.java index 59b2c41f8..3818af5f5 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/QuestionnaireAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/QuestionnaireAdapter.java @@ -11,35 +11,38 @@ public class QuestionnaireAdapter extends KnowledgeArtifactAdapter { - private Questionnaire questionnaire; - public QuestionnaireAdapter(IDomainResource questionnaire) { super(questionnaire); - if (!(questionnaire instanceof Questionnaire)) { throw new IllegalArgumentException( "resource passed as questionnaire argument is not a Questionnaire resource"); } - - this.questionnaire = (Questionnaire) questionnaire; } public QuestionnaireAdapter(Questionnaire questionnaire) { super(questionnaire); - this.questionnaire = questionnaire; } protected Questionnaire getQuestionnaire() { - return this.questionnaire; + return (Questionnaire) resource; + } + + @Override + public Questionnaire get() { + return getQuestionnaire(); + } + + @Override + public Questionnaire copy() { + return get().copy(); } @Override public List getDependencies() { List references = new ArrayList<>(); - final String referenceSource = this.getQuestionnaire().hasVersion() - ? this.getQuestionnaire().getUrl() + "|" - + this.getQuestionnaire().getVersion() - : this.getQuestionnaire().getUrl(); + final String referenceSource = getQuestionnaire().hasVersion() + ? getQuestionnaire().getUrl() + "|" + getQuestionnaire().getVersion() + : getQuestionnaire().getUrl(); /* derivedFrom item[]..definition // NOTE: This is not a simple canonical, it will have a fragment to identify the specific element @@ -61,7 +64,7 @@ public List getDependencies() { item[]..extension[sdc-questionnaire-subQuestionnaire] */ - var libraryExtensions = questionnaire.getExtensionsByUrl(Constants.CQF_LIBRARY); + var libraryExtensions = getQuestionnaire().getExtensionsByUrl(Constants.CQF_LIBRARY); for (var libraryExt : libraryExtensions) { DependencyInfo dependency = new DependencyInfo( referenceSource, diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/ResourceAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/ResourceAdapter.java index 9c745907d..602441b81 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/ResourceAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/ResourceAdapter.java @@ -3,7 +3,6 @@ import static java.util.Optional.ofNullable; import ca.uhn.fhir.context.FhirVersionEnum; -import java.util.ArrayList; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; @@ -15,10 +14,12 @@ import org.hl7.fhir.r4.model.DomainResource; import org.hl7.fhir.r4.model.Extension; import org.hl7.fhir.r4.model.Resource; +import org.opencds.cqf.fhir.utility.adapter.BaseResourceAdapter; -class ResourceAdapter implements org.opencds.cqf.fhir.utility.adapter.ResourceAdapter { +class ResourceAdapter extends BaseResourceAdapter { ResourceAdapter(IBaseResource resource) { + super(resource); if (resource == null) { throw new IllegalArgumentException("resource can not be null"); } @@ -26,14 +27,10 @@ class ResourceAdapter implements org.opencds.cqf.fhir.utility.adapter.ResourceAd if (!resource.getStructureFhirVersionEnum().equals(FhirVersionEnum.R4)) { throw new IllegalArgumentException("resource is incorrect fhir version for this adapter"); } - - this.resource = (Resource) resource; } - private Resource resource; - protected Resource getResource() { - return this.resource; + return (Resource) resource; } protected boolean isDomainResource() { @@ -44,23 +41,19 @@ protected Optional getDomainResource() { return ofNullable(resource instanceof DomainResource ? (DomainResource) resource : null); } - public IBaseResource get() { - return this.resource; - } - @Override public IBase setProperty(String name, IBase value) throws FHIRException { - return this.getResource().setProperty(name, (Base) value); + return getResource().setProperty(name, (Base) value); } @Override public IBase addChild(String name) throws FHIRException { - return this.getResource().addChild(name); + return getResource().addChild(name); } @Override public IBase getSingleProperty(String name) throws FHIRException { - IBase[] values = this.getProperty(name, true); + IBase[] values = getProperty(name, true); if (values == null || values.length == 0) { return null; @@ -75,42 +68,42 @@ public IBase getSingleProperty(String name) throws FHIRException { @Override public IBase[] getProperty(String name) throws FHIRException { - return this.getProperty(name, true); + return getProperty(name, true); } @Override public IBase[] getProperty(String name, boolean checkValid) throws FHIRException { - return this.getResource().getProperty(name.hashCode(), name, checkValid); + return getResource().getProperty(name.hashCode(), name, checkValid); } @Override public IBase makeProperty(String name) throws FHIRException { - return this.getResource().makeProperty(name.hashCode(), name); + return getResource().makeProperty(name.hashCode(), name); } @Override public String[] getTypesForProperty(String name) throws FHIRException { - return this.getResource().getTypesForProperty(name.hashCode(), name); + return getResource().getTypesForProperty(name.hashCode(), name); } @Override public IBaseResource copy() { - return this.getResource().copy(); + return getResource().copy(); } @Override public void copyValues(IBaseResource dst) { - this.getResource().copyValues((Resource) dst); + getResource().copyValues((Resource) dst); } @Override public boolean equalsDeep(IBase other) { - return this.getResource().equalsDeep((Base) other); + return getResource().equalsDeep((Base) other); } @Override public boolean equalsShallow(IBase other) { - return this.getResource().equalsShallow((Base) other); + return getResource().equalsShallow((Base) other); } @Override @@ -124,34 +117,4 @@ public void setExtension(List> extensions) { public > void addExtension(T extension) { getDomainResource().ifPresent(r -> r.addExtension((Extension) extension)); } - - @Override - public List getExtension() { - return isDomainResource() ? getDomainResource().get().getExtension() : new ArrayList<>(); - } - - @SuppressWarnings("unchecked") - @Override - public Extension getExtensionByUrl(String url) { - return isDomainResource() - ? getDomainResource().get().getExtension().stream() - .filter(e -> e.getUrl().equals(url)) - .findFirst() - .orElse(null) - : null; - } - - @Override - public List getExtensionsByUrl(String url) { - return isDomainResource() - ? getDomainResource().get().getExtension().stream() - .filter(e -> e.getUrl().equals(url)) - .collect(Collectors.toList()) - : new ArrayList<>(); - } - - @Override - public List getContained() { - return isDomainResource() ? getDomainResource().get().getContained() : new ArrayList<>(); - } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/StructureDefinitionAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/StructureDefinitionAdapter.java index 6b641fe42..6a18bd2cc 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/StructureDefinitionAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/StructureDefinitionAdapter.java @@ -1,32 +1,15 @@ package org.opencds.cqf.fhir.utility.adapter.r4; -import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; import java.util.ArrayList; -import java.util.Date; import java.util.List; -import java.util.stream.Collectors; -import org.hl7.fhir.exceptions.FHIRException; -import org.hl7.fhir.instance.model.api.IBaseExtension; -import org.hl7.fhir.instance.model.api.IBaseHasExtensions; -import org.hl7.fhir.instance.model.api.ICompositeType; import org.hl7.fhir.instance.model.api.IDomainResource; -import org.hl7.fhir.instance.model.api.IPrimitiveType; import org.hl7.fhir.r4.model.CanonicalType; -import org.hl7.fhir.r4.model.DateTimeType; -import org.hl7.fhir.r4.model.Enumerations.PublicationStatus; -import org.hl7.fhir.r4.model.Extension; -import org.hl7.fhir.r4.model.Period; -import org.hl7.fhir.r4.model.RelatedArtifact; -import org.hl7.fhir.r4.model.RelatedArtifact.RelatedArtifactType; import org.hl7.fhir.r4.model.StructureDefinition; import org.opencds.cqf.fhir.utility.Constants; import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; -import org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter; -public class StructureDefinitionAdapter extends ResourceAdapter implements KnowledgeArtifactAdapter { - - private StructureDefinition structureDefinition; +public class StructureDefinitionAdapter extends KnowledgeArtifactAdapter { public StructureDefinitionAdapter(IDomainResource structureDefinition) { super(structureDefinition); @@ -34,16 +17,14 @@ public StructureDefinitionAdapter(IDomainResource structureDefinition) { throw new IllegalArgumentException( "resource passed as planDefinition argument is not a StructureDefinition resource"); } - this.structureDefinition = (StructureDefinition) structureDefinition; } public StructureDefinitionAdapter(StructureDefinition structureDefinition) { super(structureDefinition); - this.structureDefinition = structureDefinition; } protected StructureDefinition getStructureDefinition() { - return structureDefinition; + return (StructureDefinition) resource; } @Override @@ -68,7 +49,7 @@ public List getDependencies() { extension[cpg-featureExpression].reference */ - var libraryExtensions = structureDefinition.getExtensionsByUrl(Constants.CQF_LIBRARY); + var libraryExtensions = getStructureDefinition().getExtensionsByUrl(Constants.CQF_LIBRARY); for (var libraryExt : libraryExtensions) { DependencyInfo dependency = new DependencyInfo( referenceSource, @@ -83,152 +64,11 @@ public List getDependencies() { @Override public StructureDefinition get() { - return structureDefinition; + return getStructureDefinition(); } @Override public StructureDefinition copy() { return get().copy(); } - - @Override - public String getUrl() { - return get().getUrl(); - } - - @Override - public boolean hasUrl() { - return get().hasUrl(); - } - - @Override - public void setUrl(String url) { - get().setUrl(url); - } - - @Override - public void setVersion(String version) { - get().setVersion(version); - } - - @Override - public String getVersion() { - return get().getVersion(); - } - - @Override - public boolean hasVersion() { - return get().hasVersion(); - } - - @Override - public String getName() { - return get().getName(); - } - - @Override - public void setName(String name) { - get().setName(name); - } - - @Override - public Date getApprovalDate() { - return null; - } - - @Override - public Date getDate() { - return get().getDate(); - } - - @Override - public void setDate(Date approvalDate) { - get().setDate(approvalDate); - } - - @Override - public void setDateElement(IPrimitiveType date) { - if (date != null && !(date instanceof DateTimeType)) { - throw new UnprocessableEntityException("Date must be " + DateTimeType.class.getName()); - } - get().setDateElement((DateTimeType) date); - } - - @Override - public void setApprovalDate(Date approvalDate) { - // do nothing - } - - @Override - public Period getEffectivePeriod() { - return null; - } - - @Override - public String getPurpose() { - return get().getPurpose(); - } - - @Override - public void setEffectivePeriod(ICompositeType effectivePeriod) { - if (effectivePeriod != null && !(effectivePeriod instanceof Period)) { - throw new UnprocessableEntityException("EffectivePeriod must be a valid " + Period.class.getName()); - } - // do nothing - } - - @Override - public boolean hasRelatedArtifact() { - return false; - } - - @SuppressWarnings("unchecked") - @Override - public List getRelatedArtifact() { - return new ArrayList<>(); - } - - @SuppressWarnings("unchecked") - @Override - public List getRelatedArtifactsOfType(String codeString) { - RelatedArtifactType type; - try { - type = RelatedArtifactType.fromCode(codeString); - } catch (FHIRException e) { - throw new UnprocessableEntityException("Invalid related artifact code"); - } - return getRelatedArtifact().stream().filter(ra -> ra.getType() == type).collect(Collectors.toList()); - } - - @Override - public void setRelatedArtifact(List relatedArtifacts) - throws UnprocessableEntityException { - // do nothing - } - - @Override - public void setStatus(String statusCodeString) { - PublicationStatus status; - try { - status = PublicationStatus.fromCode(statusCodeString); - } catch (FHIRException e) { - throw new UnprocessableEntityException("Invalid status code"); - } - get().setStatus(status); - } - - @Override - public String getStatus() { - return get().getStatus() == null ? null : get().getStatus().toCode(); - } - - @Override - public boolean getExperimental() { - return get().getExperimental(); - } - - @Override - public void setExtension(List> extensions) { - get().setExtension(extensions.stream().map(e -> (Extension) e).collect(Collectors.toList())); - } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/ValueSetAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/ValueSetAdapter.java index 575131e32..39af1c92c 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/ValueSetAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/ValueSetAdapter.java @@ -2,8 +2,6 @@ import static org.opencds.cqf.fhir.utility.ValueSets.getCodesInCompose; -import ca.uhn.fhir.context.FhirContext; -import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; import java.time.Instant; import java.util.ArrayList; import java.util.Collection; @@ -11,108 +9,49 @@ import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; -import org.hl7.fhir.exceptions.FHIRException; import org.hl7.fhir.instance.model.api.IBaseBackboneElement; -import org.hl7.fhir.instance.model.api.IBaseHasExtensions; -import org.hl7.fhir.instance.model.api.ICompositeType; import org.hl7.fhir.instance.model.api.IDomainResource; -import org.hl7.fhir.instance.model.api.IIdType; -import org.hl7.fhir.instance.model.api.IPrimitiveType; import org.hl7.fhir.r4.model.BooleanType; import org.hl7.fhir.r4.model.DateTimeType; -import org.hl7.fhir.r4.model.Enumerations.PublicationStatus; -import org.hl7.fhir.r4.model.Period; -import org.hl7.fhir.r4.model.RelatedArtifact; -import org.hl7.fhir.r4.model.RelatedArtifact.RelatedArtifactType; import org.hl7.fhir.r4.model.ValueSet; import org.hl7.fhir.r4.model.ValueSet.ValueSetExpansionComponent; import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; -public class ValueSetAdapter extends ResourceAdapter implements org.opencds.cqf.fhir.utility.adapter.ValueSetAdapter { - - private ValueSet valueSet; +public class ValueSetAdapter extends KnowledgeArtifactAdapter + implements org.opencds.cqf.fhir.utility.adapter.ValueSetAdapter { public ValueSetAdapter(IDomainResource valueSet) { super(valueSet); - if (!(valueSet instanceof ValueSet)) { throw new IllegalArgumentException("resource passed as valueSet argument is not a ValueSet resource"); } - - this.valueSet = (ValueSet) valueSet; } public ValueSetAdapter(ValueSet valueSet) { super(valueSet); - this.valueSet = valueSet; } protected ValueSet getValueSet() { - return this.valueSet; + return (ValueSet) resource; } @Override public ValueSet get() { - return this.valueSet; + return (ValueSet) resource; } @Override public ValueSet copy() { - return this.get().copy(); - } - - @Override - public IIdType getId() { - return this.getValueSet().getIdElement(); - } - - @Override - public void setId(IIdType id) { - this.getValueSet().setId(id); - } - - @Override - public String getName() { - return this.getValueSet().getName(); - } - - @Override - public String getPurpose() { - return this.getValueSet().getPurpose(); - } - - @Override - public void setName(String name) { - this.getValueSet().setName(name); - } - - @Override - public String getUrl() { - return this.getValueSet().getUrl(); - } - - @Override - public void setUrl(String url) { - this.getValueSet().setUrl(url); - } - - @Override - public String getVersion() { - return this.getValueSet().getVersion(); - } - - @Override - public void setVersion(String version) { - this.getValueSet().setVersion(version); + return get().copy(); } @Override public List getDependencies() { List references = new ArrayList<>(); - final String referenceSource = this.getValueSet().hasVersion() - ? this.getValueSet().getUrl() + "|" + this.getValueSet().getVersion() - : this.getValueSet().getUrl(); + final String referenceSource = getValueSet().hasVersion() + ? getValueSet().getUrl() + "|" + getValueSet().getVersion() + : getValueSet().getUrl(); /* compose.include[].valueSet @@ -121,8 +60,8 @@ public List getDependencies() { compose.exclude[].system */ Stream.concat( - this.valueSet.getCompose().getInclude().stream(), - this.valueSet.getCompose().getExclude().stream()) + getValueSet().getCompose().getInclude().stream(), + getValueSet().getCompose().getExclude().stream()) .forEach(component -> { if (component.hasValueSet()) { component.getValueSet().forEach(ct -> { @@ -146,124 +85,15 @@ public List getDependencies() { return references; } - @Override - public Date getApprovalDate() { - return null; - } - - @Override - public Period getEffectivePeriod() { - return new Period(); - } - - @Override - public String getStatus() { - return this.getValueSet().getStatus() == null - ? null - : this.getValueSet().getStatus().toCode(); - } - - @Override - public void setStatus(String statusCodeString) { - PublicationStatus status; - try { - status = PublicationStatus.fromCode(statusCodeString); - } catch (FHIRException e) { - throw new UnprocessableEntityException("Invalid status code"); - } - this.getValueSet().setStatus(status); - } - - @SuppressWarnings("unchecked") - @Override - public List getRelatedArtifactsOfType(String codeString) { - RelatedArtifactType type; - try { - type = RelatedArtifactType.fromCode(codeString); - } catch (FHIRException e) { - throw new UnprocessableEntityException("Invalid related artifact code"); - } - return this.getRelatedArtifact().stream() - .filter(ra -> ra.getType() == type) - .collect(Collectors.toList()); - } - - @Override - public boolean hasRelatedArtifact() { - return false; - } - - @SuppressWarnings("unchecked") - @Override - public List getRelatedArtifact() { - return new ArrayList<>(); - } - - @Override - public Date getDate() { - return this.getValueSet().getDate(); - } - - @Override - public boolean getExperimental() { - return this.getValueSet().getExperimental(); - } - - @Override - public void setDateElement(IPrimitiveType date) throws UnprocessableEntityException { - if (date != null && !(date instanceof DateTimeType)) { - throw new UnprocessableEntityException("Date must be " + DateTimeType.class.getName()); - } - this.getValueSet().setDateElement((DateTimeType) date); - } - - @Override - public boolean hasUrl() { - return this.getValueSet().hasUrl(); - } - - @Override - public void setApprovalDate(Date approvalDate) { - // do nothing - } - - @Override - public boolean hasVersion() { - return this.getValueSet().hasVersion(); - } - - @Override - public void setDate(Date date) { - this.getValueSet().setDate(date); - } - - @Override - public void setRelatedArtifact(List relatedArtifacts) { - // do nothing - } - - @Override - public void setEffectivePeriod(ICompositeType effectivePeriod) { - if (effectivePeriod != null && !(effectivePeriod instanceof Period)) { - throw new UnprocessableEntityException("EffectivePeriod must be " + Period.class.getName()); - } - // do nothing - } - - // @Override - // public void setExtension(List> extensions) { - // this.get().setExtension(extensions.stream().map(e -> (Extension) e).collect(Collectors.toList())); - // } - @Override public void setExpansion(T expansion) { - valueSet.setExpansion((ValueSetExpansionComponent) expansion); + getValueSet().setExpansion((ValueSetExpansionComponent) expansion); } @SuppressWarnings("unchecked") @Override public ValueSetExpansionComponent getExpansion() { - return valueSet.getExpansion(); + return getValueSet().getExpansion(); } @SuppressWarnings("unchecked") @@ -276,7 +106,7 @@ public ValueSetExpansionComponent newExpansion() { @Override public List getValueSetIncludes() { - return valueSet.getCompose().getInclude().stream() + return getValueSet().getCompose().getInclude().stream() .map(i -> i.getValueSet()) .flatMap(Collection::stream) .map(c -> c.asStringValue()) @@ -286,23 +116,24 @@ public List getValueSetIncludes() { @Override public boolean hasSimpleCompose() { - return valueSet.hasCompose() - && !valueSet.getCompose().hasExclude() - && valueSet.getCompose().getInclude().stream() + return getValueSet().hasCompose() + && !getValueSet().getCompose().hasExclude() + && getValueSet().getCompose().getInclude().stream() .noneMatch( csc -> csc.hasFilter() || csc.hasValueSet() || !csc.hasSystem() || !csc.hasConcept()); } @Override public boolean hasGroupingCompose() { - return valueSet.hasCompose() - && !valueSet.getCompose().hasExclude() - && valueSet.getCompose().getInclude().stream().noneMatch(csc -> !csc.hasValueSet() || csc.hasFilter()); + return getValueSet().hasCompose() + && !getValueSet().getCompose().hasExclude() + && getValueSet().getCompose().getInclude().stream() + .noneMatch(csc -> !csc.hasValueSet() || csc.hasFilter()); } @Override public boolean hasNaiveParameter() { - return valueSet.getExpansion().getParameter().stream() + return getValueSet().getExpansion().getParameter().stream() .anyMatch(p -> p.getName().equals("naive")); } @@ -318,7 +149,7 @@ public ValueSet.ValueSetExpansionParameterComponent createNaiveParameter() { public void naiveExpand() { var expansion = newExpansion().addParameter(createNaiveParameter()); - for (var code : getCodesInCompose(FhirContext.forR4Cached(), valueSet)) { + for (var code : getCodesInCompose(fhirContext, getValueSet())) { expansion .addContains() .setCode(code.getCode()) @@ -326,6 +157,6 @@ public void naiveExpand() { .setVersion(code.getVersion()) .setDisplay(code.getDisplay()); } - valueSet.setExpansion(expansion); + getValueSet().setExpansion(expansion); } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/KnowledgeArtifactAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/KnowledgeArtifactAdapter.java index deee1feff..c7c3912d8 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/KnowledgeArtifactAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/KnowledgeArtifactAdapter.java @@ -25,79 +25,25 @@ public class KnowledgeArtifactAdapter extends ResourceAdapter public KnowledgeArtifactAdapter(IDomainResource resource) { super(resource); if (!(resource instanceof MetadataResource)) { - throw new IllegalArgumentException("resource argument is not a MetadataResource resource"); + throw new IllegalArgumentException( + "resource passed as resource argument is not a MetadataResource resource"); } - this.adaptedResource = (MetadataResource) resource; + adaptedResource = (MetadataResource) resource; } public KnowledgeArtifactAdapter(MetadataResource resource) { super(resource); - this.adaptedResource = resource; + adaptedResource = resource; } @Override public MetadataResource get() { - return this.adaptedResource; + return adaptedResource; } @Override public MetadataResource copy() { - return this.get().copy(); - } - - @Override - public String getUrl() { - return this.get().getUrl(); - } - - @Override - public boolean hasUrl() { - return this.get().hasUrl(); - } - - @Override - public void setUrl(String url) { - this.get().setUrl(url); - } - - @Override - public void setVersion(String version) { - this.get().setVersion(version); - } - - @Override - public String getVersion() { - return this.get().getVersion(); - } - - @Override - public boolean hasVersion() { - return this.get().hasVersion(); - } - - @Override - public String getName() { - return this.get().getName(); - } - - @Override - public void setName(String name) { - this.get().setName(name); - } - - @Override - public Date getApprovalDate() { - return this.get().getApprovalDate(); - } - - @Override - public Date getDate() { - return this.get().getDate(); - } - - @Override - public void setDate(Date approvalDate) { - this.get().setDate(approvalDate); + return get().copy(); } @Override @@ -105,27 +51,7 @@ public void setDateElement(IPrimitiveType date) { if (date != null && !(date instanceof DateTimeType)) { throw new UnprocessableEntityException("Date must be " + DateTimeType.class.getName()); } - this.get().setDateElement((DateTimeType) date); - } - - @Override - public void setApprovalDate(Date approvalDate) { - this.get().setApprovalDate(approvalDate); - } - - @Override - public Period getEffectivePeriod() { - return this.get().getEffectivePeriod(); - } - - @Override - public List getDependencies() { - return new ArrayList<>(); - } - - @Override - public String getPurpose() { - return this.get().getPurpose(); + org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter.super.setDateElement(date); } @Override @@ -133,18 +59,12 @@ public void setEffectivePeriod(ICompositeType effectivePeriod) { if (effectivePeriod != null && !(effectivePeriod instanceof Period)) { throw new UnprocessableEntityException("EffectivePeriod must be a valid " + Period.class.getName()); } - this.get().setEffectivePeriod((Period) effectivePeriod); + org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter.super.setEffectivePeriod(effectivePeriod); } @Override - public boolean hasRelatedArtifact() { - return this.get().hasRelatedArtifact(); - } - - @SuppressWarnings("unchecked") - @Override - public List getRelatedArtifact() { - return this.get().getRelatedArtifact(); + public List getDependencies() { + return new ArrayList<>(); } @SuppressWarnings("unchecked") @@ -156,25 +76,15 @@ public List getRelatedArtifactsOfType(String codeString) { } catch (FHIRException e) { throw new UnprocessableEntityException("Invalid related artifact code"); } - return this.getRelatedArtifact().stream() + return getRelatedArtifact().stream() + .map(ra -> (RelatedArtifact) ra) .filter(ra -> ra.getType() == type) .collect(Collectors.toList()); } @Override - public void setRelatedArtifact(List relatedArtifacts) - throws UnprocessableEntityException { - this.get() - .setRelatedArtifact(relatedArtifacts.stream() - .map(ra -> { - try { - return (RelatedArtifact) ra; - } catch (ClassCastException e) { - throw new UnprocessableEntityException( - "All related artifacts must be of type " + RelatedArtifact.class.getName()); - } - }) - .collect(Collectors.toList())); + public String getStatus() { + return get().getStatus() == null ? null : get().getStatus().toCode(); } @Override @@ -185,16 +95,21 @@ public void setStatus(String statusCodeString) { } catch (FHIRException e) { throw new UnprocessableEntityException("Invalid status code"); } - this.get().setStatus(status); + get().setStatus(status); } @Override - public String getStatus() { - return this.get().getStatus() == null ? null : this.get().getStatus().toCode(); - } - - @Override - public boolean getExperimental() { - return this.get().getExperimental(); + public void setRelatedArtifact(List relatedArtifacts) + throws UnprocessableEntityException { + org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter.super.setRelatedArtifact(relatedArtifacts.stream() + .map(ra -> { + try { + return (RelatedArtifact) ra; + } catch (ClassCastException e) { + throw new UnprocessableEntityException( + "All related artifacts must be of type " + RelatedArtifact.class.getName()); + } + }) + .collect(Collectors.toList())); } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/LibraryAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/LibraryAdapter.java index 4d9c6a722..6725b052a 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/LibraryAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/LibraryAdapter.java @@ -2,134 +2,82 @@ import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; import java.util.ArrayList; -import java.util.Date; import java.util.List; import java.util.stream.Collectors; -import org.hl7.fhir.exceptions.FHIRException; -import org.hl7.fhir.instance.model.api.IBaseExtension; -import org.hl7.fhir.instance.model.api.IBaseHasExtensions; import org.hl7.fhir.instance.model.api.ICompositeType; import org.hl7.fhir.instance.model.api.IDomainResource; -import org.hl7.fhir.instance.model.api.IPrimitiveType; import org.hl7.fhir.r5.model.Attachment; import org.hl7.fhir.r5.model.CodeableConcept; import org.hl7.fhir.r5.model.Coding; import org.hl7.fhir.r5.model.DataRequirement; -import org.hl7.fhir.r5.model.DateTimeType; -import org.hl7.fhir.r5.model.Enumerations.PublicationStatus; -import org.hl7.fhir.r5.model.Extension; import org.hl7.fhir.r5.model.Library; -import org.hl7.fhir.r5.model.Period; import org.hl7.fhir.r5.model.RelatedArtifact; -import org.hl7.fhir.r5.model.RelatedArtifact.RelatedArtifactType; import org.hl7.fhir.r5.model.UsageContext; import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; -public class LibraryAdapter extends ResourceAdapter implements org.opencds.cqf.fhir.utility.adapter.LibraryAdapter { - - private Library library; - +public class LibraryAdapter extends KnowledgeArtifactAdapter + implements org.opencds.cqf.fhir.utility.adapter.LibraryAdapter { public LibraryAdapter(IDomainResource library) { super(library); - if (!(library instanceof Library)) { throw new IllegalArgumentException("resource passed as library argument is not a Library resource"); } + } - this.library = (Library) library; + public LibraryAdapter(Library library) { + super(library); } protected Library getLibrary() { - return this.library; + return (Library) resource; } @Override public Library get() { - return this.library; + return (Library) resource; } @Override public Library copy() { - return this.get().copy(); - } - - @Override - public String getName() { - return this.getLibrary().getName(); - } - - @Override - public String getPurpose() { - return this.getLibrary().getPurpose(); - } - - @Override - public void setName(String name) { - this.getLibrary().setName(name); - } - - @Override - public String getUrl() { - return this.getLibrary().getUrl(); - } - - @Override - public boolean hasUrl() { - return this.getLibrary().hasUrl(); - } - - @Override - public void setUrl(String url) { - this.getLibrary().setUrl(url); - } - - @Override - public String getVersion() { - return this.getLibrary().getVersion(); - } - - @Override - public boolean hasVersion() { - return this.getLibrary().hasVersion(); - } - - @Override - public void setVersion(String version) { - this.getLibrary().setVersion(version); + return get().copy(); } @Override public boolean hasContent() { - return this.getLibrary().hasContent(); + return getLibrary().hasContent(); } @Override public List getContent() { - return this.getLibrary().getContent().stream().collect(Collectors.toList()); + return getLibrary().getContent().stream().collect(Collectors.toList()); } @Override public void setContent(List attachments) { - var castAttachments = attachments.stream().map(x -> (Attachment) x).collect(Collectors.toList()); - this.getLibrary().setContent(castAttachments); + List castAttachments = + attachments.stream().map(x -> (Attachment) x).collect(Collectors.toList()); + getLibrary().setContent(castAttachments); } @Override public Attachment addContent() { - return this.getLibrary().addContent(); + return getLibrary().addContent(); } @Override public List getDependencies() { List references = new ArrayList(); final String referenceSource = - this.hasVersion() ? this.getUrl() + "|" + this.getLibrary().getVersion() : this.getUrl(); - this.getRelatedArtifact().stream() + hasVersion() ? getUrl() + "|" + getLibrary().getVersion() : getUrl(); + + // relatedArtifact[].resource + getRelatedArtifact().stream() + .map(ra -> (RelatedArtifact) ra) .filter(ra -> ra.hasResource()) .map(ra -> DependencyInfo.convertRelatedArtifact(ra, referenceSource)) - .forEach(dep -> references.add(dep)); - this.getLibrary().getDataRequirement().stream().forEach(dr -> { + .forEach(ra -> references.add(ra)); + getLibrary().getDataRequirement().stream().forEach(dr -> { dr.getProfile().stream() .filter(profile -> profile.hasValue()) .forEach(profile -> references.add(new DependencyInfo( @@ -148,131 +96,22 @@ public List getDependencies() { return references; } - @Override - public Date getApprovalDate() { - return this.library.getApprovalDate(); - } - - @Override - public Date getDate() { - return this.getLibrary().getDate(); - } - - @Override - public void setDate(Date date) { - this.getLibrary().setDate(date); - } - - @Override - public void setDateElement(IPrimitiveType date) { - if (date != null && !(date instanceof DateTimeType)) { - throw new UnprocessableEntityException("Date must be " + DateTimeType.class.getName()); - } - this.getLibrary().setDateElement((DateTimeType) date); - } - - @Override - public Period getEffectivePeriod() { - return this.getLibrary().getEffectivePeriod(); - } - - @Override - public boolean hasRelatedArtifact() { - return this.getLibrary().hasRelatedArtifact(); - } - - @SuppressWarnings("unchecked") - @Override - public List getRelatedArtifact() { - return this.getLibrary().getRelatedArtifact(); - } - - @Override - public void setEffectivePeriod(ICompositeType effectivePeriod) { - if (effectivePeriod != null && !(effectivePeriod instanceof Period)) { - throw new UnprocessableEntityException("EffectivePeriod must be org.hl7.fhir.r5.model.Period"); - } - this.getLibrary().setEffectivePeriod((Period) effectivePeriod); - } - - @SuppressWarnings("unchecked") - @Override - public List getRelatedArtifactsOfType(String codeString) { - RelatedArtifactType type; - try { - type = RelatedArtifactType.fromCode(codeString); - } catch (FHIRException e) { - throw new UnprocessableEntityException("Invalid related artifact code"); - } - return this.getRelatedArtifact().stream() - .filter(ra -> ra.getType() == type) - .collect(Collectors.toList()); - } - - @Override - public void setApprovalDate(Date approvalDate) { - this.getLibrary().setApprovalDate(approvalDate); - } - @SuppressWarnings("unchecked") @Override public List getComponents() { - return this.getRelatedArtifactsOfType("composed-of"); - } - - @Override - public void setRelatedArtifact(List relatedArtifacts) - throws UnprocessableEntityException { - this.getLibrary() - .setRelatedArtifact(relatedArtifacts.stream() - .map(ra -> { - try { - return (RelatedArtifact) ra; - } catch (ClassCastException e) { - throw new UnprocessableEntityException( - "All related artifacts must be of type " + RelatedArtifact.class.getName()); - } - }) - .collect(Collectors.toList())); - } - - @Override - public void setStatus(String statusCodeString) { - PublicationStatus status; - try { - status = PublicationStatus.fromCode(statusCodeString); - } catch (FHIRException e) { - throw new UnprocessableEntityException("Invalid status code"); - } - this.getLibrary().setStatus(status); - } - - @Override - public String getStatus() { - return this.getLibrary().getStatus() == null - ? null - : this.getLibrary().getStatus().toCode(); - } - - @Override - public boolean getExperimental() { - return this.getLibrary().getExperimental(); - } - - @Override - public void setExtension(List> extensions) { - this.get().setExtension(extensions.stream().map(e -> (Extension) e).collect(Collectors.toList())); + return getRelatedArtifactsOfType("composed-of"); } @Override public ICompositeType getType() { - return library.getType(); + return getLibrary().getType(); } @Override public LibraryAdapter setType(String type) { if (LIBRARY_TYPES.contains(type)) { - library.setType(new CodeableConcept(new Coding("http://hl7.org/fhir/ValueSet/library-type", type, ""))); + getLibrary() + .setType(new CodeableConcept(new Coding("http://hl7.org/fhir/ValueSet/library-type", type, ""))); } else { throw new UnprocessableEntityException("Invalid type: {}", type); } @@ -281,24 +120,26 @@ public LibraryAdapter setType(String type) { @Override public List getDataRequirement() { - return library.getDataRequirement(); + return getLibrary().getDataRequirement(); } @Override public LibraryAdapter addDataRequirement(ICompositeType dataRequirement) { - library.addDataRequirement((DataRequirement) dataRequirement); + getLibrary().addDataRequirement((DataRequirement) dataRequirement); return this; } @Override public LibraryAdapter setDataRequirements(List dataRequirements) { - library.setDataRequirement( - dataRequirements.stream().map(dr -> (DataRequirement) dr).collect(Collectors.toList())); + getLibrary() + .setDataRequirement(dataRequirements.stream() + .map(dr -> (DataRequirement) dr) + .collect(Collectors.toList())); return this; } @Override public List getUseContext() { - return library.getUseContext(); + return getLibrary().getUseContext(); } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/PlanDefinitionAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/PlanDefinitionAdapter.java index cddea87d3..1b95f5f91 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/PlanDefinitionAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/PlanDefinitionAdapter.java @@ -1,121 +1,49 @@ package org.opencds.cqf.fhir.utility.adapter.r5; -import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; import java.util.ArrayList; -import java.util.Date; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; -import org.hl7.fhir.exceptions.FHIRException; -import org.hl7.fhir.instance.model.api.IBaseExtension; -import org.hl7.fhir.instance.model.api.IBaseHasExtensions; -import org.hl7.fhir.instance.model.api.ICompositeType; import org.hl7.fhir.instance.model.api.IDomainResource; -import org.hl7.fhir.instance.model.api.IIdType; -import org.hl7.fhir.instance.model.api.IPrimitiveType; import org.hl7.fhir.r5.model.CanonicalType; -import org.hl7.fhir.r5.model.DateTimeType; -import org.hl7.fhir.r5.model.Enumerations.PublicationStatus; -import org.hl7.fhir.r5.model.Extension; -import org.hl7.fhir.r5.model.Period; import org.hl7.fhir.r5.model.PlanDefinition; -import org.hl7.fhir.r5.model.RelatedArtifact; -import org.hl7.fhir.r5.model.RelatedArtifact.RelatedArtifactType; import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; -import org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter; -public class PlanDefinitionAdapter extends ResourceAdapter implements KnowledgeArtifactAdapter { - - private PlanDefinition planDefinition; +public class PlanDefinitionAdapter extends KnowledgeArtifactAdapter { public PlanDefinitionAdapter(IDomainResource planDefinition) { super(planDefinition); - if (!(planDefinition instanceof PlanDefinition)) { throw new IllegalArgumentException( "resource passed as planDefinition argument is not a PlanDefinition resource"); } + } - this.planDefinition = (PlanDefinition) planDefinition; + public PlanDefinitionAdapter(PlanDefinition planDefinition) { + super(planDefinition); } protected PlanDefinition getPlanDefinition() { - return this.planDefinition; + return (PlanDefinition) resource; } @Override public PlanDefinition get() { - return this.planDefinition; + return getPlanDefinition(); } @Override public PlanDefinition copy() { - return this.get().copy(); - } - - @Override - public IIdType getId() { - return this.getPlanDefinition().getIdElement(); - } - - @Override - public void setId(IIdType id) { - this.getPlanDefinition().setId(id); - } - - @Override - public String getName() { - return this.getPlanDefinition().getName(); - } - - @Override - public String getPurpose() { - return this.getPlanDefinition().getPurpose(); - } - - @Override - public void setName(String name) { - this.getPlanDefinition().setName(name); - } - - @Override - public String getUrl() { - return this.getPlanDefinition().getUrl(); - } - - @Override - public boolean hasUrl() { - return this.getPlanDefinition().hasUrl(); - } - - @Override - public void setUrl(String url) { - this.getPlanDefinition().setUrl(url); - } - - @Override - public String getVersion() { - return this.getPlanDefinition().getVersion(); - } - - @Override - public boolean hasVersion() { - return this.getPlanDefinition().hasVersion(); - } - - @Override - public void setVersion(String version) { - this.getPlanDefinition().setVersion(version); + return get().copy(); } @Override public List getDependencies() { List references = new ArrayList<>(); - final String referenceSource = this.getPlanDefinition().hasVersion() - ? this.getPlanDefinition().getUrl() + "|" - + this.getPlanDefinition().getVersion() - : this.getPlanDefinition().getUrl(); + final String referenceSource = getPlanDefinition().hasVersion() + ? getPlanDefinition().getUrl() + "|" + getPlanDefinition().getVersion() + : getPlanDefinition().getUrl(); /* relatedArtifact[].resource library[] @@ -132,20 +60,20 @@ public List getDependencies() { */ // relatedArtifact[].resource - references.addAll(this.getRelatedArtifact().stream() + references.addAll(getRelatedArtifact().stream() .map(ra -> DependencyInfo.convertRelatedArtifact(ra, referenceSource)) .collect(Collectors.toList())); // library[] - List libraries = this.getPlanDefinition().getLibrary(); + List libraries = getPlanDefinition().getLibrary(); for (CanonicalType ct : libraries) { DependencyInfo dependency = new DependencyInfo( referenceSource, ct.getValue(), ct.getExtension(), (reference) -> ct.setValue(reference)); references.add(dependency); } // action[] - this.planDefinition.getAction().forEach(action -> getDependenciesOfAction(action, references, referenceSource)); - this.getPlanDefinition().getExtension().stream() + getPlanDefinition().getAction().forEach(action -> getDependenciesOfAction(action, references, referenceSource)); + getPlanDefinition().getExtension().stream() .filter(ext -> ext.getUrl().contains("cpg-partOf")) .filter(ext -> ext.hasValue()) .findAny() @@ -247,106 +175,4 @@ private void getDependenciesOfAction( } action.getAction().forEach(nestedAction -> getDependenciesOfAction(nestedAction, references, referenceSource)); } - - @Override - public Date getApprovalDate() { - return this.getPlanDefinition().getApprovalDate(); - } - - @Override - public Date getDate() { - return this.getPlanDefinition().getDate(); - } - - @Override - public void setDate(Date date) { - this.getPlanDefinition().setDate(date); - } - - @Override - public void setDateElement(IPrimitiveType date) { - if (date != null && !(date instanceof DateTimeType)) { - throw new UnprocessableEntityException("Date must be " + DateTimeType.class.getName()); - } - this.getPlanDefinition().setDateElement((DateTimeType) date); - } - - @Override - public Period getEffectivePeriod() { - return this.getPlanDefinition().getEffectivePeriod(); - } - - @Override - public void setApprovalDate(Date date) { - this.getPlanDefinition().setApprovalDate(date); - } - - @Override - public boolean hasRelatedArtifact() { - return this.getPlanDefinition().hasRelatedArtifact(); - } - - @SuppressWarnings("unchecked") - @Override - public List getRelatedArtifact() { - return this.getPlanDefinition().getRelatedArtifact(); - } - - @Override - public void setRelatedArtifact(List relatedArtifacts) { - this.getPlanDefinition() - .setRelatedArtifact(relatedArtifacts.stream() - .map(ra -> (RelatedArtifact) ra) - .collect(Collectors.toList())); - } - - @SuppressWarnings("unchecked") - @Override - public List getRelatedArtifactsOfType(String codeString) { - RelatedArtifactType type; - try { - type = RelatedArtifactType.fromCode(codeString); - } catch (FHIRException e) { - throw new UnprocessableEntityException("Invalid related artifact code"); - } - return this.getRelatedArtifact().stream() - .filter(ra -> ra.getType() == type) - .collect(Collectors.toList()); - } - - @Override - public void setEffectivePeriod(ICompositeType effectivePeriod) { - if (effectivePeriod != null && !(effectivePeriod instanceof Period)) { - throw new UnprocessableEntityException("EffectivePeriod must be org.hl7.fhir.r5.model.Period"); - } - this.getPlanDefinition().setEffectivePeriod((Period) effectivePeriod); - } - - @Override - public void setStatus(String statusCodeString) { - PublicationStatus status; - try { - status = PublicationStatus.fromCode(statusCodeString); - } catch (FHIRException e) { - throw new UnprocessableEntityException("Invalid status code"); - } - this.getPlanDefinition().setStatus(status); - } - - @Override - public String getStatus() { - return this.getPlanDefinition().getStatus() == null - ? null - : this.getPlanDefinition().getStatus().toCode(); - } - - @Override - public boolean getExperimental() { - return this.getPlanDefinition().getExperimental(); - } - - @Override - public void setExtension(List> extensions) { - this.get().setExtension(extensions.stream().map(e -> (Extension) e).collect(Collectors.toList())); - } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapter.java index 274025d44..b4b91c522 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapter.java @@ -11,35 +11,38 @@ public class QuestionnaireAdapter extends KnowledgeArtifactAdapter { - private Questionnaire questionnaire; - public QuestionnaireAdapter(IDomainResource questionnaire) { super(questionnaire); - if (!(questionnaire instanceof Questionnaire)) { throw new IllegalArgumentException( "resource passed as questionnaire argument is not a Questionnaire resource"); } - - this.questionnaire = (Questionnaire) questionnaire; } public QuestionnaireAdapter(Questionnaire questionnaire) { super(questionnaire); - this.questionnaire = questionnaire; } protected Questionnaire getQuestionnaire() { - return this.questionnaire; + return (Questionnaire) resource; + } + + @Override + public Questionnaire get() { + return getQuestionnaire(); + } + + @Override + public Questionnaire copy() { + return get().copy(); } @Override public List getDependencies() { List references = new ArrayList<>(); - final String referenceSource = this.getQuestionnaire().hasVersion() - ? this.getQuestionnaire().getUrl() + "|" - + this.getQuestionnaire().getVersion() - : this.getQuestionnaire().getUrl(); + final String referenceSource = getQuestionnaire().hasVersion() + ? getQuestionnaire().getUrl() + "|" + getQuestionnaire().getVersion() + : getQuestionnaire().getUrl(); /* derivedFrom item[]..definition // NOTE: This is not a simple canonical, it will have a fragment to identify the specific element @@ -61,7 +64,7 @@ public List getDependencies() { item[]..extension[sdc-questionnaire-subQuestionnaire] */ - var libraryExtensions = questionnaire.getExtensionsByUrl(Constants.CQF_LIBRARY); + var libraryExtensions = getQuestionnaire().getExtensionsByUrl(Constants.CQF_LIBRARY); for (var libraryExt : libraryExtensions) { DependencyInfo dependency = new DependencyInfo( referenceSource, diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapter.java index 1e14d8653..cf581a979 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapter.java @@ -3,7 +3,6 @@ import static java.util.Optional.ofNullable; import ca.uhn.fhir.context.FhirVersionEnum; -import java.util.ArrayList; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; @@ -15,10 +14,12 @@ import org.hl7.fhir.r5.model.DomainResource; import org.hl7.fhir.r5.model.Extension; import org.hl7.fhir.r5.model.Resource; +import org.opencds.cqf.fhir.utility.adapter.BaseResourceAdapter; -class ResourceAdapter implements org.opencds.cqf.fhir.utility.adapter.ResourceAdapter { +class ResourceAdapter extends BaseResourceAdapter { public ResourceAdapter(IBaseResource resource) { + super(resource); if (resource == null) { throw new IllegalArgumentException("resource can not be null"); } @@ -26,14 +27,10 @@ public ResourceAdapter(IBaseResource resource) { if (!resource.getStructureFhirVersionEnum().equals(FhirVersionEnum.R5)) { throw new IllegalArgumentException("resource is incorrect fhir version for this adapter"); } - - this.resource = (Resource) resource; } - private Resource resource; - protected Resource getResource() { - return this.resource; + return (Resource) resource; } protected boolean isDomainResource() { @@ -45,22 +42,22 @@ protected Optional getDomainResource() { } public IBaseResource get() { - return this.resource; + return resource; } @Override public IBase setProperty(String name, IBase value) throws FHIRException { - return this.getResource().setProperty(name, (Base) value); + return getResource().setProperty(name, (Base) value); } @Override public IBase addChild(String name) throws FHIRException { - return this.getResource().addChild(name); + return getResource().addChild(name); } @Override public IBase getSingleProperty(String name) throws FHIRException { - IBase[] values = this.getProperty(name, true); + IBase[] values = getProperty(name, true); if (values == null || values.length == 0) { return null; @@ -75,42 +72,42 @@ public IBase getSingleProperty(String name) throws FHIRException { @Override public IBase[] getProperty(String name) throws FHIRException { - return this.getProperty(name, true); + return getProperty(name, true); } @Override public IBase[] getProperty(String name, boolean checkValid) throws FHIRException { - return this.getResource().getProperty(name.hashCode(), name, checkValid); + return getResource().getProperty(name.hashCode(), name, checkValid); } @Override public IBase makeProperty(String name) throws FHIRException { - return this.getResource().makeProperty(name.hashCode(), name); + return getResource().makeProperty(name.hashCode(), name); } @Override public String[] getTypesForProperty(String name) throws FHIRException { - return this.getResource().getTypesForProperty(name.hashCode(), name); + return getResource().getTypesForProperty(name.hashCode(), name); } @Override public IBaseResource copy() { - return this.getResource().copy(); + return getResource().copy(); } @Override public void copyValues(IBaseResource dst) { - this.getResource().copyValues((Resource) dst); + getResource().copyValues((Resource) dst); } @Override public boolean equalsDeep(IBase other) { - return this.getResource().equalsDeep((Base) other); + return getResource().equalsDeep((Base) other); } @Override public boolean equalsShallow(IBase other) { - return this.getResource().equalsShallow((Base) other); + return getResource().equalsShallow((Base) other); } @Override @@ -124,34 +121,4 @@ public void setExtension(List> extensions) { public > void addExtension(T extension) { getDomainResource().ifPresent(r -> r.addExtension((Extension) extension)); } - - @Override - public List getExtension() { - return isDomainResource() ? getDomainResource().get().getExtension() : new ArrayList<>(); - } - - @SuppressWarnings("unchecked") - @Override - public Extension getExtensionByUrl(String url) { - return isDomainResource() - ? getDomainResource().get().getExtension().stream() - .filter(e -> e.getUrl().equals(url)) - .findFirst() - .orElse(null) - : null; - } - - @Override - public List getExtensionsByUrl(String url) { - return isDomainResource() - ? getDomainResource().get().getExtension().stream() - .filter(e -> e.getUrl().equals(url)) - .collect(Collectors.toList()) - : new ArrayList<>(); - } - - @Override - public List getContained() { - return isDomainResource() ? getDomainResource().get().getContained() : new ArrayList<>(); - } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapter.java index b4573587a..9610a0e6f 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapter.java @@ -6,7 +6,6 @@ import java.util.List; import java.util.stream.Collectors; import org.hl7.fhir.exceptions.FHIRException; -import org.hl7.fhir.instance.model.api.IBaseExtension; import org.hl7.fhir.instance.model.api.IBaseHasExtensions; import org.hl7.fhir.instance.model.api.ICompositeType; import org.hl7.fhir.instance.model.api.IDomainResource; @@ -14,7 +13,6 @@ import org.hl7.fhir.r5.model.CanonicalType; import org.hl7.fhir.r5.model.DateTimeType; import org.hl7.fhir.r5.model.Enumerations.PublicationStatus; -import org.hl7.fhir.r5.model.Extension; import org.hl7.fhir.r5.model.Period; import org.hl7.fhir.r5.model.RelatedArtifact; import org.hl7.fhir.r5.model.RelatedArtifact.RelatedArtifactType; @@ -26,33 +24,29 @@ public class StructureDefinitionAdapter extends ResourceAdapter implements KnowledgeArtifactAdapter { - private StructureDefinition structureDefinition; - public StructureDefinitionAdapter(IDomainResource structureDefinition) { super(structureDefinition); if (!(structureDefinition instanceof StructureDefinition)) { throw new IllegalArgumentException( "resource passed as planDefinition argument is not a StructureDefinition resource"); } - this.structureDefinition = (StructureDefinition) structureDefinition; } public StructureDefinitionAdapter(StructureDefinition structureDefinition) { super(structureDefinition); - this.structureDefinition = structureDefinition; } protected StructureDefinition getStructureDefinition() { - return this.structureDefinition; + return (StructureDefinition) resource; } @Override public List getDependencies() { List references = new ArrayList<>(); - final String referenceSource = this.getStructureDefinition().hasVersion() - ? this.getStructureDefinition().getUrl() + "|" - + this.getStructureDefinition().getVersion() - : this.getStructureDefinition().getUrl(); + final String referenceSource = getStructureDefinition().hasVersion() + ? getStructureDefinition().getUrl() + "|" + + getStructureDefinition().getVersion() + : getStructureDefinition().getUrl(); /* extension[].url modifierExtension[].url @@ -68,7 +62,7 @@ public List getDependencies() { extension[cpg-featureExpression].reference */ - var libraryExtensions = structureDefinition.getExtensionsByUrl(Constants.CQF_LIBRARY); + var libraryExtensions = getStructureDefinition().getExtensionsByUrl(Constants.CQF_LIBRARY); for (var libraryExt : libraryExtensions) { DependencyInfo dependency = new DependencyInfo( referenceSource, @@ -83,52 +77,52 @@ public List getDependencies() { @Override public StructureDefinition get() { - return this.structureDefinition; + return getStructureDefinition(); } @Override public StructureDefinition copy() { - return this.get().copy(); + return get().copy(); } @Override public String getUrl() { - return this.get().getUrl(); + return get().getUrl(); } @Override public boolean hasUrl() { - return this.get().hasUrl(); + return get().hasUrl(); } @Override public void setUrl(String url) { - this.get().setUrl(url); + get().setUrl(url); } @Override public void setVersion(String version) { - this.get().setVersion(version); + get().setVersion(version); } @Override public String getVersion() { - return this.get().getVersion(); + return get().getVersion(); } @Override public boolean hasVersion() { - return this.get().hasVersion(); + return get().hasVersion(); } @Override public String getName() { - return this.get().getName(); + return get().getName(); } @Override public void setName(String name) { - this.get().setName(name); + get().setName(name); } @Override @@ -138,12 +132,12 @@ public Date getApprovalDate() { @Override public Date getDate() { - return this.get().getDate(); + return get().getDate(); } @Override public void setDate(Date approvalDate) { - this.get().setDate(approvalDate); + get().setDate(approvalDate); } @Override @@ -151,7 +145,7 @@ public void setDateElement(IPrimitiveType date) { if (date != null && !(date instanceof DateTimeType)) { throw new UnprocessableEntityException("Date must be " + DateTimeType.class.getName()); } - this.get().setDateElement((DateTimeType) date); + get().setDateElement((DateTimeType) date); } @Override @@ -166,7 +160,7 @@ public Period getEffectivePeriod() { @Override public String getPurpose() { - return this.get().getPurpose(); + return get().getPurpose(); } @Override @@ -197,9 +191,7 @@ public List getRelatedArtifactsOfType(String codeString) { } catch (FHIRException e) { throw new UnprocessableEntityException("Invalid related artifact code"); } - return this.getRelatedArtifact().stream() - .filter(ra -> ra.getType() == type) - .collect(Collectors.toList()); + return getRelatedArtifact().stream().filter(ra -> ra.getType() == type).collect(Collectors.toList()); } @Override @@ -216,21 +208,16 @@ public void setStatus(String statusCodeString) { } catch (FHIRException e) { throw new UnprocessableEntityException("Invalid status code"); } - this.get().setStatus(status); + get().setStatus(status); } @Override public String getStatus() { - return this.get().getStatus() == null ? null : this.get().getStatus().toCode(); + return get().getStatus() == null ? null : get().getStatus().toCode(); } @Override public boolean getExperimental() { - return this.get().getExperimental(); - } - - @Override - public void setExtension(List> extensions) { - this.get().setExtension(extensions.stream().map(e -> (Extension) e).collect(Collectors.toList())); + return get().getExperimental(); } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/ValueSetAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/ValueSetAdapter.java index ce719d531..7d3ca3134 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/ValueSetAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/ValueSetAdapter.java @@ -2,8 +2,6 @@ import static org.opencds.cqf.fhir.utility.ValueSets.getCodesInCompose; -import ca.uhn.fhir.context.FhirContext; -import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; import java.time.Instant; import java.util.ArrayList; import java.util.Collection; @@ -11,101 +9,48 @@ import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; -import org.hl7.fhir.exceptions.FHIRException; import org.hl7.fhir.instance.model.api.IBaseBackboneElement; -import org.hl7.fhir.instance.model.api.IBaseHasExtensions; -import org.hl7.fhir.instance.model.api.ICompositeType; -import org.hl7.fhir.instance.model.api.IPrimitiveType; +import org.hl7.fhir.instance.model.api.IDomainResource; import org.hl7.fhir.r5.model.BooleanType; -import org.hl7.fhir.r5.model.DateTimeType; -import org.hl7.fhir.r5.model.Enumerations.PublicationStatus; -import org.hl7.fhir.r5.model.Period; -import org.hl7.fhir.r5.model.RelatedArtifact; -import org.hl7.fhir.r5.model.RelatedArtifact.RelatedArtifactType; import org.hl7.fhir.r5.model.ValueSet; import org.hl7.fhir.r5.model.ValueSet.ValueSetExpansionComponent; import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; -class ValueSetAdapter extends ResourceAdapter implements org.opencds.cqf.fhir.utility.adapter.ValueSetAdapter { +public class ValueSetAdapter extends KnowledgeArtifactAdapter + implements org.opencds.cqf.fhir.utility.adapter.ValueSetAdapter { - private ValueSet valueSet; - - public ValueSetAdapter(ValueSet valueSet) { + public ValueSetAdapter(IDomainResource valueSet) { super(valueSet); - if (!(valueSet instanceof ValueSet)) { throw new IllegalArgumentException("resource passed as valueSet argument is not a ValueSet resource"); } + } - this.valueSet = valueSet; + public ValueSetAdapter(ValueSet valueSet) { + super(valueSet); } protected ValueSet getValueSet() { - return this.valueSet; + return (ValueSet) resource; } @Override public ValueSet get() { - return this.valueSet; + return (ValueSet) resource; } @Override public ValueSet copy() { - return this.get().copy(); - } - - @Override - public String getName() { - return this.getValueSet().getName(); - } - - @Override - public String getPurpose() { - return this.getValueSet().getPurpose(); - } - - @Override - public void setName(String name) { - this.getValueSet().setName(name); - } - - @Override - public boolean hasUrl() { - return this.getValueSet().hasUrl(); - } - - @Override - public String getUrl() { - return this.getValueSet().getUrl(); - } - - @Override - public void setUrl(String url) { - this.getValueSet().setUrl(url); - } - - @Override - public String getVersion() { - return this.getValueSet().getVersion(); - } - - @Override - public boolean hasVersion() { - return this.getValueSet().hasVersion(); - } - - @Override - public void setVersion(String version) { - this.getValueSet().setVersion(version); + return get().copy(); } @Override public List getDependencies() { List references = new ArrayList<>(); - final String referenceSource = this.getValueSet().hasVersion() - ? this.getValueSet().getUrl() + "|" + this.getValueSet().getVersion() - : this.getValueSet().getUrl(); + final String referenceSource = getValueSet().hasVersion() + ? getValueSet().getUrl() + "|" + getValueSet().getVersion() + : getValueSet().getUrl(); /* compose.include[].valueSet @@ -114,8 +59,8 @@ public List getDependencies() { compose.exclude[].system */ Stream.concat( - this.getValueSet().getCompose().getInclude().stream(), - this.getValueSet().getCompose().getExclude().stream()) + getValueSet().getCompose().getInclude().stream(), + getValueSet().getCompose().getExclude().stream()) .forEach(component -> { if (component.hasValueSet()) { component.getValueSet().forEach(ct -> { @@ -139,134 +84,28 @@ public List getDependencies() { return references; } - @Override - public Date getApprovalDate() { - return this.getValueSet().getApprovalDate(); - } - - @Override - public Date getDate() { - return this.getValueSet().getDate(); - } - - @Override - public void setDate(Date date) { - this.getValueSet().setDate(date); - } - - @Override - public void setDateElement(IPrimitiveType date) throws UnprocessableEntityException { - if (date != null && !(date instanceof DateTimeType)) { - throw new UnprocessableEntityException("Date must be " + DateTimeType.class.getName()); - } - this.getValueSet().setDateElement((DateTimeType) date); - } - - @Override - public Period getEffectivePeriod() { - return this.getValueSet().getEffectivePeriod(); - } - - @Override - public boolean hasRelatedArtifact() { - return this.getValueSet().hasRelatedArtifact(); - } - - @SuppressWarnings("unchecked") - @Override - public List getRelatedArtifact() { - return this.getValueSet().getRelatedArtifact(); - } - - @Override - public void setRelatedArtifact(List relatedArtifacts) { - this.getValueSet() - .setRelatedArtifact(relatedArtifacts.stream() - .map(ra -> (RelatedArtifact) ra) - .collect(Collectors.toList())); - } - - @SuppressWarnings("unchecked") - @Override - public List getComponents() { - return this.getRelatedArtifactsOfType("composed-of"); - } - - @Override - public void setApprovalDate(Date approvalDate) { - this.getValueSet().setApprovalDate(approvalDate); - } - - @Override - public void setEffectivePeriod(ICompositeType effectivePeriod) { - if (effectivePeriod != null && !(effectivePeriod instanceof Period)) { - throw new UnprocessableEntityException("EffectivePeriod must be org.hl7.fhir.r5.model.Period"); - } - this.getValueSet().setEffectivePeriod((Period) effectivePeriod); - } - - @SuppressWarnings("unchecked") - @Override - public List getRelatedArtifactsOfType(String codeString) { - RelatedArtifactType type; - try { - type = RelatedArtifactType.fromCode(codeString); - } catch (FHIRException e) { - throw new UnprocessableEntityException("Invalid related artifact code"); - } - return this.getRelatedArtifact().stream() - .filter(ra -> ra.getType() == type) - .collect(Collectors.toList()); - } - - @Override - public void setStatus(String statusCodeString) { - PublicationStatus status; - try { - status = PublicationStatus.fromCode(statusCodeString); - } catch (FHIRException e) { - throw new UnprocessableEntityException("Invalid status code"); - } - this.getValueSet().setStatus(status); - } - - @Override - public String getStatus() { - return this.getValueSet().getStatus() == null - ? null - : this.getValueSet().getStatus().toCode(); - } - - @Override - public boolean getExperimental() { - return this.getValueSet().getExperimental(); - } - - // @Override - // public void setExtension(List> extensions) { - // this.get().setExtension(extensions.stream().map(e -> (Extension) e).collect(Collectors.toList())); - // } - @Override public void setExpansion(T expansion) { - valueSet.setExpansion((ValueSetExpansionComponent) expansion); + getValueSet().setExpansion((ValueSetExpansionComponent) expansion); } @SuppressWarnings("unchecked") @Override public ValueSetExpansionComponent getExpansion() { - return valueSet.getExpansion(); + return getValueSet().getExpansion(); } @SuppressWarnings("unchecked") @Override public ValueSetExpansionComponent newExpansion() { - return new ValueSetExpansionComponent(Date.from(Instant.now())); + var expansion = new ValueSet.ValueSetExpansionComponent(Date.from(Instant.now())); + expansion.getContains(); + return expansion; } @Override public List getValueSetIncludes() { - return valueSet.getCompose().getInclude().stream() + return getValueSet().getCompose().getInclude().stream() .map(i -> i.getValueSet()) .flatMap(Collection::stream) .map(c -> c.asStringValue()) @@ -276,23 +115,24 @@ public List getValueSetIncludes() { @Override public boolean hasSimpleCompose() { - return valueSet.hasCompose() - && !valueSet.getCompose().hasExclude() - && valueSet.getCompose().getInclude().stream() + return getValueSet().hasCompose() + && !getValueSet().getCompose().hasExclude() + && getValueSet().getCompose().getInclude().stream() .noneMatch( csc -> csc.hasFilter() || csc.hasValueSet() || !csc.hasSystem() || !csc.hasConcept()); } @Override public boolean hasGroupingCompose() { - return valueSet.hasCompose() - && !valueSet.getCompose().hasExclude() - && valueSet.getCompose().getInclude().stream().noneMatch(csc -> !csc.hasValueSet() || csc.hasFilter()); + return getValueSet().hasCompose() + && !getValueSet().getCompose().hasExclude() + && getValueSet().getCompose().getInclude().stream() + .noneMatch(csc -> !csc.hasValueSet() || csc.hasFilter()); } @Override public boolean hasNaiveParameter() { - return valueSet.getExpansion().getParameter().stream() + return getValueSet().getExpansion().getParameter().stream() .anyMatch(p -> p.getName().equals("naive")); } @@ -308,7 +148,7 @@ public ValueSet.ValueSetExpansionParameterComponent createNaiveParameter() { public void naiveExpand() { var expansion = newExpansion().addParameter(createNaiveParameter()); - for (var code : getCodesInCompose(FhirContext.forR5Cached(), valueSet)) { + for (var code : getCodesInCompose(fhirContext, getValueSet())) { expansion .addContains() .setCode(code.getCode()) @@ -316,6 +156,6 @@ public void naiveExpand() { .setVersion(code.getVersion()) .setDisplay(code.getDisplay()); } - valueSet.setExpansion(expansion); + getValueSet().setExpansion(expansion); } } diff --git a/cqf-fhir-cql/src/main/java/org/opencds/cqf/fhir/cql/engine/model/DynamicModelResolver.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/model/DynamicModelResolver.java similarity index 99% rename from cqf-fhir-cql/src/main/java/org/opencds/cqf/fhir/cql/engine/model/DynamicModelResolver.java rename to cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/model/DynamicModelResolver.java index 5fc3c3cc3..20277030f 100644 --- a/cqf-fhir-cql/src/main/java/org/opencds/cqf/fhir/cql/engine/model/DynamicModelResolver.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/model/DynamicModelResolver.java @@ -1,4 +1,4 @@ -package org.opencds.cqf.fhir.cql.engine.model; +package org.opencds.cqf.fhir.utility.model; import ca.uhn.fhir.context.BaseRuntimeElementCompositeDefinition; import ca.uhn.fhir.context.FhirContext; diff --git a/cqf-fhir-cql/src/main/java/org/opencds/cqf/fhir/cql/engine/model/FhirModelResolverCache.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/model/FhirModelResolverCache.java similarity index 97% rename from cqf-fhir-cql/src/main/java/org/opencds/cqf/fhir/cql/engine/model/FhirModelResolverCache.java rename to cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/model/FhirModelResolverCache.java index eb2146937..66cd41590 100644 --- a/cqf-fhir-cql/src/main/java/org/opencds/cqf/fhir/cql/engine/model/FhirModelResolverCache.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/model/FhirModelResolverCache.java @@ -1,4 +1,4 @@ -package org.opencds.cqf.fhir.cql.engine.model; +package org.opencds.cqf.fhir.utility.model; import static java.util.Objects.requireNonNull; diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/ReleaseVisitor.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/ReleaseVisitor.java index 1bcdaa8c9..a6b87ca1d 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/ReleaseVisitor.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/ReleaseVisitor.java @@ -17,7 +17,6 @@ import org.apache.commons.lang3.StringUtils; import org.hl7.fhir.instance.model.api.IBase; import org.hl7.fhir.instance.model.api.IBaseBackboneElement; -import org.hl7.fhir.instance.model.api.IBaseExtension; import org.hl7.fhir.instance.model.api.IBaseParameters; import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.instance.model.api.ICompositeType; @@ -37,7 +36,6 @@ public class ReleaseVisitor implements KnowledgeArtifactVisitor { private Logger log = LoggerFactory.getLogger(ReleaseVisitor.class); - @SuppressWarnings("unchecked") @Override public IBase visit( KnowledgeArtifactAdapter rootAdapter, Repository repository, IBaseParameters operationParameters) { @@ -58,7 +56,7 @@ public IBase visit( Optional versionBehavior = VisitorHelper.getParameter( "versionBehavior", operationParameters, IPrimitiveType.class) .map(t -> (String) t.getValue()); - Optional requireNonExpermimental = VisitorHelper.getParameter( + Optional requireNonExperimental = VisitorHelper.getParameter( "requireNonExperimental", operationParameters, IPrimitiveType.class) .map(t -> (String) t.getValue()); checkReleaseVersion(version, versionBehavior); @@ -76,14 +74,14 @@ public IBase visit( var rootEffectivePeriod = rootAdapter.getEffectivePeriod(); // if the root artifact is experimental then we don't need to check for experimental children if (rootAdapter.getExperimental()) { - requireNonExpermimental = Optional.of("none"); + requireNonExperimental = Optional.of("none"); } var releasedResources = internalRelease( rootAdapter, releaseVersion, rootEffectivePeriod, latestFromTxServer.orElse(false), - requireNonExpermimental, + requireNonExperimental, repository); updateReleaseLabel(rootLibrary, releaseLabel); var rootArtifactOriginalDependencies = new ArrayList(rootAdapter.getDependencies()); @@ -176,8 +174,8 @@ public IBase visit( .equalsIgnoreCase("depends-on")) .findFirst() .ifPresent(dep -> { - ((List>) resolvedRelatedArtifact.getExtension()) - .addAll((List>) dep.getExtension()); + // ((List>) resolvedRelatedArtifact.getExtension()) + // .addAll((List>) dep.getExtension()); originalDependenciesWithExtensions.removeIf( ra -> ra.getReference().equals(relatedArtifactReference)); }); @@ -208,7 +206,7 @@ private List internalRelease( artifactAdapter.setStatus("active"); artifactAdapter.setVersion(version); // Step 2: propagate effectivePeriod if it doesn't exist - propagageEffectivePeriod(rootEffectivePeriod, artifactAdapter); + propagateEffectivePeriod(rootEffectivePeriod, artifactAdapter); resourcesToUpdate.add(artifactAdapter.get()); var ownedRelatedArtifacts = artifactAdapter.getOwnedRelatedArtifacts(); @@ -288,16 +286,16 @@ private void checkNonExperimental( } } - private void propagageEffectivePeriod( + private void propagateEffectivePeriod( ICompositeType rootEffectivePeriod, KnowledgeArtifactAdapter artifactAdapter) { if (rootEffectivePeriod instanceof org.hl7.fhir.dstu3.model.Period) { - org.opencds.cqf.fhir.utility.visitor.dstu3.ReleaseVisitor.propagageEffectivePeriod( + org.opencds.cqf.fhir.utility.visitor.dstu3.ReleaseVisitor.propagateEffectivePeriod( (org.hl7.fhir.dstu3.model.Period) rootEffectivePeriod, artifactAdapter); } else if (rootEffectivePeriod instanceof org.hl7.fhir.r4.model.Period) { - org.opencds.cqf.fhir.utility.visitor.r4.ReleaseVisitor.propagageEffectivePeriod( + org.opencds.cqf.fhir.utility.visitor.r4.ReleaseVisitor.propagateEffectivePeriod( (org.hl7.fhir.r4.model.Period) rootEffectivePeriod, artifactAdapter); } else if (rootEffectivePeriod instanceof org.hl7.fhir.r5.model.Period) { - org.opencds.cqf.fhir.utility.visitor.r5.ReleaseVisitor.propagageEffectivePeriod( + org.opencds.cqf.fhir.utility.visitor.r5.ReleaseVisitor.propagateEffectivePeriod( (org.hl7.fhir.r5.model.Period) rootEffectivePeriod, artifactAdapter); } else { throw new UnprocessableEntityException( diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/dstu3/ReleaseVisitor.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/dstu3/ReleaseVisitor.java index e9b7b3e2a..ccaf26047 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/dstu3/ReleaseVisitor.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/dstu3/ReleaseVisitor.java @@ -65,7 +65,7 @@ public static void checkNonExperimental( } } - public static void propagageEffectivePeriod(Period rootEffectivePeriod, KnowledgeArtifactAdapter artifactAdapter) { + public static void propagateEffectivePeriod(Period rootEffectivePeriod, KnowledgeArtifactAdapter artifactAdapter) { Period effectivePeriod = (Period) artifactAdapter.getEffectivePeriod(); // if the root artifact period is NOT null AND HAS a start or an end date if ((rootEffectivePeriod != null && (rootEffectivePeriod.hasStart() || rootEffectivePeriod.hasEnd())) diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r4/ReleaseVisitor.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r4/ReleaseVisitor.java index 4de30c828..a1bd7ffd8 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r4/ReleaseVisitor.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r4/ReleaseVisitor.java @@ -65,7 +65,7 @@ public static void checkNonExperimental( } } - public static void propagageEffectivePeriod(Period rootEffectivePeriod, KnowledgeArtifactAdapter artifactAdapter) { + public static void propagateEffectivePeriod(Period rootEffectivePeriod, KnowledgeArtifactAdapter artifactAdapter) { Period effectivePeriod = (Period) artifactAdapter.getEffectivePeriod(); // if the root artifact period is NOT null AND HAS a start or an end date if ((rootEffectivePeriod != null && (rootEffectivePeriod.hasStart() || rootEffectivePeriod.hasEnd())) diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r5/ReleaseVisitor.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r5/ReleaseVisitor.java index 5c2d97122..74dd08c41 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r5/ReleaseVisitor.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/r5/ReleaseVisitor.java @@ -65,7 +65,7 @@ public static void checkNonExperimental( } } - public static void propagageEffectivePeriod(Period rootEffectivePeriod, KnowledgeArtifactAdapter artifactAdapter) { + public static void propagateEffectivePeriod(Period rootEffectivePeriod, KnowledgeArtifactAdapter artifactAdapter) { Period effectivePeriod = (Period) artifactAdapter.getEffectivePeriod(); // if the root artifact period is NOT null AND HAS a start or an end date if ((rootEffectivePeriod != null && (rootEffectivePeriod.hasStart() || rootEffectivePeriod.hasEnd())) diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/ValueSetsTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/ValueSetsTest.java new file mode 100644 index 000000000..0e27dd5fb --- /dev/null +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/ValueSetsTest.java @@ -0,0 +1,3 @@ +package org.opencds.cqf.fhir.utility; + +public class ValueSetsTest {} diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/StructureDefinitionAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/StructureDefinitionAdapterTest.java index 05e1beb3b..db13b5b94 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/StructureDefinitionAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/StructureDefinitionAdapterTest.java @@ -1,6 +1,7 @@ package org.opencds.cqf.fhir.utility.adapter.dstu3; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.spy; @@ -23,6 +24,7 @@ void adapter_accepts_visitor() { doReturn(new Bundle()).when(spyVisitor).visit(any(StructureDefinitionAdapter.class), any(), any()); IDomainResource structureDef = new StructureDefinition(); var adapter = new StructureDefinitionAdapter(structureDef); + assertEquals(structureDef, adapter.get()); adapter.accept(spyVisitor, null, null); verify(spyVisitor, times(1)).visit(any(StructureDefinitionAdapter.class), any(), any()); } @@ -45,12 +47,36 @@ void adapter_get_and_set_url() { var url = "www.url.com"; structureDef.setUrl(url); var adapter = new StructureDefinitionAdapter(structureDef); + assertTrue(adapter.hasUrl()); assertEquals(url, adapter.getUrl()); var newUrl = "www.url2.com"; adapter.setUrl(newUrl); + assertTrue(adapter.hasUrl()); assertEquals(newUrl, structureDef.getUrl()); } + @Test + void adapter_get_and_set_version() { + var structureDef = new StructureDefinition(); + var version = "1.0.0"; + structureDef.setVersion(version); + var adapter = new StructureDefinitionAdapter(structureDef); + assertTrue(adapter.hasVersion()); + assertEquals(version, adapter.getVersion()); + var newVersion = "1.0.1"; + adapter.setVersion(newVersion); + assertEquals(newVersion, structureDef.getVersion()); + } + + @Test + void adapter_get_and_set_status() {} + + @Test + void adapter_get_and_set_dates() {} + + @Test + void adapter_get_and_set_related() {} + @Test void adapter_get_experimental() { var structureDef = new StructureDefinition(); @@ -60,6 +86,9 @@ void adapter_get_experimental() { assertEquals(experimental, adapter.getExperimental()); } + @Test + void adapter_copy() {} + @Test void adapter_get_all_dependencies() {} } diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/ReleaseVisitorTests.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/ReleaseVisitorTests.java index 80faf3e9c..692eaad32 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/ReleaseVisitorTests.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/ReleaseVisitorTests.java @@ -39,6 +39,7 @@ import org.hl7.fhir.dstu3.model.StringType; import org.hl7.fhir.exceptions.FHIRException; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; @@ -91,6 +92,7 @@ public Bundle answer(InvocationOnMock a) throws Throwable { } @Test + @Disabled("currently failing") void visitLibraryTest() { Bundle bundle = (Bundle) jsonParser.parseResource( ReleaseVisitorTests.class.getResourceAsStream("Bundle-ersd-release-bundle.json")); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/ReleaseVisitorTests.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/ReleaseVisitorTests.java index 8ec53d011..b102278f4 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/ReleaseVisitorTests.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/ReleaseVisitorTests.java @@ -39,6 +39,7 @@ import org.hl7.fhir.r4.model.SearchParameter; import org.hl7.fhir.r4.model.StringType; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; @@ -92,6 +93,7 @@ public Bundle answer(InvocationOnMock a) throws Throwable { } @Test + @Disabled("currently failing") void visitLibraryTest() { Bundle bundle = (Bundle) jsonParser.parseResource( ReleaseVisitorTests.class.getResourceAsStream("Bundle-ersd-release-bundle.json")); @@ -224,7 +226,7 @@ void releaseResource_force_version() { @Test void releaseResource_require_non_experimental_error() { - // SpecificationLibrary - root is experimentalbut HAS experimental children + // SpecificationLibrary - root is experimental but HAS experimental children Bundle bundle = (Bundle) jsonParser.parseResource( ReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft-experimental.json")); spyRepository.transaction(bundle); @@ -516,10 +518,10 @@ void release_preserves_extensions() { ReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft.json")); spyRepository.transaction(bundle); var releaseVisitor = new ReleaseVisitor(); - var orginalLibrary = spyRepository + var originalLibrary = spyRepository .read(Library.class, new IdType("Library/SpecificationLibrary")) .copy(); - var testLibrary = orginalLibrary.copy(); + var testLibrary = originalLibrary.copy(); var libraryAdapter = new AdapterFactory().createLibrary(testLibrary); var params = parameters(part("version", new StringType("1.2.3")), part("versionBehavior", new CodeType("force"))); @@ -530,7 +532,7 @@ void release_preserves_extensions() { assertTrue(maybeLib.isPresent()); var releasedLibrary = spyRepository.read( Library.class, new IdType(maybeLib.get().getResponse().getLocation())); - for (final var originalRelatedArtifact : orginalLibrary.getRelatedArtifact()) { + for (final var originalRelatedArtifact : originalLibrary.getRelatedArtifact()) { releasedLibrary.getRelatedArtifact().forEach(releasedRelatedArtifact -> { if (Canonicals.getUrl(releasedRelatedArtifact.getResource()) .equals(Canonicals.getUrl(originalRelatedArtifact.getResource())) diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/ReleaseVisitorTests.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/ReleaseVisitorTests.java index 0e9d060fd..fd267f80d 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/ReleaseVisitorTests.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/ReleaseVisitorTests.java @@ -39,6 +39,7 @@ import org.hl7.fhir.r5.model.SearchParameter; import org.hl7.fhir.r5.model.StringType; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; @@ -91,6 +92,7 @@ public Bundle answer(InvocationOnMock a) throws Throwable { } @Test + @Disabled("currently failing") void visitLibraryTest() { Bundle bundle = (Bundle) jsonParser.parseResource( ReleaseVisitorTests.class.getResourceAsStream("Bundle-ersd-release-bundle.json")); From 481a5168224e48e03b69789716594227a4fb7eb8 Mon Sep 17 00:00:00 2001 From: "taha.attari@smilecdr.com" Date: Tue, 2 Jul 2024 12:41:50 -0400 Subject: [PATCH 10/38] stop expecting getRelatedArtifacts to pass a mutable ref --- .../opencds/cqf/fhir/utility/visitor/ReleaseVisitor.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/ReleaseVisitor.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/ReleaseVisitor.java index a6b87ca1d..125d8d3cd 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/ReleaseVisitor.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/ReleaseVisitor.java @@ -122,7 +122,9 @@ public IBase visit( } var componentToDependency = KnowledgeArtifactAdapter.newRelatedArtifact( fhirVersion, "depends-on", KnowledgeArtifactAdapter.getRelatedArtifactReference(component)); - rootAdapter.getRelatedArtifact().add(componentToDependency); + var relatedArtifacts = rootAdapter.getRelatedArtifact(); + relatedArtifacts.add(componentToDependency); + rootAdapter.setRelatedArtifact(relatedArtifacts); } var dependencies = artifactAdapter.getDependencies(); @@ -146,7 +148,9 @@ public IBase visit( if (!artifactAdapter.getUrl().equals(rootAdapter.getUrl())) { var newDep = KnowledgeArtifactAdapter.newRelatedArtifact( fhirVersion, "depends-on", dependency.getReference()); - rootAdapter.getRelatedArtifact().add(newDep); + var relatedArtifacts = rootAdapter.getRelatedArtifact(); + relatedArtifacts.add(newDep); + rootAdapter.setRelatedArtifact(relatedArtifacts); } } } From 552b2849fd326c9fee8c1b9740ee0a8512148602 Mon Sep 17 00:00:00 2001 From: Brenin Rhodes Date: Tue, 2 Jul 2024 10:43:37 -0600 Subject: [PATCH 11/38] Enable test --- .../cqf/fhir/utility/visitor/dstu3/ReleaseVisitorTests.java | 6 ++---- .../cqf/fhir/utility/visitor/r4/ReleaseVisitorTests.java | 2 -- .../cqf/fhir/utility/visitor/r5/ReleaseVisitorTests.java | 4 +--- 3 files changed, 3 insertions(+), 9 deletions(-) diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/ReleaseVisitorTests.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/ReleaseVisitorTests.java index 692eaad32..b397804e4 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/ReleaseVisitorTests.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/ReleaseVisitorTests.java @@ -39,7 +39,6 @@ import org.hl7.fhir.dstu3.model.StringType; import org.hl7.fhir.exceptions.FHIRException; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; @@ -92,7 +91,6 @@ public Bundle answer(InvocationOnMock a) throws Throwable { } @Test - @Disabled("currently failing") void visitLibraryTest() { Bundle bundle = (Bundle) jsonParser.parseResource( ReleaseVisitorTests.class.getResourceAsStream("Bundle-ersd-release-bundle.json")); @@ -225,7 +223,7 @@ void releaseResource_force_version() { @Test void releaseResource_require_non_experimental_error() { - // SpecificationLibrary - root is experimentalbut HAS experimental children + // SpecificationLibrary - root is experimental but HAS experimental children Bundle bundle = (Bundle) jsonParser.parseResource( ReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft-experimental.json")); spyRepository.transaction(bundle); @@ -267,7 +265,7 @@ void releaseResource_require_non_experimental_error() { @Test void releaseResource_require_non_experimental_warn() { - // SpecificationLibrary - root is experimentalbut HAS experimental children + // SpecificationLibrary - root is experimental but HAS experimental children Bundle bundle = (Bundle) jsonParser.parseResource( ReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft-experimental.json")); spyRepository.transaction(bundle); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/ReleaseVisitorTests.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/ReleaseVisitorTests.java index b102278f4..092cca33c 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/ReleaseVisitorTests.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/ReleaseVisitorTests.java @@ -39,7 +39,6 @@ import org.hl7.fhir.r4.model.SearchParameter; import org.hl7.fhir.r4.model.StringType; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; @@ -93,7 +92,6 @@ public Bundle answer(InvocationOnMock a) throws Throwable { } @Test - @Disabled("currently failing") void visitLibraryTest() { Bundle bundle = (Bundle) jsonParser.parseResource( ReleaseVisitorTests.class.getResourceAsStream("Bundle-ersd-release-bundle.json")); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/ReleaseVisitorTests.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/ReleaseVisitorTests.java index fd267f80d..d3184f6a0 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/ReleaseVisitorTests.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/ReleaseVisitorTests.java @@ -39,7 +39,6 @@ import org.hl7.fhir.r5.model.SearchParameter; import org.hl7.fhir.r5.model.StringType; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; @@ -92,7 +91,6 @@ public Bundle answer(InvocationOnMock a) throws Throwable { } @Test - @Disabled("currently failing") void visitLibraryTest() { Bundle bundle = (Bundle) jsonParser.parseResource( ReleaseVisitorTests.class.getResourceAsStream("Bundle-ersd-release-bundle.json")); @@ -223,7 +221,7 @@ void releaseResource_force_version() { @Test void releaseResource_require_non_experimental_error() { - // SpecificationLibrary - root is experimentalbut HAS experimental children + // SpecificationLibrary - root is experimental but HAS experimental children Bundle bundle = (Bundle) jsonParser.parseResource( ReleaseVisitorTests.class.getResourceAsStream("Bundle-small-approved-draft-experimental.json")); spyRepository.transaction(bundle); From 107d9055750abdf5c05fb7d67cc03c5483198768 Mon Sep 17 00:00:00 2001 From: Brenin Rhodes Date: Tue, 2 Jul 2024 15:27:06 -0600 Subject: [PATCH 12/38] Add tests --- .../adapter/KnowledgeArtifactAdapter.java | 16 +- .../cqf/fhir/utility/ValueSetsTest.java | 38 ++++- .../dstu3/PlanDefinitionAdapterTest.java | 71 +++++++- .../dstu3/QuestionnaireAdapaterTest.java | 65 ------- .../dstu3/QuestionnaireAdapterTest.java | 158 ++++++++++++++++++ .../dstu3/StructureDefinitionAdapterTest.java | 79 ++++++++- .../adapter/r4/PlanDefinitionAdapterTest.java | 71 +++++++- .../adapter/r4/QuestionnaireAdapaterTest.java | 65 ------- .../adapter/r4/QuestionnaireAdapterTest.java | 158 ++++++++++++++++++ .../r4/StructureDefinitionAdapterTest.java | 94 +++++++++++ .../adapter/r5/PlanDefinitionAdapterTest.java | 71 +++++++- .../adapter/r5/QuestionnaireAdapterTest.java | 95 ++++++++++- .../r5/StructureDefinitionAdapterTest.java | 96 ++++++++++- 13 files changed, 930 insertions(+), 147 deletions(-) delete mode 100644 cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/QuestionnaireAdapaterTest.java create mode 100644 cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/QuestionnaireAdapterTest.java delete mode 100644 cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/QuestionnaireAdapaterTest.java create mode 100644 cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/QuestionnaireAdapterTest.java diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/KnowledgeArtifactAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/KnowledgeArtifactAdapter.java index 5d8068881..fb8edfd54 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/KnowledgeArtifactAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/KnowledgeArtifactAdapter.java @@ -21,8 +21,11 @@ import org.opencds.cqf.fhir.api.Repository; import org.opencds.cqf.fhir.utility.BundleHelper; import org.opencds.cqf.fhir.utility.visitor.KnowledgeArtifactVisitor; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public interface KnowledgeArtifactAdapter extends ResourceAdapter { + public static final Logger logger = LoggerFactory.getLogger(KnowledgeArtifactAdapter.class); IDomainResource get(); @@ -77,8 +80,13 @@ default Date getApprovalDate() { } default void setApprovalDate(Date approvalDate) { - getModelResolver() - .setValue(get(), "approvalDate", newDateType(get().getStructureFhirVersionEnum(), approvalDate)); + try { + getModelResolver() + .setValue(get(), "approvalDate", newDateType(get().getStructureFhirVersionEnum(), approvalDate)); + } catch (Exception e) { + // Do nothing + logger.debug("Field 'approvalDate' does not exist on Resource type {}", get().fhirType()); + } } @SuppressWarnings("unchecked") @@ -113,6 +121,7 @@ default void setEffectivePeriod(ICompositeType period) { getModelResolver().setValue(get(), "effectivePeriod", period); } catch (Exception e) { // Do nothing + logger.debug("Field 'effectivePeriod' does not exist on Resource type {}", get().fhirType()); } } @@ -281,8 +290,7 @@ default void setRelatedArtifact( getModelResolver().setValue(get(), "relatedArtifact", relatedArtifacts); } catch (Exception e) { // Do nothing - throw new IllegalAccessError( - "Encountered error trying to access field 'relatedArtifact': " + e.getMessage()); + logger.debug("Field 'relatedArtifact' does not exist on Resource type {}", get().fhirType()); } } diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/ValueSetsTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/ValueSetsTest.java index 0e27dd5fb..b912e2bc6 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/ValueSetsTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/ValueSetsTest.java @@ -1,3 +1,39 @@ package org.opencds.cqf.fhir.utility; -public class ValueSetsTest {} +import static org.junit.jupiter.api.Assertions.assertEquals; + +import ca.uhn.fhir.context.FhirContext; +import java.lang.reflect.InvocationTargetException; +import org.hl7.fhir.r4.model.ValueSet; +import org.junit.jupiter.api.Test; +import org.opencds.cqf.cql.engine.runtime.Code; + +class ValueSetsTest { + private final FhirContext fhirContextR4 = FhirContext.forR4Cached(); + + @Test + void testAddCodeToExpansion() { + var code = new Code(); + code.setCode("test"); + code.setDisplay("Test"); + code.setSystem("www.test.com"); + code.setVersion("1.0.0"); + var expansion = new ValueSet.ValueSetExpansionComponent(); + try { + ValueSets.addCodeToExpansion(fhirContextR4, expansion, code); + } catch (InstantiationException + | IllegalAccessException + | IllegalArgumentException + | InvocationTargetException + | NoSuchMethodException + | SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + var codes = ValueSets.getCodesInExpansion(fhirContextR4, expansion); + assertEquals(code.getCode(), codes.get(0).getCode()); + assertEquals(code.getDisplay(), codes.get(0).getDisplay()); + assertEquals(code.getSystem(), codes.get(0).getSystem()); + assertEquals(code.getVersion(), codes.get(0).getVersion()); + } +} diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/PlanDefinitionAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/PlanDefinitionAdapterTest.java index 15262ace0..d05171de1 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/PlanDefinitionAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/PlanDefinitionAdapterTest.java @@ -1,6 +1,8 @@ package org.opencds.cqf.fhir.utility.adapter.dstu3; +import static org.junit.Assert.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doReturn; @@ -9,10 +11,16 @@ import static org.mockito.Mockito.verify; import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.model.primitive.IdDt; +import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; +import java.time.LocalDate; import java.util.Date; import java.util.List; import org.hl7.fhir.dstu3.model.Bundle; +import org.hl7.fhir.dstu3.model.Enumerations.PublicationStatus; import org.hl7.fhir.dstu3.model.Extension; +import org.hl7.fhir.dstu3.model.Library; +import org.hl7.fhir.dstu3.model.Period; import org.hl7.fhir.dstu3.model.PlanDefinition; import org.hl7.fhir.dstu3.model.Reference; import org.hl7.fhir.dstu3.model.RelatedArtifact; @@ -25,6 +33,11 @@ public class PlanDefinitionAdapterTest { private final FhirContext fhirContext = FhirContext.forDstu3Cached(); + @Test + void invalid_object_fails() { + assertThrows(IllegalArgumentException.class, () -> new PlanDefinitionAdapter(new Library())); + } + @Test void adapter_accepts_visitor() { var spyVisitor = spy(new PackageVisitor(fhirContext)); @@ -60,16 +73,59 @@ void adapter_get_and_set_url() { } @Test - void adapter_get_and_set_approvalDate() { + void adapter_get_and_set_version() { var planDef = new PlanDefinition(); + var version = "1.0.0"; + planDef.setVersion(version); + var adapter = new PlanDefinitionAdapter(planDef); + assertTrue(adapter.hasVersion()); + assertEquals(version, adapter.getVersion()); + var newVersion = "1.0.1"; + adapter.setVersion(newVersion); + assertEquals(newVersion, planDef.getVersion()); + } + + @Test + void adapter_get_and_set_status() { + var planDef = new PlanDefinition(); + var status = PublicationStatus.DRAFT; + planDef.setStatus(status); + var adapter = new PlanDefinitionAdapter(planDef); + assertEquals(status.toCode(), adapter.getStatus()); + assertThrows(UnprocessableEntityException.class, () -> adapter.setStatus("invalid-status")); + var newStatus = PublicationStatus.ACTIVE; + adapter.setStatus(newStatus.toCode()); + assertEquals(newStatus, PublicationStatus.fromCode(adapter.getStatus())); + } + + @Test + void adapter_get_and_set_dates() { + var planDef = new PlanDefinition(); + var date = new Date(); var approvalDate = new Date(); + var effectivePeriod = new Period() + .setStart(java.sql.Date.valueOf(LocalDate.parse("2020-01-01"))) + .setEnd(java.sql.Date.valueOf(LocalDate.parse("2020-12-31"))); + planDef.setDate(date); planDef.setApprovalDate(approvalDate); + planDef.setEffectivePeriod(effectivePeriod); var adapter = new PlanDefinitionAdapter(planDef); + assertEquals(date, planDef.getDate()); assertEquals(approvalDate, adapter.getApprovalDate()); + assertEquals(effectivePeriod, adapter.getEffectivePeriod()); + var newDate = new Date(); + newDate.setTime(100); + adapter.setDate(newDate); + assertEquals(newDate, planDef.getDate()); var newApprovalDate = new Date(); newApprovalDate.setTime(100); adapter.setApprovalDate(newApprovalDate); assertEquals(newApprovalDate, planDef.getApprovalDate()); + var newEffectivePeriod = new Period() + .setStart(java.sql.Date.valueOf(LocalDate.parse("2021-01-01"))) + .setEnd(java.sql.Date.valueOf(LocalDate.parse("2021-12-31"))); + adapter.setEffectivePeriod(newEffectivePeriod); + assertEquals(newEffectivePeriod, adapter.getEffectivePeriod()); } @Test @@ -91,6 +147,19 @@ void adapter_set_relatedArtifact() { assertEquals(relatedArtifactList, adapter.getRelatedArtifact()); } + @Test + void adapter_copy() { + var planDef = new PlanDefinition().setStatus(PublicationStatus.DRAFT); + planDef.setId("plan-1"); + var adapter = new PlanDefinitionAdapter(planDef); + var copy = adapter.copy(); + var adapterCopy = new PlanDefinitionAdapter(copy); + adapterCopy.setId(new IdDt("PlanDefinition", "plan-2")); + assertNotEquals(planDef.getId(), copy.getId()); + planDef.setStatus(PublicationStatus.ACTIVE); + assertNotEquals(adapter.getStatus(), copy.getStatus()); + } + @Test void adapter_get_all_dependencies() { var dependencies = List.of( diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/QuestionnaireAdapaterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/QuestionnaireAdapaterTest.java deleted file mode 100644 index 27ee83d01..000000000 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/QuestionnaireAdapaterTest.java +++ /dev/null @@ -1,65 +0,0 @@ -package org.opencds.cqf.fhir.utility.adapter.dstu3; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; - -import ca.uhn.fhir.context.FhirContext; -import org.hl7.fhir.dstu3.model.Bundle; -import org.hl7.fhir.dstu3.model.Questionnaire; -import org.hl7.fhir.instance.model.api.IDomainResource; -import org.junit.jupiter.api.Test; -import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; - -public class QuestionnaireAdapaterTest { - private final FhirContext fhirContext = FhirContext.forR4Cached(); - - @Test - void adapter_accepts_visitor() { - var spyVisitor = spy(new PackageVisitor(fhirContext)); - doReturn(new Bundle()).when(spyVisitor).visit(any(QuestionnaireAdapter.class), any(), any()); - IDomainResource questionnaire = new Questionnaire(); - var adapter = new QuestionnaireAdapter(questionnaire); - adapter.accept(spyVisitor, null, null); - verify(spyVisitor, times(1)).visit(any(QuestionnaireAdapter.class), any(), any()); - } - - @Test - void adapter_get_and_set_name() { - var questionnaire = new Questionnaire(); - var name = "name"; - questionnaire.setName(name); - var adapter = new QuestionnaireAdapter(questionnaire); - assertEquals(name, adapter.getName()); - var newName = "name2"; - adapter.setName(newName); - assertEquals(newName, questionnaire.getName()); - } - - @Test - void adapter_get_and_set_url() { - var questionnaire = new Questionnaire(); - var url = "www.url.com"; - questionnaire.setUrl(url); - var adapter = new QuestionnaireAdapter(questionnaire); - assertEquals(url, adapter.getUrl()); - var newUrl = "www.url2.com"; - adapter.setUrl(newUrl); - assertEquals(newUrl, questionnaire.getUrl()); - } - - @Test - void adapter_get_experimental() { - var questionnaire = new Questionnaire(); - var experimental = true; - questionnaire.setExperimental(experimental); - var adapter = new QuestionnaireAdapter(questionnaire); - assertEquals(experimental, adapter.getExperimental()); - } - - @Test - void adapter_get_all_dependencies() {} -} diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/QuestionnaireAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/QuestionnaireAdapterTest.java new file mode 100644 index 000000000..4d4c95ae2 --- /dev/null +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/QuestionnaireAdapterTest.java @@ -0,0 +1,158 @@ +package org.opencds.cqf.fhir.utility.adapter.dstu3; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; +import java.time.LocalDate; +import java.util.Date; +import java.util.List; +import org.hl7.fhir.dstu3.model.Bundle; +import org.hl7.fhir.dstu3.model.Enumerations.PublicationStatus; +import org.hl7.fhir.dstu3.model.Library; +import org.hl7.fhir.dstu3.model.Period; +import org.hl7.fhir.dstu3.model.Questionnaire; +import org.hl7.fhir.dstu3.model.RelatedArtifact; +import org.hl7.fhir.instance.model.api.IDomainResource; +import org.junit.jupiter.api.Test; +import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; + +public class QuestionnaireAdapterTest { + private final FhirContext fhirContext = FhirContext.forDstu3Cached(); + + @Test + void invalid_object_fails() { + assertThrows(IllegalArgumentException.class, () -> new QuestionnaireAdapter(new Library())); + } + + @Test + void adapter_accepts_visitor() { + var spyVisitor = spy(new PackageVisitor(fhirContext)); + doReturn(new Bundle()).when(spyVisitor).visit(any(QuestionnaireAdapter.class), any(), any()); + IDomainResource questionnaire = new Questionnaire(); + var adapter = new QuestionnaireAdapter(questionnaire); + adapter.accept(spyVisitor, null, null); + verify(spyVisitor, times(1)).visit(any(QuestionnaireAdapter.class), any(), any()); + } + + @Test + void adapter_get_and_set_name() { + var questionnaire = new Questionnaire(); + var name = "name"; + questionnaire.setName(name); + var adapter = new QuestionnaireAdapter(questionnaire); + assertEquals(name, adapter.getName()); + var newName = "name2"; + adapter.setName(newName); + assertEquals(newName, questionnaire.getName()); + } + + @Test + void adapter_get_and_set_url() { + var questionnaire = new Questionnaire(); + var url = "www.url.com"; + questionnaire.setUrl(url); + var adapter = new QuestionnaireAdapter(questionnaire); + assertEquals(url, adapter.getUrl()); + var newUrl = "www.url2.com"; + adapter.setUrl(newUrl); + assertEquals(newUrl, questionnaire.getUrl()); + } + + @Test + void adapter_get_and_set_version() { + var questionnaire = new Questionnaire(); + var version = "1.0.0"; + questionnaire.setVersion(version); + var adapter = new QuestionnaireAdapter(questionnaire); + assertTrue(adapter.hasVersion()); + assertEquals(version, adapter.getVersion()); + var newVersion = "1.0.1"; + adapter.setVersion(newVersion); + assertEquals(newVersion, questionnaire.getVersion()); + } + + @Test + void adapter_get_and_set_status() { + var questionnaire = new Questionnaire(); + var status = PublicationStatus.DRAFT; + questionnaire.setStatus(status); + var adapter = new QuestionnaireAdapter(questionnaire); + assertEquals(status.toCode(), adapter.getStatus()); + assertThrows(UnprocessableEntityException.class, () -> adapter.setStatus("invalid-status")); + var newStatus = PublicationStatus.ACTIVE; + adapter.setStatus(newStatus.toCode()); + assertEquals(newStatus, PublicationStatus.fromCode(adapter.getStatus())); + } + + @Test + void adapter_get_and_set_dates() { + var questionnaire = new Questionnaire(); + var date = new Date(); + var approvalDate = new Date(); + var effectivePeriod = new Period() + .setStart(java.sql.Date.valueOf(LocalDate.parse("2020-01-01"))) + .setEnd(java.sql.Date.valueOf(LocalDate.parse("2020-12-31"))); + questionnaire.setDate(date); + questionnaire.setApprovalDate(approvalDate); + questionnaire.setEffectivePeriod(effectivePeriod); + var adapter = new QuestionnaireAdapter(questionnaire); + assertEquals(date, questionnaire.getDate()); + assertEquals(approvalDate, adapter.getApprovalDate()); + assertEquals(effectivePeriod, adapter.getEffectivePeriod()); + var newDate = new Date(); + newDate.setTime(100); + adapter.setDate(newDate); + assertEquals(newDate, questionnaire.getDate()); + var newApprovalDate = new Date(); + newApprovalDate.setTime(100); + adapter.setApprovalDate(newApprovalDate); + assertEquals(newApprovalDate, questionnaire.getApprovalDate()); + var newEffectivePeriod = new Period() + .setStart(java.sql.Date.valueOf(LocalDate.parse("2021-01-01"))) + .setEnd(java.sql.Date.valueOf(LocalDate.parse("2021-12-31"))); + adapter.setEffectivePeriod(newEffectivePeriod); + assertEquals(newEffectivePeriod, adapter.getEffectivePeriod()); + } + + @Test + void adapter_get_experimental() { + var questionnaire = new Questionnaire(); + var experimental = true; + questionnaire.setExperimental(experimental); + var adapter = new QuestionnaireAdapter(questionnaire); + assertEquals(experimental, adapter.getExperimental()); + } + + @Test + void adapter_set_relatedArtifact() { + var questionnaire = new Questionnaire(); + var relatedArtifactList = List.of(new RelatedArtifact()); + var adapter = new QuestionnaireAdapter(questionnaire); + adapter.setRelatedArtifact(relatedArtifactList); + assertEquals(0, adapter.getRelatedArtifact().size()); + } + + @Test + void adapter_copy() { + var questionnaire = new Questionnaire().setStatus(PublicationStatus.DRAFT); + questionnaire.setId("plan-1"); + var adapter = new QuestionnaireAdapter(questionnaire); + var copy = adapter.copy(); + copy.setId("plan-2"); + assertNotEquals(questionnaire.getId(), copy.getId()); + questionnaire.setStatus(PublicationStatus.ACTIVE); + assertNotEquals(adapter.getStatus(), copy.getStatus()); + } + + @Test + void adapter_get_all_dependencies() {} +} diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/StructureDefinitionAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/StructureDefinitionAdapterTest.java index db13b5b94..4e093e680 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/StructureDefinitionAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/StructureDefinitionAdapterTest.java @@ -1,6 +1,8 @@ package org.opencds.cqf.fhir.utility.adapter.dstu3; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doReturn; @@ -9,14 +11,27 @@ import static org.mockito.Mockito.verify; import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; +import java.time.LocalDate; +import java.util.Date; +import java.util.List; import org.hl7.fhir.dstu3.model.Bundle; +import org.hl7.fhir.dstu3.model.Enumerations.PublicationStatus; +import org.hl7.fhir.dstu3.model.Library; +import org.hl7.fhir.dstu3.model.Period; +import org.hl7.fhir.dstu3.model.RelatedArtifact; import org.hl7.fhir.dstu3.model.StructureDefinition; import org.hl7.fhir.instance.model.api.IDomainResource; import org.junit.jupiter.api.Test; import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; public class StructureDefinitionAdapterTest { - private final FhirContext fhirContext = FhirContext.forR4Cached(); + private final FhirContext fhirContext = FhirContext.forDstu3Cached(); + + @Test + void invalid_object_fails() { + assertThrows(IllegalArgumentException.class, () -> new StructureDefinitionAdapter(new Library())); + } @Test void adapter_accepts_visitor() { @@ -69,13 +84,45 @@ void adapter_get_and_set_version() { } @Test - void adapter_get_and_set_status() {} - - @Test - void adapter_get_and_set_dates() {} + void adapter_get_and_set_status() { + var structureDef = new StructureDefinition(); + var status = PublicationStatus.DRAFT; + structureDef.setStatus(status); + var adapter = new StructureDefinitionAdapter(structureDef); + assertEquals(status.toCode(), adapter.getStatus()); + assertThrows(UnprocessableEntityException.class, () -> adapter.setStatus("invalid-status")); + var newStatus = PublicationStatus.ACTIVE; + adapter.setStatus(newStatus.toCode()); + assertEquals(newStatus, PublicationStatus.fromCode(adapter.getStatus())); + } @Test - void adapter_get_and_set_related() {} + void adapter_get_and_set_dates() { + // StructureDefinition does not have fields approvalDate and effectivePeriod + var structureDef = new StructureDefinition(); + var date = new Date(); + var effectivePeriod = new Period() + .setStart(java.sql.Date.valueOf(LocalDate.parse("2020-01-01"))) + .setEnd(java.sql.Date.valueOf(LocalDate.parse("2020-12-31"))); + structureDef.setDate(date); + var adapter = new StructureDefinitionAdapter(structureDef); + assertEquals(date, structureDef.getDate()); + assertEquals(null, adapter.getApprovalDate()); + assertNotEquals(effectivePeriod, adapter.getEffectivePeriod()); + var newDate = new Date(); + newDate.setTime(100); + adapter.setDate(newDate); + assertEquals(newDate, structureDef.getDate()); + var newApprovalDate = new Date(); + newApprovalDate.setTime(100); + adapter.setApprovalDate(newApprovalDate); + assertEquals(null, adapter.getApprovalDate()); + var newEffectivePeriod = new Period() + .setStart(java.sql.Date.valueOf(LocalDate.parse("2021-01-01"))) + .setEnd(java.sql.Date.valueOf(LocalDate.parse("2021-12-31"))); + adapter.setEffectivePeriod(newEffectivePeriod); + assertNotEquals(newEffectivePeriod, adapter.getEffectivePeriod()); + } @Test void adapter_get_experimental() { @@ -87,7 +134,25 @@ void adapter_get_experimental() { } @Test - void adapter_copy() {} + void adapter_set_relatedArtifact() { + var structureDef = new StructureDefinition(); + var relatedArtifactList = List.of(new RelatedArtifact()); + var adapter = new StructureDefinitionAdapter(structureDef); + adapter.setRelatedArtifact(relatedArtifactList); + assertEquals(0, adapter.getRelatedArtifact().size()); + } + + @Test + void adapter_copy() { + var structureDef = new StructureDefinition().setStatus(PublicationStatus.DRAFT); + structureDef.setId("plan-1"); + var adapter = new StructureDefinitionAdapter(structureDef); + var copy = adapter.copy(); + copy.setId("plan-2"); + assertNotEquals(structureDef.getId(), copy.getId()); + structureDef.setStatus(PublicationStatus.ACTIVE); + assertNotEquals(adapter.getStatus(), copy.getStatus()); + } @Test void adapter_get_all_dependencies() {} diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/PlanDefinitionAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/PlanDefinitionAdapterTest.java index 862115f3f..32c4921f6 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/PlanDefinitionAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/PlanDefinitionAdapterTest.java @@ -1,6 +1,8 @@ package org.opencds.cqf.fhir.utility.adapter.r4; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doReturn; @@ -9,12 +11,18 @@ import static org.mockito.Mockito.verify; import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.model.primitive.IdDt; +import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; +import java.time.LocalDate; import java.util.Date; import java.util.List; import org.hl7.fhir.instance.model.api.IDomainResource; import org.hl7.fhir.r4.model.Bundle; import org.hl7.fhir.r4.model.CanonicalType; +import org.hl7.fhir.r4.model.Enumerations.PublicationStatus; import org.hl7.fhir.r4.model.Extension; +import org.hl7.fhir.r4.model.Library; +import org.hl7.fhir.r4.model.Period; import org.hl7.fhir.r4.model.PlanDefinition; import org.hl7.fhir.r4.model.RelatedArtifact; import org.junit.jupiter.api.Test; @@ -23,6 +31,11 @@ public class PlanDefinitionAdapterTest { private final FhirContext fhirContext = FhirContext.forR4Cached(); + @Test + void invalid_object_fails() { + assertThrows(IllegalArgumentException.class, () -> new PlanDefinitionAdapter(new Library())); + } + @Test void adapter_accepts_visitor() { var spyVisitor = spy(new PackageVisitor(fhirContext)); @@ -58,16 +71,59 @@ void adapter_get_and_set_url() { } @Test - void adapter_get_and_set_approvalDate() { + void adapter_get_and_set_version() { var planDef = new PlanDefinition(); + var version = "1.0.0"; + planDef.setVersion(version); + var adapter = new PlanDefinitionAdapter(planDef); + assertTrue(adapter.hasVersion()); + assertEquals(version, adapter.getVersion()); + var newVersion = "1.0.1"; + adapter.setVersion(newVersion); + assertEquals(newVersion, planDef.getVersion()); + } + + @Test + void adapter_get_and_set_status() { + var planDef = new PlanDefinition(); + var status = PublicationStatus.DRAFT; + planDef.setStatus(status); + var adapter = new PlanDefinitionAdapter(planDef); + assertEquals(status.toCode(), adapter.getStatus()); + assertThrows(UnprocessableEntityException.class, () -> adapter.setStatus("invalid-status")); + var newStatus = PublicationStatus.ACTIVE; + adapter.setStatus(newStatus.toCode()); + assertEquals(newStatus, PublicationStatus.fromCode(adapter.getStatus())); + } + + @Test + void adapter_get_and_set_dates() { + var planDef = new PlanDefinition(); + var date = new Date(); var approvalDate = new Date(); + var effectivePeriod = new Period() + .setStart(java.sql.Date.valueOf(LocalDate.parse("2020-01-01"))) + .setEnd(java.sql.Date.valueOf(LocalDate.parse("2020-12-31"))); + planDef.setDate(date); planDef.setApprovalDate(approvalDate); + planDef.setEffectivePeriod(effectivePeriod); var adapter = new PlanDefinitionAdapter(planDef); + assertEquals(date, planDef.getDate()); assertEquals(approvalDate, adapter.getApprovalDate()); + assertEquals(effectivePeriod, adapter.getEffectivePeriod()); + var newDate = new Date(); + newDate.setTime(100); + adapter.setDate(newDate); + assertEquals(newDate, planDef.getDate()); var newApprovalDate = new Date(); newApprovalDate.setTime(100); adapter.setApprovalDate(newApprovalDate); assertEquals(newApprovalDate, planDef.getApprovalDate()); + var newEffectivePeriod = new Period() + .setStart(java.sql.Date.valueOf(LocalDate.parse("2021-01-01"))) + .setEnd(java.sql.Date.valueOf(LocalDate.parse("2021-12-31"))); + adapter.setEffectivePeriod(newEffectivePeriod); + assertEquals(newEffectivePeriod, adapter.getEffectivePeriod()); } @Test @@ -89,6 +145,19 @@ void adapter_set_relatedArtifact() { assertEquals(relatedArtifactList, adapter.getRelatedArtifact()); } + @Test + void adapter_copy() { + var planDef = new PlanDefinition().setStatus(PublicationStatus.DRAFT); + planDef.setId("plan-1"); + var adapter = new PlanDefinitionAdapter(planDef); + var copy = adapter.copy(); + var adapterCopy = new PlanDefinitionAdapter(copy); + adapterCopy.setId(new IdDt("PlanDefinition", "plan-2")); + assertNotEquals(planDef.getId(), copy.getId()); + planDef.setStatus(PublicationStatus.ACTIVE); + assertNotEquals(adapter.getStatus(), copy.getStatus()); + } + @Test void adapter_get_all_dependencies() { var dependencies = List.of( diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/QuestionnaireAdapaterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/QuestionnaireAdapaterTest.java deleted file mode 100644 index 65be52b57..000000000 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/QuestionnaireAdapaterTest.java +++ /dev/null @@ -1,65 +0,0 @@ -package org.opencds.cqf.fhir.utility.adapter.r4; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; - -import ca.uhn.fhir.context.FhirContext; -import org.hl7.fhir.instance.model.api.IDomainResource; -import org.hl7.fhir.r4.model.Bundle; -import org.hl7.fhir.r4.model.Questionnaire; -import org.junit.jupiter.api.Test; -import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; - -public class QuestionnaireAdapaterTest { - private final FhirContext fhirContext = FhirContext.forR4Cached(); - - @Test - void adapter_accepts_visitor() { - var spyVisitor = spy(new PackageVisitor(fhirContext)); - doReturn(new Bundle()).when(spyVisitor).visit(any(QuestionnaireAdapter.class), any(), any()); - IDomainResource questionnaire = new Questionnaire(); - var adapter = new QuestionnaireAdapter(questionnaire); - adapter.accept(spyVisitor, null, null); - verify(spyVisitor, times(1)).visit(any(QuestionnaireAdapter.class), any(), any()); - } - - @Test - void adapter_get_and_set_name() { - var questionnaire = new Questionnaire(); - var name = "name"; - questionnaire.setName(name); - var adapter = new QuestionnaireAdapter(questionnaire); - assertEquals(name, adapter.getName()); - var newName = "name2"; - adapter.setName(newName); - assertEquals(newName, questionnaire.getName()); - } - - @Test - void adapter_get_and_set_url() { - var questionnaire = new Questionnaire(); - var url = "www.url.com"; - questionnaire.setUrl(url); - var adapter = new QuestionnaireAdapter(questionnaire); - assertEquals(url, adapter.getUrl()); - var newUrl = "www.url2.com"; - adapter.setUrl(newUrl); - assertEquals(newUrl, questionnaire.getUrl()); - } - - @Test - void adapter_get_experimental() { - var questionnaire = new Questionnaire(); - var experimental = true; - questionnaire.setExperimental(experimental); - var adapter = new QuestionnaireAdapter(questionnaire); - assertEquals(experimental, adapter.getExperimental()); - } - - @Test - void adapter_get_all_dependencies() {} -} diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/QuestionnaireAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/QuestionnaireAdapterTest.java new file mode 100644 index 000000000..7c87e70d0 --- /dev/null +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/QuestionnaireAdapterTest.java @@ -0,0 +1,158 @@ +package org.opencds.cqf.fhir.utility.adapter.r4; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; +import java.time.LocalDate; +import java.util.Date; +import java.util.List; +import org.hl7.fhir.instance.model.api.IDomainResource; +import org.hl7.fhir.r4.model.Bundle; +import org.hl7.fhir.r4.model.Enumerations.PublicationStatus; +import org.hl7.fhir.r4.model.Library; +import org.hl7.fhir.r4.model.Period; +import org.hl7.fhir.r4.model.Questionnaire; +import org.hl7.fhir.r4.model.RelatedArtifact; +import org.junit.jupiter.api.Test; +import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; + +public class QuestionnaireAdapterTest { + private final FhirContext fhirContext = FhirContext.forR4Cached(); + + @Test + void invalid_object_fails() { + assertThrows(IllegalArgumentException.class, () -> new QuestionnaireAdapter(new Library())); + } + + @Test + void adapter_accepts_visitor() { + var spyVisitor = spy(new PackageVisitor(fhirContext)); + doReturn(new Bundle()).when(spyVisitor).visit(any(QuestionnaireAdapter.class), any(), any()); + IDomainResource questionnaire = new Questionnaire(); + var adapter = new QuestionnaireAdapter(questionnaire); + adapter.accept(spyVisitor, null, null); + verify(spyVisitor, times(1)).visit(any(QuestionnaireAdapter.class), any(), any()); + } + + @Test + void adapter_get_and_set_name() { + var questionnaire = new Questionnaire(); + var name = "name"; + questionnaire.setName(name); + var adapter = new QuestionnaireAdapter(questionnaire); + assertEquals(name, adapter.getName()); + var newName = "name2"; + adapter.setName(newName); + assertEquals(newName, questionnaire.getName()); + } + + @Test + void adapter_get_and_set_url() { + var questionnaire = new Questionnaire(); + var url = "www.url.com"; + questionnaire.setUrl(url); + var adapter = new QuestionnaireAdapter(questionnaire); + assertEquals(url, adapter.getUrl()); + var newUrl = "www.url2.com"; + adapter.setUrl(newUrl); + assertEquals(newUrl, questionnaire.getUrl()); + } + + @Test + void adapter_get_and_set_version() { + var questionnaire = new Questionnaire(); + var version = "1.0.0"; + questionnaire.setVersion(version); + var adapter = new QuestionnaireAdapter(questionnaire); + assertTrue(adapter.hasVersion()); + assertEquals(version, adapter.getVersion()); + var newVersion = "1.0.1"; + adapter.setVersion(newVersion); + assertEquals(newVersion, questionnaire.getVersion()); + } + + @Test + void adapter_get_and_set_status() { + var questionnaire = new Questionnaire(); + var status = PublicationStatus.DRAFT; + questionnaire.setStatus(status); + var adapter = new QuestionnaireAdapter(questionnaire); + assertEquals(status.toCode(), adapter.getStatus()); + assertThrows(UnprocessableEntityException.class, () -> adapter.setStatus("invalid-status")); + var newStatus = PublicationStatus.ACTIVE; + adapter.setStatus(newStatus.toCode()); + assertEquals(newStatus, PublicationStatus.fromCode(adapter.getStatus())); + } + + @Test + void adapter_get_and_set_dates() { + var questionnaire = new Questionnaire(); + var date = new Date(); + var approvalDate = new Date(); + var effectivePeriod = new Period() + .setStart(java.sql.Date.valueOf(LocalDate.parse("2020-01-01"))) + .setEnd(java.sql.Date.valueOf(LocalDate.parse("2020-12-31"))); + questionnaire.setDate(date); + questionnaire.setApprovalDate(approvalDate); + questionnaire.setEffectivePeriod(effectivePeriod); + var adapter = new QuestionnaireAdapter(questionnaire); + assertEquals(date, questionnaire.getDate()); + assertEquals(approvalDate, adapter.getApprovalDate()); + assertEquals(effectivePeriod, adapter.getEffectivePeriod()); + var newDate = new Date(); + newDate.setTime(100); + adapter.setDate(newDate); + assertEquals(newDate, questionnaire.getDate()); + var newApprovalDate = new Date(); + newApprovalDate.setTime(100); + adapter.setApprovalDate(newApprovalDate); + assertEquals(newApprovalDate, questionnaire.getApprovalDate()); + var newEffectivePeriod = new Period() + .setStart(java.sql.Date.valueOf(LocalDate.parse("2021-01-01"))) + .setEnd(java.sql.Date.valueOf(LocalDate.parse("2021-12-31"))); + adapter.setEffectivePeriod(newEffectivePeriod); + assertEquals(newEffectivePeriod, adapter.getEffectivePeriod()); + } + + @Test + void adapter_get_experimental() { + var questionnaire = new Questionnaire(); + var experimental = true; + questionnaire.setExperimental(experimental); + var adapter = new QuestionnaireAdapter(questionnaire); + assertEquals(experimental, adapter.getExperimental()); + } + + @Test + void adapter_set_relatedArtifact() { + var questionnaire = new Questionnaire(); + var relatedArtifactList = List.of(new RelatedArtifact()); + var adapter = new QuestionnaireAdapter(questionnaire); + adapter.setRelatedArtifact(relatedArtifactList); + assertEquals(0, adapter.getRelatedArtifact().size()); + } + + @Test + void adapter_copy() { + var questionnaire = new Questionnaire().setStatus(PublicationStatus.DRAFT); + questionnaire.setId("plan-1"); + var adapter = new QuestionnaireAdapter(questionnaire); + var copy = adapter.copy(); + copy.setId("plan-2"); + assertNotEquals(questionnaire.getId(), copy.getId()); + questionnaire.setStatus(PublicationStatus.ACTIVE); + assertNotEquals(adapter.getStatus(), copy.getStatus()); + } + + @Test + void adapter_get_all_dependencies() {} +} diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/StructureDefinitionAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/StructureDefinitionAdapterTest.java index bf6fa45f9..45a8eece0 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/StructureDefinitionAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/StructureDefinitionAdapterTest.java @@ -1,6 +1,9 @@ package org.opencds.cqf.fhir.utility.adapter.r4; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.spy; @@ -8,8 +11,16 @@ import static org.mockito.Mockito.verify; import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; +import java.time.LocalDate; +import java.util.Date; +import java.util.List; import org.hl7.fhir.instance.model.api.IDomainResource; import org.hl7.fhir.r4.model.Bundle; +import org.hl7.fhir.r4.model.Enumerations.PublicationStatus; +import org.hl7.fhir.r4.model.Library; +import org.hl7.fhir.r4.model.Period; +import org.hl7.fhir.r4.model.RelatedArtifact; import org.hl7.fhir.r4.model.StructureDefinition; import org.junit.jupiter.api.Test; import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; @@ -17,12 +28,18 @@ public class StructureDefinitionAdapterTest { private final FhirContext fhirContext = FhirContext.forR4Cached(); + @Test + void invalid_object_fails() { + assertThrows(IllegalArgumentException.class, () -> new StructureDefinitionAdapter(new Library())); + } + @Test void adapter_accepts_visitor() { var spyVisitor = spy(new PackageVisitor(fhirContext)); doReturn(new Bundle()).when(spyVisitor).visit(any(StructureDefinitionAdapter.class), any(), any()); IDomainResource structureDef = new StructureDefinition(); var adapter = new StructureDefinitionAdapter(structureDef); + assertEquals(structureDef, adapter.get()); adapter.accept(spyVisitor, null, null); verify(spyVisitor, times(1)).visit(any(StructureDefinitionAdapter.class), any(), any()); } @@ -45,12 +62,68 @@ void adapter_get_and_set_url() { var url = "www.url.com"; structureDef.setUrl(url); var adapter = new StructureDefinitionAdapter(structureDef); + assertTrue(adapter.hasUrl()); assertEquals(url, adapter.getUrl()); var newUrl = "www.url2.com"; adapter.setUrl(newUrl); + assertTrue(adapter.hasUrl()); assertEquals(newUrl, structureDef.getUrl()); } + @Test + void adapter_get_and_set_version() { + var structureDef = new StructureDefinition(); + var version = "1.0.0"; + structureDef.setVersion(version); + var adapter = new StructureDefinitionAdapter(structureDef); + assertTrue(adapter.hasVersion()); + assertEquals(version, adapter.getVersion()); + var newVersion = "1.0.1"; + adapter.setVersion(newVersion); + assertEquals(newVersion, structureDef.getVersion()); + } + + @Test + void adapter_get_and_set_status() { + var structureDef = new StructureDefinition(); + var status = PublicationStatus.DRAFT; + structureDef.setStatus(status); + var adapter = new StructureDefinitionAdapter(structureDef); + assertEquals(status.toCode(), adapter.getStatus()); + assertThrows(UnprocessableEntityException.class, () -> adapter.setStatus("invalid-status")); + var newStatus = PublicationStatus.ACTIVE; + adapter.setStatus(newStatus.toCode()); + assertEquals(newStatus, PublicationStatus.fromCode(adapter.getStatus())); + } + + @Test + void adapter_get_and_set_dates() { + // StructureDefinition does not have fields approvalDate and effectivePeriod + var structureDef = new StructureDefinition(); + var date = new Date(); + var effectivePeriod = new Period() + .setStart(java.sql.Date.valueOf(LocalDate.parse("2020-01-01"))) + .setEnd(java.sql.Date.valueOf(LocalDate.parse("2020-12-31"))); + structureDef.setDate(date); + var adapter = new StructureDefinitionAdapter(structureDef); + assertEquals(date, structureDef.getDate()); + assertEquals(null, adapter.getApprovalDate()); + assertNotEquals(effectivePeriod, adapter.getEffectivePeriod()); + var newDate = new Date(); + newDate.setTime(100); + adapter.setDate(newDate); + assertEquals(newDate, structureDef.getDate()); + var newApprovalDate = new Date(); + newApprovalDate.setTime(100); + adapter.setApprovalDate(newApprovalDate); + assertEquals(null, adapter.getApprovalDate()); + var newEffectivePeriod = new Period() + .setStart(java.sql.Date.valueOf(LocalDate.parse("2021-01-01"))) + .setEnd(java.sql.Date.valueOf(LocalDate.parse("2021-12-31"))); + adapter.setEffectivePeriod(newEffectivePeriod); + assertNotEquals(newEffectivePeriod, adapter.getEffectivePeriod()); + } + @Test void adapter_get_experimental() { var structureDef = new StructureDefinition(); @@ -60,6 +133,27 @@ void adapter_get_experimental() { assertEquals(experimental, adapter.getExperimental()); } + @Test + void adapter_set_relatedArtifact() { + var structureDef = new StructureDefinition(); + var relatedArtifactList = List.of(new RelatedArtifact()); + var adapter = new StructureDefinitionAdapter(structureDef); + adapter.setRelatedArtifact(relatedArtifactList); + assertEquals(0, adapter.getRelatedArtifact().size()); + } + + @Test + void adapter_copy() { + var structureDef = new StructureDefinition().setStatus(PublicationStatus.DRAFT); + structureDef.setId("plan-1"); + var adapter = new StructureDefinitionAdapter(structureDef); + var copy = adapter.copy(); + copy.setId("plan-2"); + assertNotEquals(structureDef.getId(), copy.getId()); + structureDef.setStatus(PublicationStatus.ACTIVE); + assertNotEquals(adapter.getStatus(), copy.getStatus()); + } + @Test void adapter_get_all_dependencies() {} } diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/PlanDefinitionAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/PlanDefinitionAdapterTest.java index 73c49adc7..48639fc53 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/PlanDefinitionAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/PlanDefinitionAdapterTest.java @@ -1,6 +1,8 @@ package org.opencds.cqf.fhir.utility.adapter.r5; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doReturn; @@ -9,12 +11,18 @@ import static org.mockito.Mockito.verify; import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.model.primitive.IdDt; +import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; +import java.time.LocalDate; import java.util.Date; import java.util.List; import org.hl7.fhir.instance.model.api.IDomainResource; import org.hl7.fhir.r5.model.Bundle; import org.hl7.fhir.r5.model.CanonicalType; +import org.hl7.fhir.r5.model.Enumerations.PublicationStatus; import org.hl7.fhir.r5.model.Extension; +import org.hl7.fhir.r5.model.Library; +import org.hl7.fhir.r5.model.Period; import org.hl7.fhir.r5.model.PlanDefinition; import org.hl7.fhir.r5.model.RelatedArtifact; import org.junit.jupiter.api.Test; @@ -23,6 +31,11 @@ public class PlanDefinitionAdapterTest { private final FhirContext fhirContext = FhirContext.forR5Cached(); + @Test + void invalid_object_fails() { + assertThrows(IllegalArgumentException.class, () -> new PlanDefinitionAdapter(new Library())); + } + @Test void adapter_accepts_visitor() { var spyVisitor = spy(new PackageVisitor(fhirContext)); @@ -58,16 +71,59 @@ void adapter_get_and_set_url() { } @Test - void adapter_get_and_set_approvalDate() { + void adapter_get_and_set_version() { var planDef = new PlanDefinition(); + var version = "1.0.0"; + planDef.setVersion(version); + var adapter = new PlanDefinitionAdapter(planDef); + assertTrue(adapter.hasVersion()); + assertEquals(version, adapter.getVersion()); + var newVersion = "1.0.1"; + adapter.setVersion(newVersion); + assertEquals(newVersion, planDef.getVersion()); + } + + @Test + void adapter_get_and_set_status() { + var planDef = new PlanDefinition(); + var status = PublicationStatus.DRAFT; + planDef.setStatus(status); + var adapter = new PlanDefinitionAdapter(planDef); + assertEquals(status.toCode(), adapter.getStatus()); + assertThrows(UnprocessableEntityException.class, () -> adapter.setStatus("invalid-status")); + var newStatus = PublicationStatus.ACTIVE; + adapter.setStatus(newStatus.toCode()); + assertEquals(newStatus, PublicationStatus.fromCode(adapter.getStatus())); + } + + @Test + void adapter_get_and_set_dates() { + var planDef = new PlanDefinition(); + var date = new Date(); var approvalDate = new Date(); + var effectivePeriod = new Period() + .setStart(java.sql.Date.valueOf(LocalDate.parse("2020-01-01"))) + .setEnd(java.sql.Date.valueOf(LocalDate.parse("2020-12-31"))); + planDef.setDate(date); planDef.setApprovalDate(approvalDate); + planDef.setEffectivePeriod(effectivePeriod); var adapter = new PlanDefinitionAdapter(planDef); + assertEquals(date, planDef.getDate()); assertEquals(approvalDate, adapter.getApprovalDate()); + assertEquals(effectivePeriod, adapter.getEffectivePeriod()); + var newDate = new Date(); + newDate.setTime(100); + adapter.setDate(newDate); + assertEquals(newDate, planDef.getDate()); var newApprovalDate = new Date(); newApprovalDate.setTime(100); adapter.setApprovalDate(newApprovalDate); assertEquals(newApprovalDate, planDef.getApprovalDate()); + var newEffectivePeriod = new Period() + .setStart(java.sql.Date.valueOf(LocalDate.parse("2021-01-01"))) + .setEnd(java.sql.Date.valueOf(LocalDate.parse("2021-12-31"))); + adapter.setEffectivePeriod(newEffectivePeriod); + assertEquals(newEffectivePeriod, adapter.getEffectivePeriod()); } @Test @@ -89,6 +145,19 @@ void adapter_set_relatedArtifact() { assertEquals(relatedArtifactList, adapter.getRelatedArtifact()); } + @Test + void adapter_copy() { + var planDef = new PlanDefinition().setStatus(PublicationStatus.DRAFT); + planDef.setId("plan-1"); + var adapter = new PlanDefinitionAdapter(planDef); + var copy = adapter.copy(); + var adapterCopy = new PlanDefinitionAdapter(copy); + adapterCopy.setId(new IdDt("PlanDefinition", "plan-2")); + assertNotEquals(planDef.getId(), copy.getId()); + planDef.setStatus(PublicationStatus.ACTIVE); + assertNotEquals(adapter.getStatus(), copy.getStatus()); + } + @Test void adapter_get_all_dependencies() { var dependencies = List.of( diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapterTest.java index a4ac32c4f..f865afd17 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapterTest.java @@ -1,6 +1,9 @@ package org.opencds.cqf.fhir.utility.adapter.r5; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.spy; @@ -8,14 +11,27 @@ import static org.mockito.Mockito.verify; import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; +import java.time.LocalDate; +import java.util.Date; +import java.util.List; import org.hl7.fhir.instance.model.api.IDomainResource; import org.hl7.fhir.r5.model.Bundle; +import org.hl7.fhir.r5.model.Enumerations.PublicationStatus; +import org.hl7.fhir.r5.model.Library; +import org.hl7.fhir.r5.model.Period; import org.hl7.fhir.r5.model.Questionnaire; +import org.hl7.fhir.r5.model.RelatedArtifact; import org.junit.jupiter.api.Test; import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; public class QuestionnaireAdapterTest { - private final FhirContext fhirContext = FhirContext.forR4Cached(); + private final FhirContext fhirContext = FhirContext.forR5Cached(); + + @Test + void invalid_object_fails() { + assertThrows(IllegalArgumentException.class, () -> new QuestionnaireAdapter(new Library())); + } @Test void adapter_accepts_visitor() { @@ -51,6 +67,62 @@ void adapter_get_and_set_url() { assertEquals(newUrl, questionnaire.getUrl()); } + @Test + void adapter_get_and_set_version() { + var questionnaire = new Questionnaire(); + var version = "1.0.0"; + questionnaire.setVersion(version); + var adapter = new QuestionnaireAdapter(questionnaire); + assertTrue(adapter.hasVersion()); + assertEquals(version, adapter.getVersion()); + var newVersion = "1.0.1"; + adapter.setVersion(newVersion); + assertEquals(newVersion, questionnaire.getVersion()); + } + + @Test + void adapter_get_and_set_status() { + var questionnaire = new Questionnaire(); + var status = PublicationStatus.DRAFT; + questionnaire.setStatus(status); + var adapter = new QuestionnaireAdapter(questionnaire); + assertEquals(status.toCode(), adapter.getStatus()); + assertThrows(UnprocessableEntityException.class, () -> adapter.setStatus("invalid-status")); + var newStatus = PublicationStatus.ACTIVE; + adapter.setStatus(newStatus.toCode()); + assertEquals(newStatus, PublicationStatus.fromCode(adapter.getStatus())); + } + + @Test + void adapter_get_and_set_dates() { + var questionnaire = new Questionnaire(); + var date = new Date(); + var approvalDate = new Date(); + var effectivePeriod = new Period() + .setStart(java.sql.Date.valueOf(LocalDate.parse("2020-01-01"))) + .setEnd(java.sql.Date.valueOf(LocalDate.parse("2020-12-31"))); + questionnaire.setDate(date); + questionnaire.setApprovalDate(approvalDate); + questionnaire.setEffectivePeriod(effectivePeriod); + var adapter = new QuestionnaireAdapter(questionnaire); + assertEquals(date, questionnaire.getDate()); + assertEquals(approvalDate, adapter.getApprovalDate()); + assertEquals(effectivePeriod, adapter.getEffectivePeriod()); + var newDate = new Date(); + newDate.setTime(100); + adapter.setDate(newDate); + assertEquals(newDate, questionnaire.getDate()); + var newApprovalDate = new Date(); + newApprovalDate.setTime(100); + adapter.setApprovalDate(newApprovalDate); + assertEquals(newApprovalDate, questionnaire.getApprovalDate()); + var newEffectivePeriod = new Period() + .setStart(java.sql.Date.valueOf(LocalDate.parse("2021-01-01"))) + .setEnd(java.sql.Date.valueOf(LocalDate.parse("2021-12-31"))); + adapter.setEffectivePeriod(newEffectivePeriod); + assertEquals(newEffectivePeriod, adapter.getEffectivePeriod()); + } + @Test void adapter_get_experimental() { var questionnaire = new Questionnaire(); @@ -60,6 +132,27 @@ void adapter_get_experimental() { assertEquals(experimental, adapter.getExperimental()); } + @Test + void adapter_set_relatedArtifact() { + var questionnaire = new Questionnaire(); + var relatedArtifactList = List.of(new RelatedArtifact()); + var adapter = new QuestionnaireAdapter(questionnaire); + adapter.setRelatedArtifact(relatedArtifactList); + assertEquals(0, adapter.getRelatedArtifact().size()); + } + + @Test + void adapter_copy() { + var questionnaire = new Questionnaire().setStatus(PublicationStatus.DRAFT); + questionnaire.setId("plan-1"); + var adapter = new QuestionnaireAdapter(questionnaire); + var copy = adapter.copy(); + copy.setId("plan-2"); + assertNotEquals(questionnaire.getId(), copy.getId()); + questionnaire.setStatus(PublicationStatus.ACTIVE); + assertNotEquals(adapter.getStatus(), copy.getStatus()); + } + @Test void adapter_get_all_dependencies() {} } diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapterTest.java index 0b711199e..68033b49d 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapterTest.java @@ -1,6 +1,9 @@ package org.opencds.cqf.fhir.utility.adapter.r5; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.spy; @@ -8,14 +11,27 @@ import static org.mockito.Mockito.verify; import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; +import java.time.LocalDate; +import java.util.Date; +import java.util.List; import org.hl7.fhir.instance.model.api.IDomainResource; import org.hl7.fhir.r5.model.Bundle; +import org.hl7.fhir.r5.model.Enumerations.PublicationStatus; +import org.hl7.fhir.r5.model.Library; +import org.hl7.fhir.r5.model.Period; +import org.hl7.fhir.r5.model.RelatedArtifact; import org.hl7.fhir.r5.model.StructureDefinition; import org.junit.jupiter.api.Test; import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; public class StructureDefinitionAdapterTest { - private final FhirContext fhirContext = FhirContext.forR4Cached(); + private final FhirContext fhirContext = FhirContext.forR5Cached(); + + @Test + void invalid_object_fails() { + assertThrows(IllegalArgumentException.class, () -> new StructureDefinitionAdapter(new Library())); + } @Test void adapter_accepts_visitor() { @@ -23,6 +39,7 @@ void adapter_accepts_visitor() { doReturn(new Bundle()).when(spyVisitor).visit(any(StructureDefinitionAdapter.class), any(), any()); IDomainResource structureDef = new StructureDefinition(); var adapter = new StructureDefinitionAdapter(structureDef); + assertEquals(structureDef, adapter.get()); adapter.accept(spyVisitor, null, null); verify(spyVisitor, times(1)).visit(any(StructureDefinitionAdapter.class), any(), any()); } @@ -45,12 +62,68 @@ void adapter_get_and_set_url() { var url = "www.url.com"; structureDef.setUrl(url); var adapter = new StructureDefinitionAdapter(structureDef); + assertTrue(adapter.hasUrl()); assertEquals(url, adapter.getUrl()); var newUrl = "www.url2.com"; adapter.setUrl(newUrl); + assertTrue(adapter.hasUrl()); assertEquals(newUrl, structureDef.getUrl()); } + @Test + void adapter_get_and_set_version() { + var structureDef = new StructureDefinition(); + var version = "1.0.0"; + structureDef.setVersion(version); + var adapter = new StructureDefinitionAdapter(structureDef); + assertTrue(adapter.hasVersion()); + assertEquals(version, adapter.getVersion()); + var newVersion = "1.0.1"; + adapter.setVersion(newVersion); + assertEquals(newVersion, structureDef.getVersion()); + } + + @Test + void adapter_get_and_set_status() { + var structureDef = new StructureDefinition(); + var status = PublicationStatus.DRAFT; + structureDef.setStatus(status); + var adapter = new StructureDefinitionAdapter(structureDef); + assertEquals(status.toCode(), adapter.getStatus()); + assertThrows(UnprocessableEntityException.class, () -> adapter.setStatus("invalid-status")); + var newStatus = PublicationStatus.ACTIVE; + adapter.setStatus(newStatus.toCode()); + assertEquals(newStatus, PublicationStatus.fromCode(adapter.getStatus())); + } + + @Test + void adapter_get_and_set_dates() { + // StructureDefinition does not have fields approvalDate and effectivePeriod + var structureDef = new StructureDefinition(); + var date = new Date(); + var effectivePeriod = new Period() + .setStart(java.sql.Date.valueOf(LocalDate.parse("2020-01-01"))) + .setEnd(java.sql.Date.valueOf(LocalDate.parse("2020-12-31"))); + structureDef.setDate(date); + var adapter = new StructureDefinitionAdapter(structureDef); + assertEquals(date, structureDef.getDate()); + assertEquals(null, adapter.getApprovalDate()); + assertNotEquals(effectivePeriod, adapter.getEffectivePeriod()); + var newDate = new Date(); + newDate.setTime(100); + adapter.setDate(newDate); + assertEquals(newDate, structureDef.getDate()); + var newApprovalDate = new Date(); + newApprovalDate.setTime(100); + adapter.setApprovalDate(newApprovalDate); + assertEquals(null, adapter.getApprovalDate()); + var newEffectivePeriod = new Period() + .setStart(java.sql.Date.valueOf(LocalDate.parse("2021-01-01"))) + .setEnd(java.sql.Date.valueOf(LocalDate.parse("2021-12-31"))); + adapter.setEffectivePeriod(newEffectivePeriod); + assertNotEquals(newEffectivePeriod, adapter.getEffectivePeriod()); + } + @Test void adapter_get_experimental() { var structureDef = new StructureDefinition(); @@ -60,6 +133,27 @@ void adapter_get_experimental() { assertEquals(experimental, adapter.getExperimental()); } + @Test + void adapter_set_relatedArtifact() { + var structureDef = new StructureDefinition(); + var relatedArtifactList = List.of(new RelatedArtifact()); + var adapter = new StructureDefinitionAdapter(structureDef); + adapter.setRelatedArtifact(relatedArtifactList); + assertEquals(0, adapter.getRelatedArtifact().size()); + } + + @Test + void adapter_copy() { + var structureDef = new StructureDefinition().setStatus(PublicationStatus.DRAFT); + structureDef.setId("plan-1"); + var adapter = new StructureDefinitionAdapter(structureDef); + var copy = adapter.copy(); + copy.setId("plan-2"); + assertNotEquals(structureDef.getId(), copy.getId()); + structureDef.setStatus(PublicationStatus.ACTIVE); + assertNotEquals(adapter.getStatus(), copy.getStatus()); + } + @Test void adapter_get_all_dependencies() {} } From 5bec3b3c225d31eaa9eea481ececc97525b5d442 Mon Sep 17 00:00:00 2001 From: Brenin Rhodes Date: Tue, 2 Jul 2024 17:25:57 -0600 Subject: [PATCH 13/38] Add tests --- .../utility/adapter/BaseResourceAdapter.java | 4 + .../fhir/utility/adapter/EndpointAdapter.java | 12 +- .../adapter/KnowledgeArtifactAdapter.java | 76 +------ .../fhir/utility/adapter/ResourceAdapter.java | 91 ++++++++ .../fhir/utility/adapter/ValueSetAdapter.java | 10 + .../adapter/dstu3/EndpointAdapter.java | 17 +- .../utility/adapter/r4/EndpointAdapter.java | 17 +- .../utility/adapter/r5/EndpointAdapter.java | 19 -- .../visitor/DataRequirementsVisitor.java | 137 ------------ .../adapter/dstu3/EndpointAdapterTest.java | 27 +++ .../adapter/dstu3/LibraryAdapterTest.java | 206 +++++++++++++++++ .../dstu3/PlanDefinitionAdapterTest.java | 2 +- .../dstu3/QuestionnaireAdapterTest.java | 2 +- .../dstu3/StructureDefinitionAdapterTest.java | 2 +- .../adapter/dstu3/ValueSetAdapterTest.java | 159 ++++++++++++++ .../adapter/r4/EndpointAdapterTest.java | 27 +++ .../adapter/r4/LibraryAdapterTest.java | 206 +++++++++++++++++ .../adapter/r4/PlanDefinitionAdapterTest.java | 2 +- .../adapter/r4/QuestionnaireAdapterTest.java | 2 +- .../r4/StructureDefinitionAdapterTest.java | 2 +- .../adapter/r4/ValueSetAdapterTest.java | 159 ++++++++++++++ .../adapter/r5/EndpointAdapterTest.java | 27 +++ .../adapter/r5/LibraryAdapterTest.java | 207 ++++++++++++++++++ .../adapter/r5/PlanDefinitionAdapterTest.java | 2 +- .../adapter/r5/QuestionnaireAdapterTest.java | 2 +- .../r5/StructureDefinitionAdapterTest.java | 2 +- .../adapter/r5/ValueSetAdapterTest.java | 162 ++++++++++++++ 27 files changed, 1312 insertions(+), 269 deletions(-) delete mode 100644 cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/DataRequirementsVisitor.java create mode 100644 cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/EndpointAdapterTest.java create mode 100644 cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/LibraryAdapterTest.java create mode 100644 cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ValueSetAdapterTest.java create mode 100644 cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/EndpointAdapterTest.java create mode 100644 cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/LibraryAdapterTest.java create mode 100644 cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/ValueSetAdapterTest.java create mode 100644 cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/EndpointAdapterTest.java create mode 100644 cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/LibraryAdapterTest.java create mode 100644 cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ValueSetAdapterTest.java diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/BaseResourceAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/BaseResourceAdapter.java index 65b11e9a9..6876e61f5 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/BaseResourceAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/BaseResourceAdapter.java @@ -20,6 +20,10 @@ public BaseResourceAdapter(IBaseResource resource) { fhirContext.getVersion().getVersion()); } + public FhirContext fhirContext() { + return fhirContext; + } + public ModelResolver getModelResolver() { return modelResolver; } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/EndpointAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/EndpointAdapter.java index db6098131..f213aca33 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/EndpointAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/EndpointAdapter.java @@ -1,7 +1,15 @@ package org.opencds.cqf.fhir.utility.adapter; +import static org.opencds.cqf.fhir.utility.adapter.ResourceAdapter.newUrlType; + public interface EndpointAdapter extends ResourceAdapter { - public String getAddress(); + public default String getAddress() { + return resolvePathString(get(), "address"); + } - public EndpointAdapter setAddress(String address); + public default void setAddress(String address) { + getModelResolver() + .setValue( + get(), "address", newUrlType(fhirContext().getVersion().getVersion(), address)); + } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/KnowledgeArtifactAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/KnowledgeArtifactAdapter.java index fb8edfd54..8a647e1aa 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/KnowledgeArtifactAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/KnowledgeArtifactAdapter.java @@ -1,5 +1,11 @@ package org.opencds.cqf.fhir.utility.adapter; +import static org.opencds.cqf.fhir.utility.adapter.ResourceAdapter.newDateTimeType; +import static org.opencds.cqf.fhir.utility.adapter.ResourceAdapter.newDateType; +import static org.opencds.cqf.fhir.utility.adapter.ResourceAdapter.newPeriod; +import static org.opencds.cqf.fhir.utility.adapter.ResourceAdapter.newStringType; +import static org.opencds.cqf.fhir.utility.adapter.ResourceAdapter.newUriType; + import ca.uhn.fhir.context.FhirVersionEnum; import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; import java.util.Date; @@ -131,76 +137,6 @@ default boolean getExperimental() { return experimental == null ? false : experimental.getValue(); } - @SuppressWarnings("unchecked") - static T newPeriod(FhirVersionEnum version) { - switch (version) { - case DSTU3: - return (T) new org.hl7.fhir.dstu3.model.Period(); - case R4: - return (T) new org.hl7.fhir.r4.model.Period(); - case R5: - return (T) new org.hl7.fhir.r5.model.Period(); - default: - throw new UnprocessableEntityException("Unsupported version: " + version.toString()); - } - } - - @SuppressWarnings("unchecked") - static > T newStringType(FhirVersionEnum version, String string) { - switch (version) { - case DSTU3: - return (T) new org.hl7.fhir.dstu3.model.StringType(string); - case R4: - return (T) new org.hl7.fhir.r4.model.StringType(string); - case R5: - return (T) new org.hl7.fhir.r5.model.StringType(string); - default: - throw new UnprocessableEntityException("Unsupported version: " + version.toString()); - } - } - - @SuppressWarnings("unchecked") - static > T newUriType(FhirVersionEnum version, String string) { - switch (version) { - case DSTU3: - return (T) new org.hl7.fhir.dstu3.model.UriType(string); - case R4: - return (T) new org.hl7.fhir.r4.model.UriType(string); - case R5: - return (T) new org.hl7.fhir.r5.model.UriType(string); - default: - throw new UnprocessableEntityException("Unsupported version: " + version.toString()); - } - } - - @SuppressWarnings("unchecked") - static > T newDateType(FhirVersionEnum version, Date date) { - switch (version) { - case DSTU3: - return (T) new org.hl7.fhir.dstu3.model.DateType(date); - case R4: - return (T) new org.hl7.fhir.r4.model.DateType(date); - case R5: - return (T) new org.hl7.fhir.r5.model.DateType(date); - default: - throw new UnprocessableEntityException("Unsupported version: " + version.toString()); - } - } - - @SuppressWarnings("unchecked") - static > T newDateTimeType(FhirVersionEnum version, Date date) { - switch (version) { - case DSTU3: - return (T) new org.hl7.fhir.dstu3.model.DateTimeType(date); - case R4: - return (T) new org.hl7.fhir.r4.model.DateTimeType(date); - case R5: - return (T) new org.hl7.fhir.r5.model.DateTimeType(date); - default: - throw new UnprocessableEntityException("Unsupported version: " + version.toString()); - } - } - @SuppressWarnings("unchecked") static T newRelatedArtifact( FhirVersionEnum version, String type, String reference) { diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/ResourceAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/ResourceAdapter.java index 4774fb74b..f73fc74e1 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/ResourceAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/ResourceAdapter.java @@ -1,12 +1,17 @@ package org.opencds.cqf.fhir.utility.adapter; +import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.context.FhirVersionEnum; +import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; import java.util.ArrayList; +import java.util.Date; import java.util.List; import java.util.stream.Collectors; import org.hl7.fhir.exceptions.FHIRException; import org.hl7.fhir.instance.model.api.IBase; import org.hl7.fhir.instance.model.api.IBaseExtension; import org.hl7.fhir.instance.model.api.IBaseResource; +import org.hl7.fhir.instance.model.api.ICompositeType; import org.hl7.fhir.instance.model.api.IPrimitiveType; import org.opencds.cqf.cql.engine.model.ModelResolver; @@ -60,6 +65,8 @@ public default boolean hasContained() { return hasContained(get()); } + public FhirContext fhirContext(); + public ModelResolver getModelResolver(); @SuppressWarnings("unchecked") @@ -112,4 +119,88 @@ public default IBase resolvePath(IBase base, String path) { public default T resolvePath(IBase base, String path, Class clazz) { return (T) resolvePath(base, path); } + + @SuppressWarnings("unchecked") + static T newPeriod(FhirVersionEnum version) { + switch (version) { + case DSTU3: + return (T) new org.hl7.fhir.dstu3.model.Period(); + case R4: + return (T) new org.hl7.fhir.r4.model.Period(); + case R5: + return (T) new org.hl7.fhir.r5.model.Period(); + default: + throw new UnprocessableEntityException("Unsupported version: " + version.toString()); + } + } + + @SuppressWarnings("unchecked") + static > T newStringType(FhirVersionEnum version, String string) { + switch (version) { + case DSTU3: + return (T) new org.hl7.fhir.dstu3.model.StringType(string); + case R4: + return (T) new org.hl7.fhir.r4.model.StringType(string); + case R5: + return (T) new org.hl7.fhir.r5.model.StringType(string); + default: + throw new UnprocessableEntityException("Unsupported version: " + version.toString()); + } + } + + @SuppressWarnings("unchecked") + static > T newUriType(FhirVersionEnum version, String string) { + switch (version) { + case DSTU3: + return (T) new org.hl7.fhir.dstu3.model.UriType(string); + case R4: + return (T) new org.hl7.fhir.r4.model.UriType(string); + case R5: + return (T) new org.hl7.fhir.r5.model.UriType(string); + default: + throw new UnprocessableEntityException("Unsupported version: " + version.toString()); + } + } + + @SuppressWarnings("unchecked") + static > T newUrlType(FhirVersionEnum version, String string) { + switch (version) { + case DSTU3: + return (T) new org.hl7.fhir.dstu3.model.UriType(string); + case R4: + return (T) new org.hl7.fhir.r4.model.UrlType(string); + case R5: + return (T) new org.hl7.fhir.r5.model.UrlType(string); + default: + throw new UnprocessableEntityException("Unsupported version: " + version.toString()); + } + } + + @SuppressWarnings("unchecked") + static > T newDateType(FhirVersionEnum version, Date date) { + switch (version) { + case DSTU3: + return (T) new org.hl7.fhir.dstu3.model.DateType(date); + case R4: + return (T) new org.hl7.fhir.r4.model.DateType(date); + case R5: + return (T) new org.hl7.fhir.r5.model.DateType(date); + default: + throw new UnprocessableEntityException("Unsupported version: " + version.toString()); + } + } + + @SuppressWarnings("unchecked") + static > T newDateTimeType(FhirVersionEnum version, Date date) { + switch (version) { + case DSTU3: + return (T) new org.hl7.fhir.dstu3.model.DateTimeType(date); + case R4: + return (T) new org.hl7.fhir.r4.model.DateTimeType(date); + case R5: + return (T) new org.hl7.fhir.r5.model.DateTimeType(date); + default: + throw new UnprocessableEntityException("Unsupported version: " + version.toString()); + } + } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/ValueSetAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/ValueSetAdapter.java index 366d81a8f..ef60953c4 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/ValueSetAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/ValueSetAdapter.java @@ -15,6 +15,16 @@ public interface ValueSetAdapter extends KnowledgeArtifactAdapter { public List getValueSetIncludes(); + // TODO: lots of duplicate code across version adapters + // public default List getValueSetIncludes() { + // return ValueSets.getIncludes(fhirContext(), get()).stream().map(i -> + // (List) resolvePathList(i, "valueSet", IPrimitiveType.class) + // ) + // .flatMap(Collection::stream) + // .map(i -> i.getValueAsString()) + // .collect(Collectors.toList()); + // } + /** * A simple compose element of a ValueSet must have a compose without an exclude element. Each element of the * include cannot have a filter or reference a ValueSet and must have a system and enumerate concepts. diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/EndpointAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/EndpointAdapter.java index 09f267327..d3b13dc59 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/EndpointAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/EndpointAdapter.java @@ -4,33 +4,18 @@ import org.hl7.fhir.instance.model.api.IBaseResource; public class EndpointAdapter extends ResourceAdapter implements org.opencds.cqf.fhir.utility.adapter.EndpointAdapter { - private final Endpoint endpoint; - public EndpointAdapter(IBaseResource endpoint) { super(endpoint); if (!(endpoint instanceof Endpoint)) { throw new IllegalArgumentException("resource passed as endpoint argument is not an Endpoint resource"); } - this.endpoint = (Endpoint) endpoint; } public EndpointAdapter(Endpoint endpoint) { super(endpoint); - this.endpoint = endpoint; } public Endpoint getEndpoint() { - return endpoint; - } - - @Override - public String getAddress() { - return endpoint.getAddress(); - } - - @Override - public EndpointAdapter setAddress(String address) { - endpoint.setAddress(address); - return this; + return (Endpoint) resource; } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/EndpointAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/EndpointAdapter.java index 520b73d17..32b557065 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/EndpointAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/EndpointAdapter.java @@ -4,33 +4,18 @@ import org.hl7.fhir.r4.model.Endpoint; public class EndpointAdapter extends ResourceAdapter implements org.opencds.cqf.fhir.utility.adapter.EndpointAdapter { - private final Endpoint endpoint; - public EndpointAdapter(IBaseResource endpoint) { super(endpoint); if (!(endpoint instanceof Endpoint)) { throw new IllegalArgumentException("resource passed as endpoint argument is not an Endpoint resource"); } - this.endpoint = (Endpoint) endpoint; } public EndpointAdapter(Endpoint endpoint) { super(endpoint); - this.endpoint = endpoint; } public Endpoint getEndpoint() { - return endpoint; - } - - @Override - public String getAddress() { - return endpoint.getAddress(); - } - - @Override - public EndpointAdapter setAddress(String address) { - endpoint.setAddress(address); - return this; + return (Endpoint) resource; } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/EndpointAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/EndpointAdapter.java index 35c14f4ff..48e22e1a8 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/EndpointAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/EndpointAdapter.java @@ -4,33 +4,14 @@ import org.hl7.fhir.r5.model.Endpoint; public class EndpointAdapter extends ResourceAdapter implements org.opencds.cqf.fhir.utility.adapter.EndpointAdapter { - private final Endpoint endpoint; - public EndpointAdapter(IBaseResource endpoint) { super(endpoint); if (!(endpoint instanceof Endpoint)) { throw new IllegalArgumentException("resource passed as endpoint argument is not an Endpoint resource"); } - this.endpoint = (Endpoint) endpoint; } public EndpointAdapter(Endpoint endpoint) { super(endpoint); - this.endpoint = endpoint; - } - - public Endpoint getEndpoint() { - return endpoint; - } - - @Override - public String getAddress() { - return endpoint.getAddress(); - } - - @Override - public EndpointAdapter setAddress(String address) { - endpoint.setAddress(address); - return this; } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/DataRequirementsVisitor.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/DataRequirementsVisitor.java deleted file mode 100644 index 18a69d1bc..000000000 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/DataRequirementsVisitor.java +++ /dev/null @@ -1,137 +0,0 @@ -package org.opencds.cqf.fhir.utility.visitor; - -import static org.opencds.cqf.fhir.utility.visitor.VisitorHelper.findUnsupportedCapability; -import static org.opencds.cqf.fhir.utility.visitor.VisitorHelper.processCanonicals; - -import ca.uhn.fhir.parser.DataFormatException; -import ca.uhn.fhir.rest.server.exceptions.NotImplementedOperationException; -import ca.uhn.fhir.rest.server.exceptions.PreconditionFailedException; -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; -import java.util.stream.Collectors; -import org.apache.commons.lang3.StringUtils; -import org.hl7.fhir.instance.model.api.IBase; -import org.hl7.fhir.instance.model.api.IBaseParameters; -import org.hl7.fhir.instance.model.api.IBaseResource; -import org.hl7.fhir.instance.model.api.ICompositeType; -import org.hl7.fhir.instance.model.api.IDomainResource; -import org.hl7.fhir.instance.model.api.IPrimitiveType; -import org.opencds.cqf.fhir.api.Repository; -import org.opencds.cqf.fhir.utility.BundleHelper; -import org.opencds.cqf.fhir.utility.Canonicals; -import org.opencds.cqf.fhir.utility.Resources; -import org.opencds.cqf.fhir.utility.SearchHelper; -import org.opencds.cqf.fhir.utility.adapter.AdapterFactory; -import org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter; -import org.opencds.cqf.fhir.utility.adapter.LibraryAdapter; - -public class DataRequirementsVisitor implements KnowledgeArtifactVisitor { - - @Override - public IBase visit(KnowledgeArtifactAdapter adapter, Repository repository, IBaseParameters parameters) { - var fhirVersion = adapter.get().getStructureFhirVersionEnum(); - Optional artifactRoute = VisitorHelper.getParameter("artifactRoute", parameters, IPrimitiveType.class) - .map(r -> (String) r.getValue()); - Optional endpointUri = VisitorHelper.getParameter("endpointUri", parameters, IPrimitiveType.class) - .map(r -> (String) r.getValue()); - Optional endpoint = - VisitorHelper.getResourceParameter("endpoint", parameters, IBaseResource.class); - Optional terminologyEndpoint = - VisitorHelper.getResourceParameter("terminologyEndpoint", parameters, IBaseResource.class); - List artifactVersion = VisitorHelper.getListParameter( - "artifactVersion", parameters, IPrimitiveType.class) - .map(l -> l.stream().map(t -> (String) t.getValue()).collect(Collectors.toList())) - .orElseGet(() -> new ArrayList<>()); - List checkArtifactVersion = VisitorHelper.getListParameter( - "checkArtifactVersion", parameters, IPrimitiveType.class) - .map(l -> l.stream().map(t -> (String) t.getValue()).collect(Collectors.toList())) - .orElseGet(() -> new ArrayList<>()); - List forceArtifactVersion = VisitorHelper.getListParameter( - "forceArtifactVersion", parameters, IPrimitiveType.class) - .map(l -> l.stream().map(t -> (String) t.getValue()).collect(Collectors.toList())) - .orElseGet(() -> new ArrayList<>()); - - if ((artifactRoute.isPresent() - && !StringUtils.isBlank(artifactRoute.get()) - && !artifactRoute.get().isEmpty()) - || (endpointUri.isPresent() - && !StringUtils.isBlank(endpointUri.get()) - && !endpointUri.get().isEmpty()) - || endpoint.isPresent()) { - throw new NotImplementedOperationException( - "This repository is not implementing custom Content and endpoints at this time"); - } - - var resource = adapter.get(); - var library = Resources.newResource( - repository.fhirContext().getResourceDefinition("Library").getImplementingClass()); - var libraryAdapter = AdapterFactory.forFhirVersion(fhirVersion).createLibrary(library); - libraryAdapter.setType("module-definition"); - recursiveGather( - resource, - libraryAdapter, - repository, - forceArtifactVersion, - forceArtifactVersion, - artifactVersion, - checkArtifactVersion, - forceArtifactVersion); - return libraryAdapter.get(); - } - - protected void recursiveGather( - IDomainResource resource, - LibraryAdapter libraryAdapter, - Repository repository, - List capability, - List include, - List artifactVersion, - List checkArtifactVersion, - List forceArtifactVersion) - throws PreconditionFailedException { - if (resource != null) { - var fhirVersion = resource.getStructureFhirVersionEnum(); - var adapter = AdapterFactory.forFhirVersion(fhirVersion).createKnowledgeArtifactAdapter(resource); - findUnsupportedCapability(adapter, capability); - processCanonicals(adapter, artifactVersion, checkArtifactVersion, forceArtifactVersion); - - // TODO: Handle duplicates - gatherRequirements(adapter).forEach(dr -> libraryAdapter.addDataRequirement(dr)); - - adapter.combineComponentsAndDependencies().stream() - // sometimes VS dependencies aren't FHIR resources - .filter(ra -> !StringUtils.isBlank(Canonicals.getResourceType(ra.getReference()))) - .filter(ra -> { - try { - var resourceDef = repository - .fhirContext() - .getResourceDefinition(Canonicals.getResourceType(ra.getReference())); - return resourceDef != null; - } catch (DataFormatException e) { - if (e.getMessage().contains("1684")) { - return false; - } else { - throw new DataFormatException(e.getMessage()); - } - } - }) - .map(ra -> SearchHelper.searchRepositoryByCanonicalWithPaging(repository, ra.getReference())) - .map(searchBundle -> (IDomainResource) BundleHelper.getEntryResourceFirstRep(searchBundle)) - .forEach(component -> recursiveGather( - component, - libraryAdapter, - repository, - capability, - include, - artifactVersion, - checkArtifactVersion, - forceArtifactVersion)); - } - } - - protected List gatherRequirements(KnowledgeArtifactAdapter adapter) { - // TODO: Refactor R4MeasureDataRequirementsService - return new ArrayList<>(); - } -} diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/EndpointAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/EndpointAdapterTest.java new file mode 100644 index 000000000..847525e2b --- /dev/null +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/EndpointAdapterTest.java @@ -0,0 +1,27 @@ +package org.opencds.cqf.fhir.utility.adapter.dstu3; + +import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.hl7.fhir.dstu3.model.Endpoint; +import org.hl7.fhir.dstu3.model.PlanDefinition; +import org.junit.jupiter.api.Test; + +public class EndpointAdapterTest { + @Test + void invalid_object_fails() { + assertThrows(IllegalArgumentException.class, () -> new LibraryAdapter(new PlanDefinition())); + } + + @Test + void adapter_get_and_set_address() { + var endpoint = new Endpoint(); + var address = "123 Test Street"; + endpoint.setAddress(address); + var adapter = new EndpointAdapter(endpoint); + assertEquals(address, adapter.getAddress()); + var newAddress = "456 Test Street"; + adapter.setAddress(newAddress); + assertEquals(newAddress, endpoint.getAddress()); + } +} diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/LibraryAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/LibraryAdapterTest.java new file mode 100644 index 000000000..dbcd28010 --- /dev/null +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/LibraryAdapterTest.java @@ -0,0 +1,206 @@ +package org.opencds.cqf.fhir.utility.adapter.dstu3; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.model.primitive.IdDt; +import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Date; +import java.util.List; +import org.hl7.fhir.dstu3.model.Bundle; +import org.hl7.fhir.dstu3.model.CodeableConcept; +import org.hl7.fhir.dstu3.model.Coding; +import org.hl7.fhir.dstu3.model.DataRequirement; +import org.hl7.fhir.dstu3.model.Enumerations.PublicationStatus; +import org.hl7.fhir.dstu3.model.Library; +import org.hl7.fhir.dstu3.model.Period; +import org.hl7.fhir.dstu3.model.PlanDefinition; +import org.hl7.fhir.dstu3.model.RelatedArtifact; +import org.hl7.fhir.dstu3.model.UsageContext; +import org.hl7.fhir.instance.model.api.IDomainResource; +import org.junit.jupiter.api.Test; +import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; + +public class LibraryAdapterTest { + private final FhirContext fhirContext = FhirContext.forDstu3Cached(); + + @Test + void invalid_object_fails() { + assertThrows(IllegalArgumentException.class, () -> new LibraryAdapter(new PlanDefinition())); + } + + @Test + void adapter_accepts_visitor() { + var spyVisitor = spy(new PackageVisitor(fhirContext)); + doReturn(new Bundle()).when(spyVisitor).visit(any(LibraryAdapter.class), any(), any()); + IDomainResource library = new Library(); + var adapter = new LibraryAdapter(library); + adapter.accept(spyVisitor, null, null); + verify(spyVisitor, times(1)).visit(any(LibraryAdapter.class), any(), any()); + } + + @Test + void adapter_get_and_set_name() { + var library = new Library(); + var name = "name"; + library.setName(name); + var adapter = new LibraryAdapter(library); + assertEquals(name, adapter.getName()); + var newName = "name2"; + adapter.setName(newName); + assertEquals(newName, library.getName()); + } + + @Test + void adapter_get_and_set_url() { + var library = new Library(); + var url = "www.url.com"; + library.setUrl(url); + var adapter = new LibraryAdapter(library); + assertEquals(url, adapter.getUrl()); + var newUrl = "www.url2.com"; + adapter.setUrl(newUrl); + assertEquals(newUrl, library.getUrl()); + } + + @Test + void adapter_get_and_set_version() { + var library = new Library(); + var version = "1.0.0"; + library.setVersion(version); + var adapter = new LibraryAdapter(library); + assertTrue(adapter.hasVersion()); + assertEquals(version, adapter.getVersion()); + var newVersion = "1.0.1"; + adapter.setVersion(newVersion); + assertEquals(newVersion, library.getVersion()); + } + + @Test + void adapter_get_and_set_status() { + var library = new Library(); + var status = PublicationStatus.DRAFT; + library.setStatus(status); + var adapter = new LibraryAdapter(library); + assertEquals(status.toCode(), adapter.getStatus()); + assertThrows(UnprocessableEntityException.class, () -> adapter.setStatus("invalid-status")); + var newStatus = PublicationStatus.ACTIVE; + adapter.setStatus(newStatus.toCode()); + assertEquals(newStatus, PublicationStatus.fromCode(adapter.getStatus())); + } + + @Test + void adapter_get_and_set_dates() { + var library = new Library(); + var date = new Date(); + var approvalDate = new Date(); + var effectivePeriod = new Period() + .setStart(java.sql.Date.valueOf(LocalDate.parse("2020-01-01"))) + .setEnd(java.sql.Date.valueOf(LocalDate.parse("2020-12-31"))); + library.setDate(date); + library.setApprovalDate(approvalDate); + library.setEffectivePeriod(effectivePeriod); + var adapter = new LibraryAdapter(library); + assertEquals(date, adapter.getDate()); + assertEquals(approvalDate, adapter.getApprovalDate()); + assertEquals(effectivePeriod, adapter.getEffectivePeriod()); + var newDate = new Date(); + newDate.setTime(100); + adapter.setDate(newDate); + assertEquals(newDate, library.getDate()); + var newApprovalDate = new Date(); + newApprovalDate.setTime(100); + adapter.setApprovalDate(newApprovalDate); + assertEquals(newApprovalDate, library.getApprovalDate()); + var newEffectivePeriod = new Period() + .setStart(java.sql.Date.valueOf(LocalDate.parse("2021-01-01"))) + .setEnd(java.sql.Date.valueOf(LocalDate.parse("2021-12-31"))); + adapter.setEffectivePeriod(newEffectivePeriod); + assertEquals(newEffectivePeriod, adapter.getEffectivePeriod()); + } + + @Test + void adapter_get_experimental() { + var library = new Library(); + var experimental = true; + library.setExperimental(experimental); + var adapter = new LibraryAdapter(library); + assertEquals(experimental, adapter.getExperimental()); + } + + @Test + void adapter_set_relatedArtifact() { + var library = new Library(); + var relatedArtifactList = List.of(new RelatedArtifact()); + var adapter = new LibraryAdapter(library); + adapter.setRelatedArtifact(relatedArtifactList); + assertEquals(relatedArtifactList, library.getRelatedArtifact()); + assertEquals(relatedArtifactList, adapter.getRelatedArtifact()); + } + + @Test + void adapter_copy() { + var library = new Library().setStatus(PublicationStatus.DRAFT); + library.setId("library-1"); + var adapter = new LibraryAdapter(library); + var copy = adapter.copy(); + var adapterCopy = new LibraryAdapter(copy); + adapterCopy.setId(new IdDt("Library", "library-2")); + assertNotEquals(library.getId(), copy.getId()); + library.setStatus(PublicationStatus.ACTIVE); + assertNotEquals(adapter.getStatus(), copy.getStatus()); + } + + @Test + void adapter_get_all_dependencies() {} + + @Test + void adapter_get_and_set_type() { + var type = new CodeableConcept(new Coding("www.test.com", "test", "Test")); + var library = new Library().setType(type); + var adapter = new LibraryAdapter(library); + assertEquals(type, adapter.getType()); + assertThrows(UnprocessableEntityException.class, () -> adapter.setType("test")); + var newType = new CodeableConcept(new Coding("http://hl7.org/fhir/ValueSet/library-type", "logic-library", "")); + adapter.setType(newType.getCoding().get(0).getCode()); + assertEquals( + newType.getCoding().get(0).getCode(), + ((CodeableConcept) adapter.getType()).getCoding().get(0).getCode()); + assertEquals( + newType.getCoding().get(0).getSystem(), + ((CodeableConcept) adapter.getType()).getCoding().get(0).getSystem()); + } + + @Test + void adapter_get_and_set_dataRequirement() { + var library = new Library(); + var dataRequirements = new ArrayList(); + dataRequirements.add(new DataRequirement().setType("Patient")); + library.setDataRequirement(dataRequirements); + var adapter = new LibraryAdapter(library); + assertEquals(dataRequirements, adapter.getDataRequirement()); + adapter.addDataRequirement(new DataRequirement().setType("Observation")); + assertEquals(library.getDataRequirement(), adapter.getDataRequirement()); + assertEquals(2, adapter.getDataRequirement().size()); + } + + @Test + void adapter_get_useContext() { + var library = new Library(); + var useContext = new UsageContext().setCode(new Coding("www.test.com", "test", "Test")); + library.setUseContext(Collections.singletonList(useContext)); + var adapter = new LibraryAdapter(library); + assertEquals(useContext, adapter.getUseContext().get(0)); + } +} diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/PlanDefinitionAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/PlanDefinitionAdapterTest.java index d05171de1..59df4f2c6 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/PlanDefinitionAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/PlanDefinitionAdapterTest.java @@ -110,7 +110,7 @@ void adapter_get_and_set_dates() { planDef.setApprovalDate(approvalDate); planDef.setEffectivePeriod(effectivePeriod); var adapter = new PlanDefinitionAdapter(planDef); - assertEquals(date, planDef.getDate()); + assertEquals(date, adapter.getDate()); assertEquals(approvalDate, adapter.getApprovalDate()); assertEquals(effectivePeriod, adapter.getEffectivePeriod()); var newDate = new Date(); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/QuestionnaireAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/QuestionnaireAdapterTest.java index 4d4c95ae2..91f922a24 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/QuestionnaireAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/QuestionnaireAdapterTest.java @@ -105,7 +105,7 @@ void adapter_get_and_set_dates() { questionnaire.setApprovalDate(approvalDate); questionnaire.setEffectivePeriod(effectivePeriod); var adapter = new QuestionnaireAdapter(questionnaire); - assertEquals(date, questionnaire.getDate()); + assertEquals(date, adapter.getDate()); assertEquals(approvalDate, adapter.getApprovalDate()); assertEquals(effectivePeriod, adapter.getEffectivePeriod()); var newDate = new Date(); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/StructureDefinitionAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/StructureDefinitionAdapterTest.java index 4e093e680..124c09b22 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/StructureDefinitionAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/StructureDefinitionAdapterTest.java @@ -106,7 +106,7 @@ void adapter_get_and_set_dates() { .setEnd(java.sql.Date.valueOf(LocalDate.parse("2020-12-31"))); structureDef.setDate(date); var adapter = new StructureDefinitionAdapter(structureDef); - assertEquals(date, structureDef.getDate()); + assertEquals(date, adapter.getDate()); assertEquals(null, adapter.getApprovalDate()); assertNotEquals(effectivePeriod, adapter.getEffectivePeriod()); var newDate = new Date(); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ValueSetAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ValueSetAdapterTest.java new file mode 100644 index 000000000..4f8e62ec8 --- /dev/null +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ValueSetAdapterTest.java @@ -0,0 +1,159 @@ +package org.opencds.cqf.fhir.utility.adapter.dstu3; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; +import java.time.LocalDate; +import java.util.Date; +import java.util.List; +import org.hl7.fhir.dstu3.model.Bundle; +import org.hl7.fhir.dstu3.model.Enumerations.PublicationStatus; +import org.hl7.fhir.dstu3.model.Library; +import org.hl7.fhir.dstu3.model.Period; +import org.hl7.fhir.dstu3.model.RelatedArtifact; +import org.hl7.fhir.dstu3.model.ValueSet; +import org.hl7.fhir.instance.model.api.IDomainResource; +import org.junit.jupiter.api.Test; +import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; + +public class ValueSetAdapterTest { + private final FhirContext fhirContext = FhirContext.forDstu3Cached(); + + @Test + void invalid_object_fails() { + assertThrows(IllegalArgumentException.class, () -> new ValueSetAdapter(new Library())); + } + + @Test + void adapter_accepts_visitor() { + var spyVisitor = spy(new PackageVisitor(fhirContext)); + doReturn(new Bundle()).when(spyVisitor).visit(any(ValueSetAdapter.class), any(), any()); + IDomainResource valueSet = new ValueSet(); + var adapter = new ValueSetAdapter(valueSet); + assertEquals(valueSet, adapter.get()); + adapter.accept(spyVisitor, null, null); + verify(spyVisitor, times(1)).visit(any(ValueSetAdapter.class), any(), any()); + } + + @Test + void adapter_get_and_set_name() { + var valueSet = new ValueSet(); + var name = "name"; + valueSet.setName(name); + var adapter = new ValueSetAdapter(valueSet); + assertEquals(name, adapter.getName()); + var newName = "name2"; + adapter.setName(newName); + assertEquals(newName, valueSet.getName()); + } + + @Test + void adapter_get_and_set_url() { + var valueSet = new ValueSet(); + var url = "www.url.com"; + valueSet.setUrl(url); + var adapter = new ValueSetAdapter(valueSet); + assertTrue(adapter.hasUrl()); + assertEquals(url, adapter.getUrl()); + var newUrl = "www.url2.com"; + adapter.setUrl(newUrl); + assertTrue(adapter.hasUrl()); + assertEquals(newUrl, valueSet.getUrl()); + } + + @Test + void adapter_get_and_set_version() { + var valueSet = new ValueSet(); + var version = "1.0.0"; + valueSet.setVersion(version); + var adapter = new ValueSetAdapter(valueSet); + assertTrue(adapter.hasVersion()); + assertEquals(version, adapter.getVersion()); + var newVersion = "1.0.1"; + adapter.setVersion(newVersion); + assertEquals(newVersion, valueSet.getVersion()); + } + + @Test + void adapter_get_and_set_status() { + var valueSet = new ValueSet(); + var status = PublicationStatus.DRAFT; + valueSet.setStatus(status); + var adapter = new ValueSetAdapter(valueSet); + assertEquals(status.toCode(), adapter.getStatus()); + assertThrows(UnprocessableEntityException.class, () -> adapter.setStatus("invalid-status")); + var newStatus = PublicationStatus.ACTIVE; + adapter.setStatus(newStatus.toCode()); + assertEquals(newStatus, PublicationStatus.fromCode(adapter.getStatus())); + } + + @Test + void adapter_get_and_set_dates() { + // ValueSet does not have fields approvalDate and effectivePeriod + var valueSet = new ValueSet(); + var date = new Date(); + var effectivePeriod = new Period() + .setStart(java.sql.Date.valueOf(LocalDate.parse("2020-01-01"))) + .setEnd(java.sql.Date.valueOf(LocalDate.parse("2020-12-31"))); + valueSet.setDate(date); + var adapter = new ValueSetAdapter(valueSet); + assertEquals(date, adapter.getDate()); + assertEquals(null, adapter.getApprovalDate()); + assertNotEquals(effectivePeriod, adapter.getEffectivePeriod()); + var newDate = new Date(); + newDate.setTime(100); + adapter.setDate(newDate); + assertEquals(newDate, valueSet.getDate()); + var newApprovalDate = new Date(); + newApprovalDate.setTime(100); + adapter.setApprovalDate(newApprovalDate); + assertEquals(null, adapter.getApprovalDate()); + var newEffectivePeriod = new Period() + .setStart(java.sql.Date.valueOf(LocalDate.parse("2021-01-01"))) + .setEnd(java.sql.Date.valueOf(LocalDate.parse("2021-12-31"))); + adapter.setEffectivePeriod(newEffectivePeriod); + assertNotEquals(newEffectivePeriod, adapter.getEffectivePeriod()); + } + + @Test + void adapter_get_experimental() { + var valueSet = new ValueSet(); + var experimental = true; + valueSet.setExperimental(experimental); + var adapter = new ValueSetAdapter(valueSet); + assertEquals(experimental, adapter.getExperimental()); + } + + @Test + void adapter_set_relatedArtifact() { + var valueSet = new ValueSet(); + var relatedArtifactList = List.of(new RelatedArtifact()); + var adapter = new ValueSetAdapter(valueSet); + adapter.setRelatedArtifact(relatedArtifactList); + assertEquals(0, adapter.getRelatedArtifact().size()); + } + + @Test + void adapter_copy() { + var valueSet = new ValueSet().setStatus(PublicationStatus.DRAFT); + valueSet.setId("valueset-1"); + var adapter = new ValueSetAdapter(valueSet); + var copy = adapter.copy(); + copy.setId("valueset-2"); + assertNotEquals(valueSet.getId(), copy.getId()); + valueSet.setStatus(PublicationStatus.ACTIVE); + assertNotEquals(adapter.getStatus(), copy.getStatus()); + } + + @Test + void adapter_get_all_dependencies() {} +} diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/EndpointAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/EndpointAdapterTest.java new file mode 100644 index 000000000..439645fb3 --- /dev/null +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/EndpointAdapterTest.java @@ -0,0 +1,27 @@ +package org.opencds.cqf.fhir.utility.adapter.r4; + +import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.hl7.fhir.r4.model.Endpoint; +import org.hl7.fhir.r4.model.PlanDefinition; +import org.junit.jupiter.api.Test; + +public class EndpointAdapterTest { + @Test + void invalid_object_fails() { + assertThrows(IllegalArgumentException.class, () -> new LibraryAdapter(new PlanDefinition())); + } + + @Test + void adapter_get_and_set_address() { + var endpoint = new Endpoint(); + var address = "123 Test Street"; + endpoint.setAddress(address); + var adapter = new EndpointAdapter(endpoint); + assertEquals(address, adapter.getAddress()); + var newAddress = "456 Test Street"; + adapter.setAddress(newAddress); + assertEquals(newAddress, endpoint.getAddress()); + } +} diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/LibraryAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/LibraryAdapterTest.java new file mode 100644 index 000000000..a9a33611a --- /dev/null +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/LibraryAdapterTest.java @@ -0,0 +1,206 @@ +package org.opencds.cqf.fhir.utility.adapter.r4; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.model.primitive.IdDt; +import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Date; +import java.util.List; +import org.hl7.fhir.instance.model.api.IDomainResource; +import org.hl7.fhir.r4.model.Bundle; +import org.hl7.fhir.r4.model.CodeableConcept; +import org.hl7.fhir.r4.model.Coding; +import org.hl7.fhir.r4.model.DataRequirement; +import org.hl7.fhir.r4.model.Enumerations.PublicationStatus; +import org.hl7.fhir.r4.model.Library; +import org.hl7.fhir.r4.model.Period; +import org.hl7.fhir.r4.model.PlanDefinition; +import org.hl7.fhir.r4.model.RelatedArtifact; +import org.hl7.fhir.r4.model.UsageContext; +import org.junit.jupiter.api.Test; +import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; + +public class LibraryAdapterTest { + private final FhirContext fhirContext = FhirContext.forR4Cached(); + + @Test + void invalid_object_fails() { + assertThrows(IllegalArgumentException.class, () -> new LibraryAdapter(new PlanDefinition())); + } + + @Test + void adapter_accepts_visitor() { + var spyVisitor = spy(new PackageVisitor(fhirContext)); + doReturn(new Bundle()).when(spyVisitor).visit(any(LibraryAdapter.class), any(), any()); + IDomainResource library = new Library(); + var adapter = new LibraryAdapter(library); + adapter.accept(spyVisitor, null, null); + verify(spyVisitor, times(1)).visit(any(LibraryAdapter.class), any(), any()); + } + + @Test + void adapter_get_and_set_name() { + var library = new Library(); + var name = "name"; + library.setName(name); + var adapter = new LibraryAdapter(library); + assertEquals(name, adapter.getName()); + var newName = "name2"; + adapter.setName(newName); + assertEquals(newName, library.getName()); + } + + @Test + void adapter_get_and_set_url() { + var library = new Library(); + var url = "www.url.com"; + library.setUrl(url); + var adapter = new LibraryAdapter(library); + assertEquals(url, adapter.getUrl()); + var newUrl = "www.url2.com"; + adapter.setUrl(newUrl); + assertEquals(newUrl, library.getUrl()); + } + + @Test + void adapter_get_and_set_version() { + var library = new Library(); + var version = "1.0.0"; + library.setVersion(version); + var adapter = new LibraryAdapter(library); + assertTrue(adapter.hasVersion()); + assertEquals(version, adapter.getVersion()); + var newVersion = "1.0.1"; + adapter.setVersion(newVersion); + assertEquals(newVersion, library.getVersion()); + } + + @Test + void adapter_get_and_set_status() { + var library = new Library(); + var status = PublicationStatus.DRAFT; + library.setStatus(status); + var adapter = new LibraryAdapter(library); + assertEquals(status.toCode(), adapter.getStatus()); + assertThrows(UnprocessableEntityException.class, () -> adapter.setStatus("invalid-status")); + var newStatus = PublicationStatus.ACTIVE; + adapter.setStatus(newStatus.toCode()); + assertEquals(newStatus, PublicationStatus.fromCode(adapter.getStatus())); + } + + @Test + void adapter_get_and_set_dates() { + var library = new Library(); + var date = new Date(); + var approvalDate = new Date(); + var effectivePeriod = new Period() + .setStart(java.sql.Date.valueOf(LocalDate.parse("2020-01-01"))) + .setEnd(java.sql.Date.valueOf(LocalDate.parse("2020-12-31"))); + library.setDate(date); + library.setApprovalDate(approvalDate); + library.setEffectivePeriod(effectivePeriod); + var adapter = new LibraryAdapter(library); + assertEquals(date, adapter.getDate()); + assertEquals(approvalDate, adapter.getApprovalDate()); + assertEquals(effectivePeriod, adapter.getEffectivePeriod()); + var newDate = new Date(); + newDate.setTime(100); + adapter.setDate(newDate); + assertEquals(newDate, library.getDate()); + var newApprovalDate = new Date(); + newApprovalDate.setTime(100); + adapter.setApprovalDate(newApprovalDate); + assertEquals(newApprovalDate, library.getApprovalDate()); + var newEffectivePeriod = new Period() + .setStart(java.sql.Date.valueOf(LocalDate.parse("2021-01-01"))) + .setEnd(java.sql.Date.valueOf(LocalDate.parse("2021-12-31"))); + adapter.setEffectivePeriod(newEffectivePeriod); + assertEquals(newEffectivePeriod, adapter.getEffectivePeriod()); + } + + @Test + void adapter_get_experimental() { + var library = new Library(); + var experimental = true; + library.setExperimental(experimental); + var adapter = new LibraryAdapter(library); + assertEquals(experimental, adapter.getExperimental()); + } + + @Test + void adapter_set_relatedArtifact() { + var library = new Library(); + var relatedArtifactList = List.of(new RelatedArtifact()); + var adapter = new LibraryAdapter(library); + adapter.setRelatedArtifact(relatedArtifactList); + assertEquals(relatedArtifactList, library.getRelatedArtifact()); + assertEquals(relatedArtifactList, adapter.getRelatedArtifact()); + } + + @Test + void adapter_copy() { + var library = new Library().setStatus(PublicationStatus.DRAFT); + library.setId("library-1"); + var adapter = new LibraryAdapter(library); + var copy = adapter.copy(); + var adapterCopy = new LibraryAdapter(copy); + adapterCopy.setId(new IdDt("Library", "library-2")); + assertNotEquals(library.getId(), copy.getId()); + library.setStatus(PublicationStatus.ACTIVE); + assertNotEquals(adapter.getStatus(), copy.getStatus()); + } + + @Test + void adapter_get_all_dependencies() {} + + @Test + void adapter_get_and_set_type() { + var type = new CodeableConcept(new Coding("www.test.com", "test", "Test")); + var library = new Library().setType(type); + var adapter = new LibraryAdapter(library); + assertEquals(type, adapter.getType()); + assertThrows(UnprocessableEntityException.class, () -> adapter.setType("test")); + var newType = new CodeableConcept(new Coding("http://hl7.org/fhir/ValueSet/library-type", "logic-library", "")); + adapter.setType(newType.getCoding().get(0).getCode()); + assertEquals( + newType.getCoding().get(0).getCode(), + ((CodeableConcept) adapter.getType()).getCoding().get(0).getCode()); + assertEquals( + newType.getCoding().get(0).getSystem(), + ((CodeableConcept) adapter.getType()).getCoding().get(0).getSystem()); + } + + @Test + void adapter_get_and_set_dataRequirement() { + var library = new Library(); + var dataRequirements = new ArrayList(); + dataRequirements.add(new DataRequirement().setType("Patient")); + library.setDataRequirement(dataRequirements); + var adapter = new LibraryAdapter(library); + assertEquals(dataRequirements, adapter.getDataRequirement()); + adapter.addDataRequirement(new DataRequirement().setType("Observation")); + assertEquals(library.getDataRequirement(), adapter.getDataRequirement()); + assertEquals(2, adapter.getDataRequirement().size()); + } + + @Test + void adapter_get_useContext() { + var library = new Library(); + var useContext = new UsageContext().setCode(new Coding("www.test.com", "test", "Test")); + library.setUseContext(Collections.singletonList(useContext)); + var adapter = new LibraryAdapter(library); + assertEquals(useContext, adapter.getUseContext().get(0)); + } +} diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/PlanDefinitionAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/PlanDefinitionAdapterTest.java index 32c4921f6..148bb016e 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/PlanDefinitionAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/PlanDefinitionAdapterTest.java @@ -108,7 +108,7 @@ void adapter_get_and_set_dates() { planDef.setApprovalDate(approvalDate); planDef.setEffectivePeriod(effectivePeriod); var adapter = new PlanDefinitionAdapter(planDef); - assertEquals(date, planDef.getDate()); + assertEquals(date, adapter.getDate()); assertEquals(approvalDate, adapter.getApprovalDate()); assertEquals(effectivePeriod, adapter.getEffectivePeriod()); var newDate = new Date(); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/QuestionnaireAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/QuestionnaireAdapterTest.java index 7c87e70d0..85c29ea15 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/QuestionnaireAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/QuestionnaireAdapterTest.java @@ -105,7 +105,7 @@ void adapter_get_and_set_dates() { questionnaire.setApprovalDate(approvalDate); questionnaire.setEffectivePeriod(effectivePeriod); var adapter = new QuestionnaireAdapter(questionnaire); - assertEquals(date, questionnaire.getDate()); + assertEquals(date, adapter.getDate()); assertEquals(approvalDate, adapter.getApprovalDate()); assertEquals(effectivePeriod, adapter.getEffectivePeriod()); var newDate = new Date(); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/StructureDefinitionAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/StructureDefinitionAdapterTest.java index 45a8eece0..619c574ed 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/StructureDefinitionAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/StructureDefinitionAdapterTest.java @@ -106,7 +106,7 @@ void adapter_get_and_set_dates() { .setEnd(java.sql.Date.valueOf(LocalDate.parse("2020-12-31"))); structureDef.setDate(date); var adapter = new StructureDefinitionAdapter(structureDef); - assertEquals(date, structureDef.getDate()); + assertEquals(date, adapter.getDate()); assertEquals(null, adapter.getApprovalDate()); assertNotEquals(effectivePeriod, adapter.getEffectivePeriod()); var newDate = new Date(); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/ValueSetAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/ValueSetAdapterTest.java new file mode 100644 index 000000000..66cd88641 --- /dev/null +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/ValueSetAdapterTest.java @@ -0,0 +1,159 @@ +package org.opencds.cqf.fhir.utility.adapter.r4; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; +import java.time.LocalDate; +import java.util.Date; +import java.util.List; +import org.hl7.fhir.instance.model.api.IDomainResource; +import org.hl7.fhir.r4.model.Bundle; +import org.hl7.fhir.r4.model.Enumerations.PublicationStatus; +import org.hl7.fhir.r4.model.Library; +import org.hl7.fhir.r4.model.Period; +import org.hl7.fhir.r4.model.RelatedArtifact; +import org.hl7.fhir.r4.model.ValueSet; +import org.junit.jupiter.api.Test; +import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; + +public class ValueSetAdapterTest { + private final FhirContext fhirContext = FhirContext.forR4Cached(); + + @Test + void invalid_object_fails() { + assertThrows(IllegalArgumentException.class, () -> new ValueSetAdapter(new Library())); + } + + @Test + void adapter_accepts_visitor() { + var spyVisitor = spy(new PackageVisitor(fhirContext)); + doReturn(new Bundle()).when(spyVisitor).visit(any(ValueSetAdapter.class), any(), any()); + IDomainResource valueSet = new ValueSet(); + var adapter = new ValueSetAdapter(valueSet); + assertEquals(valueSet, adapter.get()); + adapter.accept(spyVisitor, null, null); + verify(spyVisitor, times(1)).visit(any(ValueSetAdapter.class), any(), any()); + } + + @Test + void adapter_get_and_set_name() { + var valueSet = new ValueSet(); + var name = "name"; + valueSet.setName(name); + var adapter = new ValueSetAdapter(valueSet); + assertEquals(name, adapter.getName()); + var newName = "name2"; + adapter.setName(newName); + assertEquals(newName, valueSet.getName()); + } + + @Test + void adapter_get_and_set_url() { + var valueSet = new ValueSet(); + var url = "www.url.com"; + valueSet.setUrl(url); + var adapter = new ValueSetAdapter(valueSet); + assertTrue(adapter.hasUrl()); + assertEquals(url, adapter.getUrl()); + var newUrl = "www.url2.com"; + adapter.setUrl(newUrl); + assertTrue(adapter.hasUrl()); + assertEquals(newUrl, valueSet.getUrl()); + } + + @Test + void adapter_get_and_set_version() { + var valueSet = new ValueSet(); + var version = "1.0.0"; + valueSet.setVersion(version); + var adapter = new ValueSetAdapter(valueSet); + assertTrue(adapter.hasVersion()); + assertEquals(version, adapter.getVersion()); + var newVersion = "1.0.1"; + adapter.setVersion(newVersion); + assertEquals(newVersion, valueSet.getVersion()); + } + + @Test + void adapter_get_and_set_status() { + var valueSet = new ValueSet(); + var status = PublicationStatus.DRAFT; + valueSet.setStatus(status); + var adapter = new ValueSetAdapter(valueSet); + assertEquals(status.toCode(), adapter.getStatus()); + assertThrows(UnprocessableEntityException.class, () -> adapter.setStatus("invalid-status")); + var newStatus = PublicationStatus.ACTIVE; + adapter.setStatus(newStatus.toCode()); + assertEquals(newStatus, PublicationStatus.fromCode(adapter.getStatus())); + } + + @Test + void adapter_get_and_set_dates() { + // ValueSet does not have fields approvalDate and effectivePeriod + var valueSet = new ValueSet(); + var date = new Date(); + var effectivePeriod = new Period() + .setStart(java.sql.Date.valueOf(LocalDate.parse("2020-01-01"))) + .setEnd(java.sql.Date.valueOf(LocalDate.parse("2020-12-31"))); + valueSet.setDate(date); + var adapter = new ValueSetAdapter(valueSet); + assertEquals(date, adapter.getDate()); + assertEquals(null, adapter.getApprovalDate()); + assertNotEquals(effectivePeriod, adapter.getEffectivePeriod()); + var newDate = new Date(); + newDate.setTime(100); + adapter.setDate(newDate); + assertEquals(newDate, valueSet.getDate()); + var newApprovalDate = new Date(); + newApprovalDate.setTime(100); + adapter.setApprovalDate(newApprovalDate); + assertEquals(null, adapter.getApprovalDate()); + var newEffectivePeriod = new Period() + .setStart(java.sql.Date.valueOf(LocalDate.parse("2021-01-01"))) + .setEnd(java.sql.Date.valueOf(LocalDate.parse("2021-12-31"))); + adapter.setEffectivePeriod(newEffectivePeriod); + assertNotEquals(newEffectivePeriod, adapter.getEffectivePeriod()); + } + + @Test + void adapter_get_experimental() { + var valueSet = new ValueSet(); + var experimental = true; + valueSet.setExperimental(experimental); + var adapter = new ValueSetAdapter(valueSet); + assertEquals(experimental, adapter.getExperimental()); + } + + @Test + void adapter_set_relatedArtifact() { + var valueSet = new ValueSet(); + var relatedArtifactList = List.of(new RelatedArtifact()); + var adapter = new ValueSetAdapter(valueSet); + adapter.setRelatedArtifact(relatedArtifactList); + assertEquals(0, adapter.getRelatedArtifact().size()); + } + + @Test + void adapter_copy() { + var valueSet = new ValueSet().setStatus(PublicationStatus.DRAFT); + valueSet.setId("valueset-1"); + var adapter = new ValueSetAdapter(valueSet); + var copy = adapter.copy(); + copy.setId("valueset-2"); + assertNotEquals(valueSet.getId(), copy.getId()); + valueSet.setStatus(PublicationStatus.ACTIVE); + assertNotEquals(adapter.getStatus(), copy.getStatus()); + } + + @Test + void adapter_get_all_dependencies() {} +} diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/EndpointAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/EndpointAdapterTest.java new file mode 100644 index 000000000..4989640fe --- /dev/null +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/EndpointAdapterTest.java @@ -0,0 +1,27 @@ +package org.opencds.cqf.fhir.utility.adapter.r5; + +import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.hl7.fhir.r5.model.Endpoint; +import org.hl7.fhir.r5.model.PlanDefinition; +import org.junit.jupiter.api.Test; + +public class EndpointAdapterTest { + @Test + void invalid_object_fails() { + assertThrows(IllegalArgumentException.class, () -> new LibraryAdapter(new PlanDefinition())); + } + + @Test + void adapter_get_and_set_address() { + var endpoint = new Endpoint(); + var address = "123 Test Street"; + endpoint.setAddress(address); + var adapter = new EndpointAdapter(endpoint); + assertEquals(address, adapter.getAddress()); + var newAddress = "456 Test Street"; + adapter.setAddress(newAddress); + assertEquals(newAddress, endpoint.getAddress()); + } +} diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/LibraryAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/LibraryAdapterTest.java new file mode 100644 index 000000000..db94b8088 --- /dev/null +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/LibraryAdapterTest.java @@ -0,0 +1,207 @@ +package org.opencds.cqf.fhir.utility.adapter.r5; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.model.primitive.IdDt; +import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Date; +import java.util.List; +import org.hl7.fhir.instance.model.api.IDomainResource; +import org.hl7.fhir.r5.model.Bundle; +import org.hl7.fhir.r5.model.CodeableConcept; +import org.hl7.fhir.r5.model.Coding; +import org.hl7.fhir.r5.model.DataRequirement; +import org.hl7.fhir.r5.model.Enumerations.FHIRTypes; +import org.hl7.fhir.r5.model.Enumerations.PublicationStatus; +import org.hl7.fhir.r5.model.Library; +import org.hl7.fhir.r5.model.Period; +import org.hl7.fhir.r5.model.PlanDefinition; +import org.hl7.fhir.r5.model.RelatedArtifact; +import org.hl7.fhir.r5.model.UsageContext; +import org.junit.jupiter.api.Test; +import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; + +public class LibraryAdapterTest { + private final FhirContext fhirContext = FhirContext.forR5Cached(); + + @Test + void invalid_object_fails() { + assertThrows(IllegalArgumentException.class, () -> new LibraryAdapter(new PlanDefinition())); + } + + @Test + void adapter_accepts_visitor() { + var spyVisitor = spy(new PackageVisitor(fhirContext)); + doReturn(new Bundle()).when(spyVisitor).visit(any(LibraryAdapter.class), any(), any()); + IDomainResource library = new Library(); + var adapter = new LibraryAdapter(library); + adapter.accept(spyVisitor, null, null); + verify(spyVisitor, times(1)).visit(any(LibraryAdapter.class), any(), any()); + } + + @Test + void adapter_get_and_set_name() { + var library = new Library(); + var name = "name"; + library.setName(name); + var adapter = new LibraryAdapter(library); + assertEquals(name, adapter.getName()); + var newName = "name2"; + adapter.setName(newName); + assertEquals(newName, library.getName()); + } + + @Test + void adapter_get_and_set_url() { + var library = new Library(); + var url = "www.url.com"; + library.setUrl(url); + var adapter = new LibraryAdapter(library); + assertEquals(url, adapter.getUrl()); + var newUrl = "www.url2.com"; + adapter.setUrl(newUrl); + assertEquals(newUrl, library.getUrl()); + } + + @Test + void adapter_get_and_set_version() { + var library = new Library(); + var version = "1.0.0"; + library.setVersion(version); + var adapter = new LibraryAdapter(library); + assertTrue(adapter.hasVersion()); + assertEquals(version, adapter.getVersion()); + var newVersion = "1.0.1"; + adapter.setVersion(newVersion); + assertEquals(newVersion, library.getVersion()); + } + + @Test + void adapter_get_and_set_status() { + var library = new Library(); + var status = PublicationStatus.DRAFT; + library.setStatus(status); + var adapter = new LibraryAdapter(library); + assertEquals(status.toCode(), adapter.getStatus()); + assertThrows(UnprocessableEntityException.class, () -> adapter.setStatus("invalid-status")); + var newStatus = PublicationStatus.ACTIVE; + adapter.setStatus(newStatus.toCode()); + assertEquals(newStatus, PublicationStatus.fromCode(adapter.getStatus())); + } + + @Test + void adapter_get_and_set_dates() { + var library = new Library(); + var date = new Date(); + var approvalDate = new Date(); + var effectivePeriod = new Period() + .setStart(java.sql.Date.valueOf(LocalDate.parse("2020-01-01"))) + .setEnd(java.sql.Date.valueOf(LocalDate.parse("2020-12-31"))); + library.setDate(date); + library.setApprovalDate(approvalDate); + library.setEffectivePeriod(effectivePeriod); + var adapter = new LibraryAdapter(library); + assertEquals(date, adapter.getDate()); + assertEquals(approvalDate, adapter.getApprovalDate()); + assertEquals(effectivePeriod, adapter.getEffectivePeriod()); + var newDate = new Date(); + newDate.setTime(100); + adapter.setDate(newDate); + assertEquals(newDate, library.getDate()); + var newApprovalDate = new Date(); + newApprovalDate.setTime(100); + adapter.setApprovalDate(newApprovalDate); + assertEquals(newApprovalDate, library.getApprovalDate()); + var newEffectivePeriod = new Period() + .setStart(java.sql.Date.valueOf(LocalDate.parse("2021-01-01"))) + .setEnd(java.sql.Date.valueOf(LocalDate.parse("2021-12-31"))); + adapter.setEffectivePeriod(newEffectivePeriod); + assertEquals(newEffectivePeriod, adapter.getEffectivePeriod()); + } + + @Test + void adapter_get_experimental() { + var library = new Library(); + var experimental = true; + library.setExperimental(experimental); + var adapter = new LibraryAdapter(library); + assertEquals(experimental, adapter.getExperimental()); + } + + @Test + void adapter_set_relatedArtifact() { + var library = new Library(); + var relatedArtifactList = List.of(new RelatedArtifact()); + var adapter = new LibraryAdapter(library); + adapter.setRelatedArtifact(relatedArtifactList); + assertEquals(relatedArtifactList, library.getRelatedArtifact()); + assertEquals(relatedArtifactList, adapter.getRelatedArtifact()); + } + + @Test + void adapter_copy() { + var library = new Library().setStatus(PublicationStatus.DRAFT); + library.setId("library-1"); + var adapter = new LibraryAdapter(library); + var copy = adapter.copy(); + var adapterCopy = new LibraryAdapter(copy); + adapterCopy.setId(new IdDt("Library", "library-2")); + assertNotEquals(library.getId(), copy.getId()); + library.setStatus(PublicationStatus.ACTIVE); + assertNotEquals(adapter.getStatus(), copy.getStatus()); + } + + @Test + void adapter_get_all_dependencies() {} + + @Test + void adapter_get_and_set_type() { + var type = new CodeableConcept(new Coding("www.test.com", "test", "Test")); + var library = new Library().setType(type); + var adapter = new LibraryAdapter(library); + assertEquals(type, adapter.getType()); + assertThrows(UnprocessableEntityException.class, () -> adapter.setType("test")); + var newType = new CodeableConcept(new Coding("http://hl7.org/fhir/ValueSet/library-type", "logic-library", "")); + adapter.setType(newType.getCoding().get(0).getCode()); + assertEquals( + newType.getCoding().get(0).getCode(), + ((CodeableConcept) adapter.getType()).getCoding().get(0).getCode()); + assertEquals( + newType.getCoding().get(0).getSystem(), + ((CodeableConcept) adapter.getType()).getCoding().get(0).getSystem()); + } + + @Test + void adapter_get_and_set_dataRequirement() { + var library = new Library(); + var dataRequirements = new ArrayList(); + dataRequirements.add(new DataRequirement().setType(FHIRTypes.PATIENT)); + library.setDataRequirement(dataRequirements); + var adapter = new LibraryAdapter(library); + assertEquals(dataRequirements, adapter.getDataRequirement()); + adapter.addDataRequirement(new DataRequirement().setType(FHIRTypes.OBSERVATION)); + assertEquals(library.getDataRequirement(), adapter.getDataRequirement()); + assertEquals(2, adapter.getDataRequirement().size()); + } + + @Test + void adapter_get_useContext() { + var library = new Library(); + var useContext = new UsageContext().setCode(new Coding("www.test.com", "test", "Test")); + library.setUseContext(Collections.singletonList(useContext)); + var adapter = new LibraryAdapter(library); + assertEquals(useContext, adapter.getUseContext().get(0)); + } +} diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/PlanDefinitionAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/PlanDefinitionAdapterTest.java index 48639fc53..36f8469cb 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/PlanDefinitionAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/PlanDefinitionAdapterTest.java @@ -108,7 +108,7 @@ void adapter_get_and_set_dates() { planDef.setApprovalDate(approvalDate); planDef.setEffectivePeriod(effectivePeriod); var adapter = new PlanDefinitionAdapter(planDef); - assertEquals(date, planDef.getDate()); + assertEquals(date, adapter.getDate()); assertEquals(approvalDate, adapter.getApprovalDate()); assertEquals(effectivePeriod, adapter.getEffectivePeriod()); var newDate = new Date(); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapterTest.java index f865afd17..940d087cc 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapterTest.java @@ -105,7 +105,7 @@ void adapter_get_and_set_dates() { questionnaire.setApprovalDate(approvalDate); questionnaire.setEffectivePeriod(effectivePeriod); var adapter = new QuestionnaireAdapter(questionnaire); - assertEquals(date, questionnaire.getDate()); + assertEquals(date, adapter.getDate()); assertEquals(approvalDate, adapter.getApprovalDate()); assertEquals(effectivePeriod, adapter.getEffectivePeriod()); var newDate = new Date(); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapterTest.java index 68033b49d..feb174d79 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapterTest.java @@ -106,7 +106,7 @@ void adapter_get_and_set_dates() { .setEnd(java.sql.Date.valueOf(LocalDate.parse("2020-12-31"))); structureDef.setDate(date); var adapter = new StructureDefinitionAdapter(structureDef); - assertEquals(date, structureDef.getDate()); + assertEquals(date, adapter.getDate()); assertEquals(null, adapter.getApprovalDate()); assertNotEquals(effectivePeriod, adapter.getEffectivePeriod()); var newDate = new Date(); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ValueSetAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ValueSetAdapterTest.java new file mode 100644 index 000000000..504d5b4d7 --- /dev/null +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ValueSetAdapterTest.java @@ -0,0 +1,162 @@ +package org.opencds.cqf.fhir.utility.adapter.r5; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; +import java.time.LocalDate; +import java.util.Date; +import java.util.List; +import org.hl7.fhir.instance.model.api.IDomainResource; +import org.hl7.fhir.r5.model.Bundle; +import org.hl7.fhir.r5.model.Enumerations.PublicationStatus; +import org.hl7.fhir.r5.model.Library; +import org.hl7.fhir.r5.model.Period; +import org.hl7.fhir.r5.model.RelatedArtifact; +import org.hl7.fhir.r5.model.ValueSet; +import org.junit.jupiter.api.Test; +import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; + +public class ValueSetAdapterTest { + private final FhirContext fhirContext = FhirContext.forR5Cached(); + + @Test + void invalid_object_fails() { + assertThrows(IllegalArgumentException.class, () -> new ValueSetAdapter(new Library())); + } + + @Test + void adapter_accepts_visitor() { + var spyVisitor = spy(new PackageVisitor(fhirContext)); + doReturn(new Bundle()).when(spyVisitor).visit(any(ValueSetAdapter.class), any(), any()); + IDomainResource valueSet = new ValueSet(); + var adapter = new ValueSetAdapter(valueSet); + assertEquals(valueSet, adapter.get()); + adapter.accept(spyVisitor, null, null); + verify(spyVisitor, times(1)).visit(any(ValueSetAdapter.class), any(), any()); + } + + @Test + void adapter_get_and_set_name() { + var valueSet = new ValueSet(); + var name = "name"; + valueSet.setName(name); + var adapter = new ValueSetAdapter(valueSet); + assertEquals(name, adapter.getName()); + var newName = "name2"; + adapter.setName(newName); + assertEquals(newName, valueSet.getName()); + } + + @Test + void adapter_get_and_set_url() { + var valueSet = new ValueSet(); + var url = "www.url.com"; + valueSet.setUrl(url); + var adapter = new ValueSetAdapter(valueSet); + assertTrue(adapter.hasUrl()); + assertEquals(url, adapter.getUrl()); + var newUrl = "www.url2.com"; + adapter.setUrl(newUrl); + assertTrue(adapter.hasUrl()); + assertEquals(newUrl, valueSet.getUrl()); + } + + @Test + void adapter_get_and_set_version() { + var valueSet = new ValueSet(); + var version = "1.0.0"; + valueSet.setVersion(version); + var adapter = new ValueSetAdapter(valueSet); + assertTrue(adapter.hasVersion()); + assertEquals(version, adapter.getVersion()); + var newVersion = "1.0.1"; + adapter.setVersion(newVersion); + assertEquals(newVersion, valueSet.getVersion()); + } + + @Test + void adapter_get_and_set_status() { + var valueSet = new ValueSet(); + var status = PublicationStatus.DRAFT; + valueSet.setStatus(status); + var adapter = new ValueSetAdapter(valueSet); + assertEquals(status.toCode(), adapter.getStatus()); + assertThrows(UnprocessableEntityException.class, () -> adapter.setStatus("invalid-status")); + var newStatus = PublicationStatus.ACTIVE; + adapter.setStatus(newStatus.toCode()); + assertEquals(newStatus, PublicationStatus.fromCode(adapter.getStatus())); + } + + @Test + void adapter_get_and_set_dates() { + var valueSet = new ValueSet(); + var date = new Date(); + var approvalDate = new Date(); + var effectivePeriod = new Period() + .setStart(java.sql.Date.valueOf(LocalDate.parse("2020-01-01"))) + .setEnd(java.sql.Date.valueOf(LocalDate.parse("2020-12-31"))); + valueSet.setDate(date); + valueSet.setApprovalDate(approvalDate); + valueSet.setEffectivePeriod(effectivePeriod); + var adapter = new ValueSetAdapter(valueSet); + assertEquals(date, adapter.getDate()); + assertEquals(approvalDate, adapter.getApprovalDate()); + assertEquals(effectivePeriod, adapter.getEffectivePeriod()); + var newDate = new Date(); + newDate.setTime(100); + adapter.setDate(newDate); + assertEquals(newDate, valueSet.getDate()); + var newApprovalDate = new Date(); + newApprovalDate.setTime(100); + adapter.setApprovalDate(newApprovalDate); + assertEquals(newApprovalDate, valueSet.getApprovalDate()); + var newEffectivePeriod = new Period() + .setStart(java.sql.Date.valueOf(LocalDate.parse("2021-01-01"))) + .setEnd(java.sql.Date.valueOf(LocalDate.parse("2021-12-31"))); + adapter.setEffectivePeriod(newEffectivePeriod); + assertEquals(newEffectivePeriod, valueSet.getEffectivePeriod()); + } + + @Test + void adapter_get_experimental() { + var valueSet = new ValueSet(); + var experimental = true; + valueSet.setExperimental(experimental); + var adapter = new ValueSetAdapter(valueSet); + assertEquals(experimental, adapter.getExperimental()); + } + + @Test + void adapter_set_relatedArtifact() { + var valueSet = new ValueSet(); + var relatedArtifactList = List.of(new RelatedArtifact()); + var adapter = new ValueSetAdapter(valueSet); + adapter.setRelatedArtifact(relatedArtifactList); + assertEquals(relatedArtifactList, valueSet.getRelatedArtifact()); + assertEquals(relatedArtifactList, adapter.getRelatedArtifact()); + } + + @Test + void adapter_copy() { + var valueSet = new ValueSet().setStatus(PublicationStatus.DRAFT); + valueSet.setId("valueset-1"); + var adapter = new ValueSetAdapter(valueSet); + var copy = adapter.copy(); + copy.setId("valueset-2"); + assertNotEquals(valueSet.getId(), copy.getId()); + valueSet.setStatus(PublicationStatus.ACTIVE); + assertNotEquals(adapter.getStatus(), copy.getStatus()); + } + + @Test + void adapter_get_all_dependencies() {} +} From e0cd099043f08c9db53f7932e4c7931ea2cc0d4c Mon Sep 17 00:00:00 2001 From: Taha Date: Tue, 2 Jul 2024 17:34:41 -0700 Subject: [PATCH 14/38] Update terminology client to use url param (#488) * add method to client without valueset * spotless --------- Co-authored-by: taha.attari@smilecdr.com --- .../client/TerminologyServerClient.java | 56 ++++++++++++++++--- .../client/TerminologyServerClientTest.java | 44 ++++++++++++++- 2 files changed, 91 insertions(+), 9 deletions(-) diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/client/TerminologyServerClient.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/client/TerminologyServerClient.java index ed6dd3c21..87ab13fa1 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/client/TerminologyServerClient.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/client/TerminologyServerClient.java @@ -23,6 +23,8 @@ public class TerminologyServerClient { private final FhirContext ctx; + public static final String versionParamName = "valueSetVersion"; + public static final String urlParamName = "url"; public TerminologyServerClient(FhirContext ctx) { this.ctx = ctx; @@ -32,6 +34,30 @@ public IBaseResource expand(ValueSetAdapter valueSet, EndpointAdapter endpoint, checkNotNull(valueSet, "expected non-null value for valueSet"); checkNotNull(endpoint, "expected non-null value for endpoint"); checkNotNull(parameters, "expected non-null value for parameters"); + return expand( + endpoint, + parameters, + valueSet.getUrl(), + valueSet.getVersion(), + valueSet.get().getStructureFhirVersionEnum()); + } + + public IBaseResource expand(EndpointAdapter endpoint, ParametersAdapter parameters, FhirVersionEnum fhirVersion) { + checkNotNull(endpoint, "expected non-null value for endpoint"); + checkNotNull(parameters, "expected non-null value for parameters"); + checkNotNull(fhirVersion, "expected non-null value for fhirVersion"); + checkNotNull(parameters.getParameter(urlParamName), "expected non-null value for 'url' expansion parameter"); + return expand(endpoint, parameters, null, null, fhirVersion); + } + + public IBaseResource expand( + EndpointAdapter endpoint, + ParametersAdapter parameters, + String url, + String valueSetVersion, + FhirVersionEnum fhirVersion) { + checkNotNull(endpoint, "expected non-null value for endpoint"); + checkNotNull(parameters, "expected non-null value for parameters"); var username = endpoint.getExtensionsByUrl(Constants.VSAC_USERNAME).stream() .findFirst() .map(ext -> ext.getValue().toString()) @@ -43,14 +69,17 @@ public IBaseResource expand(ValueSetAdapter valueSet, EndpointAdapter endpoint, IGenericClient fhirClient = ctx.newRestfulGenericClient(getAddressBase(endpoint.getAddress())); Clients.registerAdditionalRequestHeadersAuth(fhirClient, username, apiKey); - if (parameters.getParameter("url") == null) { + if (parameters.getParameter(urlParamName) == null) { + if (url == null) { + throw new UnprocessableEntityException("No '" + urlParamName + "' expansion parameter present"); + } parameters.addParameter( - valueSet.get().getStructureFhirVersionEnum().equals(FhirVersionEnum.DSTU3) - ? Parameters.newUriPart(ctx, "url", valueSet.getUrl()) - : Parameters.newUrlPart(ctx, "url", valueSet.getUrl())); + fhirVersion == FhirVersionEnum.DSTU3 + ? Parameters.newUriPart(ctx, urlParamName, url) + : Parameters.newUrlPart(ctx, urlParamName, url)); } - if (valueSet.hasVersion() && parameters.getParameter("valueSetVersion") == null) { - parameters.addParameter(Parameters.newStringPart(ctx, "valueSetVersion", valueSet.getVersion())); + if (parameters.getParameter(versionParamName) == null && valueSetVersion != null) { + parameters.addParameter(Parameters.newStringPart(ctx, versionParamName, valueSetVersion)); } // Invoke on the type using the url parameter return fhirClient @@ -58,10 +87,23 @@ public IBaseResource expand(ValueSetAdapter valueSet, EndpointAdapter endpoint, .onType("ValueSet") .named("$expand") .withParameters((IBaseParameters) parameters.get()) - .returnResourceType(valueSet.get().getClass()) + .returnResourceType(getValueSetClass(fhirVersion)) .execute(); } + private Class getValueSetClass(FhirVersionEnum fhirVersion) { + switch (fhirVersion) { + case DSTU3: + return org.hl7.fhir.dstu3.model.ValueSet.class; + case R4: + return org.hl7.fhir.r4.model.ValueSet.class; + case R5: + return org.hl7.fhir.r5.model.ValueSet.class; + default: + throw new UnprocessableEntityException("Unknown ValueSet version"); + } + } + // Strips resource and id from the endpoint address URL, these are not needed as the client constructs the URL. // Converts http URLs to https private String getAddressBase(String address) { diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/client/TerminologyServerClientTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/client/TerminologyServerClientTest.java index 46448e90b..867bc0975 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/client/TerminologyServerClientTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/client/TerminologyServerClientTest.java @@ -27,8 +27,9 @@ public class TerminologyServerClientTest { private static final String authoritativeSource = "www.source.com/ValueSet"; private static final String username = "username"; private static final String password = "password"; - private static final String urlParamName = "url"; - private static final String versionParamName = "valueSetVersion"; + private static final String urlParamName = TerminologyServerClient.urlParamName; + private static final String versionParamName = TerminologyServerClient.versionParamName; + ; private FhirContext fhirContextDstu3 = FhirContext.forDstu3Cached(); private FhirContext fhirContextR4 = FhirContext.forR4Cached(); @@ -79,6 +80,19 @@ void testR4UrlAndVersion() { ((org.hl7.fhir.r4.model.StringType) params.getParameter(versionParamName).getValue()) .getValue()); + + // if url is provided we don't need to pass in a ValueSet + var urlAndVersionParams = factory.createParameters(new org.hl7.fhir.r4.model.Parameters()); + urlAndVersionParams.addParameter(urlParamName, new org.hl7.fhir.r4.model.UrlType(url)); + urlAndVersionParams.addParameter(versionParamName, new org.hl7.fhir.r4.model.StringType(version)); + Exception noException = null; + try { + var expanded = client.expand(endpoint, urlAndVersionParams, FhirVersionEnum.R4); + assertEquals(expanded.getClass(), valueSet.get().getClass()); + } catch (Exception e) { + noException = e; + } + assertNull(noException); } @Test @@ -126,6 +140,19 @@ void testR5UrlAndVersion() { ((org.hl7.fhir.r5.model.StringType) params.getParameter(versionParamName).getValue()) .getValue()); + + // if url is provided we don't need to pass in a ValueSet + var urlAndVersionParams = factory.createParameters(new org.hl7.fhir.r5.model.Parameters()); + urlAndVersionParams.addParameter(urlParamName, new org.hl7.fhir.r5.model.UrlType(url)); + urlAndVersionParams.addParameter(versionParamName, new org.hl7.fhir.r5.model.StringType(version)); + Exception noException = null; + try { + var expanded = client.expand(endpoint, urlAndVersionParams, FhirVersionEnum.R5); + assertEquals(expanded.getClass(), valueSet.get().getClass()); + } catch (Exception e) { + noException = e; + } + assertNull(noException); } @Test @@ -180,5 +207,18 @@ void testDstu3UrlAndVersion() { .orElseThrow() .getValue()) .getValue()); + + // if url is provided we don't need to pass in a ValueSet + var urlAndVersionParams = factory.createParameters(new org.hl7.fhir.dstu3.model.Parameters()); + urlAndVersionParams.addParameter(urlParamName, new org.hl7.fhir.dstu3.model.UriType(url)); + urlAndVersionParams.addParameter(versionParamName, new org.hl7.fhir.dstu3.model.StringType(version)); + Exception noException = null; + try { + var expanded = client.expand(endpoint, urlAndVersionParams, FhirVersionEnum.DSTU3); + assertEquals(expanded.getClass(), valueSet.get().getClass()); + } catch (Exception e) { + noException = e; + } + assertNull(noException); } } From 7bf5f40c42958beb999f323fde25cf46e1342a82 Mon Sep 17 00:00:00 2001 From: Brenin Rhodes Date: Tue, 2 Jul 2024 18:35:20 -0600 Subject: [PATCH 15/38] Add tests --- .../utility/adapter/BaseResourceAdapter.java | 3 + .../fhir/utility/adapter/ResourceAdapter.java | 2 +- .../adapter/dstu3/ResourceAdapter.java | 2 +- .../utility/adapter/r4/ResourceAdapter.java | 6 +- .../utility/adapter/r5/ResourceAdapter.java | 2 +- .../adapter/dstu3/ResourceAdapterTest.java | 71 +++++++++++++++++++ .../adapter/r4/ResourceAdapterTest.java | 71 +++++++++++++++++++ .../adapter/r5/ResourceAdapterTest.java | 71 +++++++++++++++++++ 8 files changed, 220 insertions(+), 8 deletions(-) create mode 100644 cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapterTest.java create mode 100644 cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/ResourceAdapterTest.java create mode 100644 cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapterTest.java diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/BaseResourceAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/BaseResourceAdapter.java index 6876e61f5..fb7f41123 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/BaseResourceAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/BaseResourceAdapter.java @@ -13,6 +13,9 @@ public abstract class BaseResourceAdapter implements ResourceAdapter { protected final ModelResolver modelResolver; public BaseResourceAdapter(IBaseResource resource) { + if (resource == null) { + throw new IllegalArgumentException("resource can not be null"); + } this.resource = resource; fhirContext = FhirContext.forCached(resource.getStructureFhirVersionEnum()); elementDefinition = fhirContext.getElementDefinition(this.resource.getClass()); diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/ResourceAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/ResourceAdapter.java index f73fc74e1..9c879b2ca 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/ResourceAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/ResourceAdapter.java @@ -41,7 +41,7 @@ public interface ResourceAdapter extends Adapter { public boolean equalsShallow(IBase other); - public void setExtension(List> extensions); + public void setExtension(List> extensions); public > void addExtension(T extension); diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapter.java index 01ae388ed..9e8a3325f 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapter.java @@ -111,7 +111,7 @@ public boolean equalsShallow(IBase other) { } @Override - public void setExtension(List> extensions) { + public void setExtension(List> extensions) { getDomainResource() .ifPresent(r -> r.setExtension( extensions.stream().map(e -> (Extension) e).collect(Collectors.toList()))); diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/ResourceAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/ResourceAdapter.java index 602441b81..4c6e30819 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/ResourceAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/ResourceAdapter.java @@ -20,10 +20,6 @@ class ResourceAdapter extends BaseResourceAdapter { ResourceAdapter(IBaseResource resource) { super(resource); - if (resource == null) { - throw new IllegalArgumentException("resource can not be null"); - } - if (!resource.getStructureFhirVersionEnum().equals(FhirVersionEnum.R4)) { throw new IllegalArgumentException("resource is incorrect fhir version for this adapter"); } @@ -107,7 +103,7 @@ public boolean equalsShallow(IBase other) { } @Override - public void setExtension(List> extensions) { + public void setExtension(List> extensions) { getDomainResource() .ifPresent(r -> r.setExtension( extensions.stream().map(e -> (Extension) e).collect(Collectors.toList()))); diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapter.java index cf581a979..5fddb1a7a 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapter.java @@ -111,7 +111,7 @@ public boolean equalsShallow(IBase other) { } @Override - public void setExtension(List> extensions) { + public void setExtension(List> extensions) { getDomainResource() .ifPresent(r -> r.setExtension( extensions.stream().map(e -> (Extension) e).collect(Collectors.toList()))); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapterTest.java new file mode 100644 index 000000000..a7a1fc079 --- /dev/null +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapterTest.java @@ -0,0 +1,71 @@ +package org.opencds.cqf.fhir.utility.adapter.dstu3; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import ca.uhn.fhir.model.primitive.IdDt; +import java.util.Date; +import java.util.List; +import org.hl7.fhir.dstu3.model.Extension; +import org.hl7.fhir.dstu3.model.IdType; +import org.hl7.fhir.dstu3.model.Meta; +import org.hl7.fhir.dstu3.model.Patient; +import org.hl7.fhir.instance.model.api.IIdType; +import org.junit.jupiter.api.Test; + +public class ResourceAdapterTest { + @Test + void invalid_object_fails() { + assertThrows(IllegalArgumentException.class, () -> new ResourceAdapter(null)); + assertThrows(IllegalArgumentException.class, () -> new ResourceAdapter(new org.hl7.fhir.r5.model.Library())); + } + + @Test + void adapter_get_and_set_property() { + var resource = new Patient(); + var id = new IdDt("patient-1"); + resource.setId(id); + var adapter = new ResourceAdapter(resource); + assertEquals(id.getValue(), ((IIdType) adapter.getSingleProperty("id")).getValue()); + var newId = new IdType("patient-2"); + adapter.setProperty("id", newId); + assertEquals(newId, resource.getIdElement()); + assertEquals("id", adapter.getTypesForProperty("id")[0]); + assertNotNull(adapter.makeProperty("language")); + var meta = (Meta) adapter.addChild("meta"); + var date = new Date(); + meta.setLastUpdated(date); + assertEquals(date, ((Meta) adapter.getSingleProperty("meta")).getLastUpdated()); + } + + @Test + void adapter_copy() { + var resource = new Patient(); + resource.setId("patient-1"); + resource.setMeta(new Meta().setLastUpdated(new Date())); + var adapter = new ResourceAdapter(resource); + var copy = (Patient) adapter.copy(); + assertTrue(adapter.equalsDeep(copy)); + copy.setMeta(new Meta().setLastUpdated(new Date())); + assertFalse(adapter.equalsDeep(copy)); + assertTrue(adapter.equalsShallow(copy)); + copy.setId("patient-2"); + assertFalse(adapter.equalsShallow(copy)); + resource.setLanguage("FR"); + adapter.copyValues(copy); + assertEquals("FR", copy.getLanguage()); + } + + @Test + void adapter_get_and_set_extension() { + var resource = new Patient(); + var extensionList = List.of(new Extension()); + var adapter = new ResourceAdapter(resource); + adapter.setExtension(extensionList); + assertEquals(extensionList, resource.getExtension()); + assertEquals(extensionList, adapter.getExtension()); + } +} diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/ResourceAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/ResourceAdapterTest.java new file mode 100644 index 000000000..d8c0a6881 --- /dev/null +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/ResourceAdapterTest.java @@ -0,0 +1,71 @@ +package org.opencds.cqf.fhir.utility.adapter.r4; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import ca.uhn.fhir.model.primitive.IdDt; +import java.util.Date; +import java.util.List; +import org.hl7.fhir.instance.model.api.IIdType; +import org.hl7.fhir.r4.model.Extension; +import org.hl7.fhir.r4.model.IdType; +import org.hl7.fhir.r4.model.Meta; +import org.hl7.fhir.r4.model.Patient; +import org.junit.jupiter.api.Test; + +public class ResourceAdapterTest { + @Test + void invalid_object_fails() { + assertThrows(IllegalArgumentException.class, () -> new ResourceAdapter(null)); + assertThrows(IllegalArgumentException.class, () -> new ResourceAdapter(new org.hl7.fhir.r5.model.Library())); + } + + @Test + void adapter_get_and_set_property() { + var resource = new Patient(); + var id = new IdDt("patient-1"); + resource.setId(id); + var adapter = new ResourceAdapter(resource); + assertEquals(id.getValue(), ((IIdType) adapter.getSingleProperty("id")).getValue()); + var newId = new IdType("patient-2"); + adapter.setProperty("id", newId); + assertEquals(newId, resource.getIdElement()); + assertEquals("id", adapter.getTypesForProperty("id")[0]); + assertNotNull(adapter.makeProperty("language")); + var meta = (Meta) adapter.addChild("meta"); + var date = new Date(); + meta.setLastUpdated(date); + assertEquals(date, ((Meta) adapter.getSingleProperty("meta")).getLastUpdated()); + } + + @Test + void adapter_copy() { + var resource = new Patient(); + resource.setId("patient-1"); + resource.setMeta(new Meta().setLastUpdated(new Date())); + var adapter = new ResourceAdapter(resource); + var copy = (Patient) adapter.copy(); + assertTrue(adapter.equalsDeep(copy)); + copy.setMeta(new Meta().setLastUpdated(new Date())); + assertFalse(adapter.equalsDeep(copy)); + assertTrue(adapter.equalsShallow(copy)); + copy.setId("patient-2"); + assertFalse(adapter.equalsShallow(copy)); + resource.setLanguage("FR"); + adapter.copyValues(copy); + assertEquals("FR", copy.getLanguage()); + } + + @Test + void adapter_get_and_set_extension() { + var resource = new Patient(); + var extensionList = List.of(new Extension()); + var adapter = new ResourceAdapter(resource); + adapter.setExtension(extensionList); + assertEquals(extensionList, resource.getExtension()); + assertEquals(extensionList, adapter.getExtension()); + } +} diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapterTest.java new file mode 100644 index 000000000..9f07612bd --- /dev/null +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapterTest.java @@ -0,0 +1,71 @@ +package org.opencds.cqf.fhir.utility.adapter.r5; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import ca.uhn.fhir.model.primitive.IdDt; +import java.util.Date; +import java.util.List; +import org.hl7.fhir.instance.model.api.IIdType; +import org.hl7.fhir.r5.model.Extension; +import org.hl7.fhir.r5.model.IdType; +import org.hl7.fhir.r5.model.Meta; +import org.hl7.fhir.r5.model.Patient; +import org.junit.jupiter.api.Test; + +public class ResourceAdapterTest { + @Test + void invalid_object_fails() { + assertThrows(IllegalArgumentException.class, () -> new ResourceAdapter(null)); + assertThrows(IllegalArgumentException.class, () -> new ResourceAdapter(new org.hl7.fhir.r4.model.Library())); + } + + @Test + void adapter_get_and_set_property() { + var resource = new Patient(); + var id = new IdDt("patient-1"); + resource.setId(id); + var adapter = new ResourceAdapter(resource); + assertEquals(id.getValue(), ((IIdType) adapter.getSingleProperty("id")).getValue()); + var newId = new IdType("patient-2"); + adapter.setProperty("id", newId); + assertEquals(newId, resource.getIdElement()); + assertEquals("id", adapter.getTypesForProperty("id")[0]); + assertNotNull(adapter.makeProperty("language")); + var meta = (Meta) adapter.addChild("meta"); + var date = new Date(); + meta.setLastUpdated(date); + assertEquals(date, ((Meta) adapter.getSingleProperty("meta")).getLastUpdated()); + } + + @Test + void adapter_copy() { + var resource = new Patient(); + resource.setId("patient-1"); + resource.setMeta(new Meta().setLastUpdated(new Date())); + var adapter = new ResourceAdapter(resource); + var copy = (Patient) adapter.copy(); + assertTrue(adapter.equalsDeep(copy)); + copy.setMeta(new Meta().setLastUpdated(new Date())); + assertFalse(adapter.equalsDeep(copy)); + assertTrue(adapter.equalsShallow(copy)); + copy.setId("patient-2"); + assertFalse(adapter.equalsShallow(copy)); + resource.setLanguage("FR"); + adapter.copyValues(copy); + assertEquals("FR", copy.getLanguage()); + } + + @Test + void adapter_get_and_set_extension() { + var resource = new Patient(); + var extensionList = List.of(new Extension()); + var adapter = new ResourceAdapter(resource); + adapter.setExtension(extensionList); + assertEquals(extensionList, resource.getExtension()); + assertEquals(extensionList, adapter.getExtension()); + } +} From cad1f1eb8e2d806e4bd35cfdb6a8f43dc43b9aeb Mon Sep 17 00:00:00 2001 From: Brenin Rhodes Date: Tue, 2 Jul 2024 18:43:33 -0600 Subject: [PATCH 16/38] Fix test --- .../cqf/fhir/utility/adapter/dstu3/ResourceAdapterTest.java | 4 +++- .../cqf/fhir/utility/adapter/r4/ResourceAdapterTest.java | 4 +++- .../cqf/fhir/utility/adapter/r5/ResourceAdapterTest.java | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapterTest.java index a7a1fc079..a0b74fb4b 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapterTest.java @@ -49,7 +49,9 @@ void adapter_copy() { var adapter = new ResourceAdapter(resource); var copy = (Patient) adapter.copy(); assertTrue(adapter.equalsDeep(copy)); - copy.setMeta(new Meta().setLastUpdated(new Date())); + var newDate = new Date(); + newDate.setTime(100); + copy.setMeta(new Meta().setLastUpdated(newDate)); assertFalse(adapter.equalsDeep(copy)); assertTrue(adapter.equalsShallow(copy)); copy.setId("patient-2"); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/ResourceAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/ResourceAdapterTest.java index d8c0a6881..164ffbf2a 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/ResourceAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/ResourceAdapterTest.java @@ -49,7 +49,9 @@ void adapter_copy() { var adapter = new ResourceAdapter(resource); var copy = (Patient) adapter.copy(); assertTrue(adapter.equalsDeep(copy)); - copy.setMeta(new Meta().setLastUpdated(new Date())); + var newDate = new Date(); + newDate.setTime(100); + copy.setMeta(new Meta().setLastUpdated(newDate)); assertFalse(adapter.equalsDeep(copy)); assertTrue(adapter.equalsShallow(copy)); copy.setId("patient-2"); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapterTest.java index 9f07612bd..1c76dae7c 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapterTest.java @@ -49,7 +49,9 @@ void adapter_copy() { var adapter = new ResourceAdapter(resource); var copy = (Patient) adapter.copy(); assertTrue(adapter.equalsDeep(copy)); - copy.setMeta(new Meta().setLastUpdated(new Date())); + var newDate = new Date(); + newDate.setTime(100); + copy.setMeta(new Meta().setLastUpdated(newDate)); assertFalse(adapter.equalsDeep(copy)); assertTrue(adapter.equalsShallow(copy)); copy.setId("patient-2"); From e0dc77f0c335bfbbd3adebc9e30788d2e2c83d84 Mon Sep 17 00:00:00 2001 From: "taha.attari@smilecdr.com" Date: Tue, 2 Jul 2024 20:54:37 -0400 Subject: [PATCH 17/38] shorter --- .../java/org/opencds/cqf/fhir/utility/ExpandHelper.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/ExpandHelper.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/ExpandHelper.java index b4c7ee298..1243a6477 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/ExpandHelper.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/ExpandHelper.java @@ -38,14 +38,12 @@ public void expandValueSet( } // Gather the Terminology Service from the valueSet's authoritativeSourceUrl. - var authoritativeSource = valueSet.getExtension().stream() + @SuppressWarnings("unchecked") + var authoritativeSourceUrl = valueSet.getExtension().stream() .filter(e -> e.getUrl().equals(Constants.AUTHORITATIVE_SOURCE_URL)) .findFirst() + .map(url -> ((IPrimitiveType) url.getValue()).getValueAsString()) .orElse(null); - @SuppressWarnings("unchecked") - var authoritativeSourceUrl = authoritativeSource == null - ? null - : ((IPrimitiveType) authoritativeSource.getValue()).getValueAsString(); // If terminologyEndpoint exists and we have no authoritativeSourceUrl or the authoritativeSourceUrl matches the // terminologyEndpoint address then we will use the terminologyEndpoint for expansion From 73a75e210fbb3ae43ad9e1b52c65cc8172a29d19 Mon Sep 17 00:00:00 2001 From: Brenin Rhodes Date: Tue, 2 Jul 2024 19:00:51 -0600 Subject: [PATCH 18/38] Add test --- .../cqf/fhir/utility/adapter/ResourceAdapter.java | 4 ++++ .../utility/adapter/dstu3/ResourceAdapterTest.java | 14 +++++++++++++- .../utility/adapter/r4/ResourceAdapterTest.java | 14 +++++++++++++- .../utility/adapter/r5/ResourceAdapterTest.java | 14 +++++++++++++- 4 files changed, 43 insertions(+), 3 deletions(-) diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/ResourceAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/ResourceAdapter.java index 9c879b2ca..76388f35e 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/ResourceAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/ResourceAdapter.java @@ -45,6 +45,10 @@ public interface ResourceAdapter extends Adapter { public > void addExtension(T extension); + public default boolean hasExtension(String url) { + return hasExtension(get(), url); + } + public default List> getExtension() { return getExtension(get()); } diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapterTest.java index a0b74fb4b..0a2eb014d 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapterTest.java @@ -14,6 +14,9 @@ import org.hl7.fhir.dstu3.model.Meta; import org.hl7.fhir.dstu3.model.Patient; import org.hl7.fhir.instance.model.api.IIdType; +import org.hl7.fhir.dstu3.model.BooleanType; +import org.hl7.fhir.dstu3.model.Library; +import org.hl7.fhir.dstu3.model.PlanDefinition; import org.junit.jupiter.api.Test; public class ResourceAdapterTest { @@ -64,10 +67,19 @@ void adapter_copy() { @Test void adapter_get_and_set_extension() { var resource = new Patient(); - var extensionList = List.of(new Extension()); + var extensionList = List.of(new Extension().setUrl("test-extension-url").setValue(new BooleanType(true))); var adapter = new ResourceAdapter(resource); adapter.setExtension(extensionList); assertEquals(extensionList, resource.getExtension()); assertEquals(extensionList, adapter.getExtension()); + assertTrue(adapter.hasExtension("test-extension-url")); + } + + @Test + void adapter_get_contained() { + var resource = new PlanDefinition(); + resource.addContained(new Library()); + var adapter = new ResourceAdapter(resource); + assertTrue(adapter.hasContained()); } } diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/ResourceAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/ResourceAdapterTest.java index 164ffbf2a..92330372c 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/ResourceAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/ResourceAdapterTest.java @@ -10,10 +10,13 @@ import java.util.Date; import java.util.List; import org.hl7.fhir.instance.model.api.IIdType; +import org.hl7.fhir.r4.model.BooleanType; import org.hl7.fhir.r4.model.Extension; import org.hl7.fhir.r4.model.IdType; +import org.hl7.fhir.r4.model.Library; import org.hl7.fhir.r4.model.Meta; import org.hl7.fhir.r4.model.Patient; +import org.hl7.fhir.r4.model.PlanDefinition; import org.junit.jupiter.api.Test; public class ResourceAdapterTest { @@ -64,10 +67,19 @@ void adapter_copy() { @Test void adapter_get_and_set_extension() { var resource = new Patient(); - var extensionList = List.of(new Extension()); + var extensionList = List.of(new Extension().setUrl("test-extension-url").setValue(new BooleanType(true))); var adapter = new ResourceAdapter(resource); adapter.setExtension(extensionList); assertEquals(extensionList, resource.getExtension()); assertEquals(extensionList, adapter.getExtension()); + assertTrue(adapter.hasExtension("test-extension-url")); + } + + @Test + void adapter_get_contained() { + var resource = new PlanDefinition(); + resource.addContained(new Library()); + var adapter = new ResourceAdapter(resource); + assertTrue(adapter.hasContained()); } } diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapterTest.java index 1c76dae7c..bd4ef5f19 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapterTest.java @@ -10,6 +10,9 @@ import java.util.Date; import java.util.List; import org.hl7.fhir.instance.model.api.IIdType; +import org.hl7.fhir.r5.model.BooleanType; +import org.hl7.fhir.r5.model.Library; +import org.hl7.fhir.r5.model.PlanDefinition; import org.hl7.fhir.r5.model.Extension; import org.hl7.fhir.r5.model.IdType; import org.hl7.fhir.r5.model.Meta; @@ -64,10 +67,19 @@ void adapter_copy() { @Test void adapter_get_and_set_extension() { var resource = new Patient(); - var extensionList = List.of(new Extension()); + var extensionList = List.of(new Extension().setUrl("test-extension-url").setValue(new BooleanType(true))); var adapter = new ResourceAdapter(resource); adapter.setExtension(extensionList); assertEquals(extensionList, resource.getExtension()); assertEquals(extensionList, adapter.getExtension()); + assertTrue(adapter.hasExtension("test-extension-url")); + } + + @Test + void adapter_get_contained() { + var resource = new PlanDefinition(); + resource.addContained(new Library()); + var adapter = new ResourceAdapter(resource); + assertTrue(adapter.hasContained()); } } From 4aaba042d4062d6692d1d6da974a4d5e5b50d54e Mon Sep 17 00:00:00 2001 From: Brenin Rhodes Date: Tue, 2 Jul 2024 19:02:21 -0600 Subject: [PATCH 19/38] cleanup --- .../cqf/fhir/utility/adapter/dstu3/ResourceAdapterTest.java | 6 +++--- .../cqf/fhir/utility/adapter/r5/ResourceAdapterTest.java | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapterTest.java index 0a2eb014d..b101719c4 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapterTest.java @@ -9,14 +9,14 @@ import ca.uhn.fhir.model.primitive.IdDt; import java.util.Date; import java.util.List; +import org.hl7.fhir.dstu3.model.BooleanType; import org.hl7.fhir.dstu3.model.Extension; import org.hl7.fhir.dstu3.model.IdType; +import org.hl7.fhir.dstu3.model.Library; import org.hl7.fhir.dstu3.model.Meta; import org.hl7.fhir.dstu3.model.Patient; -import org.hl7.fhir.instance.model.api.IIdType; -import org.hl7.fhir.dstu3.model.BooleanType; -import org.hl7.fhir.dstu3.model.Library; import org.hl7.fhir.dstu3.model.PlanDefinition; +import org.hl7.fhir.instance.model.api.IIdType; import org.junit.jupiter.api.Test; public class ResourceAdapterTest { diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapterTest.java index bd4ef5f19..ecdf8f35c 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapterTest.java @@ -11,12 +11,12 @@ import java.util.List; import org.hl7.fhir.instance.model.api.IIdType; import org.hl7.fhir.r5.model.BooleanType; -import org.hl7.fhir.r5.model.Library; -import org.hl7.fhir.r5.model.PlanDefinition; import org.hl7.fhir.r5.model.Extension; import org.hl7.fhir.r5.model.IdType; +import org.hl7.fhir.r5.model.Library; import org.hl7.fhir.r5.model.Meta; import org.hl7.fhir.r5.model.Patient; +import org.hl7.fhir.r5.model.PlanDefinition; import org.junit.jupiter.api.Test; public class ResourceAdapterTest { From 7ebc822b6371d243eefba28b8ad5f21aad45f6fa Mon Sep 17 00:00:00 2001 From: Brenin Rhodes Date: Tue, 2 Jul 2024 19:18:56 -0600 Subject: [PATCH 20/38] cleanup --- .../opencds/cqf/fhir/utility/adapter/LibraryAdapter.java | 2 +- .../cqf/fhir/utility/adapter/dstu3/LibraryAdapter.java | 7 +++---- .../cqf/fhir/utility/adapter/r4/LibraryAdapter.java | 7 +++---- .../cqf/fhir/utility/adapter/r5/LibraryAdapter.java | 7 +++---- .../cqf/fhir/utility/adapter/dstu3/LibraryAdapterTest.java | 6 +++--- .../cqf/fhir/utility/adapter/r4/LibraryAdapterTest.java | 6 +++--- .../cqf/fhir/utility/adapter/r5/LibraryAdapterTest.java | 6 +++--- 7 files changed, 19 insertions(+), 22 deletions(-) diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/LibraryAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/LibraryAdapter.java index ce66042d3..591050b8b 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/LibraryAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/LibraryAdapter.java @@ -28,7 +28,7 @@ public interface LibraryAdapter extends KnowledgeArtifactAdapter { LibraryAdapter addDataRequirement(ICompositeType dataRequirement); - LibraryAdapter setDataRequirements(List dataRequirements); + LibraryAdapter setDataRequirement(List dataRequirement); List getUseContext(); } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/LibraryAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/LibraryAdapter.java index 2b1bec4b9..6b2f752bd 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/LibraryAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/LibraryAdapter.java @@ -130,11 +130,10 @@ public LibraryAdapter addDataRequirement(ICompositeType dataRequirement) { } @Override - public LibraryAdapter setDataRequirements(List dataRequirements) { + public LibraryAdapter setDataRequirement(List dataRequirement) { getLibrary() - .setDataRequirement(dataRequirements.stream() - .map(dr -> (DataRequirement) dr) - .collect(Collectors.toList())); + .setDataRequirement( + dataRequirement.stream().map(dr -> (DataRequirement) dr).collect(Collectors.toList())); return this; } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/LibraryAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/LibraryAdapter.java index 3c238a391..8189f9158 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/LibraryAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/LibraryAdapter.java @@ -130,11 +130,10 @@ public LibraryAdapter addDataRequirement(ICompositeType dataRequirement) { } @Override - public LibraryAdapter setDataRequirements(List dataRequirements) { + public LibraryAdapter setDataRequirement(List dataRequirement) { getLibrary() - .setDataRequirement(dataRequirements.stream() - .map(dr -> (DataRequirement) dr) - .collect(Collectors.toList())); + .setDataRequirement( + dataRequirement.stream().map(dr -> (DataRequirement) dr).collect(Collectors.toList())); return this; } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/LibraryAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/LibraryAdapter.java index 6725b052a..c741dbcb4 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/LibraryAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/LibraryAdapter.java @@ -130,11 +130,10 @@ public LibraryAdapter addDataRequirement(ICompositeType dataRequirement) { } @Override - public LibraryAdapter setDataRequirements(List dataRequirements) { + public LibraryAdapter setDataRequirement(List dataRequirement) { getLibrary() - .setDataRequirement(dataRequirements.stream() - .map(dr -> (DataRequirement) dr) - .collect(Collectors.toList())); + .setDataRequirement( + dataRequirement.stream().map(dr -> (DataRequirement) dr).collect(Collectors.toList())); return this; } diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/LibraryAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/LibraryAdapterTest.java index dbcd28010..c269bfa11 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/LibraryAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/LibraryAdapterTest.java @@ -185,11 +185,11 @@ void adapter_get_and_set_type() { @Test void adapter_get_and_set_dataRequirement() { var library = new Library(); + var adapter = new LibraryAdapter(library); var dataRequirements = new ArrayList(); dataRequirements.add(new DataRequirement().setType("Patient")); - library.setDataRequirement(dataRequirements); - var adapter = new LibraryAdapter(library); - assertEquals(dataRequirements, adapter.getDataRequirement()); + adapter.setDataRequirement(dataRequirements); + assertEquals(dataRequirements, library.getDataRequirement()); adapter.addDataRequirement(new DataRequirement().setType("Observation")); assertEquals(library.getDataRequirement(), adapter.getDataRequirement()); assertEquals(2, adapter.getDataRequirement().size()); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/LibraryAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/LibraryAdapterTest.java index a9a33611a..1dfc9b647 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/LibraryAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/LibraryAdapterTest.java @@ -185,11 +185,11 @@ void adapter_get_and_set_type() { @Test void adapter_get_and_set_dataRequirement() { var library = new Library(); + var adapter = new LibraryAdapter(library); var dataRequirements = new ArrayList(); dataRequirements.add(new DataRequirement().setType("Patient")); - library.setDataRequirement(dataRequirements); - var adapter = new LibraryAdapter(library); - assertEquals(dataRequirements, adapter.getDataRequirement()); + adapter.setDataRequirement(dataRequirements); + assertEquals(dataRequirements, library.getDataRequirement()); adapter.addDataRequirement(new DataRequirement().setType("Observation")); assertEquals(library.getDataRequirement(), adapter.getDataRequirement()); assertEquals(2, adapter.getDataRequirement().size()); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/LibraryAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/LibraryAdapterTest.java index db94b8088..83007b0ca 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/LibraryAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/LibraryAdapterTest.java @@ -186,11 +186,11 @@ void adapter_get_and_set_type() { @Test void adapter_get_and_set_dataRequirement() { var library = new Library(); + var adapter = new LibraryAdapter(library); var dataRequirements = new ArrayList(); dataRequirements.add(new DataRequirement().setType(FHIRTypes.PATIENT)); - library.setDataRequirement(dataRequirements); - var adapter = new LibraryAdapter(library); - assertEquals(dataRequirements, adapter.getDataRequirement()); + adapter.setDataRequirement(dataRequirements); + assertEquals(dataRequirements, library.getDataRequirement()); adapter.addDataRequirement(new DataRequirement().setType(FHIRTypes.OBSERVATION)); assertEquals(library.getDataRequirement(), adapter.getDataRequirement()); assertEquals(2, adapter.getDataRequirement().size()); From e249b446d88718f111db032cafca6c3a28e0cbf1 Mon Sep 17 00:00:00 2001 From: Brenin Rhodes Date: Mon, 29 Jul 2024 14:39:44 -0600 Subject: [PATCH 21/38] Questionnaire and StructureDefinition adapter work and DSTU3 extension support --- .../cqf/fhir/cql/ExtensionResolver.java | 1 + .../cqf/fhir/cql/LibraryConstructor.java | 10 +- .../opencds/cqf/fhir/cql/LibraryEngine.java | 11 +- .../BaseFhirLibrarySourceProvider.java | 1 + .../cqf/fhir/cql/LibraryEngineTests.java | 1 + .../fhir/cr/common/DynamicValueProcessor.java | 2 +- .../fhir/cr/common/ExpressionProcessor.java | 73 +++-- .../generate/ElementHasCqfExpression.java | 6 +- .../generate/GenerateRequest.java | 19 +- .../questionnaire/generate/ItemGenerator.java | 2 +- .../generate/dstu3/ElementProcessor.java | 2 +- .../populate/PopulateRequest.java | 10 +- .../populate/ProcessItemWithContext.java | 2 +- .../cr/common/DynamicValueProcessorTests.java | 2 +- .../PlanDefinitionProcessorTests.java | 2 +- .../QuestionnaireProcessorTests.java | 2 +- .../generate/ElementProcessorTests.java | 2 +- .../generate/ItemGeneratorTests.java | 2 +- .../populate/ProcessItemTests.java | 2 +- ...riorAuthorizationRequest-OPA-Patient1.json | 198 ++++--------- ...e-OutpatientPriorAuthorizationRequest.json | 198 ++++--------- .../StructureDefinition-RouteOnePatient.json | 66 +---- ...riorAuthorizationRequest-OPA-Patient1.json | 276 ++++++++---------- .../opencds/cqf/fhir/utility/Constants.java | 27 +- .../cqf/fhir/utility}/CqfExpression.java | 41 ++- .../cqf/fhir/utility/adapter/Adapter.java | 186 ++++++++++++ .../fhir/utility/adapter/EndpointAdapter.java | 2 +- .../adapter/KnowledgeArtifactAdapter.java | 24 +- .../utility/adapter/ParametersAdapter.java | 3 - .../ParametersParameterComponentAdapter.java | 5 - .../fhir/utility/adapter/ResourceAdapter.java | 161 ---------- .../adapter/dstu3/AttachmentAdapter.java | 21 +- .../dstu3/KnowledgeArtifactAdapter.java | 5 +- .../utility/adapter/dstu3/LibraryAdapter.java | 4 +- .../ParametersParameterComponentAdapter.java | 21 +- .../adapter/dstu3/PlanDefinitionAdapter.java | 6 +- .../adapter/dstu3/QuestionnaireAdapter.java | 91 +++++- .../dstu3/StructureDefinitionAdapter.java | 54 +++- .../adapter/dstu3/ValueSetAdapter.java | 5 +- .../utility/adapter/r4/AttachmentAdapter.java | 20 +- .../adapter/r4/KnowledgeArtifactAdapter.java | 5 +- .../utility/adapter/r4/LibraryAdapter.java | 4 +- .../ParametersParameterComponentAdapter.java | 21 +- .../adapter/r4/PlanDefinitionAdapter.java | 7 +- .../adapter/r4/QuestionnaireAdapter.java | 97 +++++- .../r4/StructureDefinitionAdapter.java | 77 ++++- .../utility/adapter/r4/ValueSetAdapter.java | 5 +- .../utility/adapter/r5/AttachmentAdapter.java | 20 +- .../adapter/r5/KnowledgeArtifactAdapter.java | 5 +- .../utility/adapter/r5/LibraryAdapter.java | 4 +- .../ParametersParameterComponentAdapter.java | 21 +- .../adapter/r5/PlanDefinitionAdapter.java | 6 +- .../adapter/r5/QuestionnaireAdapter.java | 97 +++++- .../r5/StructureDefinitionAdapter.java | 77 ++++- .../utility/adapter/r5/ValueSetAdapter.java | 5 +- .../cqf/fhir/utility/CqfExpressionTests.java | 12 + .../adapter/dstu3/LibraryAdapterTest.java | 32 +- .../dstu3/PlanDefinitionAdapterTest.java | 24 +- .../dstu3/QuestionnaireAdapterTest.java | 37 ++- .../adapter/dstu3/ResourceAdapterTest.java | 2 + .../dstu3/StructureDefinitionAdapterTest.java | 26 +- .../adapter/dstu3/ValueSetAdapterTest.java | 12 +- .../adapter/r4/LibraryAdapterTest.java | 31 +- .../adapter/r4/PlanDefinitionAdapterTest.java | 28 +- .../adapter/r4/QuestionnaireAdapterTest.java | 70 ++++- .../adapter/r4/ResourceAdapterTest.java | 2 + .../r4/StructureDefinitionAdapterTest.java | 35 ++- .../adapter/r4/ValueSetAdapterTest.java | 12 +- .../adapter/r5/LibraryAdapterTest.java | 31 +- .../adapter/r5/PlanDefinitionAdapterTest.java | 28 +- .../adapter/r5/QuestionnaireAdapterTest.java | 70 ++++- .../adapter/r5/ResourceAdapterTest.java | 2 + .../r5/StructureDefinitionAdapterTest.java | 35 ++- .../adapter/r5/ValueSetAdapterTest.java | 12 +- .../visitor/dstu3/ReleaseVisitorTests.java | 8 +- .../visitor/r4/ReleaseVisitorTests.java | 8 +- .../visitor/r5/ReleaseVisitorTests.java | 8 +- 77 files changed, 1626 insertions(+), 916 deletions(-) rename {cqf-fhir-cql/src/main/java/org/opencds/cqf/fhir/cql => cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility}/CqfExpression.java (70%) create mode 100644 cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/CqfExpressionTests.java diff --git a/cqf-fhir-cql/src/main/java/org/opencds/cqf/fhir/cql/ExtensionResolver.java b/cqf-fhir-cql/src/main/java/org/opencds/cqf/fhir/cql/ExtensionResolver.java index 7221a6a25..ff16b1876 100644 --- a/cqf-fhir-cql/src/main/java/org/opencds/cqf/fhir/cql/ExtensionResolver.java +++ b/cqf-fhir-cql/src/main/java/org/opencds/cqf/fhir/cql/ExtensionResolver.java @@ -10,6 +10,7 @@ import org.hl7.fhir.instance.model.api.IBaseParameters; import org.hl7.fhir.instance.model.api.IIdType; import org.opencds.cqf.fhir.utility.Constants; +import org.opencds.cqf.fhir.utility.CqfExpression; /** * This class is used to resolve any CQFExpression extensions that exist on an extension. diff --git a/cqf-fhir-cql/src/main/java/org/opencds/cqf/fhir/cql/LibraryConstructor.java b/cqf-fhir-cql/src/main/java/org/opencds/cqf/fhir/cql/LibraryConstructor.java index b6a5a13ba..df237b4c9 100644 --- a/cqf-fhir-cql/src/main/java/org/opencds/cqf/fhir/cql/LibraryConstructor.java +++ b/cqf-fhir-cql/src/main/java/org/opencds/cqf/fhir/cql/LibraryConstructor.java @@ -3,6 +3,7 @@ import static java.util.Objects.requireNonNull; import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.context.FhirVersionEnum; import ca.uhn.fhir.fhirpath.IFhirPath; import java.util.List; import org.apache.commons.lang3.tuple.Pair; @@ -46,10 +47,15 @@ private void constructExpression(StringBuilder sb, String expression) { sb.append(String.format("%ndefine \"return\":%n %s", expression)); } + private String getFhirVersionString(FhirVersionEnum fhirVersion) { + // The version of the DSTU3 enum is 3.0.2 which the CQL Engine does not support. + return fhirVersion == FhirVersionEnum.DSTU3 ? "3.0.1" : fhirVersion.getFhirVersionString(); + } + private void constructIncludes(StringBuilder sb, List> libraries) { sb.append(String.format( "include FHIRHelpers version '%s' called FHIRHelpers%n", - fhirContext.getVersion().getVersion().getFhirVersionString())); + getFhirVersionString(fhirContext.getVersion().getVersion()))); if (libraries != null) { for (Pair library : libraries) { @@ -93,7 +99,7 @@ private String getTypeDeclaration(String type, Boolean isList) { private void constructUsings(StringBuilder sb) { sb.append(String.format( "using FHIR version '%s'%n", - fhirContext.getVersion().getVersion().getFhirVersionString())); + getFhirVersionString(fhirContext.getVersion().getVersion()))); } private void constructHeader(StringBuilder sb) { diff --git a/cqf-fhir-cql/src/main/java/org/opencds/cqf/fhir/cql/LibraryEngine.java b/cqf-fhir-cql/src/main/java/org/opencds/cqf/fhir/cql/LibraryEngine.java index 137b1ab87..9dc5f7c67 100644 --- a/cqf-fhir-cql/src/main/java/org/opencds/cqf/fhir/cql/LibraryEngine.java +++ b/cqf-fhir-cql/src/main/java/org/opencds/cqf/fhir/cql/LibraryEngine.java @@ -10,6 +10,8 @@ import java.util.HashSet; import java.util.List; import java.util.Set; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.tuple.ImmutablePair; import org.apache.commons.lang3.tuple.Pair; import org.cqframework.cql.cql2elm.LibrarySourceProvider; import org.cqframework.cql.cql2elm.StringLibrarySourceProvider; @@ -20,6 +22,8 @@ import org.hl7.fhir.instance.model.api.IBaseParameters; import org.opencds.cqf.fhir.api.Repository; import org.opencds.cqf.fhir.cql.engine.parameters.CqlParameterDefinition; +import org.opencds.cqf.fhir.utility.Canonicals; +import org.opencds.cqf.fhir.utility.CqfExpression; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -161,8 +165,13 @@ public List getExpressionResult( case "text/cql.expression": case "text/cql-expression": case "text/fhirpath": + var libraries = new ArrayList>(); + if (!StringUtils.isBlank(libraryToBeEvaluated)) { + libraries.add(new ImmutablePair( + libraryToBeEvaluated, Canonicals.getIdPart(libraryToBeEvaluated))); + } parametersResult = this.evaluateExpression( - expression, parameters, subjectId, null, bundle, contextParameter, resourceParameter); + expression, parameters, subjectId, libraries, bundle, contextParameter, resourceParameter); // The expression is assumed to be the parameter component name // The expression evaluator creates a library with a single expression defined as "return" results = resolveParameterValues( diff --git a/cqf-fhir-cql/src/main/java/org/opencds/cqf/fhir/cql/cql2elm/content/BaseFhirLibrarySourceProvider.java b/cqf-fhir-cql/src/main/java/org/opencds/cqf/fhir/cql/cql2elm/content/BaseFhirLibrarySourceProvider.java index 9dd2f1029..9f275ef25 100644 --- a/cqf-fhir-cql/src/main/java/org/opencds/cqf/fhir/cql/cql2elm/content/BaseFhirLibrarySourceProvider.java +++ b/cqf-fhir-cql/src/main/java/org/opencds/cqf/fhir/cql/cql2elm/content/BaseFhirLibrarySourceProvider.java @@ -46,6 +46,7 @@ protected InputStream getContentStream(IBaseResource library, String contentType for (ICompositeType attachment : libraryAdapter.getContent()) { AttachmentAdapter attachmentAdapter = this.adapterFactory.createAttachment(attachment); if (attachmentAdapter.getContentType().equals(contentType)) { + // get externalized extension if present and add custom load data return new ByteArrayInputStream(attachmentAdapter.getData()); } } diff --git a/cqf-fhir-cql/src/test/java/org/opencds/cqf/fhir/cql/LibraryEngineTests.java b/cqf-fhir-cql/src/test/java/org/opencds/cqf/fhir/cql/LibraryEngineTests.java index 0d9fd7798..ddbcb0f6c 100644 --- a/cqf-fhir-cql/src/test/java/org/opencds/cqf/fhir/cql/LibraryEngineTests.java +++ b/cqf-fhir-cql/src/test/java/org/opencds/cqf/fhir/cql/LibraryEngineTests.java @@ -15,6 +15,7 @@ import org.hl7.fhir.r4.model.StringType; import org.hl7.fhir.r4.model.Task; import org.junit.jupiter.api.Test; +import org.opencds.cqf.fhir.utility.CqfExpression; import org.opencds.cqf.fhir.utility.repository.ig.IgRepository; class LibraryEngineTests { diff --git a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/common/DynamicValueProcessor.java b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/common/DynamicValueProcessor.java index ed6dadc7c..8946dd678 100644 --- a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/common/DynamicValueProcessor.java +++ b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/common/DynamicValueProcessor.java @@ -6,7 +6,7 @@ import org.hl7.fhir.instance.model.api.IBase; import org.hl7.fhir.instance.model.api.IBaseBackboneElement; import org.hl7.fhir.instance.model.api.IBaseResource; -import org.opencds.cqf.fhir.cql.CqfExpression; +import org.opencds.cqf.fhir.utility.CqfExpression; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/common/ExpressionProcessor.java b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/common/ExpressionProcessor.java index 49e6d1c40..ddb475552 100644 --- a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/common/ExpressionProcessor.java +++ b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/common/ExpressionProcessor.java @@ -1,5 +1,6 @@ package org.opencds.cqf.fhir.cr.common; +import ca.uhn.fhir.context.FhirVersionEnum; import java.util.ArrayList; import java.util.List; import java.util.Objects; @@ -8,8 +9,8 @@ import org.hl7.fhir.instance.model.api.IBaseBackboneElement; import org.hl7.fhir.instance.model.api.IBaseExtension; import org.hl7.fhir.instance.model.api.IBaseParameters; -import org.opencds.cqf.fhir.cql.CqfExpression; import org.opencds.cqf.fhir.utility.Constants; +import org.opencds.cqf.fhir.utility.CqfExpression; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -90,37 +91,42 @@ public List getExpressionResult( .filter(e -> e.getUrl().equals(extensionUrl)) .findFirst() .orElse(null); - if (extension == null) { - return null; - } - switch (request.getFhirVersion()) { - case DSTU3: - var languageExtension = extensions.stream() - .filter(e -> e.getUrl().equals(Constants.CQF_EXPRESSION_LANGUAGE)) - .findFirst() - .orElse(null); - var libraryExtension = extensions.stream() - .filter(e -> e.getUrl().equals(Constants.CQF_LIBRARY)) - .findFirst() - .orElse(null); - return new CqfExpression( - languageExtension == null - ? null - : languageExtension.getValue().toString(), - extension.getValue().toString(), - libraryExtension == null - ? request.getDefaultLibraryUrl() - : libraryExtension.getValue().toString()); - case R4: - return CqfExpression.of( - (org.hl7.fhir.r4.model.Expression) extension.getValue(), request.getDefaultLibraryUrl()); - case R5: - return CqfExpression.of( - (org.hl7.fhir.r5.model.Expression) extension.getValue(), request.getDefaultLibraryUrl()); + return extension == null ? null : CqfExpression.of(extension, request.getDefaultLibraryUrl()); + // if (extension == null) { + // return null; + // } + // switch (request.getFhirVersion()) { + // case DSTU3: + // // var languageExtension = extensions.stream() + // var languageExtension = extension.getExtension().stream() + // .map(e -> (IBaseExtension) e) + // .filter(e -> e.getUrl().equals(Constants.CQF_EXPRESSION_LANGUAGE)) + // .findFirst() + // .orElse(null); + // // var libraryExtension = extensions.stream() + // var libraryExtension = extension.getExtension().stream() + // .map(e -> (IBaseExtension) e) + // .filter(e -> e.getUrl().equals(Constants.CQF_LIBRARY)) + // .findFirst() + // .orElse(null); + // return new CqfExpression( + // languageExtension == null + // ? null + // : languageExtension.getValue().toString(), + // extension.getValue().toString(), + // libraryExtension == null + // ? request.getDefaultLibraryUrl() + // : libraryExtension.getValue().toString()); + // case R4: + // return CqfExpression.of( + // (org.hl7.fhir.r4.model.Expression) extension.getValue(), request.getDefaultLibraryUrl()); + // case R5: + // return CqfExpression.of( + // (org.hl7.fhir.r5.model.Expression) extension.getValue(), request.getDefaultLibraryUrl()); - default: - return null; - } + // default: + // return null; + // } } /** @@ -166,7 +172,10 @@ public CqfExpression getItemInitialExpression(IOperationRequest request, IBaseBa if (!item.hasExtension()) { return null; } - var cqfExpression = getCqfExpression(request, item.getExtension(), Constants.CQF_EXPRESSION); + var expressionExtensionUrl = request.getFhirVersion() == FhirVersionEnum.DSTU3 + ? Constants.CQIF_CQL_EXPRESSION + : Constants.CQF_EXPRESSION; + var cqfExpression = getCqfExpression(request, item.getExtension(), expressionExtensionUrl); return cqfExpression != null ? cqfExpression : getCqfExpression(request, item.getExtension(), Constants.SDC_QUESTIONNAIRE_INITIAL_EXPRESSION); diff --git a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/questionnaire/generate/ElementHasCqfExpression.java b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/questionnaire/generate/ElementHasCqfExpression.java index bf9d378c4..3f2c20bec 100644 --- a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/questionnaire/generate/ElementHasCqfExpression.java +++ b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/questionnaire/generate/ElementHasCqfExpression.java @@ -3,6 +3,7 @@ import static org.opencds.cqf.fhir.cr.common.ExtensionBuilders.buildReference; import static org.opencds.cqf.fhir.cr.questionnaire.generate.IElementProcessor.createInitial; +import ca.uhn.fhir.context.FhirVersionEnum; import java.util.List; import org.hl7.fhir.instance.model.api.IAnyResource; import org.hl7.fhir.instance.model.api.IBase; @@ -26,7 +27,10 @@ public ElementHasCqfExpression(ExpressionProcessor expressionProcessor) { public IBaseBackboneElement addProperties( IOperationRequest request, List> extensions, IBaseBackboneElement questionnaireItem) { - final var expression = expressionProcessor.getCqfExpression(request, extensions, Constants.CQF_EXPRESSION); + final var expressionExtensionUrl = request.getFhirVersion() == FhirVersionEnum.DSTU3 + ? Constants.CQIF_CQL_EXPRESSION + : Constants.CQF_EXPRESSION; + final var expression = expressionProcessor.getCqfExpression(request, extensions, expressionExtensionUrl); final List results = expressionProcessor.getExpressionResult(request, expression); results.forEach(result -> { if (IAnyResource.class.isAssignableFrom(result.getClass())) { diff --git a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/questionnaire/generate/GenerateRequest.java b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/questionnaire/generate/GenerateRequest.java index 909e8a1a6..f7fa5b0f4 100644 --- a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/questionnaire/generate/GenerateRequest.java +++ b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/questionnaire/generate/GenerateRequest.java @@ -2,15 +2,18 @@ import ca.uhn.fhir.context.FhirVersionEnum; import java.util.List; +import org.hl7.fhir.dstu3.model.Reference; import org.hl7.fhir.instance.model.api.IBaseBundle; import org.hl7.fhir.instance.model.api.IBaseOperationOutcome; import org.hl7.fhir.instance.model.api.IBaseParameters; import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.instance.model.api.ICompositeType; import org.hl7.fhir.instance.model.api.IIdType; +import org.hl7.fhir.instance.model.api.IPrimitiveType; import org.opencds.cqf.cql.engine.model.ModelResolver; import org.opencds.cqf.fhir.cql.LibraryEngine; import org.opencds.cqf.fhir.cr.common.IQuestionnaireRequest; +import org.opencds.cqf.fhir.utility.Constants; public class GenerateRequest implements IQuestionnaireRequest { private final Boolean supportedOnly; @@ -47,7 +50,7 @@ public GenerateRequest( this.modelResolver = modelResolver; fhirVersion = this.libraryEngine.getRepository().fhirContext().getVersion().getVersion(); - defaultLibraryUrl = ""; + defaultLibraryUrl = resolveDefaultLibraryUrl(); profileUrl = resolvePathString(this.profile, "url"); } @@ -151,4 +154,18 @@ public void setOperationOutcome(IBaseOperationOutcome operationOutcome) { // Errors during Questionnaire generation manifest as error items throw new UnsupportedOperationException("Unimplemented method 'setOperationOutcome'"); } + + @SuppressWarnings("unchecked") + protected final String resolveDefaultLibraryUrl() { + var libraryExt = getExtensions(profile).stream() + .filter(e -> e.getUrl() + .equals(fhirVersion == FhirVersionEnum.DSTU3 ? Constants.CQIF_LIBRARY : Constants.CQF_LIBRARY)) + .findFirst() + .orElse(null); + return libraryExt == null + ? null + : fhirVersion == FhirVersionEnum.DSTU3 + ? ((Reference) libraryExt.getValue()).getReference() + : ((IPrimitiveType) libraryExt.getValue()).getValue(); + } } diff --git a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/questionnaire/generate/ItemGenerator.java b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/questionnaire/generate/ItemGenerator.java index d091f93e9..a41f757b2 100644 --- a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/questionnaire/generate/ItemGenerator.java +++ b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/questionnaire/generate/ItemGenerator.java @@ -12,11 +12,11 @@ import org.hl7.fhir.instance.model.api.ICompositeType; import org.hl7.fhir.instance.model.api.IPrimitiveType; import org.opencds.cqf.fhir.api.Repository; -import org.opencds.cqf.fhir.cql.CqfExpression; import org.opencds.cqf.fhir.cr.common.ExpressionProcessor; import org.opencds.cqf.fhir.cr.common.ExtensionProcessor; import org.opencds.cqf.fhir.cr.common.ResolveExpressionException; import org.opencds.cqf.fhir.utility.Constants; +import org.opencds.cqf.fhir.utility.CqfExpression; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/questionnaire/generate/dstu3/ElementProcessor.java b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/questionnaire/generate/dstu3/ElementProcessor.java index c6d53efd6..7d98f5fda 100644 --- a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/questionnaire/generate/dstu3/ElementProcessor.java +++ b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/questionnaire/generate/dstu3/ElementProcessor.java @@ -58,7 +58,7 @@ public IBaseBackboneElement processElement( elementHasDefaultValue.addProperties(request, element.getPattern(), item); } else if (element.hasDefaultValue()) { elementHasDefaultValue.addProperties(request, element.getDefaultValue(), item); - } else if (element.hasExtension(Constants.CQF_EXPRESSION)) { + } else if (element.hasExtension(Constants.CQIF_CQL_EXPRESSION)) { elementHasCqfExpression.addProperties(request, request.getExtensions(element), item); } else if (caseFeature != null) { var pathValue = elementHasCaseFeature.getPathValue(request, caseFeature, element); diff --git a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/questionnaire/populate/PopulateRequest.java b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/questionnaire/populate/PopulateRequest.java index 0349f2cb4..0dd544fb6 100644 --- a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/questionnaire/populate/PopulateRequest.java +++ b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/questionnaire/populate/PopulateRequest.java @@ -3,6 +3,7 @@ import static com.google.common.base.Preconditions.checkNotNull; import ca.uhn.fhir.context.FhirVersionEnum; +import org.hl7.fhir.dstu3.model.Reference; import org.hl7.fhir.instance.model.api.IBaseBundle; import org.hl7.fhir.instance.model.api.IBaseOperationOutcome; import org.hl7.fhir.instance.model.api.IBaseParameters; @@ -117,9 +118,14 @@ public void setOperationOutcome(IBaseOperationOutcome operationOutcome) { @SuppressWarnings("unchecked") protected final String resolveDefaultLibraryUrl() { var libraryExt = getExtensions(questionnaire).stream() - .filter(e -> e.getUrl().equals(Constants.CQF_LIBRARY)) + .filter(e -> e.getUrl() + .equals(fhirVersion == FhirVersionEnum.DSTU3 ? Constants.CQIF_LIBRARY : Constants.CQF_LIBRARY)) .findFirst() .orElse(null); - return libraryExt == null ? null : ((IPrimitiveType) libraryExt.getValue()).getValue(); + return libraryExt == null + ? null + : fhirVersion == FhirVersionEnum.DSTU3 + ? ((Reference) libraryExt.getValue()).getReference() + : ((IPrimitiveType) libraryExt.getValue()).getValue(); } } diff --git a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/questionnaire/populate/ProcessItemWithContext.java b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/questionnaire/populate/ProcessItemWithContext.java index cad851f59..afc1aaa54 100644 --- a/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/questionnaire/populate/ProcessItemWithContext.java +++ b/cqf-fhir-cr/src/main/java/org/opencds/cqf/fhir/cr/questionnaire/populate/ProcessItemWithContext.java @@ -13,10 +13,10 @@ import org.hl7.fhir.instance.model.api.IBase; import org.hl7.fhir.instance.model.api.IBaseBackboneElement; import org.hl7.fhir.instance.model.api.IPrimitiveType; -import org.opencds.cqf.fhir.cql.CqfExpression; import org.opencds.cqf.fhir.cr.common.ExpressionProcessor; import org.opencds.cqf.fhir.cr.common.ResolveExpressionException; import org.opencds.cqf.fhir.utility.Constants; +import org.opencds.cqf.fhir.utility.CqfExpression; public class ProcessItemWithContext { private final ExpressionProcessor expressionProcessor; diff --git a/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/common/DynamicValueProcessorTests.java b/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/common/DynamicValueProcessorTests.java index cd061d65c..a183a7007 100644 --- a/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/common/DynamicValueProcessorTests.java +++ b/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/common/DynamicValueProcessorTests.java @@ -18,10 +18,10 @@ import org.mockito.Mock; import org.mockito.Spy; import org.mockito.junit.jupiter.MockitoExtension; -import org.opencds.cqf.fhir.cql.CqfExpression; import org.opencds.cqf.fhir.cql.LibraryEngine; import org.opencds.cqf.fhir.cr.helpers.RequestHelpers; import org.opencds.cqf.fhir.cr.inputparameters.IInputParameterResolver; +import org.opencds.cqf.fhir.utility.CqfExpression; import org.slf4j.LoggerFactory; @ExtendWith(MockitoExtension.class) diff --git a/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/plandefinition/PlanDefinitionProcessorTests.java b/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/plandefinition/PlanDefinitionProcessorTests.java index d23f832ad..b19b7028c 100644 --- a/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/plandefinition/PlanDefinitionProcessorTests.java +++ b/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/plandefinition/PlanDefinitionProcessorTests.java @@ -501,7 +501,7 @@ void packageR5() { .when() .planDefinitionId("generate-questionnaire") .thenPackage() - .hasEntry(9); + .hasEntry(10); given().repositoryFor(fhirContextR5, "r5") .when() diff --git a/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/questionnaire/QuestionnaireProcessorTests.java b/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/questionnaire/QuestionnaireProcessorTests.java index fb68e7270..fba40aa8c 100644 --- a/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/questionnaire/QuestionnaireProcessorTests.java +++ b/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/questionnaire/QuestionnaireProcessorTests.java @@ -251,7 +251,7 @@ void pa_aslp_Package() { .questionnaireId(Ids.newId(fhirContextR4, "Questionnaire", "ASLPA1")) .isPut(Boolean.TRUE) .thenPackage() - .hasEntry(11) + .hasEntry(18) .firstEntryIsType(org.hl7.fhir.r4.model.Questionnaire.class); } diff --git a/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/questionnaire/generate/ElementProcessorTests.java b/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/questionnaire/generate/ElementProcessorTests.java index a5e31bfdd..88eb88a08 100644 --- a/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/questionnaire/generate/ElementProcessorTests.java +++ b/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/questionnaire/generate/ElementProcessorTests.java @@ -23,10 +23,10 @@ import org.mockito.Spy; import org.mockito.junit.jupiter.MockitoExtension; import org.opencds.cqf.fhir.api.Repository; -import org.opencds.cqf.fhir.cql.CqfExpression; import org.opencds.cqf.fhir.cql.LibraryEngine; import org.opencds.cqf.fhir.cr.common.ExpressionProcessor; import org.opencds.cqf.fhir.utility.Constants; +import org.opencds.cqf.fhir.utility.CqfExpression; @ExtendWith(MockitoExtension.class) class ElementProcessorTests { diff --git a/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/questionnaire/generate/ItemGeneratorTests.java b/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/questionnaire/generate/ItemGeneratorTests.java index 1c8d8dab6..4a4ee5de9 100644 --- a/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/questionnaire/generate/ItemGeneratorTests.java +++ b/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/questionnaire/generate/ItemGeneratorTests.java @@ -20,10 +20,10 @@ import org.mockito.Spy; import org.mockito.junit.jupiter.MockitoExtension; import org.opencds.cqf.fhir.api.Repository; -import org.opencds.cqf.fhir.cql.CqfExpression; import org.opencds.cqf.fhir.cql.LibraryEngine; import org.opencds.cqf.fhir.cr.common.ResolveExpressionException; import org.opencds.cqf.fhir.cr.helpers.RequestHelpers; +import org.opencds.cqf.fhir.utility.CqfExpression; import org.opencds.cqf.fhir.utility.Ids; @ExtendWith(MockitoExtension.class) diff --git a/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/questionnaire/populate/ProcessItemTests.java b/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/questionnaire/populate/ProcessItemTests.java index acd7cb1dd..23100c297 100644 --- a/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/questionnaire/populate/ProcessItemTests.java +++ b/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/questionnaire/populate/ProcessItemTests.java @@ -24,11 +24,11 @@ import org.mockito.Spy; import org.mockito.junit.jupiter.MockitoExtension; import org.opencds.cqf.fhir.api.Repository; -import org.opencds.cqf.fhir.cql.CqfExpression; import org.opencds.cqf.fhir.cql.LibraryEngine; import org.opencds.cqf.fhir.cr.common.ExpressionProcessor; import org.opencds.cqf.fhir.cr.common.ResolveExpressionException; import org.opencds.cqf.fhir.utility.Constants; +import org.opencds.cqf.fhir.utility.CqfExpression; @ExtendWith(MockitoExtension.class) class ProcessItemTests { diff --git a/cqf-fhir-cr/src/test/resources/org/opencds/cqf/fhir/cr/questionnaire/dstu3/resources/Questionnaire-OutpatientPriorAuthorizationRequest-OPA-Patient1.json b/cqf-fhir-cr/src/test/resources/org/opencds/cqf/fhir/cr/questionnaire/dstu3/resources/Questionnaire-OutpatientPriorAuthorizationRequest-OPA-Patient1.json index 2f58f39dc..7a73e3327 100644 --- a/cqf-fhir-cr/src/test/resources/org/opencds/cqf/fhir/cr/questionnaire/dstu3/resources/Questionnaire-OutpatientPriorAuthorizationRequest-OPA-Patient1.json +++ b/cqf-fhir-cr/src/test/resources/org/opencds/cqf/fhir/cr/questionnaire/dstu3/resources/Questionnaire-OutpatientPriorAuthorizationRequest-OPA-Patient1.json @@ -3,8 +3,10 @@ "id": "OutpatientPriorAuthorizationRequest-OPA-Patient1", "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-library", - "valueUri": "http://somewhere.org/fhir/uv/mycontentig/Library/OutpatientPriorAuthorizationPrepopulation|1.0.0" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-library", + "valueReference": { + "reference": "http://somewhere.org/fhir/uv/mycontentig/Library/OutpatientPriorAuthorizationPrepopulation|1.0.0" + } }, { "url": "http://hl7.org/fhir/uv/sdc/StructureDefinition/sdc-questionnaire-prepopulate-subject", @@ -48,12 +50,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "FacilityName" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.FacilityName" }, { "url": "http://hl7.org/fhir/StructureDefinition/questionnaireresponse-author", @@ -71,12 +69,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "FacilityNPI" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.FacilityNPI" }, { "url": "http://hl7.org/fhir/StructureDefinition/questionnaireresponse-author", @@ -113,12 +107,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "BeneficiaryFirstName" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.BeneficiaryFirstName" }, { "url": "http://hl7.org/fhir/StructureDefinition/questionnaireresponse-author", @@ -136,12 +126,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "BeneficiaryLastName" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.BeneficiaryLastName" }, { "url": "http://hl7.org/fhir/StructureDefinition/questionnaireresponse-author", @@ -159,12 +145,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "BeneficiaryDOB" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.BeneficiaryDOB" }, { "url": "http://hl7.org/fhir/StructureDefinition/questionnaireresponse-author", @@ -182,12 +164,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "BeneficiaryMedicareID" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.BeneficiaryMedicareID" }, { "url": "http://hl7.org/fhir/StructureDefinition/questionnaireresponse-author", @@ -205,12 +183,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "BeneficiaryGender" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.BeneficiaryGender" }, { "url": "http://hl7.org/fhir/StructureDefinition/questionnaireresponse-author", @@ -235,12 +209,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "OperatingPhysicianFirstName" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.OperatingPhysicianFirstName" }, { "url": "http://hl7.org/fhir/StructureDefinition/questionnaireresponse-author", @@ -258,12 +228,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "OperatingPhysicianLastName" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.OperatingPhysicianLastName" }, { "url": "http://hl7.org/fhir/StructureDefinition/questionnaireresponse-author", @@ -281,12 +247,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "OperatingPhysicianNPI" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.OperatingPhysicianNPI" }, { "url": "http://hl7.org/fhir/StructureDefinition/questionnaireresponse-author", @@ -316,12 +278,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "OperatingPhysicianAddress1" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.OperatingPhysicianAddress1" }, { "url": "http://hl7.org/fhir/StructureDefinition/questionnaireresponse-author", @@ -339,12 +297,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "OperatingPhysicianAddress2" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.OperatingPhysicianAddress2" } ], "linkId": "3.5.2", @@ -355,12 +309,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "OperatingPhysicianAddressCity" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.OperatingPhysicianAddressCity" }, { "url": "http://hl7.org/fhir/StructureDefinition/questionnaireresponse-author", @@ -378,12 +328,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "OperatingPhysicianAddressState" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.OperatingPhysicianAddressState" }, { "url": "http://hl7.org/fhir/StructureDefinition/questionnaireresponse-author", @@ -401,12 +347,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "OperatingPhysicianAddressZip" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.OperatingPhysicianAddressZip" }, { "url": "http://hl7.org/fhir/StructureDefinition/questionnaireresponse-author", @@ -433,12 +375,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "AttendingPhysicianSame" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.AttendingPhysicianSame" }, { "url": "http://hl7.org/fhir/StructureDefinition/questionnaireresponse-author", @@ -467,12 +405,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "AttendingPhysicianFirstName" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.AttendingPhysicianFirstName" }, { "url": "http://hl7.org/fhir/StructureDefinition/questionnaireresponse-author", @@ -490,12 +424,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "AttendingPhysicianLastName" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.AttendingPhysicianLastName" }, { "url": "http://hl7.org/fhir/StructureDefinition/questionnaireresponse-author", @@ -513,12 +443,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "AttendingPhysicianNPI" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.AttendingPhysicianNPI" }, { "url": "http://hl7.org/fhir/StructureDefinition/questionnaireresponse-author", @@ -548,12 +474,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "AttendingPhysicianAddress1" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.AttendingPhysicianAddress1" }, { "url": "http://hl7.org/fhir/StructureDefinition/questionnaireresponse-author", @@ -571,12 +493,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "AttendingPhysicianAddress2" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.AttendingPhysicianAddress2" } ], "linkId": "4.2.5.2", @@ -587,12 +505,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "AttendingPhysicianAddressCity" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.AttendingPhysicianAddressCity" }, { "url": "http://hl7.org/fhir/StructureDefinition/questionnaireresponse-author", @@ -610,12 +524,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "AttendingPhysicianAddressState" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.AttendingPhysicianAddressState" }, { "url": "http://hl7.org/fhir/StructureDefinition/questionnaireresponse-author", @@ -633,12 +543,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "AttendingPhysicianAddressZip" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.AttendingPhysicianAddressZip" }, { "url": "http://hl7.org/fhir/StructureDefinition/questionnaireresponse-author", diff --git a/cqf-fhir-cr/src/test/resources/org/opencds/cqf/fhir/cr/questionnaire/dstu3/resources/Questionnaire-OutpatientPriorAuthorizationRequest.json b/cqf-fhir-cr/src/test/resources/org/opencds/cqf/fhir/cr/questionnaire/dstu3/resources/Questionnaire-OutpatientPriorAuthorizationRequest.json index e4cb4a04f..7ffa97d4a 100644 --- a/cqf-fhir-cr/src/test/resources/org/opencds/cqf/fhir/cr/questionnaire/dstu3/resources/Questionnaire-OutpatientPriorAuthorizationRequest.json +++ b/cqf-fhir-cr/src/test/resources/org/opencds/cqf/fhir/cr/questionnaire/dstu3/resources/Questionnaire-OutpatientPriorAuthorizationRequest.json @@ -3,8 +3,10 @@ "id": "OutpatientPriorAuthorizationRequest", "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-library", - "valueUri": "http://somewhere.org/fhir/uv/mycontentig/Library/OutpatientPriorAuthorizationPrepopulation|1.0.0" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-library", + "valueReference": { + "reference": "http://somewhere.org/fhir/uv/mycontentig/Library/OutpatientPriorAuthorizationPrepopulation|1.0.0" + } } ], "url": "http://hl7.org/fhir/Questionnaire/OutpatientPriorAuthorizationRequest", @@ -42,12 +44,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "FacilityName" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.FacilityName" } ], "linkId": "1.1", @@ -58,12 +56,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "FacilityNPI" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.FacilityNPI" } ], "linkId": "1.2", @@ -115,12 +109,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "BeneficiaryFirstName" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.BeneficiaryFirstName" } ], "linkId": "2.1", @@ -131,12 +121,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "BeneficiaryLastName" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.BeneficiaryLastName" } ], "linkId": "2.2", @@ -147,12 +133,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "BeneficiaryDOB" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.BeneficiaryDOB" } ], "linkId": "2.3", @@ -163,12 +145,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "BeneficiaryMedicareID" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.BeneficiaryMedicareID" } ], "linkId": "2.4", @@ -179,12 +157,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "BeneficiaryGender" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.BeneficiaryGender" } ], "linkId": "2.5", @@ -233,12 +207,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "OperatingPhysicianFirstName" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.OperatingPhysicianFirstName" } ], "linkId": "3.1", @@ -249,12 +219,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "OperatingPhysicianLastName" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.OperatingPhysicianLastName" } ], "linkId": "3.2", @@ -265,12 +231,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "OperatingPhysicianNPI" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.OperatingPhysicianNPI" } ], "linkId": "3.3", @@ -293,12 +255,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "OperatingPhysicianAddress1" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.OperatingPhysicianAddress1" } ], "linkId": "3.5.1", @@ -309,12 +267,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "OperatingPhysicianAddress2" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.OperatingPhysicianAddress2" } ], "linkId": "3.5.2", @@ -325,12 +279,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "OperatingPhysicianAddressCity" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.OperatingPhysicianAddressCity" } ], "linkId": "3.5.3", @@ -341,12 +291,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "OperatingPhysicianAddressState" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.OperatingPhysicianAddressState" } ], "linkId": "3.5.4", @@ -357,12 +303,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "OperatingPhysicianAddressZip" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.OperatingPhysicianAddressZip" } ], "linkId": "3.5.5", @@ -382,12 +324,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "AttendingPhysicianSame" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.AttendingPhysicianSame" } ], "linkId": "4.1", @@ -410,12 +348,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "AttendingPhysicianFirstName" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.AttendingPhysicianFirstName" } ], "linkId": "4.2.1", @@ -426,12 +360,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "AttendingPhysicianLastName" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.AttendingPhysicianLastName" } ], "linkId": "4.2.2", @@ -442,12 +372,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "AttendingPhysicianNPI" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.AttendingPhysicianNPI" } ], "linkId": "4.2.3", @@ -470,12 +396,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "AttendingPhysicianAddress1" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.AttendingPhysicianAddress1" } ], "linkId": "4.2.5.1", @@ -486,12 +408,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "AttendingPhysicianAddress2" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.AttendingPhysicianAddress2" } ], "linkId": "4.2.5.2", @@ -502,12 +420,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "AttendingPhysicianAddressCity" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.AttendingPhysicianAddressCity" } ], "linkId": "4.2.5.3", @@ -518,12 +432,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "AttendingPhysicianAddressState" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.AttendingPhysicianAddressState" } ], "linkId": "4.2.5.4", @@ -534,12 +444,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "AttendingPhysicianAddressZip" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.AttendingPhysicianAddressZip" } ], "linkId": "4.2.5.5", diff --git a/cqf-fhir-cr/src/test/resources/org/opencds/cqf/fhir/cr/questionnaire/dstu3/resources/StructureDefinition-RouteOnePatient.json b/cqf-fhir-cr/src/test/resources/org/opencds/cqf/fhir/cr/questionnaire/dstu3/resources/StructureDefinition-RouteOnePatient.json index d80fd8f64..aa52392dc 100644 --- a/cqf-fhir-cr/src/test/resources/org/opencds/cqf/fhir/cr/questionnaire/dstu3/resources/StructureDefinition-RouteOnePatient.json +++ b/cqf-fhir-cr/src/test/resources/org/opencds/cqf/fhir/cr/questionnaire/dstu3/resources/StructureDefinition-RouteOnePatient.json @@ -12,6 +12,12 @@ { "url": "http://hl7.org/fhir/StructureDefinition/structuredefinition-wg", "valueCode": "pa" + }, + { + "url": "http://hl7.org/fhir/StructureDefinition/cqif-library", + "valueReference": { + "reference": "http://somewhere.org/fhir/uv/mycontentig/Library/OutpatientPriorAuthorizationPrepopulation" + } } ], "url": "http://fhir.org/guides/cdc/opioid-cds/StructureDefinition/RouteOnePatient", @@ -1840,16 +1846,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "BeneficiaryFirstName" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-library", - "valueString": "http://somewhere.org/fhir/uv/mycontentig/Library/OutpatientPriorAuthorizationPrepopulation" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.BeneficiaryFirstName" } ], "id": "Patient.name.given", @@ -1865,16 +1863,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "BeneficiaryLastName" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-library", - "valueString": "http://somewhere.org/fhir/uv/mycontentig/Library/OutpatientPriorAuthorizationPrepopulation" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.BeneficiaryLastName" } ], "id": "Patient.name.family", @@ -1890,16 +1880,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "BeneficiaryDOB" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-library", - "valueString": "http://somewhere.org/fhir/uv/mycontentig/Library/OutpatientPriorAuthorizationPrepopulation" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.BeneficiaryDOB" } ], "id": "Patient.birthDate", @@ -1915,16 +1897,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "BeneficiaryGender" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-library", - "valueString": "http://somewhere.org/fhir/uv/mycontentig/Library/OutpatientPriorAuthorizationPrepopulation" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.BeneficiaryGender" } ], "id": "Patient.gender", @@ -1986,16 +1960,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "BeneficiaryMedicareID" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-library", - "valueString": "http://somewhere.org/fhir/uv/mycontentig/Library/OutpatientPriorAuthorizationPrepopulation" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.BeneficiaryMedicareID" } ], "id": "Patient.identifier:MedicareID.value", diff --git a/cqf-fhir-cr/src/test/resources/org/opencds/cqf/fhir/cr/questionnaire/dstu3/tests/QuestionnaireResponse-OutpatientPriorAuthorizationRequest-OPA-Patient1.json b/cqf-fhir-cr/src/test/resources/org/opencds/cqf/fhir/cr/questionnaire/dstu3/tests/QuestionnaireResponse-OutpatientPriorAuthorizationRequest-OPA-Patient1.json index 5486d73ad..959d678a4 100644 --- a/cqf-fhir-cr/src/test/resources/org/opencds/cqf/fhir/cr/questionnaire/dstu3/tests/QuestionnaireResponse-OutpatientPriorAuthorizationRequest-OPA-Patient1.json +++ b/cqf-fhir-cr/src/test/resources/org/opencds/cqf/fhir/cr/questionnaire/dstu3/tests/QuestionnaireResponse-OutpatientPriorAuthorizationRequest-OPA-Patient1.json @@ -7,15 +7,27 @@ "id": "OutpatientPriorAuthorizationRequest", "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-library", - "valueUri": "http://somewhere.org/fhir/uv/mycontentig/Library/OutpatientPriorAuthorizationPrepopulation|1.0.0" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-library", + "valueReference": { + "reference": "http://somewhere.org/fhir/uv/mycontentig/Library/OutpatientPriorAuthorizationPrepopulation|1.0.0" + } } ], "url": "http://hl7.org/fhir/Questionnaire/OutpatientPriorAuthorizationRequest", "name": "OutpatientPriorAuthorizationRequest", "title": "Outpatient Prior Authorization Request", "status": "active", + "subjectType": [ + "Patient", + "Organization", + "Claim" + ], "date": "2022-01-04T00:00:00+00:00", + "contact": [ + { + "name": "Palmetto GBA" + } + ], "description": "Testing the form", "jurisdiction": [ { @@ -27,16 +39,6 @@ ] } ], - "contact": [ - { - "name": "Palmetto GBA" - } - ], - "subjectType": [ - "Patient", - "Organization", - "Claim" - ], "item": [ { "linkId": "1", @@ -46,12 +48,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "FacilityName" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.FacilityName" } ], "linkId": "1.1", @@ -62,12 +60,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "FacilityNPI" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.FacilityNPI" } ], "linkId": "1.2", @@ -85,7 +79,29 @@ "linkId": "1.4", "text": "Contract/Region", "type": "choice", - "required": false + "required": false, + "answerOption": [ + { + "id": "FacilityContractRegion-11501", + "valueCoding": { + "code": "11001", + "display": "Part A South Carolina" + } + }, + { + "id": "FacilityContractRegion-11003", + "valueCoding": { + "code": "11501", + "display": "Part A North Carolina" + } + }, + { + "valueCoding": { + "code": "11003", + "display": "Part A Virginia/West Virginia" + } + } + ] } ] }, @@ -97,12 +113,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "BeneficiaryFirstName" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.BeneficiaryFirstName" } ], "linkId": "2.1", @@ -113,12 +125,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "BeneficiaryLastName" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.BeneficiaryLastName" } ], "linkId": "2.2", @@ -129,12 +137,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "BeneficiaryDOB" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.BeneficiaryDOB" } ], "linkId": "2.3", @@ -145,12 +149,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "BeneficiaryMedicareID" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.BeneficiaryMedicareID" } ], "linkId": "2.4", @@ -161,18 +161,45 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "BeneficiaryGender" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.BeneficiaryGender" } ], "linkId": "2.5", "text": "Gender", "type": "choice", - "required": true + "required": true, + "answerOption": [ + { + "id": "unknown", + "valueCoding": { + "system": "http://hl7.org/fhir/ValueSet/administrative-gender", + "code": "male", + "display": "Male" + } + }, + { + "valueCoding": { + "system": "http://hl7.org/fhir/ValueSet/administrative-gender", + "code": "female", + "display": "Female" + } + }, + { + "valueCoding": { + "system": "http://hl7.org/fhir/ValueSet/administrative-gender", + "code": "other", + "display": "Other" + } + }, + { + "valueCoding": { + "system": "http://hl7.org/fhir/ValueSet/administrative-gender", + "code": "unknown", + "display": "Unknown" + } + } + ] } ] }, @@ -184,12 +211,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "OperatingPhysicianFirstName" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.OperatingPhysicianFirstName" } ], "linkId": "3.1", @@ -200,12 +223,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "OperatingPhysicianLastName" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.OperatingPhysicianLastName" } ], "linkId": "3.2", @@ -216,12 +235,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "OperatingPhysicianNPI" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.OperatingPhysicianNPI" } ], "linkId": "3.3", @@ -244,12 +259,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "OperatingPhysicianAddress1" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.OperatingPhysicianAddress1" } ], "linkId": "3.5.1", @@ -260,12 +271,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "OperatingPhysicianAddress2" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.OperatingPhysicianAddress2" } ], "linkId": "3.5.2", @@ -276,12 +283,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "OperatingPhysicianAddressCity" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.OperatingPhysicianAddressCity" } ], "linkId": "3.5.3", @@ -292,12 +295,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "OperatingPhysicianAddressState" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.OperatingPhysicianAddressState" } ], "linkId": "3.5.4", @@ -308,12 +307,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "OperatingPhysicianAddressZip" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.OperatingPhysicianAddressZip" } ], "linkId": "3.5.5", @@ -333,12 +328,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "AttendingPhysicianSame" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.AttendingPhysicianSame" } ], "linkId": "4.1", @@ -353,6 +344,7 @@ "enableWhen": [ { "question": "4.1", + "operator": "=", "answerBoolean": false } ], @@ -360,12 +352,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "AttendingPhysicianFirstName" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.AttendingPhysicianFirstName" } ], "linkId": "4.2.1", @@ -376,12 +364,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "AttendingPhysicianLastName" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.AttendingPhysicianLastName" } ], "linkId": "4.2.2", @@ -392,12 +376,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "AttendingPhysicianNPI" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.AttendingPhysicianNPI" } ], "linkId": "4.2.3", @@ -420,12 +400,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "AttendingPhysicianAddress1" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.AttendingPhysicianAddress1" } ], "linkId": "4.2.5.1", @@ -436,12 +412,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "AttendingPhysicianAddress2" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.AttendingPhysicianAddress2" } ], "linkId": "4.2.5.2", @@ -452,12 +424,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "AttendingPhysicianAddressCity" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.AttendingPhysicianAddressCity" } ], "linkId": "4.2.5.3", @@ -468,12 +436,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "AttendingPhysicianAddressState" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.AttendingPhysicianAddressState" } ], "linkId": "4.2.5.4", @@ -484,12 +448,8 @@ { "extension": [ { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression-language", - "valueString": "text/cql.identifier" - }, - { - "url": "http://hl7.org/fhir/StructureDefinition/cqf-expression", - "valueString": "AttendingPhysicianAddressZip" + "url": "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression", + "valueString": "OutpatientPriorAuthorizationPrepopulation.AttendingPhysicianAddressZip" } ], "linkId": "4.2.5.5", diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/Constants.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/Constants.java index 9465dc917..75ec387f0 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/Constants.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/Constants.java @@ -30,8 +30,19 @@ private Constants() {} public static final String REQUEST_DO_NOT_PERFORM = "http://hl7.org/fhir/StructureDefinition/request-doNotPerform"; public static final String QUESTIONNAIRE_RESPONSE_AUTHOR = "http://hl7.org/fhir/StructureDefinition/questionnaireresponse-author"; + public static final String QUESTIONNAIRE_REFERENCE_PROFILE = + "http://hl7.org/fhir/StructureDefinition/questionnaire-referenceProfile"; + public static final String QUESTIONNAIRE_UNIT_VALUE_SET = + "http://hl7.org/fhir/StructureDefinition/questionnaire-unitValueSet"; + + public static final String VARIABLE_EXTENSION = "http://hl7.org/fhir/StructureDefinition/variable"; + + public static final String CPG_ASSERTION_EXPRESSION = + "http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-assertionExpression"; public static final String CPG_FEATURE_EXPRESSION = "http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-featureExpression"; + public static final String CPG_INFERENCE_EXPRESSION = + "http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-inferenceExpression"; public static final String CPG_KNOWLEDGE_CAPABILITY = "http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-knowledgeCapability"; public static final String CPG_KNOWLEDGE_REPRESENTATION_LEVEL = @@ -45,20 +56,20 @@ private Constants() {} public static final String CPG_INPUT_TEXT = "http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-input-text"; public static final String CPG_INPUT_DESCRIPTION = "http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-input-description"; - // public static final String CQF_QUESTIONNAIRE = - // "http://hl7.org/fhir/StructureDefinition/cqf-questionnaire"; public static final String CPG_CUSTOM_ACTIVITY_KIND = "http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-custom-activity-kind"; public static final String CPG_ACTIVITY_KIND = "http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-activity-kind"; + // DSTU3 CQF Extensions + public static final String CQIF_LIBRARY = "http://hl7.org/fhir/StructureDefinition/cqif-library"; + public static final String CQIF_CQL_EXPRESSION = "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression"; + public static final String CQF_EXPANSION_PARAMETERS = "http://hl7.org/fhir/StructureDefinition/cqf-expansionParameters"; public static final String CQF_EXPRESSION = "http://hl7.org/fhir/StructureDefinition/cqf-expression"; - // This is only for dstu3 since the Expression type does not exist in that version - public static final String CQF_EXPRESSION_LANGUAGE = - "http://hl7.org/fhir/StructureDefinition/cqf-expression-language"; public static final String CQF_LIBRARY = "http://hl7.org/fhir/StructureDefinition/cqf-library"; + public static final String CQF_CALCULATED_VALUE = "http://hl7.org/fhir/StructureDefinition/cqf-calculatedValue"; public static final String CQFM_EFFECTIVE_DATA_REQUIREMENTS = "http://hl7.org/fhir/us/cqfmeasures/StructureDefinition/cqfm-effectiveDataRequirements"; @@ -98,6 +109,12 @@ private Constants() {} "http://hl7.org/fhir/uv/sdc/StructureDefinition/sdc-questionnaire-observation-extract-category"; public static final String SDC_OBSERVATION_CATEGORY = "http://hl7.org/fhir/observation-category"; public static final String SDC_CATEGORY_SURVEY = "survey"; + public static final String SDC_QUESTIONNAIRE_LAUNCH_CONTEXT = + "http://hl7.org/fhir/uv/sdc/StructureDefinition/sdc-questionnaire-launchContext"; + public static final String SDC_QUESTIONNAIRE_SUB_QUESTIONNAIRE = + "http://hl7.org/fhir/uv/sdc/StructureDefinition/sdc-questionnaire-subQuestionnaire"; + public static final String SDC_QUESTIONNAIRE_CALCULATED_EXPRESSION = + "http://hl7.org/fhir/uv/sdc/StructureDefinition/sdc-questionnaire-calculatedExpression"; // $apply parameter names public static final String APPLY_PARAMETER_ACTIVITY_DEFINITION = "activityDefinition"; diff --git a/cqf-fhir-cql/src/main/java/org/opencds/cqf/fhir/cql/CqfExpression.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/CqfExpression.java similarity index 70% rename from cqf-fhir-cql/src/main/java/org/opencds/cqf/fhir/cql/CqfExpression.java rename to cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/CqfExpression.java index eb2a0e91f..11aa9825a 100644 --- a/cqf-fhir-cql/src/main/java/org/opencds/cqf/fhir/cql/CqfExpression.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/CqfExpression.java @@ -1,6 +1,7 @@ -package org.opencds.cqf.fhir.cql; +package org.opencds.cqf.fhir.utility; -import org.opencds.cqf.fhir.utility.Constants; +import ca.uhn.fhir.context.FhirVersionEnum; +import org.hl7.fhir.instance.model.api.IBaseExtension; /** * This class is used to contain the various properties of a CqfExpression with an alternate so that @@ -15,6 +16,42 @@ public class CqfExpression { private String altExpression; private String altLibraryUrl; + public static CqfExpression of(IBaseExtension extension, String defaultLibraryUrl) { + if (extension == null) { + return null; + } + var fhirPackagePath = "org.hl7.fhir."; + var className = extension.getClass().getCanonicalName(); + var modelSplit = className.split(fhirPackagePath); + if (modelSplit.length < 2) { + throw new IllegalArgumentException(); + } + var model = modelSplit[1]; + model = model.substring(0, model.indexOf(".")).toUpperCase(); + var version = FhirVersionEnum.forVersionString(model); + switch (version) { + case DSTU3: + var libraryExtension = extension.getExtension().stream() + .map(e -> (IBaseExtension) e) + .filter(e -> e.getUrl().equals(Constants.CQIF_LIBRARY)) + .findFirst() + .orElse(null); + return new CqfExpression( + "text/cql.expression", + extension.getValue().toString(), + libraryExtension == null + ? defaultLibraryUrl + : libraryExtension.getValue().toString()); + case R4: + return CqfExpression.of((org.hl7.fhir.r4.model.Expression) extension.getValue(), defaultLibraryUrl); + case R5: + return CqfExpression.of((org.hl7.fhir.r5.model.Expression) extension.getValue(), defaultLibraryUrl); + + default: + return null; + } + } + public static CqfExpression of(org.hl7.fhir.r4.model.Expression expression, String defaultLibraryUrl) { if (expression == null) { return null; diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/Adapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/Adapter.java index f5491d858..c18a97e6a 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/Adapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/Adapter.java @@ -1,6 +1,20 @@ package org.opencds.cqf.fhir.utility.adapter; +import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.context.FhirVersionEnum; +import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Date; +import java.util.List; +import java.util.stream.Collectors; import org.hl7.fhir.instance.model.api.IBase; +import org.hl7.fhir.instance.model.api.IBaseExtension; +import org.hl7.fhir.instance.model.api.ICompositeType; +import org.hl7.fhir.instance.model.api.IPrimitiveType; +import org.opencds.cqf.cql.engine.model.ModelResolver; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Marker interface for HL7 Structure adapters @@ -8,8 +22,180 @@ * @param An HL7 Structure Type */ public interface Adapter { + public static final Logger logger = LoggerFactory.getLogger(Adapter.class); + /** * @return returns the underlying HL7 Structure for this adapter */ T get(); + + public default void setExtension(List> extensions) { + try { + getModelResolver().setValue(get(), "extension", null); + getModelResolver().setValue(get(), "extension", extensions); + } catch (Exception e) { + // Do nothing + logger.debug("Field 'extension' does not exist on Element type {}", get().fhirType()); + } + } + + public default > void addExtension(E extension) { + try { + getModelResolver().setValue(get(), "extension", Collections.singletonList(extension)); + } catch (Exception e) { + // Do nothing + logger.debug("Field 'extension' does not exist on Element type {}", get().fhirType()); + } + } + + public default boolean hasExtension() { + return !getExtension().isEmpty(); + } + + public default boolean hasExtension(String url) { + return hasExtension(get(), url); + } + + public default List> getExtension() { + return getExtension(get()); + } + + public default IBaseExtension getExtensionByUrl(String url) { + return getExtensionByUrl(get(), url); + } + + public default List> getExtensionsByUrl(String url) { + return getExtensionsByUrl(get(), url); + } + + public FhirContext fhirContext(); + + public ModelResolver getModelResolver(); + + @SuppressWarnings("unchecked") + public default > List getExtension(IBase base) { + return resolvePathList(base, "extension").stream().map(e -> (E) e).collect(Collectors.toList()); + } + + public default List> getExtensionsByUrl(IBase base, String url) { + return getExtension(base).stream().filter(e -> e.getUrl().equals(url)).collect(Collectors.toList()); + } + + public default IBaseExtension getExtensionByUrl(IBase base, String url) { + return getExtensionsByUrl(base, url).stream().findFirst().orElse(null); + } + + public default Boolean hasExtension(IBase base, String url) { + return getExtension(base).stream().anyMatch(e -> e.getUrl().equals(url)); + } + + @SuppressWarnings("unchecked") + public default List resolvePathList(IBase base, String path) { + var pathResult = getModelResolver().resolvePath(base, path); + return pathResult instanceof List ? (List) pathResult : new ArrayList<>(); + } + + @SuppressWarnings("unchecked") + public default List resolvePathList(IBase base, String path, Class clazz) { + return resolvePathList(base, path).stream().map(i -> (B) i).collect(Collectors.toList()); + } + + @SuppressWarnings("unchecked") + public default String resolvePathString(IBase base, String path) { + var result = (IPrimitiveType) resolvePath(base, path); + return result == null ? null : result.getValue(); + } + + public default IBase resolvePath(IBase base, String path) { + return (IBase) getModelResolver().resolvePath(base, path); + } + + @SuppressWarnings("unchecked") + public default B resolvePath(IBase base, String path, Class clazz) { + return (B) resolvePath(base, path); + } + + @SuppressWarnings("unchecked") + static T newPeriod(FhirVersionEnum version) { + switch (version) { + case DSTU3: + return (T) new org.hl7.fhir.dstu3.model.Period(); + case R4: + return (T) new org.hl7.fhir.r4.model.Period(); + case R5: + return (T) new org.hl7.fhir.r5.model.Period(); + default: + throw new UnprocessableEntityException("Unsupported version: " + version.toString()); + } + } + + @SuppressWarnings("unchecked") + static > T newStringType(FhirVersionEnum version, String string) { + switch (version) { + case DSTU3: + return (T) new org.hl7.fhir.dstu3.model.StringType(string); + case R4: + return (T) new org.hl7.fhir.r4.model.StringType(string); + case R5: + return (T) new org.hl7.fhir.r5.model.StringType(string); + default: + throw new UnprocessableEntityException("Unsupported version: " + version.toString()); + } + } + + @SuppressWarnings("unchecked") + static > T newUriType(FhirVersionEnum version, String string) { + switch (version) { + case DSTU3: + return (T) new org.hl7.fhir.dstu3.model.UriType(string); + case R4: + return (T) new org.hl7.fhir.r4.model.UriType(string); + case R5: + return (T) new org.hl7.fhir.r5.model.UriType(string); + default: + throw new UnprocessableEntityException("Unsupported version: " + version.toString()); + } + } + + @SuppressWarnings("unchecked") + static > T newUrlType(FhirVersionEnum version, String string) { + switch (version) { + case DSTU3: + return (T) new org.hl7.fhir.dstu3.model.UriType(string); + case R4: + return (T) new org.hl7.fhir.r4.model.UrlType(string); + case R5: + return (T) new org.hl7.fhir.r5.model.UrlType(string); + default: + throw new UnprocessableEntityException("Unsupported version: " + version.toString()); + } + } + + @SuppressWarnings("unchecked") + static > T newDateType(FhirVersionEnum version, Date date) { + switch (version) { + case DSTU3: + return (T) new org.hl7.fhir.dstu3.model.DateType(date); + case R4: + return (T) new org.hl7.fhir.r4.model.DateType(date); + case R5: + return (T) new org.hl7.fhir.r5.model.DateType(date); + default: + throw new UnprocessableEntityException("Unsupported version: " + version.toString()); + } + } + + @SuppressWarnings("unchecked") + static > T newDateTimeType(FhirVersionEnum version, Date date) { + switch (version) { + case DSTU3: + return (T) new org.hl7.fhir.dstu3.model.DateTimeType(date); + case R4: + return (T) new org.hl7.fhir.r4.model.DateTimeType(date); + case R5: + return (T) new org.hl7.fhir.r5.model.DateTimeType(date); + default: + throw new UnprocessableEntityException("Unsupported version: " + version.toString()); + } + } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/EndpointAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/EndpointAdapter.java index f213aca33..1fae75804 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/EndpointAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/EndpointAdapter.java @@ -1,6 +1,6 @@ package org.opencds.cqf.fhir.utility.adapter; -import static org.opencds.cqf.fhir.utility.adapter.ResourceAdapter.newUrlType; +import static org.opencds.cqf.fhir.utility.adapter.Adapter.newUrlType; public interface EndpointAdapter extends ResourceAdapter { public default String getAddress() { diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/KnowledgeArtifactAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/KnowledgeArtifactAdapter.java index 8a647e1aa..8b979c269 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/KnowledgeArtifactAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/KnowledgeArtifactAdapter.java @@ -1,10 +1,10 @@ package org.opencds.cqf.fhir.utility.adapter; -import static org.opencds.cqf.fhir.utility.adapter.ResourceAdapter.newDateTimeType; -import static org.opencds.cqf.fhir.utility.adapter.ResourceAdapter.newDateType; -import static org.opencds.cqf.fhir.utility.adapter.ResourceAdapter.newPeriod; -import static org.opencds.cqf.fhir.utility.adapter.ResourceAdapter.newStringType; -import static org.opencds.cqf.fhir.utility.adapter.ResourceAdapter.newUriType; +import static org.opencds.cqf.fhir.utility.adapter.Adapter.newDateTimeType; +import static org.opencds.cqf.fhir.utility.adapter.Adapter.newDateType; +import static org.opencds.cqf.fhir.utility.adapter.Adapter.newPeriod; +import static org.opencds.cqf.fhir.utility.adapter.Adapter.newStringType; +import static org.opencds.cqf.fhir.utility.adapter.Adapter.newUriType; import ca.uhn.fhir.context.FhirVersionEnum; import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; @@ -79,6 +79,20 @@ default void setVersion(String version) { List getDependencies(); + default String getReferenceSource() { + return hasVersion() ? getUrl() + "|" + getVersion() : getUrl(); + } + + default void addProfileReferences(List references, String referenceSource) { + get().getMeta().getProfile().stream() + .map(p -> (IBaseHasExtensions & IPrimitiveType) p) + .forEach(profile -> references.add(new DependencyInfo( + referenceSource, + profile.getValueAsString(), + profile.getExtension(), + (reference) -> profile.setValue(reference)))); + } + @SuppressWarnings("unchecked") default Date getApprovalDate() { IPrimitiveType approvalDate = resolvePath(get(), "approvalDate", IPrimitiveType.class); diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/ParametersAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/ParametersAdapter.java index b2fe2ae57..0363e8ce8 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/ParametersAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/ParametersAdapter.java @@ -4,12 +4,9 @@ import org.hl7.fhir.instance.model.api.IBase; import org.hl7.fhir.instance.model.api.IBaseBackboneElement; import org.hl7.fhir.instance.model.api.IBaseDatatype; -import org.hl7.fhir.instance.model.api.IBaseResource; public interface ParametersAdapter extends ResourceAdapter { - public IBaseResource get(); - public List getParameter(); public IBaseBackboneElement getParameter(String name); diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/ParametersParameterComponentAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/ParametersParameterComponentAdapter.java index 2dfe345ab..8ce61c679 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/ParametersParameterComponentAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/ParametersParameterComponentAdapter.java @@ -3,7 +3,6 @@ import java.util.List; import org.hl7.fhir.instance.model.api.IBaseBackboneElement; import org.hl7.fhir.instance.model.api.IBaseDatatype; -import org.hl7.fhir.instance.model.api.IBaseExtension; import org.hl7.fhir.instance.model.api.IBaseResource; public interface ParametersParameterComponentAdapter extends Adapter { @@ -35,8 +34,4 @@ public interface ParametersParameterComponentAdapter extends Adapter> getExtension(); } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/ResourceAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/ResourceAdapter.java index 76388f35e..174339e1f 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/ResourceAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/ResourceAdapter.java @@ -1,19 +1,9 @@ package org.opencds.cqf.fhir.utility.adapter; -import ca.uhn.fhir.context.FhirContext; -import ca.uhn.fhir.context.FhirVersionEnum; -import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; -import java.util.ArrayList; -import java.util.Date; import java.util.List; -import java.util.stream.Collectors; import org.hl7.fhir.exceptions.FHIRException; import org.hl7.fhir.instance.model.api.IBase; -import org.hl7.fhir.instance.model.api.IBaseExtension; import org.hl7.fhir.instance.model.api.IBaseResource; -import org.hl7.fhir.instance.model.api.ICompositeType; -import org.hl7.fhir.instance.model.api.IPrimitiveType; -import org.opencds.cqf.cql.engine.model.ModelResolver; public interface ResourceAdapter extends Adapter { @@ -41,26 +31,6 @@ public interface ResourceAdapter extends Adapter { public boolean equalsShallow(IBase other); - public void setExtension(List> extensions); - - public > void addExtension(T extension); - - public default boolean hasExtension(String url) { - return hasExtension(get(), url); - } - - public default List> getExtension() { - return getExtension(get()); - } - - public default IBaseExtension getExtensionByUrl(String url) { - return getExtensionByUrl(get(), url); - } - - public default List> getExtensionsByUrl(String url) { - return getExtensionsByUrl(get(), url); - } - public default List getContained() { return getContained(get()); } @@ -69,27 +39,6 @@ public default boolean hasContained() { return hasContained(get()); } - public FhirContext fhirContext(); - - public ModelResolver getModelResolver(); - - @SuppressWarnings("unchecked") - public default > List getExtension(IBase base) { - return resolvePathList(base, "extension").stream().map(e -> (E) e).collect(Collectors.toList()); - } - - public default List> getExtensionsByUrl(IBase base, String url) { - return getExtension(base).stream().filter(e -> e.getUrl().equals(url)).collect(Collectors.toList()); - } - - public default IBaseExtension getExtensionByUrl(IBase base, String url) { - return getExtensionsByUrl(base, url).stream().findFirst().orElse(null); - } - - public default Boolean hasExtension(IBase base, String url) { - return getExtension(base).stream().anyMatch(e -> e.getUrl().equals(url)); - } - public default List getContained(IBaseResource base) { return resolvePathList(base, "contained", IBaseResource.class); } @@ -97,114 +46,4 @@ public default List getContained(IBaseResource base) { public default Boolean hasContained(IBaseResource base) { return !getContained(base).isEmpty(); } - - @SuppressWarnings("unchecked") - public default List resolvePathList(IBase base, String path) { - var pathResult = getModelResolver().resolvePath(base, path); - return pathResult instanceof List ? (List) pathResult : new ArrayList<>(); - } - - @SuppressWarnings("unchecked") - public default List resolvePathList(IBase base, String path, Class clazz) { - return resolvePathList(base, path).stream().map(i -> (T) i).collect(Collectors.toList()); - } - - @SuppressWarnings("unchecked") - public default String resolvePathString(IBase base, String path) { - var result = (IPrimitiveType) resolvePath(base, path); - return result == null ? null : result.getValue(); - } - - public default IBase resolvePath(IBase base, String path) { - return (IBase) getModelResolver().resolvePath(base, path); - } - - @SuppressWarnings("unchecked") - public default T resolvePath(IBase base, String path, Class clazz) { - return (T) resolvePath(base, path); - } - - @SuppressWarnings("unchecked") - static T newPeriod(FhirVersionEnum version) { - switch (version) { - case DSTU3: - return (T) new org.hl7.fhir.dstu3.model.Period(); - case R4: - return (T) new org.hl7.fhir.r4.model.Period(); - case R5: - return (T) new org.hl7.fhir.r5.model.Period(); - default: - throw new UnprocessableEntityException("Unsupported version: " + version.toString()); - } - } - - @SuppressWarnings("unchecked") - static > T newStringType(FhirVersionEnum version, String string) { - switch (version) { - case DSTU3: - return (T) new org.hl7.fhir.dstu3.model.StringType(string); - case R4: - return (T) new org.hl7.fhir.r4.model.StringType(string); - case R5: - return (T) new org.hl7.fhir.r5.model.StringType(string); - default: - throw new UnprocessableEntityException("Unsupported version: " + version.toString()); - } - } - - @SuppressWarnings("unchecked") - static > T newUriType(FhirVersionEnum version, String string) { - switch (version) { - case DSTU3: - return (T) new org.hl7.fhir.dstu3.model.UriType(string); - case R4: - return (T) new org.hl7.fhir.r4.model.UriType(string); - case R5: - return (T) new org.hl7.fhir.r5.model.UriType(string); - default: - throw new UnprocessableEntityException("Unsupported version: " + version.toString()); - } - } - - @SuppressWarnings("unchecked") - static > T newUrlType(FhirVersionEnum version, String string) { - switch (version) { - case DSTU3: - return (T) new org.hl7.fhir.dstu3.model.UriType(string); - case R4: - return (T) new org.hl7.fhir.r4.model.UrlType(string); - case R5: - return (T) new org.hl7.fhir.r5.model.UrlType(string); - default: - throw new UnprocessableEntityException("Unsupported version: " + version.toString()); - } - } - - @SuppressWarnings("unchecked") - static > T newDateType(FhirVersionEnum version, Date date) { - switch (version) { - case DSTU3: - return (T) new org.hl7.fhir.dstu3.model.DateType(date); - case R4: - return (T) new org.hl7.fhir.r4.model.DateType(date); - case R5: - return (T) new org.hl7.fhir.r5.model.DateType(date); - default: - throw new UnprocessableEntityException("Unsupported version: " + version.toString()); - } - } - - @SuppressWarnings("unchecked") - static > T newDateTimeType(FhirVersionEnum version, Date date) { - switch (version) { - case DSTU3: - return (T) new org.hl7.fhir.dstu3.model.DateTimeType(date); - case R4: - return (T) new org.hl7.fhir.r4.model.DateTimeType(date); - case R5: - return (T) new org.hl7.fhir.r5.model.DateTimeType(date); - default: - throw new UnprocessableEntityException("Unsupported version: " + version.toString()); - } - } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/AttachmentAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/AttachmentAdapter.java index da00e4daf..3b58abcc6 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/AttachmentAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/AttachmentAdapter.java @@ -1,11 +1,17 @@ package org.opencds.cqf.fhir.utility.adapter.dstu3; +import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.context.FhirVersionEnum; import org.hl7.fhir.dstu3.model.Attachment; import org.hl7.fhir.instance.model.api.ICompositeType; +import org.opencds.cqf.cql.engine.model.ModelResolver; +import org.opencds.cqf.fhir.utility.model.FhirModelResolverCache; class AttachmentAdapter implements org.opencds.cqf.fhir.utility.adapter.AttachmentAdapter { - private Attachment attachment; + private final Attachment attachment; + private final FhirContext fhirContext; + private final ModelResolver modelResolver; public AttachmentAdapter(ICompositeType attachment) { if (attachment == null) { @@ -21,6 +27,9 @@ public AttachmentAdapter(ICompositeType attachment) { } this.attachment = (Attachment) attachment; + fhirContext = FhirContext.forCached(FhirVersionEnum.R5); + modelResolver = FhirModelResolverCache.resolverForVersion( + fhirContext.getVersion().getVersion()); } protected Attachment getAttachment() { @@ -51,4 +60,14 @@ public byte[] getData() { public void setData(byte[] data) { this.getAttachment().setData(data); } + + @Override + public FhirContext fhirContext() { + return fhirContext; + } + + @Override + public ModelResolver getModelResolver() { + return modelResolver; + } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/KnowledgeArtifactAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/KnowledgeArtifactAdapter.java index 3f909c23e..fc7d566e0 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/KnowledgeArtifactAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/KnowledgeArtifactAdapter.java @@ -64,7 +64,10 @@ public void setEffectivePeriod(ICompositeType effectivePeriod) { @Override public List getDependencies() { - return new ArrayList<>(); + List references = new ArrayList<>(); + final String referenceSource = getReferenceSource(); + addProfileReferences(references, referenceSource); + return references; } @SuppressWarnings("unchecked") diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/LibraryAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/LibraryAdapter.java index 6b2f752bd..73989dcf0 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/LibraryAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/LibraryAdapter.java @@ -68,8 +68,8 @@ public Attachment addContent() { @Override public List getDependencies() { List references = new ArrayList(); - final String referenceSource = - hasVersion() ? getUrl() + "|" + getLibrary().getVersion() : getUrl(); + final String referenceSource = getReferenceSource(); + addProfileReferences(references, referenceSource); // relatedArtifact[].resource getRelatedArtifact().stream() diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ParametersParameterComponentAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ParametersParameterComponentAdapter.java index e02e5b097..0a2937f14 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ParametersParameterComponentAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ParametersParameterComponentAdapter.java @@ -1,5 +1,7 @@ package org.opencds.cqf.fhir.utility.adapter.dstu3; +import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.context.FhirVersionEnum; import java.util.List; import java.util.stream.Collectors; import org.hl7.fhir.dstu3.model.Parameters; @@ -8,13 +10,16 @@ import org.hl7.fhir.dstu3.model.Type; import org.hl7.fhir.instance.model.api.IBaseBackboneElement; import org.hl7.fhir.instance.model.api.IBaseDatatype; -import org.hl7.fhir.instance.model.api.IBaseExtension; import org.hl7.fhir.instance.model.api.IBaseResource; +import org.opencds.cqf.cql.engine.model.ModelResolver; +import org.opencds.cqf.fhir.utility.model.FhirModelResolverCache; class ParametersParameterComponentAdapter implements org.opencds.cqf.fhir.utility.adapter.ParametersParameterComponentAdapter { - private Parameters.ParametersParameterComponent parametersParametersComponent; + private final Parameters.ParametersParameterComponent parametersParametersComponent; + private final FhirContext fhirContext; + private final ModelResolver modelResolver; public ParametersParameterComponentAdapter(IBaseBackboneElement parametersParametersComponent) { if (parametersParametersComponent == null) { @@ -27,6 +32,9 @@ public ParametersParameterComponentAdapter(IBaseBackboneElement parametersParame } this.parametersParametersComponent = (ParametersParameterComponent) parametersParametersComponent; + fhirContext = FhirContext.forCached(FhirVersionEnum.R5); + modelResolver = FhirModelResolverCache.resolverForVersion( + fhirContext.getVersion().getVersion()); } @Override @@ -102,13 +110,12 @@ public IBaseDatatype getValue() { } @Override - public Boolean hasExtension() { - return this.parametersParametersComponent.hasExtension(); + public FhirContext fhirContext() { + return fhirContext; } @Override - @SuppressWarnings("unchecked") - public List> getExtension() { - return (List>) (List) this.parametersParametersComponent.getExtension(); + public ModelResolver getModelResolver() { + return modelResolver; } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/PlanDefinitionAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/PlanDefinitionAdapter.java index eac86d410..0365ee018 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/PlanDefinitionAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/PlanDefinitionAdapter.java @@ -43,9 +43,9 @@ public PlanDefinition copy() { @Override public List getDependencies() { List references = new ArrayList<>(); - final String referenceSource = hasVersion() - ? getPlanDefinition().getUrl() + "|" + getPlanDefinition().getVersion() - : getPlanDefinition().getUrl(); + final String referenceSource = getReferenceSource(); + addProfileReferences(references, referenceSource); + /* https://build.fhir.org/ig/HL7/crmi-ig/distribution.html#package-and-data-requirements relatedArtifact[].resource diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/QuestionnaireAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/QuestionnaireAdapter.java index 2b48f5990..2721676de 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/QuestionnaireAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/QuestionnaireAdapter.java @@ -1,8 +1,11 @@ package org.opencds.cqf.fhir.utility.adapter.dstu3; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import org.hl7.fhir.dstu3.model.Questionnaire; +import org.hl7.fhir.dstu3.model.Questionnaire.QuestionnaireItemComponent; +import org.hl7.fhir.dstu3.model.Reference; import org.hl7.fhir.dstu3.model.UriType; import org.hl7.fhir.instance.model.api.IDomainResource; import org.opencds.cqf.fhir.utility.Constants; @@ -41,11 +44,14 @@ public Questionnaire copy() { @Override public List getDependencies() { List references = new ArrayList<>(); - final String referenceSource = getQuestionnaire().hasVersion() - ? getQuestionnaire().getUrl() + "|" + getQuestionnaire().getVersion() - : getQuestionnaire().getUrl(); + final String referenceSource = getReferenceSource(); + addProfileReferences(references, referenceSource); + /* derivedFrom + extension[cqf-library] + extension[launchContext] + extension[variable].reference item[]..definition // NOTE: This is not a simple canonical, it will have a fragment to identify the specific element item[]..answerValueSet item[]..extension[itemMedia] @@ -54,9 +60,6 @@ public List getDependencies() { item[]..extension[referenceProfile] item[]..extension[candidateExpression].reference item[]..extension[lookupQuestionnaire] - extension[cqf-library] - extension[launchContext] - extension[variable].reference item[]..extension[variable].reference item[]..extension[initialExpression].reference item[]..extension[calculatedExpression].reference @@ -65,16 +68,74 @@ public List getDependencies() { item[]..extension[sdc-questionnaire-subQuestionnaire] */ - var libraryExtensions = getQuestionnaire().getExtensionsByUrl(Constants.CQF_LIBRARY); - for (var libraryExt : libraryExtensions) { - DependencyInfo dependency = new DependencyInfo( - referenceSource, - ((UriType) libraryExt.getValue()).asStringValue(), - libraryExt.getExtension(), - (reference) -> libraryExt.setValue(new UriType(reference))); - references.add(dependency); - } + // Not looking at launchContext as it references only base spec profiles and these are included implicitly as + // dependencies per the CRMI IG + + getQuestionnaire() + .getExtensionsByUrl(Constants.CQIF_LIBRARY) + .forEach(libraryExt -> references.add(new DependencyInfo( + referenceSource, + ((Reference) libraryExt.getValue()).getReference(), + libraryExt.getExtension(), + (reference) -> libraryExt.setValue(new Reference(reference))))); + + // Expression type does not exist in Stu3. + // var variableExtensions = getQuestionnaire().getExtensionsByUrl(Constants.VARIABLE_EXTENSION); + // for (var variableExt : variableExtensions) { + // var expression = CqfExpression.of(variableExt, null); + // if (!StringUtils.isEmpty(expression.getLibraryUrl())) { + // var dependency = new DependencyInfo( + // referenceSource, expression.getLibraryUrl(), variableExt.getExtension(), (reference) -> {}); + // references.add(dependency); + // } + // } + + getQuestionnaire().getItem().forEach(item -> getDependenciesOfItem(item, references, referenceSource)); return references; } + + private void getDependenciesOfItem( + QuestionnaireItemComponent item, List references, String referenceSource) { + if (item.hasDefinition()) { + var definition = item.getDefinition().split("#")[0]; + // Not passing an updateReferenceConsumer here because the reference is not a simple canonical + references.add(new DependencyInfo(referenceSource, definition, item.getExtension(), null)); + } + if (item.hasOptions()) { + references.add(new DependencyInfo( + referenceSource, + item.getOptions().getReference(), + item.getExtension(), + (reference) -> item.setOptions(new Reference(reference)))); + } + var referenceExtensions = Arrays.asList( + Constants.QUESTIONNAIRE_UNIT_VALUE_SET, + Constants.QUESTIONNAIRE_REFERENCE_PROFILE, + Constants.SDC_QUESTIONNAIRE_LOOKUP_QUESTIONNAIRE, + Constants.SDC_QUESTIONNAIRE_SUB_QUESTIONNAIRE); + item.getExtension().stream() + .filter(e -> referenceExtensions.contains(e.getUrl())) + .forEach(referenceExt -> references.add(new DependencyInfo( + referenceSource, + ((UriType) referenceExt.getValue()).asStringValue(), + referenceExt.getExtension(), + (reference) -> referenceExt.setValue(new UriType(reference))))); + // var expressionExtensions = Arrays.asList( + // Constants.VARIABLE_EXTENSION, + // Constants.SDC_QUESTIONNAIRE_CANDIDATE_EXPRESSION, + // Constants.SDC_QUESTIONNAIRE_INITIAL_EXPRESSION, + // Constants.SDC_QUESTIONNAIRE_CALCULATED_EXPRESSION, + // Constants.CQF_EXPRESSION); + // item.getExtension().stream() + // .filter(e -> expressionExtensions.contains(e.getUrl())) + // .map(e -> (Expression) e.getValue()) + // .filter(e -> e.hasReference()) + // .forEach(expression -> references.add(new DependencyInfo( + // referenceSource, + // expression.getReference(), + // expression.getExtension(), + // (reference) -> expression.setReference(reference)))); + item.getItem().forEach(childItem -> getDependenciesOfItem(childItem, references, referenceSource)); + } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/StructureDefinitionAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/StructureDefinitionAdapter.java index ce9c0b9f1..7659af4bb 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/StructureDefinitionAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/StructureDefinitionAdapter.java @@ -2,10 +2,10 @@ import java.util.ArrayList; import java.util.List; -import org.hl7.fhir.dstu3.model.StringType; +import org.hl7.fhir.dstu3.model.ElementDefinition; import org.hl7.fhir.dstu3.model.StructureDefinition; +import org.hl7.fhir.dstu3.model.UriType; import org.hl7.fhir.instance.model.api.IDomainResource; -import org.opencds.cqf.fhir.utility.Constants; import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; @@ -30,10 +30,9 @@ protected StructureDefinition getStructureDefinition() { @Override public List getDependencies() { List references = new ArrayList<>(); - final String referenceSource = getStructureDefinition().hasVersion() - ? getStructureDefinition().getUrl() + "|" - + getStructureDefinition().getVersion() - : getStructureDefinition().getUrl(); + final String referenceSource = getReferenceSource(); + addProfileReferences(references, referenceSource); + /* extension[].url modifierExtension[].url @@ -49,19 +48,48 @@ public List getDependencies() { extension[cpg-featureExpression].reference */ - var libraryExtensions = getStructureDefinition().getExtensionsByUrl(Constants.CQF_LIBRARY); - for (var libraryExt : libraryExtensions) { - DependencyInfo dependency = new DependencyInfo( + if (get().hasBaseDefinition()) { + references.add(new DependencyInfo( referenceSource, - ((StringType) libraryExt.getValue()).asStringValue(), - libraryExt.getExtension(), - (reference) -> libraryExt.setValue(new StringType(reference))); - references.add(dependency); + get().getBaseDefinition(), + get().getBaseDefinitionElement().getExtension(), + (reference) -> get().setBaseDefinition(reference))); } + get().getDifferential() + .getElement() + .forEach(element -> getDependenciesOfDifferential(element, references, referenceSource)); + return references; } + private void getDependenciesOfDifferential( + ElementDefinition element, List references, String referenceSource) { + element.getType().forEach(type -> { + if (type.hasProfile()) { + references.add(new DependencyInfo( + referenceSource, + type.getProfile(), + type.getProfileElement().getExtension(), + (reference) -> type.setProfile(reference))); + } + if (type.hasTargetProfile()) { + references.add(new DependencyInfo( + referenceSource, + type.getTargetProfile(), + type.getTargetProfileElement().getExtension(), + (reference) -> type.setTargetProfile(reference))); + } + }); + if (element.getBinding().hasValueSet()) { + references.add(new DependencyInfo( + referenceSource, + element.getBinding().getValueSet().primitiveValue(), + element.getBinding().getExtension(), + (reference) -> element.getBinding().setValueSet(new UriType(reference)))); + } + } + @Override public StructureDefinition get() { return getStructureDefinition(); diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ValueSetAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ValueSetAdapter.java index 333db035d..ce09616ee 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ValueSetAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ValueSetAdapter.java @@ -48,9 +48,8 @@ public ValueSet copy() { @Override public List getDependencies() { List references = new ArrayList<>(); - final String referenceSource = getValueSet().hasVersion() - ? getValueSet().getUrl() + "|" + getValueSet().getVersion() - : getValueSet().getUrl(); + final String referenceSource = getReferenceSource(); + addProfileReferences(references, referenceSource); /* compose.include[].valueSet diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/AttachmentAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/AttachmentAdapter.java index 181ae7812..e22733f55 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/AttachmentAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/AttachmentAdapter.java @@ -1,11 +1,16 @@ package org.opencds.cqf.fhir.utility.adapter.r4; +import ca.uhn.fhir.context.FhirContext; import org.hl7.fhir.instance.model.api.ICompositeType; import org.hl7.fhir.r4.model.Attachment; +import org.opencds.cqf.cql.engine.model.ModelResolver; +import org.opencds.cqf.fhir.utility.model.FhirModelResolverCache; class AttachmentAdapter implements org.opencds.cqf.fhir.utility.adapter.AttachmentAdapter { - private Attachment attachment; + private final Attachment attachment; + private final FhirContext fhirContext; + private final ModelResolver modelResolver; public AttachmentAdapter(ICompositeType attachment) { if (attachment == null) { @@ -21,6 +26,9 @@ public AttachmentAdapter(ICompositeType attachment) { } this.attachment = (Attachment) attachment; + fhirContext = FhirContext.forR5Cached(); + modelResolver = FhirModelResolverCache.resolverForVersion( + fhirContext.getVersion().getVersion()); } protected Attachment getAttachment() { @@ -51,4 +59,14 @@ public byte[] getData() { public void setData(byte[] data) { this.getAttachment().setData(data); } + + @Override + public FhirContext fhirContext() { + return fhirContext; + } + + @Override + public ModelResolver getModelResolver() { + return modelResolver; + } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/KnowledgeArtifactAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/KnowledgeArtifactAdapter.java index 74901c195..4fec9422b 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/KnowledgeArtifactAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/KnowledgeArtifactAdapter.java @@ -64,7 +64,10 @@ public void setEffectivePeriod(ICompositeType effectivePeriod) { @Override public List getDependencies() { - return new ArrayList<>(); + List references = new ArrayList<>(); + final String referenceSource = getReferenceSource(); + addProfileReferences(references, referenceSource); + return references; } @SuppressWarnings("unchecked") diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/LibraryAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/LibraryAdapter.java index 8189f9158..a89feaccb 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/LibraryAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/LibraryAdapter.java @@ -68,8 +68,8 @@ public Attachment addContent() { @Override public List getDependencies() { List references = new ArrayList(); - final String referenceSource = - hasVersion() ? getUrl() + "|" + getLibrary().getVersion() : getUrl(); + final String referenceSource = getReferenceSource(); + addProfileReferences(references, referenceSource); // relatedArtifact[].resource getRelatedArtifact().stream() diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/ParametersParameterComponentAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/ParametersParameterComponentAdapter.java index c15159396..b137fbe17 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/ParametersParameterComponentAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/ParametersParameterComponentAdapter.java @@ -1,20 +1,25 @@ package org.opencds.cqf.fhir.utility.adapter.r4; +import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.context.FhirVersionEnum; import java.util.List; import java.util.stream.Collectors; import org.hl7.fhir.instance.model.api.IBaseBackboneElement; import org.hl7.fhir.instance.model.api.IBaseDatatype; -import org.hl7.fhir.instance.model.api.IBaseExtension; import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.r4.model.Parameters; import org.hl7.fhir.r4.model.Parameters.ParametersParameterComponent; import org.hl7.fhir.r4.model.Resource; import org.hl7.fhir.r4.model.Type; +import org.opencds.cqf.cql.engine.model.ModelResolver; +import org.opencds.cqf.fhir.utility.model.FhirModelResolverCache; class ParametersParameterComponentAdapter implements org.opencds.cqf.fhir.utility.adapter.ParametersParameterComponentAdapter { - private Parameters.ParametersParameterComponent parametersParametersComponent; + private final Parameters.ParametersParameterComponent parametersParametersComponent; + private final FhirContext fhirContext; + private final ModelResolver modelResolver; protected Parameters.ParametersParameterComponent getParametersParameterComponent() { return this.parametersParametersComponent; @@ -31,6 +36,9 @@ public ParametersParameterComponentAdapter(IBaseBackboneElement parametersParame } this.parametersParametersComponent = (ParametersParameterComponent) parametersParametersComponent; + fhirContext = FhirContext.forCached(FhirVersionEnum.R5); + modelResolver = FhirModelResolverCache.resolverForVersion( + fhirContext.getVersion().getVersion()); } @Override @@ -107,13 +115,12 @@ public IBaseDatatype getValue() { } @Override - public Boolean hasExtension() { - return this.parametersParametersComponent.hasExtension(); + public FhirContext fhirContext() { + return fhirContext; } @Override - @SuppressWarnings("unchecked") - public List> getExtension() { - return (List>) (List) this.parametersParametersComponent.getExtension(); + public ModelResolver getModelResolver() { + return modelResolver; } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/PlanDefinitionAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/PlanDefinitionAdapter.java index 1f50dfe8a..3a1230539 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/PlanDefinitionAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/PlanDefinitionAdapter.java @@ -41,9 +41,9 @@ public PlanDefinition copy() { @Override public List getDependencies() { List references = new ArrayList<>(); - final String referenceSource = getPlanDefinition().hasVersion() - ? getPlanDefinition().getUrl() + "|" + getPlanDefinition().getVersion() - : getPlanDefinition().getUrl(); + final String referenceSource = getReferenceSource(); + addProfileReferences(references, referenceSource); + /* relatedArtifact[].resource library[] @@ -73,6 +73,7 @@ public List getDependencies() { } // action[] getPlanDefinition().getAction().forEach(action -> getDependenciesOfAction(action, references, referenceSource)); + // extension[cpg-partOf] getPlanDefinition().getExtension().stream() .filter(ext -> ext.getUrl().contains("cpg-partOf")) .filter(ext -> ext.hasValue()) diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/QuestionnaireAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/QuestionnaireAdapter.java index 3818af5f5..b701b6e8a 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/QuestionnaireAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/QuestionnaireAdapter.java @@ -1,10 +1,13 @@ package org.opencds.cqf.fhir.utility.adapter.r4; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import org.hl7.fhir.instance.model.api.IDomainResource; import org.hl7.fhir.r4.model.CanonicalType; +import org.hl7.fhir.r4.model.Expression; import org.hl7.fhir.r4.model.Questionnaire; +import org.hl7.fhir.r4.model.Questionnaire.QuestionnaireItemComponent; import org.opencds.cqf.fhir.utility.Constants; import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; @@ -40,11 +43,14 @@ public Questionnaire copy() { @Override public List getDependencies() { List references = new ArrayList<>(); - final String referenceSource = getQuestionnaire().hasVersion() - ? getQuestionnaire().getUrl() + "|" + getQuestionnaire().getVersion() - : getQuestionnaire().getUrl(); + final String referenceSource = getReferenceSource(); + addProfileReferences(references, referenceSource); + /* derivedFrom + extension[cqf-library] + extension[launchContext] + extension[variable].reference item[]..definition // NOTE: This is not a simple canonical, it will have a fragment to identify the specific element item[]..answerValueSet item[]..extension[itemMedia] @@ -53,9 +59,6 @@ public List getDependencies() { item[]..extension[referenceProfile] item[]..extension[candidateExpression].reference item[]..extension[lookupQuestionnaire] - extension[cqf-library] - extension[launchContext] - extension[variable].reference item[]..extension[variable].reference item[]..extension[initialExpression].reference item[]..extension[calculatedExpression].reference @@ -64,16 +67,80 @@ public List getDependencies() { item[]..extension[sdc-questionnaire-subQuestionnaire] */ - var libraryExtensions = getQuestionnaire().getExtensionsByUrl(Constants.CQF_LIBRARY); - for (var libraryExt : libraryExtensions) { - DependencyInfo dependency = new DependencyInfo( - referenceSource, - ((CanonicalType) libraryExt.getValue()).asStringValue(), - libraryExt.getExtension(), - (reference) -> libraryExt.setValue(new CanonicalType(reference))); - references.add(dependency); - } + // Not looking at launchContext as it references only base spec profiles and these are included implicitly as + // dependencies per the CRMI IG + + getQuestionnaire() + .getDerivedFrom() + .forEach(derivedRef -> references.add(new DependencyInfo( + referenceSource, + derivedRef.asStringValue(), + derivedRef.getExtension(), + (reference) -> derivedRef.setValue(reference)))); + + getQuestionnaire() + .getExtensionsByUrl(Constants.CQF_LIBRARY) + .forEach(libraryExt -> references.add(new DependencyInfo( + referenceSource, + ((CanonicalType) libraryExt.getValue()).asStringValue(), + libraryExt.getExtension(), + (reference) -> libraryExt.setValue(new CanonicalType(reference))))); + + getQuestionnaire().getExtensionsByUrl(Constants.VARIABLE_EXTENSION).stream() + .map(e -> (Expression) e.getValue()) + .filter(e -> e.hasReference()) + .forEach(expression -> references.add(new DependencyInfo( + referenceSource, + expression.getReference(), + expression.getExtension(), + (reference) -> expression.setReference(reference)))); + + getQuestionnaire().getItem().forEach(item -> getDependenciesOfItem(item, references, referenceSource)); return references; } + + private void getDependenciesOfItem( + QuestionnaireItemComponent item, List references, String referenceSource) { + if (item.hasDefinition()) { + var definition = item.getDefinition().split("#")[0]; + // Not passing an updateReferenceConsumer here because the reference is not a simple canonical + references.add(new DependencyInfo(referenceSource, definition, item.getExtension(), null)); + } + if (item.hasAnswerValueSet()) { + references.add(new DependencyInfo( + referenceSource, + item.getAnswerValueSet(), + item.getExtension(), + (reference) -> item.setAnswerValueSet(reference))); + } + var referenceExtensions = Arrays.asList( + Constants.QUESTIONNAIRE_UNIT_VALUE_SET, + Constants.QUESTIONNAIRE_REFERENCE_PROFILE, + Constants.SDC_QUESTIONNAIRE_LOOKUP_QUESTIONNAIRE, + Constants.SDC_QUESTIONNAIRE_SUB_QUESTIONNAIRE); + item.getExtension().stream() + .filter(e -> referenceExtensions.contains(e.getUrl())) + .forEach(referenceExt -> references.add(new DependencyInfo( + referenceSource, + ((CanonicalType) referenceExt.getValue()).asStringValue(), + referenceExt.getExtension(), + (reference) -> referenceExt.setValue(new CanonicalType(reference))))); + var expressionExtensions = Arrays.asList( + Constants.VARIABLE_EXTENSION, + Constants.SDC_QUESTIONNAIRE_CANDIDATE_EXPRESSION, + Constants.SDC_QUESTIONNAIRE_INITIAL_EXPRESSION, + Constants.SDC_QUESTIONNAIRE_CALCULATED_EXPRESSION, + Constants.CQF_EXPRESSION); + item.getExtension().stream() + .filter(e -> expressionExtensions.contains(e.getUrl())) + .map(e -> (Expression) e.getValue()) + .filter(e -> e.hasReference()) + .forEach(expression -> references.add(new DependencyInfo( + referenceSource, + expression.getReference(), + expression.getExtension(), + (reference) -> expression.setReference(reference)))); + item.getItem().forEach(childItem -> getDependenciesOfItem(childItem, references, referenceSource)); + } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/StructureDefinitionAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/StructureDefinitionAdapter.java index 6a18bd2cc..8720bef5c 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/StructureDefinitionAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/StructureDefinitionAdapter.java @@ -3,7 +3,8 @@ import java.util.ArrayList; import java.util.List; import org.hl7.fhir.instance.model.api.IDomainResource; -import org.hl7.fhir.r4.model.CanonicalType; +import org.hl7.fhir.r4.model.ElementDefinition; +import org.hl7.fhir.r4.model.Expression; import org.hl7.fhir.r4.model.StructureDefinition; import org.opencds.cqf.fhir.utility.Constants; import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; @@ -30,10 +31,9 @@ protected StructureDefinition getStructureDefinition() { @Override public List getDependencies() { List references = new ArrayList<>(); - final String referenceSource = getStructureDefinition().hasVersion() - ? getStructureDefinition().getUrl() + "|" - + getStructureDefinition().getVersion() - : getStructureDefinition().getUrl(); + final String referenceSource = getReferenceSource(); + addProfileReferences(references, referenceSource); + /* extension[].url modifierExtension[].url @@ -49,19 +49,72 @@ public List getDependencies() { extension[cpg-featureExpression].reference */ - var libraryExtensions = getStructureDefinition().getExtensionsByUrl(Constants.CQF_LIBRARY); - for (var libraryExt : libraryExtensions) { - DependencyInfo dependency = new DependencyInfo( + if (get().hasBaseDefinition()) { + references.add(new DependencyInfo( referenceSource, - ((CanonicalType) libraryExt.getValue()).asStringValue(), - libraryExt.getExtension(), - (reference) -> libraryExt.setValue(new CanonicalType(reference))); - references.add(dependency); + get().getBaseDefinition(), + get().getBaseDefinitionElement().getExtension(), + (reference) -> get().setBaseDefinition(reference))); } + get().getExtensionsByUrl(Constants.CPG_ASSERTION_EXPRESSION).stream() + .filter(e -> e.getValue() instanceof Expression) + .map(e -> (Expression) e.getValue()) + .filter(e -> e.hasReference()) + .forEach(expression -> references.add(new DependencyInfo( + referenceSource, + expression.getReference(), + expression.getExtension(), + (reference) -> expression.setReference(reference)))); + get().getExtensionsByUrl(Constants.CPG_FEATURE_EXPRESSION).stream() + .filter(e -> e.getValue() instanceof Expression) + .map(e -> (Expression) e.getValue()) + .filter(e -> e.hasReference()) + .forEach(expression -> references.add(new DependencyInfo( + referenceSource, + expression.getReference(), + expression.getExtension(), + (reference) -> expression.setReference(reference)))); + get().getExtensionsByUrl(Constants.CPG_INFERENCE_EXPRESSION).stream() + .filter(e -> e.getValue() instanceof Expression) + .map(e -> (Expression) e.getValue()) + .filter(e -> e.hasReference()) + .forEach(expression -> references.add(new DependencyInfo( + referenceSource, + expression.getReference(), + expression.getExtension(), + (reference) -> expression.setReference(reference)))); + get().getDifferential() + .getElement() + .forEach(element -> getDependenciesOfDifferential(element, references, referenceSource)); return references; } + private void getDependenciesOfDifferential( + ElementDefinition element, List references, String referenceSource) { + element.getType().forEach(type -> { + type.getProfile() + .forEach(profile -> references.add(new DependencyInfo( + referenceSource, + profile.getValueAsString(), + profile.getExtension(), + (reference) -> profile.setValue(reference)))); + type.getTargetProfile() + .forEach(profile -> references.add(new DependencyInfo( + referenceSource, + profile.getValueAsString(), + profile.getExtension(), + (reference) -> profile.setValue(reference)))); + }); + if (element.getBinding().hasValueSet()) { + references.add(new DependencyInfo( + referenceSource, + element.getBinding().getValueSet(), + element.getBinding().getExtension(), + (reference) -> element.getBinding().setValueSet(reference))); + } + } + @Override public StructureDefinition get() { return getStructureDefinition(); diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/ValueSetAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/ValueSetAdapter.java index 39af1c92c..6165183be 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/ValueSetAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/ValueSetAdapter.java @@ -49,9 +49,8 @@ public ValueSet copy() { @Override public List getDependencies() { List references = new ArrayList<>(); - final String referenceSource = getValueSet().hasVersion() - ? getValueSet().getUrl() + "|" + getValueSet().getVersion() - : getValueSet().getUrl(); + final String referenceSource = getReferenceSource(); + addProfileReferences(references, referenceSource); /* compose.include[].valueSet diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/AttachmentAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/AttachmentAdapter.java index 1d88f4254..266e33692 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/AttachmentAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/AttachmentAdapter.java @@ -1,11 +1,16 @@ package org.opencds.cqf.fhir.utility.adapter.r5; +import ca.uhn.fhir.context.FhirContext; import org.hl7.fhir.instance.model.api.ICompositeType; import org.hl7.fhir.r5.model.Attachment; +import org.opencds.cqf.cql.engine.model.ModelResolver; +import org.opencds.cqf.fhir.utility.model.FhirModelResolverCache; class AttachmentAdapter implements org.opencds.cqf.fhir.utility.adapter.AttachmentAdapter { - private Attachment attachment; + private final Attachment attachment; + private final FhirContext fhirContext; + private final ModelResolver modelResolver; public AttachmentAdapter(ICompositeType attachment) { if (attachment == null) { @@ -21,6 +26,9 @@ public AttachmentAdapter(ICompositeType attachment) { } this.attachment = (Attachment) attachment; + fhirContext = FhirContext.forR5Cached(); + modelResolver = FhirModelResolverCache.resolverForVersion( + fhirContext.getVersion().getVersion()); } protected Attachment getAttachment() { @@ -51,4 +59,14 @@ public byte[] getData() { public void setData(byte[] data) { this.getAttachment().setData(data); } + + @Override + public FhirContext fhirContext() { + return fhirContext; + } + + @Override + public ModelResolver getModelResolver() { + return modelResolver; + } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/KnowledgeArtifactAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/KnowledgeArtifactAdapter.java index c7c3912d8..bc6b98cd8 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/KnowledgeArtifactAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/KnowledgeArtifactAdapter.java @@ -64,7 +64,10 @@ public void setEffectivePeriod(ICompositeType effectivePeriod) { @Override public List getDependencies() { - return new ArrayList<>(); + List references = new ArrayList<>(); + final String referenceSource = getReferenceSource(); + addProfileReferences(references, referenceSource); + return references; } @SuppressWarnings("unchecked") diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/LibraryAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/LibraryAdapter.java index c741dbcb4..e90371e6e 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/LibraryAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/LibraryAdapter.java @@ -68,8 +68,8 @@ public Attachment addContent() { @Override public List getDependencies() { List references = new ArrayList(); - final String referenceSource = - hasVersion() ? getUrl() + "|" + getLibrary().getVersion() : getUrl(); + final String referenceSource = getReferenceSource(); + addProfileReferences(references, referenceSource); // relatedArtifact[].resource getRelatedArtifact().stream() diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/ParametersParameterComponentAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/ParametersParameterComponentAdapter.java index 5f843651c..ba47dfc62 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/ParametersParameterComponentAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/ParametersParameterComponentAdapter.java @@ -1,20 +1,25 @@ package org.opencds.cqf.fhir.utility.adapter.r5; +import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.context.FhirVersionEnum; import java.util.List; import java.util.stream.Collectors; import org.hl7.fhir.instance.model.api.IBaseBackboneElement; import org.hl7.fhir.instance.model.api.IBaseDatatype; -import org.hl7.fhir.instance.model.api.IBaseExtension; import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.r5.model.DataType; import org.hl7.fhir.r5.model.Parameters; import org.hl7.fhir.r5.model.Parameters.ParametersParameterComponent; import org.hl7.fhir.r5.model.Resource; +import org.opencds.cqf.cql.engine.model.ModelResolver; +import org.opencds.cqf.fhir.utility.model.FhirModelResolverCache; class ParametersParameterComponentAdapter implements org.opencds.cqf.fhir.utility.adapter.ParametersParameterComponentAdapter { - private Parameters.ParametersParameterComponent parametersParametersComponent; + private final Parameters.ParametersParameterComponent parametersParametersComponent; + private final FhirContext fhirContext; + private final ModelResolver modelResolver; protected Parameters.ParametersParameterComponent getParametersParameterComponent() { return this.parametersParametersComponent; @@ -31,6 +36,9 @@ public ParametersParameterComponentAdapter(IBaseBackboneElement parametersParame } this.parametersParametersComponent = (ParametersParameterComponent) parametersParametersComponent; + fhirContext = FhirContext.forCached(FhirVersionEnum.R5); + modelResolver = FhirModelResolverCache.resolverForVersion( + fhirContext.getVersion().getVersion()); } @Override @@ -107,13 +115,12 @@ public IBaseDatatype getValue() { } @Override - public Boolean hasExtension() { - return this.parametersParametersComponent.hasExtension(); + public FhirContext fhirContext() { + return fhirContext; } @Override - @SuppressWarnings("unchecked") - public List> getExtension() { - return (List>) (List) this.parametersParametersComponent.getExtension(); + public ModelResolver getModelResolver() { + return modelResolver; } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/PlanDefinitionAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/PlanDefinitionAdapter.java index 1b95f5f91..0927d5ddb 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/PlanDefinitionAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/PlanDefinitionAdapter.java @@ -41,9 +41,9 @@ public PlanDefinition copy() { @Override public List getDependencies() { List references = new ArrayList<>(); - final String referenceSource = getPlanDefinition().hasVersion() - ? getPlanDefinition().getUrl() + "|" + getPlanDefinition().getVersion() - : getPlanDefinition().getUrl(); + final String referenceSource = getReferenceSource(); + addProfileReferences(references, referenceSource); + /* relatedArtifact[].resource library[] diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapter.java index b4b91c522..06669484c 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapter.java @@ -1,10 +1,13 @@ package org.opencds.cqf.fhir.utility.adapter.r5; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import org.hl7.fhir.instance.model.api.IDomainResource; import org.hl7.fhir.r5.model.CanonicalType; +import org.hl7.fhir.r5.model.Expression; import org.hl7.fhir.r5.model.Questionnaire; +import org.hl7.fhir.r5.model.Questionnaire.QuestionnaireItemComponent; import org.opencds.cqf.fhir.utility.Constants; import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; @@ -40,11 +43,14 @@ public Questionnaire copy() { @Override public List getDependencies() { List references = new ArrayList<>(); - final String referenceSource = getQuestionnaire().hasVersion() - ? getQuestionnaire().getUrl() + "|" + getQuestionnaire().getVersion() - : getQuestionnaire().getUrl(); + final String referenceSource = getReferenceSource(); + addProfileReferences(references, referenceSource); + /* derivedFrom + extension[cqf-library] + extension[launchContext] + extension[variable].reference item[]..definition // NOTE: This is not a simple canonical, it will have a fragment to identify the specific element item[]..answerValueSet item[]..extension[itemMedia] @@ -53,9 +59,6 @@ public List getDependencies() { item[]..extension[referenceProfile] item[]..extension[candidateExpression].reference item[]..extension[lookupQuestionnaire] - extension[cqf-library] - extension[launchContext] - extension[variable].reference item[]..extension[variable].reference item[]..extension[initialExpression].reference item[]..extension[calculatedExpression].reference @@ -64,16 +67,80 @@ public List getDependencies() { item[]..extension[sdc-questionnaire-subQuestionnaire] */ - var libraryExtensions = getQuestionnaire().getExtensionsByUrl(Constants.CQF_LIBRARY); - for (var libraryExt : libraryExtensions) { - DependencyInfo dependency = new DependencyInfo( - referenceSource, - ((CanonicalType) libraryExt.getValue()).asStringValue(), - libraryExt.getExtension(), - (reference) -> libraryExt.setValue(new CanonicalType(reference))); - references.add(dependency); - } + // Not looking at launchContext as it references only base spec profiles and these are included implicitly as + // dependencies per the CRMI IG + + getQuestionnaire() + .getDerivedFrom() + .forEach(derivedRef -> references.add(new DependencyInfo( + referenceSource, + derivedRef.asStringValue(), + derivedRef.getExtension(), + (reference) -> derivedRef.setValue(reference)))); + + getQuestionnaire() + .getExtensionsByUrl(Constants.CQF_LIBRARY) + .forEach(libraryExt -> references.add(new DependencyInfo( + referenceSource, + ((CanonicalType) libraryExt.getValue()).asStringValue(), + libraryExt.getExtension(), + (reference) -> libraryExt.setValue(new CanonicalType(reference))))); + + getQuestionnaire().getExtensionsByUrl(Constants.VARIABLE_EXTENSION).stream() + .map(e -> (Expression) e.getValue()) + .filter(e -> e.hasReference()) + .forEach(expression -> references.add(new DependencyInfo( + referenceSource, + expression.getReference(), + expression.getExtension(), + (reference) -> expression.setReference(reference)))); + + getQuestionnaire().getItem().forEach(item -> getDependenciesOfItem(item, references, referenceSource)); return references; } + + private void getDependenciesOfItem( + QuestionnaireItemComponent item, List references, String referenceSource) { + if (item.hasDefinition()) { + var definition = item.getDefinition().split("#")[0]; + // Not passing an updateReferenceConsumer here because the reference is not a simple canonical + references.add(new DependencyInfo(referenceSource, definition, item.getExtension(), null)); + } + if (item.hasAnswerValueSet()) { + references.add(new DependencyInfo( + referenceSource, + item.getAnswerValueSet(), + item.getExtension(), + (reference) -> item.setAnswerValueSet(reference))); + } + var referenceExtensions = Arrays.asList( + Constants.QUESTIONNAIRE_UNIT_VALUE_SET, + Constants.QUESTIONNAIRE_REFERENCE_PROFILE, + Constants.SDC_QUESTIONNAIRE_LOOKUP_QUESTIONNAIRE, + Constants.SDC_QUESTIONNAIRE_SUB_QUESTIONNAIRE); + item.getExtension().stream() + .filter(e -> referenceExtensions.contains(e.getUrl())) + .forEach(referenceExt -> references.add(new DependencyInfo( + referenceSource, + ((CanonicalType) referenceExt.getValue()).asStringValue(), + referenceExt.getExtension(), + (reference) -> referenceExt.setValue(new CanonicalType(reference))))); + var expressionExtensions = Arrays.asList( + Constants.VARIABLE_EXTENSION, + Constants.SDC_QUESTIONNAIRE_CANDIDATE_EXPRESSION, + Constants.SDC_QUESTIONNAIRE_INITIAL_EXPRESSION, + Constants.SDC_QUESTIONNAIRE_CALCULATED_EXPRESSION, + Constants.CQF_EXPRESSION); + item.getExtension().stream() + .filter(e -> expressionExtensions.contains(e.getUrl())) + .map(e -> (Expression) e.getValue()) + .filter(e -> e.hasReference()) + .forEach(expression -> references.add(new DependencyInfo( + referenceSource, + expression.getReference(), + expression.getExtension(), + (reference) -> expression.setReference(reference)))); + item.getItem().forEach(childItem -> getDependenciesOfItem(childItem, references, referenceSource)); + } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapter.java index 9610a0e6f..85b8d94d0 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapter.java @@ -10,9 +10,10 @@ import org.hl7.fhir.instance.model.api.ICompositeType; import org.hl7.fhir.instance.model.api.IDomainResource; import org.hl7.fhir.instance.model.api.IPrimitiveType; -import org.hl7.fhir.r5.model.CanonicalType; import org.hl7.fhir.r5.model.DateTimeType; +import org.hl7.fhir.r5.model.ElementDefinition; import org.hl7.fhir.r5.model.Enumerations.PublicationStatus; +import org.hl7.fhir.r5.model.Expression; import org.hl7.fhir.r5.model.Period; import org.hl7.fhir.r5.model.RelatedArtifact; import org.hl7.fhir.r5.model.RelatedArtifact.RelatedArtifactType; @@ -43,10 +44,9 @@ protected StructureDefinition getStructureDefinition() { @Override public List getDependencies() { List references = new ArrayList<>(); - final String referenceSource = getStructureDefinition().hasVersion() - ? getStructureDefinition().getUrl() + "|" - + getStructureDefinition().getVersion() - : getStructureDefinition().getUrl(); + final String referenceSource = getReferenceSource(); + addProfileReferences(references, referenceSource); + /* extension[].url modifierExtension[].url @@ -62,19 +62,72 @@ public List getDependencies() { extension[cpg-featureExpression].reference */ - var libraryExtensions = getStructureDefinition().getExtensionsByUrl(Constants.CQF_LIBRARY); - for (var libraryExt : libraryExtensions) { - DependencyInfo dependency = new DependencyInfo( + if (get().hasBaseDefinition()) { + references.add(new DependencyInfo( referenceSource, - ((CanonicalType) libraryExt.getValue()).asStringValue(), - libraryExt.getExtension(), - (reference) -> libraryExt.setValue(new CanonicalType(reference))); - references.add(dependency); + get().getBaseDefinition(), + get().getBaseDefinitionElement().getExtension(), + (reference) -> get().setBaseDefinition(reference))); } + get().getExtensionsByUrl(Constants.CPG_ASSERTION_EXPRESSION).stream() + .filter(e -> e.getValue() instanceof Expression) + .map(e -> (Expression) e.getValue()) + .filter(e -> e.hasReference()) + .forEach(expression -> references.add(new DependencyInfo( + referenceSource, + expression.getReference(), + expression.getExtension(), + (reference) -> expression.setReference(reference)))); + get().getExtensionsByUrl(Constants.CPG_FEATURE_EXPRESSION).stream() + .filter(e -> e.getValue() instanceof Expression) + .map(e -> (Expression) e.getValue()) + .filter(e -> e.hasReference()) + .forEach(expression -> references.add(new DependencyInfo( + referenceSource, + expression.getReference(), + expression.getExtension(), + (reference) -> expression.setReference(reference)))); + get().getExtensionsByUrl(Constants.CPG_INFERENCE_EXPRESSION).stream() + .filter(e -> e.getValue() instanceof Expression) + .map(e -> (Expression) e.getValue()) + .filter(e -> e.hasReference()) + .forEach(expression -> references.add(new DependencyInfo( + referenceSource, + expression.getReference(), + expression.getExtension(), + (reference) -> expression.setReference(reference)))); + get().getDifferential() + .getElement() + .forEach(element -> getDependenciesOfDifferential(element, references, referenceSource)); return references; } + private void getDependenciesOfDifferential( + ElementDefinition element, List references, String referenceSource) { + element.getType().forEach(type -> { + type.getProfile() + .forEach(profile -> references.add(new DependencyInfo( + referenceSource, + profile.getValueAsString(), + profile.getExtension(), + (reference) -> profile.setValue(reference)))); + type.getTargetProfile() + .forEach(profile -> references.add(new DependencyInfo( + referenceSource, + profile.getValueAsString(), + profile.getExtension(), + (reference) -> profile.setValue(reference)))); + }); + if (element.getBinding().hasValueSet()) { + references.add(new DependencyInfo( + referenceSource, + element.getBinding().getValueSet(), + element.getBinding().getExtension(), + (reference) -> element.getBinding().setValueSet(reference))); + } + } + @Override public StructureDefinition get() { return getStructureDefinition(); diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/ValueSetAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/ValueSetAdapter.java index 7d3ca3134..035362e91 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/ValueSetAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/ValueSetAdapter.java @@ -48,9 +48,8 @@ public ValueSet copy() { @Override public List getDependencies() { List references = new ArrayList<>(); - final String referenceSource = getValueSet().hasVersion() - ? getValueSet().getUrl() + "|" + getValueSet().getVersion() - : getValueSet().getUrl(); + final String referenceSource = getReferenceSource(); + addProfileReferences(references, referenceSource); /* compose.include[].valueSet diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/CqfExpressionTests.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/CqfExpressionTests.java new file mode 100644 index 000000000..f586ca948 --- /dev/null +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/CqfExpressionTests.java @@ -0,0 +1,12 @@ +package org.opencds.cqf.fhir.utility; + +import org.hl7.fhir.r4.model.Extension; +import org.junit.jupiter.api.Test; + +class CqfExpressionTests { + @Test + void test() { + var ext = new Extension(); + var expression = CqfExpression.of(ext, "http://test.com/Library/test"); + } +} diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/LibraryAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/LibraryAdapterTest.java index c269bfa11..a64b179cf 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/LibraryAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/LibraryAdapterTest.java @@ -18,6 +18,7 @@ import java.util.Collections; import java.util.Date; import java.util.List; +import org.hl7.fhir.dstu3.model.Attachment; import org.hl7.fhir.dstu3.model.Bundle; import org.hl7.fhir.dstu3.model.CodeableConcept; import org.hl7.fhir.dstu3.model.Coding; @@ -26,6 +27,7 @@ import org.hl7.fhir.dstu3.model.Library; import org.hl7.fhir.dstu3.model.Period; import org.hl7.fhir.dstu3.model.PlanDefinition; +import org.hl7.fhir.dstu3.model.Reference; import org.hl7.fhir.dstu3.model.RelatedArtifact; import org.hl7.fhir.dstu3.model.UsageContext; import org.hl7.fhir.instance.model.api.IDomainResource; @@ -163,7 +165,35 @@ void adapter_copy() { } @Test - void adapter_get_all_dependencies() {} + void adapter_get_all_dependencies() { + var dependencies = List.of( + "profileRef", "relatedArtifactRef", "dataRequirementProfileRef", "dataRequirementCodeFilterRef"); + var library = new Library(); + library.getMeta().addProfile(dependencies.get(0)); + library.getRelatedArtifactFirstRep().setResource(new Reference(dependencies.get(1))); + library.addDataRequirement().addProfile(dependencies.get(2)); + library.addDataRequirement().addCodeFilter().setValueSet(new Reference(dependencies.get(3))); + var adapter = new LibraryAdapter(library); + var extractedDependencies = adapter.getDependencies(); + assertEquals(extractedDependencies.size(), dependencies.size()); + extractedDependencies.forEach(dep -> { + assertTrue(dependencies.indexOf(dep.getReference()) >= 0); + }); + } + + @Test + void adapter_get_and_set_content() { + var library = new Library(); + var adapter = new LibraryAdapter(library); + var contentList = new ArrayList(); + contentList.add(new Attachment().setContentType("text/cql").setData(new byte[10])); + adapter.setContent(contentList); + assertTrue(adapter.hasContent()); + assertEquals(contentList, adapter.getContent()); + adapter.addContent().setContentType("text/xml").setData(new byte[20]); + assertEquals(2, adapter.getContent().size()); + assertEquals("text/xml", adapter.getContent().get(1).getContentType()); + } @Test void adapter_get_and_set_type() { diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/PlanDefinitionAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/PlanDefinitionAdapterTest.java index 59df4f2c6..4b9b0c8f2 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/PlanDefinitionAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/PlanDefinitionAdapterTest.java @@ -163,6 +163,7 @@ void adapter_copy() { @Test void adapter_get_all_dependencies() { var dependencies = List.of( + "profileRef", "relatedArtifactRef", "libraryRef", "actionTriggerDataReqProfile", @@ -179,26 +180,27 @@ void adapter_get_all_dependencies() { "cpgPartOfExtRef", "nestedActionDefinitionReference"); var planDef = new PlanDefinition(); - planDef.getRelatedArtifactFirstRep().setResource(new Reference(dependencies.get(0))); - planDef.getLibraryFirstRep().setReference(dependencies.get(1)); + planDef.getMeta().addProfile(dependencies.get(0)); + planDef.getRelatedArtifactFirstRep().setResource(new Reference(dependencies.get(1))); + planDef.getLibraryFirstRep().setReference(dependencies.get(2)); var action = planDef.getActionFirstRep(); action.getTriggerDefinitionFirstRep() .getEventData() - .setProfile(List.of(new UriType(dependencies.get(2)))) + .setProfile(List.of(new UriType(dependencies.get(3)))) .getCodeFilterFirstRep() - .setValueSet(new StringType(dependencies.get(3))); + .setValueSet(new StringType(dependencies.get(4))); action.getInputFirstRep() - .setProfile(List.of(new UriType(dependencies.get(4)))) + .setProfile(List.of(new UriType(dependencies.get(5)))) .getCodeFilterFirstRep() - .setValueSet(new StringType(dependencies.get(5))); + .setValueSet(new StringType(dependencies.get(6))); action.getOutputFirstRep() - .setProfile(List.of(new UriType(dependencies.get(6)))) + .setProfile(List.of(new UriType(dependencies.get(7)))) .getCodeFilterFirstRep() - .setValueSet(new StringType(dependencies.get(7))); - action.getDefinition().setReference(dependencies.get(8)); + .setValueSet(new StringType(dependencies.get(8))); + action.getDefinition().setReference(dependencies.get(9)); planDef.addExtension(new Extension( - "http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-partOf", new UriType(dependencies.get(9)))); - action.addAction().getDefinition().setReference(dependencies.get(10)); + "http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-partOf", new UriType(dependencies.get(10)))); + action.addAction().getDefinition().setReference(dependencies.get(11)); var adapter = new PlanDefinitionAdapter(planDef); var extractedDependencies = adapter.getDependencies(); assertEquals(extractedDependencies.size(), dependencies.size()); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/QuestionnaireAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/QuestionnaireAdapterTest.java index 91f922a24..45a28f320 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/QuestionnaireAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/QuestionnaireAdapterTest.java @@ -20,9 +20,11 @@ import org.hl7.fhir.dstu3.model.Library; import org.hl7.fhir.dstu3.model.Period; import org.hl7.fhir.dstu3.model.Questionnaire; +import org.hl7.fhir.dstu3.model.Reference; import org.hl7.fhir.dstu3.model.RelatedArtifact; import org.hl7.fhir.instance.model.api.IDomainResource; import org.junit.jupiter.api.Test; +import org.opencds.cqf.fhir.utility.Constants; import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; public class QuestionnaireAdapterTest { @@ -154,5 +156,38 @@ void adapter_copy() { } @Test - void adapter_get_all_dependencies() {} + void adapter_get_all_dependencies() { + var dependencies = List.of( + "profileRef", + "cqfLibraryRef", + // "variableRef", + "itemDefinitionRef" + // "answerValueSetRef", + // "itemMediaRef", + // "itemAnswerMediaRef", + // "unitValueSetRef", + // "referenceProfileRef", + // "candidateExpressionRef", + // "lookupQuestionnaireRef", + // "itemVariableRef", + // "initialExpressionRef", + // "calculatedExpressionRef", + // "calculatedValueRef", + // "expressionRef", + // "subQuestionnaireRef" + ); + var questionnaire = new Questionnaire(); + questionnaire.getMeta().addProfile(dependencies.get(0)); + questionnaire.addExtension(Constants.CQIF_LIBRARY, new Reference(dependencies.get(1))); + // var variableExt = new Extension(Constants.VARIABLE_EXTENSION).setValue(new + // Expression().setReference(dependencies.get(2))); + // questionnaire.addExtension(variableExt); + questionnaire.addItem().setDefinition(dependencies.get(2) + "#Observation"); + var adapter = new QuestionnaireAdapter(questionnaire); + var extractedDependencies = adapter.getDependencies(); + assertEquals(dependencies.size(), extractedDependencies.size()); + extractedDependencies.forEach(dep -> { + assertTrue(dependencies.indexOf(dep.getReference()) >= 0); + }); + } } diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapterTest.java index b101719c4..e1ec9d224 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapterTest.java @@ -81,5 +81,7 @@ void adapter_get_contained() { resource.addContained(new Library()); var adapter = new ResourceAdapter(resource); assertTrue(adapter.hasContained()); + assertNotNull(adapter.getContained()); + assertFalse(adapter.hasContained(adapter.getContained().get(0))); } } diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/StructureDefinitionAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/StructureDefinitionAdapterTest.java index 124c09b22..3e27917b1 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/StructureDefinitionAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/StructureDefinitionAdapterTest.java @@ -16,11 +16,13 @@ import java.util.Date; import java.util.List; import org.hl7.fhir.dstu3.model.Bundle; +import org.hl7.fhir.dstu3.model.ElementDefinition.ElementDefinitionBindingComponent; import org.hl7.fhir.dstu3.model.Enumerations.PublicationStatus; import org.hl7.fhir.dstu3.model.Library; import org.hl7.fhir.dstu3.model.Period; import org.hl7.fhir.dstu3.model.RelatedArtifact; import org.hl7.fhir.dstu3.model.StructureDefinition; +import org.hl7.fhir.dstu3.model.UriType; import org.hl7.fhir.instance.model.api.IDomainResource; import org.junit.jupiter.api.Test; import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; @@ -155,5 +157,27 @@ void adapter_copy() { } @Test - void adapter_get_all_dependencies() {} + void adapter_get_all_dependencies() { + var dependencies = List.of( + "profileRef", + "baseDefinition", + "elementProfileRef", + "elementTargetProfileRef", + "elementValueSetBindingRef"); + var structureDef = new StructureDefinition(); + structureDef.getMeta().addProfile(dependencies.get(0)); + structureDef.setBaseDefinition(dependencies.get(1)); + structureDef.getDifferential().addElement().addType().setProfile(dependencies.get(2)); + structureDef.getDifferential().addElement().addType().setTargetProfile(dependencies.get(3)); + structureDef + .getDifferential() + .addElement() + .setBinding(new ElementDefinitionBindingComponent().setValueSet(new UriType(dependencies.get(4)))); + var adapter = new StructureDefinitionAdapter(structureDef); + var extractedDependencies = adapter.getDependencies(); + assertEquals(dependencies.size(), extractedDependencies.size()); + extractedDependencies.forEach(dep -> { + assertTrue(dependencies.indexOf(dep.getReference()) >= 0); + }); + } } diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ValueSetAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ValueSetAdapterTest.java index 4f8e62ec8..b4a71a8e2 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ValueSetAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ValueSetAdapterTest.java @@ -155,5 +155,15 @@ void adapter_copy() { } @Test - void adapter_get_all_dependencies() {} + void adapter_get_all_dependencies() { + var dependencies = List.of("profileRef"); + var valueSet = new ValueSet(); + valueSet.getMeta().addProfile(dependencies.get(0)); + var adapter = new ValueSetAdapter(valueSet); + var extractedDependencies = adapter.getDependencies(); + assertEquals(extractedDependencies.size(), dependencies.size()); + extractedDependencies.forEach(dep -> { + assertTrue(dependencies.indexOf(dep.getReference()) >= 0); + }); + } } diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/LibraryAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/LibraryAdapterTest.java index 1dfc9b647..0c4c0b0bf 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/LibraryAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/LibraryAdapterTest.java @@ -19,6 +19,7 @@ import java.util.Date; import java.util.List; import org.hl7.fhir.instance.model.api.IDomainResource; +import org.hl7.fhir.r4.model.Attachment; import org.hl7.fhir.r4.model.Bundle; import org.hl7.fhir.r4.model.CodeableConcept; import org.hl7.fhir.r4.model.Coding; @@ -163,7 +164,35 @@ void adapter_copy() { } @Test - void adapter_get_all_dependencies() {} + void adapter_get_all_dependencies() { + var dependencies = List.of( + "profileRef", "relatedArtifactRef", "dataRequirementProfileRef", "dataRequirementCodeFilterRef"); + var library = new Library(); + library.getMeta().addProfile(dependencies.get(0)); + library.getRelatedArtifactFirstRep().setResource(dependencies.get(1)); + library.addDataRequirement().addProfile(dependencies.get(2)); + library.addDataRequirement().addCodeFilter().setValueSet(dependencies.get(3)); + var adapter = new LibraryAdapter(library); + var extractedDependencies = adapter.getDependencies(); + assertEquals(extractedDependencies.size(), dependencies.size()); + extractedDependencies.forEach(dep -> { + assertTrue(dependencies.indexOf(dep.getReference()) >= 0); + }); + } + + @Test + void adapter_get_and_set_content() { + var library = new Library(); + var adapter = new LibraryAdapter(library); + var contentList = new ArrayList(); + contentList.add(new Attachment().setContentType("text/cql").setData(new byte[10])); + adapter.setContent(contentList); + assertTrue(adapter.hasContent()); + assertEquals(contentList, adapter.getContent()); + adapter.addContent().setContentType("text/xml").setData(new byte[20]); + assertEquals(2, adapter.getContent().size()); + assertEquals("text/xml", adapter.getContent().get(1).getContentType()); + } @Test void adapter_get_and_set_type() { diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/PlanDefinitionAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/PlanDefinitionAdapterTest.java index 148bb016e..700d831cd 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/PlanDefinitionAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/PlanDefinitionAdapterTest.java @@ -161,6 +161,7 @@ void adapter_copy() { @Test void adapter_get_all_dependencies() { var dependencies = List.of( + "profileRef", "relatedArtifactRef", "libraryRef", "actionTriggerDataReqProfile", @@ -175,28 +176,29 @@ void adapter_get_all_dependencies() { "cpgPartOfExtRef", "nestedActionDefinitionRef"); var planDef = new PlanDefinition(); - planDef.getRelatedArtifactFirstRep().setResource(dependencies.get(0)); - planDef.getLibrary().add(new CanonicalType(dependencies.get(1))); + planDef.getMeta().addProfile(dependencies.get(0)); + planDef.getRelatedArtifactFirstRep().setResource(dependencies.get(1)); + planDef.getLibrary().add(new CanonicalType(dependencies.get(2))); var action = planDef.getActionFirstRep(); action.getTriggerFirstRep() .getDataFirstRep() - .setProfile(List.of(new CanonicalType(dependencies.get(2)))) + .setProfile(List.of(new CanonicalType(dependencies.get(3)))) .getCodeFilterFirstRep() - .setValueSet(dependencies.get(3)); + .setValueSet(dependencies.get(4)); action.getInputFirstRep() - .setProfile(List.of(new CanonicalType(dependencies.get(4)))) + .setProfile(List.of(new CanonicalType(dependencies.get(5)))) .getCodeFilterFirstRep() - .setValueSet(dependencies.get(5)); + .setValueSet(dependencies.get(6)); action.getOutputFirstRep() - .setProfile(List.of(new CanonicalType(dependencies.get(6)))) + .setProfile(List.of(new CanonicalType(dependencies.get(7)))) .getCodeFilterFirstRep() - .setValueSet(dependencies.get(7)); - action.setDefinition(new CanonicalType(dependencies.get(8))); - action.getConditionFirstRep().getExpression().setReference(dependencies.get(9)); - action.getDynamicValueFirstRep().getExpression().setReference(dependencies.get(10)); + .setValueSet(dependencies.get(8)); + action.setDefinition(new CanonicalType(dependencies.get(9))); + action.getConditionFirstRep().getExpression().setReference(dependencies.get(10)); + action.getDynamicValueFirstRep().getExpression().setReference(dependencies.get(11)); planDef.addExtension(new Extension( - "http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-partOf", new CanonicalType(dependencies.get(11)))); - action.addAction().setDefinition(new CanonicalType(dependencies.get(12))); + "http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-partOf", new CanonicalType(dependencies.get(12)))); + action.addAction().setDefinition(new CanonicalType(dependencies.get(13))); var adapter = new PlanDefinitionAdapter(planDef); var extractedDependencies = adapter.getDependencies(); assertEquals(extractedDependencies.size(), dependencies.size()); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/QuestionnaireAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/QuestionnaireAdapterTest.java index 85c29ea15..48bdcb81d 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/QuestionnaireAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/QuestionnaireAdapterTest.java @@ -17,12 +17,17 @@ import java.util.List; import org.hl7.fhir.instance.model.api.IDomainResource; import org.hl7.fhir.r4.model.Bundle; +import org.hl7.fhir.r4.model.CanonicalType; import org.hl7.fhir.r4.model.Enumerations.PublicationStatus; +import org.hl7.fhir.r4.model.Expression; +import org.hl7.fhir.r4.model.Extension; import org.hl7.fhir.r4.model.Library; import org.hl7.fhir.r4.model.Period; import org.hl7.fhir.r4.model.Questionnaire; +import org.hl7.fhir.r4.model.Questionnaire.QuestionnaireItemType; import org.hl7.fhir.r4.model.RelatedArtifact; import org.junit.jupiter.api.Test; +import org.opencds.cqf.fhir.utility.Constants; import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; public class QuestionnaireAdapterTest { @@ -154,5 +159,68 @@ void adapter_copy() { } @Test - void adapter_get_all_dependencies() {} + void adapter_get_all_dependencies() { + var dependencies = List.of( + "profileRef", + "derivedRef", + "cqfLibraryRef", + "variableRef", + "itemDefinitionRef", + "answerValueSetRef", + // "itemMediaRef", + // "itemAnswerMediaRef", + "unitValueSetRef", + "referenceProfileRef", + "candidateExpressionRef", + "lookupQuestionnaireRef", + "itemVariableRef", + "initialExpressionRef", + "calculatedExpressionRef", + // "calculatedValueRef", + "expressionRef", + "subQuestionnaireRef"); + var questionnaire = new Questionnaire(); + questionnaire.getMeta().addProfile(dependencies.get(0)); + questionnaire.addDerivedFrom(dependencies.get(1)); + questionnaire.addExtension(Constants.CQF_LIBRARY, new CanonicalType(dependencies.get(2))); + var variableExt = new Extension(Constants.VARIABLE_EXTENSION) + .setValue(new Expression().setReference(dependencies.get(3))); + questionnaire.addExtension(variableExt); + questionnaire.addItem().setDefinition(dependencies.get(4) + "#Observation"); + questionnaire.addItem().setAnswerValueSet(dependencies.get(5)); + questionnaire + .addItem() + .setType(QuestionnaireItemType.QUANTITY) + .addExtension(Constants.QUESTIONNAIRE_UNIT_VALUE_SET, new CanonicalType(dependencies.get(6))); + questionnaire + .addItem() + .addExtension(Constants.QUESTIONNAIRE_REFERENCE_PROFILE, new CanonicalType(dependencies.get(7))); + var candidateExpressionExt = new Extension(Constants.SDC_QUESTIONNAIRE_CANDIDATE_EXPRESSION) + .setValue(new Expression().setReference(dependencies.get(8))); + questionnaire.addItem().addExtension(candidateExpressionExt); + var lookupQuestionnaireExt = new Extension(Constants.SDC_QUESTIONNAIRE_LOOKUP_QUESTIONNAIRE) + .setValue(new CanonicalType(dependencies.get(9))); + questionnaire.addItem().addExtension(lookupQuestionnaireExt); + var itemVariableExt = new Extension(Constants.VARIABLE_EXTENSION) + .setValue(new Expression().setReference(dependencies.get(10))); + questionnaire.addItem().addExtension(itemVariableExt); + var initialExpressionExt = new Extension(Constants.SDC_QUESTIONNAIRE_INITIAL_EXPRESSION) + .setValue(new Expression().setReference(dependencies.get(11))); + questionnaire.addItem().addExtension(initialExpressionExt); + var calculatedExpressionExt = new Extension(Constants.SDC_QUESTIONNAIRE_CALCULATED_EXPRESSION) + .setValue(new Expression().setReference(dependencies.get(12))); + questionnaire.addItem().addExtension(calculatedExpressionExt); + var expressionExt = + new Extension(Constants.CQF_EXPRESSION).setValue(new Expression().setReference(dependencies.get(13))); + questionnaire.addItem().addExtension(expressionExt); + questionnaire + .addItem() + .addExtension(Constants.SDC_QUESTIONNAIRE_SUB_QUESTIONNAIRE, new CanonicalType(dependencies.get(14))); + var adapter = new QuestionnaireAdapter(questionnaire); + var extractedDependencies = adapter.getDependencies(); + assertEquals(dependencies.size(), extractedDependencies.size()); + extractedDependencies.forEach(dep -> { + assertTrue(dependencies.indexOf(dep.getReference()) >= 0); + }); + } } diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/ResourceAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/ResourceAdapterTest.java index 92330372c..1386ae7e1 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/ResourceAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/ResourceAdapterTest.java @@ -81,5 +81,7 @@ void adapter_get_contained() { resource.addContained(new Library()); var adapter = new ResourceAdapter(resource); assertTrue(adapter.hasContained()); + assertNotNull(adapter.getContained()); + assertFalse(adapter.hasContained(adapter.getContained().get(0))); } } diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/StructureDefinitionAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/StructureDefinitionAdapterTest.java index 619c574ed..7b6f78d9c 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/StructureDefinitionAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/StructureDefinitionAdapterTest.java @@ -17,12 +17,15 @@ import java.util.List; import org.hl7.fhir.instance.model.api.IDomainResource; import org.hl7.fhir.r4.model.Bundle; +import org.hl7.fhir.r4.model.ElementDefinition.ElementDefinitionBindingComponent; import org.hl7.fhir.r4.model.Enumerations.PublicationStatus; +import org.hl7.fhir.r4.model.Expression; import org.hl7.fhir.r4.model.Library; import org.hl7.fhir.r4.model.Period; import org.hl7.fhir.r4.model.RelatedArtifact; import org.hl7.fhir.r4.model.StructureDefinition; import org.junit.jupiter.api.Test; +import org.opencds.cqf.fhir.utility.Constants; import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; public class StructureDefinitionAdapterTest { @@ -155,5 +158,35 @@ void adapter_copy() { } @Test - void adapter_get_all_dependencies() {} + void adapter_get_all_dependencies() { + var dependencies = List.of( + "profileRef", + "baseDefinition", + "cpgAssertionRef", + "cpgFeatureRef", + "cpgInferenceRef", + "elementProfileRef", + "elementTargetProfileRef", + "elementValueSetBindingRef"); + var structureDef = new StructureDefinition(); + structureDef.getMeta().addProfile(dependencies.get(0)); + structureDef.setBaseDefinition(dependencies.get(1)); + structureDef.addExtension( + Constants.CPG_ASSERTION_EXPRESSION, new Expression().setReference(dependencies.get(2))); + structureDef.addExtension(Constants.CPG_FEATURE_EXPRESSION, new Expression().setReference(dependencies.get(3))); + structureDef.addExtension( + Constants.CPG_INFERENCE_EXPRESSION, new Expression().setReference(dependencies.get(4))); + structureDef.getDifferential().addElement().addType().addProfile(dependencies.get(5)); + structureDef.getDifferential().addElement().addType().addTargetProfile(dependencies.get(6)); + structureDef + .getDifferential() + .addElement() + .setBinding(new ElementDefinitionBindingComponent().setValueSet(dependencies.get(7))); + var adapter = new StructureDefinitionAdapter(structureDef); + var extractedDependencies = adapter.getDependencies(); + assertEquals(dependencies.size(), extractedDependencies.size()); + extractedDependencies.forEach(dep -> { + assertTrue(dependencies.indexOf(dep.getReference()) >= 0); + }); + } } diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/ValueSetAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/ValueSetAdapterTest.java index 66cd88641..fc3db891d 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/ValueSetAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/ValueSetAdapterTest.java @@ -155,5 +155,15 @@ void adapter_copy() { } @Test - void adapter_get_all_dependencies() {} + void adapter_get_all_dependencies() { + var dependencies = List.of("profileRef"); + var valueSet = new ValueSet(); + valueSet.getMeta().addProfile(dependencies.get(0)); + var adapter = new ValueSetAdapter(valueSet); + var extractedDependencies = adapter.getDependencies(); + assertEquals(dependencies.size(), extractedDependencies.size()); + extractedDependencies.forEach(dep -> { + assertTrue(dependencies.indexOf(dep.getReference()) >= 0); + }); + } } diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/LibraryAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/LibraryAdapterTest.java index 83007b0ca..00f51e14a 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/LibraryAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/LibraryAdapterTest.java @@ -19,6 +19,7 @@ import java.util.Date; import java.util.List; import org.hl7.fhir.instance.model.api.IDomainResource; +import org.hl7.fhir.r5.model.Attachment; import org.hl7.fhir.r5.model.Bundle; import org.hl7.fhir.r5.model.CodeableConcept; import org.hl7.fhir.r5.model.Coding; @@ -164,7 +165,35 @@ void adapter_copy() { } @Test - void adapter_get_all_dependencies() {} + void adapter_get_all_dependencies() { + var dependencies = List.of( + "profileRef", "relatedArtifactRef", "dataRequirementProfileRef", "dataRequirementCodeFilterRef"); + var library = new Library(); + library.getMeta().addProfile(dependencies.get(0)); + library.getRelatedArtifactFirstRep().setResource(dependencies.get(1)); + library.addDataRequirement().addProfile(dependencies.get(2)); + library.addDataRequirement().addCodeFilter().setValueSet(dependencies.get(3)); + var adapter = new LibraryAdapter(library); + var extractedDependencies = adapter.getDependencies(); + assertEquals(extractedDependencies.size(), dependencies.size()); + extractedDependencies.forEach(dep -> { + assertTrue(dependencies.indexOf(dep.getReference()) >= 0); + }); + } + + @Test + void adapter_get_and_set_content() { + var library = new Library(); + var adapter = new LibraryAdapter(library); + var contentList = new ArrayList(); + contentList.add(new Attachment().setContentType("text/cql").setData(new byte[10])); + adapter.setContent(contentList); + assertTrue(adapter.hasContent()); + assertEquals(contentList, adapter.getContent()); + adapter.addContent().setContentType("text/xml").setData(new byte[20]); + assertEquals(2, adapter.getContent().size()); + assertEquals("text/xml", adapter.getContent().get(1).getContentType()); + } @Test void adapter_get_and_set_type() { diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/PlanDefinitionAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/PlanDefinitionAdapterTest.java index 36f8469cb..0900a1fdf 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/PlanDefinitionAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/PlanDefinitionAdapterTest.java @@ -161,6 +161,7 @@ void adapter_copy() { @Test void adapter_get_all_dependencies() { var dependencies = List.of( + "profileRef", "relatedArtifactRef", "libraryRef", "actionTriggerDataReqProfile", @@ -175,30 +176,31 @@ void adapter_get_all_dependencies() { "cpgPartOfExtRef", "nestedActionDefinitionRef"); var planDef = new PlanDefinition(); - planDef.getRelatedArtifactFirstRep().setResource(dependencies.get(0)); - planDef.getLibrary().add(new CanonicalType(dependencies.get(1))); + planDef.getMeta().addProfile(dependencies.get(0)); + planDef.getRelatedArtifactFirstRep().setResource(dependencies.get(1)); + planDef.getLibrary().add(new CanonicalType(dependencies.get(2))); var action = planDef.getActionFirstRep(); action.getTriggerFirstRep() .getDataFirstRep() - .setProfile(List.of(new CanonicalType(dependencies.get(2)))) + .setProfile(List.of(new CanonicalType(dependencies.get(3)))) .getCodeFilterFirstRep() - .setValueSet(dependencies.get(3)); + .setValueSet(dependencies.get(4)); action.getInputFirstRep() .getRequirement() - .setProfile(List.of(new CanonicalType(dependencies.get(4)))) + .setProfile(List.of(new CanonicalType(dependencies.get(5)))) .getCodeFilterFirstRep() - .setValueSet(dependencies.get(5)); + .setValueSet(dependencies.get(6)); action.getOutputFirstRep() .getRequirement() - .setProfile(List.of(new CanonicalType(dependencies.get(6)))) + .setProfile(List.of(new CanonicalType(dependencies.get(7)))) .getCodeFilterFirstRep() - .setValueSet(dependencies.get(7)); - action.setDefinition(new CanonicalType(dependencies.get(8))); - action.getConditionFirstRep().getExpression().setReference(dependencies.get(9)); - action.getDynamicValueFirstRep().getExpression().setReference(dependencies.get(10)); + .setValueSet(dependencies.get(8)); + action.setDefinition(new CanonicalType(dependencies.get(9))); + action.getConditionFirstRep().getExpression().setReference(dependencies.get(10)); + action.getDynamicValueFirstRep().getExpression().setReference(dependencies.get(11)); planDef.addExtension(new Extension( - "http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-partOf", new CanonicalType(dependencies.get(11)))); - action.addAction().setDefinition(new CanonicalType(dependencies.get(12))); + "http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-partOf", new CanonicalType(dependencies.get(12)))); + action.addAction().setDefinition(new CanonicalType(dependencies.get(13))); var adapter = new PlanDefinitionAdapter(planDef); var extractedDependencies = adapter.getDependencies(); assertEquals(extractedDependencies.size(), dependencies.size()); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapterTest.java index 940d087cc..3b92bf0b6 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapterTest.java @@ -17,12 +17,17 @@ import java.util.List; import org.hl7.fhir.instance.model.api.IDomainResource; import org.hl7.fhir.r5.model.Bundle; +import org.hl7.fhir.r5.model.CanonicalType; import org.hl7.fhir.r5.model.Enumerations.PublicationStatus; +import org.hl7.fhir.r5.model.Expression; +import org.hl7.fhir.r5.model.Extension; import org.hl7.fhir.r5.model.Library; import org.hl7.fhir.r5.model.Period; import org.hl7.fhir.r5.model.Questionnaire; +import org.hl7.fhir.r5.model.Questionnaire.QuestionnaireItemType; import org.hl7.fhir.r5.model.RelatedArtifact; import org.junit.jupiter.api.Test; +import org.opencds.cqf.fhir.utility.Constants; import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; public class QuestionnaireAdapterTest { @@ -154,5 +159,68 @@ void adapter_copy() { } @Test - void adapter_get_all_dependencies() {} + void adapter_get_all_dependencies() { + var dependencies = List.of( + "profileRef", + "derivedRef", + "cqfLibraryRef", + "variableRef", + "itemDefinitionRef", + "answerValueSetRef", + // "itemMediaRef", + // "itemAnswerMediaRef", + "unitValueSetRef", + "referenceProfileRef", + "candidateExpressionRef", + "lookupQuestionnaireRef", + "itemVariableRef", + "initialExpressionRef", + "calculatedExpressionRef", + // "calculatedValueRef", + "expressionRef", + "subQuestionnaireRef"); + var questionnaire = new Questionnaire(); + questionnaire.getMeta().addProfile(dependencies.get(0)); + questionnaire.addDerivedFrom(dependencies.get(1)); + questionnaire.addExtension(Constants.CQF_LIBRARY, new CanonicalType(dependencies.get(2))); + var variableExt = new Extension(Constants.VARIABLE_EXTENSION) + .setValue(new Expression().setReference(dependencies.get(3))); + questionnaire.addExtension(variableExt); + questionnaire.addItem().setDefinition(dependencies.get(4) + "#Observation"); + questionnaire.addItem().setAnswerValueSet(dependencies.get(5)); + questionnaire + .addItem() + .setType(QuestionnaireItemType.QUANTITY) + .addExtension(Constants.QUESTIONNAIRE_UNIT_VALUE_SET, new CanonicalType(dependencies.get(6))); + questionnaire + .addItem() + .addExtension(Constants.QUESTIONNAIRE_REFERENCE_PROFILE, new CanonicalType(dependencies.get(7))); + var candidateExpressionExt = new Extension(Constants.SDC_QUESTIONNAIRE_CANDIDATE_EXPRESSION) + .setValue(new Expression().setReference(dependencies.get(8))); + questionnaire.addItem().addExtension(candidateExpressionExt); + var lookupQuestionnaireExt = new Extension(Constants.SDC_QUESTIONNAIRE_LOOKUP_QUESTIONNAIRE) + .setValue(new CanonicalType(dependencies.get(9))); + questionnaire.addItem().addExtension(lookupQuestionnaireExt); + var itemVariableExt = new Extension(Constants.VARIABLE_EXTENSION) + .setValue(new Expression().setReference(dependencies.get(10))); + questionnaire.addItem().addExtension(itemVariableExt); + var initialExpressionExt = new Extension(Constants.SDC_QUESTIONNAIRE_INITIAL_EXPRESSION) + .setValue(new Expression().setReference(dependencies.get(11))); + questionnaire.addItem().addExtension(initialExpressionExt); + var calculatedExpressionExt = new Extension(Constants.SDC_QUESTIONNAIRE_CALCULATED_EXPRESSION) + .setValue(new Expression().setReference(dependencies.get(12))); + questionnaire.addItem().addExtension(calculatedExpressionExt); + var expressionExt = + new Extension(Constants.CQF_EXPRESSION).setValue(new Expression().setReference(dependencies.get(13))); + questionnaire.addItem().addExtension(expressionExt); + questionnaire + .addItem() + .addExtension(Constants.SDC_QUESTIONNAIRE_SUB_QUESTIONNAIRE, new CanonicalType(dependencies.get(14))); + var adapter = new QuestionnaireAdapter(questionnaire); + var extractedDependencies = adapter.getDependencies(); + assertEquals(dependencies.size(), extractedDependencies.size()); + extractedDependencies.forEach(dep -> { + assertTrue(dependencies.indexOf(dep.getReference()) >= 0); + }); + } } diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapterTest.java index ecdf8f35c..212e50591 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapterTest.java @@ -81,5 +81,7 @@ void adapter_get_contained() { resource.addContained(new Library()); var adapter = new ResourceAdapter(resource); assertTrue(adapter.hasContained()); + assertNotNull(adapter.getContained()); + assertFalse(adapter.hasContained(adapter.getContained().get(0))); } } diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapterTest.java index feb174d79..95fdb4d24 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapterTest.java @@ -17,12 +17,15 @@ import java.util.List; import org.hl7.fhir.instance.model.api.IDomainResource; import org.hl7.fhir.r5.model.Bundle; +import org.hl7.fhir.r5.model.ElementDefinition.ElementDefinitionBindingComponent; import org.hl7.fhir.r5.model.Enumerations.PublicationStatus; +import org.hl7.fhir.r5.model.Expression; import org.hl7.fhir.r5.model.Library; import org.hl7.fhir.r5.model.Period; import org.hl7.fhir.r5.model.RelatedArtifact; import org.hl7.fhir.r5.model.StructureDefinition; import org.junit.jupiter.api.Test; +import org.opencds.cqf.fhir.utility.Constants; import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; public class StructureDefinitionAdapterTest { @@ -155,5 +158,35 @@ void adapter_copy() { } @Test - void adapter_get_all_dependencies() {} + void adapter_get_all_dependencies() { + var dependencies = List.of( + "profileRef", + "baseDefinition", + "cpgAssertionRef", + "cpgFeatureRef", + "cpgInferenceRef", + "elementProfileRef", + "elementTargetProfileRef", + "elementValueSetBindingRef"); + var structureDef = new StructureDefinition(); + structureDef.getMeta().addProfile(dependencies.get(0)); + structureDef.setBaseDefinition(dependencies.get(1)); + structureDef.addExtension( + Constants.CPG_ASSERTION_EXPRESSION, new Expression().setReference(dependencies.get(2))); + structureDef.addExtension(Constants.CPG_FEATURE_EXPRESSION, new Expression().setReference(dependencies.get(3))); + structureDef.addExtension( + Constants.CPG_INFERENCE_EXPRESSION, new Expression().setReference(dependencies.get(4))); + structureDef.getDifferential().addElement().addType().addProfile(dependencies.get(5)); + structureDef.getDifferential().addElement().addType().addTargetProfile(dependencies.get(6)); + structureDef + .getDifferential() + .addElement() + .setBinding(new ElementDefinitionBindingComponent().setValueSet(dependencies.get(7))); + var adapter = new StructureDefinitionAdapter(structureDef); + var extractedDependencies = adapter.getDependencies(); + assertEquals(dependencies.size(), extractedDependencies.size()); + extractedDependencies.forEach(dep -> { + assertTrue(dependencies.indexOf(dep.getReference()) >= 0); + }); + } } diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ValueSetAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ValueSetAdapterTest.java index 504d5b4d7..08fbcc329 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ValueSetAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ValueSetAdapterTest.java @@ -158,5 +158,15 @@ void adapter_copy() { } @Test - void adapter_get_all_dependencies() {} + void adapter_get_all_dependencies() { + var dependencies = List.of("profileRef"); + var valueSet = new ValueSet(); + valueSet.getMeta().addProfile(dependencies.get(0)); + var adapter = new ValueSetAdapter(valueSet); + var extractedDependencies = adapter.getDependencies(); + assertEquals(dependencies.size(), extractedDependencies.size()); + extractedDependencies.forEach(dep -> { + assertTrue(dependencies.indexOf(dep.getReference()) >= 0); + }); + } } diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/ReleaseVisitorTests.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/ReleaseVisitorTests.java index b397804e4..410e579f0 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/ReleaseVisitorTests.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/dstu3/ReleaseVisitorTests.java @@ -168,7 +168,13 @@ void visitLibraryTest() { "http://hl7.org/fhir/us/core/StructureDefinition/us-core-observation-lab", "http://hl7.org/fhir/us/core/StructureDefinition/us-core-diagnosticreport-lab", "http://hl7.org/fhir/us/ecr/StructureDefinition/eicr-document-bundle", - "http://hl7.org/fhir/StructureDefinition/ServiceRequest"); + "http://hl7.org/fhir/StructureDefinition/ServiceRequest", + "http://hl7.org/fhir/us/ecr/StructureDefinition/ersd-valueset-library", + "http://hl7.org/fhir/us/ecr/StructureDefinition/us-ph-triggering-valueset-library", + "http://hl7.org/fhir/us/ecr/StructureDefinition/ersd-plandefinition", + "http://hl7.org/fhir/us/ecr/StructureDefinition/us-ph-plandefinition", + "http://hl7.org/fhir/us/ecr/StructureDefinition/ersd-valueset", + "http://hl7.org/fhir/us/ecr/StructureDefinition/us-ph-triggering-valueset"); var expectedErsdTestArtifactComponents = Arrays.asList( "http://ersd.aimsplatform.org/fhir/PlanDefinition/release-us-ecr-specification|" + existingVersion, "http://ersd.aimsplatform.org/fhir/Library/release-rctc|" + existingVersion, diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/ReleaseVisitorTests.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/ReleaseVisitorTests.java index 092cca33c..dabf6c6d5 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/ReleaseVisitorTests.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r4/ReleaseVisitorTests.java @@ -169,7 +169,13 @@ void visitLibraryTest() { "http://hl7.org/fhir/us/core/StructureDefinition/us-core-observation-lab", "http://hl7.org/fhir/us/core/StructureDefinition/us-core-diagnosticreport-lab", "http://hl7.org/fhir/us/ecr/StructureDefinition/eicr-document-bundle", - "http://hl7.org/fhir/StructureDefinition/ServiceRequest"); + "http://hl7.org/fhir/StructureDefinition/ServiceRequest", + "http://hl7.org/fhir/us/ecr/StructureDefinition/ersd-valueset-library", + "http://hl7.org/fhir/us/ecr/StructureDefinition/us-ph-triggering-valueset-library", + "http://hl7.org/fhir/us/ecr/StructureDefinition/ersd-plandefinition", + "http://hl7.org/fhir/us/ecr/StructureDefinition/us-ph-plandefinition", + "http://hl7.org/fhir/us/ecr/StructureDefinition/ersd-valueset", + "http://hl7.org/fhir/us/ecr/StructureDefinition/us-ph-triggering-valueset"); var expectedErsdTestArtifactComponents = Arrays.asList( "http://ersd.aimsplatform.org/fhir/PlanDefinition/release-us-ecr-specification|" + existingVersion, "http://ersd.aimsplatform.org/fhir/Library/release-rctc|" + existingVersion, diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/ReleaseVisitorTests.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/ReleaseVisitorTests.java index d3184f6a0..a4d969257 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/ReleaseVisitorTests.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/visitor/r5/ReleaseVisitorTests.java @@ -166,7 +166,13 @@ void visitLibraryTest() { "http://hl7.org/fhir/us/core/StructureDefinition/us-core-observation-lab", "http://hl7.org/fhir/us/core/StructureDefinition/us-core-diagnosticreport-lab", "http://hl7.org/fhir/us/ecr/StructureDefinition/eicr-document-bundle", - "http://hl7.org/fhir/StructureDefinition/ServiceRequest"); + "http://hl7.org/fhir/StructureDefinition/ServiceRequest", + "http://hl7.org/fhir/us/ecr/StructureDefinition/ersd-valueset-library", + "http://hl7.org/fhir/us/ecr/StructureDefinition/us-ph-triggering-valueset-library", + "http://hl7.org/fhir/us/ecr/StructureDefinition/ersd-plandefinition", + "http://hl7.org/fhir/us/ecr/StructureDefinition/us-ph-plandefinition", + "http://hl7.org/fhir/us/ecr/StructureDefinition/ersd-valueset", + "http://hl7.org/fhir/us/ecr/StructureDefinition/us-ph-triggering-valueset"); var expectedErsdTestArtifactComponents = Arrays.asList( "http://ersd.aimsplatform.org/fhir/PlanDefinition/release-us-ecr-specification|" + existingVersion, "http://ersd.aimsplatform.org/fhir/Library/release-rctc|" + existingVersion, From a92cf90f52664afa6871e398cfbdcb109f3770ee Mon Sep 17 00:00:00 2001 From: Brenin Rhodes Date: Mon, 29 Jul 2024 15:10:38 -0600 Subject: [PATCH 22/38] spotless --- cqf-fhir-utility/pom.xml | 6 ------ .../fhir/utility/client/TerminologyServerClientTest.java | 7 ++----- 2 files changed, 2 insertions(+), 11 deletions(-) diff --git a/cqf-fhir-utility/pom.xml b/cqf-fhir-utility/pom.xml index df806b0ee..11b677c89 100644 --- a/cqf-fhir-utility/pom.xml +++ b/cqf-fhir-utility/pom.xml @@ -88,11 +88,5 @@ ch.qos.logback logback-classic - - - ca.uhn.hapi.fhir - hapi-fhir-base - 3.8.0 - \ No newline at end of file diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/client/TerminologyServerClientTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/client/TerminologyServerClientTest.java index d65d495cc..702990a68 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/client/TerminologyServerClientTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/client/TerminologyServerClientTest.java @@ -240,16 +240,13 @@ void addressUrlParsing() { var ts = new TerminologyServerClient(new FhirContext(version)); var theCorrectBaseServerUrl = "https://cts.nlm.nih.gov/fhir"; // remove the FHIR type and the ID if included - assertEquals( - theCorrectBaseServerUrl, ts.getAddressBase(theCorrectBaseServerUrl + "/ValueSet/1")); + assertEquals(theCorrectBaseServerUrl, ts.getAddressBase(theCorrectBaseServerUrl + "/ValueSet/1")); // remove a FHIR type if one was included assertEquals(theCorrectBaseServerUrl, ts.getAddressBase(theCorrectBaseServerUrl + "/ValueSet")); // don't break on the actual base url assertEquals(theCorrectBaseServerUrl, ts.getAddressBase(theCorrectBaseServerUrl)); // ensure it's forcing https - assertEquals( - theCorrectBaseServerUrl, - ts.getAddressBase(theCorrectBaseServerUrl.replace("https", "http"))); + assertEquals(theCorrectBaseServerUrl, ts.getAddressBase(theCorrectBaseServerUrl.replace("https", "http"))); // remove trailing slashes assertEquals(theCorrectBaseServerUrl, ts.getAddressBase(theCorrectBaseServerUrl + "/")); } From d3030a3252b9e79aa02f2cfd42501259b1fecc63 Mon Sep 17 00:00:00 2001 From: Brenin Rhodes Date: Mon, 29 Jul 2024 17:37:57 -0600 Subject: [PATCH 23/38] add tests --- .../cqf/fhir/utility/CqfExpression.java | 19 ++----- .../utility/adapter/r4/ResourceAdapter.java | 16 ------ .../cqf/fhir/utility/CqfExpressionTests.java | 52 +++++++++++++++++-- .../adapter/dstu3/ResourceAdapterTest.java | 1 + .../adapter/r4/ResourceAdapterTest.java | 1 + .../adapter/r5/ResourceAdapterTest.java | 1 + .../r5/StructureDefinitionAdapterTest.java | 6 +++ 7 files changed, 60 insertions(+), 36 deletions(-) diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/CqfExpression.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/CqfExpression.java index 11aa9825a..529a9ff99 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/CqfExpression.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/CqfExpression.java @@ -31,17 +31,8 @@ public static CqfExpression of(IBaseExtension extension, String defaultLib var version = FhirVersionEnum.forVersionString(model); switch (version) { case DSTU3: - var libraryExtension = extension.getExtension().stream() - .map(e -> (IBaseExtension) e) - .filter(e -> e.getUrl().equals(Constants.CQIF_LIBRARY)) - .findFirst() - .orElse(null); return new CqfExpression( - "text/cql.expression", - extension.getValue().toString(), - libraryExtension == null - ? defaultLibraryUrl - : libraryExtension.getValue().toString()); + "text/cql.expression", extension.getValue().toString(), defaultLibraryUrl); case R4: return CqfExpression.of((org.hl7.fhir.r4.model.Expression) extension.getValue(), defaultLibraryUrl); case R5: @@ -65,9 +56,7 @@ public static CqfExpression of(org.hl7.fhir.r4.model.Expression expression, Stri expression.hasReference() ? expression.getReference() : defaultLibraryUrl, altExpression != null ? altExpression.getLanguage() : null, altExpression != null ? altExpression.getExpression() : null, - altExpression != null && altExpression.hasReference() - ? altExpression.getReference() - : defaultLibraryUrl); + altExpression != null && altExpression.hasReference() ? altExpression.getReference() : null); } public static CqfExpression of(org.hl7.fhir.r5.model.Expression expression, String defaultLibraryUrl) { @@ -83,9 +72,7 @@ public static CqfExpression of(org.hl7.fhir.r5.model.Expression expression, Stri expression.hasReference() ? expression.getReference() : defaultLibraryUrl, altExpression != null ? altExpression.getLanguage() : null, altExpression != null ? altExpression.getExpression() : null, - altExpression != null && altExpression.hasReference() - ? altExpression.getReference() - : defaultLibraryUrl); + altExpression != null && altExpression.hasReference() ? altExpression.getReference() : null); } public CqfExpression() {} diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/ResourceAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/ResourceAdapter.java index 4c6e30819..97efd14ba 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/ResourceAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/ResourceAdapter.java @@ -3,16 +3,12 @@ import static java.util.Optional.ofNullable; import ca.uhn.fhir.context.FhirVersionEnum; -import java.util.List; import java.util.Optional; -import java.util.stream.Collectors; import org.hl7.fhir.exceptions.FHIRException; import org.hl7.fhir.instance.model.api.IBase; -import org.hl7.fhir.instance.model.api.IBaseExtension; import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.r4.model.Base; import org.hl7.fhir.r4.model.DomainResource; -import org.hl7.fhir.r4.model.Extension; import org.hl7.fhir.r4.model.Resource; import org.opencds.cqf.fhir.utility.adapter.BaseResourceAdapter; @@ -101,16 +97,4 @@ public boolean equalsDeep(IBase other) { public boolean equalsShallow(IBase other) { return getResource().equalsShallow((Base) other); } - - @Override - public void setExtension(List> extensions) { - getDomainResource() - .ifPresent(r -> r.setExtension( - extensions.stream().map(e -> (Extension) e).collect(Collectors.toList()))); - } - - @Override - public > void addExtension(T extension) { - getDomainResource().ifPresent(r -> r.addExtension((Extension) extension)); - } } diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/CqfExpressionTests.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/CqfExpressionTests.java index f586ca948..0913a24ee 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/CqfExpressionTests.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/CqfExpressionTests.java @@ -1,12 +1,56 @@ package org.opencds.cqf.fhir.utility; -import org.hl7.fhir.r4.model.Extension; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + import org.junit.jupiter.api.Test; class CqfExpressionTests { @Test - void test() { - var ext = new Extension(); - var expression = CqfExpression.of(ext, "http://test.com/Library/test"); + void testDstu3Extension() { + var expression = new org.hl7.fhir.dstu3.model.StringType("expression"); + var ext = new org.hl7.fhir.dstu3.model.Extension(Constants.CQIF_CQL_EXPRESSION, expression); + var cqfExpression = CqfExpression.of(ext, "http://test.com/Library/test"); + assertEquals(expression.getValue(), cqfExpression.getExpression()); + } + + @Test + void testR4Extension() { + var defaultLibraryUrl = "http://test.com/Library/test"; + var expression = new org.hl7.fhir.r4.model.Expression() + .setLanguage("text/cql.identifier") + .setExpression("expression"); + var altExpression = new org.hl7.fhir.r4.model.Expression() + .setLanguage("text/fhirpath") + .setExpression("altExpression"); + expression.addExtension(Constants.ALT_EXPRESSION_EXT, altExpression); + var ext = new org.hl7.fhir.r4.model.Extension(Constants.CQF_EXPRESSION, expression); + var cqfExpression = CqfExpression.of(ext, defaultLibraryUrl); + assertEquals(expression.getExpression(), cqfExpression.getExpression()); + assertEquals(expression.getLanguage(), cqfExpression.getLanguage()); + assertEquals(defaultLibraryUrl, cqfExpression.getLibraryUrl()); + assertEquals(altExpression.getExpression(), cqfExpression.getAltExpression()); + assertEquals(altExpression.getLanguage(), cqfExpression.getAltLanguage()); + assertNull(cqfExpression.getAltLibraryUrl()); + } + + @Test + void testR5Extension() { + var defaultLibraryUrl = "http://test.com/Library/test"; + var expression = new org.hl7.fhir.r5.model.Expression() + .setLanguage("text/cql.identifier") + .setExpression("expression"); + var altExpression = new org.hl7.fhir.r5.model.Expression() + .setLanguage("text/fhirpath") + .setExpression("altExpression"); + expression.addExtension(Constants.ALT_EXPRESSION_EXT, altExpression); + var ext = new org.hl7.fhir.r5.model.Extension(Constants.CQF_EXPRESSION, expression); + var cqfExpression = CqfExpression.of(ext, defaultLibraryUrl); + assertEquals(expression.getExpression(), cqfExpression.getExpression()); + assertEquals(expression.getLanguage(), cqfExpression.getLanguage()); + assertEquals(defaultLibraryUrl, cqfExpression.getLibraryUrl()); + assertEquals(altExpression.getExpression(), cqfExpression.getAltExpression()); + assertEquals(altExpression.getLanguage(), cqfExpression.getAltLanguage()); + assertNull(cqfExpression.getAltLibraryUrl()); } } diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapterTest.java index e1ec9d224..aef8dab14 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapterTest.java @@ -70,6 +70,7 @@ void adapter_get_and_set_extension() { var extensionList = List.of(new Extension().setUrl("test-extension-url").setValue(new BooleanType(true))); var adapter = new ResourceAdapter(resource); adapter.setExtension(extensionList); + assertTrue(adapter.hasExtension()); assertEquals(extensionList, resource.getExtension()); assertEquals(extensionList, adapter.getExtension()); assertTrue(adapter.hasExtension("test-extension-url")); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/ResourceAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/ResourceAdapterTest.java index 1386ae7e1..f213b7198 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/ResourceAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/ResourceAdapterTest.java @@ -70,6 +70,7 @@ void adapter_get_and_set_extension() { var extensionList = List.of(new Extension().setUrl("test-extension-url").setValue(new BooleanType(true))); var adapter = new ResourceAdapter(resource); adapter.setExtension(extensionList); + assertTrue(adapter.hasExtension()); assertEquals(extensionList, resource.getExtension()); assertEquals(extensionList, adapter.getExtension()); assertTrue(adapter.hasExtension("test-extension-url")); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapterTest.java index 212e50591..1176f0359 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapterTest.java @@ -70,6 +70,7 @@ void adapter_get_and_set_extension() { var extensionList = List.of(new Extension().setUrl("test-extension-url").setValue(new BooleanType(true))); var adapter = new ResourceAdapter(resource); adapter.setExtension(extensionList); + assertTrue(adapter.hasExtension()); assertEquals(extensionList, resource.getExtension()); assertEquals(extensionList, adapter.getExtension()); assertTrue(adapter.hasExtension("test-extension-url")); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapterTest.java index 95fdb4d24..c2d3579cc 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapterTest.java @@ -17,6 +17,7 @@ import java.util.List; import org.hl7.fhir.instance.model.api.IDomainResource; import org.hl7.fhir.r5.model.Bundle; +import org.hl7.fhir.r5.model.DateTimeType; import org.hl7.fhir.r5.model.ElementDefinition.ElementDefinitionBindingComponent; import org.hl7.fhir.r5.model.Enumerations.PublicationStatus; import org.hl7.fhir.r5.model.Expression; @@ -116,6 +117,9 @@ void adapter_get_and_set_dates() { newDate.setTime(100); adapter.setDate(newDate); assertEquals(newDate, structureDef.getDate()); + var newDateElement = new DateTimeType().setValue(new Date()); + adapter.setDateElement(newDateElement); + assertEquals(newDateElement, structureDef.getDateElement()); var newApprovalDate = new Date(); newApprovalDate.setTime(100); adapter.setApprovalDate(newApprovalDate); @@ -143,6 +147,8 @@ void adapter_set_relatedArtifact() { var adapter = new StructureDefinitionAdapter(structureDef); adapter.setRelatedArtifact(relatedArtifactList); assertEquals(0, adapter.getRelatedArtifact().size()); + assertThrows(UnprocessableEntityException.class, () -> adapter.getRelatedArtifactsOfType("invalid")); + assertEquals(0, adapter.getRelatedArtifactsOfType("depends-on").size()); } @Test From 916e4b3aa375ce2a3da131878b558d43742fb816 Mon Sep 17 00:00:00 2001 From: Brenin Rhodes Date: Thu, 8 Aug 2024 13:51:26 -0600 Subject: [PATCH 24/38] add tests --- .../adapter/dstu3/ResourceAdapter.java | 16 ---------- .../utility/adapter/r5/ResourceAdapter.java | 16 ---------- .../cqf/fhir/utility/CqfExpressionTests.java | 10 +++++++ .../cqf/fhir/utility/adapter/AdapterTest.java | 21 +++++++++++++ .../adapter/dstu3/ResourceAdapterTest.java | 20 +++++++++++++ .../adapter/r4/ResourceAdapterTest.java | 20 +++++++++++++ .../adapter/r5/EndpointAdapterTest.java | 6 ++-- .../adapter/r5/LibraryAdapterTest.java | 23 +++++++------- .../adapter/r5/PlanDefinitionAdapterTest.java | 23 +++++++------- .../adapter/r5/QuestionnaireAdapterTest.java | 23 +++++++------- .../adapter/r5/ResourceAdapterTest.java | 30 ++++++++++++++++--- .../r5/StructureDefinitionAdapterTest.java | 23 +++++++------- .../adapter/r5/ValueSetAdapterTest.java | 23 +++++++------- 13 files changed, 161 insertions(+), 93 deletions(-) create mode 100644 cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/AdapterTest.java diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapter.java index 9e8a3325f..ab81ace8c 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapter.java @@ -3,16 +3,12 @@ import static java.util.Optional.ofNullable; import ca.uhn.fhir.context.FhirVersionEnum; -import java.util.List; import java.util.Optional; -import java.util.stream.Collectors; import org.hl7.fhir.dstu3.model.Base; import org.hl7.fhir.dstu3.model.DomainResource; -import org.hl7.fhir.dstu3.model.Extension; import org.hl7.fhir.dstu3.model.Resource; import org.hl7.fhir.exceptions.FHIRException; import org.hl7.fhir.instance.model.api.IBase; -import org.hl7.fhir.instance.model.api.IBaseExtension; import org.hl7.fhir.instance.model.api.IBaseResource; import org.opencds.cqf.fhir.utility.adapter.BaseResourceAdapter; @@ -109,16 +105,4 @@ public boolean equalsDeep(IBase other) { public boolean equalsShallow(IBase other) { return getResource().equalsShallow((Base) other); } - - @Override - public void setExtension(List> extensions) { - getDomainResource() - .ifPresent(r -> r.setExtension( - extensions.stream().map(e -> (Extension) e).collect(Collectors.toList()))); - } - - @Override - public > void addExtension(T extension) { - getDomainResource().ifPresent(r -> r.addExtension((Extension) extension)); - } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapter.java index 5fddb1a7a..b7fbddc2d 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapter.java @@ -3,16 +3,12 @@ import static java.util.Optional.ofNullable; import ca.uhn.fhir.context.FhirVersionEnum; -import java.util.List; import java.util.Optional; -import java.util.stream.Collectors; import org.hl7.fhir.exceptions.FHIRException; import org.hl7.fhir.instance.model.api.IBase; -import org.hl7.fhir.instance.model.api.IBaseExtension; import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.r5.model.Base; import org.hl7.fhir.r5.model.DomainResource; -import org.hl7.fhir.r5.model.Extension; import org.hl7.fhir.r5.model.Resource; import org.opencds.cqf.fhir.utility.adapter.BaseResourceAdapter; @@ -109,16 +105,4 @@ public boolean equalsDeep(IBase other) { public boolean equalsShallow(IBase other) { return getResource().equalsShallow((Base) other); } - - @Override - public void setExtension(List> extensions) { - getDomainResource() - .ifPresent(r -> r.setExtension( - extensions.stream().map(e -> (Extension) e).collect(Collectors.toList()))); - } - - @Override - public > void addExtension(T extension) { - getDomainResource().ifPresent(r -> r.addExtension((Extension) extension)); - } } diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/CqfExpressionTests.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/CqfExpressionTests.java index 0913a24ee..9fe27fa8c 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/CqfExpressionTests.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/CqfExpressionTests.java @@ -3,9 +3,19 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; +import org.hl7.fhir.dstu2.model.Extension; +import org.hl7.fhir.instance.model.api.IBaseExtension; import org.junit.jupiter.api.Test; class CqfExpressionTests { + @Test + void testInvalidExtension() { + var cqfExpression = CqfExpression.of((IBaseExtension) null, null); + assertNull(cqfExpression); + var dstu2Expression = CqfExpression.of(new Extension(), null); + assertNull(dstu2Expression); + } + @Test void testDstu3Extension() { var expression = new org.hl7.fhir.dstu3.model.StringType("expression"); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/AdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/AdapterTest.java new file mode 100644 index 000000000..fd984bf81 --- /dev/null +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/AdapterTest.java @@ -0,0 +1,21 @@ +package org.opencds.cqf.fhir.utility.adapter; + +import static org.junit.Assert.assertThrows; + +import ca.uhn.fhir.context.FhirVersionEnum; +import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; +import java.util.Date; +import org.junit.jupiter.api.Test; + +class AdapterTest { + @Test + void testUnsupportedVersion() { + var version = FhirVersionEnum.DSTU2; + assertThrows(UnprocessableEntityException.class, () -> Adapter.newPeriod(version)); + assertThrows(UnprocessableEntityException.class, () -> Adapter.newStringType(version, "string")); + assertThrows(UnprocessableEntityException.class, () -> Adapter.newUriType(version, "uri")); + assertThrows(UnprocessableEntityException.class, () -> Adapter.newUrlType(version, "url")); + assertThrows(UnprocessableEntityException.class, () -> Adapter.newDateType(version, new Date())); + assertThrows(UnprocessableEntityException.class, () -> Adapter.newDateTimeType(version, new Date())); + } +} diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapterTest.java index aef8dab14..ddb946122 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapterTest.java @@ -7,9 +7,14 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import ca.uhn.fhir.model.primitive.IdDt; +import ch.qos.logback.classic.Level; +import ch.qos.logback.classic.Logger; +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.read.ListAppender; import java.util.Date; import java.util.List; import org.hl7.fhir.dstu3.model.BooleanType; +import org.hl7.fhir.dstu3.model.Bundle; import org.hl7.fhir.dstu3.model.Extension; import org.hl7.fhir.dstu3.model.IdType; import org.hl7.fhir.dstu3.model.Library; @@ -18,6 +23,8 @@ import org.hl7.fhir.dstu3.model.PlanDefinition; import org.hl7.fhir.instance.model.api.IIdType; import org.junit.jupiter.api.Test; +import org.opencds.cqf.fhir.utility.adapter.Adapter; +import org.slf4j.LoggerFactory; public class ResourceAdapterTest { @Test @@ -66,6 +73,19 @@ void adapter_copy() { @Test void adapter_get_and_set_extension() { + var logger = (Logger) LoggerFactory.getLogger(Adapter.class); + var listAppender = new ListAppender(); + listAppender.start(); + logger.addAppender(listAppender); + var bundle = new Bundle(); + var bundleAdapter = new ResourceAdapter(bundle); + assertFalse(bundleAdapter.hasExtension()); + bundleAdapter.setExtension(List.of(new Extension())); + assertEquals(1, listAppender.list.size()); + assertEquals(Level.DEBUG, listAppender.list.get(0).getLevel()); + bundleAdapter.addExtension(new Extension()); + assertEquals(2, listAppender.list.size()); + assertEquals(Level.DEBUG, listAppender.list.get(1).getLevel()); var resource = new Patient(); var extensionList = List.of(new Extension().setUrl("test-extension-url").setValue(new BooleanType(true))); var adapter = new ResourceAdapter(resource); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/ResourceAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/ResourceAdapterTest.java index f213b7198..edee113a0 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/ResourceAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/ResourceAdapterTest.java @@ -7,10 +7,15 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import ca.uhn.fhir.model.primitive.IdDt; +import ch.qos.logback.classic.Level; +import ch.qos.logback.classic.Logger; +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.read.ListAppender; import java.util.Date; import java.util.List; import org.hl7.fhir.instance.model.api.IIdType; import org.hl7.fhir.r4.model.BooleanType; +import org.hl7.fhir.r4.model.Bundle; import org.hl7.fhir.r4.model.Extension; import org.hl7.fhir.r4.model.IdType; import org.hl7.fhir.r4.model.Library; @@ -18,6 +23,8 @@ import org.hl7.fhir.r4.model.Patient; import org.hl7.fhir.r4.model.PlanDefinition; import org.junit.jupiter.api.Test; +import org.opencds.cqf.fhir.utility.adapter.Adapter; +import org.slf4j.LoggerFactory; public class ResourceAdapterTest { @Test @@ -66,6 +73,19 @@ void adapter_copy() { @Test void adapter_get_and_set_extension() { + var logger = (Logger) LoggerFactory.getLogger(Adapter.class); + var listAppender = new ListAppender(); + listAppender.start(); + logger.addAppender(listAppender); + var bundle = new Bundle(); + var bundleAdapter = new ResourceAdapter(bundle); + assertFalse(bundleAdapter.hasExtension()); + bundleAdapter.setExtension(List.of(new Extension())); + assertEquals(1, listAppender.list.size()); + assertEquals(Level.DEBUG, listAppender.list.get(0).getLevel()); + bundleAdapter.addExtension(new Extension()); + assertEquals(2, listAppender.list.size()); + assertEquals(Level.DEBUG, listAppender.list.get(1).getLevel()); var resource = new Patient(); var extensionList = List.of(new Extension().setUrl("test-extension-url").setValue(new BooleanType(true))); var adapter = new ResourceAdapter(resource); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/EndpointAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/EndpointAdapterTest.java index 4989640fe..400c36025 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/EndpointAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/EndpointAdapterTest.java @@ -8,9 +8,11 @@ import org.junit.jupiter.api.Test; public class EndpointAdapterTest { + private final org.opencds.cqf.fhir.utility.adapter.AdapterFactory adapterFactory = new AdapterFactory(); + @Test void invalid_object_fails() { - assertThrows(IllegalArgumentException.class, () -> new LibraryAdapter(new PlanDefinition())); + assertThrows(IllegalArgumentException.class, () -> new EndpointAdapter(new PlanDefinition())); } @Test @@ -18,7 +20,7 @@ void adapter_get_and_set_address() { var endpoint = new Endpoint(); var address = "123 Test Street"; endpoint.setAddress(address); - var adapter = new EndpointAdapter(endpoint); + var adapter = (EndpointAdapter) adapterFactory.createResource(endpoint); assertEquals(address, adapter.getAddress()); var newAddress = "456 Test Street"; adapter.setAddress(newAddress); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/LibraryAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/LibraryAdapterTest.java index 00f51e14a..0f04db43c 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/LibraryAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/LibraryAdapterTest.java @@ -36,6 +36,7 @@ public class LibraryAdapterTest { private final FhirContext fhirContext = FhirContext.forR5Cached(); + private final org.opencds.cqf.fhir.utility.adapter.AdapterFactory adapterFactory = new AdapterFactory(); @Test void invalid_object_fails() { @@ -47,7 +48,7 @@ void adapter_accepts_visitor() { var spyVisitor = spy(new PackageVisitor(fhirContext)); doReturn(new Bundle()).when(spyVisitor).visit(any(LibraryAdapter.class), any(), any()); IDomainResource library = new Library(); - var adapter = new LibraryAdapter(library); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(library); adapter.accept(spyVisitor, null, null); verify(spyVisitor, times(1)).visit(any(LibraryAdapter.class), any(), any()); } @@ -57,7 +58,7 @@ void adapter_get_and_set_name() { var library = new Library(); var name = "name"; library.setName(name); - var adapter = new LibraryAdapter(library); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(library); assertEquals(name, adapter.getName()); var newName = "name2"; adapter.setName(newName); @@ -69,7 +70,7 @@ void adapter_get_and_set_url() { var library = new Library(); var url = "www.url.com"; library.setUrl(url); - var adapter = new LibraryAdapter(library); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(library); assertEquals(url, adapter.getUrl()); var newUrl = "www.url2.com"; adapter.setUrl(newUrl); @@ -81,7 +82,7 @@ void adapter_get_and_set_version() { var library = new Library(); var version = "1.0.0"; library.setVersion(version); - var adapter = new LibraryAdapter(library); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(library); assertTrue(adapter.hasVersion()); assertEquals(version, adapter.getVersion()); var newVersion = "1.0.1"; @@ -94,7 +95,7 @@ void adapter_get_and_set_status() { var library = new Library(); var status = PublicationStatus.DRAFT; library.setStatus(status); - var adapter = new LibraryAdapter(library); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(library); assertEquals(status.toCode(), adapter.getStatus()); assertThrows(UnprocessableEntityException.class, () -> adapter.setStatus("invalid-status")); var newStatus = PublicationStatus.ACTIVE; @@ -113,7 +114,7 @@ void adapter_get_and_set_dates() { library.setDate(date); library.setApprovalDate(approvalDate); library.setEffectivePeriod(effectivePeriod); - var adapter = new LibraryAdapter(library); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(library); assertEquals(date, adapter.getDate()); assertEquals(approvalDate, adapter.getApprovalDate()); assertEquals(effectivePeriod, adapter.getEffectivePeriod()); @@ -137,7 +138,7 @@ void adapter_get_experimental() { var library = new Library(); var experimental = true; library.setExperimental(experimental); - var adapter = new LibraryAdapter(library); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(library); assertEquals(experimental, adapter.getExperimental()); } @@ -145,7 +146,7 @@ void adapter_get_experimental() { void adapter_set_relatedArtifact() { var library = new Library(); var relatedArtifactList = List.of(new RelatedArtifact()); - var adapter = new LibraryAdapter(library); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(library); adapter.setRelatedArtifact(relatedArtifactList); assertEquals(relatedArtifactList, library.getRelatedArtifact()); assertEquals(relatedArtifactList, adapter.getRelatedArtifact()); @@ -155,8 +156,8 @@ void adapter_set_relatedArtifact() { void adapter_copy() { var library = new Library().setStatus(PublicationStatus.DRAFT); library.setId("library-1"); - var adapter = new LibraryAdapter(library); - var copy = adapter.copy(); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(library); + var copy = (Library) adapter.copy(); var adapterCopy = new LibraryAdapter(copy); adapterCopy.setId(new IdDt("Library", "library-2")); assertNotEquals(library.getId(), copy.getId()); @@ -173,7 +174,7 @@ void adapter_get_all_dependencies() { library.getRelatedArtifactFirstRep().setResource(dependencies.get(1)); library.addDataRequirement().addProfile(dependencies.get(2)); library.addDataRequirement().addCodeFilter().setValueSet(dependencies.get(3)); - var adapter = new LibraryAdapter(library); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(library); var extractedDependencies = adapter.getDependencies(); assertEquals(extractedDependencies.size(), dependencies.size()); extractedDependencies.forEach(dep -> { diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/PlanDefinitionAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/PlanDefinitionAdapterTest.java index 0900a1fdf..2a5eab057 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/PlanDefinitionAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/PlanDefinitionAdapterTest.java @@ -30,6 +30,7 @@ public class PlanDefinitionAdapterTest { private final FhirContext fhirContext = FhirContext.forR5Cached(); + private final org.opencds.cqf.fhir.utility.adapter.AdapterFactory adapterFactory = new AdapterFactory(); @Test void invalid_object_fails() { @@ -41,7 +42,7 @@ void adapter_accepts_visitor() { var spyVisitor = spy(new PackageVisitor(fhirContext)); doReturn(new Bundle()).when(spyVisitor).visit(any(PlanDefinitionAdapter.class), any(), any()); IDomainResource planDef = new PlanDefinition(); - var adapter = new PlanDefinitionAdapter(planDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(planDef); adapter.accept(spyVisitor, null, null); verify(spyVisitor, times(1)).visit(any(PlanDefinitionAdapter.class), any(), any()); } @@ -51,7 +52,7 @@ void adapter_get_and_set_name() { var planDef = new PlanDefinition(); var name = "name"; planDef.setName(name); - var adapter = new PlanDefinitionAdapter(planDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(planDef); assertEquals(name, adapter.getName()); var newName = "name2"; adapter.setName(newName); @@ -63,7 +64,7 @@ void adapter_get_and_set_url() { var planDef = new PlanDefinition(); var url = "www.url.com"; planDef.setUrl(url); - var adapter = new PlanDefinitionAdapter(planDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(planDef); assertEquals(url, adapter.getUrl()); var newUrl = "www.url2.com"; adapter.setUrl(newUrl); @@ -75,7 +76,7 @@ void adapter_get_and_set_version() { var planDef = new PlanDefinition(); var version = "1.0.0"; planDef.setVersion(version); - var adapter = new PlanDefinitionAdapter(planDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(planDef); assertTrue(adapter.hasVersion()); assertEquals(version, adapter.getVersion()); var newVersion = "1.0.1"; @@ -88,7 +89,7 @@ void adapter_get_and_set_status() { var planDef = new PlanDefinition(); var status = PublicationStatus.DRAFT; planDef.setStatus(status); - var adapter = new PlanDefinitionAdapter(planDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(planDef); assertEquals(status.toCode(), adapter.getStatus()); assertThrows(UnprocessableEntityException.class, () -> adapter.setStatus("invalid-status")); var newStatus = PublicationStatus.ACTIVE; @@ -107,7 +108,7 @@ void adapter_get_and_set_dates() { planDef.setDate(date); planDef.setApprovalDate(approvalDate); planDef.setEffectivePeriod(effectivePeriod); - var adapter = new PlanDefinitionAdapter(planDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(planDef); assertEquals(date, adapter.getDate()); assertEquals(approvalDate, adapter.getApprovalDate()); assertEquals(effectivePeriod, adapter.getEffectivePeriod()); @@ -131,7 +132,7 @@ void adapter_get_experimental() { var planDef = new PlanDefinition(); var experimental = true; planDef.setExperimental(experimental); - var adapter = new PlanDefinitionAdapter(planDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(planDef); assertEquals(experimental, adapter.getExperimental()); } @@ -139,7 +140,7 @@ void adapter_get_experimental() { void adapter_set_relatedArtifact() { var planDef = new PlanDefinition(); var relatedArtifactList = List.of(new RelatedArtifact()); - var adapter = new PlanDefinitionAdapter(planDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(planDef); adapter.setRelatedArtifact(relatedArtifactList); assertEquals(relatedArtifactList, planDef.getRelatedArtifact()); assertEquals(relatedArtifactList, adapter.getRelatedArtifact()); @@ -149,8 +150,8 @@ void adapter_set_relatedArtifact() { void adapter_copy() { var planDef = new PlanDefinition().setStatus(PublicationStatus.DRAFT); planDef.setId("plan-1"); - var adapter = new PlanDefinitionAdapter(planDef); - var copy = adapter.copy(); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(planDef); + var copy = (PlanDefinition) adapter.copy(); var adapterCopy = new PlanDefinitionAdapter(copy); adapterCopy.setId(new IdDt("PlanDefinition", "plan-2")); assertNotEquals(planDef.getId(), copy.getId()); @@ -201,7 +202,7 @@ void adapter_get_all_dependencies() { planDef.addExtension(new Extension( "http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-partOf", new CanonicalType(dependencies.get(12)))); action.addAction().setDefinition(new CanonicalType(dependencies.get(13))); - var adapter = new PlanDefinitionAdapter(planDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(planDef); var extractedDependencies = adapter.getDependencies(); assertEquals(extractedDependencies.size(), dependencies.size()); extractedDependencies.forEach(dep -> { diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapterTest.java index 3b92bf0b6..01e43ea0e 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapterTest.java @@ -32,6 +32,7 @@ public class QuestionnaireAdapterTest { private final FhirContext fhirContext = FhirContext.forR5Cached(); + private final org.opencds.cqf.fhir.utility.adapter.AdapterFactory adapterFactory = new AdapterFactory(); @Test void invalid_object_fails() { @@ -43,7 +44,7 @@ void adapter_accepts_visitor() { var spyVisitor = spy(new PackageVisitor(fhirContext)); doReturn(new Bundle()).when(spyVisitor).visit(any(QuestionnaireAdapter.class), any(), any()); IDomainResource questionnaire = new Questionnaire(); - var adapter = new QuestionnaireAdapter(questionnaire); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(questionnaire); adapter.accept(spyVisitor, null, null); verify(spyVisitor, times(1)).visit(any(QuestionnaireAdapter.class), any(), any()); } @@ -53,7 +54,7 @@ void adapter_get_and_set_name() { var questionnaire = new Questionnaire(); var name = "name"; questionnaire.setName(name); - var adapter = new QuestionnaireAdapter(questionnaire); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(questionnaire); assertEquals(name, adapter.getName()); var newName = "name2"; adapter.setName(newName); @@ -65,7 +66,7 @@ void adapter_get_and_set_url() { var questionnaire = new Questionnaire(); var url = "www.url.com"; questionnaire.setUrl(url); - var adapter = new QuestionnaireAdapter(questionnaire); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(questionnaire); assertEquals(url, adapter.getUrl()); var newUrl = "www.url2.com"; adapter.setUrl(newUrl); @@ -77,7 +78,7 @@ void adapter_get_and_set_version() { var questionnaire = new Questionnaire(); var version = "1.0.0"; questionnaire.setVersion(version); - var adapter = new QuestionnaireAdapter(questionnaire); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(questionnaire); assertTrue(adapter.hasVersion()); assertEquals(version, adapter.getVersion()); var newVersion = "1.0.1"; @@ -90,7 +91,7 @@ void adapter_get_and_set_status() { var questionnaire = new Questionnaire(); var status = PublicationStatus.DRAFT; questionnaire.setStatus(status); - var adapter = new QuestionnaireAdapter(questionnaire); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(questionnaire); assertEquals(status.toCode(), adapter.getStatus()); assertThrows(UnprocessableEntityException.class, () -> adapter.setStatus("invalid-status")); var newStatus = PublicationStatus.ACTIVE; @@ -109,7 +110,7 @@ void adapter_get_and_set_dates() { questionnaire.setDate(date); questionnaire.setApprovalDate(approvalDate); questionnaire.setEffectivePeriod(effectivePeriod); - var adapter = new QuestionnaireAdapter(questionnaire); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(questionnaire); assertEquals(date, adapter.getDate()); assertEquals(approvalDate, adapter.getApprovalDate()); assertEquals(effectivePeriod, adapter.getEffectivePeriod()); @@ -133,7 +134,7 @@ void adapter_get_experimental() { var questionnaire = new Questionnaire(); var experimental = true; questionnaire.setExperimental(experimental); - var adapter = new QuestionnaireAdapter(questionnaire); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(questionnaire); assertEquals(experimental, adapter.getExperimental()); } @@ -141,7 +142,7 @@ void adapter_get_experimental() { void adapter_set_relatedArtifact() { var questionnaire = new Questionnaire(); var relatedArtifactList = List.of(new RelatedArtifact()); - var adapter = new QuestionnaireAdapter(questionnaire); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(questionnaire); adapter.setRelatedArtifact(relatedArtifactList); assertEquals(0, adapter.getRelatedArtifact().size()); } @@ -150,8 +151,8 @@ void adapter_set_relatedArtifact() { void adapter_copy() { var questionnaire = new Questionnaire().setStatus(PublicationStatus.DRAFT); questionnaire.setId("plan-1"); - var adapter = new QuestionnaireAdapter(questionnaire); - var copy = adapter.copy(); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(questionnaire); + var copy = (Questionnaire) adapter.copy(); copy.setId("plan-2"); assertNotEquals(questionnaire.getId(), copy.getId()); questionnaire.setStatus(PublicationStatus.ACTIVE); @@ -216,7 +217,7 @@ void adapter_get_all_dependencies() { questionnaire .addItem() .addExtension(Constants.SDC_QUESTIONNAIRE_SUB_QUESTIONNAIRE, new CanonicalType(dependencies.get(14))); - var adapter = new QuestionnaireAdapter(questionnaire); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(questionnaire); var extractedDependencies = adapter.getDependencies(); assertEquals(dependencies.size(), extractedDependencies.size()); extractedDependencies.forEach(dep -> { diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapterTest.java index 1176f0359..5eab66b4b 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapterTest.java @@ -7,10 +7,15 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import ca.uhn.fhir.model.primitive.IdDt; +import ch.qos.logback.classic.Level; +import ch.qos.logback.classic.Logger; +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.read.ListAppender; import java.util.Date; import java.util.List; import org.hl7.fhir.instance.model.api.IIdType; import org.hl7.fhir.r5.model.BooleanType; +import org.hl7.fhir.r5.model.Bundle; import org.hl7.fhir.r5.model.Extension; import org.hl7.fhir.r5.model.IdType; import org.hl7.fhir.r5.model.Library; @@ -18,8 +23,12 @@ import org.hl7.fhir.r5.model.Patient; import org.hl7.fhir.r5.model.PlanDefinition; import org.junit.jupiter.api.Test; +import org.opencds.cqf.fhir.utility.adapter.Adapter; +import org.slf4j.LoggerFactory; public class ResourceAdapterTest { + private final org.opencds.cqf.fhir.utility.adapter.AdapterFactory adapterFactory = new AdapterFactory(); + @Test void invalid_object_fails() { assertThrows(IllegalArgumentException.class, () -> new ResourceAdapter(null)); @@ -31,7 +40,7 @@ void adapter_get_and_set_property() { var resource = new Patient(); var id = new IdDt("patient-1"); resource.setId(id); - var adapter = new ResourceAdapter(resource); + var adapter = adapterFactory.createResource(resource); assertEquals(id.getValue(), ((IIdType) adapter.getSingleProperty("id")).getValue()); var newId = new IdType("patient-2"); adapter.setProperty("id", newId); @@ -49,7 +58,7 @@ void adapter_copy() { var resource = new Patient(); resource.setId("patient-1"); resource.setMeta(new Meta().setLastUpdated(new Date())); - var adapter = new ResourceAdapter(resource); + var adapter = adapterFactory.createResource(resource); var copy = (Patient) adapter.copy(); assertTrue(adapter.equalsDeep(copy)); var newDate = new Date(); @@ -66,9 +75,22 @@ void adapter_copy() { @Test void adapter_get_and_set_extension() { + var logger = (Logger) LoggerFactory.getLogger(Adapter.class); + var listAppender = new ListAppender(); + listAppender.start(); + logger.addAppender(listAppender); + var bundle = new Bundle(); + var bundleAdapter = adapterFactory.createResource(bundle); + assertFalse(bundleAdapter.hasExtension()); + bundleAdapter.setExtension(List.of(new Extension())); + assertEquals(1, listAppender.list.size()); + assertEquals(Level.DEBUG, listAppender.list.get(0).getLevel()); + bundleAdapter.addExtension(new Extension()); + assertEquals(2, listAppender.list.size()); + assertEquals(Level.DEBUG, listAppender.list.get(1).getLevel()); var resource = new Patient(); var extensionList = List.of(new Extension().setUrl("test-extension-url").setValue(new BooleanType(true))); - var adapter = new ResourceAdapter(resource); + var adapter = adapterFactory.createResource(resource); adapter.setExtension(extensionList); assertTrue(adapter.hasExtension()); assertEquals(extensionList, resource.getExtension()); @@ -80,7 +102,7 @@ void adapter_get_and_set_extension() { void adapter_get_contained() { var resource = new PlanDefinition(); resource.addContained(new Library()); - var adapter = new ResourceAdapter(resource); + var adapter = adapterFactory.createResource(resource); assertTrue(adapter.hasContained()); assertNotNull(adapter.getContained()); assertFalse(adapter.hasContained(adapter.getContained().get(0))); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapterTest.java index c2d3579cc..5ed8b6367 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapterTest.java @@ -31,6 +31,7 @@ public class StructureDefinitionAdapterTest { private final FhirContext fhirContext = FhirContext.forR5Cached(); + private final org.opencds.cqf.fhir.utility.adapter.AdapterFactory adapterFactory = new AdapterFactory(); @Test void invalid_object_fails() { @@ -42,7 +43,7 @@ void adapter_accepts_visitor() { var spyVisitor = spy(new PackageVisitor(fhirContext)); doReturn(new Bundle()).when(spyVisitor).visit(any(StructureDefinitionAdapter.class), any(), any()); IDomainResource structureDef = new StructureDefinition(); - var adapter = new StructureDefinitionAdapter(structureDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(structureDef); assertEquals(structureDef, adapter.get()); adapter.accept(spyVisitor, null, null); verify(spyVisitor, times(1)).visit(any(StructureDefinitionAdapter.class), any(), any()); @@ -53,7 +54,7 @@ void adapter_get_and_set_name() { var structureDef = new StructureDefinition(); var name = "name"; structureDef.setName(name); - var adapter = new StructureDefinitionAdapter(structureDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(structureDef); assertEquals(name, adapter.getName()); var newName = "name2"; adapter.setName(newName); @@ -65,7 +66,7 @@ void adapter_get_and_set_url() { var structureDef = new StructureDefinition(); var url = "www.url.com"; structureDef.setUrl(url); - var adapter = new StructureDefinitionAdapter(structureDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(structureDef); assertTrue(adapter.hasUrl()); assertEquals(url, adapter.getUrl()); var newUrl = "www.url2.com"; @@ -79,7 +80,7 @@ void adapter_get_and_set_version() { var structureDef = new StructureDefinition(); var version = "1.0.0"; structureDef.setVersion(version); - var adapter = new StructureDefinitionAdapter(structureDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(structureDef); assertTrue(adapter.hasVersion()); assertEquals(version, adapter.getVersion()); var newVersion = "1.0.1"; @@ -92,7 +93,7 @@ void adapter_get_and_set_status() { var structureDef = new StructureDefinition(); var status = PublicationStatus.DRAFT; structureDef.setStatus(status); - var adapter = new StructureDefinitionAdapter(structureDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(structureDef); assertEquals(status.toCode(), adapter.getStatus()); assertThrows(UnprocessableEntityException.class, () -> adapter.setStatus("invalid-status")); var newStatus = PublicationStatus.ACTIVE; @@ -109,7 +110,7 @@ void adapter_get_and_set_dates() { .setStart(java.sql.Date.valueOf(LocalDate.parse("2020-01-01"))) .setEnd(java.sql.Date.valueOf(LocalDate.parse("2020-12-31"))); structureDef.setDate(date); - var adapter = new StructureDefinitionAdapter(structureDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(structureDef); assertEquals(date, adapter.getDate()); assertEquals(null, adapter.getApprovalDate()); assertNotEquals(effectivePeriod, adapter.getEffectivePeriod()); @@ -136,7 +137,7 @@ void adapter_get_experimental() { var structureDef = new StructureDefinition(); var experimental = true; structureDef.setExperimental(experimental); - var adapter = new StructureDefinitionAdapter(structureDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(structureDef); assertEquals(experimental, adapter.getExperimental()); } @@ -144,7 +145,7 @@ void adapter_get_experimental() { void adapter_set_relatedArtifact() { var structureDef = new StructureDefinition(); var relatedArtifactList = List.of(new RelatedArtifact()); - var adapter = new StructureDefinitionAdapter(structureDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(structureDef); adapter.setRelatedArtifact(relatedArtifactList); assertEquals(0, adapter.getRelatedArtifact().size()); assertThrows(UnprocessableEntityException.class, () -> adapter.getRelatedArtifactsOfType("invalid")); @@ -155,8 +156,8 @@ void adapter_set_relatedArtifact() { void adapter_copy() { var structureDef = new StructureDefinition().setStatus(PublicationStatus.DRAFT); structureDef.setId("plan-1"); - var adapter = new StructureDefinitionAdapter(structureDef); - var copy = adapter.copy(); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(structureDef); + var copy = (StructureDefinition) adapter.copy(); copy.setId("plan-2"); assertNotEquals(structureDef.getId(), copy.getId()); structureDef.setStatus(PublicationStatus.ACTIVE); @@ -188,7 +189,7 @@ void adapter_get_all_dependencies() { .getDifferential() .addElement() .setBinding(new ElementDefinitionBindingComponent().setValueSet(dependencies.get(7))); - var adapter = new StructureDefinitionAdapter(structureDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(structureDef); var extractedDependencies = adapter.getDependencies(); assertEquals(dependencies.size(), extractedDependencies.size()); extractedDependencies.forEach(dep -> { diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ValueSetAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ValueSetAdapterTest.java index 08fbcc329..e37f750a2 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ValueSetAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ValueSetAdapterTest.java @@ -27,6 +27,7 @@ public class ValueSetAdapterTest { private final FhirContext fhirContext = FhirContext.forR5Cached(); + private final org.opencds.cqf.fhir.utility.adapter.AdapterFactory adapterFactory = new AdapterFactory(); @Test void invalid_object_fails() { @@ -38,7 +39,7 @@ void adapter_accepts_visitor() { var spyVisitor = spy(new PackageVisitor(fhirContext)); doReturn(new Bundle()).when(spyVisitor).visit(any(ValueSetAdapter.class), any(), any()); IDomainResource valueSet = new ValueSet(); - var adapter = new ValueSetAdapter(valueSet); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(valueSet); assertEquals(valueSet, adapter.get()); adapter.accept(spyVisitor, null, null); verify(spyVisitor, times(1)).visit(any(ValueSetAdapter.class), any(), any()); @@ -49,7 +50,7 @@ void adapter_get_and_set_name() { var valueSet = new ValueSet(); var name = "name"; valueSet.setName(name); - var adapter = new ValueSetAdapter(valueSet); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(valueSet); assertEquals(name, adapter.getName()); var newName = "name2"; adapter.setName(newName); @@ -61,7 +62,7 @@ void adapter_get_and_set_url() { var valueSet = new ValueSet(); var url = "www.url.com"; valueSet.setUrl(url); - var adapter = new ValueSetAdapter(valueSet); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(valueSet); assertTrue(adapter.hasUrl()); assertEquals(url, adapter.getUrl()); var newUrl = "www.url2.com"; @@ -75,7 +76,7 @@ void adapter_get_and_set_version() { var valueSet = new ValueSet(); var version = "1.0.0"; valueSet.setVersion(version); - var adapter = new ValueSetAdapter(valueSet); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(valueSet); assertTrue(adapter.hasVersion()); assertEquals(version, adapter.getVersion()); var newVersion = "1.0.1"; @@ -88,7 +89,7 @@ void adapter_get_and_set_status() { var valueSet = new ValueSet(); var status = PublicationStatus.DRAFT; valueSet.setStatus(status); - var adapter = new ValueSetAdapter(valueSet); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(valueSet); assertEquals(status.toCode(), adapter.getStatus()); assertThrows(UnprocessableEntityException.class, () -> adapter.setStatus("invalid-status")); var newStatus = PublicationStatus.ACTIVE; @@ -107,7 +108,7 @@ void adapter_get_and_set_dates() { valueSet.setDate(date); valueSet.setApprovalDate(approvalDate); valueSet.setEffectivePeriod(effectivePeriod); - var adapter = new ValueSetAdapter(valueSet); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(valueSet); assertEquals(date, adapter.getDate()); assertEquals(approvalDate, adapter.getApprovalDate()); assertEquals(effectivePeriod, adapter.getEffectivePeriod()); @@ -131,7 +132,7 @@ void adapter_get_experimental() { var valueSet = new ValueSet(); var experimental = true; valueSet.setExperimental(experimental); - var adapter = new ValueSetAdapter(valueSet); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(valueSet); assertEquals(experimental, adapter.getExperimental()); } @@ -139,7 +140,7 @@ void adapter_get_experimental() { void adapter_set_relatedArtifact() { var valueSet = new ValueSet(); var relatedArtifactList = List.of(new RelatedArtifact()); - var adapter = new ValueSetAdapter(valueSet); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(valueSet); adapter.setRelatedArtifact(relatedArtifactList); assertEquals(relatedArtifactList, valueSet.getRelatedArtifact()); assertEquals(relatedArtifactList, adapter.getRelatedArtifact()); @@ -149,8 +150,8 @@ void adapter_set_relatedArtifact() { void adapter_copy() { var valueSet = new ValueSet().setStatus(PublicationStatus.DRAFT); valueSet.setId("valueset-1"); - var adapter = new ValueSetAdapter(valueSet); - var copy = adapter.copy(); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(valueSet); + var copy = (ValueSet) adapter.copy(); copy.setId("valueset-2"); assertNotEquals(valueSet.getId(), copy.getId()); valueSet.setStatus(PublicationStatus.ACTIVE); @@ -162,7 +163,7 @@ void adapter_get_all_dependencies() { var dependencies = List.of("profileRef"); var valueSet = new ValueSet(); valueSet.getMeta().addProfile(dependencies.get(0)); - var adapter = new ValueSetAdapter(valueSet); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(valueSet); var extractedDependencies = adapter.getDependencies(); assertEquals(dependencies.size(), extractedDependencies.size()); extractedDependencies.forEach(dep -> { From f3a23936f96d3bcaa24a1494615c541123f38b92 Mon Sep 17 00:00:00 2001 From: Brenin Rhodes Date: Thu, 8 Aug 2024 15:17:04 -0600 Subject: [PATCH 25/38] cleanup and add tests --- .../adapter/dstu3/EndpointAdapterTest.java | 4 +- .../dstu3/KnowledgeArtifactAdapterTest.java | 146 +++++++++++++++++ .../adapter/dstu3/LibraryAdapterTest.java | 31 ++-- .../dstu3/PlanDefinitionAdapterTest.java | 23 +-- .../dstu3/QuestionnaireAdapterTest.java | 23 +-- .../adapter/dstu3/ResourceAdapterTest.java | 10 +- .../dstu3/StructureDefinitionAdapterTest.java | 23 +-- .../adapter/dstu3/ValueSetAdapterTest.java | 23 +-- .../adapter/r4/EndpointAdapterTest.java | 6 +- .../r4/KnowledgeArtifactAdapterTest.java | 146 +++++++++++++++++ .../adapter/r4/LibraryAdapterTest.java | 31 ++-- .../adapter/r4/PlanDefinitionAdapterTest.java | 23 +-- .../adapter/r4/QuestionnaireAdapterTest.java | 23 +-- .../adapter/r4/ResourceAdapterTest.java | 10 +- .../r4/StructureDefinitionAdapterTest.java | 23 +-- .../adapter/r4/ValueSetAdapterTest.java | 23 +-- .../adapter/r5/EndpointAdapterTest.java | 2 +- .../r5/KnowledgeArtifactAdapterTest.java | 153 ++++++++++++++++++ .../adapter/r5/LibraryAdapterTest.java | 2 +- .../adapter/r5/PlanDefinitionAdapterTest.java | 2 +- .../adapter/r5/QuestionnaireAdapterTest.java | 6 +- .../adapter/r5/ResourceAdapterTest.java | 2 +- .../r5/StructureDefinitionAdapterTest.java | 2 +- .../adapter/r5/ValueSetAdapterTest.java | 2 +- 24 files changed, 601 insertions(+), 138 deletions(-) create mode 100644 cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/KnowledgeArtifactAdapterTest.java create mode 100644 cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/KnowledgeArtifactAdapterTest.java create mode 100644 cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/KnowledgeArtifactAdapterTest.java diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/EndpointAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/EndpointAdapterTest.java index 847525e2b..b9df774e8 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/EndpointAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/EndpointAdapterTest.java @@ -8,6 +8,8 @@ import org.junit.jupiter.api.Test; public class EndpointAdapterTest { + private final org.opencds.cqf.fhir.utility.adapter.AdapterFactory adapterFactory = new AdapterFactory(); + @Test void invalid_object_fails() { assertThrows(IllegalArgumentException.class, () -> new LibraryAdapter(new PlanDefinition())); @@ -18,7 +20,7 @@ void adapter_get_and_set_address() { var endpoint = new Endpoint(); var address = "123 Test Street"; endpoint.setAddress(address); - var adapter = new EndpointAdapter(endpoint); + var adapter = (EndpointAdapter) adapterFactory.createResource(endpoint); assertEquals(address, adapter.getAddress()); var newAddress = "456 Test Street"; adapter.setAddress(newAddress); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/KnowledgeArtifactAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/KnowledgeArtifactAdapterTest.java new file mode 100644 index 000000000..a4d810f4e --- /dev/null +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/KnowledgeArtifactAdapterTest.java @@ -0,0 +1,146 @@ +package org.opencds.cqf.fhir.utility.adapter.dstu3; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; +import java.util.Date; +import java.util.List; +import org.hl7.fhir.dstu3.model.Bundle; +import org.hl7.fhir.dstu3.model.CompartmentDefinition; +import org.hl7.fhir.dstu3.model.Enumerations.PublicationStatus; +import org.hl7.fhir.dstu3.model.RelatedArtifact; +import org.hl7.fhir.instance.model.api.IDomainResource; +import org.junit.jupiter.api.Test; +import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; + +class KnowledgeArtifactAdapterTest { + private final FhirContext fhirContext = FhirContext.forDstu3Cached(); + private final org.opencds.cqf.fhir.utility.adapter.AdapterFactory adapterFactory = new AdapterFactory(); + + @Test + void invalid_object_fails() { + assertThrows( + UnprocessableEntityException.class, + () -> adapterFactory.createKnowledgeArtifactAdapter(new org.hl7.fhir.r4.model.Library())); + } + + @Test + void test() { + var resource = new CompartmentDefinition(); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(resource); + assertNotNull(adapter); + } + + @Test + void adapter_accepts_visitor() { + var spyVisitor = spy(new PackageVisitor(fhirContext)); + doReturn(new Bundle()).when(spyVisitor).visit(any(KnowledgeArtifactAdapter.class), any(), any()); + IDomainResource def = new CompartmentDefinition(); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(def); + adapter.accept(spyVisitor, null, null); + verify(spyVisitor, times(1)).visit(any(KnowledgeArtifactAdapter.class), any(), any()); + } + + @Test + void adapter_get_and_set_name() { + var def = new CompartmentDefinition(); + var name = "name"; + def.setName(name); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(def); + assertEquals(name, adapter.getName()); + var newName = "name2"; + adapter.setName(newName); + assertEquals(newName, def.getName()); + } + + @Test + void adapter_get_and_set_url() { + var def = new CompartmentDefinition(); + var url = "www.url.com"; + def.setUrl(url); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(def); + assertEquals(url, adapter.getUrl()); + var newUrl = "www.url2.com"; + adapter.setUrl(newUrl); + assertEquals(newUrl, def.getUrl()); + } + + @Test + void adapter_get_and_set_version() { + var def = new CompartmentDefinition(); + var version = "1.0.0"; + def.setVersion(version); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(def); + assertTrue(adapter.hasVersion()); + assertEquals(version, adapter.getVersion()); + var newVersion = "1.0.1"; + adapter.setVersion(newVersion); + assertEquals(newVersion, def.getVersion()); + } + + @Test + void adapter_get_and_set_status() { + var def = new CompartmentDefinition(); + var status = PublicationStatus.DRAFT; + def.setStatus(status); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(def); + assertEquals(status.toCode(), adapter.getStatus()); + assertThrows(UnprocessableEntityException.class, () -> adapter.setStatus("invalid-status")); + var newStatus = PublicationStatus.ACTIVE; + adapter.setStatus(newStatus.toCode()); + assertEquals(newStatus, PublicationStatus.fromCode(adapter.getStatus())); + } + + @Test + void adapter_get_and_set_dates() { + var def = new CompartmentDefinition(); + var date = new Date(); + def.setDate(date); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(def); + assertEquals(date, adapter.getDate()); + var newDate = new Date(); + newDate.setTime(100); + adapter.setDate(newDate); + assertEquals(newDate, def.getDate()); + } + + @Test + void adapter_get_experimental() { + var def = new CompartmentDefinition(); + var experimental = true; + def.setExperimental(experimental); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(def); + assertEquals(experimental, adapter.getExperimental()); + } + + @Test + void adapter_set_relatedArtifact() { + var def = new CompartmentDefinition(); + var relatedArtifactList = List.of(new RelatedArtifact()); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(def); + adapter.setRelatedArtifact(relatedArtifactList); + assertEquals(0, adapter.getRelatedArtifact().size()); + } + + @Test + void adapter_copy() { + var def = new CompartmentDefinition().setStatus(PublicationStatus.DRAFT); + def.setId("def-1"); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(def); + var copy = (CompartmentDefinition) adapter.copy(); + copy.setId("def-2"); + assertNotEquals(def.getId(), copy.getId()); + def.setStatus(PublicationStatus.ACTIVE); + assertNotEquals(adapter.getStatus(), copy.getStatus()); + } +} diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/LibraryAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/LibraryAdapterTest.java index a64b179cf..13a512447 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/LibraryAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/LibraryAdapterTest.java @@ -36,6 +36,7 @@ public class LibraryAdapterTest { private final FhirContext fhirContext = FhirContext.forDstu3Cached(); + private final org.opencds.cqf.fhir.utility.adapter.AdapterFactory adapterFactory = new AdapterFactory(); @Test void invalid_object_fails() { @@ -47,7 +48,7 @@ void adapter_accepts_visitor() { var spyVisitor = spy(new PackageVisitor(fhirContext)); doReturn(new Bundle()).when(spyVisitor).visit(any(LibraryAdapter.class), any(), any()); IDomainResource library = new Library(); - var adapter = new LibraryAdapter(library); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(library); adapter.accept(spyVisitor, null, null); verify(spyVisitor, times(1)).visit(any(LibraryAdapter.class), any(), any()); } @@ -57,7 +58,7 @@ void adapter_get_and_set_name() { var library = new Library(); var name = "name"; library.setName(name); - var adapter = new LibraryAdapter(library); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(library); assertEquals(name, adapter.getName()); var newName = "name2"; adapter.setName(newName); @@ -69,7 +70,7 @@ void adapter_get_and_set_url() { var library = new Library(); var url = "www.url.com"; library.setUrl(url); - var adapter = new LibraryAdapter(library); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(library); assertEquals(url, adapter.getUrl()); var newUrl = "www.url2.com"; adapter.setUrl(newUrl); @@ -81,7 +82,7 @@ void adapter_get_and_set_version() { var library = new Library(); var version = "1.0.0"; library.setVersion(version); - var adapter = new LibraryAdapter(library); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(library); assertTrue(adapter.hasVersion()); assertEquals(version, adapter.getVersion()); var newVersion = "1.0.1"; @@ -94,7 +95,7 @@ void adapter_get_and_set_status() { var library = new Library(); var status = PublicationStatus.DRAFT; library.setStatus(status); - var adapter = new LibraryAdapter(library); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(library); assertEquals(status.toCode(), adapter.getStatus()); assertThrows(UnprocessableEntityException.class, () -> adapter.setStatus("invalid-status")); var newStatus = PublicationStatus.ACTIVE; @@ -113,7 +114,7 @@ void adapter_get_and_set_dates() { library.setDate(date); library.setApprovalDate(approvalDate); library.setEffectivePeriod(effectivePeriod); - var adapter = new LibraryAdapter(library); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(library); assertEquals(date, adapter.getDate()); assertEquals(approvalDate, adapter.getApprovalDate()); assertEquals(effectivePeriod, adapter.getEffectivePeriod()); @@ -137,7 +138,7 @@ void adapter_get_experimental() { var library = new Library(); var experimental = true; library.setExperimental(experimental); - var adapter = new LibraryAdapter(library); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(library); assertEquals(experimental, adapter.getExperimental()); } @@ -145,7 +146,7 @@ void adapter_get_experimental() { void adapter_set_relatedArtifact() { var library = new Library(); var relatedArtifactList = List.of(new RelatedArtifact()); - var adapter = new LibraryAdapter(library); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(library); adapter.setRelatedArtifact(relatedArtifactList); assertEquals(relatedArtifactList, library.getRelatedArtifact()); assertEquals(relatedArtifactList, adapter.getRelatedArtifact()); @@ -155,8 +156,8 @@ void adapter_set_relatedArtifact() { void adapter_copy() { var library = new Library().setStatus(PublicationStatus.DRAFT); library.setId("library-1"); - var adapter = new LibraryAdapter(library); - var copy = adapter.copy(); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(library); + var copy = (Library) adapter.copy(); var adapterCopy = new LibraryAdapter(copy); adapterCopy.setId(new IdDt("Library", "library-2")); assertNotEquals(library.getId(), copy.getId()); @@ -173,7 +174,7 @@ void adapter_get_all_dependencies() { library.getRelatedArtifactFirstRep().setResource(new Reference(dependencies.get(1))); library.addDataRequirement().addProfile(dependencies.get(2)); library.addDataRequirement().addCodeFilter().setValueSet(new Reference(dependencies.get(3))); - var adapter = new LibraryAdapter(library); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(library); var extractedDependencies = adapter.getDependencies(); assertEquals(extractedDependencies.size(), dependencies.size()); extractedDependencies.forEach(dep -> { @@ -184,7 +185,7 @@ void adapter_get_all_dependencies() { @Test void adapter_get_and_set_content() { var library = new Library(); - var adapter = new LibraryAdapter(library); + var adapter = (LibraryAdapter) adapterFactory.createKnowledgeArtifactAdapter(library); var contentList = new ArrayList(); contentList.add(new Attachment().setContentType("text/cql").setData(new byte[10])); adapter.setContent(contentList); @@ -199,7 +200,7 @@ void adapter_get_and_set_content() { void adapter_get_and_set_type() { var type = new CodeableConcept(new Coding("www.test.com", "test", "Test")); var library = new Library().setType(type); - var adapter = new LibraryAdapter(library); + var adapter = (LibraryAdapter) adapterFactory.createKnowledgeArtifactAdapter(library); assertEquals(type, adapter.getType()); assertThrows(UnprocessableEntityException.class, () -> adapter.setType("test")); var newType = new CodeableConcept(new Coding("http://hl7.org/fhir/ValueSet/library-type", "logic-library", "")); @@ -215,7 +216,7 @@ void adapter_get_and_set_type() { @Test void adapter_get_and_set_dataRequirement() { var library = new Library(); - var adapter = new LibraryAdapter(library); + var adapter = (LibraryAdapter) adapterFactory.createKnowledgeArtifactAdapter(library); var dataRequirements = new ArrayList(); dataRequirements.add(new DataRequirement().setType("Patient")); adapter.setDataRequirement(dataRequirements); @@ -230,7 +231,7 @@ void adapter_get_useContext() { var library = new Library(); var useContext = new UsageContext().setCode(new Coding("www.test.com", "test", "Test")); library.setUseContext(Collections.singletonList(useContext)); - var adapter = new LibraryAdapter(library); + var adapter = (LibraryAdapter) adapterFactory.createKnowledgeArtifactAdapter(library); assertEquals(useContext, adapter.getUseContext().get(0)); } } diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/PlanDefinitionAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/PlanDefinitionAdapterTest.java index 4b9b0c8f2..4696f5cab 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/PlanDefinitionAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/PlanDefinitionAdapterTest.java @@ -32,6 +32,7 @@ public class PlanDefinitionAdapterTest { private final FhirContext fhirContext = FhirContext.forDstu3Cached(); + private final org.opencds.cqf.fhir.utility.adapter.AdapterFactory adapterFactory = new AdapterFactory(); @Test void invalid_object_fails() { @@ -43,7 +44,7 @@ void adapter_accepts_visitor() { var spyVisitor = spy(new PackageVisitor(fhirContext)); doReturn(new Bundle()).when(spyVisitor).visit(any(PlanDefinitionAdapter.class), any(), any()); IDomainResource planDef = new PlanDefinition(); - var adapter = new PlanDefinitionAdapter(planDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(planDef); adapter.accept(spyVisitor, null, null); verify(spyVisitor, times(1)).visit(any(PlanDefinitionAdapter.class), any(), any()); } @@ -53,7 +54,7 @@ void adapter_get_and_set_name() { var planDef = new PlanDefinition(); var name = "name"; planDef.setName(name); - var adapter = new PlanDefinitionAdapter(planDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(planDef); assertEquals(name, adapter.getName()); var newName = "name2"; adapter.setName(newName); @@ -65,7 +66,7 @@ void adapter_get_and_set_url() { var planDef = new PlanDefinition(); var url = "www.url.com"; planDef.setUrl(url); - var adapter = new PlanDefinitionAdapter(planDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(planDef); assertEquals(url, adapter.getUrl()); var newUrl = "www.url2.com"; adapter.setUrl(newUrl); @@ -77,7 +78,7 @@ void adapter_get_and_set_version() { var planDef = new PlanDefinition(); var version = "1.0.0"; planDef.setVersion(version); - var adapter = new PlanDefinitionAdapter(planDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(planDef); assertTrue(adapter.hasVersion()); assertEquals(version, adapter.getVersion()); var newVersion = "1.0.1"; @@ -90,7 +91,7 @@ void adapter_get_and_set_status() { var planDef = new PlanDefinition(); var status = PublicationStatus.DRAFT; planDef.setStatus(status); - var adapter = new PlanDefinitionAdapter(planDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(planDef); assertEquals(status.toCode(), adapter.getStatus()); assertThrows(UnprocessableEntityException.class, () -> adapter.setStatus("invalid-status")); var newStatus = PublicationStatus.ACTIVE; @@ -109,7 +110,7 @@ void adapter_get_and_set_dates() { planDef.setDate(date); planDef.setApprovalDate(approvalDate); planDef.setEffectivePeriod(effectivePeriod); - var adapter = new PlanDefinitionAdapter(planDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(planDef); assertEquals(date, adapter.getDate()); assertEquals(approvalDate, adapter.getApprovalDate()); assertEquals(effectivePeriod, adapter.getEffectivePeriod()); @@ -133,7 +134,7 @@ void adapter_get_experimental() { var planDef = new PlanDefinition(); var experimental = true; planDef.setExperimental(experimental); - var adapter = new PlanDefinitionAdapter(planDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(planDef); assertEquals(experimental, adapter.getExperimental()); } @@ -141,7 +142,7 @@ void adapter_get_experimental() { void adapter_set_relatedArtifact() { var planDef = new PlanDefinition(); var relatedArtifactList = List.of(new RelatedArtifact()); - var adapter = new PlanDefinitionAdapter(planDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(planDef); adapter.setRelatedArtifact(relatedArtifactList); assertEquals(relatedArtifactList, planDef.getRelatedArtifact()); assertEquals(relatedArtifactList, adapter.getRelatedArtifact()); @@ -151,8 +152,8 @@ void adapter_set_relatedArtifact() { void adapter_copy() { var planDef = new PlanDefinition().setStatus(PublicationStatus.DRAFT); planDef.setId("plan-1"); - var adapter = new PlanDefinitionAdapter(planDef); - var copy = adapter.copy(); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(planDef); + var copy = (PlanDefinition) adapter.copy(); var adapterCopy = new PlanDefinitionAdapter(copy); adapterCopy.setId(new IdDt("PlanDefinition", "plan-2")); assertNotEquals(planDef.getId(), copy.getId()); @@ -201,7 +202,7 @@ void adapter_get_all_dependencies() { planDef.addExtension(new Extension( "http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-partOf", new UriType(dependencies.get(10)))); action.addAction().getDefinition().setReference(dependencies.get(11)); - var adapter = new PlanDefinitionAdapter(planDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(planDef); var extractedDependencies = adapter.getDependencies(); assertEquals(extractedDependencies.size(), dependencies.size()); extractedDependencies.forEach(dep -> { diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/QuestionnaireAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/QuestionnaireAdapterTest.java index 45a28f320..7d3e2cc16 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/QuestionnaireAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/QuestionnaireAdapterTest.java @@ -29,6 +29,7 @@ public class QuestionnaireAdapterTest { private final FhirContext fhirContext = FhirContext.forDstu3Cached(); + private final org.opencds.cqf.fhir.utility.adapter.AdapterFactory adapterFactory = new AdapterFactory(); @Test void invalid_object_fails() { @@ -40,7 +41,7 @@ void adapter_accepts_visitor() { var spyVisitor = spy(new PackageVisitor(fhirContext)); doReturn(new Bundle()).when(spyVisitor).visit(any(QuestionnaireAdapter.class), any(), any()); IDomainResource questionnaire = new Questionnaire(); - var adapter = new QuestionnaireAdapter(questionnaire); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(questionnaire); adapter.accept(spyVisitor, null, null); verify(spyVisitor, times(1)).visit(any(QuestionnaireAdapter.class), any(), any()); } @@ -50,7 +51,7 @@ void adapter_get_and_set_name() { var questionnaire = new Questionnaire(); var name = "name"; questionnaire.setName(name); - var adapter = new QuestionnaireAdapter(questionnaire); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(questionnaire); assertEquals(name, adapter.getName()); var newName = "name2"; adapter.setName(newName); @@ -62,7 +63,7 @@ void adapter_get_and_set_url() { var questionnaire = new Questionnaire(); var url = "www.url.com"; questionnaire.setUrl(url); - var adapter = new QuestionnaireAdapter(questionnaire); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(questionnaire); assertEquals(url, adapter.getUrl()); var newUrl = "www.url2.com"; adapter.setUrl(newUrl); @@ -74,7 +75,7 @@ void adapter_get_and_set_version() { var questionnaire = new Questionnaire(); var version = "1.0.0"; questionnaire.setVersion(version); - var adapter = new QuestionnaireAdapter(questionnaire); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(questionnaire); assertTrue(adapter.hasVersion()); assertEquals(version, adapter.getVersion()); var newVersion = "1.0.1"; @@ -87,7 +88,7 @@ void adapter_get_and_set_status() { var questionnaire = new Questionnaire(); var status = PublicationStatus.DRAFT; questionnaire.setStatus(status); - var adapter = new QuestionnaireAdapter(questionnaire); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(questionnaire); assertEquals(status.toCode(), adapter.getStatus()); assertThrows(UnprocessableEntityException.class, () -> adapter.setStatus("invalid-status")); var newStatus = PublicationStatus.ACTIVE; @@ -106,7 +107,7 @@ void adapter_get_and_set_dates() { questionnaire.setDate(date); questionnaire.setApprovalDate(approvalDate); questionnaire.setEffectivePeriod(effectivePeriod); - var adapter = new QuestionnaireAdapter(questionnaire); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(questionnaire); assertEquals(date, adapter.getDate()); assertEquals(approvalDate, adapter.getApprovalDate()); assertEquals(effectivePeriod, adapter.getEffectivePeriod()); @@ -130,7 +131,7 @@ void adapter_get_experimental() { var questionnaire = new Questionnaire(); var experimental = true; questionnaire.setExperimental(experimental); - var adapter = new QuestionnaireAdapter(questionnaire); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(questionnaire); assertEquals(experimental, adapter.getExperimental()); } @@ -138,7 +139,7 @@ void adapter_get_experimental() { void adapter_set_relatedArtifact() { var questionnaire = new Questionnaire(); var relatedArtifactList = List.of(new RelatedArtifact()); - var adapter = new QuestionnaireAdapter(questionnaire); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(questionnaire); adapter.setRelatedArtifact(relatedArtifactList); assertEquals(0, adapter.getRelatedArtifact().size()); } @@ -147,8 +148,8 @@ void adapter_set_relatedArtifact() { void adapter_copy() { var questionnaire = new Questionnaire().setStatus(PublicationStatus.DRAFT); questionnaire.setId("plan-1"); - var adapter = new QuestionnaireAdapter(questionnaire); - var copy = adapter.copy(); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(questionnaire); + var copy = (Questionnaire) adapter.copy(); copy.setId("plan-2"); assertNotEquals(questionnaire.getId(), copy.getId()); questionnaire.setStatus(PublicationStatus.ACTIVE); @@ -183,7 +184,7 @@ void adapter_get_all_dependencies() { // Expression().setReference(dependencies.get(2))); // questionnaire.addExtension(variableExt); questionnaire.addItem().setDefinition(dependencies.get(2) + "#Observation"); - var adapter = new QuestionnaireAdapter(questionnaire); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(questionnaire); var extractedDependencies = adapter.getDependencies(); assertEquals(dependencies.size(), extractedDependencies.size()); extractedDependencies.forEach(dep -> { diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapterTest.java index ddb946122..77bd1223a 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapterTest.java @@ -27,6 +27,8 @@ import org.slf4j.LoggerFactory; public class ResourceAdapterTest { + private final org.opencds.cqf.fhir.utility.adapter.AdapterFactory adapterFactory = new AdapterFactory(); + @Test void invalid_object_fails() { assertThrows(IllegalArgumentException.class, () -> new ResourceAdapter(null)); @@ -38,7 +40,7 @@ void adapter_get_and_set_property() { var resource = new Patient(); var id = new IdDt("patient-1"); resource.setId(id); - var adapter = new ResourceAdapter(resource); + var adapter = adapterFactory.createResource(resource); assertEquals(id.getValue(), ((IIdType) adapter.getSingleProperty("id")).getValue()); var newId = new IdType("patient-2"); adapter.setProperty("id", newId); @@ -56,7 +58,7 @@ void adapter_copy() { var resource = new Patient(); resource.setId("patient-1"); resource.setMeta(new Meta().setLastUpdated(new Date())); - var adapter = new ResourceAdapter(resource); + var adapter = adapterFactory.createResource(resource); var copy = (Patient) adapter.copy(); assertTrue(adapter.equalsDeep(copy)); var newDate = new Date(); @@ -88,7 +90,7 @@ void adapter_get_and_set_extension() { assertEquals(Level.DEBUG, listAppender.list.get(1).getLevel()); var resource = new Patient(); var extensionList = List.of(new Extension().setUrl("test-extension-url").setValue(new BooleanType(true))); - var adapter = new ResourceAdapter(resource); + var adapter = adapterFactory.createResource(resource); adapter.setExtension(extensionList); assertTrue(adapter.hasExtension()); assertEquals(extensionList, resource.getExtension()); @@ -100,7 +102,7 @@ void adapter_get_and_set_extension() { void adapter_get_contained() { var resource = new PlanDefinition(); resource.addContained(new Library()); - var adapter = new ResourceAdapter(resource); + var adapter = adapterFactory.createResource(resource); assertTrue(adapter.hasContained()); assertNotNull(adapter.getContained()); assertFalse(adapter.hasContained(adapter.getContained().get(0))); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/StructureDefinitionAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/StructureDefinitionAdapterTest.java index 3e27917b1..9605aab35 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/StructureDefinitionAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/StructureDefinitionAdapterTest.java @@ -29,6 +29,7 @@ public class StructureDefinitionAdapterTest { private final FhirContext fhirContext = FhirContext.forDstu3Cached(); + private final org.opencds.cqf.fhir.utility.adapter.AdapterFactory adapterFactory = new AdapterFactory(); @Test void invalid_object_fails() { @@ -40,7 +41,7 @@ void adapter_accepts_visitor() { var spyVisitor = spy(new PackageVisitor(fhirContext)); doReturn(new Bundle()).when(spyVisitor).visit(any(StructureDefinitionAdapter.class), any(), any()); IDomainResource structureDef = new StructureDefinition(); - var adapter = new StructureDefinitionAdapter(structureDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(structureDef); assertEquals(structureDef, adapter.get()); adapter.accept(spyVisitor, null, null); verify(spyVisitor, times(1)).visit(any(StructureDefinitionAdapter.class), any(), any()); @@ -51,7 +52,7 @@ void adapter_get_and_set_name() { var structureDef = new StructureDefinition(); var name = "name"; structureDef.setName(name); - var adapter = new StructureDefinitionAdapter(structureDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(structureDef); assertEquals(name, adapter.getName()); var newName = "name2"; adapter.setName(newName); @@ -63,7 +64,7 @@ void adapter_get_and_set_url() { var structureDef = new StructureDefinition(); var url = "www.url.com"; structureDef.setUrl(url); - var adapter = new StructureDefinitionAdapter(structureDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(structureDef); assertTrue(adapter.hasUrl()); assertEquals(url, adapter.getUrl()); var newUrl = "www.url2.com"; @@ -77,7 +78,7 @@ void adapter_get_and_set_version() { var structureDef = new StructureDefinition(); var version = "1.0.0"; structureDef.setVersion(version); - var adapter = new StructureDefinitionAdapter(structureDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(structureDef); assertTrue(adapter.hasVersion()); assertEquals(version, adapter.getVersion()); var newVersion = "1.0.1"; @@ -90,7 +91,7 @@ void adapter_get_and_set_status() { var structureDef = new StructureDefinition(); var status = PublicationStatus.DRAFT; structureDef.setStatus(status); - var adapter = new StructureDefinitionAdapter(structureDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(structureDef); assertEquals(status.toCode(), adapter.getStatus()); assertThrows(UnprocessableEntityException.class, () -> adapter.setStatus("invalid-status")); var newStatus = PublicationStatus.ACTIVE; @@ -107,7 +108,7 @@ void adapter_get_and_set_dates() { .setStart(java.sql.Date.valueOf(LocalDate.parse("2020-01-01"))) .setEnd(java.sql.Date.valueOf(LocalDate.parse("2020-12-31"))); structureDef.setDate(date); - var adapter = new StructureDefinitionAdapter(structureDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(structureDef); assertEquals(date, adapter.getDate()); assertEquals(null, adapter.getApprovalDate()); assertNotEquals(effectivePeriod, adapter.getEffectivePeriod()); @@ -131,7 +132,7 @@ void adapter_get_experimental() { var structureDef = new StructureDefinition(); var experimental = true; structureDef.setExperimental(experimental); - var adapter = new StructureDefinitionAdapter(structureDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(structureDef); assertEquals(experimental, adapter.getExperimental()); } @@ -139,7 +140,7 @@ void adapter_get_experimental() { void adapter_set_relatedArtifact() { var structureDef = new StructureDefinition(); var relatedArtifactList = List.of(new RelatedArtifact()); - var adapter = new StructureDefinitionAdapter(structureDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(structureDef); adapter.setRelatedArtifact(relatedArtifactList); assertEquals(0, adapter.getRelatedArtifact().size()); } @@ -148,8 +149,8 @@ void adapter_set_relatedArtifact() { void adapter_copy() { var structureDef = new StructureDefinition().setStatus(PublicationStatus.DRAFT); structureDef.setId("plan-1"); - var adapter = new StructureDefinitionAdapter(structureDef); - var copy = adapter.copy(); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(structureDef); + var copy = (StructureDefinition) adapter.copy(); copy.setId("plan-2"); assertNotEquals(structureDef.getId(), copy.getId()); structureDef.setStatus(PublicationStatus.ACTIVE); @@ -173,7 +174,7 @@ void adapter_get_all_dependencies() { .getDifferential() .addElement() .setBinding(new ElementDefinitionBindingComponent().setValueSet(new UriType(dependencies.get(4)))); - var adapter = new StructureDefinitionAdapter(structureDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(structureDef); var extractedDependencies = adapter.getDependencies(); assertEquals(dependencies.size(), extractedDependencies.size()); extractedDependencies.forEach(dep -> { diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ValueSetAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ValueSetAdapterTest.java index b4a71a8e2..14a6d09ca 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ValueSetAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ValueSetAdapterTest.java @@ -27,6 +27,7 @@ public class ValueSetAdapterTest { private final FhirContext fhirContext = FhirContext.forDstu3Cached(); + private final org.opencds.cqf.fhir.utility.adapter.AdapterFactory adapterFactory = new AdapterFactory(); @Test void invalid_object_fails() { @@ -38,7 +39,7 @@ void adapter_accepts_visitor() { var spyVisitor = spy(new PackageVisitor(fhirContext)); doReturn(new Bundle()).when(spyVisitor).visit(any(ValueSetAdapter.class), any(), any()); IDomainResource valueSet = new ValueSet(); - var adapter = new ValueSetAdapter(valueSet); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(valueSet); assertEquals(valueSet, adapter.get()); adapter.accept(spyVisitor, null, null); verify(spyVisitor, times(1)).visit(any(ValueSetAdapter.class), any(), any()); @@ -49,7 +50,7 @@ void adapter_get_and_set_name() { var valueSet = new ValueSet(); var name = "name"; valueSet.setName(name); - var adapter = new ValueSetAdapter(valueSet); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(valueSet); assertEquals(name, adapter.getName()); var newName = "name2"; adapter.setName(newName); @@ -61,7 +62,7 @@ void adapter_get_and_set_url() { var valueSet = new ValueSet(); var url = "www.url.com"; valueSet.setUrl(url); - var adapter = new ValueSetAdapter(valueSet); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(valueSet); assertTrue(adapter.hasUrl()); assertEquals(url, adapter.getUrl()); var newUrl = "www.url2.com"; @@ -75,7 +76,7 @@ void adapter_get_and_set_version() { var valueSet = new ValueSet(); var version = "1.0.0"; valueSet.setVersion(version); - var adapter = new ValueSetAdapter(valueSet); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(valueSet); assertTrue(adapter.hasVersion()); assertEquals(version, adapter.getVersion()); var newVersion = "1.0.1"; @@ -88,7 +89,7 @@ void adapter_get_and_set_status() { var valueSet = new ValueSet(); var status = PublicationStatus.DRAFT; valueSet.setStatus(status); - var adapter = new ValueSetAdapter(valueSet); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(valueSet); assertEquals(status.toCode(), adapter.getStatus()); assertThrows(UnprocessableEntityException.class, () -> adapter.setStatus("invalid-status")); var newStatus = PublicationStatus.ACTIVE; @@ -105,7 +106,7 @@ void adapter_get_and_set_dates() { .setStart(java.sql.Date.valueOf(LocalDate.parse("2020-01-01"))) .setEnd(java.sql.Date.valueOf(LocalDate.parse("2020-12-31"))); valueSet.setDate(date); - var adapter = new ValueSetAdapter(valueSet); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(valueSet); assertEquals(date, adapter.getDate()); assertEquals(null, adapter.getApprovalDate()); assertNotEquals(effectivePeriod, adapter.getEffectivePeriod()); @@ -129,7 +130,7 @@ void adapter_get_experimental() { var valueSet = new ValueSet(); var experimental = true; valueSet.setExperimental(experimental); - var adapter = new ValueSetAdapter(valueSet); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(valueSet); assertEquals(experimental, adapter.getExperimental()); } @@ -137,7 +138,7 @@ void adapter_get_experimental() { void adapter_set_relatedArtifact() { var valueSet = new ValueSet(); var relatedArtifactList = List.of(new RelatedArtifact()); - var adapter = new ValueSetAdapter(valueSet); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(valueSet); adapter.setRelatedArtifact(relatedArtifactList); assertEquals(0, adapter.getRelatedArtifact().size()); } @@ -146,8 +147,8 @@ void adapter_set_relatedArtifact() { void adapter_copy() { var valueSet = new ValueSet().setStatus(PublicationStatus.DRAFT); valueSet.setId("valueset-1"); - var adapter = new ValueSetAdapter(valueSet); - var copy = adapter.copy(); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(valueSet); + var copy = (ValueSet) adapter.copy(); copy.setId("valueset-2"); assertNotEquals(valueSet.getId(), copy.getId()); valueSet.setStatus(PublicationStatus.ACTIVE); @@ -159,7 +160,7 @@ void adapter_get_all_dependencies() { var dependencies = List.of("profileRef"); var valueSet = new ValueSet(); valueSet.getMeta().addProfile(dependencies.get(0)); - var adapter = new ValueSetAdapter(valueSet); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(valueSet); var extractedDependencies = adapter.getDependencies(); assertEquals(extractedDependencies.size(), dependencies.size()); extractedDependencies.forEach(dep -> { diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/EndpointAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/EndpointAdapterTest.java index 439645fb3..7e93e3cd4 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/EndpointAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/EndpointAdapterTest.java @@ -8,9 +8,11 @@ import org.junit.jupiter.api.Test; public class EndpointAdapterTest { + private final org.opencds.cqf.fhir.utility.adapter.AdapterFactory adapterFactory = new AdapterFactory(); + @Test void invalid_object_fails() { - assertThrows(IllegalArgumentException.class, () -> new LibraryAdapter(new PlanDefinition())); + assertThrows(IllegalArgumentException.class, () -> new EndpointAdapter(new PlanDefinition())); } @Test @@ -18,7 +20,7 @@ void adapter_get_and_set_address() { var endpoint = new Endpoint(); var address = "123 Test Street"; endpoint.setAddress(address); - var adapter = new EndpointAdapter(endpoint); + var adapter = (EndpointAdapter) adapterFactory.createResource(endpoint); assertEquals(address, adapter.getAddress()); var newAddress = "456 Test Street"; adapter.setAddress(newAddress); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/KnowledgeArtifactAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/KnowledgeArtifactAdapterTest.java new file mode 100644 index 000000000..0b5daf1c1 --- /dev/null +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/KnowledgeArtifactAdapterTest.java @@ -0,0 +1,146 @@ +package org.opencds.cqf.fhir.utility.adapter.r4; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; +import java.util.Date; +import java.util.List; +import org.hl7.fhir.instance.model.api.IDomainResource; +import org.hl7.fhir.r4.model.Bundle; +import org.hl7.fhir.r4.model.CompartmentDefinition; +import org.hl7.fhir.r4.model.Enumerations.PublicationStatus; +import org.hl7.fhir.r4.model.RelatedArtifact; +import org.junit.jupiter.api.Test; +import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; + +class KnowledgeArtifactAdapterTest { + private final FhirContext fhirContext = FhirContext.forR4Cached(); + private final org.opencds.cqf.fhir.utility.adapter.AdapterFactory adapterFactory = new AdapterFactory(); + + @Test + void invalid_object_fails() { + assertThrows( + UnprocessableEntityException.class, + () -> adapterFactory.createKnowledgeArtifactAdapter(new org.hl7.fhir.r5.model.Library())); + } + + @Test + void test() { + var resource = new CompartmentDefinition(); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(resource); + assertNotNull(adapter); + } + + @Test + void adapter_accepts_visitor() { + var spyVisitor = spy(new PackageVisitor(fhirContext)); + doReturn(new Bundle()).when(spyVisitor).visit(any(KnowledgeArtifactAdapter.class), any(), any()); + IDomainResource def = new CompartmentDefinition(); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(def); + adapter.accept(spyVisitor, null, null); + verify(spyVisitor, times(1)).visit(any(KnowledgeArtifactAdapter.class), any(), any()); + } + + @Test + void adapter_get_and_set_name() { + var def = new CompartmentDefinition(); + var name = "name"; + def.setName(name); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(def); + assertEquals(name, adapter.getName()); + var newName = "name2"; + adapter.setName(newName); + assertEquals(newName, def.getName()); + } + + @Test + void adapter_get_and_set_url() { + var def = new CompartmentDefinition(); + var url = "www.url.com"; + def.setUrl(url); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(def); + assertEquals(url, adapter.getUrl()); + var newUrl = "www.url2.com"; + adapter.setUrl(newUrl); + assertEquals(newUrl, def.getUrl()); + } + + @Test + void adapter_get_and_set_version() { + var def = new CompartmentDefinition(); + var version = "1.0.0"; + def.setVersion(version); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(def); + assertTrue(adapter.hasVersion()); + assertEquals(version, adapter.getVersion()); + var newVersion = "1.0.1"; + adapter.setVersion(newVersion); + assertEquals(newVersion, def.getVersion()); + } + + @Test + void adapter_get_and_set_status() { + var def = new CompartmentDefinition(); + var status = PublicationStatus.DRAFT; + def.setStatus(status); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(def); + assertEquals(status.toCode(), adapter.getStatus()); + assertThrows(UnprocessableEntityException.class, () -> adapter.setStatus("invalid-status")); + var newStatus = PublicationStatus.ACTIVE; + adapter.setStatus(newStatus.toCode()); + assertEquals(newStatus, PublicationStatus.fromCode(adapter.getStatus())); + } + + @Test + void adapter_get_and_set_dates() { + var def = new CompartmentDefinition(); + var date = new Date(); + def.setDate(date); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(def); + assertEquals(date, adapter.getDate()); + var newDate = new Date(); + newDate.setTime(100); + adapter.setDate(newDate); + assertEquals(newDate, def.getDate()); + } + + @Test + void adapter_get_experimental() { + var def = new CompartmentDefinition(); + var experimental = true; + def.setExperimental(experimental); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(def); + assertEquals(experimental, adapter.getExperimental()); + } + + @Test + void adapter_set_relatedArtifact() { + var def = new CompartmentDefinition(); + var relatedArtifactList = List.of(new RelatedArtifact()); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(def); + adapter.setRelatedArtifact(relatedArtifactList); + assertEquals(0, adapter.getRelatedArtifact().size()); + } + + @Test + void adapter_copy() { + var def = new CompartmentDefinition().setStatus(PublicationStatus.DRAFT); + def.setId("def-1"); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(def); + var copy = (CompartmentDefinition) adapter.copy(); + copy.setId("def-2"); + assertNotEquals(def.getId(), copy.getId()); + def.setStatus(PublicationStatus.ACTIVE); + assertNotEquals(adapter.getStatus(), copy.getStatus()); + } +} diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/LibraryAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/LibraryAdapterTest.java index 0c4c0b0bf..47f8e6b95 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/LibraryAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/LibraryAdapterTest.java @@ -35,6 +35,7 @@ public class LibraryAdapterTest { private final FhirContext fhirContext = FhirContext.forR4Cached(); + private final org.opencds.cqf.fhir.utility.adapter.AdapterFactory adapterFactory = new AdapterFactory(); @Test void invalid_object_fails() { @@ -46,7 +47,7 @@ void adapter_accepts_visitor() { var spyVisitor = spy(new PackageVisitor(fhirContext)); doReturn(new Bundle()).when(spyVisitor).visit(any(LibraryAdapter.class), any(), any()); IDomainResource library = new Library(); - var adapter = new LibraryAdapter(library); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(library); adapter.accept(spyVisitor, null, null); verify(spyVisitor, times(1)).visit(any(LibraryAdapter.class), any(), any()); } @@ -56,7 +57,7 @@ void adapter_get_and_set_name() { var library = new Library(); var name = "name"; library.setName(name); - var adapter = new LibraryAdapter(library); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(library); assertEquals(name, adapter.getName()); var newName = "name2"; adapter.setName(newName); @@ -68,7 +69,7 @@ void adapter_get_and_set_url() { var library = new Library(); var url = "www.url.com"; library.setUrl(url); - var adapter = new LibraryAdapter(library); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(library); assertEquals(url, adapter.getUrl()); var newUrl = "www.url2.com"; adapter.setUrl(newUrl); @@ -80,7 +81,7 @@ void adapter_get_and_set_version() { var library = new Library(); var version = "1.0.0"; library.setVersion(version); - var adapter = new LibraryAdapter(library); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(library); assertTrue(adapter.hasVersion()); assertEquals(version, adapter.getVersion()); var newVersion = "1.0.1"; @@ -93,7 +94,7 @@ void adapter_get_and_set_status() { var library = new Library(); var status = PublicationStatus.DRAFT; library.setStatus(status); - var adapter = new LibraryAdapter(library); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(library); assertEquals(status.toCode(), adapter.getStatus()); assertThrows(UnprocessableEntityException.class, () -> adapter.setStatus("invalid-status")); var newStatus = PublicationStatus.ACTIVE; @@ -112,7 +113,7 @@ void adapter_get_and_set_dates() { library.setDate(date); library.setApprovalDate(approvalDate); library.setEffectivePeriod(effectivePeriod); - var adapter = new LibraryAdapter(library); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(library); assertEquals(date, adapter.getDate()); assertEquals(approvalDate, adapter.getApprovalDate()); assertEquals(effectivePeriod, adapter.getEffectivePeriod()); @@ -136,7 +137,7 @@ void adapter_get_experimental() { var library = new Library(); var experimental = true; library.setExperimental(experimental); - var adapter = new LibraryAdapter(library); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(library); assertEquals(experimental, adapter.getExperimental()); } @@ -144,7 +145,7 @@ void adapter_get_experimental() { void adapter_set_relatedArtifact() { var library = new Library(); var relatedArtifactList = List.of(new RelatedArtifact()); - var adapter = new LibraryAdapter(library); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(library); adapter.setRelatedArtifact(relatedArtifactList); assertEquals(relatedArtifactList, library.getRelatedArtifact()); assertEquals(relatedArtifactList, adapter.getRelatedArtifact()); @@ -154,8 +155,8 @@ void adapter_set_relatedArtifact() { void adapter_copy() { var library = new Library().setStatus(PublicationStatus.DRAFT); library.setId("library-1"); - var adapter = new LibraryAdapter(library); - var copy = adapter.copy(); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(library); + var copy = (Library) adapter.copy(); var adapterCopy = new LibraryAdapter(copy); adapterCopy.setId(new IdDt("Library", "library-2")); assertNotEquals(library.getId(), copy.getId()); @@ -172,7 +173,7 @@ void adapter_get_all_dependencies() { library.getRelatedArtifactFirstRep().setResource(dependencies.get(1)); library.addDataRequirement().addProfile(dependencies.get(2)); library.addDataRequirement().addCodeFilter().setValueSet(dependencies.get(3)); - var adapter = new LibraryAdapter(library); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(library); var extractedDependencies = adapter.getDependencies(); assertEquals(extractedDependencies.size(), dependencies.size()); extractedDependencies.forEach(dep -> { @@ -183,7 +184,7 @@ void adapter_get_all_dependencies() { @Test void adapter_get_and_set_content() { var library = new Library(); - var adapter = new LibraryAdapter(library); + var adapter = (LibraryAdapter) adapterFactory.createKnowledgeArtifactAdapter(library); var contentList = new ArrayList(); contentList.add(new Attachment().setContentType("text/cql").setData(new byte[10])); adapter.setContent(contentList); @@ -198,7 +199,7 @@ void adapter_get_and_set_content() { void adapter_get_and_set_type() { var type = new CodeableConcept(new Coding("www.test.com", "test", "Test")); var library = new Library().setType(type); - var adapter = new LibraryAdapter(library); + var adapter = (LibraryAdapter) adapterFactory.createKnowledgeArtifactAdapter(library); assertEquals(type, adapter.getType()); assertThrows(UnprocessableEntityException.class, () -> adapter.setType("test")); var newType = new CodeableConcept(new Coding("http://hl7.org/fhir/ValueSet/library-type", "logic-library", "")); @@ -214,7 +215,7 @@ void adapter_get_and_set_type() { @Test void adapter_get_and_set_dataRequirement() { var library = new Library(); - var adapter = new LibraryAdapter(library); + var adapter = (LibraryAdapter) adapterFactory.createKnowledgeArtifactAdapter(library); var dataRequirements = new ArrayList(); dataRequirements.add(new DataRequirement().setType("Patient")); adapter.setDataRequirement(dataRequirements); @@ -229,7 +230,7 @@ void adapter_get_useContext() { var library = new Library(); var useContext = new UsageContext().setCode(new Coding("www.test.com", "test", "Test")); library.setUseContext(Collections.singletonList(useContext)); - var adapter = new LibraryAdapter(library); + var adapter = (LibraryAdapter) adapterFactory.createKnowledgeArtifactAdapter(library); assertEquals(useContext, adapter.getUseContext().get(0)); } } diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/PlanDefinitionAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/PlanDefinitionAdapterTest.java index 700d831cd..7c869d849 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/PlanDefinitionAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/PlanDefinitionAdapterTest.java @@ -30,6 +30,7 @@ public class PlanDefinitionAdapterTest { private final FhirContext fhirContext = FhirContext.forR4Cached(); + private final org.opencds.cqf.fhir.utility.adapter.AdapterFactory adapterFactory = new AdapterFactory(); @Test void invalid_object_fails() { @@ -41,7 +42,7 @@ void adapter_accepts_visitor() { var spyVisitor = spy(new PackageVisitor(fhirContext)); doReturn(new Bundle()).when(spyVisitor).visit(any(PlanDefinitionAdapter.class), any(), any()); IDomainResource planDef = new PlanDefinition(); - var adapter = new PlanDefinitionAdapter(planDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(planDef); adapter.accept(spyVisitor, null, null); verify(spyVisitor, times(1)).visit(any(PlanDefinitionAdapter.class), any(), any()); } @@ -51,7 +52,7 @@ void adapter_get_and_set_name() { var planDef = new PlanDefinition(); var name = "name"; planDef.setName(name); - var adapter = new PlanDefinitionAdapter(planDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(planDef); assertEquals(name, adapter.getName()); var newName = "name2"; adapter.setName(newName); @@ -63,7 +64,7 @@ void adapter_get_and_set_url() { var planDef = new PlanDefinition(); var url = "www.url.com"; planDef.setUrl(url); - var adapter = new PlanDefinitionAdapter(planDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(planDef); assertEquals(url, adapter.getUrl()); var newUrl = "www.url2.com"; adapter.setUrl(newUrl); @@ -75,7 +76,7 @@ void adapter_get_and_set_version() { var planDef = new PlanDefinition(); var version = "1.0.0"; planDef.setVersion(version); - var adapter = new PlanDefinitionAdapter(planDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(planDef); assertTrue(adapter.hasVersion()); assertEquals(version, adapter.getVersion()); var newVersion = "1.0.1"; @@ -88,7 +89,7 @@ void adapter_get_and_set_status() { var planDef = new PlanDefinition(); var status = PublicationStatus.DRAFT; planDef.setStatus(status); - var adapter = new PlanDefinitionAdapter(planDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(planDef); assertEquals(status.toCode(), adapter.getStatus()); assertThrows(UnprocessableEntityException.class, () -> adapter.setStatus("invalid-status")); var newStatus = PublicationStatus.ACTIVE; @@ -107,7 +108,7 @@ void adapter_get_and_set_dates() { planDef.setDate(date); planDef.setApprovalDate(approvalDate); planDef.setEffectivePeriod(effectivePeriod); - var adapter = new PlanDefinitionAdapter(planDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(planDef); assertEquals(date, adapter.getDate()); assertEquals(approvalDate, adapter.getApprovalDate()); assertEquals(effectivePeriod, adapter.getEffectivePeriod()); @@ -131,7 +132,7 @@ void adapter_get_experimental() { var planDef = new PlanDefinition(); var experimental = true; planDef.setExperimental(experimental); - var adapter = new PlanDefinitionAdapter(planDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(planDef); assertEquals(experimental, adapter.getExperimental()); } @@ -139,7 +140,7 @@ void adapter_get_experimental() { void adapter_set_relatedArtifact() { var planDef = new PlanDefinition(); var relatedArtifactList = List.of(new RelatedArtifact()); - var adapter = new PlanDefinitionAdapter(planDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(planDef); adapter.setRelatedArtifact(relatedArtifactList); assertEquals(relatedArtifactList, planDef.getRelatedArtifact()); assertEquals(relatedArtifactList, adapter.getRelatedArtifact()); @@ -149,8 +150,8 @@ void adapter_set_relatedArtifact() { void adapter_copy() { var planDef = new PlanDefinition().setStatus(PublicationStatus.DRAFT); planDef.setId("plan-1"); - var adapter = new PlanDefinitionAdapter(planDef); - var copy = adapter.copy(); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(planDef); + var copy = (PlanDefinition) adapter.copy(); var adapterCopy = new PlanDefinitionAdapter(copy); adapterCopy.setId(new IdDt("PlanDefinition", "plan-2")); assertNotEquals(planDef.getId(), copy.getId()); @@ -199,7 +200,7 @@ void adapter_get_all_dependencies() { planDef.addExtension(new Extension( "http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-partOf", new CanonicalType(dependencies.get(12)))); action.addAction().setDefinition(new CanonicalType(dependencies.get(13))); - var adapter = new PlanDefinitionAdapter(planDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(planDef); var extractedDependencies = adapter.getDependencies(); assertEquals(extractedDependencies.size(), dependencies.size()); extractedDependencies.forEach(dep -> { diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/QuestionnaireAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/QuestionnaireAdapterTest.java index 48bdcb81d..3c627faf3 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/QuestionnaireAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/QuestionnaireAdapterTest.java @@ -32,6 +32,7 @@ public class QuestionnaireAdapterTest { private final FhirContext fhirContext = FhirContext.forR4Cached(); + private final org.opencds.cqf.fhir.utility.adapter.AdapterFactory adapterFactory = new AdapterFactory(); @Test void invalid_object_fails() { @@ -43,7 +44,7 @@ void adapter_accepts_visitor() { var spyVisitor = spy(new PackageVisitor(fhirContext)); doReturn(new Bundle()).when(spyVisitor).visit(any(QuestionnaireAdapter.class), any(), any()); IDomainResource questionnaire = new Questionnaire(); - var adapter = new QuestionnaireAdapter(questionnaire); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(questionnaire); adapter.accept(spyVisitor, null, null); verify(spyVisitor, times(1)).visit(any(QuestionnaireAdapter.class), any(), any()); } @@ -53,7 +54,7 @@ void adapter_get_and_set_name() { var questionnaire = new Questionnaire(); var name = "name"; questionnaire.setName(name); - var adapter = new QuestionnaireAdapter(questionnaire); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(questionnaire); assertEquals(name, adapter.getName()); var newName = "name2"; adapter.setName(newName); @@ -65,7 +66,7 @@ void adapter_get_and_set_url() { var questionnaire = new Questionnaire(); var url = "www.url.com"; questionnaire.setUrl(url); - var adapter = new QuestionnaireAdapter(questionnaire); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(questionnaire); assertEquals(url, adapter.getUrl()); var newUrl = "www.url2.com"; adapter.setUrl(newUrl); @@ -77,7 +78,7 @@ void adapter_get_and_set_version() { var questionnaire = new Questionnaire(); var version = "1.0.0"; questionnaire.setVersion(version); - var adapter = new QuestionnaireAdapter(questionnaire); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(questionnaire); assertTrue(adapter.hasVersion()); assertEquals(version, adapter.getVersion()); var newVersion = "1.0.1"; @@ -90,7 +91,7 @@ void adapter_get_and_set_status() { var questionnaire = new Questionnaire(); var status = PublicationStatus.DRAFT; questionnaire.setStatus(status); - var adapter = new QuestionnaireAdapter(questionnaire); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(questionnaire); assertEquals(status.toCode(), adapter.getStatus()); assertThrows(UnprocessableEntityException.class, () -> adapter.setStatus("invalid-status")); var newStatus = PublicationStatus.ACTIVE; @@ -109,7 +110,7 @@ void adapter_get_and_set_dates() { questionnaire.setDate(date); questionnaire.setApprovalDate(approvalDate); questionnaire.setEffectivePeriod(effectivePeriod); - var adapter = new QuestionnaireAdapter(questionnaire); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(questionnaire); assertEquals(date, adapter.getDate()); assertEquals(approvalDate, adapter.getApprovalDate()); assertEquals(effectivePeriod, adapter.getEffectivePeriod()); @@ -133,7 +134,7 @@ void adapter_get_experimental() { var questionnaire = new Questionnaire(); var experimental = true; questionnaire.setExperimental(experimental); - var adapter = new QuestionnaireAdapter(questionnaire); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(questionnaire); assertEquals(experimental, adapter.getExperimental()); } @@ -141,7 +142,7 @@ void adapter_get_experimental() { void adapter_set_relatedArtifact() { var questionnaire = new Questionnaire(); var relatedArtifactList = List.of(new RelatedArtifact()); - var adapter = new QuestionnaireAdapter(questionnaire); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(questionnaire); adapter.setRelatedArtifact(relatedArtifactList); assertEquals(0, adapter.getRelatedArtifact().size()); } @@ -150,8 +151,8 @@ void adapter_set_relatedArtifact() { void adapter_copy() { var questionnaire = new Questionnaire().setStatus(PublicationStatus.DRAFT); questionnaire.setId("plan-1"); - var adapter = new QuestionnaireAdapter(questionnaire); - var copy = adapter.copy(); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(questionnaire); + var copy = (Questionnaire) adapter.copy(); copy.setId("plan-2"); assertNotEquals(questionnaire.getId(), copy.getId()); questionnaire.setStatus(PublicationStatus.ACTIVE); @@ -216,7 +217,7 @@ void adapter_get_all_dependencies() { questionnaire .addItem() .addExtension(Constants.SDC_QUESTIONNAIRE_SUB_QUESTIONNAIRE, new CanonicalType(dependencies.get(14))); - var adapter = new QuestionnaireAdapter(questionnaire); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(questionnaire); var extractedDependencies = adapter.getDependencies(); assertEquals(dependencies.size(), extractedDependencies.size()); extractedDependencies.forEach(dep -> { diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/ResourceAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/ResourceAdapterTest.java index edee113a0..0add91c2a 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/ResourceAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/ResourceAdapterTest.java @@ -27,6 +27,8 @@ import org.slf4j.LoggerFactory; public class ResourceAdapterTest { + private final org.opencds.cqf.fhir.utility.adapter.AdapterFactory adapterFactory = new AdapterFactory(); + @Test void invalid_object_fails() { assertThrows(IllegalArgumentException.class, () -> new ResourceAdapter(null)); @@ -38,7 +40,7 @@ void adapter_get_and_set_property() { var resource = new Patient(); var id = new IdDt("patient-1"); resource.setId(id); - var adapter = new ResourceAdapter(resource); + var adapter = adapterFactory.createResource(resource); assertEquals(id.getValue(), ((IIdType) adapter.getSingleProperty("id")).getValue()); var newId = new IdType("patient-2"); adapter.setProperty("id", newId); @@ -56,7 +58,7 @@ void adapter_copy() { var resource = new Patient(); resource.setId("patient-1"); resource.setMeta(new Meta().setLastUpdated(new Date())); - var adapter = new ResourceAdapter(resource); + var adapter = adapterFactory.createResource(resource); var copy = (Patient) adapter.copy(); assertTrue(adapter.equalsDeep(copy)); var newDate = new Date(); @@ -88,7 +90,7 @@ void adapter_get_and_set_extension() { assertEquals(Level.DEBUG, listAppender.list.get(1).getLevel()); var resource = new Patient(); var extensionList = List.of(new Extension().setUrl("test-extension-url").setValue(new BooleanType(true))); - var adapter = new ResourceAdapter(resource); + var adapter = adapterFactory.createResource(resource); adapter.setExtension(extensionList); assertTrue(adapter.hasExtension()); assertEquals(extensionList, resource.getExtension()); @@ -100,7 +102,7 @@ void adapter_get_and_set_extension() { void adapter_get_contained() { var resource = new PlanDefinition(); resource.addContained(new Library()); - var adapter = new ResourceAdapter(resource); + var adapter = adapterFactory.createResource(resource); assertTrue(adapter.hasContained()); assertNotNull(adapter.getContained()); assertFalse(adapter.hasContained(adapter.getContained().get(0))); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/StructureDefinitionAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/StructureDefinitionAdapterTest.java index 7b6f78d9c..5461ad78a 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/StructureDefinitionAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/StructureDefinitionAdapterTest.java @@ -30,6 +30,7 @@ public class StructureDefinitionAdapterTest { private final FhirContext fhirContext = FhirContext.forR4Cached(); + private final org.opencds.cqf.fhir.utility.adapter.AdapterFactory adapterFactory = new AdapterFactory(); @Test void invalid_object_fails() { @@ -41,7 +42,7 @@ void adapter_accepts_visitor() { var spyVisitor = spy(new PackageVisitor(fhirContext)); doReturn(new Bundle()).when(spyVisitor).visit(any(StructureDefinitionAdapter.class), any(), any()); IDomainResource structureDef = new StructureDefinition(); - var adapter = new StructureDefinitionAdapter(structureDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(structureDef); assertEquals(structureDef, adapter.get()); adapter.accept(spyVisitor, null, null); verify(spyVisitor, times(1)).visit(any(StructureDefinitionAdapter.class), any(), any()); @@ -52,7 +53,7 @@ void adapter_get_and_set_name() { var structureDef = new StructureDefinition(); var name = "name"; structureDef.setName(name); - var adapter = new StructureDefinitionAdapter(structureDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(structureDef); assertEquals(name, adapter.getName()); var newName = "name2"; adapter.setName(newName); @@ -64,7 +65,7 @@ void adapter_get_and_set_url() { var structureDef = new StructureDefinition(); var url = "www.url.com"; structureDef.setUrl(url); - var adapter = new StructureDefinitionAdapter(structureDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(structureDef); assertTrue(adapter.hasUrl()); assertEquals(url, adapter.getUrl()); var newUrl = "www.url2.com"; @@ -78,7 +79,7 @@ void adapter_get_and_set_version() { var structureDef = new StructureDefinition(); var version = "1.0.0"; structureDef.setVersion(version); - var adapter = new StructureDefinitionAdapter(structureDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(structureDef); assertTrue(adapter.hasVersion()); assertEquals(version, adapter.getVersion()); var newVersion = "1.0.1"; @@ -91,7 +92,7 @@ void adapter_get_and_set_status() { var structureDef = new StructureDefinition(); var status = PublicationStatus.DRAFT; structureDef.setStatus(status); - var adapter = new StructureDefinitionAdapter(structureDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(structureDef); assertEquals(status.toCode(), adapter.getStatus()); assertThrows(UnprocessableEntityException.class, () -> adapter.setStatus("invalid-status")); var newStatus = PublicationStatus.ACTIVE; @@ -108,7 +109,7 @@ void adapter_get_and_set_dates() { .setStart(java.sql.Date.valueOf(LocalDate.parse("2020-01-01"))) .setEnd(java.sql.Date.valueOf(LocalDate.parse("2020-12-31"))); structureDef.setDate(date); - var adapter = new StructureDefinitionAdapter(structureDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(structureDef); assertEquals(date, adapter.getDate()); assertEquals(null, adapter.getApprovalDate()); assertNotEquals(effectivePeriod, adapter.getEffectivePeriod()); @@ -132,7 +133,7 @@ void adapter_get_experimental() { var structureDef = new StructureDefinition(); var experimental = true; structureDef.setExperimental(experimental); - var adapter = new StructureDefinitionAdapter(structureDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(structureDef); assertEquals(experimental, adapter.getExperimental()); } @@ -140,7 +141,7 @@ void adapter_get_experimental() { void adapter_set_relatedArtifact() { var structureDef = new StructureDefinition(); var relatedArtifactList = List.of(new RelatedArtifact()); - var adapter = new StructureDefinitionAdapter(structureDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(structureDef); adapter.setRelatedArtifact(relatedArtifactList); assertEquals(0, adapter.getRelatedArtifact().size()); } @@ -149,8 +150,8 @@ void adapter_set_relatedArtifact() { void adapter_copy() { var structureDef = new StructureDefinition().setStatus(PublicationStatus.DRAFT); structureDef.setId("plan-1"); - var adapter = new StructureDefinitionAdapter(structureDef); - var copy = adapter.copy(); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(structureDef); + var copy = (StructureDefinition) adapter.copy(); copy.setId("plan-2"); assertNotEquals(structureDef.getId(), copy.getId()); structureDef.setStatus(PublicationStatus.ACTIVE); @@ -182,7 +183,7 @@ void adapter_get_all_dependencies() { .getDifferential() .addElement() .setBinding(new ElementDefinitionBindingComponent().setValueSet(dependencies.get(7))); - var adapter = new StructureDefinitionAdapter(structureDef); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(structureDef); var extractedDependencies = adapter.getDependencies(); assertEquals(dependencies.size(), extractedDependencies.size()); extractedDependencies.forEach(dep -> { diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/ValueSetAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/ValueSetAdapterTest.java index fc3db891d..51dfbc2e5 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/ValueSetAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/ValueSetAdapterTest.java @@ -27,6 +27,7 @@ public class ValueSetAdapterTest { private final FhirContext fhirContext = FhirContext.forR4Cached(); + private final org.opencds.cqf.fhir.utility.adapter.AdapterFactory adapterFactory = new AdapterFactory(); @Test void invalid_object_fails() { @@ -38,7 +39,7 @@ void adapter_accepts_visitor() { var spyVisitor = spy(new PackageVisitor(fhirContext)); doReturn(new Bundle()).when(spyVisitor).visit(any(ValueSetAdapter.class), any(), any()); IDomainResource valueSet = new ValueSet(); - var adapter = new ValueSetAdapter(valueSet); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(valueSet); assertEquals(valueSet, adapter.get()); adapter.accept(spyVisitor, null, null); verify(spyVisitor, times(1)).visit(any(ValueSetAdapter.class), any(), any()); @@ -49,7 +50,7 @@ void adapter_get_and_set_name() { var valueSet = new ValueSet(); var name = "name"; valueSet.setName(name); - var adapter = new ValueSetAdapter(valueSet); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(valueSet); assertEquals(name, adapter.getName()); var newName = "name2"; adapter.setName(newName); @@ -61,7 +62,7 @@ void adapter_get_and_set_url() { var valueSet = new ValueSet(); var url = "www.url.com"; valueSet.setUrl(url); - var adapter = new ValueSetAdapter(valueSet); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(valueSet); assertTrue(adapter.hasUrl()); assertEquals(url, adapter.getUrl()); var newUrl = "www.url2.com"; @@ -75,7 +76,7 @@ void adapter_get_and_set_version() { var valueSet = new ValueSet(); var version = "1.0.0"; valueSet.setVersion(version); - var adapter = new ValueSetAdapter(valueSet); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(valueSet); assertTrue(adapter.hasVersion()); assertEquals(version, adapter.getVersion()); var newVersion = "1.0.1"; @@ -88,7 +89,7 @@ void adapter_get_and_set_status() { var valueSet = new ValueSet(); var status = PublicationStatus.DRAFT; valueSet.setStatus(status); - var adapter = new ValueSetAdapter(valueSet); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(valueSet); assertEquals(status.toCode(), adapter.getStatus()); assertThrows(UnprocessableEntityException.class, () -> adapter.setStatus("invalid-status")); var newStatus = PublicationStatus.ACTIVE; @@ -105,7 +106,7 @@ void adapter_get_and_set_dates() { .setStart(java.sql.Date.valueOf(LocalDate.parse("2020-01-01"))) .setEnd(java.sql.Date.valueOf(LocalDate.parse("2020-12-31"))); valueSet.setDate(date); - var adapter = new ValueSetAdapter(valueSet); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(valueSet); assertEquals(date, adapter.getDate()); assertEquals(null, adapter.getApprovalDate()); assertNotEquals(effectivePeriod, adapter.getEffectivePeriod()); @@ -129,7 +130,7 @@ void adapter_get_experimental() { var valueSet = new ValueSet(); var experimental = true; valueSet.setExperimental(experimental); - var adapter = new ValueSetAdapter(valueSet); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(valueSet); assertEquals(experimental, adapter.getExperimental()); } @@ -137,7 +138,7 @@ void adapter_get_experimental() { void adapter_set_relatedArtifact() { var valueSet = new ValueSet(); var relatedArtifactList = List.of(new RelatedArtifact()); - var adapter = new ValueSetAdapter(valueSet); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(valueSet); adapter.setRelatedArtifact(relatedArtifactList); assertEquals(0, adapter.getRelatedArtifact().size()); } @@ -146,8 +147,8 @@ void adapter_set_relatedArtifact() { void adapter_copy() { var valueSet = new ValueSet().setStatus(PublicationStatus.DRAFT); valueSet.setId("valueset-1"); - var adapter = new ValueSetAdapter(valueSet); - var copy = adapter.copy(); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(valueSet); + var copy = (ValueSet) adapter.copy(); copy.setId("valueset-2"); assertNotEquals(valueSet.getId(), copy.getId()); valueSet.setStatus(PublicationStatus.ACTIVE); @@ -159,7 +160,7 @@ void adapter_get_all_dependencies() { var dependencies = List.of("profileRef"); var valueSet = new ValueSet(); valueSet.getMeta().addProfile(dependencies.get(0)); - var adapter = new ValueSetAdapter(valueSet); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(valueSet); var extractedDependencies = adapter.getDependencies(); assertEquals(dependencies.size(), extractedDependencies.size()); extractedDependencies.forEach(dep -> { diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/EndpointAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/EndpointAdapterTest.java index 400c36025..0dee94209 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/EndpointAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/EndpointAdapterTest.java @@ -7,7 +7,7 @@ import org.hl7.fhir.r5.model.PlanDefinition; import org.junit.jupiter.api.Test; -public class EndpointAdapterTest { +class EndpointAdapterTest { private final org.opencds.cqf.fhir.utility.adapter.AdapterFactory adapterFactory = new AdapterFactory(); @Test diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/KnowledgeArtifactAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/KnowledgeArtifactAdapterTest.java new file mode 100644 index 000000000..f84751f26 --- /dev/null +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/KnowledgeArtifactAdapterTest.java @@ -0,0 +1,153 @@ +package org.opencds.cqf.fhir.utility.adapter.r5; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; +import java.util.Date; +import java.util.List; +import org.hl7.fhir.instance.model.api.IDomainResource; +import org.hl7.fhir.r5.model.Bundle; +import org.hl7.fhir.r5.model.ChargeItemDefinition; +import org.hl7.fhir.r5.model.Enumerations.PublicationStatus; +import org.hl7.fhir.r5.model.RelatedArtifact; +import org.junit.jupiter.api.Test; +import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; + +class KnowledgeArtifactAdapterTest { + private final FhirContext fhirContext = FhirContext.forR5Cached(); + private final org.opencds.cqf.fhir.utility.adapter.AdapterFactory adapterFactory = new AdapterFactory(); + + @Test + void invalid_object_fails() { + assertThrows( + UnprocessableEntityException.class, + () -> adapterFactory.createKnowledgeArtifactAdapter(new org.hl7.fhir.r4.model.Library())); + } + + @Test + void test() { + var resource = new ChargeItemDefinition(); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(resource); + assertNotNull(adapter); + } + + @Test + void adapter_accepts_visitor() { + var spyVisitor = spy(new PackageVisitor(fhirContext)); + doReturn(new Bundle()).when(spyVisitor).visit(any(KnowledgeArtifactAdapter.class), any(), any()); + IDomainResource def = new ChargeItemDefinition(); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(def); + adapter.accept(spyVisitor, null, null); + verify(spyVisitor, times(1)).visit(any(KnowledgeArtifactAdapter.class), any(), any()); + } + + @Test + void adapter_get_and_set_name() { + var def = new ChargeItemDefinition(); + var name = "name"; + def.setName(name); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(def); + assertEquals(name, adapter.getName()); + var newName = "name2"; + adapter.setName(newName); + assertEquals(newName, def.getName()); + } + + @Test + void adapter_get_and_set_url() { + var def = new ChargeItemDefinition(); + var url = "www.url.com"; + def.setUrl(url); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(def); + assertEquals(url, adapter.getUrl()); + var newUrl = "www.url2.com"; + adapter.setUrl(newUrl); + assertEquals(newUrl, def.getUrl()); + } + + @Test + void adapter_get_and_set_version() { + var def = new ChargeItemDefinition(); + var version = "1.0.0"; + def.setVersion(version); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(def); + assertTrue(adapter.hasVersion()); + assertEquals(version, adapter.getVersion()); + var newVersion = "1.0.1"; + adapter.setVersion(newVersion); + assertEquals(newVersion, def.getVersion()); + } + + @Test + void adapter_get_and_set_status() { + var def = new ChargeItemDefinition(); + var status = PublicationStatus.DRAFT; + def.setStatus(status); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(def); + assertEquals(status.toCode(), adapter.getStatus()); + assertThrows(UnprocessableEntityException.class, () -> adapter.setStatus("invalid-status")); + var newStatus = PublicationStatus.ACTIVE; + adapter.setStatus(newStatus.toCode()); + assertEquals(newStatus, PublicationStatus.fromCode(adapter.getStatus())); + } + + @Test + void adapter_get_and_set_dates() { + var def = new ChargeItemDefinition(); + var date = new Date(); + var approvalDate = new Date(); + def.setDate(date); + def.setApprovalDate(approvalDate); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(def); + assertEquals(date, adapter.getDate()); + assertEquals(approvalDate, adapter.getApprovalDate()); + var newDate = new Date(); + newDate.setTime(100); + adapter.setDate(newDate); + assertEquals(newDate, def.getDate()); + var newApprovalDate = new Date(); + newApprovalDate.setTime(100); + adapter.setApprovalDate(newApprovalDate); + assertEquals(newApprovalDate, def.getApprovalDate()); + } + + @Test + void adapter_get_experimental() { + var def = new ChargeItemDefinition(); + var experimental = true; + def.setExperimental(experimental); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(def); + assertEquals(experimental, adapter.getExperimental()); + } + + @Test + void adapter_set_relatedArtifact() { + var def = new ChargeItemDefinition(); + var relatedArtifactList = List.of(new RelatedArtifact()); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(def); + adapter.setRelatedArtifact(relatedArtifactList); + assertEquals(0, adapter.getRelatedArtifact().size()); + } + + @Test + void adapter_copy() { + var def = new ChargeItemDefinition().setStatus(PublicationStatus.DRAFT); + def.setId("def-1"); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(def); + var copy = (ChargeItemDefinition) adapter.copy(); + copy.setId("def-2"); + assertNotEquals(def.getId(), copy.getId()); + def.setStatus(PublicationStatus.ACTIVE); + assertNotEquals(adapter.getStatus(), copy.getStatus()); + } +} diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/LibraryAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/LibraryAdapterTest.java index 0f04db43c..851d9fb59 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/LibraryAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/LibraryAdapterTest.java @@ -34,7 +34,7 @@ import org.junit.jupiter.api.Test; import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; -public class LibraryAdapterTest { +class LibraryAdapterTest { private final FhirContext fhirContext = FhirContext.forR5Cached(); private final org.opencds.cqf.fhir.utility.adapter.AdapterFactory adapterFactory = new AdapterFactory(); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/PlanDefinitionAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/PlanDefinitionAdapterTest.java index 2a5eab057..84e693783 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/PlanDefinitionAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/PlanDefinitionAdapterTest.java @@ -28,7 +28,7 @@ import org.junit.jupiter.api.Test; import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; -public class PlanDefinitionAdapterTest { +class PlanDefinitionAdapterTest { private final FhirContext fhirContext = FhirContext.forR5Cached(); private final org.opencds.cqf.fhir.utility.adapter.AdapterFactory adapterFactory = new AdapterFactory(); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapterTest.java index 01e43ea0e..c2c2963ec 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapterTest.java @@ -30,7 +30,7 @@ import org.opencds.cqf.fhir.utility.Constants; import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; -public class QuestionnaireAdapterTest { +class QuestionnaireAdapterTest { private final FhirContext fhirContext = FhirContext.forR5Cached(); private final org.opencds.cqf.fhir.utility.adapter.AdapterFactory adapterFactory = new AdapterFactory(); @@ -150,10 +150,10 @@ void adapter_set_relatedArtifact() { @Test void adapter_copy() { var questionnaire = new Questionnaire().setStatus(PublicationStatus.DRAFT); - questionnaire.setId("plan-1"); + questionnaire.setId("question-1"); var adapter = adapterFactory.createKnowledgeArtifactAdapter(questionnaire); var copy = (Questionnaire) adapter.copy(); - copy.setId("plan-2"); + copy.setId("question-2"); assertNotEquals(questionnaire.getId(), copy.getId()); questionnaire.setStatus(PublicationStatus.ACTIVE); assertNotEquals(adapter.getStatus(), copy.getStatus()); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapterTest.java index 5eab66b4b..38afed7ac 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapterTest.java @@ -26,7 +26,7 @@ import org.opencds.cqf.fhir.utility.adapter.Adapter; import org.slf4j.LoggerFactory; -public class ResourceAdapterTest { +class ResourceAdapterTest { private final org.opencds.cqf.fhir.utility.adapter.AdapterFactory adapterFactory = new AdapterFactory(); @Test diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapterTest.java index 5ed8b6367..13de313ef 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapterTest.java @@ -29,7 +29,7 @@ import org.opencds.cqf.fhir.utility.Constants; import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; -public class StructureDefinitionAdapterTest { +class StructureDefinitionAdapterTest { private final FhirContext fhirContext = FhirContext.forR5Cached(); private final org.opencds.cqf.fhir.utility.adapter.AdapterFactory adapterFactory = new AdapterFactory(); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ValueSetAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ValueSetAdapterTest.java index e37f750a2..8e5b8efb2 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ValueSetAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ValueSetAdapterTest.java @@ -25,7 +25,7 @@ import org.junit.jupiter.api.Test; import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; -public class ValueSetAdapterTest { +class ValueSetAdapterTest { private final FhirContext fhirContext = FhirContext.forR5Cached(); private final org.opencds.cqf.fhir.utility.adapter.AdapterFactory adapterFactory = new AdapterFactory(); From 15d639bc8b1bb3a4550f25576a43d2e192b3fa41 Mon Sep 17 00:00:00 2001 From: Brenin Rhodes Date: Thu, 8 Aug 2024 16:02:11 -0600 Subject: [PATCH 26/38] add tests --- .../adapter/r5/StructureDefinitionAdapter.java | 13 +------------ .../r5/StructureDefinitionAdapterTest.java | 15 +++++++++++---- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapter.java index 85b8d94d0..152e4977e 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapter.java @@ -4,7 +4,6 @@ import java.util.ArrayList; import java.util.Date; import java.util.List; -import java.util.stream.Collectors; import org.hl7.fhir.exceptions.FHIRException; import org.hl7.fhir.instance.model.api.IBaseHasExtensions; import org.hl7.fhir.instance.model.api.ICompositeType; @@ -16,7 +15,6 @@ import org.hl7.fhir.r5.model.Expression; import org.hl7.fhir.r5.model.Period; import org.hl7.fhir.r5.model.RelatedArtifact; -import org.hl7.fhir.r5.model.RelatedArtifact.RelatedArtifactType; import org.hl7.fhir.r5.model.StructureDefinition; import org.opencds.cqf.fhir.utility.Constants; import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; @@ -218,9 +216,6 @@ public String getPurpose() { @Override public void setEffectivePeriod(ICompositeType effectivePeriod) { - if (effectivePeriod != null && !(effectivePeriod instanceof Period)) { - throw new UnprocessableEntityException("EffectivePeriod must be a valid " + Period.class.getName()); - } // do nothing } @@ -238,13 +233,7 @@ public List getRelatedArtifact() { @SuppressWarnings("unchecked") @Override public List getRelatedArtifactsOfType(String codeString) { - RelatedArtifactType type; - try { - type = RelatedArtifactType.fromCode(codeString); - } catch (FHIRException e) { - throw new UnprocessableEntityException("Invalid related artifact code"); - } - return getRelatedArtifact().stream().filter(ra -> ra.getType() == type).collect(Collectors.toList()); + return new ArrayList<>(); } @Override diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapterTest.java index 13de313ef..9fa1e3c6e 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/StructureDefinitionAdapterTest.java @@ -1,7 +1,9 @@ package org.opencds.cqf.fhir.utility.adapter.r5; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; @@ -18,12 +20,14 @@ import org.hl7.fhir.instance.model.api.IDomainResource; import org.hl7.fhir.r5.model.Bundle; import org.hl7.fhir.r5.model.DateTimeType; +import org.hl7.fhir.r5.model.DateType; import org.hl7.fhir.r5.model.ElementDefinition.ElementDefinitionBindingComponent; import org.hl7.fhir.r5.model.Enumerations.PublicationStatus; import org.hl7.fhir.r5.model.Expression; import org.hl7.fhir.r5.model.Library; import org.hl7.fhir.r5.model.Period; import org.hl7.fhir.r5.model.RelatedArtifact; +import org.hl7.fhir.r5.model.RelatedArtifact.RelatedArtifactType; import org.hl7.fhir.r5.model.StructureDefinition; import org.junit.jupiter.api.Test; import org.opencds.cqf.fhir.utility.Constants; @@ -36,6 +40,7 @@ class StructureDefinitionAdapterTest { @Test void invalid_object_fails() { assertThrows(IllegalArgumentException.class, () -> new StructureDefinitionAdapter(new Library())); + assertNotNull(new StructureDefinitionAdapter((IDomainResource) new StructureDefinition())); } @Test @@ -118,6 +123,7 @@ void adapter_get_and_set_dates() { newDate.setTime(100); adapter.setDate(newDate); assertEquals(newDate, structureDef.getDate()); + assertThrows(UnprocessableEntityException.class, () -> adapter.setDateElement(new DateType())); var newDateElement = new DateTimeType().setValue(new Date()); adapter.setDateElement(newDateElement); assertEquals(newDateElement, structureDef.getDateElement()); @@ -144,24 +150,25 @@ void adapter_get_experimental() { @Test void adapter_set_relatedArtifact() { var structureDef = new StructureDefinition(); - var relatedArtifactList = List.of(new RelatedArtifact()); + var relatedArtifactList = List.of(new RelatedArtifact().setType(RelatedArtifactType.DEPENDSON)); var adapter = adapterFactory.createKnowledgeArtifactAdapter(structureDef); + assertFalse(adapter.hasRelatedArtifact()); adapter.setRelatedArtifact(relatedArtifactList); assertEquals(0, adapter.getRelatedArtifact().size()); - assertThrows(UnprocessableEntityException.class, () -> adapter.getRelatedArtifactsOfType("invalid")); assertEquals(0, adapter.getRelatedArtifactsOfType("depends-on").size()); } @Test void adapter_copy() { - var structureDef = new StructureDefinition().setStatus(PublicationStatus.DRAFT); + var structureDef = new StructureDefinition(); structureDef.setId("plan-1"); var adapter = adapterFactory.createKnowledgeArtifactAdapter(structureDef); var copy = (StructureDefinition) adapter.copy(); copy.setId("plan-2"); assertNotEquals(structureDef.getId(), copy.getId()); + assertEquals(copy.getStatus(), PublicationStatus.fromCode(adapter.getStatus())); structureDef.setStatus(PublicationStatus.ACTIVE); - assertNotEquals(adapter.getStatus(), copy.getStatus()); + assertNotEquals(PublicationStatus.fromCode(adapter.getStatus()), copy.getStatus()); } @Test From e68aadd770d76d3f85fb5da5c40c90d60ddec95a Mon Sep 17 00:00:00 2001 From: Brenin Rhodes Date: Tue, 13 Aug 2024 09:03:14 -0600 Subject: [PATCH 27/38] cleanup and tests --- .../utility/adapter/QuestionnaireAdapter.java | 23 ++++++++++++++++ .../adapter/dstu3/AttachmentAdapter.java | 3 +-- .../adapter/dstu3/EndpointAdapter.java | 8 ------ .../adapter/dstu3/QuestionnaireAdapter.java | 26 +++---------------- .../utility/adapter/r4/AttachmentAdapter.java | 2 +- .../utility/adapter/r4/EndpointAdapter.java | 8 ------ .../adapter/r4/QuestionnaireAdapter.java | 19 +++----------- .../utility/adapter/r5/EndpointAdapter.java | 4 --- .../adapter/r5/QuestionnaireAdapter.java | 19 +++----------- .../dstu3/QuestionnaireAdapterTest.java | 25 +++++++++++++----- .../r4/KnowledgeArtifactAdapterTest.java | 14 +++++++++- .../r4/StructureDefinitionAdapterTest.java | 2 ++ .../r5/KnowledgeArtifactAdapterTest.java | 19 +++++++++++++- 13 files changed, 87 insertions(+), 85 deletions(-) create mode 100644 cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/QuestionnaireAdapter.java diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/QuestionnaireAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/QuestionnaireAdapter.java new file mode 100644 index 000000000..80e9108cb --- /dev/null +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/QuestionnaireAdapter.java @@ -0,0 +1,23 @@ +package org.opencds.cqf.fhir.utility.adapter; + +import java.util.Arrays; +import java.util.List; +import org.opencds.cqf.fhir.utility.Constants; + +/** + * This interface exposes common functionality across all FHIR Questionnaire versions. + */ +public interface QuestionnaireAdapter { + public static List REFERENCE_EXTENSIONS = Arrays.asList( + Constants.QUESTIONNAIRE_UNIT_VALUE_SET, + Constants.QUESTIONNAIRE_REFERENCE_PROFILE, + Constants.SDC_QUESTIONNAIRE_LOOKUP_QUESTIONNAIRE, + Constants.SDC_QUESTIONNAIRE_SUB_QUESTIONNAIRE); + + public static List EXPRESSION_EXTENSIONS = Arrays.asList( + Constants.VARIABLE_EXTENSION, + Constants.SDC_QUESTIONNAIRE_CANDIDATE_EXPRESSION, + Constants.SDC_QUESTIONNAIRE_INITIAL_EXPRESSION, + Constants.SDC_QUESTIONNAIRE_CALCULATED_EXPRESSION, + Constants.CQF_EXPRESSION); +} diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/AttachmentAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/AttachmentAdapter.java index 3b58abcc6..0512a5e49 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/AttachmentAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/AttachmentAdapter.java @@ -1,7 +1,6 @@ package org.opencds.cqf.fhir.utility.adapter.dstu3; import ca.uhn.fhir.context.FhirContext; -import ca.uhn.fhir.context.FhirVersionEnum; import org.hl7.fhir.dstu3.model.Attachment; import org.hl7.fhir.instance.model.api.ICompositeType; import org.opencds.cqf.cql.engine.model.ModelResolver; @@ -27,7 +26,7 @@ public AttachmentAdapter(ICompositeType attachment) { } this.attachment = (Attachment) attachment; - fhirContext = FhirContext.forCached(FhirVersionEnum.R5); + fhirContext = FhirContext.forDstu3Cached(); modelResolver = FhirModelResolverCache.resolverForVersion( fhirContext.getVersion().getVersion()); } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/EndpointAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/EndpointAdapter.java index d3b13dc59..ea60fb1cb 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/EndpointAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/EndpointAdapter.java @@ -10,12 +10,4 @@ public EndpointAdapter(IBaseResource endpoint) { throw new IllegalArgumentException("resource passed as endpoint argument is not an Endpoint resource"); } } - - public EndpointAdapter(Endpoint endpoint) { - super(endpoint); - } - - public Endpoint getEndpoint() { - return (Endpoint) resource; - } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/QuestionnaireAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/QuestionnaireAdapter.java index 2721676de..c88c2a2a8 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/QuestionnaireAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/QuestionnaireAdapter.java @@ -1,7 +1,6 @@ package org.opencds.cqf.fhir.utility.adapter.dstu3; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import org.hl7.fhir.dstu3.model.Questionnaire; import org.hl7.fhir.dstu3.model.Questionnaire.QuestionnaireItemComponent; @@ -12,7 +11,8 @@ import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; -public class QuestionnaireAdapter extends KnowledgeArtifactAdapter { +public class QuestionnaireAdapter extends KnowledgeArtifactAdapter + implements org.opencds.cqf.fhir.utility.adapter.QuestionnaireAdapter { public QuestionnaireAdapter(IDomainResource questionnaire) { super(questionnaire); @@ -109,33 +109,13 @@ private void getDependenciesOfItem( item.getExtension(), (reference) -> item.setOptions(new Reference(reference)))); } - var referenceExtensions = Arrays.asList( - Constants.QUESTIONNAIRE_UNIT_VALUE_SET, - Constants.QUESTIONNAIRE_REFERENCE_PROFILE, - Constants.SDC_QUESTIONNAIRE_LOOKUP_QUESTIONNAIRE, - Constants.SDC_QUESTIONNAIRE_SUB_QUESTIONNAIRE); item.getExtension().stream() - .filter(e -> referenceExtensions.contains(e.getUrl())) + .filter(e -> REFERENCE_EXTENSIONS.contains(e.getUrl())) .forEach(referenceExt -> references.add(new DependencyInfo( referenceSource, ((UriType) referenceExt.getValue()).asStringValue(), referenceExt.getExtension(), (reference) -> referenceExt.setValue(new UriType(reference))))); - // var expressionExtensions = Arrays.asList( - // Constants.VARIABLE_EXTENSION, - // Constants.SDC_QUESTIONNAIRE_CANDIDATE_EXPRESSION, - // Constants.SDC_QUESTIONNAIRE_INITIAL_EXPRESSION, - // Constants.SDC_QUESTIONNAIRE_CALCULATED_EXPRESSION, - // Constants.CQF_EXPRESSION); - // item.getExtension().stream() - // .filter(e -> expressionExtensions.contains(e.getUrl())) - // .map(e -> (Expression) e.getValue()) - // .filter(e -> e.hasReference()) - // .forEach(expression -> references.add(new DependencyInfo( - // referenceSource, - // expression.getReference(), - // expression.getExtension(), - // (reference) -> expression.setReference(reference)))); item.getItem().forEach(childItem -> getDependenciesOfItem(childItem, references, referenceSource)); } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/AttachmentAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/AttachmentAdapter.java index e22733f55..cbd0e3584 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/AttachmentAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/AttachmentAdapter.java @@ -26,7 +26,7 @@ public AttachmentAdapter(ICompositeType attachment) { } this.attachment = (Attachment) attachment; - fhirContext = FhirContext.forR5Cached(); + fhirContext = FhirContext.forR4Cached(); modelResolver = FhirModelResolverCache.resolverForVersion( fhirContext.getVersion().getVersion()); } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/EndpointAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/EndpointAdapter.java index 32b557065..76a283fed 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/EndpointAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/EndpointAdapter.java @@ -10,12 +10,4 @@ public EndpointAdapter(IBaseResource endpoint) { throw new IllegalArgumentException("resource passed as endpoint argument is not an Endpoint resource"); } } - - public EndpointAdapter(Endpoint endpoint) { - super(endpoint); - } - - public Endpoint getEndpoint() { - return (Endpoint) resource; - } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/QuestionnaireAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/QuestionnaireAdapter.java index b701b6e8a..7056d3c01 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/QuestionnaireAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/QuestionnaireAdapter.java @@ -1,7 +1,6 @@ package org.opencds.cqf.fhir.utility.adapter.r4; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import org.hl7.fhir.instance.model.api.IDomainResource; import org.hl7.fhir.r4.model.CanonicalType; @@ -12,7 +11,8 @@ import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; -public class QuestionnaireAdapter extends KnowledgeArtifactAdapter { +public class QuestionnaireAdapter extends KnowledgeArtifactAdapter + implements org.opencds.cqf.fhir.utility.adapter.QuestionnaireAdapter { public QuestionnaireAdapter(IDomainResource questionnaire) { super(questionnaire); @@ -114,26 +114,15 @@ private void getDependenciesOfItem( item.getExtension(), (reference) -> item.setAnswerValueSet(reference))); } - var referenceExtensions = Arrays.asList( - Constants.QUESTIONNAIRE_UNIT_VALUE_SET, - Constants.QUESTIONNAIRE_REFERENCE_PROFILE, - Constants.SDC_QUESTIONNAIRE_LOOKUP_QUESTIONNAIRE, - Constants.SDC_QUESTIONNAIRE_SUB_QUESTIONNAIRE); item.getExtension().stream() - .filter(e -> referenceExtensions.contains(e.getUrl())) + .filter(e -> REFERENCE_EXTENSIONS.contains(e.getUrl())) .forEach(referenceExt -> references.add(new DependencyInfo( referenceSource, ((CanonicalType) referenceExt.getValue()).asStringValue(), referenceExt.getExtension(), (reference) -> referenceExt.setValue(new CanonicalType(reference))))); - var expressionExtensions = Arrays.asList( - Constants.VARIABLE_EXTENSION, - Constants.SDC_QUESTIONNAIRE_CANDIDATE_EXPRESSION, - Constants.SDC_QUESTIONNAIRE_INITIAL_EXPRESSION, - Constants.SDC_QUESTIONNAIRE_CALCULATED_EXPRESSION, - Constants.CQF_EXPRESSION); item.getExtension().stream() - .filter(e -> expressionExtensions.contains(e.getUrl())) + .filter(e -> EXPRESSION_EXTENSIONS.contains(e.getUrl())) .map(e -> (Expression) e.getValue()) .filter(e -> e.hasReference()) .forEach(expression -> references.add(new DependencyInfo( diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/EndpointAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/EndpointAdapter.java index 48e22e1a8..b986a786a 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/EndpointAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/EndpointAdapter.java @@ -10,8 +10,4 @@ public EndpointAdapter(IBaseResource endpoint) { throw new IllegalArgumentException("resource passed as endpoint argument is not an Endpoint resource"); } } - - public EndpointAdapter(Endpoint endpoint) { - super(endpoint); - } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapter.java index 06669484c..e201cff85 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/QuestionnaireAdapter.java @@ -1,7 +1,6 @@ package org.opencds.cqf.fhir.utility.adapter.r5; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import org.hl7.fhir.instance.model.api.IDomainResource; import org.hl7.fhir.r5.model.CanonicalType; @@ -12,7 +11,8 @@ import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; -public class QuestionnaireAdapter extends KnowledgeArtifactAdapter { +public class QuestionnaireAdapter extends KnowledgeArtifactAdapter + implements org.opencds.cqf.fhir.utility.adapter.QuestionnaireAdapter { public QuestionnaireAdapter(IDomainResource questionnaire) { super(questionnaire); @@ -114,26 +114,15 @@ private void getDependenciesOfItem( item.getExtension(), (reference) -> item.setAnswerValueSet(reference))); } - var referenceExtensions = Arrays.asList( - Constants.QUESTIONNAIRE_UNIT_VALUE_SET, - Constants.QUESTIONNAIRE_REFERENCE_PROFILE, - Constants.SDC_QUESTIONNAIRE_LOOKUP_QUESTIONNAIRE, - Constants.SDC_QUESTIONNAIRE_SUB_QUESTIONNAIRE); item.getExtension().stream() - .filter(e -> referenceExtensions.contains(e.getUrl())) + .filter(e -> REFERENCE_EXTENSIONS.contains(e.getUrl())) .forEach(referenceExt -> references.add(new DependencyInfo( referenceSource, ((CanonicalType) referenceExt.getValue()).asStringValue(), referenceExt.getExtension(), (reference) -> referenceExt.setValue(new CanonicalType(reference))))); - var expressionExtensions = Arrays.asList( - Constants.VARIABLE_EXTENSION, - Constants.SDC_QUESTIONNAIRE_CANDIDATE_EXPRESSION, - Constants.SDC_QUESTIONNAIRE_INITIAL_EXPRESSION, - Constants.SDC_QUESTIONNAIRE_CALCULATED_EXPRESSION, - Constants.CQF_EXPRESSION); item.getExtension().stream() - .filter(e -> expressionExtensions.contains(e.getUrl())) + .filter(e -> EXPRESSION_EXTENSIONS.contains(e.getUrl())) .map(e -> (Expression) e.getValue()) .filter(e -> e.hasReference()) .forEach(expression -> references.add(new DependencyInfo( diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/QuestionnaireAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/QuestionnaireAdapterTest.java index 7d3e2cc16..5cc582b64 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/QuestionnaireAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/QuestionnaireAdapterTest.java @@ -22,6 +22,7 @@ import org.hl7.fhir.dstu3.model.Questionnaire; import org.hl7.fhir.dstu3.model.Reference; import org.hl7.fhir.dstu3.model.RelatedArtifact; +import org.hl7.fhir.dstu3.model.UriType; import org.hl7.fhir.instance.model.api.IDomainResource; import org.junit.jupiter.api.Test; import org.opencds.cqf.fhir.utility.Constants; @@ -162,21 +163,20 @@ void adapter_get_all_dependencies() { "profileRef", "cqfLibraryRef", // "variableRef", - "itemDefinitionRef" - // "answerValueSetRef", + "itemDefinitionRef", + "answerValueSetRef", // "itemMediaRef", // "itemAnswerMediaRef", - // "unitValueSetRef", - // "referenceProfileRef", + "unitValueSetRef", + "referenceProfileRef", // "candidateExpressionRef", - // "lookupQuestionnaireRef", + "lookupQuestionnaireRef", // "itemVariableRef", // "initialExpressionRef", // "calculatedExpressionRef", // "calculatedValueRef", // "expressionRef", - // "subQuestionnaireRef" - ); + "subQuestionnaireRef"); var questionnaire = new Questionnaire(); questionnaire.getMeta().addProfile(dependencies.get(0)); questionnaire.addExtension(Constants.CQIF_LIBRARY, new Reference(dependencies.get(1))); @@ -184,6 +184,17 @@ void adapter_get_all_dependencies() { // Expression().setReference(dependencies.get(2))); // questionnaire.addExtension(variableExt); questionnaire.addItem().setDefinition(dependencies.get(2) + "#Observation"); + questionnaire.addItem().setOptions(new Reference(dependencies.get(3))); + questionnaire.addItem().addExtension(Constants.QUESTIONNAIRE_UNIT_VALUE_SET, new UriType(dependencies.get(4))); + questionnaire + .addItem() + .addExtension(Constants.QUESTIONNAIRE_REFERENCE_PROFILE, new UriType(dependencies.get(5))); + questionnaire + .addItem() + .addExtension(Constants.SDC_QUESTIONNAIRE_LOOKUP_QUESTIONNAIRE, new UriType(dependencies.get(6))); + questionnaire + .addItem() + .addExtension(Constants.SDC_QUESTIONNAIRE_SUB_QUESTIONNAIRE, new UriType(dependencies.get(7))); var adapter = adapterFactory.createKnowledgeArtifactAdapter(questionnaire); var extractedDependencies = adapter.getDependencies(); assertEquals(dependencies.size(), extractedDependencies.size()); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/KnowledgeArtifactAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/KnowledgeArtifactAdapterTest.java index 0b5daf1c1..b0a99fb3b 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/KnowledgeArtifactAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/KnowledgeArtifactAdapterTest.java @@ -13,13 +13,18 @@ import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; +import java.util.Arrays; import java.util.Date; import java.util.List; import org.hl7.fhir.instance.model.api.IDomainResource; import org.hl7.fhir.r4.model.Bundle; import org.hl7.fhir.r4.model.CompartmentDefinition; +import org.hl7.fhir.r4.model.DateTimeType; +import org.hl7.fhir.r4.model.DateType; import org.hl7.fhir.r4.model.Enumerations.PublicationStatus; +import org.hl7.fhir.r4.model.Extension; import org.hl7.fhir.r4.model.RelatedArtifact; +import org.hl7.fhir.r5.model.Period; import org.junit.jupiter.api.Test; import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; @@ -31,7 +36,7 @@ class KnowledgeArtifactAdapterTest { void invalid_object_fails() { assertThrows( UnprocessableEntityException.class, - () -> adapterFactory.createKnowledgeArtifactAdapter(new org.hl7.fhir.r5.model.Library())); + () -> new KnowledgeArtifactAdapter(new org.hl7.fhir.r5.model.Library())); } @Test @@ -112,6 +117,11 @@ void adapter_get_and_set_dates() { newDate.setTime(100); adapter.setDate(newDate); assertEquals(newDate, def.getDate()); + assertThrows(UnprocessableEntityException.class, () -> adapter.setDateElement(new DateType())); + var newDateElement = new DateTimeType().setValue(new Date()); + adapter.setDateElement(newDateElement); + assertEquals(newDateElement, def.getDateElement()); + assertThrows(UnprocessableEntityException.class, () -> adapter.setEffectivePeriod(new Extension())); } @Test @@ -130,6 +140,8 @@ void adapter_set_relatedArtifact() { var adapter = adapterFactory.createKnowledgeArtifactAdapter(def); adapter.setRelatedArtifact(relatedArtifactList); assertEquals(0, adapter.getRelatedArtifact().size()); + assertThrows(UnprocessableEntityException.class, () -> adapter.setRelatedArtifact(Arrays.asList(new Period()))); + assertThrows(UnprocessableEntityException.class, () -> adapter.getRelatedArtifactsOfType("depends")); } @Test diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/StructureDefinitionAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/StructureDefinitionAdapterTest.java index 5461ad78a..100795adb 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/StructureDefinitionAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/StructureDefinitionAdapterTest.java @@ -2,6 +2,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; @@ -35,6 +36,7 @@ public class StructureDefinitionAdapterTest { @Test void invalid_object_fails() { assertThrows(IllegalArgumentException.class, () -> new StructureDefinitionAdapter(new Library())); + assertNotNull(new StructureDefinitionAdapter((IDomainResource) new StructureDefinition())); } @Test diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/KnowledgeArtifactAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/KnowledgeArtifactAdapterTest.java index f84751f26..021c973ea 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/KnowledgeArtifactAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/KnowledgeArtifactAdapterTest.java @@ -13,12 +13,17 @@ import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; +import java.util.Arrays; import java.util.Date; import java.util.List; import org.hl7.fhir.instance.model.api.IDomainResource; import org.hl7.fhir.r5.model.Bundle; import org.hl7.fhir.r5.model.ChargeItemDefinition; +import org.hl7.fhir.r5.model.DateTimeType; +import org.hl7.fhir.r5.model.DateType; import org.hl7.fhir.r5.model.Enumerations.PublicationStatus; +import org.hl7.fhir.r5.model.Extension; +import org.hl7.fhir.r5.model.Period; import org.hl7.fhir.r5.model.RelatedArtifact; import org.junit.jupiter.api.Test; import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; @@ -31,7 +36,7 @@ class KnowledgeArtifactAdapterTest { void invalid_object_fails() { assertThrows( UnprocessableEntityException.class, - () -> adapterFactory.createKnowledgeArtifactAdapter(new org.hl7.fhir.r4.model.Library())); + () -> new KnowledgeArtifactAdapter(new org.hl7.fhir.r4.model.Library())); } @Test @@ -115,6 +120,16 @@ void adapter_get_and_set_dates() { newDate.setTime(100); adapter.setDate(newDate); assertEquals(newDate, def.getDate()); + assertThrows(UnprocessableEntityException.class, () -> adapter.setDateElement(new DateType())); + var newDateElement = new DateTimeType().setValue(new Date()); + adapter.setDateElement(newDateElement); + assertEquals(newDateElement, def.getDateElement()); + assertThrows(UnprocessableEntityException.class, () -> adapter.setEffectivePeriod(new Extension())); + var newEffectivePeriod = new Period(); + newEffectivePeriod.setStart(new Date()); + newEffectivePeriod.setEnd(new Date()); + adapter.setEffectivePeriod(newEffectivePeriod); + assertThrows(Error.class, () -> def.getEffectivePeriod()); var newApprovalDate = new Date(); newApprovalDate.setTime(100); adapter.setApprovalDate(newApprovalDate); @@ -137,6 +152,8 @@ void adapter_set_relatedArtifact() { var adapter = adapterFactory.createKnowledgeArtifactAdapter(def); adapter.setRelatedArtifact(relatedArtifactList); assertEquals(0, adapter.getRelatedArtifact().size()); + assertThrows(UnprocessableEntityException.class, () -> adapter.setRelatedArtifact(Arrays.asList(new Period()))); + assertThrows(UnprocessableEntityException.class, () -> adapter.getRelatedArtifactsOfType("depends")); } @Test From c6fff892c41dcc2f76dc3f134266375ae2c6c8ac Mon Sep 17 00:00:00 2001 From: Brenin Rhodes Date: Tue, 13 Aug 2024 09:08:07 -0600 Subject: [PATCH 28/38] cleanup --- .../dstu3/KnowledgeArtifactAdapterTest.java | 16 ++++++++++++++-- .../adapter/r4/KnowledgeArtifactAdapterTest.java | 4 ++-- .../adapter/r5/KnowledgeArtifactAdapterTest.java | 2 +- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/KnowledgeArtifactAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/KnowledgeArtifactAdapterTest.java index a4d810f4e..9553223ec 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/KnowledgeArtifactAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/KnowledgeArtifactAdapterTest.java @@ -13,11 +13,16 @@ import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; +import java.util.Arrays; import java.util.Date; import java.util.List; import org.hl7.fhir.dstu3.model.Bundle; import org.hl7.fhir.dstu3.model.CompartmentDefinition; +import org.hl7.fhir.dstu3.model.DateTimeType; +import org.hl7.fhir.dstu3.model.DateType; import org.hl7.fhir.dstu3.model.Enumerations.PublicationStatus; +import org.hl7.fhir.dstu3.model.Extension; +import org.hl7.fhir.dstu3.model.Period; import org.hl7.fhir.dstu3.model.RelatedArtifact; import org.hl7.fhir.instance.model.api.IDomainResource; import org.junit.jupiter.api.Test; @@ -30,8 +35,8 @@ class KnowledgeArtifactAdapterTest { @Test void invalid_object_fails() { assertThrows( - UnprocessableEntityException.class, - () -> adapterFactory.createKnowledgeArtifactAdapter(new org.hl7.fhir.r4.model.Library())); + IllegalArgumentException.class, + () -> new KnowledgeArtifactAdapter(new org.hl7.fhir.r4.model.Library())); } @Test @@ -112,6 +117,11 @@ void adapter_get_and_set_dates() { newDate.setTime(100); adapter.setDate(newDate); assertEquals(newDate, def.getDate()); + assertThrows(UnprocessableEntityException.class, () -> adapter.setDateElement(new DateType())); + var newDateElement = new DateTimeType().setValue(new Date()); + adapter.setDateElement(newDateElement); + assertEquals(newDateElement, def.getDateElement()); + assertThrows(UnprocessableEntityException.class, () -> adapter.setEffectivePeriod(new Extension())); } @Test @@ -130,6 +140,8 @@ void adapter_set_relatedArtifact() { var adapter = adapterFactory.createKnowledgeArtifactAdapter(def); adapter.setRelatedArtifact(relatedArtifactList); assertEquals(0, adapter.getRelatedArtifact().size()); + assertThrows(UnprocessableEntityException.class, () -> adapter.setRelatedArtifact(Arrays.asList(new Period()))); + assertThrows(UnprocessableEntityException.class, () -> adapter.getRelatedArtifactsOfType("depends")); } @Test diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/KnowledgeArtifactAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/KnowledgeArtifactAdapterTest.java index b0a99fb3b..cfb9dffed 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/KnowledgeArtifactAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/KnowledgeArtifactAdapterTest.java @@ -23,8 +23,8 @@ import org.hl7.fhir.r4.model.DateType; import org.hl7.fhir.r4.model.Enumerations.PublicationStatus; import org.hl7.fhir.r4.model.Extension; +import org.hl7.fhir.r4.model.Period; import org.hl7.fhir.r4.model.RelatedArtifact; -import org.hl7.fhir.r5.model.Period; import org.junit.jupiter.api.Test; import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; @@ -35,7 +35,7 @@ class KnowledgeArtifactAdapterTest { @Test void invalid_object_fails() { assertThrows( - UnprocessableEntityException.class, + IllegalArgumentException.class, () -> new KnowledgeArtifactAdapter(new org.hl7.fhir.r5.model.Library())); } diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/KnowledgeArtifactAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/KnowledgeArtifactAdapterTest.java index 021c973ea..e96386f90 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/KnowledgeArtifactAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/KnowledgeArtifactAdapterTest.java @@ -35,7 +35,7 @@ class KnowledgeArtifactAdapterTest { @Test void invalid_object_fails() { assertThrows( - UnprocessableEntityException.class, + IllegalArgumentException.class, () -> new KnowledgeArtifactAdapter(new org.hl7.fhir.r4.model.Library())); } From 6f2f820f2ed8056cd5af9c69d3137eca1ef4b5e8 Mon Sep 17 00:00:00 2001 From: Brenin Rhodes Date: Tue, 13 Aug 2024 10:46:07 -0600 Subject: [PATCH 29/38] add tests --- .../adapter/dstu3/ResourceAdapter.java | 4 -- .../cqf/fhir/utility/CqfExpressionTests.java | 60 ++++++++++++++++++- .../utility/adapter/AdapterFactoryTest.java | 26 ++++++++ .../adapter/dstu3/EndpointAdapterTest.java | 2 +- .../adapter/dstu3/ResourceAdapterTest.java | 10 +++- .../adapter/r4/ResourceAdapterTest.java | 10 +++- .../adapter/r5/ResourceAdapterTest.java | 10 +++- 7 files changed, 112 insertions(+), 10 deletions(-) create mode 100644 cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/AdapterFactoryTest.java diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapter.java index ab81ace8c..04cd80bae 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapter.java @@ -37,10 +37,6 @@ protected Optional getDomainResource() { return ofNullable(resource instanceof DomainResource ? (DomainResource) resource : null); } - public IBaseResource get() { - return resource; - } - @Override public IBase setProperty(String name, IBase value) throws FHIRException { return getResource().setProperty(name, (Base) value); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/CqfExpressionTests.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/CqfExpressionTests.java index 9fe27fa8c..12f2570c6 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/CqfExpressionTests.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/CqfExpressionTests.java @@ -3,7 +3,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; -import org.hl7.fhir.dstu2.model.Extension; import org.hl7.fhir.instance.model.api.IBaseExtension; import org.junit.jupiter.api.Test; @@ -12,8 +11,33 @@ class CqfExpressionTests { void testInvalidExtension() { var cqfExpression = CqfExpression.of((IBaseExtension) null, null); assertNull(cqfExpression); - var dstu2Expression = CqfExpression.of(new Extension(), null); + var dstu2Expression = CqfExpression.of(new org.hl7.fhir.dstu2.model.Extension(), null); assertNull(dstu2Expression); + assertNull(CqfExpression.of(new org.hl7.fhir.r4.model.Extension(), null)); + assertNull(CqfExpression.of(new org.hl7.fhir.r5.model.Extension(), null)); + } + + @Test + void testCqfExpression() { + var language = "text/cql"; + var expression = "expression"; + var libraryUrl = "Library/lib"; + var altLanguage = "text/fhirpath"; + var altExpression = "altExpression"; + var altLibraryUrl = "Library/alt"; + var cqfExpression = new CqfExpression(); + cqfExpression.setLanguage(language); + cqfExpression.setExpression(expression); + cqfExpression.setLibraryUrl(libraryUrl); + cqfExpression.setAltLanguage(altLanguage); + cqfExpression.setAltExpression(altExpression); + cqfExpression.setAltLibraryUrl(altLibraryUrl); + assertEquals(language, cqfExpression.getLanguage()); + assertEquals(expression, cqfExpression.getExpression()); + assertEquals(libraryUrl, cqfExpression.getLibraryUrl()); + assertEquals(altLanguage, cqfExpression.getAltLanguage()); + assertEquals(altExpression, cqfExpression.getAltExpression()); + assertEquals(altLibraryUrl, cqfExpression.getAltLibraryUrl()); } @Test @@ -26,6 +50,22 @@ void testDstu3Extension() { @Test void testR4Extension() { + var defaultLibraryUrl = "http://test.com/Library/test"; + var expression = new org.hl7.fhir.r4.model.Expression() + .setLanguage("text/cql.identifier") + .setExpression("expression"); + var ext = new org.hl7.fhir.r4.model.Extension(Constants.CQF_EXPRESSION, expression); + var cqfExpression = CqfExpression.of(ext, defaultLibraryUrl); + assertEquals(expression.getExpression(), cqfExpression.getExpression()); + assertEquals(expression.getLanguage(), cqfExpression.getLanguage()); + assertEquals(defaultLibraryUrl, cqfExpression.getLibraryUrl()); + assertNull(cqfExpression.getAltExpression()); + assertNull(cqfExpression.getAltLanguage()); + assertNull(cqfExpression.getAltLibraryUrl()); + } + + @Test + void testR4ExtensionWithAlternate() { var defaultLibraryUrl = "http://test.com/Library/test"; var expression = new org.hl7.fhir.r4.model.Expression() .setLanguage("text/cql.identifier") @@ -46,6 +86,22 @@ void testR4Extension() { @Test void testR5Extension() { + var defaultLibraryUrl = "http://test.com/Library/test"; + var expression = new org.hl7.fhir.r5.model.Expression() + .setLanguage("text/cql.identifier") + .setExpression("expression"); + var ext = new org.hl7.fhir.r5.model.Extension(Constants.CQF_EXPRESSION, expression); + var cqfExpression = CqfExpression.of(ext, defaultLibraryUrl); + assertEquals(expression.getExpression(), cqfExpression.getExpression()); + assertEquals(expression.getLanguage(), cqfExpression.getLanguage()); + assertEquals(defaultLibraryUrl, cqfExpression.getLibraryUrl()); + assertNull(cqfExpression.getAltExpression()); + assertNull(cqfExpression.getAltLanguage()); + assertNull(cqfExpression.getAltLibraryUrl()); + } + + @Test + void testR5ExtensionWithAlternate() { var defaultLibraryUrl = "http://test.com/Library/test"; var expression = new org.hl7.fhir.r5.model.Expression() .setLanguage("text/cql.identifier") diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/AdapterFactoryTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/AdapterFactoryTest.java new file mode 100644 index 000000000..3cc9a44c6 --- /dev/null +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/AdapterFactoryTest.java @@ -0,0 +1,26 @@ +package org.opencds.cqf.fhir.utility.adapter; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.hl7.fhir.r4.model.Patient; +import org.junit.jupiter.api.Test; + +import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.context.FhirVersionEnum; + +class AdapterFactoryTest { + @Test + void testUnsupportedVersion() { + assertThrows(IllegalArgumentException.class, () -> AdapterFactory.forFhirVersion(FhirVersionEnum.DSTU2)); + } + + @Test + void testFhirContext() { + var context = FhirContext.forR4Cached(); + var adapterFactory = AdapterFactory.forFhirContext(context); + assertNotNull(adapterFactory); + var adapter = adapterFactory.createResource(new Patient()); + assertNotNull(adapter); + } +} diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/EndpointAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/EndpointAdapterTest.java index b9df774e8..fb7ed47b6 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/EndpointAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/EndpointAdapterTest.java @@ -12,7 +12,7 @@ public class EndpointAdapterTest { @Test void invalid_object_fails() { - assertThrows(IllegalArgumentException.class, () -> new LibraryAdapter(new PlanDefinition())); + assertThrows(IllegalArgumentException.class, () -> new EndpointAdapter(new PlanDefinition())); } @Test diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapterTest.java index 77bd1223a..c10fd3d1c 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/ResourceAdapterTest.java @@ -3,6 +3,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -16,6 +17,7 @@ import org.hl7.fhir.dstu3.model.BooleanType; import org.hl7.fhir.dstu3.model.Bundle; import org.hl7.fhir.dstu3.model.Extension; +import org.hl7.fhir.dstu3.model.HumanName; import org.hl7.fhir.dstu3.model.IdType; import org.hl7.fhir.dstu3.model.Library; import org.hl7.fhir.dstu3.model.Meta; @@ -41,16 +43,22 @@ void adapter_get_and_set_property() { var id = new IdDt("patient-1"); resource.setId(id); var adapter = adapterFactory.createResource(resource); + assertTrue(((ResourceAdapter) adapter).isDomainResource()); + assertEquals(resource, ((ResourceAdapter) adapter).getDomainResource().get()); assertEquals(id.getValue(), ((IIdType) adapter.getSingleProperty("id")).getValue()); var newId = new IdType("patient-2"); adapter.setProperty("id", newId); assertEquals(newId, resource.getIdElement()); assertEquals("id", adapter.getTypesForProperty("id")[0]); assertNotNull(adapter.makeProperty("language")); + assertNull(adapter.getSingleProperty("meta")); var meta = (Meta) adapter.addChild("meta"); var date = new Date(); meta.setLastUpdated(date); - assertEquals(date, ((Meta) adapter.getSingleProperty("meta")).getLastUpdated()); + assertEquals(date, ((Meta) adapter.getProperty("meta")[0]).getLastUpdated()); + resource.addName(new HumanName().addGiven("name1")); + resource.addName(new HumanName().addGiven("name2")); + assertThrows(IllegalArgumentException.class, () -> adapter.getSingleProperty("name")); } @Test diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/ResourceAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/ResourceAdapterTest.java index 0add91c2a..d789de86f 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/ResourceAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/ResourceAdapterTest.java @@ -2,6 +2,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -17,6 +18,7 @@ import org.hl7.fhir.r4.model.BooleanType; import org.hl7.fhir.r4.model.Bundle; import org.hl7.fhir.r4.model.Extension; +import org.hl7.fhir.r4.model.HumanName; import org.hl7.fhir.r4.model.IdType; import org.hl7.fhir.r4.model.Library; import org.hl7.fhir.r4.model.Meta; @@ -41,16 +43,22 @@ void adapter_get_and_set_property() { var id = new IdDt("patient-1"); resource.setId(id); var adapter = adapterFactory.createResource(resource); + assertTrue(((ResourceAdapter) adapter).isDomainResource()); + assertEquals(resource, ((ResourceAdapter) adapter).getDomainResource().get()); assertEquals(id.getValue(), ((IIdType) adapter.getSingleProperty("id")).getValue()); var newId = new IdType("patient-2"); adapter.setProperty("id", newId); assertEquals(newId, resource.getIdElement()); assertEquals("id", adapter.getTypesForProperty("id")[0]); assertNotNull(adapter.makeProperty("language")); + assertNull(adapter.getSingleProperty("meta")); var meta = (Meta) adapter.addChild("meta"); var date = new Date(); meta.setLastUpdated(date); - assertEquals(date, ((Meta) adapter.getSingleProperty("meta")).getLastUpdated()); + assertEquals(date, ((Meta) adapter.getProperty("meta")[0]).getLastUpdated()); + resource.addName(new HumanName().addGiven("name1")); + resource.addName(new HumanName().addGiven("name2")); + assertThrows(IllegalArgumentException.class, () -> adapter.getSingleProperty("name")); } @Test diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapterTest.java index 38afed7ac..3ec0e260f 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/ResourceAdapterTest.java @@ -2,6 +2,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -17,6 +18,7 @@ import org.hl7.fhir.r5.model.BooleanType; import org.hl7.fhir.r5.model.Bundle; import org.hl7.fhir.r5.model.Extension; +import org.hl7.fhir.r5.model.HumanName; import org.hl7.fhir.r5.model.IdType; import org.hl7.fhir.r5.model.Library; import org.hl7.fhir.r5.model.Meta; @@ -41,16 +43,22 @@ void adapter_get_and_set_property() { var id = new IdDt("patient-1"); resource.setId(id); var adapter = adapterFactory.createResource(resource); + assertTrue(((ResourceAdapter) adapter).isDomainResource()); + assertEquals(resource, ((ResourceAdapter) adapter).getDomainResource().get()); assertEquals(id.getValue(), ((IIdType) adapter.getSingleProperty("id")).getValue()); var newId = new IdType("patient-2"); adapter.setProperty("id", newId); assertEquals(newId, resource.getIdElement()); assertEquals("id", adapter.getTypesForProperty("id")[0]); assertNotNull(adapter.makeProperty("language")); + assertNull(adapter.getSingleProperty("meta")); var meta = (Meta) adapter.addChild("meta"); var date = new Date(); meta.setLastUpdated(date); - assertEquals(date, ((Meta) adapter.getSingleProperty("meta")).getLastUpdated()); + assertEquals(date, ((Meta) adapter.getProperty("meta")[0]).getLastUpdated()); + resource.addName(new HumanName().addGiven("name1")); + resource.addName(new HumanName().addGiven("name2")); + assertThrows(IllegalArgumentException.class, () -> adapter.getSingleProperty("name")); } @Test From 5b618a57b7cc9a9fc27ba97036b1a1bc936362a7 Mon Sep 17 00:00:00 2001 From: Brenin Rhodes Date: Tue, 13 Aug 2024 10:46:47 -0600 Subject: [PATCH 30/38] spotless --- .../opencds/cqf/fhir/utility/adapter/AdapterFactoryTest.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/AdapterFactoryTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/AdapterFactoryTest.java index 3cc9a44c6..a576bd2d0 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/AdapterFactoryTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/AdapterFactoryTest.java @@ -3,11 +3,10 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; -import org.hl7.fhir.r4.model.Patient; -import org.junit.jupiter.api.Test; - import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.context.FhirVersionEnum; +import org.hl7.fhir.r4.model.Patient; +import org.junit.jupiter.api.Test; class AdapterFactoryTest { @Test From bc8c8a117ed730e8852703f4354f73e719586172 Mon Sep 17 00:00:00 2001 From: Brenin Rhodes Date: Tue, 13 Aug 2024 11:49:39 -0600 Subject: [PATCH 31/38] add tests --- .../cqf/fhir/cql/LibraryEngineTests.java | 21 +++++--- .../opencds/cqf/fhir/cql/cql/TestLibrary.cql | 8 +++ .../cql/resources/Library-TestLibrary.json | 13 +++++ .../fhir/cr/common/PackageProcessorTests.java | 53 +++++++++++++++++++ .../cqf/fhir/utility/ValueSetsTest.java | 9 ++++ 5 files changed, 96 insertions(+), 8 deletions(-) create mode 100644 cqf-fhir-cql/src/test/resources/org/opencds/cqf/fhir/cql/cql/TestLibrary.cql create mode 100644 cqf-fhir-cql/src/test/resources/org/opencds/cqf/fhir/cql/resources/Library-TestLibrary.json create mode 100644 cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/common/PackageProcessorTests.java diff --git a/cqf-fhir-cql/src/test/java/org/opencds/cqf/fhir/cql/LibraryEngineTests.java b/cqf-fhir-cql/src/test/java/org/opencds/cqf/fhir/cql/LibraryEngineTests.java index ddbcb0f6c..df855222b 100644 --- a/cqf-fhir-cql/src/test/java/org/opencds/cqf/fhir/cql/LibraryEngineTests.java +++ b/cqf-fhir-cql/src/test/java/org/opencds/cqf/fhir/cql/LibraryEngineTests.java @@ -15,18 +15,18 @@ import org.hl7.fhir.r4.model.StringType; import org.hl7.fhir.r4.model.Task; import org.junit.jupiter.api.Test; +import org.opencds.cqf.fhir.api.Repository; import org.opencds.cqf.fhir.utility.CqfExpression; import org.opencds.cqf.fhir.utility.repository.ig.IgRepository; class LibraryEngineTests { + Repository repository = + new IgRepository(FhirContext.forR4Cached(), Paths.get(getResourcePath(LibraryEngineTests.class))); + LibraryEngine libraryEngine = new LibraryEngine(repository, EvaluationSettings.getDefault()); @Test void fhirPath() { var patientId = "Patient/Patient1"; - var repository = - new IgRepository(FhirContext.forR4Cached(), Paths.get(getResourcePath(LibraryEngineTests.class))); - var libraryEngine = new LibraryEngine(repository, EvaluationSettings.getDefault()); - var params = parameters(); params.addParameter(part("%subject", new Patient().addName(new HumanName().addGiven("Alice")))); params.addParameter(part("%practitioner", new Practitioner().addName(new HumanName().addGiven("Michael")))); @@ -49,10 +49,6 @@ void fhirPath() { @Test void fhirPathWithResource() { var patientId = "Patient/Patient1"; - var repository = - new IgRepository(FhirContext.forR4Cached(), Paths.get(getResourcePath(LibraryEngineTests.class))); - var libraryEngine = new LibraryEngine(repository, EvaluationSettings.getDefault()); - var params = parameters(); params.addParameter(part("%subject", new Patient().addName(new HumanName().addGiven("Alice")))); params.addParameter(part("%practitioner", new Practitioner().addName(new HumanName().addGiven("Michael")))); @@ -68,4 +64,13 @@ void fhirPathWithResource() { "test-code", ((CodeableConcept) result.get(0)).getCodingFirstRep().getCode()); } + + @Test + void expressionWithLibraryReference() { + var patientId = "Patient/Patient1"; + var expression = + new CqfExpression("text/cql", "TestLibrary.testExpression", "http://fhir.test/Library/TestLibrary"); + var result = libraryEngine.resolveExpression(patientId, expression, null, null, null, null); + assertEquals(((StringType) result.get(0)).getValue(), "I am a test"); + } } diff --git a/cqf-fhir-cql/src/test/resources/org/opencds/cqf/fhir/cql/cql/TestLibrary.cql b/cqf-fhir-cql/src/test/resources/org/opencds/cqf/fhir/cql/cql/TestLibrary.cql new file mode 100644 index 000000000..62e821c83 --- /dev/null +++ b/cqf-fhir-cql/src/test/resources/org/opencds/cqf/fhir/cql/cql/TestLibrary.cql @@ -0,0 +1,8 @@ +library TestLibrary version '1.0.0' + +using FHIR version '4.0.1' + +context Patient + +define "testExpression": + 'I am a test' \ No newline at end of file diff --git a/cqf-fhir-cql/src/test/resources/org/opencds/cqf/fhir/cql/resources/Library-TestLibrary.json b/cqf-fhir-cql/src/test/resources/org/opencds/cqf/fhir/cql/resources/Library-TestLibrary.json new file mode 100644 index 000000000..187fbffea --- /dev/null +++ b/cqf-fhir-cql/src/test/resources/org/opencds/cqf/fhir/cql/resources/Library-TestLibrary.json @@ -0,0 +1,13 @@ +{ + "resourceType": "Library", + "id": "TestLibrary", + "url": "http://fhir.test/Library/TestLibrary", + "version": "1.0.0", + "name": "TestLibrary", + "content": [ + { + "contentType": "text/cql", + "url": "../cql/TestLibrary.cql" + } + ] +} \ No newline at end of file diff --git a/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/common/PackageProcessorTests.java b/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/common/PackageProcessorTests.java new file mode 100644 index 000000000..42b0c0a0b --- /dev/null +++ b/cqf-fhir-cr/src/test/java/org/opencds/cqf/fhir/cr/common/PackageProcessorTests.java @@ -0,0 +1,53 @@ +package org.opencds.cqf.fhir.cr.common; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.Mockito.doReturn; + +import ca.uhn.fhir.context.FhirContext; +import org.hl7.fhir.r4.model.Bundle.BundleEntryComponent; +import org.hl7.fhir.r4.model.Bundle.HTTPVerb; +import org.hl7.fhir.r4.model.PlanDefinition; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.opencds.cqf.fhir.api.Repository; +import org.opencds.cqf.fhir.utility.BundleHelper; + +@ExtendWith(MockitoExtension.class) +class PackageProcessorTests { + FhirContext fhirContext = FhirContext.forR4Cached(); + + @Mock + Repository repository; + + PackageProcessor packageProcessor; + + @BeforeEach + void setup() { + doReturn(fhirContext).when(repository).fhirContext(); + packageProcessor = new PackageProcessor(repository); + } + + @Test + void testPOST() { + var resource = new PlanDefinition().setId("test"); + var bundle = packageProcessor.packageResource(resource); + assertNotNull(bundle); + var entry = (BundleEntryComponent) BundleHelper.getEntryFirstRep(bundle); + assertEquals(HTTPVerb.POST, entry.getRequest().getMethod()); + assertEquals(resource, BundleHelper.getEntryResourceFirstRep(bundle)); + } + + @Test + void testPUT() { + var resource = new PlanDefinition().setId("test"); + var bundle = packageProcessor.packageResource(resource, "PUT"); + assertNotNull(bundle); + var entry = (BundleEntryComponent) BundleHelper.getEntryFirstRep(bundle); + assertEquals(HTTPVerb.PUT, entry.getRequest().getMethod()); + assertEquals(resource, BundleHelper.getEntryResourceFirstRep(bundle)); + } +} diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/ValueSetsTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/ValueSetsTest.java index b912e2bc6..720eb2a4f 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/ValueSetsTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/ValueSetsTest.java @@ -1,6 +1,7 @@ package org.opencds.cqf.fhir.utility; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; import ca.uhn.fhir.context.FhirContext; import java.lang.reflect.InvocationTargetException; @@ -11,6 +12,14 @@ class ValueSetsTest { private final FhirContext fhirContextR4 = FhirContext.forR4Cached(); + @Test + void testNulls() { + assertNull(ValueSets.getContainsInExpansion(fhirContextR4, null)); + var expansion = new ValueSet.ValueSetExpansionComponent(); + assertNull(ValueSets.getContainsInExpansion(fhirContextR4, expansion)); + assertNull(ValueSets.getCodesInCompose(fhirContextR4, new ValueSet())); + } + @Test void testAddCodeToExpansion() { var code = new Code(); From 51fbd478ead25f27cef6669b3b0edfe9f3babd0b Mon Sep 17 00:00:00 2001 From: Brenin Rhodes Date: Wed, 14 Aug 2024 07:19:19 -0600 Subject: [PATCH 32/38] Add MeasureAdapter --- .../cqf/fhir/utility/SearchHelper.java | 80 +++++++++++-- .../adapter/KnowledgeArtifactAdapter.java | 44 +++++-- .../utility/adapter/dstu3/AdapterFactory.java | 3 + .../utility/adapter/dstu3/MeasureAdapter.java | 108 ++++++++++++++++++ .../utility/adapter/r4/AdapterFactory.java | 3 + .../utility/adapter/r4/MeasureAdapter.java | 106 +++++++++++++++++ .../utility/adapter/r5/AdapterFactory.java | 3 + .../utility/adapter/r5/MeasureAdapter.java | 106 +++++++++++++++++ 8 files changed, 433 insertions(+), 20 deletions(-) create mode 100644 cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/MeasureAdapter.java create mode 100644 cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/MeasureAdapter.java create mode 100644 cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/MeasureAdapter.java diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/SearchHelper.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/SearchHelper.java index eba1849ef..b3095c6e8 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/SearchHelper.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/SearchHelper.java @@ -3,6 +3,7 @@ import static org.opencds.cqf.fhir.utility.BundleHelper.getEntryResourceFirstRep; import ca.uhn.fhir.model.api.IQueryParameterType; +import ca.uhn.fhir.parser.DataFormatException; import java.util.Collections; import java.util.List; import java.util.Map; @@ -51,14 +52,75 @@ public static IBaseResource readRepository(Repository repository, IIdType id) { */ public static > IBaseResource searchRepositoryByCanonical( Repository repository, CanonicalType canonical) { - var resourceType = repository - .fhirContext() - .getResourceDefinition(Canonicals.getResourceType(canonical)) - .getImplementingClass(); + var resourceType = getResourceType(repository, canonical); return searchRepositoryByCanonical(repository, canonical, resourceType); } + /** + * Gets the resource type for the given canonical, based on the convention that canonical + * URLs are of the form [base]/[resourceType]/[tail] + * + * If the URL does not conform to the convention, the cqf-resourceType extension is used + * to determine the type of the resource, if present. + * + * If no extension is present, the type of the canonical is assumed to be CodeSystem, on + * the grounds that most (if not all) non-conventional URLs are for CodeSystem uris. + * + * @param + * @param repository the repository to search + * @param canonical the canonical url to search for + * @return + */ + private static > Class getResourceType( + Repository repository, CanonicalType canonical) { + Class resourceType = null; + try { + resourceType = repository + .fhirContext() + .getResourceDefinition(Canonicals.getResourceType(canonical)) + .getImplementingClass(); + } catch (DataFormatException e) { + // TODO: Use the "cqf-resourceType" extension to figure this out, if it's present + // NOTE: This is based on the assumption that only CodeSystems don't follow the canonical pattern... + resourceType = + repository.fhirContext().getResourceDefinition("CodeSystem").getImplementingClass(); + } + + return resourceType; + } + + /** + * Gets the resource type for the given canonical, based on the convention that canonical + * URLs are of the form [base]/[resourceType]/[tail] + * + * If the URL does not conform to the convention, the cqf-resourceType extension is used + * to determine the type of the resource, if present. + * + * If no extension is present, the type of the canonical is assumed to be CodeSystem, on + * the grounds that most (if not all) non-conventional URLs are for CodeSystem uris. + * + * @param repository + * @param canonical + * @return + */ + private static Class getResourceType(Repository repository, String canonical) { + Class resourceType = null; + try { + resourceType = repository + .fhirContext() + .getResourceDefinition(Canonicals.getResourceType(canonical)) + .getImplementingClass(); + } catch (RuntimeException e) { + // TODO: Use the "cqf-resourceType" extension to figure this out, if it's present + // NOTE: This is based on the assumption that only CodeSystems don't follow the canonical pattern... + resourceType = + repository.fhirContext().getResourceDefinition("CodeSystem").getImplementingClass(); + } + + return resourceType; + } + /** * Searches the given Repository and returns the first entry found * @@ -95,10 +157,7 @@ IBaseResource searchRepositoryByCanonical( */ public static > IBaseBundle searchRepositoryByCanonicalWithPaging( Repository repository, CanonicalType canonical) { - var resourceType = repository - .fhirContext() - .getResourceDefinition(Canonicals.getResourceType(canonical)) - .getImplementingClass(); + var resourceType = getResourceType(repository, canonical); return searchRepositoryByCanonicalWithPaging(repository, canonical, resourceType); } @@ -113,10 +172,7 @@ public static > IBaseBundle searchR */ public static > IBaseBundle searchRepositoryByCanonicalWithPaging( Repository repository, String canonical) { - var resourceType = repository - .fhirContext() - .getResourceDefinition(Canonicals.getResourceType(canonical)) - .getImplementingClass(); + var resourceType = getResourceType(repository, canonical); return searchRepositoryByCanonicalWithPaging(repository, canonical, resourceType); } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/KnowledgeArtifactAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/KnowledgeArtifactAdapter.java index 8b979c269..5e0de4c0d 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/KnowledgeArtifactAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/KnowledgeArtifactAdapter.java @@ -53,6 +53,26 @@ default void setName(String name) { getModelResolver().setValue(get(), "name", newStringType(get().getStructureFhirVersionEnum(), name)); } + default boolean hasTitle() { + return StringUtils.isNotBlank(getTitle()); + } + + default String getTitle() { + return resolvePathString(get(), "title"); + } + + default void setTitle(String title) { + getModelResolver().setValue(get(), "title", title); + } + + default String getDescriptor() { + return String.format( + "%s %s%s", + this.get().fhirType(), + this.hasTitle() ? this.getTitle() : this.getName(), + this.hasVersion() ? ", " + this.getVersion() : ""); + } + default boolean hasUrl() { return StringUtils.isNotBlank(getUrl()); } @@ -153,22 +173,25 @@ default boolean getExperimental() { @SuppressWarnings("unchecked") static T newRelatedArtifact( - FhirVersionEnum version, String type, String reference) { + FhirVersionEnum version, String type, String reference, String display) { switch (version) { case DSTU3: var dstu3 = new org.hl7.fhir.dstu3.model.RelatedArtifact(); dstu3.setType(org.hl7.fhir.dstu3.model.RelatedArtifact.RelatedArtifactType.fromCode(type)) - .setResource(new Reference(reference)); + .setResource(new Reference(reference)) + .setDisplay(display); return (T) dstu3; case R4: var r4 = new org.hl7.fhir.r4.model.RelatedArtifact(); r4.setType(org.hl7.fhir.r4.model.RelatedArtifact.RelatedArtifactType.fromCode(type)) - .setResource(reference); + .setResource(reference) + .setDisplay(display); return (T) r4; case R5: var r5 = new org.hl7.fhir.r5.model.RelatedArtifact(); r5.setType(org.hl7.fhir.r5.model.RelatedArtifact.RelatedArtifactType.fromCode(type)) - .setResource(reference); + .setResource(reference) + .setDisplay(display); return (T) r5; default: @@ -209,15 +232,20 @@ static String getRelatedArtifact } static void setRelatedArtifactReference( - T relatedArtifact, String reference) { + T relatedArtifact, String reference, String display) { if (relatedArtifact instanceof org.hl7.fhir.dstu3.model.RelatedArtifact) { ((org.hl7.fhir.dstu3.model.RelatedArtifact) relatedArtifact) .getResource() - .setReference(reference); + .setReference(reference) + .setDisplay(display); } else if (relatedArtifact instanceof org.hl7.fhir.r4.model.RelatedArtifact) { - ((org.hl7.fhir.r4.model.RelatedArtifact) relatedArtifact).setResource(reference); + ((org.hl7.fhir.r4.model.RelatedArtifact) relatedArtifact) + .setResource(reference) + .setDisplay(display); } else if (relatedArtifact instanceof org.hl7.fhir.r5.model.RelatedArtifact) { - ((org.hl7.fhir.r5.model.RelatedArtifact) relatedArtifact).setResource(reference); + ((org.hl7.fhir.r5.model.RelatedArtifact) relatedArtifact) + .setResource(reference) + .setDisplay(display); } else { throw new UnprocessableEntityException("Must be a valid RelatedArtifact"); } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/AdapterFactory.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/AdapterFactory.java index 5eb2718e5..81b473f84 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/AdapterFactory.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/AdapterFactory.java @@ -3,6 +3,7 @@ import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; import org.hl7.fhir.dstu3.model.Endpoint; import org.hl7.fhir.dstu3.model.Library; +import org.hl7.fhir.dstu3.model.Measure; import org.hl7.fhir.dstu3.model.MetadataResource; import org.hl7.fhir.dstu3.model.Parameters; import org.hl7.fhir.dstu3.model.PlanDefinition; @@ -36,6 +37,8 @@ public org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter createKnowl org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter adapter; if (resource instanceof Library) { adapter = createLibrary(resource); + } else if (resource instanceof Measure) { + adapter = new MeasureAdapter((Measure) resource); } else if (resource instanceof PlanDefinition) { adapter = new PlanDefinitionAdapter((PlanDefinition) resource); } else if (resource instanceof Questionnaire) { diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/MeasureAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/MeasureAdapter.java new file mode 100644 index 000000000..7c65580da --- /dev/null +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/MeasureAdapter.java @@ -0,0 +1,108 @@ +package org.opencds.cqf.fhir.utility.adapter.dstu3; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; +import org.hl7.fhir.dstu3.model.Library; +import org.hl7.fhir.dstu3.model.Measure; +import org.hl7.fhir.dstu3.model.Reference; +import org.hl7.fhir.instance.model.api.IDomainResource; +import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; +import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; + +public class MeasureAdapter extends KnowledgeArtifactAdapter { + + private Measure measure; + + public MeasureAdapter(IDomainResource measure) { + super(measure); + + if (!(measure instanceof Measure)) { + throw new IllegalArgumentException("resource passed as measure argument is not a Measure resource"); + } + + this.measure = (Measure) measure; + } + + public MeasureAdapter(Measure measure) { + super(measure); + this.measure = measure; + } + + protected Measure getMeasure() { + return this.measure; + } + + private boolean checkedEffectiveDataRequirements; + private Library effectiveDataRequirements; + private LibraryAdapter effectiveDataRequirementsAdapter; + + private void findEffectiveDataRequirements() { + if (!checkedEffectiveDataRequirements) { + var edrExtensions = this.getMeasure().getExtension().stream() + .filter(ext -> ext.getUrl().endsWith("-effectiveDataRequirements")) + .filter(ext -> ext.hasValue()) + .collect(Collectors.toList()); + + var edrExtension = edrExtensions.size() == 1 ? edrExtensions.get(0) : null; + if (edrExtension != null) { + var edrReference = ((Reference) edrExtension.getValue()).getReference(); + for (var c : getMeasure().getContained()) { + if (c.hasId() && String.format("#%s", c.getId()).equals(edrReference) && c instanceof Library) { + effectiveDataRequirements = (Library) c; + effectiveDataRequirementsAdapter = new LibraryAdapter(effectiveDataRequirements); + } + } + } + checkedEffectiveDataRequirements = true; + } + } + + @Override + public List getDependencies() { + + // If an effectiveDataRequirements library is present, use it exclusively + findEffectiveDataRequirements(); + if (effectiveDataRequirements != null) { + return effectiveDataRequirementsAdapter.getDependencies(); + } + + // Otherwise, fall back to the relatedArtifact and library + List references = new ArrayList<>(); + final String referenceSource = this.getMeasure().hasVersion() + ? this.getMeasure().getUrl() + "|" + this.getMeasure().getVersion() + : this.getMeasure().getUrl(); + /* + relatedArtifact[].resource + library[] + group[].population[].criteria.reference + group[].stratifier[].criteria.reference + group[].stratifier[].component[].criteria.reference + supplementalData[].criteria.reference + extension[cqfm-inputParameters][] + extension[cqfm-expansionParameters][] + extension[cqfm-effectiveDataRequirements] + extension[cqfm-cqlOptions] + extension[cqfm-component][].resource + extension[crmi-effectiveDataRequirements] + */ + + // relatedArtifact[].resource + references.addAll(this.getRelatedArtifact().stream() + .map(ra -> DependencyInfo.convertRelatedArtifact(ra, referenceSource)) + .collect(Collectors.toList())); + + // library[] + List libraries = this.getMeasure().getLibrary(); + for (Reference ref : libraries) { + DependencyInfo dependency = new DependencyInfo( + referenceSource, + ref.getReference(), + ref.getExtension(), + (reference) -> ref.setReference(reference)); + references.add(dependency); + } + + return references; + } +} diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/AdapterFactory.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/AdapterFactory.java index 43607c983..ddbca71e8 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/AdapterFactory.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/AdapterFactory.java @@ -8,6 +8,7 @@ import org.hl7.fhir.instance.model.api.IDomainResource; import org.hl7.fhir.r4.model.Endpoint; import org.hl7.fhir.r4.model.Library; +import org.hl7.fhir.r4.model.Measure; import org.hl7.fhir.r4.model.MetadataResource; import org.hl7.fhir.r4.model.Parameters; import org.hl7.fhir.r4.model.PlanDefinition; @@ -36,6 +37,8 @@ public org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter createKnowl org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter adapter; if (resource instanceof Library) { adapter = createLibrary(resource); + } else if (resource instanceof Measure) { + adapter = new MeasureAdapter((Measure) resource); } else if (resource instanceof PlanDefinition) { adapter = new PlanDefinitionAdapter((PlanDefinition) resource); } else if (resource instanceof Questionnaire) { diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/MeasureAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/MeasureAdapter.java new file mode 100644 index 000000000..49042ff8a --- /dev/null +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/MeasureAdapter.java @@ -0,0 +1,106 @@ +package org.opencds.cqf.fhir.utility.adapter.r4; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; +import org.hl7.fhir.instance.model.api.IDomainResource; +import org.hl7.fhir.r4.model.CanonicalType; +import org.hl7.fhir.r4.model.Library; +import org.hl7.fhir.r4.model.Measure; +import org.hl7.fhir.r4.model.Reference; +import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; +import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; + +public class MeasureAdapter extends KnowledgeArtifactAdapter { + + private Measure measure; + + public MeasureAdapter(IDomainResource measure) { + super(measure); + + if (!(measure instanceof Measure)) { + throw new IllegalArgumentException("resource passed as measure argument is not a Measure resource"); + } + + this.measure = (Measure) measure; + } + + public MeasureAdapter(Measure measure) { + super(measure); + this.measure = measure; + } + + protected Measure getMeasure() { + return this.measure; + } + + private boolean checkedEffectiveDataRequirements; + private Library effectiveDataRequirements; + private LibraryAdapter effectiveDataRequirementsAdapter; + + private void findEffectiveDataRequirements() { + if (!checkedEffectiveDataRequirements) { + var edrExtensions = this.getMeasure().getExtension().stream() + .filter(ext -> ext.getUrl().endsWith("-effectiveDataRequirements")) + .filter(ext -> ext.hasValue()) + .collect(Collectors.toList()); + + var edrExtension = edrExtensions.size() == 1 ? edrExtensions.get(0) : null; + if (edrExtension != null) { + var edrReference = ((Reference) edrExtension.getValue()).getReference(); + for (var c : getMeasure().getContained()) { + if (c.hasId() && String.format("#%s", c.getId()).equals(edrReference) && c instanceof Library) { + effectiveDataRequirements = (Library) c; + effectiveDataRequirementsAdapter = new LibraryAdapter(effectiveDataRequirements); + } + } + } + checkedEffectiveDataRequirements = true; + } + } + + @Override + public List getDependencies() { + + // If an effectiveDataRequirements library is present, use it exclusively + findEffectiveDataRequirements(); + if (effectiveDataRequirements != null) { + return effectiveDataRequirementsAdapter.getDependencies(); + } + + // Otherwise, fall back to the relatedArtifact and library + List references = new ArrayList<>(); + final String referenceSource = this.getMeasure().hasVersion() + ? this.getMeasure().getUrl() + "|" + this.getMeasure().getVersion() + : this.getMeasure().getUrl(); + /* + relatedArtifact[].resource + library[] + group[].population[].criteria.reference + group[].stratifier[].criteria.reference + group[].stratifier[].component[].criteria.reference + supplementalData[].criteria.reference + extension[cqfm-inputParameters][] + extension[cqfm-expansionParameters][] + extension[cqfm-effectiveDataRequirements] + extension[cqfm-cqlOptions] + extension[cqfm-component][].resource + extension[crmi-effectiveDataRequirements] + */ + + // relatedArtifact[].resource + references.addAll(this.getRelatedArtifact().stream() + .map(ra -> DependencyInfo.convertRelatedArtifact(ra, referenceSource)) + .collect(Collectors.toList())); + + // library[] + List libraries = this.getMeasure().getLibrary(); + for (CanonicalType ct : libraries) { + DependencyInfo dependency = new DependencyInfo( + referenceSource, ct.getValue(), ct.getExtension(), (reference) -> ct.setValue(reference)); + references.add(dependency); + } + + return references; + } +} diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/AdapterFactory.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/AdapterFactory.java index cc510618e..3ae19dcb8 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/AdapterFactory.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/AdapterFactory.java @@ -8,6 +8,7 @@ import org.hl7.fhir.instance.model.api.IDomainResource; import org.hl7.fhir.r5.model.Endpoint; import org.hl7.fhir.r5.model.Library; +import org.hl7.fhir.r5.model.Measure; import org.hl7.fhir.r5.model.MetadataResource; import org.hl7.fhir.r5.model.Parameters; import org.hl7.fhir.r5.model.PlanDefinition; @@ -36,6 +37,8 @@ public org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter createKnowl org.opencds.cqf.fhir.utility.adapter.KnowledgeArtifactAdapter adapter; if (resource instanceof Library) { adapter = createLibrary(resource); + } else if (resource instanceof Measure) { + adapter = new MeasureAdapter((Measure) resource); } else if (resource instanceof PlanDefinition) { adapter = new PlanDefinitionAdapter((PlanDefinition) resource); } else if (resource instanceof Questionnaire) { diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/MeasureAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/MeasureAdapter.java new file mode 100644 index 000000000..bcd8139fd --- /dev/null +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/MeasureAdapter.java @@ -0,0 +1,106 @@ +package org.opencds.cqf.fhir.utility.adapter.r5; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; +import org.hl7.fhir.instance.model.api.IDomainResource; +import org.hl7.fhir.r5.model.CanonicalType; +import org.hl7.fhir.r5.model.Library; +import org.hl7.fhir.r5.model.Measure; +import org.hl7.fhir.r5.model.Reference; +import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; +import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; + +public class MeasureAdapter extends KnowledgeArtifactAdapter { + + private Measure measure; + + public MeasureAdapter(IDomainResource measure) { + super(measure); + + if (!(measure instanceof Measure)) { + throw new IllegalArgumentException("resource passed as measure argument is not a Measure resource"); + } + + this.measure = (Measure) measure; + } + + public MeasureAdapter(Measure measure) { + super(measure); + this.measure = measure; + } + + protected Measure getMeasure() { + return this.measure; + } + + private boolean checkedEffectiveDataRequirements; + private Library effectiveDataRequirements; + private LibraryAdapter effectiveDataRequirementsAdapter; + + private void findEffectiveDataRequirements() { + if (!checkedEffectiveDataRequirements) { + var edrExtensions = this.getMeasure().getExtension().stream() + .filter(ext -> ext.getUrl().endsWith("-effectiveDataRequirements")) + .filter(ext -> ext.hasValue()) + .collect(Collectors.toList()); + + var edrExtension = edrExtensions.size() == 1 ? edrExtensions.get(0) : null; + if (edrExtension != null) { + var edrReference = ((Reference) edrExtension.getValue()).getReference(); + for (var c : getMeasure().getContained()) { + if (c.hasId() && String.format("#%s", c.getId()).equals(edrReference) && c instanceof Library) { + effectiveDataRequirements = (Library) c; + effectiveDataRequirementsAdapter = new LibraryAdapter(effectiveDataRequirements); + } + } + } + checkedEffectiveDataRequirements = true; + } + } + + @Override + public List getDependencies() { + + // If an effectiveDataRequirements library is present, use it exclusively + findEffectiveDataRequirements(); + if (effectiveDataRequirements != null) { + return effectiveDataRequirementsAdapter.getDependencies(); + } + + // Otherwise, fall back to the relatedArtifact and library + List references = new ArrayList<>(); + final String referenceSource = this.getMeasure().hasVersion() + ? this.getMeasure().getUrl() + "|" + this.getMeasure().getVersion() + : this.getMeasure().getUrl(); + /* + relatedArtifact[].resource + library[] + group[].population[].criteria.reference + group[].stratifier[].criteria.reference + group[].stratifier[].component[].criteria.reference + supplementalData[].criteria.reference + extension[cqfm-inputParameters][] + extension[cqfm-expansionParameters][] + extension[cqfm-effectiveDataRequirements] + extension[cqfm-cqlOptions] + extension[cqfm-component][].resource + extension[crmi-effectiveDataRequirements] + */ + + // relatedArtifact[].resource + references.addAll(this.getRelatedArtifact().stream() + .map(ra -> DependencyInfo.convertRelatedArtifact(ra, referenceSource)) + .collect(Collectors.toList())); + + // library[] + List libraries = this.getMeasure().getLibrary(); + for (CanonicalType ct : libraries) { + DependencyInfo dependency = new DependencyInfo( + referenceSource, ct.getValue(), ct.getExtension(), (reference) -> ct.setValue(reference)); + references.add(dependency); + } + + return references; + } +} From 95ac7421cfb3089b37567db76c50bd46bb79a6ae Mon Sep 17 00:00:00 2001 From: Brenin Rhodes Date: Wed, 14 Aug 2024 07:23:09 -0600 Subject: [PATCH 33/38] fix method signatures --- .../opencds/cqf/fhir/utility/visitor/ReleaseVisitor.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/ReleaseVisitor.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/ReleaseVisitor.java index 125d8d3cd..37539b197 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/ReleaseVisitor.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/ReleaseVisitor.java @@ -112,16 +112,16 @@ public IBase visit( var adapter = AdapterFactory.forFhirVersion(resource.getStructureFhirVersionEnum()) .createKnowledgeArtifactAdapter(resource); var reference = String.format("%s|%s", adapter.getUrl(), adapter.getVersion()); - KnowledgeArtifactAdapter.setRelatedArtifactReference(component, reference); + KnowledgeArtifactAdapter.setRelatedArtifactReference(component, reference, null); } else if (Canonicals.getVersion(relatedArtifactReference) == null || Canonicals.getVersion(relatedArtifactReference).isEmpty()) { // if the not Owned component doesn't have a version, try to find the latest version String updatedReference = tryUpdateReferenceToLatestActiveVersion( relatedArtifactReference, repository, artifactAdapter.getUrl()); - KnowledgeArtifactAdapter.setRelatedArtifactReference(component, updatedReference); + KnowledgeArtifactAdapter.setRelatedArtifactReference(component, updatedReference, null); } var componentToDependency = KnowledgeArtifactAdapter.newRelatedArtifact( - fhirVersion, "depends-on", KnowledgeArtifactAdapter.getRelatedArtifactReference(component)); + fhirVersion, "depends-on", KnowledgeArtifactAdapter.getRelatedArtifactReference(component), null); var relatedArtifacts = rootAdapter.getRelatedArtifact(); relatedArtifacts.add(componentToDependency); rootAdapter.setRelatedArtifact(relatedArtifacts); @@ -147,7 +147,7 @@ public IBase visit( // only add the dependency to the manifest if it is from a leaf artifact if (!artifactAdapter.getUrl().equals(rootAdapter.getUrl())) { var newDep = KnowledgeArtifactAdapter.newRelatedArtifact( - fhirVersion, "depends-on", dependency.getReference()); + fhirVersion, "depends-on", dependency.getReference(), null); var relatedArtifacts = rootAdapter.getRelatedArtifact(); relatedArtifacts.add(newDep); rootAdapter.setRelatedArtifact(relatedArtifacts); From e4636118f506262493edab074b0fdb8ad483f178 Mon Sep 17 00:00:00 2001 From: Brenin Rhodes Date: Wed, 14 Aug 2024 07:39:21 -0600 Subject: [PATCH 34/38] Add tests --- .../utility/adapter/dstu3/MeasureAdapter.java | 18 +- .../utility/adapter/r4/MeasureAdapter.java | 18 +- .../utility/adapter/r5/MeasureAdapter.java | 18 +- .../fhir/utility/visitor/ReleaseVisitor.java | 5 +- .../adapter/dstu3/MeasureAdapterTest.java | 176 +++++++++++++++++ .../adapter/r4/MeasureAdapterTest.java | 177 ++++++++++++++++++ .../adapter/r5/MeasureAdapterTest.java | 177 ++++++++++++++++++ 7 files changed, 567 insertions(+), 22 deletions(-) create mode 100644 cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/MeasureAdapterTest.java create mode 100644 cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/MeasureAdapterTest.java create mode 100644 cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/MeasureAdapterTest.java diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/MeasureAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/MeasureAdapter.java index 7c65580da..60c2b8c2e 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/MeasureAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/MeasureAdapter.java @@ -12,25 +12,29 @@ public class MeasureAdapter extends KnowledgeArtifactAdapter { - private Measure measure; - public MeasureAdapter(IDomainResource measure) { super(measure); - if (!(measure instanceof Measure)) { throw new IllegalArgumentException("resource passed as measure argument is not a Measure resource"); } - - this.measure = (Measure) measure; } public MeasureAdapter(Measure measure) { super(measure); - this.measure = measure; } protected Measure getMeasure() { - return this.measure; + return (Measure) resource; + } + + @Override + public Measure get() { + return getMeasure(); + } + + @Override + public Measure copy() { + return get().copy(); } private boolean checkedEffectiveDataRequirements; diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/MeasureAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/MeasureAdapter.java index 49042ff8a..a6d2eb746 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/MeasureAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/MeasureAdapter.java @@ -13,25 +13,29 @@ public class MeasureAdapter extends KnowledgeArtifactAdapter { - private Measure measure; - public MeasureAdapter(IDomainResource measure) { super(measure); - if (!(measure instanceof Measure)) { throw new IllegalArgumentException("resource passed as measure argument is not a Measure resource"); } - - this.measure = (Measure) measure; } public MeasureAdapter(Measure measure) { super(measure); - this.measure = measure; } protected Measure getMeasure() { - return this.measure; + return (Measure) resource; + } + + @Override + public Measure get() { + return getMeasure(); + } + + @Override + public Measure copy() { + return get().copy(); } private boolean checkedEffectiveDataRequirements; diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/MeasureAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/MeasureAdapter.java index bcd8139fd..0460ea87d 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/MeasureAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/MeasureAdapter.java @@ -13,25 +13,29 @@ public class MeasureAdapter extends KnowledgeArtifactAdapter { - private Measure measure; - public MeasureAdapter(IDomainResource measure) { super(measure); - if (!(measure instanceof Measure)) { throw new IllegalArgumentException("resource passed as measure argument is not a Measure resource"); } - - this.measure = (Measure) measure; } public MeasureAdapter(Measure measure) { super(measure); - this.measure = measure; } protected Measure getMeasure() { - return this.measure; + return (Measure) resource; + } + + @Override + public Measure get() { + return getMeasure(); + } + + @Override + public Measure copy() { + return get().copy(); } private boolean checkedEffectiveDataRequirements; diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/ReleaseVisitor.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/ReleaseVisitor.java index 37539b197..a5b53c6b2 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/ReleaseVisitor.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/visitor/ReleaseVisitor.java @@ -121,7 +121,10 @@ public IBase visit( KnowledgeArtifactAdapter.setRelatedArtifactReference(component, updatedReference, null); } var componentToDependency = KnowledgeArtifactAdapter.newRelatedArtifact( - fhirVersion, "depends-on", KnowledgeArtifactAdapter.getRelatedArtifactReference(component), null); + fhirVersion, + "depends-on", + KnowledgeArtifactAdapter.getRelatedArtifactReference(component), + null); var relatedArtifacts = rootAdapter.getRelatedArtifact(); relatedArtifacts.add(componentToDependency); rootAdapter.setRelatedArtifact(relatedArtifacts); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/MeasureAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/MeasureAdapterTest.java new file mode 100644 index 000000000..602acb0c4 --- /dev/null +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/MeasureAdapterTest.java @@ -0,0 +1,176 @@ +package org.opencds.cqf.fhir.utility.adapter.dstu3; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.model.primitive.IdDt; +import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; +import java.time.LocalDate; +import java.util.Date; +import java.util.List; +import org.hl7.fhir.dstu3.model.Bundle; +import org.hl7.fhir.dstu3.model.Enumerations.PublicationStatus; +import org.hl7.fhir.dstu3.model.Library; +import org.hl7.fhir.dstu3.model.Measure; +import org.hl7.fhir.dstu3.model.Period; +import org.hl7.fhir.dstu3.model.RelatedArtifact; +import org.hl7.fhir.instance.model.api.IDomainResource; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; + +public class MeasureAdapterTest { + private final FhirContext fhirContext = FhirContext.forDstu3Cached(); + private final org.opencds.cqf.fhir.utility.adapter.AdapterFactory adapterFactory = new AdapterFactory(); + + @Test + void invalid_object_fails() { + assertThrows(IllegalArgumentException.class, () -> new MeasureAdapter(new Library())); + } + + @Test + void adapter_accepts_visitor() { + var spyVisitor = spy(new PackageVisitor(fhirContext)); + doReturn(new Bundle()).when(spyVisitor).visit(any(MeasureAdapter.class), any(), any()); + IDomainResource measure = new Measure(); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(measure); + adapter.accept(spyVisitor, null, null); + verify(spyVisitor, times(1)).visit(any(MeasureAdapter.class), any(), any()); + } + + @Test + void adapter_get_and_set_name() { + var measure = new Measure(); + var name = "name"; + measure.setName(name); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(measure); + assertEquals(name, adapter.getName()); + var newName = "name2"; + adapter.setName(newName); + assertEquals(newName, measure.getName()); + } + + @Test + void adapter_get_and_set_url() { + var measure = new Measure(); + var url = "www.url.com"; + measure.setUrl(url); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(measure); + assertEquals(url, adapter.getUrl()); + var newUrl = "www.url2.com"; + adapter.setUrl(newUrl); + assertEquals(newUrl, measure.getUrl()); + } + + @Test + void adapter_get_and_set_version() { + var measure = new Measure(); + var version = "1.0.0"; + measure.setVersion(version); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(measure); + assertTrue(adapter.hasVersion()); + assertEquals(version, adapter.getVersion()); + var newVersion = "1.0.1"; + adapter.setVersion(newVersion); + assertEquals(newVersion, measure.getVersion()); + } + + @Test + void adapter_get_and_set_status() { + var measure = new Measure(); + var status = PublicationStatus.DRAFT; + measure.setStatus(status); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(measure); + assertEquals(status.toCode(), adapter.getStatus()); + assertThrows(UnprocessableEntityException.class, () -> adapter.setStatus("invalid-status")); + var newStatus = PublicationStatus.ACTIVE; + adapter.setStatus(newStatus.toCode()); + assertEquals(newStatus, PublicationStatus.fromCode(adapter.getStatus())); + } + + @Test + void adapter_get_and_set_dates() { + var measure = new Measure(); + var date = new Date(); + var approvalDate = new Date(); + var effectivePeriod = new Period() + .setStart(java.sql.Date.valueOf(LocalDate.parse("2020-01-01"))) + .setEnd(java.sql.Date.valueOf(LocalDate.parse("2020-12-31"))); + measure.setDate(date); + measure.setApprovalDate(approvalDate); + measure.setEffectivePeriod(effectivePeriod); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(measure); + assertEquals(date, adapter.getDate()); + assertEquals(approvalDate, adapter.getApprovalDate()); + assertEquals(effectivePeriod, adapter.getEffectivePeriod()); + var newDate = new Date(); + newDate.setTime(100); + adapter.setDate(newDate); + assertEquals(newDate, measure.getDate()); + var newApprovalDate = new Date(); + newApprovalDate.setTime(100); + adapter.setApprovalDate(newApprovalDate); + assertEquals(newApprovalDate, measure.getApprovalDate()); + var newEffectivePeriod = new Period() + .setStart(java.sql.Date.valueOf(LocalDate.parse("2021-01-01"))) + .setEnd(java.sql.Date.valueOf(LocalDate.parse("2021-12-31"))); + adapter.setEffectivePeriod(newEffectivePeriod); + assertEquals(newEffectivePeriod, adapter.getEffectivePeriod()); + } + + @Test + void adapter_get_experimental() { + var measure = new Measure(); + var experimental = true; + measure.setExperimental(experimental); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(measure); + assertEquals(experimental, adapter.getExperimental()); + } + + @Test + void adapter_set_relatedArtifact() { + var measure = new Measure(); + var relatedArtifactList = List.of(new RelatedArtifact()); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(measure); + adapter.setRelatedArtifact(relatedArtifactList); + assertEquals(relatedArtifactList, measure.getRelatedArtifact()); + assertEquals(relatedArtifactList, adapter.getRelatedArtifact()); + } + + @Test + void adapter_copy() { + var measure = new Measure().setStatus(PublicationStatus.DRAFT); + measure.setId("plan-1"); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(measure); + var copy = (Measure) adapter.copy(); + var adapterCopy = new MeasureAdapter(copy); + adapterCopy.setId(new IdDt("Measure", "plan-2")); + assertNotEquals(measure.getId(), copy.getId()); + measure.setStatus(PublicationStatus.ACTIVE); + assertNotEquals(adapter.getStatus(), copy.getStatus()); + } + + @Test + @Disabled + void adapter_get_all_dependencies() { + var dependencies = List.of(""); + var measure = new Measure(); + measure.getMeta().addProfile(dependencies.get(0)); + // measure.getRelatedArtifactFirstRep().setResource(dependencies.get(1)); + // measure.getLibrary().add(new CanonicalType(dependencies.get(2))); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(measure); + var extractedDependencies = adapter.getDependencies(); + assertEquals(extractedDependencies.size(), dependencies.size()); + extractedDependencies.forEach(dep -> { + assertTrue(dependencies.indexOf(dep.getReference()) >= 0); + }); + } +} diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/MeasureAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/MeasureAdapterTest.java new file mode 100644 index 000000000..a6d86cae6 --- /dev/null +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/MeasureAdapterTest.java @@ -0,0 +1,177 @@ +package org.opencds.cqf.fhir.utility.adapter.r4; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.model.primitive.IdDt; +import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; +import java.time.LocalDate; +import java.util.Date; +import java.util.List; +import org.hl7.fhir.instance.model.api.IDomainResource; +import org.hl7.fhir.r4.model.Bundle; +import org.hl7.fhir.r4.model.CanonicalType; +import org.hl7.fhir.r4.model.Enumerations.PublicationStatus; +import org.hl7.fhir.r4.model.Library; +import org.hl7.fhir.r4.model.Measure; +import org.hl7.fhir.r4.model.Period; +import org.hl7.fhir.r4.model.RelatedArtifact; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; + +public class MeasureAdapterTest { + private final FhirContext fhirContext = FhirContext.forR4Cached(); + private final org.opencds.cqf.fhir.utility.adapter.AdapterFactory adapterFactory = new AdapterFactory(); + + @Test + void invalid_object_fails() { + assertThrows(IllegalArgumentException.class, () -> new MeasureAdapter(new Library())); + } + + @Test + void adapter_accepts_visitor() { + var spyVisitor = spy(new PackageVisitor(fhirContext)); + doReturn(new Bundle()).when(spyVisitor).visit(any(MeasureAdapter.class), any(), any()); + IDomainResource measure = new Measure(); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(measure); + adapter.accept(spyVisitor, null, null); + verify(spyVisitor, times(1)).visit(any(MeasureAdapter.class), any(), any()); + } + + @Test + void adapter_get_and_set_name() { + var measure = new Measure(); + var name = "name"; + measure.setName(name); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(measure); + assertEquals(name, adapter.getName()); + var newName = "name2"; + adapter.setName(newName); + assertEquals(newName, measure.getName()); + } + + @Test + void adapter_get_and_set_url() { + var measure = new Measure(); + var url = "www.url.com"; + measure.setUrl(url); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(measure); + assertEquals(url, adapter.getUrl()); + var newUrl = "www.url2.com"; + adapter.setUrl(newUrl); + assertEquals(newUrl, measure.getUrl()); + } + + @Test + void adapter_get_and_set_version() { + var measure = new Measure(); + var version = "1.0.0"; + measure.setVersion(version); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(measure); + assertTrue(adapter.hasVersion()); + assertEquals(version, adapter.getVersion()); + var newVersion = "1.0.1"; + adapter.setVersion(newVersion); + assertEquals(newVersion, measure.getVersion()); + } + + @Test + void adapter_get_and_set_status() { + var measure = new Measure(); + var status = PublicationStatus.DRAFT; + measure.setStatus(status); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(measure); + assertEquals(status.toCode(), adapter.getStatus()); + assertThrows(UnprocessableEntityException.class, () -> adapter.setStatus("invalid-status")); + var newStatus = PublicationStatus.ACTIVE; + adapter.setStatus(newStatus.toCode()); + assertEquals(newStatus, PublicationStatus.fromCode(adapter.getStatus())); + } + + @Test + void adapter_get_and_set_dates() { + var measure = new Measure(); + var date = new Date(); + var approvalDate = new Date(); + var effectivePeriod = new Period() + .setStart(java.sql.Date.valueOf(LocalDate.parse("2020-01-01"))) + .setEnd(java.sql.Date.valueOf(LocalDate.parse("2020-12-31"))); + measure.setDate(date); + measure.setApprovalDate(approvalDate); + measure.setEffectivePeriod(effectivePeriod); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(measure); + assertEquals(date, adapter.getDate()); + assertEquals(approvalDate, adapter.getApprovalDate()); + assertEquals(effectivePeriod, adapter.getEffectivePeriod()); + var newDate = new Date(); + newDate.setTime(100); + adapter.setDate(newDate); + assertEquals(newDate, measure.getDate()); + var newApprovalDate = new Date(); + newApprovalDate.setTime(100); + adapter.setApprovalDate(newApprovalDate); + assertEquals(newApprovalDate, measure.getApprovalDate()); + var newEffectivePeriod = new Period() + .setStart(java.sql.Date.valueOf(LocalDate.parse("2021-01-01"))) + .setEnd(java.sql.Date.valueOf(LocalDate.parse("2021-12-31"))); + adapter.setEffectivePeriod(newEffectivePeriod); + assertEquals(newEffectivePeriod, adapter.getEffectivePeriod()); + } + + @Test + void adapter_get_experimental() { + var measure = new Measure(); + var experimental = true; + measure.setExperimental(experimental); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(measure); + assertEquals(experimental, adapter.getExperimental()); + } + + @Test + void adapter_set_relatedArtifact() { + var measure = new Measure(); + var relatedArtifactList = List.of(new RelatedArtifact()); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(measure); + adapter.setRelatedArtifact(relatedArtifactList); + assertEquals(relatedArtifactList, measure.getRelatedArtifact()); + assertEquals(relatedArtifactList, adapter.getRelatedArtifact()); + } + + @Test + void adapter_copy() { + var measure = new Measure().setStatus(PublicationStatus.DRAFT); + measure.setId("plan-1"); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(measure); + var copy = (Measure) adapter.copy(); + var adapterCopy = new MeasureAdapter(copy); + adapterCopy.setId(new IdDt("Measure", "plan-2")); + assertNotEquals(measure.getId(), copy.getId()); + measure.setStatus(PublicationStatus.ACTIVE); + assertNotEquals(adapter.getStatus(), copy.getStatus()); + } + + @Test + @Disabled + void adapter_get_all_dependencies() { + var dependencies = List.of(""); + var measure = new Measure(); + measure.getMeta().addProfile(dependencies.get(0)); + measure.getRelatedArtifactFirstRep().setResource(dependencies.get(1)); + measure.getLibrary().add(new CanonicalType(dependencies.get(2))); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(measure); + var extractedDependencies = adapter.getDependencies(); + assertEquals(extractedDependencies.size(), dependencies.size()); + extractedDependencies.forEach(dep -> { + assertTrue(dependencies.indexOf(dep.getReference()) >= 0); + }); + } +} diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/MeasureAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/MeasureAdapterTest.java new file mode 100644 index 000000000..63637c67a --- /dev/null +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/MeasureAdapterTest.java @@ -0,0 +1,177 @@ +package org.opencds.cqf.fhir.utility.adapter.r5; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.model.primitive.IdDt; +import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; +import java.time.LocalDate; +import java.util.Date; +import java.util.List; +import org.hl7.fhir.instance.model.api.IDomainResource; +import org.hl7.fhir.r5.model.Bundle; +import org.hl7.fhir.r5.model.CanonicalType; +import org.hl7.fhir.r5.model.Enumerations.PublicationStatus; +import org.hl7.fhir.r5.model.Library; +import org.hl7.fhir.r5.model.Measure; +import org.hl7.fhir.r5.model.Period; +import org.hl7.fhir.r5.model.RelatedArtifact; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; + +public class MeasureAdapterTest { + private final FhirContext fhirContext = FhirContext.forR5Cached(); + private final org.opencds.cqf.fhir.utility.adapter.AdapterFactory adapterFactory = new AdapterFactory(); + + @Test + void invalid_object_fails() { + assertThrows(IllegalArgumentException.class, () -> new MeasureAdapter(new Library())); + } + + @Test + void adapter_accepts_visitor() { + var spyVisitor = spy(new PackageVisitor(fhirContext)); + doReturn(new Bundle()).when(spyVisitor).visit(any(MeasureAdapter.class), any(), any()); + IDomainResource measure = new Measure(); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(measure); + adapter.accept(spyVisitor, null, null); + verify(spyVisitor, times(1)).visit(any(MeasureAdapter.class), any(), any()); + } + + @Test + void adapter_get_and_set_name() { + var measure = new Measure(); + var name = "name"; + measure.setName(name); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(measure); + assertEquals(name, adapter.getName()); + var newName = "name2"; + adapter.setName(newName); + assertEquals(newName, measure.getName()); + } + + @Test + void adapter_get_and_set_url() { + var measure = new Measure(); + var url = "www.url.com"; + measure.setUrl(url); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(measure); + assertEquals(url, adapter.getUrl()); + var newUrl = "www.url2.com"; + adapter.setUrl(newUrl); + assertEquals(newUrl, measure.getUrl()); + } + + @Test + void adapter_get_and_set_version() { + var measure = new Measure(); + var version = "1.0.0"; + measure.setVersion(version); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(measure); + assertTrue(adapter.hasVersion()); + assertEquals(version, adapter.getVersion()); + var newVersion = "1.0.1"; + adapter.setVersion(newVersion); + assertEquals(newVersion, measure.getVersion()); + } + + @Test + void adapter_get_and_set_status() { + var measure = new Measure(); + var status = PublicationStatus.DRAFT; + measure.setStatus(status); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(measure); + assertEquals(status.toCode(), adapter.getStatus()); + assertThrows(UnprocessableEntityException.class, () -> adapter.setStatus("invalid-status")); + var newStatus = PublicationStatus.ACTIVE; + adapter.setStatus(newStatus.toCode()); + assertEquals(newStatus, PublicationStatus.fromCode(adapter.getStatus())); + } + + @Test + void adapter_get_and_set_dates() { + var measure = new Measure(); + var date = new Date(); + var approvalDate = new Date(); + var effectivePeriod = new Period() + .setStart(java.sql.Date.valueOf(LocalDate.parse("2020-01-01"))) + .setEnd(java.sql.Date.valueOf(LocalDate.parse("2020-12-31"))); + measure.setDate(date); + measure.setApprovalDate(approvalDate); + measure.setEffectivePeriod(effectivePeriod); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(measure); + assertEquals(date, adapter.getDate()); + assertEquals(approvalDate, adapter.getApprovalDate()); + assertEquals(effectivePeriod, adapter.getEffectivePeriod()); + var newDate = new Date(); + newDate.setTime(100); + adapter.setDate(newDate); + assertEquals(newDate, measure.getDate()); + var newApprovalDate = new Date(); + newApprovalDate.setTime(100); + adapter.setApprovalDate(newApprovalDate); + assertEquals(newApprovalDate, measure.getApprovalDate()); + var newEffectivePeriod = new Period() + .setStart(java.sql.Date.valueOf(LocalDate.parse("2021-01-01"))) + .setEnd(java.sql.Date.valueOf(LocalDate.parse("2021-12-31"))); + adapter.setEffectivePeriod(newEffectivePeriod); + assertEquals(newEffectivePeriod, adapter.getEffectivePeriod()); + } + + @Test + void adapter_get_experimental() { + var measure = new Measure(); + var experimental = true; + measure.setExperimental(experimental); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(measure); + assertEquals(experimental, adapter.getExperimental()); + } + + @Test + void adapter_set_relatedArtifact() { + var measure = new Measure(); + var relatedArtifactList = List.of(new RelatedArtifact()); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(measure); + adapter.setRelatedArtifact(relatedArtifactList); + assertEquals(relatedArtifactList, measure.getRelatedArtifact()); + assertEquals(relatedArtifactList, adapter.getRelatedArtifact()); + } + + @Test + void adapter_copy() { + var measure = new Measure().setStatus(PublicationStatus.DRAFT); + measure.setId("plan-1"); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(measure); + var copy = (Measure) adapter.copy(); + var adapterCopy = new MeasureAdapter(copy); + adapterCopy.setId(new IdDt("Measure", "plan-2")); + assertNotEquals(measure.getId(), copy.getId()); + measure.setStatus(PublicationStatus.ACTIVE); + assertNotEquals(adapter.getStatus(), copy.getStatus()); + } + + @Test + @Disabled + void adapter_get_all_dependencies() { + var dependencies = List.of(""); + var measure = new Measure(); + measure.getMeta().addProfile(dependencies.get(0)); + measure.getRelatedArtifactFirstRep().setResource(dependencies.get(1)); + measure.getLibrary().add(new CanonicalType(dependencies.get(2))); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(measure); + var extractedDependencies = adapter.getDependencies(); + assertEquals(extractedDependencies.size(), dependencies.size()); + extractedDependencies.forEach(dep -> { + assertTrue(dependencies.indexOf(dep.getReference()) >= 0); + }); + } +} From e93b44fd82444f3fab72aa7b968859963f1359c3 Mon Sep 17 00:00:00 2001 From: Brenin Rhodes Date: Wed, 14 Aug 2024 09:25:01 -0600 Subject: [PATCH 35/38] add tests --- .../utility/adapter/dstu3/MeasureAdapter.java | 11 ++--- .../utility/adapter/r4/MeasureAdapter.java | 14 +++---- .../utility/adapter/r5/MeasureAdapter.java | 14 +++---- .../adapter/dstu3/MeasureAdapterTest.java | 42 ++++++++++++++++--- .../adapter/r4/MeasureAdapterTest.java | 39 +++++++++++++++-- .../adapter/r5/MeasureAdapterTest.java | 37 ++++++++++++++-- 6 files changed, 126 insertions(+), 31 deletions(-) diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/MeasureAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/MeasureAdapter.java index 60c2b8c2e..f1fdb1dae 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/MeasureAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/MeasureAdapter.java @@ -64,18 +64,19 @@ private void findEffectiveDataRequirements() { @Override public List getDependencies() { + List references = new ArrayList<>(); + final String referenceSource = getReferenceSource(); + addProfileReferences(references, referenceSource); // If an effectiveDataRequirements library is present, use it exclusively findEffectiveDataRequirements(); if (effectiveDataRequirements != null) { - return effectiveDataRequirementsAdapter.getDependencies(); + references.addAll(effectiveDataRequirementsAdapter.getDependencies()); + return references; } // Otherwise, fall back to the relatedArtifact and library - List references = new ArrayList<>(); - final String referenceSource = this.getMeasure().hasVersion() - ? this.getMeasure().getUrl() + "|" + this.getMeasure().getVersion() - : this.getMeasure().getUrl(); + /* relatedArtifact[].resource library[] diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/MeasureAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/MeasureAdapter.java index a6d2eb746..f56bda8f9 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/MeasureAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/MeasureAdapter.java @@ -7,7 +7,6 @@ import org.hl7.fhir.r4.model.CanonicalType; import org.hl7.fhir.r4.model.Library; import org.hl7.fhir.r4.model.Measure; -import org.hl7.fhir.r4.model.Reference; import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; @@ -51,7 +50,7 @@ private void findEffectiveDataRequirements() { var edrExtension = edrExtensions.size() == 1 ? edrExtensions.get(0) : null; if (edrExtension != null) { - var edrReference = ((Reference) edrExtension.getValue()).getReference(); + var edrReference = ((CanonicalType) edrExtension.getValue()).getValue(); for (var c : getMeasure().getContained()) { if (c.hasId() && String.format("#%s", c.getId()).equals(edrReference) && c instanceof Library) { effectiveDataRequirements = (Library) c; @@ -65,18 +64,19 @@ private void findEffectiveDataRequirements() { @Override public List getDependencies() { + List references = new ArrayList<>(); + final String referenceSource = getReferenceSource(); + addProfileReferences(references, referenceSource); // If an effectiveDataRequirements library is present, use it exclusively findEffectiveDataRequirements(); if (effectiveDataRequirements != null) { - return effectiveDataRequirementsAdapter.getDependencies(); + references.addAll(effectiveDataRequirementsAdapter.getDependencies()); + return references; } // Otherwise, fall back to the relatedArtifact and library - List references = new ArrayList<>(); - final String referenceSource = this.getMeasure().hasVersion() - ? this.getMeasure().getUrl() + "|" + this.getMeasure().getVersion() - : this.getMeasure().getUrl(); + /* relatedArtifact[].resource library[] diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/MeasureAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/MeasureAdapter.java index 0460ea87d..9e39cbb93 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/MeasureAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/MeasureAdapter.java @@ -7,7 +7,6 @@ import org.hl7.fhir.r5.model.CanonicalType; import org.hl7.fhir.r5.model.Library; import org.hl7.fhir.r5.model.Measure; -import org.hl7.fhir.r5.model.Reference; import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; @@ -51,7 +50,7 @@ private void findEffectiveDataRequirements() { var edrExtension = edrExtensions.size() == 1 ? edrExtensions.get(0) : null; if (edrExtension != null) { - var edrReference = ((Reference) edrExtension.getValue()).getReference(); + var edrReference = ((CanonicalType) edrExtension.getValue()).getValue(); for (var c : getMeasure().getContained()) { if (c.hasId() && String.format("#%s", c.getId()).equals(edrReference) && c instanceof Library) { effectiveDataRequirements = (Library) c; @@ -65,18 +64,19 @@ private void findEffectiveDataRequirements() { @Override public List getDependencies() { + List references = new ArrayList<>(); + final String referenceSource = getReferenceSource(); + addProfileReferences(references, referenceSource); // If an effectiveDataRequirements library is present, use it exclusively findEffectiveDataRequirements(); if (effectiveDataRequirements != null) { - return effectiveDataRequirementsAdapter.getDependencies(); + references.addAll(effectiveDataRequirementsAdapter.getDependencies()); + return references; } // Otherwise, fall back to the relatedArtifact and library - List references = new ArrayList<>(); - final String referenceSource = this.getMeasure().hasVersion() - ? this.getMeasure().getUrl() + "|" + this.getMeasure().getVersion() - : this.getMeasure().getUrl(); + /* relatedArtifact[].resource library[] diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/MeasureAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/MeasureAdapterTest.java index 602acb0c4..27850aa4f 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/MeasureAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/MeasureAdapterTest.java @@ -17,14 +17,18 @@ import java.util.Date; import java.util.List; import org.hl7.fhir.dstu3.model.Bundle; +import org.hl7.fhir.dstu3.model.CodeableConcept; +import org.hl7.fhir.dstu3.model.Coding; import org.hl7.fhir.dstu3.model.Enumerations.PublicationStatus; import org.hl7.fhir.dstu3.model.Library; import org.hl7.fhir.dstu3.model.Measure; +import org.hl7.fhir.dstu3.model.Patient; import org.hl7.fhir.dstu3.model.Period; +import org.hl7.fhir.dstu3.model.Reference; import org.hl7.fhir.dstu3.model.RelatedArtifact; import org.hl7.fhir.instance.model.api.IDomainResource; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; +import org.opencds.cqf.fhir.utility.Constants; import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; public class MeasureAdapterTest { @@ -159,13 +163,12 @@ void adapter_copy() { } @Test - @Disabled void adapter_get_all_dependencies() { - var dependencies = List.of(""); + var dependencies = List.of("profileRef", "relatedArtifactRef", "libraryRef"); var measure = new Measure(); measure.getMeta().addProfile(dependencies.get(0)); - // measure.getRelatedArtifactFirstRep().setResource(dependencies.get(1)); - // measure.getLibrary().add(new CanonicalType(dependencies.get(2))); + measure.getRelatedArtifactFirstRep().setResource(new Reference(dependencies.get(1))); + measure.getLibrary().add(new Reference(dependencies.get(2))); var adapter = adapterFactory.createKnowledgeArtifactAdapter(measure); var extractedDependencies = adapter.getDependencies(); assertEquals(extractedDependencies.size(), dependencies.size()); @@ -173,4 +176,33 @@ void adapter_get_all_dependencies() { assertTrue(dependencies.indexOf(dep.getReference()) >= 0); }); } + + @Test + void adapter_get_all_dependencies_with_effective_data_requirements() { + var dependencies = List.of( + "libraryProfileRef", + "relatedArtifactRef", + "dataRequirementProfileRef", + "dataRequirementCodeFilterRef", + "measureProfileRef"); + var library = new Library() + .setType(new CodeableConcept(new Coding( + "http://terminology.hl7.org/CodeSystem/library-type", + "module-definition", + "Module Definition"))); + library.setId("test"); + library.getMeta().addProfile(dependencies.get(0)); + library.getRelatedArtifactFirstRep().setResource(new Reference(dependencies.get(1))); + library.addDataRequirement().addProfile(dependencies.get(2)); + library.addDataRequirement().addCodeFilter().setValueSet(new Reference(dependencies.get(3))); + var measure = new Measure().addContained(new Patient()).addContained(library); + measure.getMeta().addProfile(dependencies.get(4)); + measure.addExtension(Constants.CQFM_EFFECTIVE_DATA_REQUIREMENTS, new Reference("#test")); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(measure); + var extractedDependencies = adapter.getDependencies(); + assertEquals(dependencies.size(), extractedDependencies.size()); + extractedDependencies.forEach(dep -> { + assertTrue(dependencies.indexOf(dep.getReference()) >= 0); + }); + } } diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/MeasureAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/MeasureAdapterTest.java index a6d86cae6..595c90920 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/MeasureAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/MeasureAdapterTest.java @@ -19,13 +19,16 @@ import org.hl7.fhir.instance.model.api.IDomainResource; import org.hl7.fhir.r4.model.Bundle; import org.hl7.fhir.r4.model.CanonicalType; +import org.hl7.fhir.r4.model.CodeableConcept; +import org.hl7.fhir.r4.model.Coding; import org.hl7.fhir.r4.model.Enumerations.PublicationStatus; import org.hl7.fhir.r4.model.Library; import org.hl7.fhir.r4.model.Measure; +import org.hl7.fhir.r4.model.Patient; import org.hl7.fhir.r4.model.Period; import org.hl7.fhir.r4.model.RelatedArtifact; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; +import org.opencds.cqf.fhir.utility.Constants; import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; public class MeasureAdapterTest { @@ -160,16 +163,44 @@ void adapter_copy() { } @Test - @Disabled void adapter_get_all_dependencies() { - var dependencies = List.of(""); + var dependencies = List.of("profileRef", "relatedArtifactRef", "libraryRef"); var measure = new Measure(); measure.getMeta().addProfile(dependencies.get(0)); measure.getRelatedArtifactFirstRep().setResource(dependencies.get(1)); measure.getLibrary().add(new CanonicalType(dependencies.get(2))); var adapter = adapterFactory.createKnowledgeArtifactAdapter(measure); var extractedDependencies = adapter.getDependencies(); - assertEquals(extractedDependencies.size(), dependencies.size()); + assertEquals(dependencies.size(), extractedDependencies.size()); + extractedDependencies.forEach(dep -> { + assertTrue(dependencies.indexOf(dep.getReference()) >= 0); + }); + } + + @Test + void adapter_get_all_dependencies_with_effective_data_requirements() { + var dependencies = List.of( + "libraryProfileRef", + "relatedArtifactRef", + "dataRequirementProfileRef", + "dataRequirementCodeFilterRef", + "measureProfileRef"); + var library = new Library() + .setType(new CodeableConcept(new Coding( + "http://terminology.hl7.org/CodeSystem/library-type", + "module-definition", + "Module Definition"))); + library.setId("test"); + library.getMeta().addProfile(dependencies.get(0)); + library.getRelatedArtifactFirstRep().setResource(dependencies.get(1)); + library.addDataRequirement().addProfile(dependencies.get(2)); + library.addDataRequirement().addCodeFilter().setValueSet(dependencies.get(3)); + var measure = new Measure().addContained(new Patient()).addContained(library); + measure.getMeta().addProfile(dependencies.get(4)); + measure.addExtension(Constants.CQFM_EFFECTIVE_DATA_REQUIREMENTS, new CanonicalType("#test")); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(measure); + var extractedDependencies = adapter.getDependencies(); + assertEquals(dependencies.size(), extractedDependencies.size()); extractedDependencies.forEach(dep -> { assertTrue(dependencies.indexOf(dep.getReference()) >= 0); }); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/MeasureAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/MeasureAdapterTest.java index 63637c67a..c7c6b52df 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/MeasureAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/MeasureAdapterTest.java @@ -19,13 +19,16 @@ import org.hl7.fhir.instance.model.api.IDomainResource; import org.hl7.fhir.r5.model.Bundle; import org.hl7.fhir.r5.model.CanonicalType; +import org.hl7.fhir.r5.model.CodeableConcept; +import org.hl7.fhir.r5.model.Coding; import org.hl7.fhir.r5.model.Enumerations.PublicationStatus; import org.hl7.fhir.r5.model.Library; import org.hl7.fhir.r5.model.Measure; +import org.hl7.fhir.r5.model.Patient; import org.hl7.fhir.r5.model.Period; import org.hl7.fhir.r5.model.RelatedArtifact; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; +import org.opencds.cqf.fhir.utility.Constants; import org.opencds.cqf.fhir.utility.visitor.PackageVisitor; public class MeasureAdapterTest { @@ -160,9 +163,8 @@ void adapter_copy() { } @Test - @Disabled void adapter_get_all_dependencies() { - var dependencies = List.of(""); + var dependencies = List.of("profileRef", "relatedArtifactRef", "libraryRef"); var measure = new Measure(); measure.getMeta().addProfile(dependencies.get(0)); measure.getRelatedArtifactFirstRep().setResource(dependencies.get(1)); @@ -174,4 +176,33 @@ void adapter_get_all_dependencies() { assertTrue(dependencies.indexOf(dep.getReference()) >= 0); }); } + + @Test + void adapter_get_all_dependencies_with_effective_data_requirements() { + var dependencies = List.of( + "libraryProfileRef", + "relatedArtifactRef", + "dataRequirementProfileRef", + "dataRequirementCodeFilterRef", + "measureProfileRef"); + var library = new Library() + .setType(new CodeableConcept(new Coding( + "http://terminology.hl7.org/CodeSystem/library-type", + "module-definition", + "Module Definition"))); + library.setId("test"); + library.getMeta().addProfile(dependencies.get(0)); + library.getRelatedArtifactFirstRep().setResource(dependencies.get(1)); + library.addDataRequirement().addProfile(dependencies.get(2)); + library.addDataRequirement().addCodeFilter().setValueSet(dependencies.get(3)); + var measure = new Measure().addContained(new Patient()).addContained(library); + measure.getMeta().addProfile(dependencies.get(4)); + measure.addExtension(Constants.CQFM_EFFECTIVE_DATA_REQUIREMENTS, new CanonicalType("#test")); + var adapter = adapterFactory.createKnowledgeArtifactAdapter(measure); + var extractedDependencies = adapter.getDependencies(); + assertEquals(dependencies.size(), extractedDependencies.size()); + extractedDependencies.forEach(dep -> { + assertTrue(dependencies.indexOf(dep.getReference()) >= 0); + }); + } } From 2c995fba4fb5f511e44c484d50bbb54f8d4c2612 Mon Sep 17 00:00:00 2001 From: Brenin Rhodes Date: Wed, 14 Aug 2024 10:32:09 -0600 Subject: [PATCH 36/38] Add dependency tracing logic to MeasureAdapters --- .../opencds/cqf/fhir/utility/Constants.java | 7 ++ .../fhir/utility/adapter/MeasureAdapter.java | 16 +++ .../utility/adapter/dstu3/MeasureAdapter.java | 60 +++++++++--- .../utility/adapter/r4/MeasureAdapter.java | 98 ++++++++++++++++++- .../utility/adapter/r5/MeasureAdapter.java | 98 ++++++++++++++++++- 5 files changed, 258 insertions(+), 21 deletions(-) create mode 100644 cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/MeasureAdapter.java diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/Constants.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/Constants.java index 75ec387f0..a0c0f0246 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/Constants.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/Constants.java @@ -65,6 +65,7 @@ private Constants() {} public static final String CQIF_LIBRARY = "http://hl7.org/fhir/StructureDefinition/cqif-library"; public static final String CQIF_CQL_EXPRESSION = "http://hl7.org/fhir/StructureDefinition/cqif-cqlExpression"; + public static final String CQF_CQL_OPTIONS = "http://hl7.org/fhir/StructureDefinition/cqf-cqlOptions"; public static final String CQF_EXPANSION_PARAMETERS = "http://hl7.org/fhir/StructureDefinition/cqf-expansionParameters"; public static final String CQF_EXPRESSION = "http://hl7.org/fhir/StructureDefinition/cqf-expression"; @@ -77,6 +78,12 @@ private Constants() {} "http://hl7.org/fhir/us/cqfmeasures/StructureDefinition/cqfm-logicDefinition"; public static final String CQFM_SOFTWARE_SYSTEM = "http://hl7.org/fhir/us/cqfmeasures/StructureDefinition/cqfm-softwaresystem"; + public static final String CQFM_INPUT_PARAMETERS = + "http://hl7.org/fhir/us/cqfmeasures/StructureDefinition/cqfm-inputParameters"; + public static final String CQFM_COMPONENT = "http://hl7.org/fhir/us/cqfmeasures/StructureDefinition/cqfm-component"; + + public static final String CRMI_EFFECTIVE_DATA_REQUIREMENTS = + "http://hl7.org/fhir/uv/crmi/StructureDefinition/crmi-effectiveDataRequirements"; public static final String DTR_QUESTIONNAIRE_RESPONSE_QUESTIONNAIRE = "http://hl7.org/fhir/us/davinci-dtr/StructureDefinition/dtr-questionnaireresponse-questionnaire"; diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/MeasureAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/MeasureAdapter.java new file mode 100644 index 000000000..fa5382518 --- /dev/null +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/MeasureAdapter.java @@ -0,0 +1,16 @@ +package org.opencds.cqf.fhir.utility.adapter; + +import java.util.Arrays; +import java.util.List; +import org.opencds.cqf.fhir.utility.Constants; + +/** + * This interface exposes common functionality across all FHIR Questionnaire versions. + */ +public interface MeasureAdapter { + public static List CANONICAL_EXTENSIONS = + Arrays.asList(Constants.CQFM_EFFECTIVE_DATA_REQUIREMENTS, Constants.CRMI_EFFECTIVE_DATA_REQUIREMENTS); + + public static List REFERENCE_EXTENSIONS = Arrays.asList( + Constants.CQFM_INPUT_PARAMETERS, Constants.CQF_EXPANSION_PARAMETERS, Constants.CQF_CQL_OPTIONS); +} diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/MeasureAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/MeasureAdapter.java index f1fdb1dae..95efc6f55 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/MeasureAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/MeasureAdapter.java @@ -6,11 +6,14 @@ import org.hl7.fhir.dstu3.model.Library; import org.hl7.fhir.dstu3.model.Measure; import org.hl7.fhir.dstu3.model.Reference; +import org.hl7.fhir.dstu3.model.RelatedArtifact; import org.hl7.fhir.instance.model.api.IDomainResource; +import org.opencds.cqf.fhir.utility.Constants; import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; -public class MeasureAdapter extends KnowledgeArtifactAdapter { +public class MeasureAdapter extends KnowledgeArtifactAdapter + implements org.opencds.cqf.fhir.utility.adapter.MeasureAdapter { public MeasureAdapter(IDomainResource measure) { super(measure); @@ -80,10 +83,10 @@ public List getDependencies() { /* relatedArtifact[].resource library[] - group[].population[].criteria.reference - group[].stratifier[].criteria.reference - group[].stratifier[].component[].criteria.reference - supplementalData[].criteria.reference + group[].population[].criteria.reference - no path on dstu3 + group[].stratifier[].criteria.reference - no path on dstu3 + group[].stratifier[].component[].criteria.reference - no path on dstu3 + supplementalData[].criteria.reference - no path on dstu3 extension[cqfm-inputParameters][] extension[cqfm-expansionParameters][] extension[cqfm-effectiveDataRequirements] @@ -93,21 +96,56 @@ public List getDependencies() { */ // relatedArtifact[].resource - references.addAll(this.getRelatedArtifact().stream() + references.addAll(getRelatedArtifact().stream() .map(ra -> DependencyInfo.convertRelatedArtifact(ra, referenceSource)) .collect(Collectors.toList())); // library[] - List libraries = this.getMeasure().getLibrary(); - for (Reference ref : libraries) { + for (var library : getMeasure().getLibrary()) { DependencyInfo dependency = new DependencyInfo( referenceSource, - ref.getReference(), - ref.getExtension(), - (reference) -> ref.setReference(reference)); + library.getReference(), + library.getExtension(), + (reference) -> library.setReference(reference)); references.add(dependency); } + // extension[cqfm-effectiveDataRequirements] + // extension[crmi-effectiveDataRequirements] + get().getExtension().stream() + .filter(e -> CANONICAL_EXTENSIONS.contains(e.getUrl())) + .forEach(referenceExt -> references.add(new DependencyInfo( + referenceSource, + ((Reference) referenceExt.getValue()).getReference(), + referenceExt.getExtension(), + (reference) -> referenceExt.setValue(new Reference(reference))))); + + // extension[cqfm-inputParameters][] + // extension[cqfm-expansionParameters][] + // extension[cqfm-cqlOptions] + // extension[cqfm-effectiveDataRequirements] + // extension[crmi-effectiveDataRequirements] + get().getExtension().stream() + .filter(e -> REFERENCE_EXTENSIONS.contains(e.getUrl())) + .forEach(referenceExt -> references.add(new DependencyInfo( + referenceSource, + ((Reference) referenceExt.getValue()).getReference(), + referenceExt.getExtension(), + (reference) -> referenceExt.setValue(new Reference(reference))))); + + // extension[cqfm-component][].resource + get().getExtensionsByUrl(Constants.CQFM_COMPONENT).forEach(ext -> { + final var ref = (RelatedArtifact) ext.getValue(); + if (ref.hasResource() && ref.getResource().hasReference()) { + final var dep = new DependencyInfo( + referenceSource, + ref.getResource().getReference(), + ref.getExtension(), + (reference) -> ref.getResource().setReference(reference)); + references.add(dep); + } + }); + return references; } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/MeasureAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/MeasureAdapter.java index f56bda8f9..05cbfddaa 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/MeasureAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r4/MeasureAdapter.java @@ -7,10 +7,14 @@ import org.hl7.fhir.r4.model.CanonicalType; import org.hl7.fhir.r4.model.Library; import org.hl7.fhir.r4.model.Measure; +import org.hl7.fhir.r4.model.Reference; +import org.hl7.fhir.r4.model.RelatedArtifact; +import org.opencds.cqf.fhir.utility.Constants; import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; -public class MeasureAdapter extends KnowledgeArtifactAdapter { +public class MeasureAdapter extends KnowledgeArtifactAdapter + implements org.opencds.cqf.fhir.utility.adapter.MeasureAdapter { public MeasureAdapter(IDomainResource measure) { super(measure); @@ -93,18 +97,102 @@ public List getDependencies() { */ // relatedArtifact[].resource - references.addAll(this.getRelatedArtifact().stream() + references.addAll(getRelatedArtifact().stream() .map(ra -> DependencyInfo.convertRelatedArtifact(ra, referenceSource)) .collect(Collectors.toList())); // library[] - List libraries = this.getMeasure().getLibrary(); - for (CanonicalType ct : libraries) { + for (var library : getMeasure().getLibrary()) { DependencyInfo dependency = new DependencyInfo( - referenceSource, ct.getValue(), ct.getExtension(), (reference) -> ct.setValue(reference)); + referenceSource, + library.getValue(), + library.getExtension(), + (reference) -> library.setValue(reference)); references.add(dependency); } + for (final var group : getMeasure().getGroup()) { + for (final var population : group.getPopulation()) { + // group[].population[].criteria.reference + if (population.getCriteria().hasReference()) { + final var dependency = new DependencyInfo( + referenceSource, + population.getCriteria().getReference(), + population.getCriteria().getExtension(), + (reference) -> population.getCriteria().setReference(reference)); + references.add(dependency); + } + } + for (final var stratifier : group.getStratifier()) { + // group[].stratifier[].criteria.reference + if (stratifier.getCriteria().hasReference()) { + final var dependency = new DependencyInfo( + referenceSource, + stratifier.getCriteria().getReference(), + stratifier.getCriteria().getExtension(), + (reference) -> stratifier.getCriteria().setReference(reference)); + references.add(dependency); + } + for (final var component : stratifier.getComponent()) { + // group[].stratifier[].component[].criteria.reference + if (component.getCriteria().hasReference()) { + final var stratifierComponentDep = new DependencyInfo( + referenceSource, + component.getCriteria().getReference(), + component.getCriteria().getExtension(), + (reference) -> component.getCriteria().setReference(reference)); + references.add(stratifierComponentDep); + } + } + } + } + + for (final var supplement : getMeasure().getSupplementalData()) { + // supplementalData[].criteria.reference + if (supplement.getCriteria().hasReference()) { + final var dependency = new DependencyInfo( + referenceSource, + supplement.getCriteria().getReference(), + supplement.getCriteria().getExtension(), + (reference) -> supplement.getCriteria().setReference(reference)); + references.add(dependency); + } + } + + // extension[cqfm-effectiveDataRequirements] + // extension[crmi-effectiveDataRequirements] + get().getExtension().stream() + .filter(e -> CANONICAL_EXTENSIONS.contains(e.getUrl())) + .forEach(referenceExt -> references.add(new DependencyInfo( + referenceSource, + ((CanonicalType) referenceExt.getValue()).getValue(), + referenceExt.getExtension(), + (reference) -> referenceExt.setValue(new CanonicalType(reference))))); + + // extension[cqfm-inputParameters][] + // extension[cqfm-expansionParameters][] + // extension[cqfm-cqlOptions] + get().getExtension().stream() + .filter(e -> REFERENCE_EXTENSIONS.contains(e.getUrl())) + .forEach(referenceExt -> references.add(new DependencyInfo( + referenceSource, + ((Reference) referenceExt.getValue()).getReference(), + referenceExt.getExtension(), + (reference) -> referenceExt.setValue(new Reference(reference))))); + + // extension[cqfm-component][].resource + get().getExtensionsByUrl(Constants.CQFM_COMPONENT).forEach(ext -> { + final var ref = (RelatedArtifact) ext.getValue(); + if (ref.hasResource()) { + final var dep = new DependencyInfo( + referenceSource, + ref.getResource(), + ref.getExtension(), + (reference) -> ref.setResource(reference)); + references.add(dep); + } + }); + return references; } } diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/MeasureAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/MeasureAdapter.java index 9e39cbb93..eef30e04b 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/MeasureAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/r5/MeasureAdapter.java @@ -7,10 +7,14 @@ import org.hl7.fhir.r5.model.CanonicalType; import org.hl7.fhir.r5.model.Library; import org.hl7.fhir.r5.model.Measure; +import org.hl7.fhir.r5.model.Reference; +import org.hl7.fhir.r5.model.RelatedArtifact; +import org.opencds.cqf.fhir.utility.Constants; import org.opencds.cqf.fhir.utility.adapter.DependencyInfo; import org.opencds.cqf.fhir.utility.adapter.IDependencyInfo; -public class MeasureAdapter extends KnowledgeArtifactAdapter { +public class MeasureAdapter extends KnowledgeArtifactAdapter + implements org.opencds.cqf.fhir.utility.adapter.MeasureAdapter { public MeasureAdapter(IDomainResource measure) { super(measure); @@ -93,18 +97,102 @@ public List getDependencies() { */ // relatedArtifact[].resource - references.addAll(this.getRelatedArtifact().stream() + references.addAll(getRelatedArtifact().stream() .map(ra -> DependencyInfo.convertRelatedArtifact(ra, referenceSource)) .collect(Collectors.toList())); // library[] - List libraries = this.getMeasure().getLibrary(); - for (CanonicalType ct : libraries) { + for (var library : getMeasure().getLibrary()) { DependencyInfo dependency = new DependencyInfo( - referenceSource, ct.getValue(), ct.getExtension(), (reference) -> ct.setValue(reference)); + referenceSource, + library.getValue(), + library.getExtension(), + (reference) -> library.setValue(reference)); references.add(dependency); } + for (final var group : getMeasure().getGroup()) { + for (final var population : group.getPopulation()) { + // group[].population[].criteria.reference + if (population.getCriteria().hasReference()) { + final var dependency = new DependencyInfo( + referenceSource, + population.getCriteria().getReference(), + population.getCriteria().getExtension(), + (reference) -> population.getCriteria().setReference(reference)); + references.add(dependency); + } + } + for (final var stratifier : group.getStratifier()) { + // group[].stratifier[].criteria.reference + if (stratifier.getCriteria().hasReference()) { + final var dependency = new DependencyInfo( + referenceSource, + stratifier.getCriteria().getReference(), + stratifier.getCriteria().getExtension(), + (reference) -> stratifier.getCriteria().setReference(reference)); + references.add(dependency); + } + for (final var component : stratifier.getComponent()) { + // group[].stratifier[].component[].criteria.reference + if (component.getCriteria().hasReference()) { + final var stratifierComponentDep = new DependencyInfo( + referenceSource, + component.getCriteria().getReference(), + component.getCriteria().getExtension(), + (reference) -> component.getCriteria().setReference(reference)); + references.add(stratifierComponentDep); + } + } + } + } + + for (final var supplement : getMeasure().getSupplementalData()) { + // supplementalData[].criteria.reference + if (supplement.getCriteria().hasReference()) { + final var dependency = new DependencyInfo( + referenceSource, + supplement.getCriteria().getReference(), + supplement.getCriteria().getExtension(), + (reference) -> supplement.getCriteria().setReference(reference)); + references.add(dependency); + } + } + + // extension[cqfm-effectiveDataRequirements] + // extension[crmi-effectiveDataRequirements] + get().getExtension().stream() + .filter(e -> CANONICAL_EXTENSIONS.contains(e.getUrl())) + .forEach(referenceExt -> references.add(new DependencyInfo( + referenceSource, + ((CanonicalType) referenceExt.getValue()).getValue(), + referenceExt.getExtension(), + (reference) -> referenceExt.setValue(new CanonicalType(reference))))); + + // extension[cqfm-inputParameters][] + // extension[cqfm-expansionParameters][] + // extension[cqfm-cqlOptions] + get().getExtension().stream() + .filter(e -> REFERENCE_EXTENSIONS.contains(e.getUrl())) + .forEach(referenceExt -> references.add(new DependencyInfo( + referenceSource, + ((Reference) referenceExt.getValue()).getReference(), + referenceExt.getExtension(), + (reference) -> referenceExt.setValue(new Reference(reference))))); + + // extension[cqfm-component][].resource + get().getExtensionsByUrl(Constants.CQFM_COMPONENT).forEach(ext -> { + final var ref = (RelatedArtifact) ext.getValue(); + if (ref.hasResource()) { + final var dep = new DependencyInfo( + referenceSource, + ref.getResource(), + ref.getExtension(), + (reference) -> ref.setResource(reference)); + references.add(dep); + } + }); + return references; } } From 46dc456511d3a9d88fcafa5c0d52ff16ac782f2a Mon Sep 17 00:00:00 2001 From: Brenin Rhodes Date: Wed, 14 Aug 2024 19:02:45 -0600 Subject: [PATCH 37/38] add tests --- .../utility/adapter/dstu3/MeasureAdapter.java | 2 -- .../adapter/dstu3/MeasureAdapterTest.java | 14 +++++++++- .../adapter/r4/MeasureAdapterTest.java | 26 ++++++++++++++++++- .../adapter/r5/MeasureAdapterTest.java | 26 ++++++++++++++++++- 4 files changed, 63 insertions(+), 5 deletions(-) diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/MeasureAdapter.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/MeasureAdapter.java index 95efc6f55..e266d4c22 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/MeasureAdapter.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/adapter/dstu3/MeasureAdapter.java @@ -123,8 +123,6 @@ public List getDependencies() { // extension[cqfm-inputParameters][] // extension[cqfm-expansionParameters][] // extension[cqfm-cqlOptions] - // extension[cqfm-effectiveDataRequirements] - // extension[crmi-effectiveDataRequirements] get().getExtension().stream() .filter(e -> REFERENCE_EXTENSIONS.contains(e.getUrl())) .forEach(referenceExt -> references.add(new DependencyInfo( diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/MeasureAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/MeasureAdapterTest.java index 27850aa4f..20c9b928e 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/MeasureAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/dstu3/MeasureAdapterTest.java @@ -164,11 +164,23 @@ void adapter_copy() { @Test void adapter_get_all_dependencies() { - var dependencies = List.of("profileRef", "relatedArtifactRef", "libraryRef"); + var dependencies = List.of( + "profileRef", + "relatedArtifactRef", + "libraryRef", + "inputParametersRef", + "expansionParametersRef", + "cqlOptionsRef", + "componentRef"); var measure = new Measure(); measure.getMeta().addProfile(dependencies.get(0)); measure.getRelatedArtifactFirstRep().setResource(new Reference(dependencies.get(1))); measure.getLibrary().add(new Reference(dependencies.get(2))); + measure.addExtension(Constants.CQFM_INPUT_PARAMETERS, new Reference(dependencies.get(3))); + measure.addExtension(Constants.CQF_EXPANSION_PARAMETERS, new Reference(dependencies.get(4))); + measure.addExtension(Constants.CQF_CQL_OPTIONS, new Reference(dependencies.get(5))); + measure.addExtension( + Constants.CQFM_COMPONENT, new RelatedArtifact().setResource(new Reference(dependencies.get(6)))); var adapter = adapterFactory.createKnowledgeArtifactAdapter(measure); var extractedDependencies = adapter.getDependencies(); assertEquals(extractedDependencies.size(), dependencies.size()); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/MeasureAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/MeasureAdapterTest.java index 595c90920..522c94889 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/MeasureAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r4/MeasureAdapterTest.java @@ -22,10 +22,12 @@ import org.hl7.fhir.r4.model.CodeableConcept; import org.hl7.fhir.r4.model.Coding; import org.hl7.fhir.r4.model.Enumerations.PublicationStatus; +import org.hl7.fhir.r4.model.Expression; import org.hl7.fhir.r4.model.Library; import org.hl7.fhir.r4.model.Measure; import org.hl7.fhir.r4.model.Patient; import org.hl7.fhir.r4.model.Period; +import org.hl7.fhir.r4.model.Reference; import org.hl7.fhir.r4.model.RelatedArtifact; import org.junit.jupiter.api.Test; import org.opencds.cqf.fhir.utility.Constants; @@ -164,11 +166,33 @@ void adapter_copy() { @Test void adapter_get_all_dependencies() { - var dependencies = List.of("profileRef", "relatedArtifactRef", "libraryRef"); + var dependencies = List.of( + "profileRef", + "relatedArtifactRef", + "libraryRef", + "populationCriteriaRef", + "stratifierCriteriaRef", + "stratifierComponentCriteriaRef", + "supplementalDataCriteriaRef", + "inputParametersRef", + "expansionParametersRef", + "cqlOptionsRef", + "componentRef"); var measure = new Measure(); measure.getMeta().addProfile(dependencies.get(0)); measure.getRelatedArtifactFirstRep().setResource(dependencies.get(1)); measure.getLibrary().add(new CanonicalType(dependencies.get(2))); + measure.addGroup().addPopulation().setCriteria(new Expression().setReference(dependencies.get(3))); + measure.addGroup().addStratifier().setCriteria(new Expression().setReference(dependencies.get(4))); + measure.addGroup() + .addStratifier() + .addComponent() + .setCriteria(new Expression().setReference(dependencies.get(5))); + measure.addSupplementalData().setCriteria(new Expression().setReference(dependencies.get(6))); + measure.addExtension(Constants.CQFM_INPUT_PARAMETERS, new Reference(dependencies.get(7))); + measure.addExtension(Constants.CQF_EXPANSION_PARAMETERS, new Reference(dependencies.get(8))); + measure.addExtension(Constants.CQF_CQL_OPTIONS, new Reference(dependencies.get(9))); + measure.addExtension(Constants.CQFM_COMPONENT, new RelatedArtifact().setResource(dependencies.get(10))); var adapter = adapterFactory.createKnowledgeArtifactAdapter(measure); var extractedDependencies = adapter.getDependencies(); assertEquals(dependencies.size(), extractedDependencies.size()); diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/MeasureAdapterTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/MeasureAdapterTest.java index c7c6b52df..51a745c8f 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/MeasureAdapterTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/adapter/r5/MeasureAdapterTest.java @@ -22,10 +22,12 @@ import org.hl7.fhir.r5.model.CodeableConcept; import org.hl7.fhir.r5.model.Coding; import org.hl7.fhir.r5.model.Enumerations.PublicationStatus; +import org.hl7.fhir.r5.model.Expression; import org.hl7.fhir.r5.model.Library; import org.hl7.fhir.r5.model.Measure; import org.hl7.fhir.r5.model.Patient; import org.hl7.fhir.r5.model.Period; +import org.hl7.fhir.r5.model.Reference; import org.hl7.fhir.r5.model.RelatedArtifact; import org.junit.jupiter.api.Test; import org.opencds.cqf.fhir.utility.Constants; @@ -164,11 +166,33 @@ void adapter_copy() { @Test void adapter_get_all_dependencies() { - var dependencies = List.of("profileRef", "relatedArtifactRef", "libraryRef"); + var dependencies = List.of( + "profileRef", + "relatedArtifactRef", + "libraryRef", + "populationCriteriaRef", + "stratifierCriteriaRef", + "stratifierComponentCriteriaRef", + "supplementalDataCriteriaRef", + "inputParametersRef", + "expansionParametersRef", + "cqlOptionsRef", + "componentRef"); var measure = new Measure(); measure.getMeta().addProfile(dependencies.get(0)); measure.getRelatedArtifactFirstRep().setResource(dependencies.get(1)); measure.getLibrary().add(new CanonicalType(dependencies.get(2))); + measure.addGroup().addPopulation().setCriteria(new Expression().setReference(dependencies.get(3))); + measure.addGroup().addStratifier().setCriteria(new Expression().setReference(dependencies.get(4))); + measure.addGroup() + .addStratifier() + .addComponent() + .setCriteria(new Expression().setReference(dependencies.get(5))); + measure.addSupplementalData().setCriteria(new Expression().setReference(dependencies.get(6))); + measure.addExtension(Constants.CQFM_INPUT_PARAMETERS, new Reference(dependencies.get(7))); + measure.addExtension(Constants.CQF_EXPANSION_PARAMETERS, new Reference(dependencies.get(8))); + measure.addExtension(Constants.CQF_CQL_OPTIONS, new Reference(dependencies.get(9))); + measure.addExtension(Constants.CQFM_COMPONENT, new RelatedArtifact().setResource(dependencies.get(10))); var adapter = adapterFactory.createKnowledgeArtifactAdapter(measure); var extractedDependencies = adapter.getDependencies(); assertEquals(extractedDependencies.size(), dependencies.size()); From 9e3f2cdc10df7e11bbaa3ea26988243cb1459b70 Mon Sep 17 00:00:00 2001 From: Brenin Rhodes Date: Thu, 15 Aug 2024 11:38:13 -0600 Subject: [PATCH 38/38] add tests --- .../opencds/cqf/fhir/utility/ValueSets.java | 5 - .../cqf/fhir/utility/ValueSetsTest.java | 170 +++++++++++++++++- 2 files changed, 168 insertions(+), 7 deletions(-) diff --git a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/ValueSets.java b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/ValueSets.java index d7558ade9..38eb63b09 100644 --- a/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/ValueSets.java +++ b/cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/ValueSets.java @@ -333,11 +333,6 @@ public static String getId(FhirContext fhirContext, IBaseResource valueSet) { return getStringValueFromPrimitiveAccessor(valueSet, idDef.getAccessor()); } - public static String getResourceType(FhirContext fhirContext, IBaseResource resource) { - RuntimeResourceDefinition def = fhirContext.getResourceDefinition(resource); - return def.getName(); - } - private static String getStringValueFromPrimitiveAccessor(IBase value, IAccessor accessor) { if (value == null || accessor == null) { return null; diff --git a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/ValueSetsTest.java b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/ValueSetsTest.java index 720eb2a4f..da24e6256 100644 --- a/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/ValueSetsTest.java +++ b/cqf-fhir-utility/src/test/java/org/opencds/cqf/fhir/utility/ValueSetsTest.java @@ -5,6 +5,7 @@ import ca.uhn.fhir.context.FhirContext; import java.lang.reflect.InvocationTargetException; +import java.util.Collections; import org.hl7.fhir.r4.model.ValueSet; import org.junit.jupiter.api.Test; import org.opencds.cqf.cql.engine.runtime.Code; @@ -13,11 +14,176 @@ class ValueSetsTest { private final FhirContext fhirContextR4 = FhirContext.forR4Cached(); @Test - void testNulls() { + void testGetCompose() { + var valueSet = new ValueSet(); + assertNull(ValueSets.getCompose(fhirContextR4, valueSet)); + var compose = new ValueSet.ValueSetComposeComponent(); + valueSet.setCompose(compose); + assertEquals(compose, ValueSets.getCompose(fhirContextR4, valueSet)); + } + + @Test + void testGetIncludes() { + var valueSet = new ValueSet(); + assertNull(ValueSets.getIncludes(fhirContextR4, valueSet)); + var include = new ValueSet.ConceptSetComponent(); + var compose = new ValueSet.ValueSetComposeComponent(); + valueSet.setCompose(compose); + assertNull(ValueSets.getIncludes(fhirContextR4, valueSet)); + compose.addInclude(include); + assertEquals(include, ValueSets.getIncludes(fhirContextR4, valueSet).get(0)); + } + + @Test + void testGetIncludeConcepts() { + var concept = new ValueSet.ConceptReferenceComponent(); + var include = new ValueSet.ConceptSetComponent().addConcept(concept); + var valueSet = new ValueSet(); + assertNull(ValueSets.getIncludeConcepts(fhirContextR4, valueSet)); + valueSet.setCompose(new ValueSet.ValueSetComposeComponent().addInclude(include)); + assertEquals( + concept, ValueSets.getIncludeConcepts(fhirContextR4, valueSet).get(0)); + } + + @Test + void testGetIncludeFilters() { + var filter = new ValueSet.ConceptSetFilterComponent(); + var include = new ValueSet.ConceptSetComponent().addFilter(filter); + var valueSet = new ValueSet(); + assertNull(ValueSets.getIncludeFilters(fhirContextR4, valueSet)); + valueSet.setCompose(new ValueSet.ValueSetComposeComponent().addInclude(include)); + assertEquals( + filter, ValueSets.getIncludeFilters(fhirContextR4, valueSet).get(0)); + } + + @Test + void testGetExcludes() { + var valueSet = new ValueSet(); + assertNull(ValueSets.getExcludes(fhirContextR4, valueSet)); + var exclude = new ValueSet.ConceptSetComponent(); + var compose = new ValueSet.ValueSetComposeComponent(); + valueSet.setCompose(compose); + assertNull(ValueSets.getExcludes(fhirContextR4, valueSet)); + compose.addExclude(exclude); + assertEquals(exclude, ValueSets.getExcludes(fhirContextR4, valueSet).get(0)); + } + + @Test + void testGetExcludeConcepts() { + var concept = new ValueSet.ConceptReferenceComponent(); + var exclude = new ValueSet.ConceptSetComponent().addConcept(concept); + var valueSet = new ValueSet(); + assertNull(ValueSets.getExcludeConcepts(fhirContextR4, valueSet)); + valueSet.setCompose(new ValueSet.ValueSetComposeComponent().addExclude(exclude)); + assertEquals( + concept, ValueSets.getExcludeConcepts(fhirContextR4, valueSet).get(0)); + } + + @Test + void testGetExcludeFilters() { + var filter = new ValueSet.ConceptSetFilterComponent(); + var exclude = new ValueSet.ConceptSetComponent().addFilter(filter); + var valueSet = new ValueSet(); + assertNull(ValueSets.getExcludeFilters(fhirContextR4, valueSet)); + valueSet.setCompose(new ValueSet.ValueSetComposeComponent().addExclude(exclude)); + assertEquals( + filter, ValueSets.getExcludeFilters(fhirContextR4, valueSet).get(0)); + } + + @Test + void testGetExpansion() { + var valueSet = new ValueSet(); + assertNull(ValueSets.getExpansion(fhirContextR4, valueSet)); + var expansion = new ValueSet.ValueSetExpansionComponent(); + valueSet.setExpansion(expansion); + assertEquals(expansion, ValueSets.getExpansion(fhirContextR4, valueSet)); + } + + @Test + void testGetContainsInExpansion() { assertNull(ValueSets.getContainsInExpansion(fhirContextR4, null)); var expansion = new ValueSet.ValueSetExpansionComponent(); assertNull(ValueSets.getContainsInExpansion(fhirContextR4, expansion)); - assertNull(ValueSets.getCodesInCompose(fhirContextR4, new ValueSet())); + var contains = new ValueSet.ValueSetExpansionContainsComponent(); + expansion.addContains(contains); + assertEquals( + contains, + ValueSets.getContainsInExpansion(fhirContextR4, expansion).get(0)); + } + + @Test + void testGetContains() { + var contains = new ValueSet.ValueSetExpansionContainsComponent(); + var valueSet = new ValueSet().setExpansion(new ValueSet.ValueSetExpansionComponent().addContains(contains)); + assertEquals(contains, ValueSets.getContains(fhirContextR4, valueSet).get(0)); + } + + @Test + void testGetCodesInCompose() { + var code = "test"; + var display = "Test"; + var version = "1.0.0"; + var system = "http://fhir.test"; + var valueSet = new ValueSet().setCompose(new ValueSet.ValueSetComposeComponent()); + assertNull(ValueSets.getCodesInCompose(fhirContextR4, valueSet)); + valueSet.getCompose() + .addInclude(new ValueSet.ConceptSetComponent() + .setVersion(version) + .setSystem(system) + .addConcept(new ValueSet.ConceptReferenceComponent() + .setCode(code) + .setDisplay(display))); + var actualCode = ValueSets.getCodesInCompose(fhirContextR4, valueSet).get(0); + assertEquals(code, actualCode.getCode()); + assertEquals(display, actualCode.getDisplay()); + assertEquals(version, actualCode.getVersion()); + assertEquals(system, actualCode.getSystem()); + } + + @Test + void testGetCodesInContains() { + var code = "test"; + var display = "Test"; + var version = "1.0.0"; + var system = "http://fhir.test"; + assertNull(ValueSets.getCodesInContains(fhirContextR4, null)); + var contains = new ValueSet.ValueSetExpansionContainsComponent() + .setCode(code) + .setDisplay(display) + .setVersion(version) + .setSystem(system); + var actualCode = ValueSets.getCodesInContains(fhirContextR4, Collections.singletonList(contains)) + .get(0); + assertEquals(code, actualCode.getCode()); + assertEquals(display, actualCode.getDisplay()); + assertEquals(version, actualCode.getVersion()); + assertEquals(system, actualCode.getSystem()); + } + + @Test + void testGetCodesInExpansion() { + var code = "test"; + var display = "Test"; + var version = "1.0.0"; + var system = "http://fhir.test"; + var expansion = new ValueSet.ValueSetExpansionComponent() + .addContains(new ValueSet.ValueSetExpansionContainsComponent() + .setCode(code) + .setDisplay(display) + .setVersion(version) + .setSystem(system)); + var valueSet = new ValueSet().setExpansion(expansion); + var expansionCode = + ValueSets.getCodesInExpansion(fhirContextR4, expansion).get(0); + assertEquals(code, expansionCode.getCode()); + assertEquals(display, expansionCode.getDisplay()); + assertEquals(version, expansionCode.getVersion()); + assertEquals(system, expansionCode.getSystem()); + var actualCode = ValueSets.getCodesInExpansion(fhirContextR4, valueSet).get(0); + assertEquals(code, actualCode.getCode()); + assertEquals(display, actualCode.getDisplay()); + assertEquals(version, actualCode.getVersion()); + assertEquals(system, actualCode.getSystem()); } @Test