Skip to content

Commit

Permalink
Resolve issue with Dstu3 Library identifier (#446)
Browse files Browse the repository at this point in the history
* Allow for Dstu3 references which may be just resourceType/id

* Add exception test

* spotless
  • Loading branch information
barhodes committed Apr 8, 2024
1 parent 3c4c2d3 commit 75de12e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import ca.uhn.fhir.fhirpath.IFhirPath;
import java.util.List;
import org.apache.commons.lang3.tuple.Pair;
import org.hl7.elm.r1.VersionedIdentifier;
import org.opencds.cqf.fhir.cql.engine.parameters.CqlParameterDefinition;
import org.opencds.cqf.fhir.utility.FhirPathCache;
import org.slf4j.Logger;
Expand Down Expand Up @@ -54,7 +53,7 @@ private void constructIncludes(StringBuilder sb, List<Pair<String, String>> libr

if (libraries != null) {
for (Pair<String, String> library : libraries) {
VersionedIdentifier vi = getVersionedIdentifier(library.getLeft());
var vi = VersionedIdentifiers.forUrl(library.getLeft());
sb.append(String.format("include \"%s\"", vi.getId()));
if (vi.getVersion() != null) {
sb.append(String.format(" version '%s'", vi.getVersion()));
Expand Down Expand Up @@ -100,32 +99,4 @@ private void constructUsings(StringBuilder sb) {
private void constructHeader(StringBuilder sb) {
sb.append(String.format("library expression version '1.0.0'%n%n"));
}

protected VersionedIdentifier getVersionedIdentifier(String url) {
if (!url.contains("/Library/")) {
throw new IllegalArgumentException(
"Invalid resource type for determining library version identifier: Library");
}
String[] urlSplit = url.split("/Library/");
if (urlSplit.length != 2) {
throw new IllegalArgumentException(
"Invalid url, Library.url SHALL be <CQL namespace url>/Library/<CQL library name>");
}

// TODO: Use the namespace manager here to do the mapping?
// String cqlNamespaceUrl = urlSplit[0];

String cqlName = urlSplit[1];
VersionedIdentifier versionedIdentifier = new VersionedIdentifier();
if (cqlName.contains("|")) {
String[] nameVersion = cqlName.split("\\|");
String name = nameVersion[0];
String version = nameVersion[1];
versionedIdentifier.setId(name);
versionedIdentifier.setVersion(version);
} else {
versionedIdentifier.setId(cqlName);
}
return versionedIdentifier;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@ private VersionedIdentifiers() {
}

public static VersionedIdentifier forUrl(String url) {
if (!url.contains("/Library/")) {
if (!url.contains("/Library/") && !url.startsWith("Library/")) {
throw new IllegalArgumentException(
"Invalid resource type for determining library version identifier: Library");
}
String[] urlSplit = url.split("/Library/");
if (urlSplit.length != 2) {

String[] urlSplit = url.split("Library/");
if (urlSplit.length > 2) {
throw new IllegalArgumentException(
"Invalid url, Library.url SHALL be <CQL namespace url>/Library/<CQL library name>");
}

String cqlName = urlSplit[1];
String cqlName = urlSplit.length == 1 ? urlSplit[0] : urlSplit[1];
VersionedIdentifier versionedIdentifier = new VersionedIdentifier();
if (cqlName.contains("|")) {
String[] nameVersion = cqlName.split("\\|");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.opencds.cqf.fhir.cql;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.Test;

class VersionedIdentifiersTests {
@Test
void testExceptions() {
assertThrows(
IllegalArgumentException.class,
() -> VersionedIdentifiers.forUrl("http:https://fhir.org/guides/cdc/opioid-cds/Binary/HelloWorld"));

assertThrows(
IllegalArgumentException.class,
() -> VersionedIdentifiers.forUrl(
"http:https://fhir.org/guides/cdc/opioid-cds/Library/HelloWorld/Library/HelloWorld"));
}

@Test
void testUrl() {
var url = "http:https://fhir.org/guides/cdc/opioid-cds/Library/HelloWorld";
var id = VersionedIdentifiers.forUrl(url);
assertEquals("HelloWorld", id.getId());
assertNull(id.getVersion());
}

@Test
void testCanonical() {
var url = "http:https://fhir.org/guides/cdc/opioid-cds/Library/HelloWorld|1.0.0";
var id = VersionedIdentifiers.forUrl(url);
assertEquals("HelloWorld", id.getId());
assertEquals("1.0.0", id.getVersion());
}

@Test
void testReference() {
var reference = "Library/HelloWorld";
var id = VersionedIdentifiers.forUrl(reference);
assertEquals("HelloWorld", id.getId());
assertNull(id.getVersion());
}
}

0 comments on commit 75de12e

Please sign in to comment.