Search in sources :

Example 51 with ContentClaim

use of org.apache.nifi.controller.repository.claim.ContentClaim in project nifi by apache.

the class TestStandardProcessSession method testReadAfterSessionClosesStream.

@Test
public void testReadAfterSessionClosesStream() 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);
    final FlowFile flowFile = session.get();
    assertNotNull(flowFile);
    final AtomicReference<InputStream> inputStreamHolder = new AtomicReference<>(null);
    session.read(flowFile, true, new InputStreamCallback() {

        @Override
        public void process(final InputStream inputStream) throws IOException {
            inputStreamHolder.set(inputStream);
        }
    });
    assertDisabled(inputStreamHolder.get());
}
Also used : 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) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) InputStreamCallback(org.apache.nifi.processor.io.InputStreamCallback) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) Test(org.junit.Test)

Example 52 with ContentClaim

use of org.apache.nifi.controller.repository.claim.ContentClaim in project nifi by apache.

the class TestStandardProcessSession method testExpireDecrementsClaimsOnce.

@Test
@SuppressWarnings("unchecked")
public void testExpireDecrementsClaimsOnce() throws IOException {
    final ContentClaim contentClaim = contentRepo.create(false);
    final FlowFileRecord flowFileRecord = new StandardFlowFileRecord.Builder().addAttribute("uuid", "12345678-1234-1234-1234-123456789012").entryDate(System.currentTimeMillis()).contentClaim(contentClaim).build();
    Mockito.doAnswer(new Answer<List<FlowFileRecord>>() {

        int iterations = 0;

        @Override
        public List<FlowFileRecord> answer(InvocationOnMock invocation) throws Throwable {
            if (iterations++ == 0) {
                final Set<FlowFileRecord> expired = invocation.getArgumentAt(1, Set.class);
                expired.add(flowFileRecord);
            }
            return null;
        }
    }).when(flowFileQueue).poll(Mockito.any(FlowFileFilter.class), Mockito.any(Set.class));
    session.expireFlowFiles();
    // if the content claim count is decremented to less than 0, an exception will be thrown.
    session.commit();
    assertEquals(1L, contentRepo.getClaimsRemoved());
}
Also used : StandardContentClaim(org.apache.nifi.controller.repository.claim.StandardContentClaim) ContentClaim(org.apache.nifi.controller.repository.claim.ContentClaim) Set(java.util.Set) HashSet(java.util.HashSet) InvocationOnMock(org.mockito.invocation.InvocationOnMock) FlowFileFilter(org.apache.nifi.processor.FlowFileFilter) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 53 with ContentClaim

use of org.apache.nifi.controller.repository.claim.ContentClaim in project nifi by apache.

the class TestStandardProcessSession method testUpdateFlowFileModifiedMultipleTimesHasTransientClaimsOnCommit.

@Test
public void testUpdateFlowFileModifiedMultipleTimesHasTransientClaimsOnCommit() {
    flowFileQueue.put(new MockFlowFile(1L));
    FlowFile flowFile = session.get();
    for (int i = 0; i < 5; i++) {
        final byte[] content = String.valueOf(i).getBytes();
        flowFile = session.write(flowFile, out -> out.write(content));
    }
    session.transfer(flowFile, new Relationship.Builder().name("success").build());
    session.commit();
    final List<RepositoryRecord> repoUpdates = flowFileRepo.getUpdates();
    assertEquals(1, repoUpdates.size());
    // Should be 4 transient claims because it was written to 5 times. So 4 transient + 1 actual claim.
    final RepositoryRecord record = repoUpdates.get(0);
    assertEquals(RepositoryRecordType.UPDATE, record.getType());
    final List<ContentClaim> transientClaims = record.getTransientClaims();
    assertEquals(4, transientClaims.size());
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) OutputStreamCallback(org.apache.nifi.processor.io.OutputStreamCallback) Arrays(java.util.Arrays) FlowFileFilter(org.apache.nifi.processor.FlowFileFilter) ProcessGroup(org.apache.nifi.groups.ProcessGroup) ConnectableType(org.apache.nifi.connectable.ConnectableType) Mockito.doThrow(org.mockito.Mockito.doThrow) ByteArrayInputStream(java.io.ByteArrayInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StandardContentClaim(org.apache.nifi.controller.repository.claim.StandardContentClaim) FlowFileFilterResult(org.apache.nifi.processor.FlowFileFilter.FlowFileFilterResult) Map(java.util.Map) After(org.junit.After) MockProvenanceRepository(org.apache.nifi.provenance.MockProvenanceRepository) Connectable(org.apache.nifi.connectable.Connectable) Connection(org.apache.nifi.connectable.Connection) Path(java.nio.file.Path) InputStreamCallback(org.apache.nifi.processor.io.InputStreamCallback) FlowFileAccessException(org.apache.nifi.processor.exception.FlowFileAccessException) FlowFile(org.apache.nifi.flowfile.FlowFile) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) FilterOutputStream(java.io.FilterOutputStream) MissingFlowFileException(org.apache.nifi.processor.exception.MissingFlowFileException) FileNotFoundException(java.io.FileNotFoundException) Matchers.any(org.mockito.Matchers.any) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) Pattern(java.util.regex.Pattern) StreamCallback(org.apache.nifi.processor.io.StreamCallback) MockFlowFile(org.apache.nifi.util.MockFlowFile) ContentClaim(org.apache.nifi.controller.repository.claim.ContentClaim) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ProcessScheduler(org.apache.nifi.controller.ProcessScheduler) HashMap(java.util.HashMap) ProvenanceEventRepository(org.apache.nifi.provenance.ProvenanceEventRepository) AtomicReference(java.util.concurrent.atomic.AtomicReference) ProcessException(org.apache.nifi.processor.exception.ProcessException) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) HashSet(java.util.HashSet) StandardFlowFileQueue(org.apache.nifi.controller.StandardFlowFileQueue) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Relationship(org.apache.nifi.processor.Relationship) ResourceClaim(org.apache.nifi.controller.repository.claim.ResourceClaim) ProvenanceEventRecord(org.apache.nifi.provenance.ProvenanceEventRecord) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) Before(org.junit.Before) OutputStream(java.io.OutputStream) StandardResourceClaimManager(org.apache.nifi.controller.repository.claim.StandardResourceClaimManager) Files(java.nio.file.Files) Assert.assertNotNull(org.junit.Assert.assertNotNull) ProvenanceEventType(org.apache.nifi.provenance.ProvenanceEventType) Assert.assertTrue(org.junit.Assert.assertTrue) Matchers.notNull(org.mockito.Matchers.notNull) FileOutputStream(java.io.FileOutputStream) Mockito.times(org.mockito.Mockito.times) IOException(java.io.IOException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) FileInputStream(java.io.FileInputStream) File(java.io.File) Mockito.verify(org.mockito.Mockito.verify) AtomicLong(java.util.concurrent.atomic.AtomicLong) ResourceClaimManager(org.apache.nifi.controller.repository.claim.ResourceClaimManager) Mockito(org.mockito.Mockito) NiFiProperties(org.apache.nifi.util.NiFiProperties) Ignore(org.junit.Ignore) Paths(java.nio.file.Paths) StreamUtils(org.apache.nifi.stream.io.StreamUtils) CoreAttributes(org.apache.nifi.flowfile.attributes.CoreAttributes) Assert(org.junit.Assert) Collections(java.util.Collections) FlowFileQueue(org.apache.nifi.controller.queue.FlowFileQueue) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) 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) Relationship(org.apache.nifi.processor.Relationship) Test(org.junit.Test)

