Skip to content

Commit

Permalink
Incremented version, added support for auto-imports and modules
Browse files Browse the repository at this point in the history
  • Loading branch information
mattyb149 committed Jun 9, 2016
1 parent cc14edf commit 0ada4c9
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 68 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

.gradle/
.idea/
*.ipr
*.iws
*.iml
build/

# Mobile Tools for Java (J2ME)
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ java -cp nifi-script-tester-<version>-all.jar [options] script_file

-input=<directory> Send each file in the specified directory as a flow file to the script

-modules=<paths> Comma-separated list of paths (files or directories) containing script modules/JARs



## Build
Expand All @@ -43,15 +45,15 @@ The JAR is available on Bintray at https://bintray.com/mattyb149/maven/nifi-scri
<dependency>
<groupId>mattyb149</groupId>
<artifactId>nifi-script-tester</artifactId>
<version>1.0</version>
<version>1.1</version>
<type>jar</type>
<classifier>all</classifier>
</dependency>
```

### Gradle
```gradle
compile(group: 'mattyb149', name: 'nifi-script-tester', version: '1.0', ext: 'jar', classifier: 'all')
compile(group: 'mattyb149', name: 'nifi-script-tester', version: '1.1', ext: 'jar', classifier: 'all')
```

## License
Expand Down
8 changes: 7 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*
******************************************************************************/
plugins {
id 'application'
id 'com.jfrog.bintray' version '1.6'
id 'java'
id 'maven'
Expand All @@ -22,7 +23,7 @@ plugins {
id 'com.github.johnrengelman.shadow' version '1.2.3'
}

version = '1.0'
version = '1.1'
description = "A project to create a stub/mock environment for testing ExecuteScript processors"
group = 'mattyb149'
status = 'RELEASE'
Expand All @@ -37,13 +38,18 @@ repositories {
dependencies {
compile 'org.apache.nifi:nifi-api:0.6.1'
compile 'org.apache.nifi:nifi-mock:0.6.1'
testCompile 'junit:junit:4.12'
runtime 'commons-io:commons-io:2.4'
runtime 'junit:junit:4.12'
runtime 'org.slf4j:slf4j-log4j12:1.7.14'

// script engine(s)
runtime 'org.codehaus.groovy:groovy-all:2.4.6'
}

mainClassName = 'nifi.ScriptRunner'


// custom tasks for creating source/javadoc jars
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/nifi/ScriptRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,15 @@ public class ScriptRunner {
private static boolean outputFailure = false;
private static String inputFileDir = "";
private static String scriptPath = "";
private static String modulePaths = "";
private static int numFiles = 0;

public static void main(String[] args) {


// Expecting a single arg with the filename, will figure out language from file extension
if (args == null || args.length < 1) {
System.err.println("Usage: java -cp nifi-script-tester.jar [options] <script file>");
System.err.println("Usage: java -jar nifi-script-tester-<version>-all.jar [options] <script file>");
System.err.println(" Where options may include:");
System.err.println(" -success Output information about flow files that were transferred to the success relationship. Defaults to true");
System.err.println(" -failure Output information about flow files that were transferred to the failure relationship. Defaults to false");
Expand All @@ -67,6 +68,7 @@ public static void main(String[] args) {
System.err.println(" -all-rels Output information about flow files that were transferred to any relationship. Defaults to false");
System.err.println(" -all Output content, attributes, etc. about flow files that were transferred to any relationship. Defaults to false");
System.err.println(" -input=<directory> Send each file in the specified directory as a flow file to the script");
System.err.println(" -modules=<paths> Comma-separated list of paths (files or directories) containing script modules/JARs");
System.exit(1);
}

Expand Down Expand Up @@ -100,6 +102,8 @@ public static void main(String[] args) {
outputAttributes = true;
} else if (arg.startsWith("-input=")) {
inputFileDir = arg.substring("-input=".length());
} else if (arg.startsWith("-modules=")) {
modulePaths = arg.substring("-modules=".length());
} else {
scriptPath = arg;
}
Expand Down Expand Up @@ -131,7 +135,7 @@ public static void main(String[] args) {
runner.setValidateExpressionUsage(false);
runner.setProperty(ExecuteScript.SCRIPT_ENGINE, scriptEngineName);
runner.setProperty(ExecuteScript.SCRIPT_FILE, scriptPath);
runner.setProperty(ExecuteScript.MODULES, "src/test/resources/modules");
runner.setProperty(ExecuteScript.MODULES, modulePaths.isEmpty() ? "src/test/resources/modules" : modulePaths);

runner.assertValid();
try {
Expand Down
63 changes: 0 additions & 63 deletions src/main/java/nifi/script/impl/JythonScriptEngineConfigurator.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# 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.

nifi.script.impl.GroovyScriptEngineConfigurator
nifi.script.impl.JavascriptScriptEngineConfigurator
7 changes: 7 additions & 0 deletions src/test/java/nifi/ScriptRunnerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.InputStream;

/**
Expand Down Expand Up @@ -74,4 +75,10 @@ public void testOutputAllWithInputDir() throws Exception {
ScriptRunner.main(new String[]{"-all", "-input=src/test/resources/input_files", "src/test/resources/test_read_input.groovy"});
}

@Test
public void testReadWrite() throws Exception {
System.setIn(new FileInputStream("src/test/resources/input_files/jolt.json"));
ScriptRunner.main(new String[]{"-all", "src/test/resources/test_json2json.groovy"});
}

}
13 changes: 13 additions & 0 deletions src/test/resources/input_files/jolt.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"rating": {
"primary": {
"value": 3
},
"quality": {
"value": 3
},
"metric": {
"value": 6
}
}
}
31 changes: 31 additions & 0 deletions src/test/resources/test_json2json.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import org.apache.commons.io.IOUtils
import java.nio.charset.*

def flowFile = session.get();
if (flowFile == null) {
return;
}
def slurper = new groovy.json.JsonSlurper()

flowFile = session.write(flowFile,
{ inputStream, outputStream ->
def text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
def obj = slurper.parseText(text)
def builder = new groovy.json.JsonBuilder()
builder.call {
'Range' 5
'Rating' "${obj.rating.primary.value}"
'SecondaryRatings' {
obj.rating.findAll {it.key != "primary"}.each {k,v ->
"$k" {
'Id' "$k"
'Range' 5
'Value' v.value
}
}
}
}
outputStream.write(builder.toPrettyString().getBytes(StandardCharsets.UTF_8))
} as StreamCallback)
flowFile = session.putAttribute(flowFile, "filename", flowFile.getAttribute('filename').tokenize('.')[0]+'_translated.json')
session.transfer(flowFile, REL_SUCCESS)

0 comments on commit 0ada4c9

Please sign in to comment.