You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by GitBox <gi...@apache.org> on 2022/06/22 16:01:43 UTC

[GitHub] [nifi] exceptionfactory opened a new pull request, #6146: NIFI-10153 Refactor DocGeneratorTest and remove binary test files

exceptionfactory opened a new pull request, #6146:
URL: https://github.com/apache/nifi/pull/6146

   # Summary
   
   [NIFI-10153](https://issues.apache.org/jira/browse/NIFI-10153) Refactors the `DocGeneratorTest` in `nifi-documentation` and to eliminate the need for binary NAR files in the project repository. The refactored implementation mocks the `ExtensionManager` and returns component and bundle information to support HTML documentation generation.
   
   # Tracking
   
   Please complete the following tracking steps prior to pull request creation.
   
   ### Issue Tracking
   
   - [X] [Apache NiFi Jira](https://issues.apache.org/jira/browse/NIFI) issue created
   
   ### Pull Request Tracking
   
   - [X] Pull Request title starts with Apache NiFi Jira issue number, such as `NIFI-00000`
   - [X] Pull Request commit message starts with Apache NiFi Jira issue number, as such `NIFI-00000`
   
   ### Pull Request Formatting
   
   - [X] Pull Request based on current revision of the `main` branch
   - [X] Pull Request refers to a feature branch with one commit containing changes
   
   # Verification
   
   Please indicate the verification steps performed prior to pull request creation.
   
   ### Build
   
   - [ ] Build completed using `mvn clean install -P contrib-check`
     - [ ] JDK 8
     - [ ] JDK 11
     - [ ] JDK 17
   
   ### Licensing
   
   - [ ] New dependencies are compatible with the [Apache License 2.0](https://apache.org/licenses/LICENSE-2.0) according to the [License Policy](https://www.apache.org/legal/resolved.html)
   - [ ] New dependencies are documented in applicable `LICENSE` and `NOTICE` files
   
   ### Documentation
   
   - [ ] Documentation formatting appears as expected in rendered files
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nifi] kevdoran closed pull request #6146: NIFI-10153 Refactor DocGeneratorTest and remove binary test files

Posted by GitBox <gi...@apache.org>.
kevdoran closed pull request #6146: NIFI-10153 Refactor DocGeneratorTest and remove binary test files
URL: https://github.com/apache/nifi/pull/6146


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nifi] exceptionfactory commented on pull request #6146: NIFI-10153 Refactor DocGeneratorTest and remove binary test files

Posted by GitBox <gi...@apache.org>.
exceptionfactory commented on PR #6146:
URL: https://github.com/apache/nifi/pull/6146#issuecomment-1163519264

   Thanks for the review @bejancsaba!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nifi] bejancsaba commented on a diff in pull request #6146: NIFI-10153 Refactor DocGeneratorTest and remove binary test files

Posted by GitBox <gi...@apache.org>.
bejancsaba commented on code in PR #6146:
URL: https://github.com/apache/nifi/pull/6146#discussion_r904144871


##########
nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-documentation/src/test/java/org/apache/nifi/documentation/DocGeneratorTest.java:
##########
@@ -19,100 +19,87 @@
 import org.apache.commons.io.FileUtils;
 import org.apache.nifi.bundle.Bundle;
 import org.apache.nifi.bundle.BundleCoordinate;
-import org.apache.nifi.nar.ExtensionDiscoveringManager;
+import org.apache.nifi.bundle.BundleDetails;
+import org.apache.nifi.documentation.example.ProcessorWithLogger;
+import org.apache.nifi.nar.ExtensionDefinition;
+import org.apache.nifi.nar.ExtensionManager;
 import org.apache.nifi.nar.ExtensionMapping;
-import org.apache.nifi.nar.NarClassLoadersHolder;
-import org.apache.nifi.nar.NarUnpacker;
-import org.apache.nifi.nar.StandardExtensionDiscoveringManager;
-import org.apache.nifi.nar.SystemBundle;
+import org.apache.nifi.processor.Processor;
 import org.apache.nifi.util.NiFiProperties;
