Search in sources :

Example 11 with Processor

use of org.apache.nifi.processor.Processor in project nifi by apache.

the class TestStandardRemoteGroupPort method setupMockProcessSession.

private void setupMockProcessSession() {
    // Construct a RemoteGroupPort as a processor to use NiFi mock library.
    final Processor remoteGroupPort = mock(Processor.class);
    final Set<Relationship> relationships = new HashSet<>();
    relationships.add(Relationship.ANONYMOUS);
    when(remoteGroupPort.getRelationships()).thenReturn(relationships);
    when(remoteGroupPort.getIdentifier()).thenReturn("remote-group-port-id");
    sessionState = new SharedSessionState(remoteGroupPort, new AtomicLong(0));
    processSession = new MockProcessSession(sessionState, remoteGroupPort);
    processContext = new MockProcessContext(remoteGroupPort);
}
Also used : SharedSessionState(org.apache.nifi.util.SharedSessionState) AtomicLong(java.util.concurrent.atomic.AtomicLong) Processor(org.apache.nifi.processor.Processor) Relationship(org.apache.nifi.processor.Relationship) MockProcessSession(org.apache.nifi.util.MockProcessSession) MockProcessContext(org.apache.nifi.util.MockProcessContext) HashSet(java.util.HashSet)

Example 12 with Processor

use of org.apache.nifi.processor.Processor in project nifi by apache.

the class PriorityAttributePrioritizerTest method testPrioritizer.

@Test
public void testPrioritizer() throws InstantiationException, IllegalAccessException {
    final Processor processor = new SimpleProcessor();
    final AtomicLong idGenerator = new AtomicLong(0L);
    final MockProcessSession session = new MockProcessSession(new SharedSessionState(processor, idGenerator), Mockito.mock(Processor.class));
    final MockFlowFile ffNoPriority = session.create();
    final MockFlowFile ffPri1 = session.create();
    ffPri1.putAttributes(attrsPri1);
    final MockFlowFile ffPri2 = session.create();
    ffPri2.putAttributes(attrsPri2);
    final MockFlowFile ffPrin1 = session.create();
    ffPrin1.putAttributes(attrsPrin1);
    final MockFlowFile ffPriA = session.create();
    ffPriA.putAttributes(attrsPriA);
    final MockFlowFile ffPriB = session.create();
    ffPriB.putAttributes(attrsPriB);
    final MockFlowFile ffPriLP = session.create();
    ffPriLP.putAttributes(attrsPriLP);
    final MockFlowFile ffPriLN = session.create();
    ffPriLN.putAttributes(attrsPriLN);
    final PriorityAttributePrioritizer prioritizer = new PriorityAttributePrioritizer();
    assertEquals(0, prioritizer.compare(null, null));
    assertEquals(-1, prioritizer.compare(ffNoPriority, null));
    assertEquals(1, prioritizer.compare(null, ffNoPriority));
    assertEquals(0, prioritizer.compare(ffNoPriority, ffNoPriority));
    assertEquals(-1, prioritizer.compare(ffPri1, ffNoPriority));
    assertEquals(1, prioritizer.compare(ffNoPriority, ffPri1));
    assertEquals(0, prioritizer.compare(ffPri1, ffPri1));
    assertEquals(-1, prioritizer.compare(ffPri1, ffPri2));
    assertEquals(1, prioritizer.compare(ffPri2, ffPri1));
    assertEquals(-1, prioritizer.compare(ffPrin1, ffPri1));
    assertEquals(1, prioritizer.compare(ffPri1, ffPrin1));
    assertEquals(-1, prioritizer.compare(ffPri1, ffPriA));
    assertEquals(1, prioritizer.compare(ffPriA, ffPri1));
    assertEquals(0, prioritizer.compare(ffPriA, ffPriA));
    assertEquals(-1, prioritizer.compare(ffPriA, ffPriB));
    assertEquals(1, prioritizer.compare(ffPriB, ffPriA));
    assertEquals(1, prioritizer.compare(ffPriLP, ffPri1));
    assertEquals(-1, prioritizer.compare(ffPri1, ffPriLP));
    assertEquals(-1, prioritizer.compare(ffPriLN, ffPri1));
    assertEquals(1, prioritizer.compare(ffPri1, ffPriLN));
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) AtomicLong(java.util.concurrent.atomic.AtomicLong) SharedSessionState(org.apache.nifi.util.SharedSessionState) Processor(org.apache.nifi.processor.Processor) AbstractProcessor(org.apache.nifi.processor.AbstractProcessor) MockProcessSession(org.apache.nifi.util.MockProcessSession) Test(org.junit.Test)

Example 13 with Processor

use of org.apache.nifi.processor.Processor in project nifi by apache.

the class TestMockProcessSession method testReadWithoutCloseThrowsExceptionOnCommit.

