Skip to content

Commit

Permalink
Add PathEvaluatorLibrary test
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-karuri committed Oct 23, 2020
1 parent 372cf2f commit be72da9
Show file tree
Hide file tree
Showing 3 changed files with 2,565 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,14 @@ public FHIRPathStringValue evaluateStringExpression(DomainResource resource, Str
}

/**
* Evaluates a String expression on a bundle and returns a String result or null
* Evaluates a FHIR Path {@param expression} on a {@param bundle} and returns a String result
* or null if the query fails or doesn't have results
*
* @param bundle
* @param expression
* @return
*/

public String extractStringFromBundle(Bundle bundle, String expression) {
try {
Iterator<FHIRPathNode> iterator = fhirPathEvaluator.evaluate(bundle, expression).iterator();
Expand All @@ -150,7 +152,8 @@ public String extractStringFromBundle(Bundle bundle, String expression) {


/**
* Evaluates a String expression on a bundle and returns a List of String results
* Evaluates a FHIR Path {@param expression} on a {@param bundle} and returns a List of String results
* or null if the query fails or doesn't have results
*
* @param bundle
* @param expression
Expand All @@ -168,18 +171,25 @@ public List<String> extractStringsFromBundle(Bundle bundle, String expression) {
}
return strs;
}


/**
* Returns a {@link FHIRPathElementNode}'s value as a String
* @param fhirPathElementNode
* @return
*/
private String convertElementNodeValToStr(FHIRPathElementNode fhirPathElementNode) {
return fhirPathElementNode.getValue().asStringValue().string();
}

/**
* Evaluates a String expression on a bundle and returns a Resource result or null
* Evaluates a FHIR Path {@param expression} on a {@param bundle} and returns a List of {@link Element}s
* or null if the query fails or doesn't have results
*
* @param bundle
* @param expression
* @return
*/

public List<Element> extractElementsFromBundle(Bundle bundle, String expression) {
List<Element> elements = new ArrayList<>();
try {
Expand All @@ -195,12 +205,14 @@ public List<Element> extractElementsFromBundle(Bundle bundle, String expression)
}

/**
* Evaluates a String expression on a bundle and returns a Resource result or null
* Evaluates a FHIR Path {@param expression} on a {@param bundle} and returns a {@link Resource} result
* or null if the query fails or doesn't have results
*
* @param bundle
* @param expression
* @return
*/

public Resource extractResourceFromBundle(Bundle bundle, String expression) {
try {
Iterator<FHIRPathNode> iterator = fhirPathEvaluator.evaluate(bundle, expression).iterator();
Expand All @@ -210,20 +222,4 @@ public Resource extractResourceFromBundle(Bundle bundle, String expression) {
return null;
}
}

public void setLocationProvider(LocationProvider locationProvider) {
this.locationProvider = locationProvider;
}

public void setClientProvider(ClientProvider clientProvider) {
this.clientProvider = clientProvider;
}

public void setTaskProvider(TaskProvider taskProvider) {
this.taskProvider = taskProvider;
}

public void setEventProvider(EventProvider eventProvider) {
this.eventProvider = eventProvider;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.InputStream;
import java.nio.file.Paths;
import java.util.Calendar;

import com.ibm.fhir.model.format.Format;
import com.ibm.fhir.model.parser.FHIRParser;
import com.ibm.fhir.model.resource.Bundle;
import org.junit.Before;
import org.junit.Test;

Expand All @@ -30,6 +36,8 @@ public class PathEvaluatorLibraryTest {
private PathEvaluatorLibrary pathEvaluatorLibrary;

private Patient patient;

private Bundle deviceDefinitionBundle;

@Before
public void startUp() {
Expand Down Expand Up @@ -109,5 +117,26 @@ public void testEvaluateActionExpressions() {
node = pathEvaluatorLibrary.evaluateStringExpression(patient, "'Cancelled'");
assertEquals("Cancelled", node.string());
}


@Test
public void testExtractStringFromBundleShouldReturnCorrectString() throws Exception {
String str = pathEvaluatorLibrary.extractStringFromBundle(getDeviceDefinitionBundle(), "$this.entry.resource.where(identifier.where(value='d3fdac0e-061e-b068-2bed-5a95e803636f')).capability.where(type.where(text='instructions')).description.text");
assertEquals("Collect blood sample", str);
}

private Bundle getDeviceDefinitionBundle() throws Exception {
if (deviceDefinitionBundle == null) {
InputStream stream = getClass().getClassLoader().getResourceAsStream("DeviceDefinition.json");
deviceDefinitionBundle = FHIRParser.parser(Format.JSON).parse(stream);
}
return deviceDefinitionBundle;
}

public static String getTestFilePath() {
return getBasePackageFilePath() + "src/main/java/org/smartregister/pathevaluator" + File.separator;
}

public static String getBasePackageFilePath() {
return Paths.get(".").toAbsolutePath().normalize().toString() + File.separator;
}
}
Loading

0 comments on commit be72da9

Please sign in to comment.