Search in sources :

Example 86 with FlowFile

use of org.apache.nifi.flowfile.FlowFile 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 87 with FlowFile

use of org.apache.nifi.flowfile.FlowFile in project nifi by apache.

the class ConsumeAMQP method processResource.

/**
 * Will construct a {@link FlowFile} containing the body of the consumed AMQP message (if {@link GetResponse} returned by {@link AMQPConsumer} is
 * not null) and AMQP properties that came with message which are added to a {@link FlowFile} as attributes, transferring {@link FlowFile} to
 * 'success' {@link Relationship}.
 */
@Override
protected void processResource(final Connection connection, final AMQPConsumer consumer, final ProcessContext context, final ProcessSession session) {
    final GetResponse response = consumer.consume();
    if (response == null) {
        context.yield();
        return;
    }
    FlowFile flowFile = session.create();
    flowFile = session.write(flowFile, out -> out.write(response.getBody()));
    final BasicProperties amqpProperties = response.getProps();
    final Map<String, String> attributes = buildAttributes(amqpProperties);
    flowFile = session.putAllAttributes(flowFile, attributes);
    session.getProvenanceReporter().receive(flowFile, connection.toString() + "/" + context.getProperty(QUEUE).getValue());
    session.transfer(flowFile, REL_SUCCESS);
}
Also used : StandardValidators(org.apache.nifi.processor.util.StandardValidators) CapabilityDescription(org.apache.nifi.annotation.documentation.CapabilityDescription) FlowFile(org.apache.nifi.flowfile.FlowFile) ProcessContext(org.apache.nifi.processor.ProcessContext) Set(java.util.Set) HashMap(java.util.HashMap) ProcessSession(org.apache.nifi.processor.ProcessSession) WritesAttribute(org.apache.nifi.annotation.behavior.WritesAttribute) Connection(com.rabbitmq.client.Connection) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) List(java.util.List) InputRequirement(org.apache.nifi.annotation.behavior.InputRequirement) WritesAttributes(org.apache.nifi.annotation.behavior.WritesAttributes) Relationship(org.apache.nifi.processor.Relationship) Map(java.util.Map) Requirement(org.apache.nifi.annotation.behavior.InputRequirement.Requirement) Tags(org.apache.nifi.annotation.documentation.Tags) Collections(java.util.Collections) BasicProperties(com.rabbitmq.client.AMQP.BasicProperties) GetResponse(com.rabbitmq.client.GetResponse) FlowFile(org.apache.nifi.flowfile.FlowFile) BasicProperties(com.rabbitmq.client.AMQP.BasicProperties) GetResponse(com.rabbitmq.client.GetResponse)

Example 88 with FlowFile

use of org.apache.nifi.flowfile.FlowFile in project nifi by apache.

the class MalformedChunkHandlerTest method testHandle.

@Test
public void testHandle() {
    String name = "name";
    byte[] badChunk = { 8 };
    FlowFile original = mock(FlowFile.class);
    FlowFile updated1 = mock(FlowFile.class);
    FlowFile updated2 = mock(FlowFile.class);
    FlowFile updated3 = mock(FlowFile.class);
    FlowFile updated4 = mock(FlowFile.class);
    ProcessSession session = mock(ProcessSession.class);
    when(session.create(original)).thenReturn(updated1);
    when(session.putAttribute(updated1, CoreAttributes.FILENAME.key(), name)).thenReturn(updated2);
    when(session.putAttribute(updated2, CoreAttributes.MIME_TYPE.key(), MediaType.APPLICATION_BINARY.toString())).thenReturn(updated3);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    when(session.write(eq(updated3), any(OutputStreamCallback.class))).thenAnswer(invocation -> {
        ((OutputStreamCallback) invocation.getArguments()[1]).process(out);
        return updated4;
    });
    malformedChunkHandler.handle(original, session, name, badChunk);
    verify(session).transfer(updated4, badChunkRelationship);
    assertArrayEquals(badChunk, out.toByteArray());
}
Also used : ProcessSession(org.apache.nifi.processor.ProcessSession) FlowFile(org.apache.nifi.flowfile.FlowFile) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStreamCallback(org.apache.nifi.processor.io.OutputStreamCallback) Test(org.junit.Test)

Example 89 with FlowFile

use of org.apache.nifi.flowfile.FlowFile in project nifi by apache.

the class ParseEvtxTest method testProcessChunkGranularity.

