Skip to content

Commit 3f4495c

Browse files
committed
Started testing EditFilesComposerForUpdate.
1 parent 97d58e6 commit 3f4495c

File tree

4 files changed

+231
-67
lines changed

4 files changed

+231
-67
lines changed

src/main/java/nl/knaw/dans/dvingest/core/dansbag/EditFilesComposerForUpdate.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,13 @@ private Map<Integer, FileInfo> getFilesToReplace(Map<Path, FileInfo> pathToFileI
172172
}
173173

174174
/**
175-
* Creatings a mapping for moving files to a new location. To determine this, the file needs to be unique in the old and the new version, because its checksum is used to locate it. Files that
175+
* Creating a mapping for moving files to a new location. To determine this, the file needs to be unique in the old and the new version, because its checksum is used to locate it. Files that
176176
* occur multiple times in either the old or the new version cannot be moved in this way. They will appear to have been deleted in the old version and added in the new. This has the same net
177177
* result, except that the "Changes" overview in Dataverse does not record that the file was effectively moved.
178178
*
179179
* @param pathToFileMetaInLatestVersion map from path to file metadata in the old version
180180
* @param pathToFileInfo map from path to file info in the new version (i.e. the deposit).
181-
* @return
181+
* @return a map from old path to new path
182182
*/
183183
Map<Path, Path> getOldToNewPathOfFilesToMove(Map<Path, FileMeta> pathToFileMetaInLatestVersion, Map<Path, FileInfo> pathToFileInfo) {
184184

@@ -199,6 +199,7 @@ Map<Path, Path> getOldToNewPathOfFilesToMove(Map<Path, FileMeta> pathToFileMetaI
199199

200200
return intersects.stream()
201201
.map(c -> Map.entry(checksumsToPathNonDuplicatedFilesInLatestVersion.get(c), checksumsToPathNonDuplicatedFilesInDeposit.get(c)))
202+
.filter(entry -> !entry.getKey().equals(entry.getValue())) // filter out files that are not moved (this was not present in the old code)
202203
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
203204

204205
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright (C) 2024 DANS - Data Archiving and Networked Services ([email protected])
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package nl.knaw.dans.dvingest.core.dansbag;
17+
18+
import nl.knaw.dans.dvingest.core.bagprocessor.DataversePath;
19+
import nl.knaw.dans.dvingest.core.dansbag.deposit.FileInfo;
20+
import nl.knaw.dans.lib.dataverse.model.file.FileMeta;
21+
22+
import java.nio.file.Path;
23+
import java.time.Instant;
24+
import java.time.temporal.ChronoUnit;
25+
import java.util.List;
26+
import java.util.Map;
27+
28+
public class EditFilesComposerFixture {
29+
protected static final Instant inThePast = Instant.parse("2010-01-01T00:00:00Z");
30+
protected final Instant inTheFuture = Instant.now().plus(1, ChronoUnit.DAYS);
31+
protected EditFilesComposer editFilesComposer;
32+
33+
/*
34+
* Helper methods to set things up.
35+
*/
36+
protected FileInfo file(String path, String checksum, boolean restricted, String description, List<String> categories) {
37+
var fileMeta = new FileMeta();
38+
var dataversePath = new DataversePath(path);
39+
fileMeta.setLabel(dataversePath.getLabel());
40+
fileMeta.setDirectoryLabel(dataversePath.getDirectoryLabel());
41+
fileMeta.setRestrict(restricted);
42+
if (description != null) {
43+
fileMeta.setDescription(description);
44+
}
45+
if (categories != null) {
46+
fileMeta.setCategories(categories);
47+
}
48+
return new FileInfo(Path.of(path), checksum, false, fileMeta);
49+
}
50+
51+
private FileInfo sanitizedFile(String path, String sanitizedPath, String checksum, boolean restricted, String description, List<String> categories) {
52+
var fileMeta = new FileMeta();
53+
var dataversePath = new DataversePath(sanitizedPath);
54+
fileMeta.setLabel(dataversePath.getLabel());
55+
fileMeta.setDirectoryLabel(dataversePath.getDirectoryLabel());
56+
fileMeta.setRestrict(restricted);
57+
if (description != null) {
58+
fileMeta.setDescription(description);
59+
}
60+
if (categories != null) {
61+
fileMeta.setCategories(categories);
62+
}
63+
return new FileInfo(Path.of(path), checksum, true, fileMeta);
64+
}
65+
66+
protected FileInfo sanitizedFile(String path, String sanitizedPath, String checksum) {
67+
return sanitizedFile(path, sanitizedPath, checksum, false, null, null);
68+
}
69+
70+
protected FileInfo file(String path, String checksum) {
71+
return file(path, checksum, false, null, null);
72+
}
73+
74+
protected FileInfo file(String path, String checksum, boolean restricted) {
75+
return file(path, checksum, restricted, null, null);
76+
}
77+
78+
protected FileInfo file(String path, String checksum, boolean restricted, String description) {
79+
return file(path, checksum, restricted, description, null);
80+
}
81+
82+
protected void add(Map<Path, FileInfo> map, FileInfo fileInfo) {
83+
map.put(fileInfo.getPath(), fileInfo);
84+
}
85+
}

src/test/java/nl/knaw/dans/dvingest/core/dansbag/EditFilesComposerForUpdateTest.java

Lines changed: 142 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,146 @@
1515
*/
1616
package nl.knaw.dans.dvingest.core.dansbag;
1717

18-
public class EditFilesComposerForUpdateTest {
18+
import nl.knaw.dans.dvingest.core.bagprocessor.DataversePath;
19+
import nl.knaw.dans.dvingest.core.dansbag.deposit.FileInfo;
20+
import nl.knaw.dans.dvingest.core.service.DataverseService;
21+
import nl.knaw.dans.lib.dataverse.model.file.Checksum;
22+
import nl.knaw.dans.lib.dataverse.model.file.DataFile;
23+
import nl.knaw.dans.lib.dataverse.model.file.FileMeta;
24+
import org.junit.jupiter.api.Test;
25+
26+
import java.nio.file.Path;
27+
import java.util.HashMap;
28+
import java.util.List;
29+
import java.util.Map;
30+
31+
import static org.assertj.core.api.Assertions.assertThat;
32+
import static org.mockito.ArgumentMatchers.anyString;
33+
import static org.mockito.Mockito.mock;
34+
import static org.mockito.Mockito.when;
35+
36+
public class EditFilesComposerForUpdateTest extends EditFilesComposerFixture {
37+
private final DataverseService dataverseServiceMock = mock(DataverseService.class);
38+
39+
private FileMeta fileMeta(String path, String checksum) {
40+
var fileMeta = new FileMeta();
41+
var dataversePath = new DataversePath(path);
42+
fileMeta.setLabel(dataversePath.getLabel());
43+
fileMeta.setDirectoryLabel(dataversePath.getDirectoryLabel());
44+
var dataFile = new DataFile();
45+
var cs = new Checksum();
46+
cs.setType("SHA-1");
47+
cs.setValue(checksum);
48+
dataFile.setChecksum(cs);
49+
dataFile.setFilename(dataversePath.getLabel());
50+
// No directoryLabel?
51+
fileMeta.setDataFile(dataFile);
52+
return fileMeta;
53+
}
54+
55+
@Test
56+
public void file_with_same_path_and_different_checksum_is_replaced() throws Exception {
57+
// Given
58+
when(dataverseServiceMock.getFiles(anyString())).thenReturn(List.of(fileMeta("file1.txt", "oldchecksum")));
59+
Map<Path, FileInfo> map = new HashMap<>();
60+
add(map, file("file1.txt", "newchecksum"));
61+
62+
editFilesComposer = new EditFilesComposerForUpdate(map, inThePast, "doi:some", null, List.of(), dataverseServiceMock);
63+
64+
// When
65+
var editFiles = editFilesComposer.composeEditFiles();
66+
67+
// Then
68+
assertThat(editFiles.getReplaceFiles()).hasSize(1);
69+
assertThat(editFiles.getReplaceFiles().get(0)).isEqualTo("file1.txt");
70+
71+
// The rest is not affected
72+
assertThat(editFiles.getAutoRenameFiles()).isEmpty();
73+
assertThat(editFiles.getIgnoreFiles()).isEmpty();
74+
assertThat(editFiles.getUpdateFileMetas()).isEmpty();
75+
assertThat(editFiles.getAddEmbargoes()).isEmpty();
76+
assertThat(editFiles.getDeleteFiles()).isEmpty();
77+
assertThat(editFiles.getMoveFiles()).isEmpty();
78+
}
79+
80+
@Test
81+
public void file_with_same_path_and_same_checksum_is_NOT_replaced() throws Exception {
82+
// Given
83+
when(dataverseServiceMock.getFiles(anyString())).thenReturn(List.of(fileMeta("file1.txt", "oldchecksum")));
84+
Map<Path, FileInfo> map = new HashMap<>();
85+
add(map, file("file1.txt", "oldchecksum"));
86+
87+
editFilesComposer = new EditFilesComposerForUpdate(map, inThePast, "doi:some", null, List.of(), dataverseServiceMock);
88+
89+
// When
90+
var editFiles = editFilesComposer.composeEditFiles();
91+
92+
// Then
93+
assertThat(editFiles.getReplaceFiles()).isEmpty();
94+
95+
// The rest is not affected
96+
assertThat(editFiles.getAutoRenameFiles()).isEmpty();
97+
assertThat(editFiles.getIgnoreFiles()).isEmpty();
98+
assertThat(editFiles.getUpdateFileMetas()).isEmpty();
99+
assertThat(editFiles.getAddEmbargoes()).isEmpty();
100+
assertThat(editFiles.getDeleteFiles()).isEmpty();
101+
assertThat(editFiles.getMoveFiles()).isEmpty();
102+
}
103+
104+
@Test
105+
public void file_with_different_path_and_same_checksum_is_moved() throws Exception {
106+
// Given
107+
when(dataverseServiceMock.getFiles(anyString())).thenReturn(List.of(fileMeta("path/to/file1.txt", "oldchecksum")));
108+
Map<Path, FileInfo> map = new HashMap<>();
109+
add(map, file("path/three/file2.txt", "oldchecksum"));
110+
111+
editFilesComposer = new EditFilesComposerForUpdate(map, inThePast, "doi:some", null, List.of(), dataverseServiceMock);
112+
113+
// When
114+
var editFiles = editFilesComposer.composeEditFiles();
115+
116+
// Then
117+
assertThat(editFiles.getMoveFiles()).hasSize(1);
118+
assertThat(editFiles.getMoveFiles().get(0).getFrom()).isEqualTo("path/to/file1.txt");
119+
assertThat(editFiles.getMoveFiles().get(0).getTo()).isEqualTo("path/three/file2.txt");
120+
121+
// The rest is not affected
122+
assertThat(editFiles.getReplaceFiles()).isEmpty();
123+
assertThat(editFiles.getAutoRenameFiles()).isEmpty();
124+
assertThat(editFiles.getIgnoreFiles()).isEmpty();
125+
assertThat(editFiles.getUpdateFileMetas()).isEmpty();
126+
assertThat(editFiles.getAddEmbargoes()).isEmpty();
127+
assertThat(editFiles.getDeleteFiles()).isEmpty();
128+
}
129+
130+
// @Test
131+
// public void ambiguous_move_is_implemented_add_delete_and_add() throws Exception {
132+
// // Given
133+
// when(dataverseServiceMock.getFiles(anyString())).thenReturn(List.of(fileMeta("path/to/file1.txt", "oldchecksum")));
134+
// Map<Path, FileInfo> map = new HashMap<>();
135+
// add(map, file("path/three/file1.txt", "oldchecksum"));
136+
// add(map, file("path/three/file2.txt", "oldchecksum"));
137+
//
138+
// editFilesComposer = new EditFilesComposerForUpdate(map, inThePast, "doi:some", null, List.of(), dataverseServiceMock);
139+
//
140+
// // When
141+
// var editFiles = editFilesComposer.composeEditFiles();
142+
//
143+
// // Then
144+
//
145+
// assertThat(editFiles.getDeleteFiles()).hasSize(1);
146+
// assertThat(editFiles.getDeleteFiles().get(0)).isEqualTo("path/to/file1.txt");
147+
//
148+
// // The rest is not affected
149+
// assertThat(editFiles.getReplaceFiles()).isEmpty();
150+
// assertThat(editFiles.getAutoRenameFiles()).isEmpty();
151+
// assertThat(editFiles.getIgnoreFiles()).isEmpty();
152+
// assertThat(editFiles.getUpdateFileMetas()).isEmpty();
153+
// assertThat(editFiles.getAddEmbargoes()).isEmpty();
154+
// assertThat(editFiles.getMoveFiles()).isEmpty();
155+
// }
156+
157+
158+
159+
19160
}

src/test/java/nl/knaw/dans/dvingest/core/dansbag/EditFilesComposerTest.java

Lines changed: 1 addition & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -15,83 +15,20 @@
1515
*/
1616
package nl.knaw.dans.dvingest.core.dansbag;
1717

18-
import nl.knaw.dans.dvingest.core.bagprocessor.DataversePath;
1918
import nl.knaw.dans.dvingest.core.dansbag.deposit.FileInfo;
2019
import nl.knaw.dans.dvingest.core.yaml.AddEmbargo;
2120
import nl.knaw.dans.lib.dataverse.model.file.FileMeta;
2221
import org.junit.jupiter.api.Test;
2322

2423
import java.nio.file.Path;
25-
import java.time.Instant;
26-
import java.time.temporal.ChronoUnit;
2724
import java.util.HashMap;
2825
import java.util.List;
2926
import java.util.Map;
3027
import java.util.regex.Pattern;
3128

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

34-
public class EditFilesComposerTest {
35-
private static final Instant inThePast = Instant.parse("2010-01-01T00:00:00Z");
36-
private final Instant inTheFuture = Instant.now().plus(1, ChronoUnit.DAYS);
37-
private EditFilesComposer editFilesComposer;
38-
39-
/*
40-
* Helper methods to set things up.
41-
*/
42-
private FileInfo file(String path, String checksum, boolean restricted, String description, List<String> categories) {
43-
var fileMeta = new FileMeta();
44-
var dataversePath = new DataversePath(path);
45-
fileMeta.setLabel(dataversePath.getLabel());
46-
fileMeta.setDirectoryLabel(dataversePath.getDirectoryLabel());
47-
fileMeta.setRestrict(restricted);
48-
if (description != null) {
49-
fileMeta.setDescription(description);
50-
}
51-
if (categories != null) {
52-
fileMeta.setCategories(categories);
53-
}
54-
return new FileInfo(Path.of(path), checksum, false, fileMeta);
55-
}
56-
57-
private FileInfo sanitizedFile(String path, String sanitizedPath, String checksum, boolean restricted, String description, List<String> categories) {
58-
var fileMeta = new FileMeta();
59-
var dataversePath = new DataversePath(sanitizedPath);
60-
fileMeta.setLabel(dataversePath.getLabel());
61-
fileMeta.setDirectoryLabel(dataversePath.getDirectoryLabel());
62-
fileMeta.setRestrict(restricted);
63-
if (description != null) {
64-
fileMeta.setDescription(description);
65-
}
66-
if (categories != null) {
67-
fileMeta.setCategories(categories);
68-
}
69-
return new FileInfo(Path.of(path), checksum, true, fileMeta);
70-
}
71-
72-
private FileInfo sanitizedFile(String path, String sanitizedPath, String checksum) {
73-
return sanitizedFile(path, sanitizedPath, checksum, false, null, null);
74-
}
75-
76-
private FileInfo file(String path, String checksum) {
77-
return file(path, checksum, false, null, null);
78-
}
79-
80-
private FileInfo file(String path, String checksum, boolean restricted) {
81-
return file(path, checksum, restricted, null, null);
82-
}
83-
84-
private FileInfo file(String path, String checksum, boolean restricted, String description) {
85-
return file(path, checksum, restricted, description, null);
86-
}
87-
88-
private void add(Map<Path, FileInfo> map, FileInfo fileInfo) {
89-
map.put(fileInfo.getPath(), fileInfo);
90-
}
91-
92-
/*
93-
* Tests
94-
*/
31+
public class EditFilesComposerTest extends EditFilesComposerFixture {
9532

9633
@Test
9734
public void adding_two_unrestricted_files_leaves_editFiles_empty() {

0 commit comments

Comments
 (0)