From a7aaea4227b6b804930dcbc83408508336e69199 Mon Sep 17 00:00:00 2001 From: Evan Chicoine Date: Wed, 13 Sep 2023 12:33:13 -0400 Subject: [PATCH] 80% code coverage on ExtractMatBundleOperation class --- .../operation/ExtractMatBundleOperation.java | 22 ++- .../ExtractMatBundleOperationTest.java | 163 ++++++++++++++++++ 2 files changed, 179 insertions(+), 6 deletions(-) create mode 100644 tooling/src/test/java/org/opencds/cqf/tooling/operation/ExtractMatBundleOperationTest.java diff --git a/tooling/src/main/java/org/opencds/cqf/tooling/operation/ExtractMatBundleOperation.java b/tooling/src/main/java/org/opencds/cqf/tooling/operation/ExtractMatBundleOperation.java index f23a20fa4..6c347efc5 100644 --- a/tooling/src/main/java/org/opencds/cqf/tooling/operation/ExtractMatBundleOperation.java +++ b/tooling/src/main/java/org/opencds/cqf/tooling/operation/ExtractMatBundleOperation.java @@ -22,9 +22,11 @@ public class ExtractMatBundleOperation extends Operation { public static final String ERROR_BUNDLE_FILE_IS_REQUIRED = "The path to a bundle file is required"; public static final String ERROR_DIR_IS_NOT_A_DIRECTORY = "The path specified with -dir is not a directory."; public static final String ERROR_DIR_IS_EMPTY = "The path specified with -dir is empty."; + public static final String ERROR_BUNDLE_LOCATION_NONEXISTENT = "The path specified for the bundle doesn't exist on your system."; public static final String INFO_EXTRACTION_SUCCESSFUL = "Extraction completed successfully"; public static final String VERSION_R4 = "r4"; public static final String VERSION_STU3 = "stu3"; + public static final String ERROR_NOT_JSON_OR_XML = "The path to a bundle file of type json or xml is required."; @Override @@ -82,18 +84,21 @@ public void execute(String[] args) { } }//end arg loop - //determine location is provided by user, if not, throw arugment exception: + //determine location is provided by user, if not, throw argument exception: if (inputLocation == null) { throw new IllegalArgumentException(ERROR_BUNDLE_FILE_IS_REQUIRED); } else { + File bundleFile = new File(inputLocation); + if (!bundleFile.exists()){ + throw new IllegalArgumentException(ERROR_BUNDLE_LOCATION_NONEXISTENT); + } if (directoryFlagPresent) { //ensure the input is a directory before trying to return a list of its files: - if (!new File(inputLocation).isDirectory()) { + if (!bundleFile.isDirectory()) { throw new IllegalArgumentException(ERROR_DIR_IS_NOT_A_DIRECTORY); } } else { - File bundleFile = new File(inputLocation); - if (!bundleFile.exists() || bundleFile.isDirectory()) { + if (bundleFile.isDirectory()) { throw new IllegalArgumentException(ERROR_BUNDLE_FILE_IS_REQUIRED); } } @@ -117,7 +122,7 @@ public void execute(String[] args) { } - private void processSingleFile(File bundleFile, String version, boolean suppressNarrative) { + void processSingleFile(File bundleFile, String version, boolean suppressNarrative) { String inputFileLocation = bundleFile.getAbsolutePath(); LogUtils.info(String.format("Extracting MAT bundle from %s", inputFileLocation)); @@ -169,7 +174,7 @@ private void processSingleFile(File bundleFile, String version, boolean suppress } } else { - throw new IllegalArgumentException("The path to a bundle file of type json or xml is required"); + throw new IllegalArgumentException(ERROR_NOT_JSON_OR_XML); } //sometimes tests leave library or measure files behind so we want to make sure we only iterate over bundle files: @@ -180,6 +185,11 @@ private void processSingleFile(File bundleFile, String version, boolean suppress //call the Bundle utilities to extract the bundle String outputDir = bundleFile.getAbsoluteFile().getParent(); + //ensure output path assigned by user is utilized: + if (!getOutputPath().isEmpty()){ + outputDir = getOutputPath(); + } + BundleUtils.extractResources(bundle, encoding, outputDir, suppressNarrative, version); //move and properly rename the files diff --git a/tooling/src/test/java/org/opencds/cqf/tooling/operation/ExtractMatBundleOperationTest.java b/tooling/src/test/java/org/opencds/cqf/tooling/operation/ExtractMatBundleOperationTest.java new file mode 100644 index 000000000..2cfb74879 --- /dev/null +++ b/tooling/src/test/java/org/opencds/cqf/tooling/operation/ExtractMatBundleOperationTest.java @@ -0,0 +1,163 @@ +package org.opencds.cqf.tooling.operation; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import java.io.File; +import java.io.IOException; +import java.net.URL; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +public class ExtractMatBundleOperationTest { + + private ExtractMatBundleOperation operation; + + @Rule + public TemporaryFolder tempFolder = new TemporaryFolder(); + + @Before + public void setUp() { + operation = new ExtractMatBundleOperation(); + } + + @Test + public void testExecuteWithMissingBundleFile() { + String[] args = { "-ExtractMatBundle" }; + + try { + operation.execute(args); + fail("Expected IllegalArgumentException was not thrown"); + } catch (IllegalArgumentException e) { + assertEquals("The path to a bundle file is required", e.getMessage()); + } + } + + @Test + public void testExecuteWithNonExistentBundleFile() { + String[] args = { "-ExtractMatBundle", "nonexistent.json" }; + + try { + operation.execute(args); + fail("Expected IllegalArgumentException was not thrown"); + } catch (IllegalArgumentException e) { + assertEquals("The path specified for the bundle doesn't exist on your system.", e.getMessage()); + } + } + + @Test + public void testExecuteWithNonExistentDirectory() throws IOException { + File nonexistentDir = tempFolder.newFile("nonexistent_directory"); + String[] args = { "-ExtractMatBundle", nonexistentDir.getAbsolutePath(), "-dir" }; + + try { + operation.execute(args); + fail("Expected IllegalArgumentException was not thrown"); + } catch (IllegalArgumentException e) { + assertEquals("The path specified with -dir is not a directory.", e.getMessage()); + } + } + + @Test + public void testExecuteWithEmptyDirectory() throws IOException { + File emptyDir = tempFolder.newFolder("emptyDir"); + String[] args = { "-ExtractMatBundle", emptyDir.getAbsolutePath(), "-dir" }; + + try { + operation.execute(args); + fail("Expected IllegalArgumentException was not thrown"); + } catch (IllegalArgumentException e) { + assertEquals("The path specified with -dir is empty.", e.getMessage()); + } + } + + @Test + public void testExecuteWithNonJsonFile() throws IOException { + File nonJsonFile = tempFolder.newFile("file.non_json"); + String[] args = { "-ExtractMatBundle", nonJsonFile.getAbsolutePath() }; + + try { + operation.execute(args); + fail("Expected IllegalArgumentException was not thrown"); + } catch (IllegalArgumentException e) { + assertEquals("The path to a bundle file of type json or xml is required.", e.getMessage()); + } + } + + @Test + public void testExecuteWithNonXmlFile() throws IOException { + File nonXmlFile = tempFolder.newFile("file.non_xml"); + String[] args = { "-ExtractMatBundle", nonXmlFile.getAbsolutePath() }; + + try { + operation.execute(args); + fail("Expected IllegalArgumentException was not thrown"); + } catch (IllegalArgumentException e) { + assertEquals("The path to a bundle file of type json or xml is required.", e.getMessage()); + } + } + @Test + public void testExecuteWithFileAndDirArg() throws IOException { + File nonXmlFile = tempFolder.newFile("file.xml"); + String[] args = { "-ExtractMatBundle", nonXmlFile.getAbsolutePath(), "-dir" }; + + try { + operation.execute(args); + fail("Expected IllegalArgumentException was not thrown"); + } catch (IllegalArgumentException e) { + assertEquals("The path specified with -dir is not a directory.", e.getMessage()); + } + } + + @Test + public void testExecuteWithNonsense() throws IOException { + File nonXmlFile = tempFolder.newFile("file.xml"); + String[] args = { "-ExtractMatBundle", nonXmlFile.getAbsolutePath(), "-nonsense" }; + + try { + operation.execute(args); + fail("Expected IllegalArgumentException was not thrown"); + } catch (IllegalArgumentException e) { + assertEquals("Invalid argument: -nonsense", e.getMessage()); + } + } + + + @Test + public void TestExtractMatBundleWithDirectory() throws IOException { + ExtractMatBundleOperation o = new ExtractMatBundleOperation(); + // Use ClassLoader to load the resource + ClassLoader classLoader = getClass().getClassLoader(); + String resourcePath = "org/opencds/cqf/tooling/utilities/ecqm-content-r4-2021/bundles_small/"; + URL resourceUrl = classLoader.getResource(resourcePath); + + if (resourceUrl == null) { + throw new IllegalArgumentException("Resource not found: " + resourcePath); + } + + + File emptyDir = tempFolder.newFolder("bundles"); + // Create a new thread to execute the operation + Thread executionThread = new Thread(new Runnable() { + public void run() { + o.execute(new String[] { "-ExtractMATBundle", resourceUrl.getFile(), "-dir", "-op=" + emptyDir.getAbsolutePath() }); + } + }); + + // Start the thread + executionThread.start(); + + // Wait for the thread to finish + try { + executionThread.join(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + + File[] files = emptyDir.listFiles(); + + assertEquals(16, emptyDir.listFiles().length); + } +}