@Test
public void testProcessChunkGranularity() throws IOException, MalformedChunkException, XMLStreamException {
    String basename = "basename";
    int chunkNum = 5;
    int offset = 10001;
    byte[] badChunk = { 8 };
    RootNodeHandler rootNodeHandler1 = mock(RootNodeHandler.class);
    RootNodeHandler rootNodeHandler2 = mock(RootNodeHandler.class);
    OutputStream out2 = mock(OutputStream.class);
    when(rootNodeHandlerFactory.create(out)).thenReturn(rootNodeHandler1);
    when(rootNodeHandlerFactory.create(out2)).thenReturn(rootNodeHandler2);
    ChunkHeader chunkHeader1 = mock(ChunkHeader.class);
    ChunkHeader chunkHeader2 = mock(ChunkHeader.class);
    Record record1 = mock(Record.class);
    Record record2 = mock(Record.class);
    Record record3 = mock(Record.class);
    RootNode rootNode1 = mock(RootNode.class);
    RootNode rootNode2 = mock(RootNode.class);
    RootNode rootNode3 = mock(RootNode.class);
    ProcessSession session = mock(ProcessSession.class);
    FlowFile flowFile = mock(FlowFile.class);
    FlowFile created1 = mock(FlowFile.class);
    FlowFile updated1 = mock(FlowFile.class);
    FlowFile created2 = mock(FlowFile.class);
    FlowFile updated2 = mock(FlowFile.class);
    MalformedChunkException malformedChunkException = new MalformedChunkException("Test", null, offset, chunkNum, badChunk);
    when(session.create(flowFile)).thenReturn(created1).thenReturn(created2).thenReturn(null);
    when(session.write(eq(created1), any(OutputStreamCallback.class))).thenAnswer(invocation -> {
        ((OutputStreamCallback) invocation.getArguments()[1]).process(out);
        return updated1;
    });
    when(session.write(eq(created2), any(OutputStreamCallback.class))).thenAnswer(invocation -> {
        ((OutputStreamCallback) invocation.getArguments()[1]).process(out2);
        return updated2;
    });
    when(record1.getRootNode()).thenReturn(rootNode1);
    when(record2.getRootNode()).thenReturn(rootNode2);
    when(record3.getRootNode()).thenReturn(rootNode3);
    when(fileHeader.hasNext()).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(false);
    when(fileHeader.next()).thenThrow(malformedChunkException).thenReturn(chunkHeader1).thenReturn(chunkHeader2).thenReturn(null);
    when(chunkHeader1.hasNext()).thenReturn(true).thenReturn(false);
    when(chunkHeader1.next()).thenReturn(record1).thenReturn(null);
    when(chunkHeader2.hasNext()).thenReturn(true).thenReturn(true).thenReturn(false);
    when(chunkHeader2.next()).thenReturn(record2).thenReturn(record3).thenReturn(null);
    parseEvtx.processChunkGranularity(session, componentLog, flowFile, basename, in);
    verify(malformedChunkHandler).handle(flowFile, session, parseEvtx.getName(basename, chunkNum, null, ParseEvtx.EVTX_EXTENSION), badChunk);
    verify(rootNodeHandler1).handle(rootNode1);
    verify(rootNodeHandler1).close();
    verify(rootNodeHandler2).handle(rootNode2);
    verify(rootNodeHandler2).handle(rootNode3);
    verify(rootNodeHandler2).close();
}
Also used : ProcessSession(org.apache.nifi.processor.ProcessSession) RootNode(org.apache.nifi.processors.evtx.parser.bxml.RootNode) FlowFile(org.apache.nifi.flowfile.FlowFile) MockFlowFile(org.apache.nifi.util.MockFlowFile) OutputStream(java.io.OutputStream) ChunkHeader(org.apache.nifi.processors.evtx.parser.ChunkHeader) Record(org.apache.nifi.processors.evtx.parser.Record) Mockito.anyString(org.mockito.Mockito.anyString) MalformedChunkException(org.apache.nifi.processors.evtx.parser.MalformedChunkException) OutputStreamCallback(org.apache.nifi.processor.io.OutputStreamCallback) Test(org.junit.Test)

Example 90 with FlowFile

use of org.apache.nifi.flowfile.FlowFile in project nifi by apache.

the class ParseEvtxTest method testGetBasenameEvtxExtension.

@Test
public void testGetBasenameEvtxExtension() {
    String basename = "basename";
    FlowFile flowFile = mock(FlowFile.class);
    when(flowFile.getAttribute(CoreAttributes.FILENAME.key())).thenReturn(basename + ".evtx");
    assertEquals(basename, parseEvtx.getBasename(flowFile, componentLog));
    verifyNoMoreInteractions(componentLog);
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) MockFlowFile(org.apache.nifi.util.MockFlowFile) Mockito.anyString(org.mockito.Mockito.anyString) Test(org.junit.Test)

Aggregations

FlowFile (org.apache.nifi.flowfile.FlowFile)500 IOException (java.io.IOException)236 ProcessException (org.apache.nifi.processor.exception.ProcessException)193 HashMap (java.util.HashMap)160 InputStream (java.io.InputStream)145 OutputStream (java.io.OutputStream)131 ComponentLog (org.apache.nifi.logging.ComponentLog)119 Test (org.junit.Test)116 ArrayList (java.util.ArrayList)113 Map (java.util.Map)105 MockFlowFile (org.apache.nifi.util.MockFlowFile)103 ProcessSession (org.apache.nifi.processor.ProcessSession)99 OutputStreamCallback (org.apache.nifi.processor.io.OutputStreamCallback)83 Relationship (org.apache.nifi.processor.Relationship)78 InputStreamCallback (org.apache.nifi.processor.io.InputStreamCallback)78 HashSet (java.util.HashSet)75 List (java.util.List)67 StopWatch (org.apache.nifi.util.StopWatch)59 Set (java.util.Set)56 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)55