-import org.junit.Assert;
-import org.junit.Test;
-import org.junit.rules.TemporaryFolder;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.junit.jupiter.api.io.TempDir;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
 
-import java.io.BufferedInputStream;
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.IOException;
-import java.io.InputStream;
-import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.util.Collection;
+import java.util.Collections;
 import java.util.Properties;
 import java.util.Set;
 
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
 public class DocGeneratorTest {
+    private static final Class<ProcessorWithLogger> PROCESSOR_CLASS = ProcessorWithLogger.class;
+
+    private static final String[] HTML_EXTENSIONS = new String[]{"html"};
+
+    private static final boolean RECURSIVE_ENABLED = true;
+
+    @Mock
+    ExtensionManager extensionManager;
 
     @Test
-    public void testProcessorLoadsNarResources() throws IOException, ClassNotFoundException {
-        TemporaryFolder temporaryFolder = new TemporaryFolder();
-        temporaryFolder.create();
-
-        NiFiProperties properties = loadSpecifiedProperties("/conf/nifi.properties",
-                NiFiProperties.COMPONENT_DOCS_DIRECTORY,
-                temporaryFolder.getRoot().getAbsolutePath());
-
-        final Bundle systemBundle = SystemBundle.create(properties);
-        final ExtensionMapping mapping = NarUnpacker.unpackNars(properties, systemBundle);
-
-        NarClassLoadersHolder.getInstance().init(properties.getFrameworkWorkingDirectory(), properties.getExtensionsWorkingDirectory());
-
-        final ExtensionDiscoveringManager extensionManager = new StandardExtensionDiscoveringManager();
-        extensionManager.discoverExtensions(systemBundle, NarClassLoadersHolder.getInstance().getBundles());
-
-        DocGenerator.generate(properties, extensionManager, mapping);
-
-        final String extensionClassName = "org.apache.nifi.processors.WriteResourceToStream";
-        final BundleCoordinate coordinate = mapping.getProcessorNames().get(extensionClassName).stream().findFirst().get();
-        final String path = coordinate.getGroup() + "/" + coordinate.getId() + "/" + coordinate.getVersion() + "/" + extensionClassName;
-        File processorDirectory = new File(temporaryFolder.getRoot(), path);
-        File indexHtml = new File(processorDirectory, "index.html");
-        Assert.assertTrue(indexHtml + " should have been generated", indexHtml.exists());
-        String generatedHtml = FileUtils.readFileToString(indexHtml, Charset.defaultCharset());
-        Assert.assertNotNull(generatedHtml);
-        Assert.assertTrue(generatedHtml.contains("This example processor loads a resource from the nar and writes it to the FlowFile content"));
-        Assert.assertTrue(generatedHtml.contains("files that were successfully processed"));
-        Assert.assertTrue(generatedHtml.contains("files that were not successfully processed"));
-        Assert.assertTrue(generatedHtml.contains("resources"));
+    void testGenerateExtensionsNotFound(@TempDir final File workingDirectory) {
+        final NiFiProperties properties = getProperties(workingDirectory);
+        final ExtensionMapping extensionMapping = new ExtensionMapping();
+
+        DocGenerator.generate(properties, extensionManager, extensionMapping);
+
+        final Collection<File> files = FileUtils.listFiles(workingDirectory, HTML_EXTENSIONS, RECURSIVE_ENABLED);
+        assertTrue(files.isEmpty());
+    }
+
+    @Test
+    void testGenerateProcessor(@TempDir final File workingDirectory) throws IOException {
+        final NiFiProperties properties = getProperties(workingDirectory);
+        final ExtensionMapping extensionMapping = new ExtensionMapping();
+
+        final BundleCoordinate bundleCoordinate = BundleCoordinate.UNKNOWN_COORDINATE;
+        final BundleDetails bundleDetails = new BundleDetails.Builder().workingDir(workingDirectory).coordinate(bundleCoordinate).build();
+        final Bundle bundle = new Bundle(bundleDetails, getClass().getClassLoader());
+        final ExtensionDefinition definition = new ExtensionDefinition(PROCESSOR_CLASS.getName(), bundle, Processor.class);
+        final Set<ExtensionDefinition> extensions = Collections.singleton(definition);
+        when(extensionManager.getExtensions(eq(Processor.class))).thenReturn(extensions);
+        doReturn(PROCESSOR_CLASS).when(extensionManager).getClass(eq(definition));
+
+        final Processor processor = new ProcessorWithLogger();
+        when(extensionManager.getTempComponent(eq(PROCESSOR_CLASS.getName()), eq(bundleCoordinate))).thenReturn(processor);

Review Comment:
   As you don't really need the processor you could just simply do Mockito.mock(ProcessorWithLogger.class) at return.



##########
nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-documentation/src/test/java/org/apache/nifi/documentation/DocGeneratorTest.java:
##########
@@ -19,100 +19,87 @@
 import org.apache.commons.io.FileUtils;
 import org.apache.nifi.bundle.Bundle;
 import org.apache.nifi.bundle.BundleCoordinate;
-import org.apache.nifi.nar.ExtensionDiscoveringManager;
+import org.apache.nifi.bundle.BundleDetails;
+import org.apache.nifi.documentation.example.ProcessorWithLogger;
+import org.apache.nifi.nar.ExtensionDefinition;
+import org.apache.nifi.nar.ExtensionManager;
 import org.apache.nifi.nar.ExtensionMapping;
-import org.apache.nifi.nar.NarClassLoadersHolder;
-import org.apache.nifi.nar.NarUnpacker;
-import org.apache.nifi.nar.StandardExtensionDiscoveringManager;
-import org.apache.nifi.nar.SystemBundle;
+import org.apache.nifi.processor.Processor;
 import org.apache.nifi.util.NiFiProperties;
-import org.junit.Assert;
-import org.junit.Test;
-import org.junit.rules.TemporaryFolder;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.junit.jupiter.api.io.TempDir;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
 
-import java.io.BufferedInputStream;
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.IOException;
-import java.io.InputStream;
-import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.util.Collection;
+import java.util.Collections;
 import java.util.Properties;
 import java.util.Set;
 
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
 public class DocGeneratorTest {
+    private static final Class<ProcessorWithLogger> PROCESSOR_CLASS = ProcessorWithLogger.class;
+
+    private static final String[] HTML_EXTENSIONS = new String[]{"html"};
+
+    private static final boolean RECURSIVE_ENABLED = true;
+
+    @Mock
+    ExtensionManager extensionManager;
 
     @Test
-    public void testProcessorLoadsNarResources() throws IOException, ClassNotFoundException {
-        TemporaryFolder temporaryFolder = new TemporaryFolder();
-        temporaryFolder.create();
-
-        NiFiProperties properties = loadSpecifiedProperties("/conf/nifi.properties",
-                NiFiProperties.COMPONENT_DOCS_DIRECTORY,
-                temporaryFolder.getRoot().getAbsolutePath());
-
-        final Bundle systemBundle = SystemBundle.create(properties);
-        final ExtensionMapping mapping = NarUnpacker.unpackNars(properties, systemBundle);
-
-        NarClassLoadersHolder.getInstance().init(properties.getFrameworkWorkingDirectory(), properties.getExtensionsWorkingDirectory());
-
-        final ExtensionDiscoveringManager extensionManager = new StandardExtensionDiscoveringManager();
-        extensionManager.discoverExtensions(systemBundle, NarClassLoadersHolder.getInstance().getBundles());
-
-        DocGenerator.generate(properties, extensionManager, mapping);
-
-        final String extensionClassName = "org.apache.nifi.processors.WriteResourceToStream";
-        final BundleCoordinate coordinate = mapping.getProcessorNames().get(extensionClassName).stream().findFirst().get();
-        final String path = coordinate.getGroup() + "/" + coordinate.getId() + "/" + coordinate.getVersion() + "/" + extensionClassName;
-        File processorDirectory = new File(temporaryFolder.getRoot(), path);
-        File indexHtml = new File(processorDirectory, "index.html");
-        Assert.assertTrue(indexHtml + " should have been generated", indexHtml.exists());
-        String generatedHtml = FileUtils.readFileToString(indexHtml, Charset.defaultCharset());
-        Assert.assertNotNull(generatedHtml);
-        Assert.assertTrue(generatedHtml.contains("This example processor loads a resource from the nar and writes it to the FlowFile content"));
-        Assert.assertTrue(generatedHtml.contains("files that were successfully processed"));
-        Assert.assertTrue(generatedHtml.contains("files that were not successfully processed"));
-        Assert.assertTrue(generatedHtml.contains("resources"));
+    void testGenerateExtensionsNotFound(@TempDir final File workingDirectory) {
+        final NiFiProperties properties = getProperties(workingDirectory);
+        final ExtensionMapping extensionMapping = new ExtensionMapping();
+
+        DocGenerator.generate(properties, extensionManager, extensionMapping);
+
+        final Collection<File> files = FileUtils.listFiles(workingDirectory, HTML_EXTENSIONS, RECURSIVE_ENABLED);
+        assertTrue(files.isEmpty());
+    }
+
+    @Test
+    void testGenerateProcessor(@TempDir final File workingDirectory) throws IOException {
+        final NiFiProperties properties = getProperties(workingDirectory);
+        final ExtensionMapping extensionMapping = new ExtensionMapping();
+
+        final BundleCoordinate bundleCoordinate = BundleCoordinate.UNKNOWN_COORDINATE;
+        final BundleDetails bundleDetails = new BundleDetails.Builder().workingDir(workingDirectory).coordinate(bundleCoordinate).build();
+        final Bundle bundle = new Bundle(bundleDetails, getClass().getClassLoader());
+        final ExtensionDefinition definition = new ExtensionDefinition(PROCESSOR_CLASS.getName(), bundle, Processor.class);
+        final Set<ExtensionDefinition> extensions = Collections.singleton(definition);
+        when(extensionManager.getExtensions(eq(Processor.class))).thenReturn(extensions);
+        doReturn(PROCESSOR_CLASS).when(extensionManager).getClass(eq(definition));
+
+        final Processor processor = new ProcessorWithLogger();
+        when(extensionManager.getTempComponent(eq(PROCESSOR_CLASS.getName()), eq(bundleCoordinate))).thenReturn(processor);
+
+        DocGenerator.generate(properties, extensionManager, extensionMapping);
+
+        final Collection<File> files = FileUtils.listFiles(workingDirectory, HTML_EXTENSIONS, RECURSIVE_ENABLED);
+        assertFalse(files.isEmpty());
+
+        final File file = files.iterator().next();
+        final byte[] bytes = Files.readAllBytes(file.toPath());

Review Comment:
   Can you use Files.readString? It is basically the same you can save a few lines :)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nifi] exceptionfactory commented on pull request #6146: NIFI-10153 Refactor DocGeneratorTest and remove binary test files

Posted by GitBox <gi...@apache.org>.
exceptionfactory commented on PR #6146:
URL: https://github.com/apache/nifi/pull/6146#issuecomment-1167852555

   Thanks @kevdoran, rebased.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nifi] exceptionfactory commented on a diff in pull request #6146: NIFI-10153 Refactor DocGeneratorTest and remove binary test files

Posted by GitBox <gi...@apache.org>.
exceptionfactory commented on code in PR #6146:
URL: https://github.com/apache/nifi/pull/6146#discussion_r904150930


##########
nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-documentation/src/test/java/org/apache/nifi/documentation/DocGeneratorTest.java:
##########
@@ -19,100 +19,87 @@
 import org.apache.commons.io.FileUtils;
 import org.apache.nifi.bundle.Bundle;
 import org.apache.nifi.bundle.BundleCoordinate;
-import org.apache.nifi.nar.ExtensionDiscoveringManager;
+import org.apache.nifi.bundle.BundleDetails;
+import org.apache.nifi.documentation.example.ProcessorWithLogger;
+import org.apache.nifi.nar.ExtensionDefinition;
+import org.apache.nifi.nar.ExtensionManager;
 import org.apache.nifi.nar.ExtensionMapping;
-import org.apache.nifi.nar.NarClassLoadersHolder;
-import org.apache.nifi.nar.NarUnpacker;
-import org.apache.nifi.nar.StandardExtensionDiscoveringManager;
-import org.apache.nifi.nar.SystemBundle;
+import org.apache.nifi.processor.Processor;
 import org.apache.nifi.util.NiFiProperties;
-import org.junit.Assert;
-import org.junit.Test;
-import org.junit.rules.TemporaryFolder;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.junit.jupiter.api.io.TempDir;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
 
-import java.io.BufferedInputStream;
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.IOException;
-import java.io.InputStream;
-import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.util.Collection;
+import java.util.Collections;
 import java.util.Properties;
 import java.util.Set;
 
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
 public class DocGeneratorTest {
+    private static final Class<ProcessorWithLogger> PROCESSOR_CLASS = ProcessorWithLogger.class;
+
+    private static final String[] HTML_EXTENSIONS = new String[]{"html"};
+
+    private static final boolean RECURSIVE_ENABLED = true;
+
+    @Mock
+    ExtensionManager extensionManager;
 
     @Test
-    public void testProcessorLoadsNarResources() throws IOException, ClassNotFoundException {
-        TemporaryFolder temporaryFolder = new TemporaryFolder();
-        temporaryFolder.create();
-
-        NiFiProperties properties = loadSpecifiedProperties("/conf/nifi.properties",
-                NiFiProperties.COMPONENT_DOCS_DIRECTORY,
-                temporaryFolder.getRoot().getAbsolutePath());
-
-        final Bundle systemBundle = SystemBundle.create(properties);
-        final ExtensionMapping mapping = NarUnpacker.unpackNars(properties, systemBundle);
-
-        NarClassLoadersHolder.getInstance().init(properties.getFrameworkWorkingDirectory(), properties.getExtensionsWorkingDirectory());
-
-        final ExtensionDiscoveringManager extensionManager = new StandardExtensionDiscoveringManager();
-        extensionManager.discoverExtensions(systemBundle, NarClassLoadersHolder.getInstance().getBundles());
-
-        DocGenerator.generate(properties, extensionManager, mapping);
-
-        final String extensionClassName = "org.apache.nifi.processors.WriteResourceToStream";
-        final BundleCoordinate coordinate = mapping.getProcessorNames().get(extensionClassName).stream().findFirst().get();
-        final String path = coordinate.getGroup() + "/" + coordinate.getId() + "/" + coordinate.getVersion() + "/" + extensionClassName;
-        File processorDirectory = new File(temporaryFolder.getRoot(), path);
-        File indexHtml = new File(processorDirectory, "index.html");
-        Assert.assertTrue(indexHtml + " should have been generated", indexHtml.exists());
-        String generatedHtml = FileUtils.readFileToString(indexHtml, Charset.defaultCharset());
-        Assert.assertNotNull(generatedHtml);
-        Assert.assertTrue(generatedHtml.contains("This example processor loads a resource from the nar and writes it to the FlowFile content"));
-        Assert.assertTrue(generatedHtml.contains("files that were successfully processed"));
-        Assert.assertTrue(generatedHtml.contains("files that were not successfully processed"));
-        Assert.assertTrue(generatedHtml.contains("resources"));
+    void testGenerateExtensionsNotFound(@TempDir final File workingDirectory) {
+        final NiFiProperties properties = getProperties(workingDirectory);
+        final ExtensionMapping extensionMapping = new ExtensionMapping();
+
+        DocGenerator.generate(properties, extensionManager, extensionMapping);
+
+        final Collection<File> files = FileUtils.listFiles(workingDirectory, HTML_EXTENSIONS, RECURSIVE_ENABLED);
+        assertTrue(files.isEmpty());
+    }
+
+    @Test
+    void testGenerateProcessor(@TempDir final File workingDirectory) throws IOException {
+        final NiFiProperties properties = getProperties(workingDirectory);
+        final ExtensionMapping extensionMapping = new ExtensionMapping();
+
+        final BundleCoordinate bundleCoordinate = BundleCoordinate.UNKNOWN_COORDINATE;
+        final BundleDetails bundleDetails = new BundleDetails.Builder().workingDir(workingDirectory).coordinate(bundleCoordinate).build();
+        final Bundle bundle = new Bundle(bundleDetails, getClass().getClassLoader());
+        final ExtensionDefinition definition = new ExtensionDefinition(PROCESSOR_CLASS.getName(), bundle, Processor.class);
+        final Set<ExtensionDefinition> extensions = Collections.singleton(definition);
+        when(extensionManager.getExtensions(eq(Processor.class))).thenReturn(extensions);
+        doReturn(PROCESSOR_CLASS).when(extensionManager).getClass(eq(definition));
+
+        final Processor processor = new ProcessorWithLogger();
+        when(extensionManager.getTempComponent(eq(PROCESSOR_CLASS.getName()), eq(bundleCoordinate))).thenReturn(processor);

Review Comment:
   Actually the documentation generation uses the instantiated component during HTML generation.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nifi] exceptionfactory commented on a diff in pull request #6146: NIFI-10153 Refactor DocGeneratorTest and remove binary test files

Posted by GitBox <gi...@apache.org>.
exceptionfactory commented on code in PR #6146:
URL: https://github.com/apache/nifi/pull/6146#discussion_r904152124


##########
nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-documentation/src/test/java/org/apache/nifi/documentation/DocGeneratorTest.java:
##########
@@ -19,100 +19,87 @@
 import org.apache.commons.io.FileUtils;
 import org.apache.nifi.bundle.Bundle;
 import org.apache.nifi.bundle.BundleCoordinate;
-import org.apache.nifi.nar.ExtensionDiscoveringManager;
+import org.apache.nifi.bundle.BundleDetails;
+import org.apache.nifi.documentation.example.ProcessorWithLogger;
+import org.apache.nifi.nar.ExtensionDefinition;
+import org.apache.nifi.nar.ExtensionManager;
 import org.apache.nifi.nar.ExtensionMapping;
-import org.apache.nifi.nar.NarClassLoadersHolder;
-import org.apache.nifi.nar.NarUnpacker;
-import org.apache.nifi.nar.StandardExtensionDiscoveringManager;
-import org.apache.nifi.nar.SystemBundle;
+import org.apache.nifi.processor.Processor;
 import org.apache.nifi.util.NiFiProperties;
-import org.junit.Assert;
-import org.junit.Test;
-import org.junit.rules.TemporaryFolder;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.junit.jupiter.api.io.TempDir;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
 
-import java.io.BufferedInputStream;
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.IOException;
-import java.io.InputStream;
-import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.util.Collection;
+import java.util.Collections;
 import java.util.Properties;
 import java.util.Set;
 
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
 public class DocGeneratorTest {
+    private static final Class<ProcessorWithLogger> PROCESSOR_CLASS = ProcessorWithLogger.class;
+
+    private static final String[] HTML_EXTENSIONS = new String[]{"html"};
+
+    private static final boolean RECURSIVE_ENABLED = true;
+
+    @Mock
+    ExtensionManager extensionManager;
 
     @Test
-    public void testProcessorLoadsNarResources() throws IOException, ClassNotFoundException {
-        TemporaryFolder temporaryFolder = new TemporaryFolder();
-        temporaryFolder.create();
-
-        NiFiProperties properties = loadSpecifiedProperties("/conf/nifi.properties",
-                NiFiProperties.COMPONENT_DOCS_DIRECTORY,
-                temporaryFolder.getRoot().getAbsolutePath());
-
-        final Bundle systemBundle = SystemBundle.create(properties);
-        final ExtensionMapping mapping = NarUnpacker.unpackNars(properties, systemBundle);
-
-        NarClassLoadersHolder.getInstance().init(properties.getFrameworkWorkingDirectory(), properties.getExtensionsWorkingDirectory());
-
-        final ExtensionDiscoveringManager extensionManager = new StandardExtensionDiscoveringManager();
-        extensionManager.discoverExtensions(systemBundle, NarClassLoadersHolder.getInstance().getBundles());
-
-        DocGenerator.generate(properties, extensionManager, mapping);
-
-        final String extensionClassName = "org.apache.nifi.processors.WriteResourceToStream";
-        final BundleCoordinate coordinate = mapping.getProcessorNames().get(extensionClassName).stream().findFirst().get();
-        final String path = coordinate.getGroup() + "/" + coordinate.getId() + "/" + coordinate.getVersion() + "/" + extensionClassName;
-        File processorDirectory = new File(temporaryFolder.getRoot(), path);
-        File indexHtml = new File(processorDirectory, "index.html");
-        Assert.assertTrue(indexHtml + " should have been generated", indexHtml.exists());
-        String generatedHtml = FileUtils.readFileToString(indexHtml, Charset.defaultCharset());
-        Assert.assertNotNull(generatedHtml);
-        Assert.assertTrue(generatedHtml.contains("This example processor loads a resource from the nar and writes it to the FlowFile content"));
-        Assert.assertTrue(generatedHtml.contains("files that were successfully processed"));
-        Assert.assertTrue(generatedHtml.contains("files that were not successfully processed"));
-        Assert.assertTrue(generatedHtml.contains("resources"));
+    void testGenerateExtensionsNotFound(@TempDir final File workingDirectory) {
+        final NiFiProperties properties = getProperties(workingDirectory);
+        final ExtensionMapping extensionMapping = new ExtensionMapping();
+
+        DocGenerator.generate(properties, extensionManager, extensionMapping);
+
+        final Collection<File> files = FileUtils.listFiles(workingDirectory, HTML_EXTENSIONS, RECURSIVE_ENABLED);
+        assertTrue(files.isEmpty());
+    }
+
+    @Test
+    void testGenerateProcessor(@TempDir final File workingDirectory) throws IOException {
+        final NiFiProperties properties = getProperties(workingDirectory);
+        final ExtensionMapping extensionMapping = new ExtensionMapping();
+
+        final BundleCoordinate bundleCoordinate = BundleCoordinate.UNKNOWN_COORDINATE;
+        final BundleDetails bundleDetails = new BundleDetails.Builder().workingDir(workingDirectory).coordinate(bundleCoordinate).build();
+        final Bundle bundle = new Bundle(bundleDetails, getClass().getClassLoader());
+        final ExtensionDefinition definition = new ExtensionDefinition(PROCESSOR_CLASS.getName(), bundle, Processor.class);
+        final Set<ExtensionDefinition> extensions = Collections.singleton(definition);
+        when(extensionManager.getExtensions(eq(Processor.class))).thenReturn(extensions);
+        doReturn(PROCESSOR_CLASS).when(extensionManager).getClass(eq(definition));
+
+        final Processor processor = new ProcessorWithLogger();
+        when(extensionManager.getTempComponent(eq(PROCESSOR_CLASS.getName()), eq(bundleCoordinate))).thenReturn(processor);
+
+        DocGenerator.generate(properties, extensionManager, extensionMapping);
+
+        final Collection<File> files = FileUtils.listFiles(workingDirectory, HTML_EXTENSIONS, RECURSIVE_ENABLED);
+        assertFalse(files.isEmpty());
+
+        final File file = files.iterator().next();
+        final byte[] bytes = Files.readAllBytes(file.toPath());

Review Comment:
   It looks like [Files.readString](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/nio/file/Files.html#readString(java.nio.file.Path)) was introduced in Java 11, so this seems to be the best approach for now.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org