Skip to content

Commit

Permalink
added FHIR evaluate method implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
luis-pabon-tf committed Mar 18, 2024
1 parent 81b8295 commit 8246674
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ import gov.hhs.cdc.trustedintermediary.context.TestApplicationContext
import gov.hhs.cdc.trustedintermediary.wrappers.HapiFhirEngine
import org.hl7.fhir.instance.model.api.IBaseResource
import org.hl7.fhir.r4.model.Bundle
import org.hl7.fhir.r4.model.DiagnosticReport
import org.hl7.fhir.r4.model.ServiceRequest
import org.hl7.fhir.r4.utils.FHIRLexer
import spock.lang.Specification

// @todo build actual tests this is a skeleton
class HapiFhirEngineImplementationTest extends Specification {
HapiFhirEngine engine
Bundle bundle
DiagnosticReport diaReport
ServiceRequest servRequest

def setup() {
TestApplicationContext.reset()
Expand All @@ -21,6 +25,19 @@ class HapiFhirEngineImplementationTest extends Specification {

bundle = new Bundle()
bundle.id = "abc123"

diaReport = new DiagnosticReport()
diaReport.id = "ghi789"
servRequest = new ServiceRequest()
servRequest.id = "def456"

def entry1 = new Bundle.BundleEntryComponent()
entry1.resource = diaReport
bundle.addEntry(entry1)

def entry2 = new Bundle.BundleEntryComponent()
entry2.resource = servRequest
bundle.addEntry(entry2)
}

def cleanup() {
Expand Down Expand Up @@ -61,7 +78,7 @@ class HapiFhirEngineImplementationTest extends Specification {

def "parsePath throws FHIRLexerException on fake method"() {
given:
def path = "Bundle.entry.resource.BADMETHOD(MessageHeader)"
def path = "Bundle.entry.resource.BadMethod(MessageHeader)"

when:
engine.parsePath(path)
Expand Down Expand Up @@ -124,4 +141,29 @@ class HapiFhirEngineImplementationTest extends Specification {
then:
result == false
}

def "evaluateCondition throws FHIRLexerException on fake method"() {
given:
def path = "Bundle.entry[0].resource.BadMethod('blah')"

when:
engine.evaluateCondition(bundle as IBaseResource, path)

then:
thrown(FHIRLexer.FHIRLexerException)
}

def "evaluate returns the correct resource if it's available"() {
given:
def path = "Bundle.entry.resource.ofType(DiagnosticReport)[0]"

when:
def result = engine.evaluate(bundle as IBaseResource, path)

then:
result.size() == 1
result.first().class == DiagnosticReport.class
(result.first() as DiagnosticReport).id == diaReport.id
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,25 @@ public ExpressionNode parsePath(String fhirPath) {
return pathEngine.parse(fhirPath);
}

public List<Base> evaluate(IBaseResource root, String expression) {

Check notice

Code scanning / CodeQL

Missing Override annotation Note library

This method overrides
HapiFhirEngine.evaluate
; it is advisable to add an Override annotation.
var expressionNode = parsePath(expression);
var base = (Base) root;

List<Base> retVal;
if (expressionNode == null) {
retVal = new ArrayList<>();
} else {
try {
retVal = pathEngine.evaluate(base, expressionNode);
} catch (Exception e) {
// log exception
retVal = new ArrayList<>();
}
}

return retVal;
}

public Boolean evaluateCondition(IBaseResource root, String expression) throws Exception {

Check notice

Code scanning / CodeQL

Missing Override annotation Note library

This method overrides
HapiFhirEngine.evaluateCondition
; it is advisable to add an Override annotation.
var expressionNode = parsePath(expression);
var base = (Base) root;
Expand All @@ -51,7 +70,8 @@ public Boolean evaluateCondition(IBaseResource root, String expression) throws E
// for the purposes of the evaluating a schema condition that is the same as being false
retVal = false;
} else {
throw new Exception("add here");
throw new Exception(
"add here"); // @todo consider defaulting to false and just logging the error
// throw new FhirParseException("FHIR Path expression did not evaluate to a boolean
// type: $expression", new Exception());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package gov.hhs.cdc.trustedintermediary.wrappers;

import java.util.List;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.r4.context.IWorkerContext;
import org.hl7.fhir.r4.model.Base;
import org.hl7.fhir.r4.model.ExpressionNode;
import org.hl7.fhir.r4.utils.FHIRPathEngine;

public interface HapiFhirEngine {
ExpressionNode parsePath(String fhirPath);

Boolean evaluateCondition(IBaseResource resource, String expression) throws Exception;
List<Base> evaluate(IBaseResource root, String expression);

Boolean evaluateCondition(IBaseResource root, String expression) throws Exception;

IWorkerContext getContext();

Expand Down

0 comments on commit 8246674

Please sign in to comment.