NIFI-10153 Refactored DocGeneratorTest and removed binary test files

This closes #6146

Signed-off-by: Kevin Doran <kdoran@apache.org>
This commit is contained in:
exceptionfactory 2022-06-22 10:57:57 -05:00 committed by Kevin Doran
parent 9d379121c4
commit c1c1d0da82
No known key found for this signature in database
GPG Key ID: 5621A6244B77AC02
6 changed files with 62 additions and 201 deletions

View File

@ -19,101 +19,87 @@ package org.apache.nifi.documentation;
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.nar.NarUnpackMode;
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();
void testGenerateExtensionsNotFound(@TempDir final File workingDirectory) {
final NiFiProperties properties = getProperties(workingDirectory);
final ExtensionMapping extensionMapping = new ExtensionMapping();
NiFiProperties properties = loadSpecifiedProperties("/conf/nifi.properties",
NiFiProperties.COMPONENT_DOCS_DIRECTORY,
temporaryFolder.getRoot().getAbsolutePath());
DocGenerator.generate(properties, extensionManager, extensionMapping);
final Bundle systemBundle = SystemBundle.create(properties);
final ExtensionMapping mapping = NarUnpacker.unpackNars(properties, systemBundle, NarUnpackMode.UNPACK_INDIVIDUAL_JARS);
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"));
final Collection<File> files = FileUtils.listFiles(workingDirectory, HTML_EXTENSIONS, RECURSIVE_ENABLED);
assertTrue(files.isEmpty());
}
private NiFiProperties loadSpecifiedProperties(final String propertiesFile, final String key, final String value) {
String file = DocGeneratorTest.class.getResource(propertiesFile).getFile();
@Test
void testGenerateProcessor(@TempDir final File workingDirectory) throws IOException {
final NiFiProperties properties = getProperties(workingDirectory);
final ExtensionMapping extensionMapping = new ExtensionMapping();
System.setProperty(NiFiProperties.PROPERTIES_FILE_PATH, file);
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 Properties props = new Properties();
InputStream inStream = null;
try {
inStream = new BufferedInputStream(new FileInputStream(file));
props.load(inStream);
} catch (final Exception ex) {
throw new RuntimeException("Cannot load properties file due to "
+ ex.getLocalizedMessage(), ex);
} finally {
if (null != inStream) {
try {
inStream.close();
} catch (final Exception ex) {
/**
* do nothing *
*/
}
}
}
final Processor processor = new ProcessorWithLogger();
when(extensionManager.getTempComponent(eq(PROCESSOR_CLASS.getName()), eq(bundleCoordinate))).thenReturn(processor);
if (key != null && value != null) {
props.setProperty(key, value);
}
DocGenerator.generate(properties, extensionManager, extensionMapping);
return new NiFiProperties() {
@Override
public String getProperty(String key) {
return props.getProperty(key);
}
final Collection<File> files = FileUtils.listFiles(workingDirectory, HTML_EXTENSIONS, RECURSIVE_ENABLED);
assertFalse(files.isEmpty());
@Override
public Set<String> getPropertyKeys() {
return props.stringPropertyNames();
}
};
final File file = files.iterator().next();
final byte[] bytes = Files.readAllBytes(file.toPath());
final String html = new String(bytes, StandardCharsets.UTF_8);
assertTrue(html.contains(PROCESSOR_CLASS.getSimpleName()));
}
private NiFiProperties getProperties(final File workingDirectory) {
final Properties properties = new Properties();
properties.setProperty(NiFiProperties.COMPONENT_DOCS_DIRECTORY, workingDirectory.getAbsolutePath());
return NiFiProperties.createBasicNiFiProperties(null, properties);
}
}

View File

