Skip to content

Commit

Permalink
Finishing testing EditFilesComposer for now.
Browse files Browse the repository at this point in the history
  • Loading branch information
janvanmansum committed Dec 8, 2024
1 parent dcb3da1 commit 97d58e6
Showing 1 changed file with 103 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,18 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import static org.assertj.core.api.Assertions.assertThat;

public class EditFilesComposerTest {
private static final Instant inThePast = Instant.parse("2010-01-01T00:00:00Z");
private Instant inTheFuture = Instant.now().plus(1, ChronoUnit.DAYS);
private final Instant inTheFuture = Instant.now().plus(1, ChronoUnit.DAYS);
private EditFilesComposer editFilesComposer;

/*
* Helper methods to set things up.
*/
private FileInfo file(String path, String checksum, boolean restricted, String description, List<String> categories) {
var fileMeta = new FileMeta();
var dataversePath = new DataversePath(path);
Expand All @@ -50,6 +54,25 @@ private FileInfo file(String path, String checksum, boolean restricted, String d
return new FileInfo(Path.of(path), checksum, false, fileMeta);
}

private FileInfo sanitizedFile(String path, String sanitizedPath, String checksum, boolean restricted, String description, List<String> categories) {
var fileMeta = new FileMeta();
var dataversePath = new DataversePath(sanitizedPath);
fileMeta.setLabel(dataversePath.getLabel());
fileMeta.setDirectoryLabel(dataversePath.getDirectoryLabel());
fileMeta.setRestrict(restricted);
if (description != null) {
fileMeta.setDescription(description);
}
if (categories != null) {
fileMeta.setCategories(categories);
}
return new FileInfo(Path.of(path), checksum, true, fileMeta);
}

private FileInfo sanitizedFile(String path, String sanitizedPath, String checksum) {
return sanitizedFile(path, sanitizedPath, checksum, false, null, null);
}

private FileInfo file(String path, String checksum) {
return file(path, checksum, false, null, null);
}
Expand All @@ -66,6 +89,10 @@ private void add(Map<Path, FileInfo> map, FileInfo fileInfo) {
map.put(fileInfo.getPath(), fileInfo);
}

/*
* Tests
*/

@Test
public void adding_two_unrestricted_files_leaves_editFiles_empty() {
// Given
Expand Down Expand Up @@ -190,4 +217,79 @@ public void setting_dateAvailable_to_future_date_embargoes_all_files() {
assertThat(editFiles.getMoveFiles()).isEmpty();
}

@Test
public void sanitized_files_are_add_to_autoRenamedFiles() {
// Given
Map<Path, FileInfo> map = new HashMap<>();
add(map, sanitizedFile("file1.txt", "file1_sanitized.txt", "checksum1"));
add(map, file("file2.txt", "checksum2"));
editFilesComposer = new EditFilesComposer(map, inThePast, null, List.of());

// When
var editFiles = editFilesComposer.composeEditFiles();

// Then
var autoRenameFiles = editFiles.getAutoRenameFiles();
assertThat(autoRenameFiles).hasSize(1);
assertThat(autoRenameFiles.get(0).getFrom()).isEqualTo("file1.txt");
assertThat(autoRenameFiles.get(0).getTo()).isEqualTo("file1_sanitized.txt");

// The rest is not affected
assertThat(editFiles.getAddRestrictedFiles()).isEmpty();
assertThat(editFiles.getIgnoreFiles()).isEmpty();
assertThat(editFiles.getUpdateFileMetas()).isEmpty();
assertThat(editFiles.getAddEmbargoes()).isEmpty();
assertThat(editFiles.getDeleteFiles()).isEmpty();
assertThat(editFiles.getMoveFiles()).isEmpty();
}

@Test
public void fileExclusionPattern_ignores_files_matching_pattern() {
// Given
Map<Path, FileInfo> map = new HashMap<>();
add(map, file("file1.txt", "checksum1"));
add(map, file("file2.txt", "checksum2"));
editFilesComposer = new EditFilesComposer(map, inThePast, Pattern.compile("file1.*"), List.of());

// When
var editFiles = editFilesComposer.composeEditFiles();

// Then
assertThat(editFiles.getIgnoreFiles()).containsExactly("file1.txt");

// The rest is not affected
assertThat(editFiles.getAddRestrictedFiles()).isEmpty();
assertThat(editFiles.getAutoRenameFiles()).isEmpty();
assertThat(editFiles.getUpdateFileMetas()).isEmpty();
assertThat(editFiles.getAddEmbargoes()).isEmpty();
assertThat(editFiles.getDeleteFiles()).isEmpty();
assertThat(editFiles.getMoveFiles()).isEmpty();
}

@Test
public void embargoExclusions_ignores_files_matching_filepaths() {
// Given
Map<Path, FileInfo> map = new HashMap<>();
add(map, file("file1.txt", "checksum1"));
add(map, file("file2.txt", "checksum2"));
add(map, file("subdir/file3.txt", "checksum3"));
editFilesComposer = new EditFilesComposer(map, inTheFuture, null, List.of("file2.txt", "subdir/file3.txt"));

// When
var editFiles = editFilesComposer.composeEditFiles();

// Then
var addEmbargoes = editFiles.getAddEmbargoes();
assertThat(addEmbargoes).hasSize(1); // There is only one embargo, covering all files
assertThat(addEmbargoes).extracting(AddEmbargo::getFilePaths).containsExactly(List.of("file1.txt"));

// The rest is not affected
assertThat(editFiles.getAddRestrictedFiles()).isEmpty();
assertThat(editFiles.getAutoRenameFiles()).isEmpty();
assertThat(editFiles.getIgnoreFiles()).isEmpty();
assertThat(editFiles.getUpdateFileMetas()).isEmpty();
assertThat(editFiles.getDeleteFiles()).isEmpty();
assertThat(editFiles.getMoveFiles()).isEmpty();
}

}

0 comments on commit 97d58e6

Please sign in to comment.