Search in sources :

Example 1 with BagReader

use of gov.loc.repository.bagit.reader.BagReader in project bagit-java by LibraryOfCongress.

the class ReaderWriterVerifierIntegrationTest method testReaderWriterVersion95.

@Test
public void testReaderWriterVersion95() throws Exception {
    BagReader reader = new BagReader();
    Path rootDir = Paths.get(this.getClass().getClassLoader().getResource("bags/v0_95/bag").toURI());
    Bag bag = reader.read(rootDir);
    Path outputDir = Paths.get(folder.newFolder().toURI());
    BagWriter.write(bag, outputDir);
    testBagsEqual(rootDir, outputDir);
    try (BagVerifier verifier = new BagVerifier()) {
        verifier.isValid(reader.read(outputDir), true);
    }
}
Also used : Path(java.nio.file.Path) BagVerifier(gov.loc.repository.bagit.verify.BagVerifier) BagReader(gov.loc.repository.bagit.reader.BagReader) Bag(gov.loc.repository.bagit.domain.Bag) Test(org.junit.Test)

Example 2 with BagReader

use of gov.loc.repository.bagit.reader.BagReader in project bagit-java by LibraryOfCongress.

the class BagLinterTest method testCheckAgainstProfile.

@Test
public void testCheckAgainstProfile() throws Exception {
    Path profileJson = new File("src/test/resources/bagitProfiles/exampleProfile.json").toPath();
    Path bagRootPath = new File("src/test/resources/bagitProfileTestBags/profileConformantBag").toPath();
    BagReader reader = new BagReader();
    Bag bag = reader.read(bagRootPath);
    try (InputStream inputStream = Files.newInputStream(profileJson, StandardOpenOption.READ)) {
        BagLinter.checkAgainstProfile(inputStream, bag);
    }
}
Also used : Path(java.nio.file.Path) InputStream(java.io.InputStream) BagReader(gov.loc.repository.bagit.reader.BagReader) Bag(gov.loc.repository.bagit.domain.Bag) File(java.io.File) PrivateConstructorTest(gov.loc.repository.bagit.PrivateConstructorTest) Test(org.junit.Test)

Example 3 with BagReader

use of gov.loc.repository.bagit.reader.BagReader in project epadd by ePADD.

the class Archive method readArchiveBag.

/* //maps locId (int) to docId (edu.stanford.muse.index.Document) using indexer storage structures
    public Integer getLDocIdForContentDocId(String docId){
        return indexer.contentDocIds.entrySet().stream().filter(e->docId.equals(e.getValue())).findAny().map(Map.Entry::getKey).orElse(0);
    }

    public String getDocIdForContentLDocId(Integer ldocId){
        return indexer.contentDocIds.get(ldocId);
    }*/
/*public Integer getLDocIdForBlobDocId(String docId){
        return indexer.blobDocIds.entrySet().stream().filter(e->docId.equals(e.getValue())).findAny().map(Map.Entry::getKey).orElse(0);
    }*/
/*public String getDocIdForBlobLDocId(Integer ldocId) {
        return indexer.blobDocIds.get(ldocId);
    }
*/
// input is a bag present in userdir
public static Bag readArchiveBag(String userdir) {
    Path rootDir = Paths.get(userdir);
    BagReader reader = new BagReader();
    String errorMessage = "";
    Bag bag = null;
    try {
        bag = reader.read(rootDir);
    } catch (IOException e) {
        e.printStackTrace();
        errorMessage = "Could not read directory " + rootDir;
    } catch (UnparsableVersionException e) {
        e.printStackTrace();
        errorMessage = "Bag is of unparsable version: " + e.getMessage();
    } catch (MaliciousPathException e) {
        e.printStackTrace();
        errorMessage = "Malicious path found for the bag: " + e.getMessage();
    } catch (UnsupportedAlgorithmException e) {
        e.printStackTrace();
        errorMessage = "The bag has checksums for unsupported algorithms: " + e.getMessage();
    } catch (InvalidBagitFileFormatException e) {
        e.printStackTrace();
        errorMessage = "The file format of the bag is invalid :" + e.getMessage();
    }
    // If error in verification return null;
    if (!Util.nullOrEmpty(errorMessage))
        return null;
    else
        return bag;
}
Also used : BagReader(gov.loc.repository.bagit.reader.BagReader)

Example 4 with BagReader

use of gov.loc.repository.bagit.reader.BagReader in project bagit-java by LibraryOfCongress.

the class BagVerifierTest method testErrorWhenUnspportedAlgorithmException.

@Test(expected = UnsupportedAlgorithmException.class)
public void testErrorWhenUnspportedAlgorithmException() throws Exception {
    Path sha3BagDir = Paths.get(getClass().getClassLoader().getResource("sha3Bag").toURI());
    BagReader extendedReader = new BagReader();
    Bag bag = extendedReader.read(sha3BagDir);
    sut.isValid(bag, true);
}
Also used : Path(java.nio.file.Path) BagReader(gov.loc.repository.bagit.reader.BagReader) Bag(gov.loc.repository.bagit.domain.Bag) Test(org.junit.Test)

Example 5 with BagReader

use of gov.loc.repository.bagit.reader.BagReader in project bagit-java by LibraryOfCongress.

the class BagVerifierTest method testAddSHA3SupportViaExtension.

@Test
public void testAddSHA3SupportViaExtension() throws Exception {
    Path sha3BagDir = Paths.get(new File("src/test/resources/sha3Bag").toURI());
    MySupportedNameToAlgorithmMapping mapping = new MySupportedNameToAlgorithmMapping();
    BagReader extendedReader = new BagReader(mapping);
    Bag bag = extendedReader.read(sha3BagDir);
    try (BagVerifier extendedSut = new BagVerifier(mapping)) {
        extendedSut.isValid(bag, true);
    }
}
Also used : Path(java.nio.file.Path) BagReader(gov.loc.repository.bagit.reader.BagReader) Bag(gov.loc.repository.bagit.domain.Bag) File(java.io.File) Test(org.junit.Test)

Aggregations

BagReader (gov.loc.repository.bagit.reader.BagReader)10 Bag (gov.loc.repository.bagit.domain.Bag)9 Path (java.nio.file.Path)9 Test (org.junit.Test)9 BagVerifier (gov.loc.repository.bagit.verify.BagVerifier)6 File (java.io.File)2 PrivateConstructorTest (gov.loc.repository.bagit.PrivateConstructorTest)1 InputStream (java.io.InputStream)1