Search in sources :

Example 6 with MockProcessorInitializationContext

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

the class TestFetchElasticsearch5 method testCreateElasticsearch5ClientWithException.

@Test(expected = ProcessException.class)
public void testCreateElasticsearch5ClientWithException() throws ProcessException {
    FetchElasticsearch5TestProcessor processor = new FetchElasticsearch5TestProcessor(true) {

        @Override
        protected Client getTransportClient(Settings.Builder settingsBuilder, String xPackPath, String username, String password, List<InetSocketAddress> esHosts, ComponentLog log) throws MalformedURLException {
            throw new MalformedURLException();
        }
    };
    MockProcessContext context = new MockProcessContext(processor);
    processor.initialize(new MockProcessorInitializationContext(processor, context));
    processor.callCreateElasticsearchClient(context);
}
Also used : MalformedURLException(java.net.MalformedURLException) GetRequestBuilder(org.elasticsearch.action.get.GetRequestBuilder) MockProcessorInitializationContext(org.apache.nifi.util.MockProcessorInitializationContext) List(java.util.List) Matchers.anyString(org.mockito.Matchers.anyString) ComponentLog(org.apache.nifi.logging.ComponentLog) MockProcessContext(org.apache.nifi.util.MockProcessContext) Test(org.junit.Test)

Example 7 with MockProcessorInitializationContext

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

the class TestFetchElasticsearch method testCreateElasticsearchClientWithException.

@Test(expected = ProcessException.class)
public void testCreateElasticsearchClientWithException() throws ProcessException {
    FetchElasticsearchTestProcessor processor = new FetchElasticsearchTestProcessor(true) {

        @Override
        protected TransportClient getTransportClient(Settings.Builder settingsBuilder, String shieldUrl, String username, String password) throws MalformedURLException {
            throw new MalformedURLException();
        }
    };
    MockProcessContext context = new MockProcessContext(processor);
    processor.initialize(new MockProcessorInitializationContext(processor, context));
    processor.callCreateElasticsearchClient(context);
}
Also used : MalformedURLException(java.net.MalformedURLException) GetRequestBuilder(org.elasticsearch.action.get.GetRequestBuilder) MockProcessorInitializationContext(org.apache.nifi.util.MockProcessorInitializationContext) Matchers.anyString(org.mockito.Matchers.anyString) MockProcessContext(org.apache.nifi.util.MockProcessContext) Test(org.junit.Test)

Example 8 with MockProcessorInitializationContext

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

the class TestInvokeJavascript method testScriptDefinedRelationship.

/**
 * Tests a scripted processor written in Javascript 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 relationships defined in it.
 *
 * @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, "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);
    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 9 with MockProcessorInitializationContext

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

the class TestJmsConsumer method testMap2FlowFileTextMessage.

@Test
public void testMap2FlowFileTextMessage() throws Exception {
    TestRunner runner = TestRunners.newTestRunner(GetJMSQueue.class);
    TextMessage textMessage = new ActiveMQTextMessage();
    String payload = "Hello world!";
    textMessage.setText(payload);
    ProcessContext context = runner.getProcessContext();
    ProcessSession session = runner.getProcessSessionFactory().createSession();
    ProcessorInitializationContext pic = new MockProcessorInitializationContext(runner.getProcessor(), (MockProcessContext) runner.getProcessContext());
    JmsProcessingSummary summary = JmsConsumer.map2FlowFile(context, session, textMessage, true, pic.getLogger());
    assertEquals("TextMessage 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("", payload, 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) 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) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage) InputStreamCallback(org.apache.nifi.processor.io.InputStreamCallback) TextMessage(javax.jms.TextMessage) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage) Test(org.junit.Test)

Example 10 with MockProcessorInitializationContext

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

the class ExecuteGroovyScriptTest method setup.

@Before
public void setup() throws Exception {
    // init processor
    proc = new ExecuteGroovyScript();
    MockProcessContext context = new MockProcessContext(proc);
    MockProcessorInitializationContext initContext = new MockProcessorInitializationContext(proc, context);
    proc.initialize(initContext);
    assertNotNull(proc.getSupportedPropertyDescriptors());
    runner = TestRunners.newTestRunner(proc);
    runner.addControllerService("dbcp", dbcp, new HashMap<>());
    runner.enableControllerService(dbcp);
}
Also used : MockProcessorInitializationContext(org.apache.nifi.util.MockProcessorInitializationContext) MockProcessContext(org.apache.nifi.util.MockProcessContext) Before(org.junit.Before)

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