Example 54 with ContentClaim

use of org.apache.nifi.controller.repository.claim.ContentClaim in project nifi by apache.

the class TestStandardProcessSession method testAppendDoesNotDecrementContentClaimIfNotNeeded.

@Test
public void testAppendDoesNotDecrementContentClaimIfNotNeeded() {
    FlowFile flowFile = session.create();
    session.append(flowFile, new OutputStreamCallback() {

        @Override
        public void process(OutputStream out) throws IOException {
            out.write("hello".getBytes());
        }
    });
    final Set<ContentClaim> existingClaims = contentRepo.getExistingClaims();
    assertEquals(1, existingClaims.size());
    final ContentClaim claim = existingClaims.iterator().next();
    final int countAfterAppend = contentRepo.getClaimantCount(claim);
    assertEquals(1, countAfterAppend);
}
Also used : 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) 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 55 with ContentClaim

use of org.apache.nifi.controller.repository.claim.ContentClaim in project nifi by apache.

the class TestStandardProcessSession method testOpenMultipleInputStreamsToFlowFile.

@Test
public void testOpenMultipleInputStreamsToFlowFile() throws IOException {
    final ContentClaim claim = contentRepo.create(false);
    try (final OutputStream out = contentRepo.write(claim)) {
        out.write("hello, world".getBytes());
    }
    final FlowFileRecord flowFileRecord = new StandardFlowFileRecord.Builder().contentClaim(claim).addAttribute("uuid", "12345678-1234-1234-1234-123456789012").entryDate(System.currentTimeMillis()).size(12L).build();
    flowFileQueue.put(flowFileRecord);
    final FlowFile flowFile = session.get();
    InputStream in = session.read(flowFile);
    final byte[] buffer = new byte[12];
    StreamUtils.fillBuffer(in, buffer);
    assertEquals("hello, world", new String(buffer));
    InputStream in2 = session.read(flowFile);
    StreamUtils.fillBuffer(in2, buffer);
    assertEquals("hello, world", new String(buffer));
    in.close();
    in2.close();
    session.remove(flowFile);
    session.commit();
}
Also used : 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) 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) Test(org.junit.Test)

Aggregations

ContentClaim (org.apache.nifi.controller.repository.claim.ContentClaim)79 StandardContentClaim (org.apache.nifi.controller.repository.claim.StandardContentClaim)51 Test (org.junit.Test)40 OutputStream (java.io.OutputStream)39 ByteArrayOutputStream (java.io.ByteArrayOutputStream)30 IOException (java.io.IOException)26 InputStream (java.io.InputStream)22 ResourceClaim (org.apache.nifi.controller.repository.claim.ResourceClaim)22 ByteArrayInputStream (java.io.ByteArrayInputStream)20 FlowFile (org.apache.nifi.flowfile.FlowFile)19 Path (java.nio.file.Path)18 ArrayList (java.util.ArrayList)16 HashMap (java.util.HashMap)16 FlowFileQueue (org.apache.nifi.controller.queue.FlowFileQueue)14 Map (java.util.Map)13 FileOutputStream (java.io.FileOutputStream)12 FilterOutputStream (java.io.FilterOutputStream)12 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)12 FlowFileAccessException (org.apache.nifi.processor.exception.FlowFileAccessException)12 ProvenanceEventRecord (org.apache.nifi.provenance.ProvenanceEventRecord)12