From f6690cab8424a20211a35e7e8d483b55a62ed536 Mon Sep 17 00:00:00 2001 From: John Scancella Date: Wed, 22 Jul 2020 19:52:44 -0400 Subject: [PATCH] refs #41 - add newly made payload manifests to tag manifest list --- .../com/github/jscancella/domain/Bag.java | 21 +++++++++++++++++-- .../github/jscancella/domain/Manifest.java | 8 ++++++- .../com/github/jscancella/domain/BagTest.java | 11 ++++++---- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/github/jscancella/domain/Bag.java b/src/main/java/com/github/jscancella/domain/Bag.java index d144c1f..417cefa 100644 --- a/src/main/java/com/github/jscancella/domain/Bag.java +++ b/src/main/java/com/github/jscancella/domain/Bag.java @@ -6,6 +6,7 @@ import java.nio.file.DirectoryStream; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import java.util.AbstractMap.SimpleImmutableEntry; import java.util.ArrayList; @@ -47,6 +48,7 @@ /** * The main representation of the bagit spec. This is an immutable object. */ +@SuppressWarnings("PMD.TooManyMethods") public final class Bag { private static final Logger logger = LoggerFactory.getLogger(Bag.class); private static final ResourceBundle messages = ResourceBundle.getBundle("MessageBundle"); @@ -335,14 +337,29 @@ public Bag write(final Path writeTo) throws IOException { } final Set newPayloadManifests = writeManifests(writeTo, payLoadManifests); - ManifestWriter.writePayloadManifests(newPayloadManifests, writeTo, fileEncoding); + final Set newPayloadManifestFiles = ManifestWriter.writePayloadManifests(newPayloadManifests, writeTo, fileEncoding); + final Set updatedTagManifests = updateTagManifests(newPayloadManifestFiles); - final Set newTagManifests = writeManifests(writeTo, tagManifests); + final Set newTagManifests = writeManifests(writeTo, updatedTagManifests); ManifestWriter.writeTagManifests(newTagManifests, writeTo, fileEncoding); return new Bag(version, fileEncoding, newPayloadManifests, newTagManifests, itemsToFetch, metadata, writeTo); } + private Set updateTagManifests(final Set newPayloadManifestFiles) throws IOException{ + final Set updatedTagManifests = new HashSet<>(tagManifests.size()); + for(final Manifest manifest : tagManifests) { + //TODO //tagManifests + newPayloadManifestFiles entries + final ManifestBuilder builder = new ManifestBuilder(manifest); + for(final Path payloadManifestFile : newPayloadManifestFiles) { + builder.addFile(payloadManifestFile, Paths.get("")); + } + updatedTagManifests.add(builder.build()); + } + + return updatedTagManifests; + } + @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") private Set writeManifests(final Path writeTo, final Set manifests) throws IOException{ final Set newTagManifests = new HashSet<>(); diff --git a/src/main/java/com/github/jscancella/domain/Manifest.java b/src/main/java/com/github/jscancella/domain/Manifest.java index 0b17eee..6cb3691 100644 --- a/src/main/java/com/github/jscancella/domain/Manifest.java +++ b/src/main/java/com/github/jscancella/domain/Manifest.java @@ -67,13 +67,19 @@ public boolean equals(final Object obj) { public static final class ManifestBuilder { private String algorithmName; private Hasher hasher; - private final List entries = new ArrayList<>(); + private final List entries; /** * @param bagitAlgorithmName the bagit algorithm name */ public ManifestBuilder(final String bagitAlgorithmName){ this.bagitAlgorithmName(bagitAlgorithmName); + this.entries = new ArrayList<>(); + } + + public ManifestBuilder(final Manifest manifestToClone) { + this.bagitAlgorithmName(manifestToClone.getBagitAlgorithmName()); + this.entries = new ArrayList<>(manifestToClone.getEntries()); } /** diff --git a/src/test/java/com/github/jscancella/domain/BagTest.java b/src/test/java/com/github/jscancella/domain/BagTest.java index 92df9a5..164b24e 100644 --- a/src/test/java/com/github/jscancella/domain/BagTest.java +++ b/src/test/java/com/github/jscancella/domain/BagTest.java @@ -10,6 +10,8 @@ import org.junit.jupiter.api.Test; import com.github.jscancella.TempFolderTest; +import com.github.jscancella.hash.Hasher; +import com.github.jscancella.hash.StandardHasher; public class BagTest extends TempFolderTest{ @@ -73,14 +75,15 @@ public void testBagCreation() throws Exception{ Assertions.assertEquals( Arrays.asList("foo: bar"), Files.readAllLines(expectedMetadataFile)); - Assertions.assertTrue(Files.exists(expectedTagmanifestFile)); - Assertions.assertEquals( - Arrays.asList("b1946ac92492d2347c6235b4d2611184 foo.txt"), Files.readAllLines(expectedTagmanifestFile)); - Assertions.assertTrue(Files.exists(expectedManifestFile)); + String manifestFileHash = StandardHasher.MD5.hash(expectedManifestFile); Assertions.assertEquals( Arrays.asList("b1946ac92492d2347c6235b4d2611184 data/foo.txt"), Files.readAllLines(expectedManifestFile)); + Assertions.assertTrue(Files.exists(expectedTagmanifestFile)); + Assertions.assertEquals( + Arrays.asList("b1946ac92492d2347c6235b4d2611184 foo.txt", manifestFileHash + " manifest-md5.txt"), Files.readAllLines(expectedTagmanifestFile)); + Assertions.assertTrue(Files.exists(expectedTagFile)); Assertions.assertArrayEquals(Files.readAllBytes(tagFile), Files.readAllBytes(expectedTagFile));