Skip to content

Commit

Permalink
Test exceptions are caught for fhir queries
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-karuri committed Nov 16, 2020
1 parent 7b362c2 commit 6d20327
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ private String convertElementNodeValToStr(FHIRPathElementNode fhirPathElementNod
* @param expression
* @return
*/

public List<Element> extractElementsFromBundle(Bundle bundle, String expression) {
List<Element> elements = new ArrayList<>();
try {
Expand All @@ -199,7 +198,7 @@ public List<Element> extractElementsFromBundle(Bundle bundle, String expression)
}
} catch (FHIRPathException e) {
logger.log(Level.SEVERE, "Error executing expression " + expression, e);
return null;
return elements;
}
return elements;
}
Expand All @@ -212,7 +211,6 @@ public List<Element> extractElementsFromBundle(Bundle bundle, String expression)
* @param expression
* @return
*/

public Resource extractResourceFromBundle(Bundle bundle, String expression) {
try {
Iterator<FHIRPathNode> iterator = fhirPathEvaluator.evaluate(bundle, expression).iterator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.ibm.fhir.path.FHIRPathStringValue;
import com.ibm.fhir.path.exception.FHIRPathException;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.powermock.reflect.Whitebox;
Expand All @@ -30,6 +31,7 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;

/**
* @author Samuel Githengi created on 06/10/20
Expand Down Expand Up @@ -148,11 +150,31 @@ public void testExtractElementsFromBundleShouldGetAllRelevantElements() throws E
}

@Test
public void testextractResourceFromBundleShouldExtractCorrectResource() throws Exception {
public void testExtractResourceFromBundleShouldExtractCorrectResource() throws Exception {
verifyResourceIsExtracted("d3fdac0e-061e-b068-2bed-5a95e803636f");
verifyResourceIsExtracted("cf4443a1-f582-74ea-be89-ae53b5fd7bfe");
}

@Test
public void testExtractStringFromBundleShouldReturnNullWhenFHIRExceptionOccurs() {
Assert.assertNull(pathEvaluatorLibrary.extractStringFromBundle(mock(Bundle.class), "null=null"));
}

@Test
public void testExtractStringsFromBundleShouldReturnEmptyListWhenFHIRExceptionOccurs() {
Assert.assertEquals(0, pathEvaluatorLibrary.extractStringsFromBundle(mock(Bundle.class), "null=null").size());
}

@Test
public void testExtractElementsFromBundleShouldReturnEmptyListWhenFHIRExceptionOccurs() {
Assert.assertEquals(0, pathEvaluatorLibrary.extractElementsFromBundle(mock(Bundle.class), "null=null").size());
}

@Test
public void testExtractResourceFromBundleShouldThrowFHIRException() {
Assert.assertNull(pathEvaluatorLibrary.extractResourceFromBundle(mock(Bundle.class), "null=null"));
}

private void verifyCorrectStringIsExtracted(String resourceId, String expectedString) throws Exception {
String str = pathEvaluatorLibrary.extractStringFromBundle(getDeviceDefinitionBundle(), String.format("$this.entry.resource.where(identifier.where(value='%s')).capability.where(type.where(text='instructions')).description.text", resourceId));
assertEquals(expectedString, str);
Expand Down

0 comments on commit 6d20327

Please sign in to comment.