diff --git a/src/main/java/gov/hhs/aspr/ms/util/resourcehelper/ResourceError.java b/src/main/java/gov/hhs/aspr/ms/util/resourcehelper/ResourceError.java new file mode 100644 index 0000000..686d170 --- /dev/null +++ b/src/main/java/gov/hhs/aspr/ms/util/resourcehelper/ResourceError.java @@ -0,0 +1,18 @@ +package gov.hhs.aspr.ms.util.resourcehelper; + +import gov.hhs.aspr.ms.util.errors.ContractError; + +public enum ResourceError implements ContractError { + UNKNOWN_FILE("Provided file does not exist"); + + private final String description; + + private ResourceError(final String description) { + this.description = description; + } + + @Override + public String getDescription() { + return description; + } +} diff --git a/src/main/java/gov/hhs/aspr/ms/util/resourcehelper/TestResourceHelper.java b/src/main/java/gov/hhs/aspr/ms/util/resourcehelper/ResourceHelper.java similarity index 55% rename from src/main/java/gov/hhs/aspr/ms/util/resourcehelper/TestResourceHelper.java rename to src/main/java/gov/hhs/aspr/ms/util/resourcehelper/ResourceHelper.java index 254816e..1757f7b 100644 --- a/src/main/java/gov/hhs/aspr/ms/util/resourcehelper/TestResourceHelper.java +++ b/src/main/java/gov/hhs/aspr/ms/util/resourcehelper/ResourceHelper.java @@ -5,11 +5,14 @@ import java.net.URI; import java.net.URISyntaxException; import java.net.URL; +import java.nio.file.Files; import java.nio.file.Path; -public class TestResourceHelper { +import gov.hhs.aspr.ms.util.errors.ContractException; - private TestResourceHelper() { +public class ResourceHelper { + + private ResourceHelper() { } public static Path getResourceDir(Class classRef) { @@ -47,6 +50,32 @@ public static void createOutputFile(Path filePath, String fileName) { createFile(isAfile); } + public static Path validatePath(String path, boolean isOutput) { + Path maybePath = Path.of(path); + File maybeFile = maybePath.toFile(); + + boolean isDirectory = maybeFile.isDirectory(); + boolean isFile = maybeFile.isFile(); + + // if the given string corresponds to a file that exists, return path + if (isFile) { + return maybePath; + } + + // if file does not exist, ensure the path is not a directory and that the + // parent directory of the outputFile exists. + if (isOutput && !isDirectory) { + Path parentPath = maybePath.getParent(); + + if (Files.exists(parentPath)) { + return maybePath; + } + } + + // otherwise throw an exception + throw new ContractException(ResourceError.UNKNOWN_FILE, path); + } + protected static void createFile(File file) { try { file.createNewFile(); @@ -54,4 +83,5 @@ protected static void createFile(File file) { throw new RuntimeException(e); } } + } diff --git a/src/test/java/gov/hhs/aspr/ms/util/resourcehelper/AT_ResourceError.java b/src/test/java/gov/hhs/aspr/ms/util/resourcehelper/AT_ResourceError.java new file mode 100644 index 0000000..170cbee --- /dev/null +++ b/src/test/java/gov/hhs/aspr/ms/util/resourcehelper/AT_ResourceError.java @@ -0,0 +1,27 @@ +package gov.hhs.aspr.ms.util.resourcehelper; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.LinkedHashSet; +import java.util.Set; + +import org.junit.jupiter.api.Test; + +import gov.hhs.aspr.ms.util.annotations.UnitTestMethod; + +public class AT_ResourceError { + @Test + @UnitTestMethod(target = ResourceError.class, name = "getDescription", args = {}) + public void testGetDescription() { + // show that each description is a unique, non-null and non-empty string + Set descriptions = new LinkedHashSet<>(); + for (ResourceError personBlockError : ResourceError.values()) { + String description = personBlockError.getDescription(); + assertNotNull(description, "null description for " + personBlockError); + assertTrue(description.length() > 0, "empty string for " + personBlockError); + boolean unique = descriptions.add(description); + assertTrue(unique, "description for " + personBlockError + " is not unique"); + } + } +} diff --git a/src/test/java/gov/hhs/aspr/ms/util/resourcehelper/AT_ResourceHelper.java b/src/test/java/gov/hhs/aspr/ms/util/resourcehelper/AT_ResourceHelper.java new file mode 100644 index 0000000..5354b7f --- /dev/null +++ b/src/test/java/gov/hhs/aspr/ms/util/resourcehelper/AT_ResourceHelper.java @@ -0,0 +1,144 @@ +package gov.hhs.aspr.ms.util.resourcehelper; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.File; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URISyntaxException; +import java.net.URL; +import java.nio.file.Path; + +import org.junit.jupiter.api.Test; + +import gov.hhs.aspr.ms.util.annotations.UnitTestMethod; +import gov.hhs.aspr.ms.util.errors.ContractException; + +public class AT_ResourceHelper { + + @Test + @UnitTestMethod(target = ResourceHelper.class, name = "getResourceDir", args = { Class.class }) + public void testGetResourceDir() throws MalformedURLException { + Path path = ResourceHelper.getResourceDir(this.getClass()); + + assertNotNull(path); + assertTrue(path.toFile().exists()); + assertTrue(path.resolve("resourceHelper.json").toFile().exists()); + + // preconditions + // bad url syntax + URL url = new URL("file:${my.properties}"); + + RuntimeException runtimeException = assertThrows(RuntimeException.class, () -> { + ResourceHelper.getURI(url); + }); + + assertTrue(runtimeException.getCause() instanceof URISyntaxException); + } + + @Test + @UnitTestMethod(target = ResourceHelper.class, name = "makeOutputDir", args = { Path.class }) + public void testMakeTestOutputDir() { + Path path = ResourceHelper.getResourceDir(this.getClass()).resolve("additional-folder"); + + Path newPath = ResourceHelper.makeOutputDir(path); + + assertNotNull(newPath); + assertTrue(newPath.toFile().exists()); + } + + @Test + @UnitTestMethod(target = ResourceHelper.class, name = "makeOutputDir", args = { Path.class, String.class }) + public void testMakeTestOutputDir_SubDir() { + Path path = ResourceHelper.getResourceDir(this.getClass()).resolve("additional-folder"); + + Path newPath = ResourceHelper.makeOutputDir(path, "subFolder"); + + assertNotNull(newPath); + assertTrue(newPath.toFile().exists()); + } + + private class IOExceptionFile extends File { + + /** + * + */ + private static final long serialVersionUID = 1L; + + public IOExceptionFile(String pathname) { + super(pathname); + } + + public IOExceptionFile(File file) { + super(file, pathSeparator); + } + + @Override + public boolean createNewFile() throws IOException { + + throw new IOException(); + } + + } + + @Test + @UnitTestMethod(target = ResourceHelper.class, name = "createOutputFile", args = { Path.class, + String.class }) + public void testCreateTestOutputFile() throws IOException { + Path path = ResourceHelper.getResourceDir(this.getClass()); + + Path newPath = ResourceHelper.makeOutputDir(path); + + String fileName = "foo.txt"; + ResourceHelper.createOutputFile(newPath, fileName); + + assertTrue(newPath.resolve(fileName).toFile().exists()); + + // test for delete/recreat file if it exists + ResourceHelper.createOutputFile(newPath, fileName); + + // preconditions + // force an IOException + RuntimeException runtimeException = assertThrows(RuntimeException.class, () -> { + IOExceptionFile testFile = new IOExceptionFile(newPath.resolve(fileName).toFile()); + ResourceHelper.createFile(testFile); + }); + + assertTrue(runtimeException.getCause() instanceof IOException); + } + + @Test + @UnitTestMethod(target = ResourceHelper.class, name = "validatePath", args = { String.class, boolean.class }) + public void testValidatePath() { + Path resourceDir = ResourceHelper.getResourceDir(getClass()); + // path is a file that is not output and exists + String path = resourceDir.resolve("resourceHelper.json").toAbsolutePath().toString(); + Path expectedPath = resourceDir.resolve("resourceHelper.json").toAbsolutePath(); + assertEquals(expectedPath, ResourceHelper.validatePath(path, false)); + + // file is an output file that does not exist, but the directory the file + // resides in exists + ResourceHelper.makeOutputDir(resourceDir, "testOutput"); + path = resourceDir.resolve("testOutput").resolve("resourceHelper.json").toAbsolutePath().toString(); + expectedPath = resourceDir.resolve("testOutput").resolve("resourceHelper.json").toAbsolutePath(); + assertEquals(expectedPath, ResourceHelper.validatePath(path, true)); + + // preconditions + // File does not exist and it is not an outputFile + ContractException contractException = assertThrows(ContractException.class, () -> { + ResourceHelper.validatePath("testOutput", false); + }); + + assertEquals(ResourceError.UNKNOWN_FILE, contractException.getErrorType()); + + // is output but parent directory does not exist + contractException = assertThrows(ContractException.class, () -> { + ResourceHelper.validatePath(resourceDir.resolve("nonExistantDir").resolve("file.json").toString(), true); + }); + + assertEquals(ResourceError.UNKNOWN_FILE, contractException.getErrorType()); + } +} diff --git a/src/test/java/gov/hhs/aspr/ms/util/resourcehelper/AT_TestResourceHelper.java b/src/test/java/gov/hhs/aspr/ms/util/resourcehelper/AT_TestResourceHelper.java deleted file mode 100644 index 0a707e9..0000000 --- a/src/test/java/gov/hhs/aspr/ms/util/resourcehelper/AT_TestResourceHelper.java +++ /dev/null @@ -1,109 +0,0 @@ -package gov.hhs.aspr.ms.util.resourcehelper; - -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.io.File; -import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URISyntaxException; -import java.net.URL; -import java.nio.file.Path; - -import org.junit.jupiter.api.Test; - -import gov.hhs.aspr.ms.util.annotations.UnitTestMethod; - -public class AT_TestResourceHelper { - - @Test - @UnitTestMethod(target = TestResourceHelper.class, name = "getResourceDir", args = { Class.class }) - public void testGetResourceDir() throws MalformedURLException { - Path path = TestResourceHelper.getResourceDir(this.getClass()); - - assertNotNull(path); - assertTrue(path.toFile().exists()); - - // preconditions - // bad url syntax - URL url = new URL("file:${my.properties}"); - - RuntimeException runtimeException = assertThrows(RuntimeException.class, () -> { - TestResourceHelper.getURI(url); - }); - - assertTrue(runtimeException.getCause() instanceof URISyntaxException); - } - - @Test - @UnitTestMethod(target = TestResourceHelper.class, name = "makeOutputDir", args = { Path.class }) - public void testMakeTestOutputDir() { - Path path = TestResourceHelper.getResourceDir(this.getClass()).resolve("additional-folder"); - - Path newPath = TestResourceHelper.makeOutputDir(path); - - assertNotNull(newPath); - assertTrue(newPath.toFile().exists()); - } - - @Test - @UnitTestMethod(target = TestResourceHelper.class, name = "makeOutputDir", args = { Path.class, String.class }) - public void testMakeTestOutputDir_SubDir() { - Path path = TestResourceHelper.getResourceDir(this.getClass()).resolve("additional-folder"); - - Path newPath = TestResourceHelper.makeOutputDir(path, "subFolder"); - - assertNotNull(newPath); - assertTrue(newPath.toFile().exists()); - } - - private class IOExceptionFile extends File { - - /** - * - */ - private static final long serialVersionUID = 1L; - - public IOExceptionFile(String pathname) { - super(pathname); - } - - public IOExceptionFile(File file) { - super(file, pathSeparator); - } - - @Override - public boolean createNewFile() throws IOException { - - throw new IOException(); - } - - } - - @Test - @UnitTestMethod(target = TestResourceHelper.class, name = "createOutputFile", args = { Path.class, - String.class }) - public void testCreateTestOutputFile() throws IOException { - Path path = TestResourceHelper.getResourceDir(this.getClass()); - - Path newPath = TestResourceHelper.makeOutputDir(path); - - String fileName = "foo.txt"; - TestResourceHelper.createOutputFile(newPath, fileName); - - assertTrue(newPath.resolve(fileName).toFile().exists()); - - // test for delete/recreat file if it exists - TestResourceHelper.createOutputFile(newPath, fileName); - - // preconditions - // force an IOException - RuntimeException runtimeException = assertThrows(RuntimeException.class, () -> { - IOExceptionFile testFile = new IOExceptionFile(newPath.resolve(fileName).toFile()); - TestResourceHelper.createFile(testFile); - }); - - assertTrue(runtimeException.getCause() instanceof IOException); - } -} diff --git a/src/test/resources/resourceHelper.json b/src/test/resources/resourceHelper.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/src/test/resources/resourceHelper.json @@ -0,0 +1 @@ +{} \ No newline at end of file