Commit 7bdf265f authored by Phillip Grenier's avatar Phillip Grenier
Browse files

Add test case and change name of meta-inf file

parent 63ac934e
......@@ -34,14 +34,23 @@
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-mock</artifactId>
<version>${nifi.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
......@@ -52,6 +61,11 @@
<version>1.0.0-incubating</version>
<extensions>true</extensions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.15</version>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
......@@ -5,13 +5,16 @@
*/
package rocks.nifi.examples.processors;
import com.jayway.jsonpath.JsonPath;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import org.apache.commons.io.IOUtils;
import org.apache.nifi.components.PropertyDescriptor;
import org.apache.nifi.flowfile.FlowFile;
......@@ -20,14 +23,21 @@ 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.annotation.CapabilityDescription;
import org.apache.nifi.processor.annotation.SideEffectFree;
import org.apache.nifi.processor.annotation.Tags;
import org.apache.nifi.processor.exception.ProcessException;
import org.apache.nifi.processor.io.InputStreamCallback;
import org.apache.nifi.processor.io.OutputStreamCallback;
import org.apache.nifi.processor.util.StandardValidators;
/**
*
* @author phillip
*/
@SideEffectFree
@Tags({"JSON", "NIFI ROCKS"})
@CapabilityDescription("Fetch value from json path.")
public class JsonProcessor extends AbstractProcessor {
private List<PropertyDescriptor> properties;
......@@ -58,14 +68,27 @@ public class JsonProcessor extends AbstractProcessor {
@Override
public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException {
FlowFile flowfile = session.get();
final AtomicReference<String> value = new AtomicReference<>();
session.read(flowfile, new InputStreamCallback() {
@Override
public void process(InputStream in) throws IOException {
String json = IOUtils.toString(in);
String result = JsonPath.read(json, "$.hello");
value.set(result);
}
});
flowfile = session.write(flowfile, new OutputStreamCallback() {
@Override
public void process(OutputStream out) throws IOException {
out.write(value.get().getBytes());
}
});
session.transfer(flowfile, SUCCESS);
}
@Override
......
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package rocks.nifi.examples.processors;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Set;
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.util.TestRunner;
import org.apache.nifi.util.TestRunners;
import org.junit.Test;
import static org.junit.Assert.*;
/**
*
* @author phillip
*/
public class JsonProcessorTest {
/**
* Test of onTrigger method, of class JsonProcessor.
*/
@org.junit.Test
public void testOnTrigger() {
InputStream content = new ByteArrayInputStream("{\"hello\":\"nifi rocks\"}".getBytes());
TestRunner runner = TestRunners.newTestRunner(new JsonProcessor());
runner.setProperty(JsonProcessor.JSON_PATH, "$.hello");
runner.enqueue(content);
runner.run();
runner.assertQueueEmpty();
// TODO review the generated test code and remove the default call to fail.
// fail("The test case is a prototype.");
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment