Skip to content

Commit

Permalink
refs #41 - add newly made payload manifests to tag manifest list
Browse files Browse the repository at this point in the history
  • Loading branch information
jscancella committed Jul 22, 2020
1 parent aa1cce5 commit f6690ca
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
21 changes: 19 additions & 2 deletions src/main/java/com/github/jscancella/domain/Bag.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -335,14 +337,29 @@ public Bag write(final Path writeTo) throws IOException {
}

final Set<Manifest> newPayloadManifests = writeManifests(writeTo, payLoadManifests);
ManifestWriter.writePayloadManifests(newPayloadManifests, writeTo, fileEncoding);
final Set<Path> newPayloadManifestFiles = ManifestWriter.writePayloadManifests(newPayloadManifests, writeTo, fileEncoding);
final Set<Manifest> updatedTagManifests = updateTagManifests(newPayloadManifestFiles);

final Set<Manifest> newTagManifests = writeManifests(writeTo, tagManifests);
final Set<Manifest> newTagManifests = writeManifests(writeTo, updatedTagManifests);
ManifestWriter.writeTagManifests(newTagManifests, writeTo, fileEncoding);

return new Bag(version, fileEncoding, newPayloadManifests, newTagManifests, itemsToFetch, metadata, writeTo);
}

private Set<Manifest> updateTagManifests(final Set<Path> newPayloadManifestFiles) throws IOException{
final Set<Manifest> 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<Manifest> writeManifests(final Path writeTo, final Set<Manifest> manifests) throws IOException{
final Set<Manifest> newTagManifests = new HashSet<>();
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/com/github/jscancella/domain/Manifest.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,19 @@ public boolean equals(final Object obj) {
public static final class ManifestBuilder {
private String algorithmName;
private Hasher hasher;
private final List<ManifestEntry> entries = new ArrayList<>();
private final List<ManifestEntry> 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());
}

/**
Expand Down
11 changes: 7 additions & 4 deletions src/test/java/com/github/jscancella/domain/BagTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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{

Expand Down Expand Up @@ -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));

Expand Down

0 comments on commit f6690ca

Please sign in to comment.