Skip to content

Commit

Permalink
remove isValid checking for bag creation #383
Browse files Browse the repository at this point in the history
  • Loading branch information
gmh04 committed Nov 15, 2017
1 parent 5a2a7e0 commit 2e5d382
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,17 @@ public class Packager {
public static final String fileTypeMetaFileName = "filetype.json";
public static final String externalMetaFileName = "external.txt";

// Create a bag from an existing directory.
public static boolean createBag(File dir) throws Exception {
Bag bag = BagCreator.bagInPlace(
/**
* Create a bag from an existing directory.
* @param dir Directory to create bag in.
* @return New Bag object if successful.
* @throws Exception
*/
public static Bag createBag(File dir) throws Exception {
return BagCreator.bagInPlace(
dir.toPath(),
Arrays.asList(StandardSupportedAlgorithms.MD5),
true); // include hidden files

return Packager.isValid(bag);
}

// Validate an existing bag
public static boolean validateBag(File dir) throws Exception {
Bag bag = new BagReader().read(dir.toPath());

return Packager.isValid(bag);
}

/**
Expand All @@ -65,8 +61,9 @@ public static boolean validateBag(File dir) throws Exception {
* @return True if bag is valid.
* @throws Exception
*/
private static boolean isValid(Bag bag) {
public static boolean validateBag(File dir) throws Exception {
boolean isValid = false;
Bag bag = new BagReader().read(dir.toPath());

BagVerifier bv = new BagVerifier();
try {
Expand Down Expand Up @@ -217,7 +214,7 @@ public static void main(String[] args) {
File dir = new File(args[0]);
if(dir.isDirectory()) {
try {
if(Packager.createBag(dir)){
if(Packager.createBag(dir) != null){
status = 0;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.datavaultplatform.worker.operations;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

Expand All @@ -18,6 +19,9 @@
import org.junit.BeforeClass;
import org.junit.Test;

import gov.loc.repository.bagit.domain.Bag;
import gov.loc.repository.bagit.domain.Manifest;

public class PackagerTest {
private static String packagerResources;
private static File testDir;
Expand Down Expand Up @@ -68,15 +72,25 @@ public void testCreateBag() {
}

try {
Packager.createBag(dir);

Bag bag = Packager.createBag(dir);
assertNotNull(bag);

// check bag contains expected files
for (File file : dir.listFiles()) {
if (file.isFile()) {
System.out.println(file.getName());
assertTrue(expectFiles.contains(file.getName()));
}
}

File testFile = new File(dir + File.separator + "data", TEST_FILE);
assertTrue(testFile.exists());

// check test file checksum
FileInputStream fis = new FileInputStream(testFile);
String md5 = DigestUtils.md5Hex(fis);
fis.close();
Manifest manifest= bag.getPayLoadManifests().iterator().next();
assertEquals(md5, manifest.getFileToChecksumMap().get(testFile.toPath()));
}
catch(Exception ex) {
ex.printStackTrace();
Expand Down Expand Up @@ -134,7 +148,6 @@ public void testValidateBag() {
File.separator + TEST_FILE1));
assertTrue(lines.contains(this.getChecksum(test2file) + " data" +
File.separator + CHILD_DIR_NAME + File.separator + TEST_FILE2));
System.out.println(this.getChecksum(test3file));
assertTrue(lines.contains(this.getChecksum(test3file) + " data" +
File.separator + TEST_FILE_WITH_SPACE));

Expand Down Expand Up @@ -175,7 +188,7 @@ public void testAddMetadata() {
}

try {
if(Packager.createBag(dir)){
if(Packager.createBag(dir) != null){
// the contents of the metadata files are of no
// interest for the purposes of this test
final String DEPOSIT_META = "jings";
Expand Down Expand Up @@ -235,7 +248,7 @@ public void testExtractMetadata() {

try {
File metaDir = new File(testDir, "tmp");
if(Packager.createBag(dir) && Packager.extractMetadata(dir, metaDir)){
if(Packager.createBag(dir) != null && Packager.extractMetadata(dir, metaDir)){
// check metadata files are in new directory
assertTrue(new File(metaDir, "tagmanifest-md5.txt").exists());
assertTrue(new File(metaDir, "bag-info.txt").exists());
Expand Down Expand Up @@ -268,7 +281,7 @@ public void testInvalidFilenames() {
}

try {
if(Packager.createBag(dir)){
if(Packager.createBag(dir) != null){
// TODO: this should fail
//fail(TEST_FILE + " is not a valid filename");
}
Expand Down

0 comments on commit 2e5d382

Please sign in to comment.