Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

80% code coverage on ExtractMatBundleOperation class #2

Merged
merged 1 commit into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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));
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading