Search in sources :

Example 1 with MockProcessorInitializationContext

use of org.apache.nifi.util.MockProcessorInitializationContext in project nifi by apache.

the class TestInvokeGroovy method testScriptDefinedAttribute.

/**
 * Tests a script that has a Groovy Processor that that reads the first line of text from the flowfiles content and
 * stores the value in an attribute of the outgoing flowfile.
 *
 * @throws Exception Any error encountered while testing
 */
@Test
public void testScriptDefinedAttribute() throws Exception {
    InvokeScriptedProcessor processor = new InvokeScriptedProcessor();
    MockProcessContext context = new MockProcessContext(processor);
    MockProcessorInitializationContext initContext = new MockProcessorInitializationContext(processor, context);
    processor.initialize(initContext);
    context.setProperty(scriptingComponent.getScriptingComponentHelper().SCRIPT_ENGINE, "Groovy");
    context.setProperty(ScriptingComponentUtils.SCRIPT_FILE, "target/test/resources/groovy/test_reader.groovy");
    context.setProperty(ScriptingComponentUtils.MODULES, "target/test/resources/groovy");
    // State Manger is unused, and a null reference is specified
    processor.customValidate(new MockValidationContext(context));
    processor.setup(context);
    List<PropertyDescriptor> descriptors = processor.getSupportedPropertyDescriptors();
    assertNotNull(descriptors);
    assertTrue(descriptors.size() > 0);
    boolean found = false;
    for (PropertyDescriptor descriptor : descriptors) {
        if (descriptor.getName().equals("test-attribute")) {
            found = true;
            break;
        }
    }
    assertTrue(found);
}
Also used : PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) MockProcessorInitializationContext(org.apache.nifi.util.MockProcessorInitializationContext) MockValidationContext(org.apache.nifi.util.MockValidationContext) MockProcessContext(org.apache.nifi.util.MockProcessContext) Test(org.junit.Test)

Example 2 with MockProcessorInitializationContext

use of org.apache.nifi.util.MockProcessorInitializationContext in project nifi by apache.

the class TestInvokeGroovy method testScriptDefinedRelationship.

/**
 * Tests a script that has a Groovy Processor that that reads the first line of text from the flowfiles content and
 * stores the value in an attribute of the outgoing flowfile.
 *
 * @throws Exception Any error encountered while testing
 */
@Test
public void testScriptDefinedRelationship() throws Exception {
    InvokeScriptedProcessor processor = new InvokeScriptedProcessor();
    MockProcessContext context = new MockProcessContext(processor);
    MockProcessorInitializationContext initContext = new MockProcessorInitializationContext(processor, context);
    processor.initialize(initContext);
    context.setProperty(scriptingComponent.getScriptingComponentHelper().SCRIPT_ENGINE, "Groovy");
    context.setProperty(ScriptingComponentUtils.SCRIPT_FILE, "target/test/resources/groovy/test_reader.groovy");
    // State Manger is unused, and a null reference is specified
    processor.customValidate(new MockValidationContext(context));
    processor.setup(context);
    Set<Relationship> relationships = processor.getRelationships();
    assertNotNull(relationships);
    assertTrue(relationships.size() > 0);
    boolean found = false;
    for (Relationship relationship : relationships) {
        if (relationship.getName().equals("test")) {
            found = true;
            break;
        }
    }
    assertTrue(found);
}
Also used : MockProcessorInitializationContext(org.apache.nifi.util.MockProcessorInitializationContext) Relationship(org.apache.nifi.processor.Relationship) MockValidationContext(org.apache.nifi.util.MockValidationContext) MockProcessContext(org.apache.nifi.util.MockProcessContext) Test(org.junit.Test)

Example 3 with MockProcessorInitializationContext

use of org.apache.nifi.util.MockProcessorInitializationContext in project nifi by apache.

the class TestInvokeJavascript method testScriptDefinedAttribute.

/**
 * Tests a scripted processor written in Javascript that reads the first line of text from the flowfiles content and
 * stores the value in an attribute of the outgoing flowfile.
 * Confirms that the scripted processor can return property descriptors defined in it.
 *
 * @throws Exception Any error encountered while testing
 */
@Test
public void testScriptDefinedAttribute() throws Exception {
    InvokeScriptedProcessor processor = new InvokeScriptedProcessor();
    MockProcessContext context = new MockProcessContext(processor);
    MockProcessorInitializationContext initContext = new MockProcessorInitializationContext(processor, context);
    processor.initialize(initContext);
    context.setProperty(scriptingComponent.getScriptingComponentHelper().SCRIPT_ENGINE, "ECMAScript");
    context.setProperty(ScriptingComponentUtils.SCRIPT_FILE, "target/test/resources/javascript/test_reader.js");
    context.setProperty(ScriptingComponentUtils.MODULES, "target/test/resources/javascript");
    // State Manger is unused, and a null reference is specified
    processor.customValidate(new MockValidationContext(context));
    processor.setup(context);
    List<PropertyDescriptor> descriptors = processor.getSupportedPropertyDescriptors();
    assertNotNull(descriptors);
    assertTrue(descriptors.size() > 0);
    boolean found = false;
    for (PropertyDescriptor descriptor : descriptors) {
        if (descriptor.getName().equals("test-attribute")) {
            found = true;
            break;
        }
    }
    assertTrue(found);
}
Also used : PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) MockProcessorInitializationContext(org.apache.nifi.util.MockProcessorInitializationContext) MockValidationContext(org.apache.nifi.util.MockValidationContext) MockProcessContext(org.apache.nifi.util.MockProcessContext) Test(org.junit.Test)

Example 4 with MockProcessorInitializationContext

use of org.apache.nifi.util.MockProcessorInitializationContext in project nifi by apache.

the class TestJmsConsumer method testMap2FlowFileBytesMessage.

/**
 * Test BytesMessage to FlowFile conversion
 *
 * @throws java.lang.Exception ex
 */
@Test
public void testMap2FlowFileBytesMessage() throws Exception {
    TestRunner runner = TestRunners.newTestRunner(GetJMSQueue.class);
    BytesMessage bytesMessage = new ActiveMQBytesMessage();
    String sourceString = "Apache NiFi is an easy to use, powerful, and reliable system to process and distribute data.!";
    byte[] payload = sourceString.getBytes("UTF-8");
    bytesMessage.writeBytes(payload);
    bytesMessage.reset();
    ProcessContext context = runner.getProcessContext();
    ProcessSession session = runner.getProcessSessionFactory().createSession();
    ProcessorInitializationContext pic = new MockProcessorInitializationContext(runner.getProcessor(), (MockProcessContext) runner.getProcessContext());
    JmsProcessingSummary summary = JmsConsumer.map2FlowFile(context, session, bytesMessage, true, pic.getLogger());
    assertEquals("BytesMessage content length should equal to FlowFile content size", payload.length, summary.getLastFlowFile().getSize());
    final byte[] buffer = new byte[payload.length];
    runner.clearTransferState();
    session.read(summary.getLastFlowFile(), new InputStreamCallback() {

        @Override
        public void process(InputStream in) throws IOException {
            StreamUtils.fillBuffer(in, buffer, false);
        }
    });
    String contentString = new String(buffer, "UTF-8");
    assertEquals("", sourceString, contentString);
}
Also used : ProcessSession(org.apache.nifi.processor.ProcessSession) TestRunner(org.apache.nifi.util.TestRunner) InputStream(java.io.InputStream) MockProcessorInitializationContext(org.apache.nifi.util.MockProcessorInitializationContext) ActiveMQBytesMessage(org.apache.activemq.command.ActiveMQBytesMessage) BytesMessage(javax.jms.BytesMessage) JmsProcessingSummary(org.apache.nifi.processors.standard.util.JmsProcessingSummary) IOException(java.io.IOException) ProcessContext(org.apache.nifi.processor.ProcessContext) MockProcessContext(org.apache.nifi.util.MockProcessContext) MockProcessorInitializationContext(org.apache.nifi.util.MockProcessorInitializationContext) ProcessorInitializationContext(org.apache.nifi.processor.ProcessorInitializationContext) ActiveMQBytesMessage(org.apache.activemq.command.ActiveMQBytesMessage) InputStreamCallback(org.apache.nifi.processor.io.InputStreamCallback) Test(org.junit.Test)

