diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index 6efca362..583b7cb2 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -11,7 +11,6 @@ on: push: branches: [ main, development ] pull_request: - branches: [ main, development ] workflow_dispatch: env: diff --git a/src/main/java/edu/kit/datamanager/ro_crate/entities/data/DataEntity.java b/src/main/java/edu/kit/datamanager/ro_crate/entities/data/DataEntity.java index 50ca8d2a..0d052fba 100644 --- a/src/main/java/edu/kit/datamanager/ro_crate/entities/data/DataEntity.java +++ b/src/main/java/edu/kit/datamanager/ro_crate/entities/data/DataEntity.java @@ -65,6 +65,7 @@ public void addAuthorId(String id) { * @throws ZipException when something goes wrong with the writing to the * zip file. */ + @Deprecated(since = "2.1.0", forRemoval = true) public void saveToZip(ZipFile zipFile) throws ZipException { if (this.path != null) { ZipParameters zipParameters = new ZipParameters(); @@ -82,6 +83,7 @@ public void saveToZip(ZipFile zipFile) throws ZipException { * zip file. * @throws IOException If opening the file input stream fails. */ + @Deprecated(since = "2.1.0", forRemoval = true) public void saveToStream(ZipOutputStream zipStream) throws ZipException, IOException { if (this.path != null) { ZipUtil.addFileToZipStream(zipStream, this.path.toFile(), this.getId()); @@ -95,6 +97,7 @@ public void saveToStream(ZipOutputStream zipStream) throws ZipException, IOExcep * @param file the folder location where the entity should be written. * @throws IOException if something goes wrong with the writing. */ + @Deprecated(since = "2.1.0", forRemoval = true) public void savetoFile(File file) throws IOException { if (this.getPath() != null) { if (this.getPath().toFile().isDirectory()) { diff --git a/src/main/java/edu/kit/datamanager/ro_crate/writer/FolderStrategy.java b/src/main/java/edu/kit/datamanager/ro_crate/writer/FolderStrategy.java index b2585637..7a982648 100644 --- a/src/main/java/edu/kit/datamanager/ro_crate/writer/FolderStrategy.java +++ b/src/main/java/edu/kit/datamanager/ro_crate/writer/FolderStrategy.java @@ -25,6 +25,16 @@ public class FolderStrategy implements GenericWriterStrategy { private static final Logger logger = LoggerFactory.getLogger(FolderStrategy.class); + protected void saveDataEntity(DataEntity dataEntity, File file) throws IOException { + if (dataEntity.getPath() != null) { + if (dataEntity.getPath().toFile().isDirectory()) { + FileUtils.copyDirectory(dataEntity.getPath().toFile(), file.toPath().resolve(dataEntity.getId()).toFile()); + } else { + FileUtils.copyFile(dataEntity.getPath().toFile(), file.toPath().resolve(dataEntity.getId()).toFile()); + } + } + } + @Override public void save(Crate crate, String destination) { File file = new File(destination); @@ -54,7 +64,7 @@ public void save(Crate crate, String destination) { } for (DataEntity dataEntity : crate.getAllDataEntities()) { try { - dataEntity.savetoFile(file); + this.saveDataEntity(dataEntity, file); } catch (IOException e) { logger.error("Cannot save " + dataEntity.getId() + " to destination folder!", e); } diff --git a/src/main/java/edu/kit/datamanager/ro_crate/writer/ZipStrategy.java b/src/main/java/edu/kit/datamanager/ro_crate/writer/ZipStrategy.java index c944aad3..508460b4 100644 --- a/src/main/java/edu/kit/datamanager/ro_crate/writer/ZipStrategy.java +++ b/src/main/java/edu/kit/datamanager/ro_crate/writer/ZipStrategy.java @@ -35,17 +35,25 @@ public void save(Crate crate, String destination) { } } - private void saveDataEntities(Crate crate, ZipFile zipFile) { + protected void saveDataEntities(Crate crate, ZipFile zipFile) { for (DataEntity dataEntity : crate.getAllDataEntities()) { try { - dataEntity.saveToZip(zipFile); + this.saveDataEntity(dataEntity, zipFile); } catch (ZipException e) { logger.error("Could not save " + dataEntity.getId() + " to zip file!", e); } } } - private void saveMetadataJson(Crate crate, ZipFile zipFile) { + protected void saveDataEntity(DataEntity dataEntity, ZipFile zipFile) throws ZipException { + if (dataEntity.getPath() != null) { + ZipParameters zipParameters = new ZipParameters(); + zipParameters.setFileNameInZip(dataEntity.getId()); + zipFile.addFile(dataEntity.getPath().toFile(), zipParameters); + } + } + + protected void saveMetadataJson(Crate crate, ZipFile zipFile) { try { // write the metadata.json file ZipParameters zipParameters = new ZipParameters(); diff --git a/src/main/java/edu/kit/datamanager/ro_crate/writer/ZipStreamStrategy.java b/src/main/java/edu/kit/datamanager/ro_crate/writer/ZipStreamStrategy.java index 818c064c..5f0d5fc3 100644 --- a/src/main/java/edu/kit/datamanager/ro_crate/writer/ZipStreamStrategy.java +++ b/src/main/java/edu/kit/datamanager/ro_crate/writer/ZipStreamStrategy.java @@ -12,6 +12,10 @@ import java.io.InputStream; import java.io.OutputStream; import java.nio.charset.StandardCharsets; +import java.nio.file.Path; + +import edu.kit.datamanager.ro_crate.util.ZipUtil; +import net.lingala.zip4j.exception.ZipException; import net.lingala.zip4j.io.outputstream.ZipOutputStream; import net.lingala.zip4j.model.ZipParameters; import org.slf4j.Logger; @@ -36,17 +40,24 @@ public void save(Crate crate, OutputStream destination) { } } - private void saveDataEntities(Crate crate, ZipOutputStream zipStream) { + protected void saveDataEntities(Crate crate, ZipOutputStream zipStream) { for (DataEntity dataEntity : crate.getAllDataEntities()) { try { - dataEntity.saveToStream(zipStream); + this.saveDataEntity(dataEntity, zipStream); } catch (IOException e) { logger.error("Could not save {} to zip stream!", dataEntity.getId(), e); } } } - private void saveMetadataJson(Crate crate, ZipOutputStream zipStream) { + protected void saveDataEntity(DataEntity dataEntity, ZipOutputStream zipStream) throws ZipException, IOException { + Path path = dataEntity.getPath(); + if (path != null) { + ZipUtil.addFileToZipStream(zipStream, path.toFile(), dataEntity.getId()); + } + } + + protected void saveMetadataJson(Crate crate, ZipOutputStream zipStream) { try { // write the metadata.json file ZipParameters zipParameters = new ZipParameters(); diff --git a/src/test/java/edu/kit/datamanager/ro_crate/HelpFunctions.java b/src/test/java/edu/kit/datamanager/ro_crate/HelpFunctions.java index 48c2d858..85529fb6 100644 --- a/src/test/java/edu/kit/datamanager/ro_crate/HelpFunctions.java +++ b/src/test/java/edu/kit/datamanager/ro_crate/HelpFunctions.java @@ -11,6 +11,7 @@ import org.apache.commons.io.FileUtils; import io.json.compare.JSONCompare; import io.json.compare.JsonComparator; +import org.opentest4j.AssertionFailedError; import java.io.File; import java.io.IOException; @@ -19,6 +20,7 @@ import java.util.function.Function; import java.util.stream.Collectors; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; public class HelpFunctions { @@ -89,6 +91,16 @@ public static void compareCrateJsonToFileInResources(Crate crate1, String jsonFi compare(node1, node2, true); } + /** + * Compare two directories. The method will compare the content of the two and return true if they are equal. + * + * @param dir1 the first directory + * @param dir2 the second directory + * @return true if the two directories are equal, false otherwise. + * @throws IOException If something goes wrong with the file system. + * + */ + //@Deprecated(since = "2.1.0", forRemoval = true) public static boolean compareTwoDir(File dir1, File dir2) throws IOException { // compare the content of the two directories Map result_map = FileUtils.listFiles(dir1, null, true) @@ -117,4 +129,53 @@ public static boolean compareTwoDir(File dir1, File dir2) throws IOException { } return true; } + + /** + * Asserts that the two directories are equal. Throws an exception otherwise. + * + * @param originalDirectory the original directory to compare to + * @param testingDirectory the testing directory to test + * @throws IOException If something goes wrong with the file system. + */ + public static void assertEqualDirectories(File originalDirectory, File testingDirectory) throws IOException, AssertionFailedError { + assertTrue(originalDirectory.isDirectory(), "The original directory is not a directory."); + assertTrue(testingDirectory.isDirectory(), "The testing directory is not a directory."); + // compare the content of the two directories + Map referenceFiles = FileUtils.listFiles(originalDirectory, null, true) + .stream() + .collect(Collectors.toMap(java.io.File::getName, Function.identity())); + + Map compareThoseFiles = FileUtils.listFiles(testingDirectory, null, true) + .stream() + .collect(Collectors.toMap(java.io.File::getName, Function.identity()));; + + for (String filename : compareThoseFiles.keySet()) { + // we do that because the ro-crate-metadata.json can be differently formatted, + // or the order of the entities may be different + // the same holds for the html file + if (filename.equals("ro-crate-preview.html") || filename.equals("ro-crate-metadata.json")) { + if (!referenceFiles.containsKey(filename)) { + throw new AssertionFailedError("The file %s is not present in the reference directory.".formatted(filename)); + } + } else if (!FileUtils.contentEqualsIgnoreEOL(compareThoseFiles.get(filename), referenceFiles.get(filename), null)) { + throw new AssertionFailedError("The content of the file %s is not equal.".formatted(filename)); + } + } + + referenceFiles.keySet().forEach(filename -> { + if (!compareThoseFiles.containsKey(filename)) { + throw new AssertionFailedError("The file %s is not present in the testing directory.".formatted(filename)); + } + }); + + compareThoseFiles.keySet().forEach(filename -> { + if (!referenceFiles.containsKey(filename)) { + throw new AssertionFailedError("The file %s is not present in the reference directory.".formatted(filename)); + } + }); + + if (referenceFiles.size() != compareThoseFiles.size()) { + throw new AssertionFailedError("The number of files in the two directories is not equal."); + } + } } diff --git a/src/test/java/edu/kit/datamanager/ro_crate/writer/CrateWriterTest.java b/src/test/java/edu/kit/datamanager/ro_crate/writer/CrateWriterTest.java index d86f0f6a..c670fc14 100644 --- a/src/test/java/edu/kit/datamanager/ro_crate/writer/CrateWriterTest.java +++ b/src/test/java/edu/kit/datamanager/ro_crate/writer/CrateWriterTest.java @@ -123,9 +123,9 @@ void testWriting(@TempDir Path tempDir) throws IOException { Path extractionPath = tempDir.resolve("extracted_for_testing"); this.ensureCrateIsExtractedIn(pathToZip, extractionPath); // compare the extracted directory with the correct one - assertTrue(HelpFunctions.compareTwoDir( + HelpFunctions.assertEqualDirectories( correctCrate.toFile(), - extractionPath.toFile())); + extractionPath.toFile()); HelpFunctions.compareCrateJsonToFileInResources( builtCrate, "/json/crate/fileAndDir.json");