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) {
}
}
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);
}
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) {
}
}
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));
}
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();
}
Aggregations