Skip to content

Commit 13b597a

Browse files
authored
Merge pull request #962 from UNC-Libraries/bxc-2624-dep-cleanup
BXC-2624 - Fix deposit cleanup failures
2 parents bedfc43 + 248b281 commit 13b597a

File tree

9 files changed

+177
-12
lines changed

9 files changed

+177
-12
lines changed

deposit/src/main/java/edu/unc/lib/deposit/CleanupDepositJob.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ private File deleteFile(URI uri) {
7474
Files.delete(cFile.toPath());
7575
LOG.info("Deleted file: {}", cFile.getAbsoluteFile());
7676
} catch (NoSuchFileException e) {
77-
LOG.warn("Cannot cleanup file {}, it does not exist", uri);
77+
LOG.debug("Cannot cleanup file {}, it does not exist", uri);
7878
} catch (IOException e) {
7979
LOG.error("Cannot delete a staged file: " + uri.toString(), e);
8080
}

deposit/src/main/java/edu/unc/lib/deposit/validate/ValidateFileAvailabilityJob.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import edu.unc.lib.deposit.work.AbstractDepositJob;
3030
import edu.unc.lib.dl.fedora.PID;
31+
import edu.unc.lib.dl.persist.api.ingest.IngestSource;
3132
import edu.unc.lib.dl.persist.api.ingest.IngestSourceManager;
3233
import edu.unc.lib.dl.persist.api.ingest.UnknownIngestSourceException;
3334
import edu.unc.lib.dl.rdf.CdrDeposit;
@@ -73,9 +74,13 @@ public void runJob() {
7374
try {
7475
URI manifestURI = URI.create(entry.getValue());
7576
// If no ingest source can be found for the file, then file not available
76-
sourceManager.getIngestSourceForUri(manifestURI);
77+
IngestSource source = sourceManager.getIngestSourceForUri(manifestURI);
78+
if (!source.exists(manifestURI)) {
79+
log.debug("Failed find staged file {} in deposit {}", href, getDepositUUID());
80+
badlyStagedFiles.add(href);
81+
}
7782
} catch (UnknownIngestSourceException e) {
78-
log.debug("Failed find staged file {} in deposit {}", href, getDepositUUID(), e);
83+
log.debug("Could not determine staging location for {} in deposit {}", href, getDepositUUID(), e);
7984
badlyStagedFiles.add(href);
8085
}
8186

deposit/src/test/java/edu/unc/lib/deposit/CleanupDepositJobTest.java

+17
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131

3232
import java.io.File;
3333
import java.net.URI;
34+
import java.nio.file.Files;
35+
import java.nio.file.Paths;
3436
import java.util.HashMap;
3537
import java.util.Map;
3638

@@ -202,6 +204,21 @@ public void cleanupMissingStagingFolderTest() throws Exception {
202204
assertDepositCleanedUp();
203205
}
204206

207+
@Test
208+
public void cleanupFileAlreadyRemovedTest() throws Exception {
209+
addIngestedFilesToModel();
210+
Files.delete(Paths.get(stagingPath, "project/folderA/ingested"));
211+
212+
when(ingestSource.isReadOnly()).thenReturn(false);
213+
when(sourceManager.getIngestSourceForUri(any(URI.class))).thenReturn(ingestSource);
214+
215+
job.run();
216+
217+
assertIngestedFilesEmptyFoldersDeleted(stagingFolder);
218+
219+
assertDepositCleanedUp();
220+
}
221+
205222
/**
206223
* Test that registering multiple staging locations with different policies
207224
* works correctly

deposit/src/test/java/edu/unc/lib/deposit/validate/ValidateFileAvailabilityJobTest.java

+17-2
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ public void init() throws Exception {
9191
when(depositStatusFactory.getState(anyString()))
9292
.thenReturn(DepositState.running);
9393
when(sourceManager.getIngestSourceForUri(any(URI.class))).thenReturn(ingestSource);
94+
when(ingestSource.exists(any(URI.class))).thenReturn(true);
9495

9596
depositPid = job.getDepositPID();
9697

@@ -143,12 +144,26 @@ public void absolutePresentTest() {
143144
}
144145

145146
@Test(expected = JobFailedException.class)
146-
public void missingFileTest() {
147+
public void invalidSourceTest() {
147148
Model model = job.getWritableModel();
148149
Bag depBag = model.createBag(depositPid.getRepositoryPath());
149150

150151
when(sourceManager.getIngestSourceForUri(any(URI.class)))
151-
.thenThrow(new UnknownIngestSourceException("missing"));
152+
.thenThrow(new UnknownIngestSourceException("Not source"));
153+
154+
addFileObject(depBag, "missing.pdf");
155+
156+
job.closeModel();
157+
158+
job.run();
159+
}
160+
161+
@Test(expected = JobFailedException.class)
162+
public void missingFileTest() {
163+
Model model = job.getWritableModel();
164+
Bag depBag = model.createBag(depositPid.getRepositoryPath());
165+
166+
when(ingestSource.exists(any(URI.class))).thenReturn(false);
152167

153168
addFileObject(depBag, "missing.pdf");
154169

fcrepo-clients/src/main/java/edu/unc/lib/dl/persist/api/ingest/IngestSource.java

+8
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ public interface IngestSource {
8686
*/
8787
boolean isValidUri(URI uri);
8888

89+
/**
90+
* Returns true if the provided URI exists within this ingest source
91+
*
92+
* @param uri
93+
* @return
94+
*/
95+
boolean exists(URI uri);
96+
8997
/**
9098
* List the potential candidates for ingest from this source location.
9199
*

persistence/src/main/java/edu/unc/lib/dl/persist/services/ingest/FilesystemIngestSource.java

+12-2
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,23 @@ private boolean isValidPath(Path path) {
149149
for (String pattern : patterns) {
150150
PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:" + baseString + "/" + pattern + "*");
151151
if (matcher.matches(path)) {
152-
return path.toFile().exists();
152+
return true;
153153
}
154154
}
155155

156156
return false;
157157
}
158158

159+
@Override
160+
public boolean exists(URI uri) {
161+
Path path = Paths.get(uri).normalize();
162+
if (!path.startsWith(basePath)) {
163+
return false;
164+
}
165+
166+
return Files.exists(path);
167+
}
168+
159169
@Override
160170
public List<IngestSourceCandidate> listCandidates() {
161171
List<IngestSourceCandidate> candidates = new ArrayList<>();
@@ -249,7 +259,7 @@ private Bag getBagitBag(Path filePath) throws IOException, UnparsableVersionExce
249259
public URI resolveRelativePath(String relative) {
250260
Path candidatePath = basePath.resolve(relative).normalize();
251261

252-
if (!isValidPath(candidatePath)) {
262+
if (!isValidPath(candidatePath) || Files.notExists(candidatePath)) {
253263
throw new InvalidIngestSourceCandidateException("Invalid ingest source path " + relative);
254264
}
255265

persistence/src/test/java/edu/unc/lib/dl/persist/services/ingest/FilesystemIngestSourceTest.java

+49-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public void isValidUriDoesNotExist() throws Exception {
115115

116116
URI testUri = Paths.get(sourceFolderPath.toString(), "someSubPath").toUri();
117117

118-
assertFalse(ingestSource.isValidUri(testUri));
118+
assertTrue(ingestSource.isValidUri(testUri));
119119
}
120120

121121
@Test
@@ -184,6 +184,31 @@ public void isValidUriNonFileUri() throws Exception {
184184
assertFalse(ingestSource.isValidUri(uri));
185185
}
186186

187+
@Test
188+
public void existsNotInBase() throws Exception {
189+
ingestSource.setBase(sourceFolderPath.toUri().toString());
190+
191+
URI uri = URI.create("file:///some/weird/path");
192+
assertFalse(ingestSource.exists(uri));
193+
}
194+
195+
@Test
196+
public void existsInBaseNotFound() throws Exception {
197+
ingestSource.setBase(sourceFolderPath.toUri().toString());
198+
199+
URI uri = sourceFolderPath.resolve("unknown/path.txt").toUri();
200+
assertFalse(ingestSource.exists(uri));
201+
}
202+
203+
@Test
204+
public void existsSubpathExists() throws Exception {
205+
ingestSource.setBase(sourceFolderPath.toUri().toString());
206+
207+
Path path = sourceFolderPath.resolve("someSubPath/child");
208+
Files.createDirectories(path);
209+
assertTrue(ingestSource.exists(path.toUri()));
210+
}
211+
187212
@Test
188213
public void listCandidatesBag() throws Exception {
189214
ingestSource.setId("testsource");
@@ -307,6 +332,29 @@ public void resolveRelativePathBag() throws Exception {
307332
assertEquals(sourceFolderPath.resolve("bag_with_files").toUri(), candUri);
308333
}
309334

335+
@Test
336+
public void resolveRelativePathBagNested() throws Exception {
337+
ingestSource.setId("testsource");
338+
ingestSource.setBase(sourceFolderPath.toUri().toString());
339+
ingestSource.setPatterns(asList("*"));
340+
341+
addBagToSource(sourceFolderPath);
342+
343+
URI candUri = ingestSource.resolveRelativePath("bag_with_files/data/test1.txt");
344+
assertEquals(sourceFolderPath.resolve("bag_with_files/data/test1.txt").toUri(), candUri);
345+
}
346+
347+
@Test(expected = InvalidIngestSourceCandidateException.class)
348+
public void resolveRelativePathBagNestedNotExist() throws Exception {
349+
ingestSource.setId("testsource");
350+
ingestSource.setBase(sourceFolderPath.toUri().toString());
351+
ingestSource.setPatterns(asList("*"));
352+
353+
addBagToSource(sourceFolderPath);
354+
355+
ingestSource.resolveRelativePath("bag_with_files/data/ohno.txt");
356+
}
357+
310358
@Test(expected = InvalidIngestSourceCandidateException.class)
311359
public void resolveRelativePathOutsideOfSource() throws Exception {
312360
ingestSource.setId("testsource");

persistence/src/test/java/edu/unc/lib/dl/persist/services/ingest/IngestSourceManagerImplTest.java

+60
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import static org.mockito.Mockito.when;
3030
import static org.mockito.MockitoAnnotations.initMocks;
3131

32+
import java.net.URI;
3233
import java.nio.file.Path;
3334
import java.util.ArrayList;
3435
import java.util.List;
@@ -315,6 +316,65 @@ public void testListCandidatesNoMatchingSource() throws Exception {
315316
assertTrue("No candidates expected", candidates.isEmpty());
316317
}
317318

319+
@Test
320+
public void testGetIngestSourceForUri_NestedUri() throws Exception {
321+
configPath = createConfigFile(createBasicConfig("testsource", destPid));
322+
Path source2FolderPath = tmpFolder.newFolder().toPath();
323+
324+
Map<String, Object> source2 = createConfig(
325+
"testsource2",
326+
"Source 2",
327+
source2FolderPath,
328+
asList("*"),
329+
destPid);
330+
configPath = createConfigFile(source2, createBasicConfig("testsource", destPid));
331+
332+
Path targetPath = addBagToSource(sourceFolderPath);
333+
334+
initializeManager();
335+
336+
IngestSource source = sourceMan.getIngestSourceForUri(targetPath.toUri());
337+
assertEquals("testsource", source.getId());
338+
}
339+
340+
@Test
341+
public void testGetIngestSourceForUri_ValidPathTargetNotExist() throws Exception {
342+
configPath = createConfigFile(createBasicConfig("testsource", destPid));
343+
344+
initializeManager();
345+
346+
URI targetUri = sourceFolderPath.resolve("my_target").toUri();
347+
348+
IngestSource source = sourceMan.getIngestSourceForUri(targetUri);
349+
assertEquals("testsource", source.getId());
350+
}
351+
352+
@Test
353+
public void testGetIngestSourceForUri_NestedPath() throws Exception {
354+
configPath = createConfigFile(createBasicConfig("testsource", destPid));
355+
356+
Path bagPath = addBagToSource(sourceFolderPath);
357+
Path nestedPath = bagPath.resolve("data/test1.txt");
358+
359+
initializeManager();
360+
361+
IngestSource source = sourceMan.getIngestSourceForUri(nestedPath.toUri());
362+
assertEquals("testsource", source.getId());
363+
}
364+
365+
@Test
366+
public void testGetIngestSourceForUri_NestedNotExist() throws Exception {
367+
configPath = createConfigFile(createBasicConfig("testsource", destPid));
368+
369+
Path bagPath = addBagToSource(sourceFolderPath);
370+
Path nestedPath = bagPath.resolve("data/somewhere.txt");
371+
372+
initializeManager();
373+
374+
IngestSource source = sourceMan.getIngestSourceForUri(nestedPath.toUri());
375+
assertEquals("testsource", source.getId());
376+
}
377+
318378
private PID makePid() {
319379
return PIDs.get(UUID.randomUUID().toString());
320380
}

persistence/src/test/java/edu/unc/lib/dl/persist/services/ingest/IngestSourceTestHelper.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,16 @@ public class IngestSourceTestHelper {
4848
private IngestSourceTestHelper() {
4949
}
5050

51-
public static void addBagToSource(Path ingestSourcePath) throws Exception {
51+
public static Path addBagToSource(Path ingestSourcePath) throws Exception {
5252
String bagName = "bag_with_files";
53-
addBagToSource(ingestSourcePath, bagName, bagName);
53+
return addBagToSource(ingestSourcePath, bagName, bagName);
5454
}
5555

56-
public static void addBagToSource(Path ingestSourcePath, String bagName, String destName) throws Exception {
56+
public static Path addBagToSource(Path ingestSourcePath, String bagName, String destName) throws Exception {
5757
File original = new File("src/test/resources/ingestSources/" + bagName);
58-
FileUtils.copyDirectory(original, ingestSourcePath.resolve(destName).toFile());
58+
Path bagPath = ingestSourcePath.resolve(destName);
59+
FileUtils.copyDirectory(original, bagPath.toFile());
60+
return bagPath;
5961
}
6062

6163
public static IngestSourceCandidate findCandidateByPath(String patternMatched,

0 commit comments

Comments
 (0)