Search in sources :

Example 36 with FlowFile

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

the class TestStandardProcessSession method testContentNotFoundExceptionThrownWhenUnableToReadDataOffsetTooLarge.

@Test
public void testContentNotFoundExceptionThrownWhenUnableToReadDataOffsetTooLarge() {
    final FlowFileRecord flowFileRecord = new StandardFlowFileRecord.Builder().addAttribute("uuid", "12345678-1234-1234-1234-123456789012").entryDate(System.currentTimeMillis()).contentClaim(new StandardContentClaim(resourceClaimManager.newResourceClaim("x", "x", "0", true, false), 0L)).build();
    flowFileQueue.put(flowFileRecord);
    FlowFile ff1 = session.get();
    ff1 = session.write(ff1, new OutputStreamCallback() {

        @Override
        public void process(OutputStream out) throws IOException {
        }
    });
    session.transfer(ff1);
    session.commit();
    final FlowFileRecord flowFileRecord2 = new StandardFlowFileRecord.Builder().addAttribute("uuid", "12345678-1234-1234-1234-123456789012").entryDate(System.currentTimeMillis()).contentClaim(new StandardContentClaim(resourceClaimManager.newResourceClaim("x", "x", "0", true, false), 0L)).contentClaimOffset(1000L).size(1L).build();
    flowFileQueue.put(flowFileRecord2);
    // attempt to read the data.
    try {
        session.get();
        final FlowFile ff2 = session.get();
        session.read(ff2, new InputStreamCallback() {

            @Override
            public void process(InputStream in) throws IOException {
            }
        });
        Assert.fail("Expected MissingFlowFileException");
    } catch (final MissingFlowFileException mffe) {
    }
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) MockFlowFile(org.apache.nifi.util.MockFlowFile) StandardContentClaim(org.apache.nifi.controller.repository.claim.StandardContentClaim) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FilterOutputStream(java.io.FilterOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) InputStreamCallback(org.apache.nifi.processor.io.InputStreamCallback) OutputStreamCallback(org.apache.nifi.processor.io.OutputStreamCallback) IOException(java.io.IOException) MissingFlowFileException(org.apache.nifi.processor.exception.MissingFlowFileException) Test(org.junit.Test)

Example 37 with FlowFile

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

the class TestStandardProcessSession method testForkOneToOneReported.

@Test
public void testForkOneToOneReported() throws IOException {
    final FlowFileRecord flowFileRecord = new StandardFlowFileRecord.Builder().addAttribute("uuid", "12345678-1234-1234-1234-123456789012").entryDate(System.currentTimeMillis()).build();
    flowFileQueue.put(flowFileRecord);
    // we have to increment the ID generator because we are creating a FlowFile without the FlowFile Repository's knowledge
    flowFileRepo.idGenerator.getAndIncrement();
    final FlowFile orig = session.get();
    final FlowFile newFlowFile = session.create(orig);
    session.transfer(newFlowFile, new Relationship.Builder().name("A").build());
    session.getProvenanceReporter().fork(newFlowFile, Collections.singleton(orig));
    session.remove(orig);
    session.commit();
    final List<ProvenanceEventRecord> events = provenanceRepo.getEvents(0L, 1000);
    assertEquals(2, events.size());
    final ProvenanceEventRecord firstRecord = events.get(0);
    final ProvenanceEventRecord secondRecord = events.get(1);
    assertEquals(ProvenanceEventType.FORK, firstRecord.getEventType());
    assertEquals(ProvenanceEventType.DROP, secondRecord.getEventType());
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) MockFlowFile(org.apache.nifi.util.MockFlowFile) Relationship(org.apache.nifi.processor.Relationship) ProvenanceEventRecord(org.apache.nifi.provenance.ProvenanceEventRecord) Test(org.junit.Test)

Example 38 with FlowFile

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

the class TestStandardProcessSession method createConnection.

