From b4ee97c77135379bb1f659dced51e790050c5eb4 Mon Sep 17 00:00:00 2001 From: danbress Date: Sat, 30 May 2015 11:38:55 -0400 Subject: [PATCH] NIFI-632 generating a test nar which loads a resource during its init method --- .../nifi-nifi-example-nar/pom.xml | 36 +++++++ .../nifi-nifi-example-processors/pom.xml | 70 ++++++++++++ .../processors/WriteResourceToStream.java | 102 ++++++++++++++++++ .../org.apache.nifi.processor.Processor | 15 +++ .../src/main/resources/file.txt | 1 + .../processors/WriteResourceToStreamTest.java | 47 ++++++++ .../nifi-external/nifi-example-bundle/pom.xml | 33 ++++++ nifi/nifi-external/pom.xml | 2 + 8 files changed, 306 insertions(+) create mode 100644 nifi/nifi-external/nifi-example-bundle/nifi-nifi-example-nar/pom.xml create mode 100644 nifi/nifi-external/nifi-example-bundle/nifi-nifi-example-processors/pom.xml create mode 100644 nifi/nifi-external/nifi-example-bundle/nifi-nifi-example-processors/src/main/java/org/apache/nifi/processors/WriteResourceToStream.java create mode 100644 nifi/nifi-external/nifi-example-bundle/nifi-nifi-example-processors/src/main/resources/META-INF/services/org.apache.nifi.processor.Processor create mode 100644 nifi/nifi-external/nifi-example-bundle/nifi-nifi-example-processors/src/main/resources/file.txt create mode 100644 nifi/nifi-external/nifi-example-bundle/nifi-nifi-example-processors/src/test/java/org/apache/nifi/processors/WriteResourceToStreamTest.java create mode 100644 nifi/nifi-external/nifi-example-bundle/pom.xml diff --git a/nifi/nifi-external/nifi-example-bundle/nifi-nifi-example-nar/pom.xml b/nifi/nifi-external/nifi-example-bundle/nifi-nifi-example-nar/pom.xml new file mode 100644 index 0000000000..55df946bef --- /dev/null +++ b/nifi/nifi-external/nifi-example-bundle/nifi-nifi-example-nar/pom.xml @@ -0,0 +1,36 @@ + + + + 4.0.0 + + + org.apache.nifi + nifi-example-bundle + 0.1.1-incubating-SNAPSHOT + + + nifi-nifi-example-nar + nar + + + + org.apache.nifi + nifi-nifi-example-processors + ${project.version} + + + + diff --git a/nifi/nifi-external/nifi-example-bundle/nifi-nifi-example-processors/pom.xml b/nifi/nifi-external/nifi-example-bundle/nifi-nifi-example-processors/pom.xml new file mode 100644 index 0000000000..b4fbc85172 --- /dev/null +++ b/nifi/nifi-external/nifi-example-bundle/nifi-nifi-example-processors/pom.xml @@ -0,0 +1,70 @@ + + + + 4.0.0 + + + org.apache.nifi + nifi-example-bundle + 0.1.1-incubating-SNAPSHOT + + + nifi-nifi-example-processors + jar + + + + org.apache.nifi + nifi-api + + + org.apache.nifi + nifi-processor-utils + + + org.apache.nifi + nifi-mock + test + + + org.slf4j + slf4j-simple + test + + + commons-io + commons-io + + + junit + junit + test + + + + + + org.apache.rat + apache-rat-plugin + + + src/main/resources/file.txt + + + + + + diff --git a/nifi/nifi-external/nifi-example-bundle/nifi-nifi-example-processors/src/main/java/org/apache/nifi/processors/WriteResourceToStream.java b/nifi/nifi-external/nifi-example-bundle/nifi-nifi-example-processors/src/main/java/org/apache/nifi/processors/WriteResourceToStream.java new file mode 100644 index 0000000000..7849128b7e --- /dev/null +++ b/nifi/nifi-external/nifi-example-bundle/nifi-nifi-example-processors/src/main/java/org/apache/nifi/processors/WriteResourceToStream.java @@ -0,0 +1,102 @@ +/* + * 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; + +import java.io.IOException; +import java.io.OutputStream; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + +import org.apache.commons.io.IOUtils; +import org.apache.nifi.annotation.documentation.CapabilityDescription; +import org.apache.nifi.annotation.documentation.Tags; +import org.apache.nifi.annotation.lifecycle.OnScheduled; +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.ProcessorInitializationContext; +import org.apache.nifi.processor.Relationship; +import org.apache.nifi.processor.exception.ProcessException; +import org.apache.nifi.processor.io.OutputStreamCallback; + +@Tags({ "example" }) +@CapabilityDescription("Provide a description") +public class WriteResourceToStream extends AbstractProcessor { + + public static final Relationship REL_SUCCESS = new Relationship.Builder() + .name("success") + .description("files that were successfully processed").build(); + public static final Relationship REL_FAILURE = new Relationship.Builder() + .name("success") + .description("files that were notsuccessfully processed").build(); + + private Set relationships; + + private String resourceData; + + @Override + protected void init(final ProcessorInitializationContext context) { + + final Set relationships = new HashSet(); + relationships.add(REL_SUCCESS); + relationships.add(REL_FAILURE); + this.relationships = Collections.unmodifiableSet(relationships); + + try { + this.resourceData = IOUtils.toString(Thread.currentThread() + .getContextClassLoader().getResourceAsStream("file.txt")); + } catch (IOException e) { + throw new RuntimeException("Unable to load resources", e); + } + } + + @Override + public Set getRelationships() { + return this.relationships; + } + + @OnScheduled + public void onScheduled(final ProcessContext context) { + + } + + @Override + public void onTrigger(final ProcessContext context, + final ProcessSession session) throws ProcessException { + FlowFile flowFile = session.get(); + if (flowFile == null) { + return; + } + + try { + flowFile = session.write(flowFile, new OutputStreamCallback() { + + @Override + public void process(OutputStream out) throws IOException { + IOUtils.write(resourceData, out); + + } + }); + session.transfer(flowFile, REL_SUCCESS); + } catch (ProcessException ex) { + getLogger().error("Unable to process", ex); + session.transfer(flowFile, REL_FAILURE); + } + } +} diff --git a/nifi/nifi-external/nifi-example-bundle/nifi-nifi-example-processors/src/main/resources/META-INF/services/org.apache.nifi.processor.Processor b/nifi/nifi-external/nifi-example-bundle/nifi-nifi-example-processors/src/main/resources/META-INF/services/org.apache.nifi.processor.Processor new file mode 100644 index 0000000000..94485417f9 --- /dev/null +++ b/nifi/nifi-external/nifi-example-bundle/nifi-nifi-example-processors/src/main/resources/META-INF/services/org.apache.nifi.processor.Processor @@ -0,0 +1,15 @@ +# 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. +org.apache.nifi.processors.WriteResourceToStream \ No newline at end of file diff --git a/nifi/nifi-external/nifi-example-bundle/nifi-nifi-example-processors/src/main/resources/file.txt b/nifi/nifi-external/nifi-example-bundle/nifi-nifi-example-processors/src/main/resources/file.txt new file mode 100644 index 0000000000..f0e1d6c9e3 --- /dev/null +++ b/nifi/nifi-external/nifi-example-bundle/nifi-nifi-example-processors/src/main/resources/file.txt @@ -0,0 +1 @@ +this came from a resource \ No newline at end of file diff --git a/nifi/nifi-external/nifi-example-bundle/nifi-nifi-example-processors/src/test/java/org/apache/nifi/processors/WriteResourceToStreamTest.java b/nifi/nifi-external/nifi-example-bundle/nifi-nifi-example-processors/src/test/java/org/apache/nifi/processors/WriteResourceToStreamTest.java new file mode 100644 index 0000000000..7a84df58a2 --- /dev/null +++ b/nifi/nifi-external/nifi-example-bundle/nifi-nifi-example-processors/src/test/java/org/apache/nifi/processors/WriteResourceToStreamTest.java @@ -0,0 +1,47 @@ +/* + * 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; + +import org.junit.Assert; + +import org.apache.nifi.util.TestRunner; +import org.apache.nifi.util.TestRunners; +import org.junit.Before; +import org.junit.Test; + +public class WriteResourceToStreamTest { + + private TestRunner testRunner; + + @Before + public void init() { + testRunner = TestRunners.newTestRunner(WriteResourceToStream.class); + } + + @Test + public void testProcessor() { + testRunner.enqueue(new byte[] { 1, 2, 3, 4, 5 }); + testRunner.run(); + testRunner.assertAllFlowFilesTransferred(WriteResourceToStream.REL_SUCCESS, 1); + final byte[] data = testRunner + .getFlowFilesForRelationship(WriteResourceToStream.REL_SUCCESS).get(0) + .toByteArray(); + final String stringData = new String(data); + Assert.assertEquals("this came from a resource", stringData); + } + +} diff --git a/nifi/nifi-external/nifi-example-bundle/pom.xml b/nifi/nifi-external/nifi-example-bundle/pom.xml new file mode 100644 index 0000000000..f1e47531c3 --- /dev/null +++ b/nifi/nifi-external/nifi-example-bundle/pom.xml @@ -0,0 +1,33 @@ + + + + 4.0.0 + + + org.apache.nifi + nifi-nar-bundles + 0.1.1-incubating-SNAPSHOT + + + nifi-example-bundle + pom + + + nifi-nifi-example-processors + nifi-nifi-example-nar + + + diff --git a/nifi/nifi-external/pom.xml b/nifi/nifi-external/pom.xml index 0c70c4ac60..8a2e585c8b 100644 --- a/nifi/nifi-external/pom.xml +++ b/nifi/nifi-external/pom.xml @@ -26,5 +26,7 @@ nifi-spark-receiver nifi-storm-spout + nifi-example-bundle +