-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* archive project * list projects archive status * count number of archived projects * projectPaths to projectNames, .archived directory constant, rename archiveProject to archiveProjects, throw error, remove countArchivedProjects method, add --include-archived option to listProjects cmd * add description for --include-archived option * move line out of loop, remove unused constant, rename method to listProjectsInDirectory, check for /.archived/ * if project archived, set allowedActions to empty array * if project archived, set allowedActions to empty array * throw error if no projectNames
- Loading branch information
Showing
9 changed files
with
303 additions
and
21 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
src/main/java/edu/unc/lib/boxc/migration/cdm/ArchiveProjectsCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package edu.unc.lib.boxc.migration.cdm; | ||
|
||
import edu.unc.lib.boxc.migration.cdm.exceptions.InvalidProjectStateException; | ||
import edu.unc.lib.boxc.migration.cdm.services.ArchiveProjectsService; | ||
import org.slf4j.Logger; | ||
import picocli.CommandLine.Option; | ||
import picocli.CommandLine.ParentCommand; | ||
import picocli.CommandLine.Command; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.Callable; | ||
|
||
import static edu.unc.lib.boxc.migration.cdm.util.CLIConstants.outputLogger; | ||
import static org.slf4j.LoggerFactory.getLogger; | ||
|
||
@Command(name = "archive", | ||
description = {"Archive a project or list of projects. These projects will be moved to an 'archived' folder."}) | ||
public class ArchiveProjectsCommand implements Callable<Integer> { | ||
private static final Logger log = getLogger(ArchiveProjectsCommand.class); | ||
|
||
@ParentCommand | ||
private CLIMain parentCommand; | ||
|
||
@Option(names = { "-p", "--project-names" }, | ||
split = ",", | ||
description = {"Specify project or list of projects to be archived"}) | ||
private List<String> projectNames; | ||
|
||
private ArchiveProjectsService archiveProjectsService; | ||
|
||
@Override | ||
public Integer call() throws Exception { | ||
try { | ||
archiveProjectsService = new ArchiveProjectsService(); | ||
archiveProjectsService.archiveProjects(parentCommand.getWorkingDirectory(), projectNames); | ||
return 0; | ||
} catch(InvalidProjectStateException e) { | ||
outputLogger.info("Archiving project(s) failed: {}", e.getMessage()); | ||
return 1; | ||
} catch (Exception e) { | ||
log.error("Failed to archive project(s)", e); | ||
outputLogger.info("Archiving project(s) failed: {}", e.getMessage()); | ||
return 1; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/main/java/edu/unc/lib/boxc/migration/cdm/services/ArchiveProjectsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package edu.unc.lib.boxc.migration.cdm.services; | ||
|
||
import edu.unc.lib.boxc.migration.cdm.exceptions.InvalidProjectStateException; | ||
import org.apache.commons.io.FileUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
|
||
/** | ||
* Service for archiving chompb project(s) | ||
* @author krwong | ||
*/ | ||
public class ArchiveProjectsService { | ||
private static final Logger log = LoggerFactory.getLogger(ArchiveProjectsService.class); | ||
public static final String ARCHIVED = ".archived"; | ||
|
||
/** | ||
* Archive a list of projects | ||
*/ | ||
public void archiveProjects(Path currentDirectory, List<String> projectNames) throws IOException { | ||
if (projectNames == null || projectNames.isEmpty()) { | ||
throw new InvalidProjectStateException("Project names cannot be empty. " + | ||
"Please specify a valid project name(s)."); | ||
} | ||
|
||
Path archiveDirectory = currentDirectory.resolve(ARCHIVED); | ||
for (String projectName : projectNames) { | ||
Path projectDirectory = currentDirectory.resolve(projectName); | ||
|
||
if (Files.notExists(projectDirectory)) { | ||
throw new InvalidProjectStateException("Migration project " + projectName + " does not exist"); | ||
} | ||
|
||
if (Files.isDirectory(projectDirectory)) { | ||
FileUtils.moveDirectoryToDirectory(projectDirectory.toFile(), | ||
archiveDirectory.toFile(), true); | ||
} else { | ||
throw new InvalidProjectStateException("Migration project " + projectName + " is not a directory"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/test/java/edu/unc/lib/boxc/migration/cdm/ArchiveProjectsCommandIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package edu.unc.lib.boxc.migration.cdm; | ||
|
||
import edu.unc.lib.boxc.migration.cdm.services.ArchiveProjectsService; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.nio.file.Files; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
public class ArchiveProjectsCommandIT extends AbstractCommandIT { | ||
private static final String PROJECT_NAME_2 = "proj2"; | ||
|
||
@BeforeEach | ||
public void setup() throws Exception { | ||
initProjectAndHelper(); | ||
setupChompbConfig(); | ||
} | ||
|
||
@Test | ||
public void archiveProjectTest() throws Exception { | ||
String[] args = new String[] { | ||
"-w", String.valueOf(baseDir), | ||
"archive", | ||
"-p", project.getProjectName()}; | ||
executeExpectSuccess(args); | ||
|
||
assertTrue(Files.exists(tmpFolder.resolve(ArchiveProjectsService.ARCHIVED + "/" | ||
+ project.getProjectName()))); | ||
} | ||
|
||
@Test | ||
public void archiveInvalidProjectTest() throws Exception { | ||
String[] args = new String[] { | ||
"-w", String.valueOf(baseDir), | ||
"archive", | ||
"-p", PROJECT_NAME_2}; | ||
executeExpectFailure(args); | ||
|
||
assertOutputContains("Migration project " + PROJECT_NAME_2 + " does not exist"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
src/test/java/edu/unc/lib/boxc/migration/cdm/services/ArchiveProjectsServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package edu.unc.lib.boxc.migration.cdm.services; | ||
|
||
import edu.unc.lib.boxc.migration.cdm.model.MigrationProject; | ||
import edu.unc.lib.boxc.migration.cdm.test.BxcEnvironmentHelper; | ||
import edu.unc.lib.boxc.migration.cdm.test.CdmEnvironmentHelper; | ||
import edu.unc.lib.boxc.migration.cdm.test.SipServiceHelper; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
|
||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
import static org.mockito.MockitoAnnotations.openMocks; | ||
|
||
public class ArchiveProjectsServiceTest { | ||
private static final String PROJECT_NAME = "proj"; | ||
private static final String PROJECT_NAME_2 = "proj2"; | ||
|
||
@TempDir | ||
public Path tmpFolder; | ||
|
||
private SipServiceHelper testHelper; | ||
private MigrationProject project; | ||
private ArchiveProjectsService service; | ||
|
||
private AutoCloseable closeable; | ||
|
||
@BeforeEach | ||
public void setup() throws Exception { | ||
closeable = openMocks(this); | ||
project = MigrationProjectFactory.createMigrationProject( | ||
tmpFolder, PROJECT_NAME, null, "user", CdmEnvironmentHelper.DEFAULT_ENV_ID, | ||
BxcEnvironmentHelper.DEFAULT_ENV_ID, MigrationProject.PROJECT_SOURCE_CDM); | ||
testHelper = new SipServiceHelper(project, tmpFolder); | ||
|
||
service = new ArchiveProjectsService(); | ||
} | ||
|
||
@AfterEach | ||
void closeService() throws Exception { | ||
closeable.close(); | ||
} | ||
|
||
@Test | ||
public void archiveProjectTest() throws Exception { | ||
List<String> testProjects = new ArrayList<>(); | ||
testProjects.add(PROJECT_NAME); | ||
service.archiveProjects(tmpFolder, testProjects); | ||
|
||
assertTrue(Files.exists(tmpFolder.resolve(service.ARCHIVED + "/" + PROJECT_NAME))); | ||
} | ||
|
||
@Test | ||
public void archiveInvalidProjectTest() throws Exception { | ||
try { | ||
List<String> testProjects = new ArrayList<>(); | ||
testProjects.add(PROJECT_NAME_2); | ||
service.archiveProjects(tmpFolder, testProjects); | ||
fail(); | ||
} catch (Exception e) { | ||
assertTrue(e.getMessage().contains("Migration project " + PROJECT_NAME_2 + " does not exist")); | ||
} | ||
} | ||
|
||
@Test | ||
public void archiveEmptyProjectTest() throws Exception { | ||
try { | ||
List<String> testProjects = new ArrayList<>();; | ||
service.archiveProjects(tmpFolder, testProjects); | ||
fail(); | ||
} catch (Exception e) { | ||
assertTrue(e.getMessage().contains("Project names cannot be empty")); | ||
} | ||
} | ||
|
||
@Test | ||
public void archiveMultipleProjectsTest() throws Exception { | ||
project = MigrationProjectFactory.createMigrationProject( | ||
tmpFolder, PROJECT_NAME_2, null, "user", CdmEnvironmentHelper.DEFAULT_ENV_ID, | ||
BxcEnvironmentHelper.DEFAULT_ENV_ID, MigrationProject.PROJECT_SOURCE_CDM); | ||
|
||
List<String> testProjects = new ArrayList<>(); | ||
testProjects.add(PROJECT_NAME); | ||
testProjects.add(PROJECT_NAME_2); | ||
service.archiveProjects(tmpFolder, testProjects); | ||
|
||
assertTrue(Files.exists(tmpFolder.resolve(service.ARCHIVED + "/" + PROJECT_NAME))); | ||
assertTrue(Files.exists(tmpFolder.resolve(service.ARCHIVED + "/" + PROJECT_NAME_2))); | ||
} | ||
} |
Oops, something went wrong.