Example 5 with MockProcessorInitializationContext

use of org.apache.nifi.util.MockProcessorInitializationContext in project nifi by apache.

the class TestJmsConsumer method testMap2FlowFileMapMessage.

/**
 * Test MapMessage to FlowFile conversion
 *
 * @throws java.lang.Exception ex
 */
@Test
public void testMap2FlowFileMapMessage() throws Exception {
    TestRunner runner = TestRunners.newTestRunner(GetJMSQueue.class);
    MapMessage mapMessage = createMapMessage();
    ProcessContext context = runner.getProcessContext();
    ProcessSession session = runner.getProcessSessionFactory().createSession();
    ProcessorInitializationContext pic = new MockProcessorInitializationContext(runner.getProcessor(), (MockProcessContext) runner.getProcessContext());
    JmsProcessingSummary summary = JmsConsumer.map2FlowFile(context, session, mapMessage, true, pic.getLogger());
    assertEquals("MapMessage should not create FlowFile content", 0, summary.getBytesReceived());
    Map<String, String> attributes = summary.getLastFlowFile().getAttributes();
    assertEquals("", "Arnold", attributes.get(JmsConsumer.MAP_MESSAGE_PREFIX + "name"));
    assertEquals("", "97", attributes.get(JmsConsumer.MAP_MESSAGE_PREFIX + "age"));
    assertEquals("", "89686.564", attributes.get(JmsConsumer.MAP_MESSAGE_PREFIX + "xyz"));
    assertEquals("", "true", attributes.get(JmsConsumer.MAP_MESSAGE_PREFIX + "good"));
}
Also used : ProcessSession(org.apache.nifi.processor.ProcessSession) TestRunner(org.apache.nifi.util.TestRunner) ActiveMQMapMessage(org.apache.activemq.command.ActiveMQMapMessage) MapMessage(javax.jms.MapMessage) MockProcessorInitializationContext(org.apache.nifi.util.MockProcessorInitializationContext) JmsProcessingSummary(org.apache.nifi.processors.standard.util.JmsProcessingSummary) ProcessContext(org.apache.nifi.processor.ProcessContext) MockProcessContext(org.apache.nifi.util.MockProcessContext) MockProcessorInitializationContext(org.apache.nifi.util.MockProcessorInitializationContext) ProcessorInitializationContext(org.apache.nifi.processor.ProcessorInitializationContext) Test(org.junit.Test)

Aggregations

MockProcessContext (org.apache.nifi.util.MockProcessContext)10 MockProcessorInitializationContext (org.apache.nifi.util.MockProcessorInitializationContext)10 Test (org.junit.Test)9 MockValidationContext (org.apache.nifi.util.MockValidationContext)4 ProcessContext (org.apache.nifi.processor.ProcessContext)3 ProcessSession (org.apache.nifi.processor.ProcessSession)3 ProcessorInitializationContext (org.apache.nifi.processor.ProcessorInitializationContext)3 JmsProcessingSummary (org.apache.nifi.processors.standard.util.JmsProcessingSummary)3 TestRunner (org.apache.nifi.util.TestRunner)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 MalformedURLException (java.net.MalformedURLException)2 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)2 Relationship (org.apache.nifi.processor.Relationship)2 InputStreamCallback (org.apache.nifi.processor.io.InputStreamCallback)2 GetRequestBuilder (org.elasticsearch.action.get.GetRequestBuilder)2 Matchers.anyString (org.mockito.Matchers.anyString)2 List (java.util.List)1 BytesMessage (javax.jms.BytesMessage)1 MapMessage (javax.jms.MapMessage)1