@Test
public void testReadWithoutCloseThrowsExceptionOnCommit() throws IOException {
    final Processor processor = new PoorlyBehavedProcessor();
    final MockProcessSession session = new MockProcessSession(new SharedSessionState(processor, new AtomicLong(0L)), processor);
    FlowFile flowFile = session.createFlowFile("hello, world".getBytes());
    final InputStream in = session.read(flowFile);
    final byte[] buffer = new byte[12];
    StreamUtils.fillBuffer(in, buffer);
    assertEquals("hello, world", new String(buffer));
    session.remove(flowFile);
    try {
        session.commit();
        Assert.fail("Was able to commit session without closing InputStream");
    } catch (final FlowFileHandlingException ffhe) {
        System.out.println(ffhe.toString());
    }
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) AtomicLong(java.util.concurrent.atomic.AtomicLong) Processor(org.apache.nifi.processor.Processor) AbstractProcessor(org.apache.nifi.processor.AbstractProcessor) InputStream(java.io.InputStream) FlowFileHandlingException(org.apache.nifi.processor.exception.FlowFileHandlingException) Test(org.junit.Test)

Example 14 with Processor

use of org.apache.nifi.processor.Processor in project nifi by apache.

the class TestMockProcessSession method testTransferUnknownRelationship.

@Test
public void testTransferUnknownRelationship() {
    final Processor processor = new PoorlyBehavedProcessor();
    final MockProcessSession session = new MockProcessSession(new SharedSessionState(processor, new AtomicLong(0L)), processor);
    FlowFile ff1 = session.createFlowFile("hello, world".getBytes());
    final Relationship fakeRel = new Relationship.Builder().name("FAKE").build();
    try {
        session.transfer(ff1, fakeRel);
        Assert.fail("Should have thrown IllegalArgumentException");
    } catch (final IllegalArgumentException ie) {
    }
    try {
        session.transfer(Collections.singleton(ff1), fakeRel);
        Assert.fail("Should have thrown IllegalArgumentException");
    } catch (final IllegalArgumentException ie) {
    }
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) AtomicLong(java.util.concurrent.atomic.AtomicLong) Processor(org.apache.nifi.processor.Processor) AbstractProcessor(org.apache.nifi.processor.AbstractProcessor) Relationship(org.apache.nifi.processor.Relationship) Test(org.junit.Test)

Example 15 with Processor

use of org.apache.nifi.processor.Processor in project nifi by apache.

the class InvokeScriptedProcessor method getSupportedPropertyDescriptors.

/**
 * Returns a list of property descriptors supported by this processor. The
 * list always includes properties such as script engine name, script file
 * name, script body name, script arguments, and an external module path. If
 * the scripted processor also defines supported properties, those are added
 * to the list as well.
 *
 * @return a List of PropertyDescriptor objects supported by this processor
 */
@Override
protected List<PropertyDescriptor> getSupportedPropertyDescriptors() {
    synchronized (scriptingComponentHelper.isInitialized) {
        if (!scriptingComponentHelper.isInitialized.get()) {
            scriptingComponentHelper.createResources();
        }
    }
    List<PropertyDescriptor> supportedPropertyDescriptors = new ArrayList<>();
    supportedPropertyDescriptors.addAll(scriptingComponentHelper.getDescriptors());
    final Processor instance = processor.get();
    if (instance != null) {
        try {
            final List<PropertyDescriptor> instanceDescriptors = instance.getPropertyDescriptors();
            if (instanceDescriptors != null) {
                supportedPropertyDescriptors.addAll(instanceDescriptors);
            }
        } catch (final Throwable t) {
            final ComponentLog logger = getLogger();
            final String message = "Unable to get property descriptors from Processor: " + t;
            logger.error(message);
            if (logger.isDebugEnabled()) {
                logger.error(message, t);
            }
        }
    }
    return Collections.unmodifiableList(supportedPropertyDescriptors);
}
Also used : AbstractSessionFactoryProcessor(org.apache.nifi.processor.AbstractSessionFactoryProcessor) Processor(org.apache.nifi.processor.Processor) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) ArrayList(java.util.ArrayList) ComponentLog(org.apache.nifi.logging.ComponentLog)

Aggregations

Processor (org.apache.nifi.processor.Processor)50 Test (org.junit.Test)19 AbstractProcessor (org.apache.nifi.processor.AbstractProcessor)13 ComponentLog (org.apache.nifi.logging.ComponentLog)12 NarCloseable (org.apache.nifi.nar.NarCloseable)12 AtomicLong (java.util.concurrent.atomic.AtomicLong)11 Relationship (org.apache.nifi.processor.Relationship)11 ProcessorNode (org.apache.nifi.controller.ProcessorNode)9 HashSet (java.util.HashSet)8 StandardComponentVariableRegistry (org.apache.nifi.registry.variable.StandardComponentVariableRegistry)8 StandardProcessorInitializationContext (org.apache.nifi.processor.StandardProcessorInitializationContext)7 StandardProcessorNode (org.apache.nifi.controller.StandardProcessorNode)6 AbstractSessionFactoryProcessor (org.apache.nifi.processor.AbstractSessionFactoryProcessor)6 ProcessorInitializationContext (org.apache.nifi.processor.ProcessorInitializationContext)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 StandardValidationContextFactory (org.apache.nifi.processor.StandardValidationContextFactory)5 Collection (java.util.Collection)4 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)4