From 474df053d64deb0783cf02830f9092ca9d24edb2 Mon Sep 17 00:00:00 2001 From: Bryan Rosander Date: Wed, 4 Jan 2017 10:34:00 -0500 Subject: [PATCH] NIFI-3276 - FileSystemRepository.getPath() only check exists if necessary This closes #1388. --- .../repository/FileSystemRepository.java | 6 ++-- .../repository/TestFileSystemRepository.java | 33 +++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/controller/repository/FileSystemRepository.java b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/controller/repository/FileSystemRepository.java index 5eb9c5aec6..7108cfe81d 100644 --- a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/controller/repository/FileSystemRepository.java +++ b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/controller/repository/FileSystemRepository.java @@ -498,10 +498,10 @@ public class FileSystemRepository implements ContentRepository { // If the data does not exist, create a Path that points to where the data would exist in the archive directory. if (!Files.exists(resolvedPath)) { resolvedPath = getArchivePath(claim.getResourceClaim()); - } - if (verifyExists && !Files.exists(resolvedPath)) { - throw new ContentNotFoundException(claim); + if (verifyExists && !Files.exists(resolvedPath)) { + throw new ContentNotFoundException(claim); + } } return resolvedPath; } diff --git a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/test/java/org/apache/nifi/controller/repository/TestFileSystemRepository.java b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/test/java/org/apache/nifi/controller/repository/TestFileSystemRepository.java index af33ccb2a7..7edb0e7592 100644 --- a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/test/java/org/apache/nifi/controller/repository/TestFileSystemRepository.java +++ b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/test/java/org/apache/nifi/controller/repository/TestFileSystemRepository.java @@ -16,6 +16,7 @@ */ package org.apache.nifi.controller.repository; +import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; @@ -400,6 +401,38 @@ public class TestFileSystemRepository { } } + @Test + public void testReadWithContentArchived() throws IOException { + final ContentClaim claim = repository.create(true); + final Path path = getPath(claim); + Files.deleteIfExists(path); + + Path archivePath = FileSystemRepository.getArchivePath(path); + + Files.createDirectories(archivePath.getParent()); + final byte[] data = "The quick brown fox jumps over the lazy dog".getBytes(); + try (final OutputStream out = Files.newOutputStream(archivePath, StandardOpenOption.WRITE, StandardOpenOption.CREATE)) { + out.write(data); + } + + try (final InputStream inStream = repository.read(claim)) { + assertNotNull(inStream); + final byte[] dataRead = readFully(inStream, data.length); + assertArrayEquals(data, dataRead); + } + } + + @Test(expected = ContentNotFoundException.class) + public void testReadWithNoContentArchived() throws IOException { + final ContentClaim claim = repository.create(true); + final Path path = getPath(claim); + Files.deleteIfExists(path); + + Path archivePath = FileSystemRepository.getArchivePath(path); + Files.deleteIfExists(archivePath); + repository.read(claim).close(); + } + @Test public void testWrite() throws IOException { final ContentClaim claim = repository.create(true);