001/*
002 * The contents of this file are subject to the license and copyright detailed
003 * in the LICENSE and NOTICE files at the root of the source tree.
004 */
005package org.duraspace.bagit;
006
007
008import static org.junit.Assert.assertNotNull;
009import static org.junit.Assert.assertTrue;
010import static org.junit.Assert.fail;
011
012import java.io.IOException;
013import java.net.URISyntaxException;
014import java.net.URL;
015import java.nio.file.Files;
016import java.nio.file.Path;
017import java.nio.file.Paths;
018import java.util.Arrays;
019import java.util.Collection;
020import java.util.Objects;
021
022import org.apache.commons.io.FileUtils;
023import org.junit.After;
024import org.junit.Before;
025import org.junit.Test;
026import org.junit.runner.RunWith;
027import org.junit.runners.Parameterized;
028import org.junit.runners.Parameterized.Parameters;
029
030/**
031 * Test that zip, tar, and tar+gz extraction works as expected
032 *
033 * @author mikejritter
034 * @since 2020-02-13
035 */
036@RunWith(Parameterized.class)
037public class BagDeserializerTest {
038
039    @Parameters(name = "extract {0}")
040    public static Collection<Object[]> data() {
041        return Arrays.asList(new Object[][] {
042            {"bag-tar.tar", "bag-tar"},
043            {"bag-tar-no-dirs.tar", "bag-tar"},
044            {"bag-zip.zip", "bag-zip"},
045            {"bag-zip-no-dirs.zip", "bag-zip"},
046            {"bag-gzip.tar.gz", "bag-gzip"},
047        });
048    }
049
050    public static final String BAG_INFO_TXT = "bag-info.txt";
051    public static final String DATA_DIR = "data";
052    private final String group = "compress";
053    private Path target;
054
055    private final String archive;
056    private final String expectedDir;
057
058    public BagDeserializerTest(final String archive, final String expectedDir) {
059        this.archive = archive;
060        this.expectedDir = expectedDir;
061    }
062
063    @Before
064    public void setup() throws URISyntaxException {
065        final URL sample = this.getClass().getClassLoader().getResource("sample");
066        target = Paths.get(Objects.requireNonNull(sample).toURI());
067        assertNotNull(target);
068    }
069
070    @After
071    public void cleanup() throws IOException {
072        FileUtils.deleteDirectory(target.resolve(group).resolve(expectedDir).toFile());
073    }
074
075    @Test
076    public void testExtract() {
077        final Path path = target.resolve(group).resolve(archive);
078        try {
079            final BagProfile profile = new BagProfile(BagProfile.BuiltIn.BEYOND_THE_REPOSITORY);
080            final BagDeserializer deserializer = SerializationSupport.deserializerFor(path, profile);
081            deserializer.deserialize(path);
082        } catch (IOException e) {
083            fail("Unexpected exception:\n" + e.getMessage());
084        }
085
086        final Path bag = target.resolve(group).resolve(expectedDir);
087        assertTrue(Files.exists(bag));
088        assertTrue(Files.exists(bag.resolve(BAG_INFO_TXT)));
089        assertTrue(Files.exists(bag.resolve(DATA_DIR)));
090        assertTrue(Files.isDirectory(bag.resolve(DATA_DIR)));
091    }
092
093}