NIFI-632 generating a test nar which loads a resource during its init method
Project: http://git-wip-us.apache.org/repos/asf/incubator-nifi/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-nifi/commit/b4ee97c7 Tree: http://git-wip-us.apache.org/repos/asf/incubator-nifi/tree/b4ee97c7 Diff: http://git-wip-us.apache.org/repos/asf/incubator-nifi/diff/b4ee97c7 Branch: refs/heads/NIFI-680 Commit: b4ee97c77135379bb1f659dced51e790050c5eb4 Parents: 30055dd Author: danbress <dbr...@onyxconsults.com> Authored: Sat May 30 11:38:55 2015 -0400 Committer: danbress <dbr...@onyxconsults.com> Committed: Tue Jun 9 21:34:21 2015 -0400 ---------------------------------------------------------------------- .../nifi-nifi-example-nar/pom.xml | 36 +++++++ .../nifi-nifi-example-processors/pom.xml | 70 +++++++++++++ .../nifi/processors/WriteResourceToStream.java | 102 +++++++++++++++++++ .../org.apache.nifi.processor.Processor | 15 +++ .../src/main/resources/file.txt | 1 + .../processors/WriteResourceToStreamTest.java | 47 +++++++++ nifi/nifi-external/nifi-example-bundle/pom.xml | 33 ++++++ nifi/nifi-external/pom.xml | 2 + 8 files changed, 306 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/b4ee97c7/nifi/nifi-external/nifi-example-bundle/nifi-nifi-example-nar/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 0000000..55df946 --- /dev/null +++ b/nifi/nifi-external/nifi-example-bundle/nifi-nifi-example-nar/pom.xml @@ -0,0 +1,36 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.nifi</groupId> + <artifactId>nifi-example-bundle</artifactId> + <version>0.1.1-incubating-SNAPSHOT</version> + </parent> + + <artifactId>nifi-nifi-example-nar</artifactId> + <packaging>nar</packaging> + + <dependencies> + <dependency> + <groupId>org.apache.nifi</groupId> + <artifactId>nifi-nifi-example-processors</artifactId> + <version>${project.version}</version> + </dependency> + </dependencies> + +</project> http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/b4ee97c7/nifi/nifi-external/nifi-example-bundle/nifi-nifi-example-processors/pom.xml ---------------------------------------------------------------------- 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 0000000..b4fbc85 --- /dev/null +++ b/nifi/nifi-external/nifi-example-bundle/nifi-nifi-example-processors/pom.xml @@ -0,0 +1,70 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.nifi</groupId> + <artifactId>nifi-example-bundle</artifactId> + <version>0.1.1-incubating-SNAPSHOT</version> + </parent> + + <artifactId>nifi-nifi-example-processors</artifactId> + <packaging>jar</packaging> + + <dependencies> + <dependency> + <groupId>org.apache.nifi</groupId> + <artifactId>nifi-api</artifactId> + </dependency> + <dependency> + <groupId>org.apache.nifi</groupId> + <artifactId>nifi-processor-utils</artifactId> + </dependency> + <dependency> + <groupId>org.apache.nifi</groupId> + <artifactId>nifi-mock</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-simple</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>commons-io</groupId> + <artifactId>commons-io</artifactId> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + </dependencies> + <build> + <plugins> + <plugin> + <groupId>org.apache.rat</groupId> + <artifactId>apache-rat-plugin</artifactId> + <configuration> + <excludes> + <exclude>src/main/resources/file.txt</exclude> + </excludes> + </configuration> + </plugin> + </plugins> + </build> +</project> http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/b4ee97c7/nifi/nifi-external/nifi-example-bundle/nifi-nifi-example-processors/src/main/java/org/apache/nifi/processors/WriteResourceToStream.java ---------------------------------------------------------------------- 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 0000000..7849128 --- /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<Relationship> relationships; + + private String resourceData; + + @Override + protected void init(final ProcessorInitializationContext context) { + + final Set<Relationship> relationships = new HashSet<Relationship>(); + 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<Relationship> 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); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/b4ee97c7/nifi/nifi-external/nifi-example-bundle/nifi-nifi-example-processors/src/main/resources/META-INF/services/org.apache.nifi.processor.Processor ---------------------------------------------------------------------- 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 0000000..9448541 --- /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 http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/b4ee97c7/nifi/nifi-external/nifi-example-bundle/nifi-nifi-example-processors/src/main/resources/file.txt ---------------------------------------------------------------------- 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 0000000..f0e1d6c --- /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 http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/b4ee97c7/nifi/nifi-external/nifi-example-bundle/nifi-nifi-example-processors/src/test/java/org/apache/nifi/processors/WriteResourceToStreamTest.java ---------------------------------------------------------------------- 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 0000000..7a84df5 --- /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); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/b4ee97c7/nifi/nifi-external/nifi-example-bundle/pom.xml ---------------------------------------------------------------------- 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 0000000..f1e4753 --- /dev/null +++ b/nifi/nifi-external/nifi-example-bundle/pom.xml @@ -0,0 +1,33 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.nifi</groupId> + <artifactId>nifi-nar-bundles</artifactId> + <version>0.1.1-incubating-SNAPSHOT</version> + </parent> + + <artifactId>nifi-example-bundle</artifactId> + <packaging>pom</packaging> + + <modules> + <module>nifi-nifi-example-processors</module> + <module>nifi-nifi-example-nar</module> + </modules> + +</project> http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/b4ee97c7/nifi/nifi-external/pom.xml ---------------------------------------------------------------------- diff --git a/nifi/nifi-external/pom.xml b/nifi/nifi-external/pom.xml index 0c70c4a..8a2e585 100644 --- a/nifi/nifi-external/pom.xml +++ b/nifi/nifi-external/pom.xml @@ -26,5 +26,7 @@ <modules> <module>nifi-spark-receiver</module> <module>nifi-storm-spout</module> + <module>nifi-example-bundle</module> </modules> </project> +