Skip to content

Commit

Permalink
NIFI-12294 Standardized NAR Entry Loading (#7958)
Browse files Browse the repository at this point in the history
- Consolidated duplicative NAR file entry normalization
  • Loading branch information
exceptionfactory authored Nov 1, 2023
1 parent df3f5b4 commit 945d8b5
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;
Expand All @@ -44,30 +45,28 @@

public class NarUnpackerTest {

private static final String PROPERTIES_PATH = "/NarUnpacker/conf/nifi.properties";

@BeforeAll
public static void copyResources() throws IOException {

final Path sourcePath = Paths.get("./src/test/resources");
final Path targetPath = Paths.get("./target");

Files.walkFileTree(sourcePath, new SimpleFileVisitor<Path>() {
Files.walkFileTree(sourcePath, new SimpleFileVisitor<>() {

@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
throws IOException {
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {

Path relativeSource = sourcePath.relativize(dir);
Path target = targetPath.resolve(relativeSource);

Files.createDirectories(target);

return FileVisitResult.CONTINUE;

}

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {

Path relativeSource = sourcePath.relativize(file);
Path target = targetPath.resolve(relativeSource);
Expand All @@ -81,8 +80,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)

@Test
public void testUnpackNars() {

NiFiProperties properties = loadSpecifiedProperties("/NarUnpacker/conf/nifi.properties", Collections.EMPTY_MAP);
NiFiProperties properties = loadSpecifiedProperties(Collections.emptyMap());

assertEquals("./target/NarUnpacker/lib/",
properties.getProperty("nifi.nar.library.directory"));
Expand All @@ -93,10 +91,10 @@ public void testUnpackNars() {

assertEquals(2, extensionMapping.getAllExtensionNames().size());

assertTrue(extensionMapping.getAllExtensionNames().keySet().contains("org.apache.nifi.processors.dummy.one"));
assertTrue(extensionMapping.getAllExtensionNames().keySet().contains("org.apache.nifi.processors.dummy.two"));
assertTrue(extensionMapping.getAllExtensionNames().containsKey("org.apache.nifi.processors.dummy.one"));
assertTrue(extensionMapping.getAllExtensionNames().containsKey("org.apache.nifi.processors.dummy.two"));
final File extensionsWorkingDir = properties.getExtensionsWorkingDirectory();
File[] extensionFiles = extensionsWorkingDir.listFiles();
File[] extensionFiles = Objects.requireNonNull(extensionsWorkingDir.listFiles());

Set<String> expectedNars = new HashSet<>();
expectedNars.add("dummy-one.nar-unpacked");
Expand All @@ -110,24 +108,22 @@ public void testUnpackNars() {
}

@Test
public void testUnpackNarsFromEmptyDir() throws IOException {

public void testUnpackNarsFromEmptyDir() {
final File emptyDir = new File("./target/empty/dir");
emptyDir.delete();
emptyDir.deleteOnExit();
assertTrue(emptyDir.mkdirs());

final Map<String, String> others = new HashMap<>();
others.put("nifi.nar.library.directory.alt", emptyDir.toString());
NiFiProperties properties = loadSpecifiedProperties("/NarUnpacker/conf/nifi.properties", others);
NiFiProperties properties = loadSpecifiedProperties(others);

final ExtensionMapping extensionMapping = NarUnpacker.unpackNars(properties, SystemBundle.create(properties), NarUnpackMode.UNPACK_INDIVIDUAL_JARS);

assertEquals(1, extensionMapping.getAllExtensionNames().size());
assertTrue(extensionMapping.getAllExtensionNames().keySet().contains("org.apache.nifi.processors.dummy.one"));
assertTrue(extensionMapping.getAllExtensionNames().containsKey("org.apache.nifi.processors.dummy.one"));

final File extensionsWorkingDir = properties.getExtensionsWorkingDirectory();
File[] extensionFiles = extensionsWorkingDir.listFiles();
File[] extensionFiles = Objects.requireNonNull(extensionsWorkingDir.listFiles());

assertEquals(2, extensionFiles.length);

Expand All @@ -139,23 +135,21 @@ public void testUnpackNarsFromEmptyDir() throws IOException {

@Test
public void testUnpackNarsFromNonExistantDir() {

final File nonExistantDir = new File("./target/this/dir/should/not/exist/");
nonExistantDir.delete();
nonExistantDir.deleteOnExit();

final Map<String, String> others = new HashMap<>();
others.put("nifi.nar.library.directory.alt", nonExistantDir.toString());
NiFiProperties properties = loadSpecifiedProperties("/NarUnpacker/conf/nifi.properties", others);
NiFiProperties properties = loadSpecifiedProperties(others);

final ExtensionMapping extensionMapping = NarUnpacker.unpackNars(properties, SystemBundle.create(properties), NarUnpackMode.UNPACK_INDIVIDUAL_JARS);

assertTrue(extensionMapping.getAllExtensionNames().keySet().contains("org.apache.nifi.processors.dummy.one"));
assertTrue(extensionMapping.getAllExtensionNames().containsKey("org.apache.nifi.processors.dummy.one"));

assertEquals(1, extensionMapping.getAllExtensionNames().size());

final File extensionsWorkingDir = properties.getExtensionsWorkingDirectory();
File[] extensionFiles = extensionsWorkingDir.listFiles();
File[] extensionFiles = Objects.requireNonNull(extensionsWorkingDir.listFiles());

assertEquals(2, extensionFiles.length);

Expand All @@ -167,24 +161,23 @@ public void testUnpackNarsFromNonExistantDir() {

@Test
public void testUnpackNarsFromNonDir() throws IOException {

final File nonDir = new File("./target/file.txt");
nonDir.createNewFile();
assertTrue(nonDir.createNewFile());
nonDir.deleteOnExit();

final Map<String, String> others = new HashMap<>();
others.put("nifi.nar.library.directory.alt", nonDir.toString());
NiFiProperties properties = loadSpecifiedProperties("/NarUnpacker/conf/nifi.properties", others);
NiFiProperties properties = loadSpecifiedProperties(others);

final ExtensionMapping extensionMapping = NarUnpacker.unpackNars(properties, SystemBundle.create(properties), NarUnpackMode.UNPACK_INDIVIDUAL_JARS);

assertNull(extensionMapping);
}

private NiFiProperties loadSpecifiedProperties(final String propertiesFile, final Map<String, String> others) {
private NiFiProperties loadSpecifiedProperties(final Map<String, String> others) {
String filePath;
try {
filePath = NarUnpackerTest.class.getResource(propertiesFile).toURI().getPath();
filePath = Objects.requireNonNull(NarUnpackerTest.class.getResource(PROPERTIES_PATH)).toURI().getPath();
} catch (URISyntaxException ex) {
throw new RuntimeException("Cannot load properties file due to " + ex.getLocalizedMessage(), ex);
}
Expand Down
Loading

0 comments on commit 945d8b5

Please sign in to comment.