Github user JPercivall commented on a diff in the pull request: https://github.com/apache/nifi/pull/252#discussion_r67221005 --- Diff: nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/test/java/org/apache/nifi/processors/media/TestExtractMediaMetadata.java --- @@ -0,0 +1,411 @@ +/* + * 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.media; + +import org.apache.nifi.components.PropertyDescriptor; +import org.apache.nifi.processor.ProcessContext; +import org.apache.nifi.processor.Relationship; +import org.apache.nifi.util.MockFlowFile; +import org.apache.nifi.util.TestRunner; +import org.apache.nifi.util.TestRunners; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.Set; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class TestExtractMediaMetadata { + + @Test + public void testProperties() { + final TestRunner runner = TestRunners.newTestRunner(new ExtractMediaMetadata()); + ProcessContext context = runner.getProcessContext(); + Map<PropertyDescriptor, String> propertyValues = context.getProperties(); + assertEquals(5, propertyValues.size()); + } + + @Test + public void testRelationShips() { + final TestRunner runner = TestRunners.newTestRunner(new ExtractMediaMetadata()); + ProcessContext context = runner.getProcessContext(); + Set<Relationship> relationships = context.getAvailableRelationships(); + assertEquals(2, relationships.size()); + assertTrue(relationships.contains(ExtractMediaMetadata.SUCCESS)); + assertTrue(relationships.contains(ExtractMediaMetadata.FAILURE)); + } + + @Test + public void testTextBytes() throws IOException { + final TestRunner runner = TestRunners.newTestRunner(new ExtractMediaMetadata()); + runner.setProperty(ExtractMediaMetadata.FILENAME_FILTER, ".*[tTxXtT]"); + runner.setProperty(ExtractMediaMetadata.MIME_TYPE_FILTER, "text/.*"); + runner.setProperty(ExtractMediaMetadata.METADATA_KEY_FILTER, ""); + runner.setProperty(ExtractMediaMetadata.METADATA_KEY_PREFIX, "txt."); + runner.assertValid(); + + final Map<String, String> attrs = new HashMap<>(); + attrs.put("filename", "test1.txt"); + runner.enqueue("test1".getBytes(), attrs); + runner.run(); + + runner.assertAllFlowFilesTransferred(ExtractMediaMetadata.SUCCESS, 1); + runner.assertTransferCount(ExtractMediaMetadata.FAILURE, 0); + + final List<MockFlowFile> successFiles = runner.getFlowFilesForRelationship(ExtractMediaMetadata.SUCCESS); + MockFlowFile flowFile0 = successFiles.get(0); + flowFile0.assertAttributeExists("filename"); + flowFile0.assertAttributeEquals("filename", "test1.txt"); + flowFile0.assertAttributeExists("txt.Content-Type"); + assertTrue(flowFile0.getAttribute("txt.Content-Type").startsWith("text/plain")); + flowFile0.assertAttributeExists("txt.X-Parsed-By"); + assertTrue(flowFile0.getAttribute("txt.X-Parsed-By").contains("org.apache.tika.parser.DefaultParser")); + assertTrue(flowFile0.getAttribute("txt.X-Parsed-By").contains("org.apache.tika.parser.txt.TXTParser")); + flowFile0.assertAttributeExists("txt.Content-Encoding"); + flowFile0.assertAttributeEquals("txt.Content-Encoding", "ISO-8859-1"); + flowFile0.assertContentEquals("test1".getBytes("UTF-8")); + } + + @Test + public void testNoFlowFile() throws IOException { + final TestRunner runner = TestRunners.newTestRunner(new ExtractMediaMetadata()); + runner.setProperty(ExtractMediaMetadata.FILENAME_FILTER, ".*[tTxXtT]"); + runner.setProperty(ExtractMediaMetadata.MIME_TYPE_FILTER, "text/.*"); + runner.setProperty(ExtractMediaMetadata.METADATA_KEY_FILTER, ""); + runner.setProperty(ExtractMediaMetadata.METADATA_KEY_PREFIX, "txt."); + runner.assertValid(); + + runner.run(); + + runner.assertAllFlowFilesTransferred(ExtractMediaMetadata.SUCCESS, 0); + runner.assertTransferCount(ExtractMediaMetadata.FAILURE, 0); + } + + @Test + public void testTextFile() throws IOException { + final TestRunner runner = TestRunners.newTestRunner(new ExtractMediaMetadata()); + runner.setProperty(ExtractMediaMetadata.FILENAME_FILTER, ".*[tTxXtT]"); + runner.setProperty(ExtractMediaMetadata.MIME_TYPE_FILTER, "text/.*"); + runner.setProperty(ExtractMediaMetadata.METADATA_KEY_FILTER, ""); + runner.setProperty(ExtractMediaMetadata.METADATA_KEY_PREFIX, "txt."); + runner.assertValid(); + + runner.enqueue(new File("target/test-classes/notImage.txt").toPath()); + runner.run(); + + runner.assertAllFlowFilesTransferred(ExtractMediaMetadata.SUCCESS, 1); + runner.assertTransferCount(ExtractMediaMetadata.FAILURE, 0); + + final List<MockFlowFile> successFiles = runner.getFlowFilesForRelationship(ExtractMediaMetadata.SUCCESS); + MockFlowFile flowFile0 = successFiles.get(0); + flowFile0.assertAttributeExists("filename"); + flowFile0.assertAttributeEquals("filename", "notImage.txt"); + flowFile0.assertAttributeExists("txt.Content-Type"); + assertTrue(flowFile0.getAttribute("txt.Content-Type").startsWith("text/plain")); + flowFile0.assertAttributeExists("txt.X-Parsed-By"); + assertTrue(flowFile0.getAttribute("txt.X-Parsed-By").contains("org.apache.tika.parser.DefaultParser")); + assertTrue(flowFile0.getAttribute("txt.X-Parsed-By").contains("org.apache.tika.parser.txt.TXTParser")); + flowFile0.assertAttributeExists("txt.Content-Encoding"); + flowFile0.assertAttributeEquals("txt.Content-Encoding", "ISO-8859-1"); + flowFile0.assertContentEquals("This file is not an image and is used for testing the image metadata extractor.".getBytes("UTF-8")); + } + + @Test + public void testJunkBytes() throws IOException { + final TestRunner runner = TestRunners.newTestRunner(new ExtractMediaMetadata()); + runner.setProperty(ExtractMediaMetadata.FILENAME_FILTER, ".*"); + runner.setProperty(ExtractMediaMetadata.METADATA_KEY_FILTER, ""); + runner.setProperty(ExtractMediaMetadata.METADATA_KEY_PREFIX, "junk."); + runner.assertValid(); + + final Map<String, String> attrs = new HashMap<>(); + attrs.put("filename", "junk"); + Random random = new Random(); + byte[] bytes = new byte[2048]; --- End diff -- Random is not used and the byte[] will always be initialized to be full of zeros[1]. Random should be used to fill it like so: `random.nextBytes(bytes);` [1] http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---