@ -1,125 +0,0 @@
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Core Properties #
nifi.flow.configuration.file=./target/flow.xml.gz
nifi.flow.configuration.archive.dir=./target/archive/
nifi.flowcontroller.autoResumeState=true
nifi.flowcontroller.graceful.shutdown.period=10 sec
nifi.flowservice.writedelay.interval=2 sec
nifi.administrative.yield.duration=30 sec
nifi.reporting.task.configuration.file=./target/reporting-tasks.xml
nifi.controller.service.configuration.file=./target/controller-services.xml
nifi.templates.directory=./target/templates
nifi.ui.banner.text=UI Banner Text
nifi.ui.autorefresh.interval=30 sec
nifi.nar.library.directory=./target/test-classes/lib/
nifi.nar.library.directory.alt=./target/test-classes/lib2/
nifi.nar.working.directory=./target/work/nar/
# H2 Settings
nifi.database.directory=./target/database_repository
nifi.h2.url.append=;LOCK_TIMEOUT=25000;WRITE_DELAY=0;AUTO_SERVER=FALSE
# FlowFile Repository
nifi.flowfile.repository.directory=./target/test-repo
nifi.flowfile.repository.partitions=1
nifi.flowfile.repository.checkpoint.interval=2 mins
nifi.queue.swap.threshold=20000
nifi.swap.storage.directory=./target/test-repo/swap
nifi.swap.in.period=5 sec
nifi.swap.in.threads=1
nifi.swap.out.period=5 sec
nifi.swap.out.threads=4
# Content Repository
nifi.content.claim.max.appendable.size=10 MB
nifi.content.claim.max.flow.files=100
nifi.content.repository.directory.default=./target/content_repository
# Provenance Repository Properties
nifi.provenance.repository.storage.directory=./target/provenance_repository
nifi.provenance.repository.max.storage.time=24 hours
nifi.provenance.repository.max.storage.size=1 GB
nifi.provenance.repository.rollover.time=30 secs
nifi.provenance.repository.rollover.size=100 MB
# Site to Site properties
nifi.remote.input.socket.port=9990
nifi.remote.input.secure=true
# web properties #
nifi.web.war.directory=./target/lib
nifi.web.http.host=
nifi.web.http.port=8080
nifi.web.https.host=
nifi.web.https.port=
nifi.web.jetty.working.directory=./target/work/jetty
# security properties #
nifi.sensitive.props.key=key
nifi.sensitive.props.algorithm=PBEWITHMD5AND256BITAES-CBC-OPENSSL
nifi.security.keystore=
nifi.security.keystoreType=
nifi.security.keystorePasswd=
nifi.security.keyPasswd=
nifi.security.truststore=
nifi.security.truststoreType=
nifi.security.truststorePasswd=
nifi.security.user.authorizer=
# cluster common properties (cluster manager and nodes must have same values) #
nifi.cluster.protocol.heartbeat.interval=5 sec
nifi.cluster.protocol.is.secure=false
nifi.cluster.protocol.socket.timeout=30 sec
nifi.cluster.protocol.connection.handshake.timeout=45 sec
# if multicast is used, then nifi.cluster.protocol.multicast.xxx properties must be configured #
nifi.cluster.protocol.use.multicast=false
nifi.cluster.protocol.multicast.address=
nifi.cluster.protocol.multicast.port=
nifi.cluster.protocol.multicast.service.broadcast.delay=500 ms
nifi.cluster.protocol.multicast.service.locator.attempts=3
nifi.cluster.protocol.multicast.service.locator.attempts.delay=1 sec
# cluster node properties (only configure for cluster nodes) #
nifi.cluster.is.node=false
nifi.cluster.node.address=
nifi.cluster.node.protocol.port=
nifi.cluster.node.protocol.threads=2
# if multicast is not used, nifi.cluster.node.unicast.xxx must have same values as nifi.cluster.manager.xxx #
nifi.cluster.node.unicast.manager.address=
nifi.cluster.node.unicast.manager.protocol.port=
nifi.cluster.node.unicast.manager.authority.provider.port=
# cluster manager properties (only configure for cluster manager) #
nifi.cluster.is.manager=false
nifi.cluster.manager.address=
nifi.cluster.manager.protocol.port=
nifi.cluster.manager.authority.provider.port=
nifi.cluster.manager.authority.provider.threads=10
nifi.cluster.manager.node.firewall.file=
nifi.cluster.manager.node.event.history.size=10
nifi.cluster.manager.node.api.connection.timeout=30 sec
nifi.cluster.manager.node.api.read.timeout=30 sec
nifi.cluster.manager.node.api.request.threads=10
nifi.cluster.manager.flow.retrieval.delay=5 sec
nifi.cluster.manager.protocol.threads=10
nifi.cluster.manager.safemode.duration=0 sec
# analytics properties #
nifi.analytics.predict.interval=3 mins