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

move path validator #14

Merged
merged 1 commit into from
Feb 4, 2024
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
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -47,11 +50,38 @@ 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();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

}
Original file line number Diff line number Diff line change
@@ -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<String> 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");
}
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}

This file was deleted.

1 change: 1 addition & 0 deletions src/test/resources/resourceHelper.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Loading