Skip to content

Commit

Permalink
Some unit tests for EditFilesComposer.
Browse files Browse the repository at this point in the history
  • Loading branch information
janvanmansum committed Dec 8, 2024
1 parent d973557 commit dcb3da1
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
Expand All @@ -60,7 +59,6 @@
@Slf4j
public class DansBagMappingServiceImpl implements DansBagMappingService {
private static final DateTimeFormatter yyyymmddPattern = DateTimeFormat.forPattern("YYYY-MM-dd");
private static final SimpleDateFormat yyyymmddFormat = new SimpleDateFormat("YYYY-MM-dd");

private final DepositToDvDatasetMetadataMapper depositToDvDatasetMetadataMapper;
private final DataverseService dataverseService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,179 @@
*/
package nl.knaw.dans.dvingest.core.dansbag;

import nl.knaw.dans.dvingest.core.bagprocessor.DataversePath;
import nl.knaw.dans.dvingest.core.dansbag.deposit.FileInfo;
import nl.knaw.dans.dvingest.core.yaml.AddEmbargo;
import nl.knaw.dans.lib.dataverse.model.file.FileMeta;
import org.junit.jupiter.api.Test;

import java.nio.file.Path;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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 EditFilesComposer editFilesComposer;

private FileInfo file(String path, String checksum, boolean restricted, String description, List<String> categories) {
var fileMeta = new FileMeta();
var dataversePath = new DataversePath(path);
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, false, fileMeta);
}

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

private FileInfo file(String path, String checksum, boolean restricted) {
return file(path, checksum, restricted, null, null);
}

private FileInfo file(String path, String checksum, boolean restricted, String description) {
return file(path, checksum, restricted, description, null);
}

private void add(Map<Path, FileInfo> map, FileInfo fileInfo) {
map.put(fileInfo.getPath(), fileInfo);
}

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

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

// Then
assertThat(editFiles.getAddRestrictedFiles()).isEmpty();
assertThat(editFiles.getAutoRenameFiles()).isEmpty();
assertThat(editFiles.getIgnoreFiles()).isEmpty();
assertThat(editFiles.getUpdateFileMetas()).isEmpty();
assertThat(editFiles.getAddEmbargoes()).isEmpty();
assertThat(editFiles.getDeleteFiles()).isEmpty();
assertThat(editFiles.getMoveFiles()).isEmpty();
}

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

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

// Then
var addRestrictedFiles = editFiles.getAddRestrictedFiles();
assertThat(addRestrictedFiles).hasSize(2);
assertThat(addRestrictedFiles).contains("file1.txt", "file2.txt");

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

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

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

// Then
var updateFileMetas = editFiles.getUpdateFileMetas();
assertThat(updateFileMetas).hasSize(2);
assertThat(updateFileMetas).extracting(FileMeta::getDescription).contains("description1", "description2");

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

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

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

// Then
var updateFileMetas = editFiles.getUpdateFileMetas();
assertThat(updateFileMetas).hasSize(2);
assertThat(updateFileMetas).extracting(FileMeta::getCategories).contains(List.of("category1"), List.of("category2"));

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

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

// 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", "file2.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 dcb3da1

Please sign in to comment.