-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
89642d8
commit d7a4352
Showing
5 changed files
with
218 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
src/test/java/nl/knaw/dans/dvingest/core/bagprocessor/FilesInDatasetCacheNonNullTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright (C) 2024 DANS - Data Archiving and Networked Services ([email protected]) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package nl.knaw.dans.dvingest.core.bagprocessor; | ||
|
||
import nl.knaw.dans.dvingest.core.service.DataverseService; | ||
import nl.knaw.dans.lib.dataverse.model.file.FileMeta; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
|
||
import java.util.Map; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
public class FilesInDatasetCacheNonNullTest { | ||
private final DataverseService dataverseServiceMock = Mockito.mock(DataverseService.class); | ||
|
||
@Test | ||
public void constructor_throws_exception_when_dataverseService_is_null() { | ||
assertThatThrownBy(() -> new FilesInDatasetCache(null, Map.of())) | ||
.isInstanceOf(NullPointerException.class); | ||
} | ||
|
||
@Test | ||
public void constructor_throws_exception_when_autoRenamedFiles_is_null() { | ||
assertThatThrownBy(() -> new FilesInDatasetCache(dataverseServiceMock, null)) | ||
.isInstanceOf(NullPointerException.class); | ||
} | ||
|
||
@Test | ||
public void get_throws_exception_when_filepath_is_null() { | ||
var filesInDatasetCache = new FilesInDatasetCache(dataverseServiceMock, Map.of()); | ||
assertThatThrownBy(() -> filesInDatasetCache.get(null)) | ||
.isInstanceOf(NullPointerException.class); | ||
} | ||
|
||
@Test | ||
public void put_throws_exception_when_fileMeta_is_null() { | ||
var filesInDatasetCache = new FilesInDatasetCache(dataverseServiceMock, Map.of()); | ||
assertThatThrownBy(() -> filesInDatasetCache.put(null)) | ||
.isInstanceOf(NullPointerException.class); | ||
} | ||
|
||
@Test | ||
public void createFileMetaForMovedFile_throws_exception_when_toPath_is_null() { | ||
var filesInDatasetCache = new FilesInDatasetCache(dataverseServiceMock, Map.of()); | ||
var fileMeta = new FileMeta(); | ||
assertThatThrownBy(() -> filesInDatasetCache.createFileMetaForMovedFile(null, fileMeta)) | ||
.isInstanceOf(NullPointerException.class); | ||
} | ||
|
||
@Test | ||
public void createFileMetaForMovedFile_throws_exception_when_fileMeta_is_null() { | ||
var filesInDatasetCache = new FilesInDatasetCache(dataverseServiceMock, Map.of()); | ||
assertThatThrownBy(() -> filesInDatasetCache.createFileMetaForMovedFile("path", null)) | ||
.isInstanceOf(NullPointerException.class); | ||
} | ||
|
||
@Test | ||
public void remove_throws_exception_when_filepath_is_null() { | ||
var filesInDatasetCache = new FilesInDatasetCache(dataverseServiceMock, Map.of()); | ||
assertThatThrownBy(() -> filesInDatasetCache.remove(null)) | ||
.isInstanceOf(NullPointerException.class); | ||
} | ||
|
||
@Test | ||
public void downloadFromDataset_throws_exception_when_pid_is_null() { | ||
var filesInDatasetCache = new FilesInDatasetCache(dataverseServiceMock, Map.of()); | ||
assertThatThrownBy(() -> filesInDatasetCache.downloadFromDataset(null)) | ||
.isInstanceOf(NullPointerException.class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,18 @@ | ||
/* | ||
* Copyright (C) 2024 DANS - Data Archiving and Networked Services ([email protected]) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package nl.knaw.dans.dvingest.core.bagprocessor; | ||
|
||
import nl.knaw.dans.dvingest.core.service.DataverseService; | ||
|
@@ -9,6 +24,7 @@ | |
import java.util.Map; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
public class FilesInDatasetCacheTest { | ||
private final DataverseService dataverseServiceMock = Mockito.mock(DataverseService.class); | ||
|
@@ -34,19 +50,106 @@ public void get_returns_fileMeta_by_filepath() { | |
} | ||
|
||
@Test | ||
public void put_auto_renames_filepath() { | ||
public void get_returns_null_for_nonexistent_filepath() { | ||
// Given | ||
var filesInDatasetCache = new FilesInDatasetCache(dataverseServiceMock, Map.of()); | ||
|
||
// When | ||
var result = filesInDatasetCache.get("nonexistent/filepath"); | ||
|
||
// Then | ||
assertThat(result).isNull(); | ||
} | ||
|
||
@Test | ||
public void put_adds_fileMeta_to_cache() { | ||
// Given | ||
var filesInDatasetCache = new FilesInDatasetCache(dataverseServiceMock, Map.of()); | ||
var fileMeta = new FileMeta(); | ||
fileMeta.setLabel("label"); | ||
fileMeta.setDirectoryLabel("directoryLabel"); | ||
|
||
// When | ||
filesInDatasetCache.put(fileMeta); | ||
|
||
// Then | ||
assertThat(filesInDatasetCache.get("directoryLabel/label")).isEqualTo(fileMeta); | ||
} | ||
|
||
@Test | ||
public void remove_deletes_fileMeta_from_cache() { | ||
// Given | ||
var filesInDatasetCache = new FilesInDatasetCache(dataverseServiceMock, Map.of()); | ||
var fileMeta = new FileMeta(); | ||
fileMeta.setLabel("label"); | ||
fileMeta.setDirectoryLabel("directoryLabel"); | ||
filesInDatasetCache.put(fileMeta); | ||
|
||
// When | ||
filesInDatasetCache.remove("directoryLabel/label"); | ||
|
||
// Then | ||
assertThat(filesInDatasetCache.get("directoryLabel/label")).isNull(); | ||
} | ||
|
||
@Test | ||
public void downloadFromDataset_initializes_cache() throws Exception { | ||
// Given | ||
var filesInDatasetCache = new FilesInDatasetCache(dataverseServiceMock, Map.of("directoryLabel/label", "newDirectoryLabel/newLabel")); | ||
var fileMeta = new FileMeta(); | ||
fileMeta.setLabel("label"); | ||
fileMeta.setDirectoryLabel("directoryLabel"); | ||
Mockito.when(dataverseServiceMock.getFiles("pid")).thenReturn(java.util.List.of(fileMeta)); | ||
var filesInDatasetCache = new FilesInDatasetCache(dataverseServiceMock, Map.of()); | ||
|
||
// When | ||
filesInDatasetCache.downloadFromDataset("pid"); | ||
|
||
// Then | ||
assertThat(filesInDatasetCache.get("directoryLabel/label")).isEqualTo(fileMeta); | ||
} | ||
|
||
@Test | ||
public void downloadFromDataset_throws_exception_if_already_initialized() throws Exception { | ||
// Given | ||
var filesInDatasetCache = new FilesInDatasetCache(dataverseServiceMock, Map.of()); | ||
filesInDatasetCache.downloadFromDataset("pid"); | ||
|
||
// When / Then | ||
assertThatThrownBy(() -> filesInDatasetCache.downloadFromDataset("pid")) | ||
.isInstanceOf(IllegalStateException.class) | ||
.hasMessage("Cache already initialized"); | ||
} | ||
|
||
@Test | ||
public void createFileMetaForMovedFile_updates_fileMeta_with_new_path() { | ||
// Given | ||
var filesInDatasetCache = new FilesInDatasetCache(dataverseServiceMock, Map.of("oldPath/file.txt", "newPath/file.txt")); | ||
var fileMeta = new FileMeta(); | ||
fileMeta.setLabel("file.txt"); | ||
fileMeta.setDirectoryLabel("oldPath"); | ||
|
||
// When | ||
var updatedFileMeta = filesInDatasetCache.createFileMetaForMovedFile("newPath/file.txt", fileMeta); | ||
|
||
// Then | ||
assertThat(updatedFileMeta.getLabel()).isEqualTo("file.txt"); | ||
assertThat(updatedFileMeta.getDirectoryLabel()).isEqualTo("newPath"); | ||
} | ||
|
||
@Test | ||
public void get_returns_fileMeta_by_old_name_after_rename() { | ||
// Given | ||
var filesInDatasetCache = new FilesInDatasetCache(dataverseServiceMock, Map.of("oldPath/file.txt", "newPath/file.txt")); | ||
var fileMeta = new FileMeta(); | ||
fileMeta.setLabel("file.txt"); | ||
fileMeta.setDirectoryLabel("newPath"); | ||
filesInDatasetCache.put(fileMeta); | ||
var returnedFileMeta = filesInDatasetCache.get("newDirectoryLabel/newLabel"); | ||
|
||
// When | ||
var result = filesInDatasetCache.get("oldPath/file.txt"); | ||
|
||
// Then | ||
assertThat(returnedFileMeta).isEqualTo(fileMeta); | ||
assertThat(result).isEqualTo(fileMeta); | ||
} | ||
|
||
} | ||
} |