@SuppressWarnings("unchecked")
private Connection createConnection(AtomicReference<FlowFileQueue> flowFileQueueReference) {
    final Connection connection = Mockito.mock(Connection.class);
    FlowFileQueue flowFileQueueFromReference = flowFileQueueReference.get();
    final FlowFileQueue localFlowFileQueue;
    if (flowFileQueueFromReference == null) {
        localFlowFileQueue = createFlowFileQueueSpy(connection);
        flowFileQueueReference.set(localFlowFileQueue);
    } else {
        localFlowFileQueue = flowFileQueueFromReference;
    }
    when(connection.getFlowFileQueue()).thenReturn(localFlowFileQueue);
    Mockito.doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            localFlowFileQueue.put((FlowFileRecord) invocation.getArguments()[0]);
            return null;
        }
    }).when(connection).enqueue(Mockito.any(FlowFileRecord.class));
    Mockito.doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            localFlowFileQueue.putAll((Collection<FlowFileRecord>) invocation.getArguments()[0]);
            return null;
        }
    }).when(connection).enqueue(Mockito.any(Collection.class));
    final Connectable dest = Mockito.mock(Connectable.class);
    when(connection.getDestination()).thenReturn(dest);
    when(connection.getSource()).thenReturn(dest);
    Mockito.doAnswer(new Answer<FlowFile>() {

        @Override
        public FlowFile answer(InvocationOnMock invocation) throws Throwable {
            return localFlowFileQueue.poll(invocation.getArgumentAt(0, Set.class));
        }
    }).when(connection).poll(any(Set.class));
    Mockito.doAnswer(new Answer<List<FlowFileRecord>>() {

        @Override
        public List<FlowFileRecord> answer(InvocationOnMock invocation) throws Throwable {
            return localFlowFileQueue.poll(invocation.getArgumentAt(0, FlowFileFilter.class), invocation.getArgumentAt(1, Set.class));
        }
    }).when(connection).poll(any(FlowFileFilter.class), any(Set.class));
    Mockito.when(connection.getIdentifier()).thenReturn("conn-uuid");
    return connection;
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) MockFlowFile(org.apache.nifi.util.MockFlowFile) Set(java.util.Set) HashSet(java.util.HashSet) Connection(org.apache.nifi.connectable.Connection) FlowFileFilter(org.apache.nifi.processor.FlowFileFilter) StandardFlowFileQueue(org.apache.nifi.controller.StandardFlowFileQueue) FlowFileQueue(org.apache.nifi.controller.queue.FlowFileQueue) Connectable(org.apache.nifi.connectable.Connectable) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Collection(java.util.Collection) List(java.util.List) ArrayList(java.util.ArrayList)

Example 39 with FlowFile

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

the class TestStandardProcessSession method testProcessExceptionThrownIfCallbackThrowsInOutputStreamCallback.

@Test
public void testProcessExceptionThrownIfCallbackThrowsInOutputStreamCallback() {
    final FlowFile ff1 = session.create();
    final RuntimeException runtime = new RuntimeException();
    try {
        session.write(ff1, new OutputStreamCallback() {

            @Override
            public void process(final OutputStream out) throws IOException {
                throw runtime;
            }
        });
        Assert.fail("Should have thrown RuntimeException");
    } catch (final RuntimeException re) {
        assertTrue(runtime == re);
    }
    final IOException ioe = new IOException();
    try {
        session.write(ff1, new OutputStreamCallback() {

            @Override
            public void process(OutputStream out) throws IOException {
                throw ioe;
            }
        });
        Assert.fail("Should have thrown ProcessException");
    } catch (final ProcessException pe) {
        assertTrue(ioe == pe.getCause());
    }
    final ProcessException pe = new ProcessException();
    try {
        session.write(ff1, new OutputStreamCallback() {

            @Override
            public void process(OutputStream out) throws IOException {
                throw pe;
            }
        });
        Assert.fail("Should have thrown ProcessException");
    } catch (final ProcessException pe2) {
        assertTrue(pe == pe2);
    }
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) MockFlowFile(org.apache.nifi.util.MockFlowFile) ProcessException(org.apache.nifi.processor.exception.ProcessException) FilterOutputStream(java.io.FilterOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) OutputStreamCallback(org.apache.nifi.processor.io.OutputStreamCallback) IOException(java.io.IOException) Test(org.junit.Test)

Example 40 with FlowFile

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

the class TestStandardProcessSession method testCreateThenRollbackRemovesContent.

@Test
public void testCreateThenRollbackRemovesContent() throws IOException {
    final FlowFileRecord flowFileRecord = new StandardFlowFileRecord.Builder().addAttribute("uuid", "12345678-1234-1234-1234-123456789012").entryDate(System.currentTimeMillis()).build();
    flowFileQueue.put(flowFileRecord);
    final StreamCallback nop = new StreamCallback() {

        @Override
        public void process(InputStream in, OutputStream out) throws IOException {
        }
    };
    session.create();
    FlowFile flowFile = session.create(flowFileRecord);
    flowFile = session.write(flowFile, nop);
    FlowFile flowFile2 = session.create(flowFileRecord);
    flowFile2 = session.write(flowFile2, nop);
    session.write(flowFile2, nop);
    final FlowFile flowFile3 = session.create();
    session.write(flowFile3, nop);
    session.rollback();
    assertEquals(4, contentRepo.getClaimsRemoved());
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) MockFlowFile(org.apache.nifi.util.MockFlowFile) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FilterOutputStream(java.io.FilterOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) OutputStreamCallback(org.apache.nifi.processor.io.OutputStreamCallback) InputStreamCallback(org.apache.nifi.processor.io.InputStreamCallback) StreamCallback(org.apache.nifi.processor.io.StreamCallback) 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