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
007import static org.assertj.core.api.Assertions.assertThat;
008
009import java.io.IOException;
010import java.net.URI;
011import java.net.URISyntaxException;
012import java.nio.file.Files;
013import java.nio.file.Path;
014import java.nio.file.Paths;
015import java.util.Objects;
016import java.util.Set;
017import java.util.stream.Collectors;
018import java.util.zip.GZIPInputStream;
019
020import org.apache.commons.compress.archivers.ArchiveEntry;
021import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
022import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
023import org.junit.Before;
024import org.junit.Test;
025
026/**
027 * Tests for {@link BagSerializer} and implementing classes
028 *
029 * @author mikejritter
030 * @since 2020-02-24
031 */
032public class BagSerializerTest {
033
034    private Path bag;
035    private Path resources;
036    private BagProfile profile;
037    private Set<Path> bagFiles;
038
039    @Before
040    public void setup() throws IOException, URISyntaxException {
041        final String samples = "sample";
042
043        profile = new BagProfile(BagProfile.BuiltIn.BEYOND_THE_REPOSITORY);
044        final URI sample = Objects.requireNonNull(this.getClass().getClassLoader().getResource(samples)).toURI();
045        resources = Paths.get(sample);
046        bag = Paths.get(sample).resolve("bag");
047        bagFiles = Files.walk(bag)
048                        .map(path -> resources.relativize(path))
049                        .collect(Collectors.toSet());
050    }
051
052    @Test
053    public void testZipSerializer() throws IOException {
054        final BagSerializer zipper = SerializationSupport.serializerFor("zip", profile);
055        zipper.serialize(bag);
056
057        final Path zippedBag = resources.resolve("bag.zip");
058
059        assertThat(zippedBag).exists();
060        assertThat(zippedBag).isRegularFile();
061
062        // just make sure we can read it
063        try (ZipArchiveInputStream zipIn = new ZipArchiveInputStream(Files.newInputStream(zippedBag))) {
064            ArchiveEntry entry;
065            while ((entry = zipIn.getNextEntry()) != null) {
066                assertThat(bagFiles).contains(Paths.get(entry.getName()));
067            }
068        }
069
070        Files.delete(zippedBag);
071    }
072
073    @Test
074    public void testTarSerializer() throws IOException {
075        final BagSerializer serializer = SerializationSupport.serializerFor("tar", profile);
076        serializer.serialize(bag);
077
078        final Path serializedBag = resources.resolve("bag.tar");
079
080        assertThat(serializedBag).exists();
081        assertThat(serializedBag).isRegularFile();
082
083        // just make sure we can read it
084        try (TarArchiveInputStream zipIn = new TarArchiveInputStream(Files.newInputStream(serializedBag))) {
085            ArchiveEntry entry;
086            while ((entry = zipIn.getNextEntry()) != null) {
087                assertThat(bagFiles).contains(Paths.get(entry.getName()));
088            }
089        }
090
091        Files.delete(serializedBag);
092    }
093
094    @Test
095    public void testGZipSerializer() throws IOException {
096        final BagSerializer serializer = SerializationSupport.serializerFor("tgz", profile);
097        serializer.serialize(bag);
098
099        final Path gzippedBag = resources.resolve("bag.tar.gz");
100
101        assertThat(gzippedBag).exists();
102        assertThat(gzippedBag).isRegularFile();
103
104        // just make sure we can read it
105        try (GZIPInputStream gzip = new GZIPInputStream(Files.newInputStream(gzippedBag));
106             TarArchiveInputStream zipIn = new TarArchiveInputStream(gzip)) {
107            ArchiveEntry entry;
108            while ((entry = zipIn.getNextEntry()) != null) {
109                assertThat(bagFiles).contains(Paths.get(entry.getName()));
110            }
111        }
112
113        Files.delete(gzippedBag);
114    }
115
116}