Search in sources :

Example 46 with FlowFile

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

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

the class TestStandardProcessSession method testCloneOriginalDataSmaller.

@Test
public void testCloneOriginalDataSmaller() throws IOException {
    final byte[] originalContent = "hello".getBytes();
    final byte[] replacementContent = "NEW DATA".getBytes();
    final Connection conn1 = createConnection();
    final FlowFileRecord flowFileRecord = new StandardFlowFileRecord.Builder().id(1000L).addAttribute("uuid", "12345678-1234-1234-1234-123456789012").entryDate(System.currentTimeMillis()).contentClaim(contentRepo.create(originalContent)).size(originalContent.length).build();
    flowFileQueue.put(flowFileRecord);
    when(connectable.getIncomingConnections()).thenReturn(Collections.singletonList(conn1));
    final FlowFile input = session.get();
    assertEquals(originalContent.length, input.getSize());
    final FlowFile modified = session.write(input, (in, out) -> out.write(replacementContent));
    assertEquals(replacementContent.length, modified.getSize());
    // Clone 'input', not 'modified' because we want to ensure that we use the outdated reference to ensure
    // that the framework uses the most current reference.
    final FlowFile clone = session.clone(input);
    assertEquals(replacementContent.length, clone.getSize());
    final byte[] buffer = new byte[replacementContent.length];
    try (final InputStream in = session.read(clone)) {
        StreamUtils.fillBuffer(in, buffer);
    }
    assertArrayEquals(replacementContent, buffer);
}
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) Connection(org.apache.nifi.connectable.Connection) Test(org.junit.Test)

Example 48 with FlowFile

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

the class TestStandardProcessSession method testTransferUnknownRelationship.

@Test
public void testTransferUnknownRelationship() {
    final FlowFileRecord flowFileRecord1 = new StandardFlowFileRecord.Builder().id(1L).addAttribute("uuid", "11111111-1111-1111-1111-111111111111").entryDate(System.currentTimeMillis()).build();
    flowFileQueue.put(flowFileRecord1);
    FlowFile ff1 = session.get();
    ff1 = session.putAttribute(ff1, "index", "1");
    try {
        session.transfer(ff1, FAKE_RELATIONSHIP);
        Assert.fail("Expected IllegalArgumentException");
    } catch (IllegalArgumentException iae) {
    }
    try {
        final Collection<FlowFile> collection = new HashSet<>();
        collection.add(ff1);
        session.transfer(collection, FAKE_RELATIONSHIP);
        Assert.fail("Expected IllegalArgumentException");
    } catch (IllegalArgumentException iae) {
    }
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) MockFlowFile(org.apache.nifi.util.MockFlowFile) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 49 with FlowFile

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

the class TestStandardProcessSession method testUuidAttributeCannotBeUpdated.

@Test
public void testUuidAttributeCannotBeUpdated() {
    String originalUuid = "11111111-1111-1111-1111-111111111111";
    final FlowFileRecord flowFileRecord1 = new StandardFlowFileRecord.Builder().id(1L).addAttribute("uuid", originalUuid).entryDate(System.currentTimeMillis()).build();
    flowFileQueue.put(flowFileRecord1);
    FlowFile flowFile = session.get();
    assertNotNull(flowFile);
    final String uuid = CoreAttributes.UUID.key();
    final String newUuid = "22222222-2222-2222-2222-222222222222";
    flowFile = session.putAttribute(flowFile, uuid, newUuid);
    assertEquals(originalUuid, flowFile.getAttribute(uuid));
    final Map<String, String> uuidMap = new HashMap<>(1);
    uuidMap.put(uuid, newUuid);
    flowFile = session.putAllAttributes(flowFile, uuidMap);
    assertEquals(originalUuid, flowFile.getAttribute(uuid));
    flowFile = session.removeAllAttributes(flowFile, Pattern.compile("uuid"));
    assertEquals(originalUuid, flowFile.getAttribute(uuid));
    flowFile = session.removeAllAttributes(flowFile, Collections.singleton(uuid));
    assertEquals(originalUuid, flowFile.getAttribute(uuid));
    flowFile = session.removeAttribute(flowFile, uuid);
    assertEquals(originalUuid, flowFile.getAttribute(uuid));
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) MockFlowFile(org.apache.nifi.util.MockFlowFile) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 50 with FlowFile

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