Search in sources :

Example 6 with OutputStreamCallback

use of org.apache.nifi.processor.io.OutputStreamCallback 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 7 with OutputStreamCallback

use of org.apache.nifi.processor.io.OutputStreamCallback in project nifi by apache.

the class TestStandardProcessSession method testContentModifiedEmittedAndNotAttributesModified.

@Test
public void testContentModifiedEmittedAndNotAttributesModified() throws IOException {
    final FlowFileRecord flowFile = new StandardFlowFileRecord.Builder().id(1L).addAttribute("uuid", "000000000000-0000-0000-0000-00000000").build();
    this.flowFileQueue.put(flowFile);
    FlowFile existingFlowFile = session.get();
    existingFlowFile = session.write(existingFlowFile, new OutputStreamCallback() {

        @Override
        public void process(OutputStream out) throws IOException {
        }
    });
    existingFlowFile = session.putAttribute(existingFlowFile, "attr", "a");
    session.transfer(existingFlowFile, new Relationship.Builder().name("A").build());
    session.commit();
    final List<ProvenanceEventRecord> events = provenanceRepo.getEvents(0L, 10000);
    assertFalse(events.isEmpty());
    assertEquals(1, events.size());
    final ProvenanceEventRecord event = events.get(0);
    assertEquals(ProvenanceEventType.CONTENT_MODIFIED, event.getEventType());
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) MockFlowFile(org.apache.nifi.util.MockFlowFile) FilterOutputStream(java.io.FilterOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) Relationship(org.apache.nifi.processor.Relationship) ProvenanceEventRecord(org.apache.nifi.provenance.ProvenanceEventRecord) OutputStreamCallback(org.apache.nifi.processor.io.OutputStreamCallback) Test(org.junit.Test)

Example 8 with OutputStreamCallback

use of org.apache.nifi.processor.io.OutputStreamCallback in project nifi by apache.

the class TestStandardProcessSession method testContentModifiedNotEmittedForCreate.

@Test
public void testContentModifiedNotEmittedForCreate() throws IOException {
    FlowFile newFlowFile = session.create();
    newFlowFile = session.write(newFlowFile, new OutputStreamCallback() {

        @Override
        public void process(OutputStream out) throws IOException {
        }
    });
    session.transfer(newFlowFile, new Relationship.Builder().name("A").build());
    session.commit();
    final List<ProvenanceEventRecord> events = provenanceRepo.getEvents(0L, 10000);
    assertFalse(events.isEmpty());
    assertEquals(1, events.size());
    final ProvenanceEventRecord event = events.get(0);
    assertEquals(ProvenanceEventType.CREATE, event.getEventType());
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) MockFlowFile(org.apache.nifi.util.MockFlowFile) FilterOutputStream(java.io.FilterOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) Relationship(org.apache.nifi.processor.Relationship) ProvenanceEventRecord(org.apache.nifi.provenance.ProvenanceEventRecord) OutputStreamCallback(org.apache.nifi.processor.io.OutputStreamCallback) Test(org.junit.Test)

Example 9 with OutputStreamCallback

use of org.apache.nifi.processor.io.OutputStreamCallback in project nifi by apache.

the class TestStandardProcessSession method testExportTo.

@Test
public void testExportTo() throws IOException {
    final ContentClaim claim = contentRepo.create(false);
    final FlowFileRecord flowFileRecord = new StandardFlowFileRecord.Builder().contentClaim(claim).addAttribute("uuid", "12345678-1234-1234-1234-123456789012").entryDate(System.currentTimeMillis()).build();
    flowFileQueue.put(flowFileRecord);
    FlowFile flowFile = session.get();
    assertNotNull(flowFile);
    flowFile = session.append(flowFile, new OutputStreamCallback() {

        @Override
        public void process(OutputStream out) throws IOException {
            out.write("Hello World".getBytes());
        }
    });
    // should be OK
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    session.exportTo(flowFile, os);
    assertEquals("Hello World", new String(os.toByteArray()));
    os.close();
    // should throw ProcessException because of IOException (from processor code)
    FileOutputStream mock = Mockito.mock(FileOutputStream.class);
    doThrow(new IOException()).when(mock).write((byte[]) notNull(), any(Integer.class), any(Integer.class));
    try {
        session.exportTo(flowFile, mock);
        Assert.fail("Expected ProcessException");
    } catch (ProcessException e) {
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FlowFile(org.apache.nifi.flowfile.FlowFile) MockFlowFile(org.apache.nifi.util.MockFlowFile) StandardContentClaim(org.apache.nifi.controller.repository.claim.StandardContentClaim) ContentClaim(org.apache.nifi.controller.repository.claim.ContentClaim) ProcessException(org.apache.nifi.processor.exception.ProcessException) FilterOutputStream(java.io.FilterOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) OutputStreamCallback(org.apache.nifi.processor.io.OutputStreamCallback) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Test(org.junit.Test)

Example 10 with OutputStreamCallback

use of org.apache.nifi.processor.io.OutputStreamCallback in project nifi by apache.

the class TestStandardProcessSession method testReadFromInputStream.

@Test
public void testReadFromInputStream() throws IOException {
    FlowFile flowFile = session.create();
    flowFile = session.write(flowFile, new OutputStreamCallback() {

        @Override
        public void process(final OutputStream out) throws IOException {
            out.write("hello, world".getBytes());
        }
    });
    try (InputStream in = session.read(flowFile)) {
        final byte[] buffer = new byte[12];
        StreamUtils.fillBuffer(in, buffer);
        assertEquals("hello, world", new String(buffer));
    }
    session.remove(flowFile);
    session.commit();
}
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) Test(org.junit.Test)

Aggregations

FlowFile (org.apache.nifi.flowfile.FlowFile)70 OutputStreamCallback (org.apache.nifi.processor.io.OutputStreamCallback)70 OutputStream (java.io.OutputStream)69 IOException (java.io.IOException)39 ProcessException (org.apache.nifi.processor.exception.ProcessException)27 HashMap (java.util.HashMap)25 InputStream (java.io.InputStream)24 Test (org.junit.Test)24 MockFlowFile (org.apache.nifi.util.MockFlowFile)23 ByteArrayOutputStream (java.io.ByteArrayOutputStream)20 ComponentLog (org.apache.nifi.logging.ComponentLog)17 FileOutputStream (java.io.FileOutputStream)16 FilterOutputStream (java.io.FilterOutputStream)16 InputStreamCallback (org.apache.nifi.processor.io.InputStreamCallback)14 ArrayList (java.util.ArrayList)12 Map (java.util.Map)12 ProcessSession (org.apache.nifi.processor.ProcessSession)12 BufferedOutputStream (org.apache.nifi.stream.io.BufferedOutputStream)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)9 StandardContentClaim (org.apache.nifi.controller.repository.claim.StandardContentClaim)9