exceptionfactory commented on a change in pull request #5732:
URL: https://github.com/apache/nifi/pull/5732#discussion_r810323812



##########
File path: 
nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/main/java/org/apache/nifi/processors/document/ExtractDocumentText.java
##########
@@ -0,0 +1,130 @@
+/*
+ * 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.
+ */
+package org.apache.nifi.processors.document;
+
+import org.apache.nifi.annotation.documentation.CapabilityDescription;
+import org.apache.nifi.annotation.documentation.Tags;
+import org.apache.nifi.components.PropertyDescriptor;
+import org.apache.nifi.expression.ExpressionLanguageScope;
+import org.apache.nifi.flowfile.FlowFile;
+import org.apache.nifi.processor.AbstractProcessor;
+import org.apache.nifi.processor.ProcessContext;
+import org.apache.nifi.processor.ProcessSession;
+import org.apache.nifi.processor.Relationship;
+import org.apache.nifi.processor.exception.ProcessException;
+import org.apache.nifi.processor.util.StandardValidators;
+import org.apache.tika.Tika;
+import org.apache.tika.exception.TikaException;
+
+import java.io.BufferedInputStream;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicReference;
+
+@Tags({"extract, text, pdf, word, excel, powerpoint, office"})
+@CapabilityDescription("Run Apache Tika text extraction to extra the text from 
supported binary file formats such as PDF " +
+        "and Microsoft Office files.")
+public class ExtractDocumentText extends AbstractProcessor {
+    private static final String TEXT_PLAIN = "text/plain";
+
+    public static final String FIELD_MAX_TEXT_LENGTH = "max-text-length";
+    public static final String FIELD_SUCCESS = "success";
+    public static final String FIELD_FAILURE = "failure";
+
+    public static final PropertyDescriptor MAX_TEXT_LENGTH = new 
PropertyDescriptor.Builder()
+            .name(FIELD_MAX_TEXT_LENGTH)
+            .displayName("Max Output Text Length")
+            .description("The maximum length of text to retrieve. This is used 
to limit memory usage for " +
+                    "dealing with large files. Specify -1 for unlimited 
length.")
+            
.required(false).defaultValue("-1").addValidator(StandardValidators.INTEGER_VALIDATOR)
+            
.expressionLanguageSupported(ExpressionLanguageScope.FLOWFILE_ATTRIBUTES).build();
+
+    public static final Relationship REL_SUCCESS = new 
Relationship.Builder().name(FIELD_SUCCESS)
+            .description("Content extraction successful").build();
+
+    public static final Relationship REL_FAILURE = new 
Relationship.Builder().name(FIELD_FAILURE)
+            .description("Content extraction failed").build();
+
+    private List<PropertyDescriptor> descriptors = 
Collections.unmodifiableList(Arrays.asList(MAX_TEXT_LENGTH));
+    private Set<Relationship> relationships = Collections.unmodifiableSet(new 
HashSet<>(Arrays.asList(REL_SUCCESS, REL_FAILURE)));
+
+    @Override
+    public Set<Relationship> getRelationships() {
+        return this.relationships;
+    }
+
+    @Override
+    public final List<PropertyDescriptor> getSupportedPropertyDescriptors() {
+        return descriptors;
+    }
+
+    @Override
+    public void onTrigger(final ProcessContext context, final ProcessSession 
session) throws ProcessException {
+        FlowFile flowFile = session.get();
+        if (flowFile == null) {
+            return;
+        }
+
+        final int maxTextLength = 
context.getProperty(MAX_TEXT_LENGTH).evaluateAttributeExpressions(flowFile).asInteger();
+        final String filename = flowFile.getAttribute("filename");
+
+        try {
+            final AtomicReference<String> type = new AtomicReference<>();
+            final AtomicReference<Boolean> exceptionThrown = new 
AtomicReference<>(false);
+
+            flowFile = session.write(flowFile, (inputStream, outputStream) -> {
+                BufferedInputStream buffStream = new 
BufferedInputStream(inputStream);
+                Tika tika = new Tika();
+                String text = "";
+                try {
+                    type.set(tika.detect(buffStream, filename));
+                    tika.setMaxStringLength(maxTextLength);
+                    text = tika.parseToString(buffStream);
+
+                } catch (TikaException e) {
+                    getLogger().error("Parsing failed ", e);
+                    exceptionThrown.set(true);
+                }
+
+                outputStream.write(text.getBytes());
+                buffStream.close();
+            });
+
+            if (exceptionThrown.get()) {
+                session.transfer(flowFile, REL_FAILURE);
+            } else {
+
+                Map<String, String> mimeAttrs = new HashMap<>();
+                mimeAttrs.put("mime.type", TEXT_PLAIN);
+                mimeAttrs.put("orig.mime.type", type.get());
+
+                flowFile = session.putAllAttributes(flowFile, mimeAttrs);
+                session.transfer(flowFile, REL_SUCCESS);
+            }
+        } catch (final Throwable t) {
+            getLogger().error("Unable to process ExtractTextProcessor file {} 
" +
+                    "{} failed to process due to {}; rolling back session", 
new Object[]{ t.getLocalizedMessage(), this, t});

Review comment:
       This log invocation can be streamlined to avoid duplicating the 
exception message, and the unnecessary `Object[]` wrapper:
   ```suggestion
               getLogger().error("Extraction Failed {}", flowFile, t);
   ```

##########
File path: 
nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/main/java/org/apache/nifi/processors/document/ExtractDocumentText.java
##########
@@ -0,0 +1,130 @@
+/*
+ * 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.
+ */
+package org.apache.nifi.processors.document;
+
+import org.apache.nifi.annotation.documentation.CapabilityDescription;
+import org.apache.nifi.annotation.documentation.Tags;
+import org.apache.nifi.components.PropertyDescriptor;
+import org.apache.nifi.expression.ExpressionLanguageScope;
+import org.apache.nifi.flowfile.FlowFile;
+import org.apache.nifi.processor.AbstractProcessor;
+import org.apache.nifi.processor.ProcessContext;
+import org.apache.nifi.processor.ProcessSession;
+import org.apache.nifi.processor.Relationship;
+import org.apache.nifi.processor.exception.ProcessException;
+import org.apache.nifi.processor.util.StandardValidators;
+import org.apache.tika.Tika;
+import org.apache.tika.exception.TikaException;
+
+import java.io.BufferedInputStream;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicReference;
+
+@Tags({"extract, text, pdf, word, excel, powerpoint, office"})
+@CapabilityDescription("Run Apache Tika text extraction to extra the text from 
supported binary file formats such as PDF " +
+        "and Microsoft Office files.")
+public class ExtractDocumentText extends AbstractProcessor {
+    private static final String TEXT_PLAIN = "text/plain";
+
+    public static final String FIELD_MAX_TEXT_LENGTH = "max-text-length";
+    public static final String FIELD_SUCCESS = "success";
+    public static final String FIELD_FAILURE = "failure";
+
+    public static final PropertyDescriptor MAX_TEXT_LENGTH = new 
PropertyDescriptor.Builder()
+            .name(FIELD_MAX_TEXT_LENGTH)
+            .displayName("Max Output Text Length")
+            .description("The maximum length of text to retrieve. This is used 
to limit memory usage for " +
+                    "dealing with large files. Specify -1 for unlimited 
length.")
+            
.required(false).defaultValue("-1").addValidator(StandardValidators.INTEGER_VALIDATOR)
+            
.expressionLanguageSupported(ExpressionLanguageScope.FLOWFILE_ATTRIBUTES).build();
+
+    public static final Relationship REL_SUCCESS = new 
Relationship.Builder().name(FIELD_SUCCESS)
+            .description("Content extraction successful").build();
+
+    public static final Relationship REL_FAILURE = new 
Relationship.Builder().name(FIELD_FAILURE)
+            .description("Content extraction failed").build();
+
+    private List<PropertyDescriptor> descriptors = 
Collections.unmodifiableList(Arrays.asList(MAX_TEXT_LENGTH));
+    private Set<Relationship> relationships = Collections.unmodifiableSet(new 
HashSet<>(Arrays.asList(REL_SUCCESS, REL_FAILURE)));
+
+    @Override
+    public Set<Relationship> getRelationships() {
+        return this.relationships;
+    }
+
+    @Override
+    public final List<PropertyDescriptor> getSupportedPropertyDescriptors() {
+        return descriptors;
+    }
+
+    @Override
+    public void onTrigger(final ProcessContext context, final ProcessSession 
session) throws ProcessException {
+        FlowFile flowFile = session.get();
+        if (flowFile == null) {
+            return;
+        }
+
+        final int maxTextLength = 
context.getProperty(MAX_TEXT_LENGTH).evaluateAttributeExpressions(flowFile).asInteger();
+        final String filename = flowFile.getAttribute("filename");
+
+        try {
+            final AtomicReference<String> type = new AtomicReference<>();
+            final AtomicReference<Boolean> exceptionThrown = new 
AtomicReference<>(false);
+
+            flowFile = session.write(flowFile, (inputStream, outputStream) -> {
+                BufferedInputStream buffStream = new 
BufferedInputStream(inputStream);
+                Tika tika = new Tika();
+                String text = "";
+                try {
+                    type.set(tika.detect(buffStream, filename));
+                    tika.setMaxStringLength(maxTextLength);
+                    text = tika.parseToString(buffStream);
+
+                } catch (TikaException e) {
+                    getLogger().error("Parsing failed ", e);

Review comment:
       This log should be removed as the outer exception handling should 
indicate the failure details.
   ```suggestion
   ```

##########
File path: 
nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/main/java/org/apache/nifi/processors/document/ExtractDocumentText.java
##########
@@ -0,0 +1,130 @@
+/*
+ * 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.
+ */
+package org.apache.nifi.processors.document;
+
+import org.apache.nifi.annotation.documentation.CapabilityDescription;
+import org.apache.nifi.annotation.documentation.Tags;
+import org.apache.nifi.components.PropertyDescriptor;
+import org.apache.nifi.expression.ExpressionLanguageScope;
+import org.apache.nifi.flowfile.FlowFile;
+import org.apache.nifi.processor.AbstractProcessor;
+import org.apache.nifi.processor.ProcessContext;
+import org.apache.nifi.processor.ProcessSession;
+import org.apache.nifi.processor.Relationship;
+import org.apache.nifi.processor.exception.ProcessException;
+import org.apache.nifi.processor.util.StandardValidators;
+import org.apache.tika.Tika;
+import org.apache.tika.exception.TikaException;
+
+import java.io.BufferedInputStream;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicReference;
+
+@Tags({"extract, text, pdf, word, excel, powerpoint, office"})
+@CapabilityDescription("Run Apache Tika text extraction to extra the text from 
supported binary file formats such as PDF " +
+        "and Microsoft Office files.")
+public class ExtractDocumentText extends AbstractProcessor {
+    private static final String TEXT_PLAIN = "text/plain";
+
+    public static final String FIELD_MAX_TEXT_LENGTH = "max-text-length";
+    public static final String FIELD_SUCCESS = "success";
+    public static final String FIELD_FAILURE = "failure";
+
+    public static final PropertyDescriptor MAX_TEXT_LENGTH = new 
PropertyDescriptor.Builder()
+            .name(FIELD_MAX_TEXT_LENGTH)
+            .displayName("Max Output Text Length")
+            .description("The maximum length of text to retrieve. This is used 
to limit memory usage for " +
+                    "dealing with large files. Specify -1 for unlimited 
length.")
+            
.required(false).defaultValue("-1").addValidator(StandardValidators.INTEGER_VALIDATOR)
+            
.expressionLanguageSupported(ExpressionLanguageScope.FLOWFILE_ATTRIBUTES).build();
+
+    public static final Relationship REL_SUCCESS = new 
Relationship.Builder().name(FIELD_SUCCESS)
+            .description("Content extraction successful").build();
+
+    public static final Relationship REL_FAILURE = new 
Relationship.Builder().name(FIELD_FAILURE)
+            .description("Content extraction failed").build();
+
+    private List<PropertyDescriptor> descriptors = 
Collections.unmodifiableList(Arrays.asList(MAX_TEXT_LENGTH));
+    private Set<Relationship> relationships = Collections.unmodifiableSet(new 
HashSet<>(Arrays.asList(REL_SUCCESS, REL_FAILURE)));
+
+    @Override
+    public Set<Relationship> getRelationships() {
+        return this.relationships;
+    }
+
+    @Override
+    public final List<PropertyDescriptor> getSupportedPropertyDescriptors() {
+        return descriptors;
+    }
+
+    @Override
+    public void onTrigger(final ProcessContext context, final ProcessSession 
session) throws ProcessException {
+        FlowFile flowFile = session.get();
+        if (flowFile == null) {
+            return;
+        }
+
+        final int maxTextLength = 
context.getProperty(MAX_TEXT_LENGTH).evaluateAttributeExpressions(flowFile).asInteger();
+        final String filename = flowFile.getAttribute("filename");
+
+        try {
+            final AtomicReference<String> type = new AtomicReference<>();
+            final AtomicReference<Boolean> exceptionThrown = new 
AtomicReference<>(false);

Review comment:
       This approach to capturing the exception should be refactored. The 
`TikaException` could be wrapped in a `ProcessException` and thrown in the 
write() callback, allowing the outer try-catch block to use standard exception 
handling.

##########
File path: 
nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/main/java/org/apache/nifi/processors/document/ExtractDocumentText.java
##########
@@ -0,0 +1,130 @@
+/*
+ * 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.
+ */
+package org.apache.nifi.processors.document;
+
+import org.apache.nifi.annotation.documentation.CapabilityDescription;
+import org.apache.nifi.annotation.documentation.Tags;
+import org.apache.nifi.components.PropertyDescriptor;
+import org.apache.nifi.expression.ExpressionLanguageScope;
+import org.apache.nifi.flowfile.FlowFile;
+import org.apache.nifi.processor.AbstractProcessor;
+import org.apache.nifi.processor.ProcessContext;
+import org.apache.nifi.processor.ProcessSession;
+import org.apache.nifi.processor.Relationship;
+import org.apache.nifi.processor.exception.ProcessException;
+import org.apache.nifi.processor.util.StandardValidators;
+import org.apache.tika.Tika;
+import org.apache.tika.exception.TikaException;
+
+import java.io.BufferedInputStream;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicReference;
+
+@Tags({"extract, text, pdf, word, excel, powerpoint, office"})
+@CapabilityDescription("Run Apache Tika text extraction to extra the text from 
supported binary file formats such as PDF " +
+        "and Microsoft Office files.")
+public class ExtractDocumentText extends AbstractProcessor {
+    private static final String TEXT_PLAIN = "text/plain";
+
+    public static final String FIELD_MAX_TEXT_LENGTH = "max-text-length";
+    public static final String FIELD_SUCCESS = "success";
+    public static final String FIELD_FAILURE = "failure";
+
+    public static final PropertyDescriptor MAX_TEXT_LENGTH = new 
PropertyDescriptor.Builder()
+            .name(FIELD_MAX_TEXT_LENGTH)
+            .displayName("Max Output Text Length")
+            .description("The maximum length of text to retrieve. This is used 
to limit memory usage for " +
+                    "dealing with large files. Specify -1 for unlimited 
length.")
+            
.required(false).defaultValue("-1").addValidator(StandardValidators.INTEGER_VALIDATOR)
+            
.expressionLanguageSupported(ExpressionLanguageScope.FLOWFILE_ATTRIBUTES).build();
+
+    public static final Relationship REL_SUCCESS = new 
Relationship.Builder().name(FIELD_SUCCESS)
+            .description("Content extraction successful").build();
+
+    public static final Relationship REL_FAILURE = new 
Relationship.Builder().name(FIELD_FAILURE)
+            .description("Content extraction failed").build();
+
+    private List<PropertyDescriptor> descriptors = 
Collections.unmodifiableList(Arrays.asList(MAX_TEXT_LENGTH));
+    private Set<Relationship> relationships = Collections.unmodifiableSet(new 
HashSet<>(Arrays.asList(REL_SUCCESS, REL_FAILURE)));
+
+    @Override
+    public Set<Relationship> getRelationships() {
+        return this.relationships;
+    }
+
+    @Override
+    public final List<PropertyDescriptor> getSupportedPropertyDescriptors() {
+        return descriptors;
+    }
+
+    @Override
+    public void onTrigger(final ProcessContext context, final ProcessSession 
session) throws ProcessException {
+        FlowFile flowFile = session.get();
+        if (flowFile == null) {
+            return;
+        }
+
+        final int maxTextLength = 
context.getProperty(MAX_TEXT_LENGTH).evaluateAttributeExpressions(flowFile).asInteger();
+        final String filename = flowFile.getAttribute("filename");
+
+        try {
+            final AtomicReference<String> type = new AtomicReference<>();
+            final AtomicReference<Boolean> exceptionThrown = new 
AtomicReference<>(false);
+
+            flowFile = session.write(flowFile, (inputStream, outputStream) -> {
+                BufferedInputStream buffStream = new 
BufferedInputStream(inputStream);
+                Tika tika = new Tika();
+                String text = "";
+                try {
+                    type.set(tika.detect(buffStream, filename));
+                    tika.setMaxStringLength(maxTextLength);
+                    text = tika.parseToString(buffStream);

Review comment:
       This parsing approach should be changed to use 
`Tika.parse(InputStream)`, which returns a `Reader`. Using something like 
`IOUtils.copy()` would allow streaming the parsed contents to the FlowFile 
OutputStream, as opposed to reading the entire string into memory.

##########
File path: 
nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/main/java/org/apache/nifi/processors/document/ExtractDocumentText.java
##########
@@ -0,0 +1,130 @@
+/*
+ * 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.
+ */
+package org.apache.nifi.processors.document;
+
+import org.apache.nifi.annotation.documentation.CapabilityDescription;
+import org.apache.nifi.annotation.documentation.Tags;
+import org.apache.nifi.components.PropertyDescriptor;
+import org.apache.nifi.expression.ExpressionLanguageScope;
+import org.apache.nifi.flowfile.FlowFile;
+import org.apache.nifi.processor.AbstractProcessor;
+import org.apache.nifi.processor.ProcessContext;
+import org.apache.nifi.processor.ProcessSession;
+import org.apache.nifi.processor.Relationship;
+import org.apache.nifi.processor.exception.ProcessException;
+import org.apache.nifi.processor.util.StandardValidators;
+import org.apache.tika.Tika;
+import org.apache.tika.exception.TikaException;
+
+import java.io.BufferedInputStream;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicReference;
+
+@Tags({"extract, text, pdf, word, excel, powerpoint, office"})
+@CapabilityDescription("Run Apache Tika text extraction to extra the text from 
supported binary file formats such as PDF " +
+        "and Microsoft Office files.")
+public class ExtractDocumentText extends AbstractProcessor {
+    private static final String TEXT_PLAIN = "text/plain";
+
+    public static final String FIELD_MAX_TEXT_LENGTH = "max-text-length";
+    public static final String FIELD_SUCCESS = "success";
+    public static final String FIELD_FAILURE = "failure";
+
+    public static final PropertyDescriptor MAX_TEXT_LENGTH = new 
PropertyDescriptor.Builder()
+            .name(FIELD_MAX_TEXT_LENGTH)
+            .displayName("Max Output Text Length")
+            .description("The maximum length of text to retrieve. This is used 
to limit memory usage for " +
+                    "dealing with large files. Specify -1 for unlimited 
length.")
+            
.required(false).defaultValue("-1").addValidator(StandardValidators.INTEGER_VALIDATOR)
+            
.expressionLanguageSupported(ExpressionLanguageScope.FLOWFILE_ATTRIBUTES).build();

Review comment:
       Changing to use `Tika.parse(InputStream)` should remove the need for 
this property.

##########
File path: 
nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/test/java/org/apache/nifi/processors/document/ExtractDocumentTextTest.java
##########
@@ -0,0 +1,258 @@
+/*
+ * 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.
+ */
+package org.apache.nifi.processors.document;
+
+import org.apache.nifi.flowfile.attributes.CoreAttributes;
+import org.apache.nifi.util.MockFlowFile;
+import org.apache.nifi.util.TestRunner;
+import org.apache.nifi.util.TestRunners;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.UnsupportedEncodingException;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+
+public class ExtractDocumentTextTest {
+
+    private static final String ORIG_MIME_TYPE_ATTR = "orig.mime.type";
+    private TestRunner testRunner;
+
+    @BeforeEach
+    public void init() {
+        testRunner = TestRunners.newTestRunner(ExtractDocumentText.class);
+    }
+
+    @Test
+    @DisplayName("Should support PDF types without exceptions being thrown")
+    public void processorShouldSupportPDF() {
+        try {
+            final String filename = "simple.pdf";
+            MockFlowFile flowFile = testRunner.enqueue(new 
FileInputStream("src/test/resources/" + filename));
+            Map<String, String> attrs = Collections.singletonMap("filename", 
filename);
+            flowFile.putAttributes(attrs);
+        } catch (FileNotFoundException e) {
+
+        }
+
+        testRunner.assertValid();
+        testRunner.run();
+        testRunner.assertTransferCount(ExtractDocumentText.REL_FAILURE, 0);
+
+        List<MockFlowFile> successFiles = 
testRunner.getFlowFilesForRelationship(ExtractDocumentText.REL_SUCCESS);
+        for (MockFlowFile mockFile : successFiles) {
+            try {
+                String result = new String(mockFile.toByteArray(), "UTF-8");
+                String trimmedResult = result.trim();
+                assertTrue(trimmedResult.startsWith("A Simple PDF File"));
+            } catch (UnsupportedEncodingException e) {
+            }
+        }
+    }
+
+    @Test
+    @DisplayName("Should support MS Word DOC types without throwing 
exceptions")
+    public void processorShouldSupportDOC() {
+        try {
+            final String filename = "simple.doc";
+            MockFlowFile flowFile = testRunner.enqueue(new 
FileInputStream("src/test/resources/" + filename));
+            Map<String, String> attrs = Collections.singletonMap("filename", 
filename);
+            flowFile.putAttributes(attrs);
+        } catch (FileNotFoundException e) {
+
+        }
+
+        testRunner.assertValid();
+        testRunner.run();
+        testRunner.assertTransferCount(ExtractDocumentText.REL_FAILURE, 0);
+
+        List<MockFlowFile> successFiles = 
testRunner.getFlowFilesForRelationship(ExtractDocumentText.REL_SUCCESS);
+        for (MockFlowFile mockFile : successFiles) {
+            try {
+                String result = new String(mockFile.toByteArray(), "UTF-8");
+                String trimmedResult = result.trim();
+                assertTrue(trimmedResult.startsWith("A Simple WORD DOC File"));
+            } catch (UnsupportedEncodingException e) {
+            }
+        }
+    }
+
+    @Test
+    @DisplayName("Should support MS Word DOCX types without exception")
+    public void processorShouldSupportDOCX() {
+        try {
+            final String filename = "simple.docx";
+            MockFlowFile flowFile = testRunner.enqueue(new 
FileInputStream("src/test/resources/" + filename));
+            Map<String, String> attrs = Collections.singletonMap("filename", 
filename);
+            flowFile.putAttributes(attrs);
+        } catch (FileNotFoundException e) {
+
+        }
+
+        testRunner.assertValid();
+        testRunner.run();
+        testRunner.assertTransferCount(ExtractDocumentText.REL_FAILURE, 0);
+
+        List<MockFlowFile> successFiles = 
testRunner.getFlowFilesForRelationship(ExtractDocumentText.REL_SUCCESS);
+        for (MockFlowFile mockFile : successFiles) {
+            try {
+                String result = new String(mockFile.toByteArray(), "UTF-8");
+                String trimmedResult = result.trim();
+                assertTrue(trimmedResult.startsWith("A Simple WORD DOCX 
File"));
+            } catch (UnsupportedEncodingException e) {
+            }

Review comment:
       Most of the test methods appear to follow this approach.  In general, 
empty catch blocks should be avoided, and for test methods, the exception types 
can be added to the signature.




-- 
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-unsubscr...@nifi.apache.org

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


Reply via email to