Search in sources :

Example 1 with ProcessContext

use of org.apache.nifi.processor.ProcessContext in project nifi by apache.

the class TestHttpFlowFileServerProtocol method testReceiveZeroFile.

@Test
public void testReceiveZeroFile() throws Exception {
    final HttpFlowFileServerProtocol serverProtocol = getDefaultHttpFlowFileServerProtocol();
    final Peer peer = getDefaultPeer("testReceiveZeroFile");
    final HttpServerCommunicationsSession commsSession = (HttpServerCommunicationsSession) peer.getCommunicationsSession();
    commsSession.setUserDn("unit-test");
    serverProtocol.handshake(peer);
    assertTrue(serverProtocol.isHandshakeSuccessful());
    final FlowFileCodec negotiatedCoded = serverProtocol.negotiateCodec(peer);
    final ProcessContext context = null;
    final ProcessSession processSession = mock(ProcessSession.class);
    final InputStream httpInputStream = new ByteArrayInputStream(new byte[] {});
    ((HttpInput) commsSession.getInput()).setInputStream(httpInputStream);
    // Execute test using mock
    final int flowFileReceived = serverProtocol.receiveFlowFiles(peer, context, processSession, negotiatedCoded);
    assertEquals(0, flowFileReceived);
}
Also used : MockProcessSession(org.apache.nifi.util.MockProcessSession) ProcessSession(org.apache.nifi.processor.ProcessSession) HttpServerCommunicationsSession(org.apache.nifi.remote.io.http.HttpServerCommunicationsSession) HttpInput(org.apache.nifi.remote.io.http.HttpInput) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Peer(org.apache.nifi.remote.Peer) FlowFileCodec(org.apache.nifi.remote.codec.FlowFileCodec) StandardFlowFileCodec(org.apache.nifi.remote.codec.StandardFlowFileCodec) MockProcessContext(org.apache.nifi.util.MockProcessContext) ProcessContext(org.apache.nifi.processor.ProcessContext) Test(org.junit.Test)

Example 2 with ProcessContext

use of org.apache.nifi.processor.ProcessContext in project nifi by apache.

the class TestHttpFlowFileServerProtocol method testShutdown.

@Test
public void testShutdown() throws Exception {
    final HttpFlowFileServerProtocol serverProtocol = getDefaultHttpFlowFileServerProtocol();
    final Peer peer = getDefaultPeer();
    serverProtocol.handshake(peer);
    assertTrue(serverProtocol.isHandshakeSuccessful());
    final FlowFileCodec negotiatedCoded = serverProtocol.negotiateCodec(peer);
    assertTrue(negotiatedCoded instanceof StandardFlowFileCodec);
    assertEquals(negotiatedCoded, serverProtocol.getPreNegotiatedCodec());
    assertEquals(1234, serverProtocol.getRequestExpiration());
    serverProtocol.shutdown(peer);
    final ProcessContext context = null;
    final ProcessSession processSession = null;
    try {
        serverProtocol.transferFlowFiles(peer, context, processSession, negotiatedCoded);
        fail("transferFlowFiles should fail since it's already shutdown.");
    } catch (final IllegalStateException e) {
    }
    try {
        serverProtocol.receiveFlowFiles(peer, context, processSession, negotiatedCoded);
        fail("receiveFlowFiles should fail since it's already shutdown.");
    } catch (final IllegalStateException e) {
    }
}
Also used : MockProcessSession(org.apache.nifi.util.MockProcessSession) ProcessSession(org.apache.nifi.processor.ProcessSession) Peer(org.apache.nifi.remote.Peer) StandardFlowFileCodec(org.apache.nifi.remote.codec.StandardFlowFileCodec) FlowFileCodec(org.apache.nifi.remote.codec.FlowFileCodec) StandardFlowFileCodec(org.apache.nifi.remote.codec.StandardFlowFileCodec) MockProcessContext(org.apache.nifi.util.MockProcessContext) ProcessContext(org.apache.nifi.processor.ProcessContext) Test(org.junit.Test)

Example 3 with ProcessContext

use of org.apache.nifi.processor.ProcessContext in project nifi by apache.

the class TestHttpFlowFileServerProtocol method testTransferZeroFile.

@Test
public void testTransferZeroFile() throws Exception {
    final HttpFlowFileServerProtocol serverProtocol = getDefaultHttpFlowFileServerProtocol();
    final Peer peer = getDefaultPeer();
    serverProtocol.handshake(peer);
    assertTrue(serverProtocol.isHandshakeSuccessful());
    final FlowFileCodec negotiatedCoded = serverProtocol.negotiateCodec(peer);
    final ProcessContext context = null;
    final ProcessSession processSession = mock(ProcessSession.class);
    // Execute test using mock
    final int flowFileSent = serverProtocol.transferFlowFiles(peer, context, processSession, negotiatedCoded);
    assertEquals(0, flowFileSent);
}
Also used : MockProcessSession(org.apache.nifi.util.MockProcessSession) ProcessSession(org.apache.nifi.processor.ProcessSession) Peer(org.apache.nifi.remote.Peer) FlowFileCodec(org.apache.nifi.remote.codec.FlowFileCodec) StandardFlowFileCodec(org.apache.nifi.remote.codec.StandardFlowFileCodec) MockProcessContext(org.apache.nifi.util.MockProcessContext) ProcessContext(org.apache.nifi.processor.ProcessContext) Test(org.junit.Test)

Example 4 with ProcessContext

use of org.apache.nifi.processor.ProcessContext in project nifi by apache.

the class ITPutS3Object method testS3MultipartAgeoff.

@Ignore
@Test
public void testS3MultipartAgeoff() throws InterruptedException, IOException {
    final PutS3Object processor = new PutS3Object();
    final TestRunner runner = TestRunners.newTestRunner(processor);
    final ProcessContext context = runner.getProcessContext();
    runner.setProperty(PutS3Object.CREDENTIALS_FILE, CREDENTIALS_FILE);
    runner.setProperty(PutS3Object.REGION, REGION);
    runner.setProperty(PutS3Object.BUCKET, BUCKET_NAME);
    // set check interval and age off to minimum values
    runner.setProperty(PutS3Object.MULTIPART_S3_AGEOFF_INTERVAL, "1 milli");
    runner.setProperty(PutS3Object.MULTIPART_S3_MAX_AGE, "1 milli");
    // create some dummy uploads
    for (Integer i = 0; i < 3; i++) {
        final InitiateMultipartUploadRequest initiateRequest = new InitiateMultipartUploadRequest(BUCKET_NAME, "file" + i.toString() + ".txt");
        try {
            client.initiateMultipartUpload(initiateRequest);
        } catch (AmazonClientException e) {
            Assert.fail("Failed to initiate upload: " + e.getMessage());
        }
    }
    // Age off is time dependent, so test has some timing constraints.  This
    // sleep() delays long enough to satisfy interval and age intervals.
    Thread.sleep(2000L);
    // System millis are used for timing, but it is incremented on each
    // call to circumvent what appears to be caching in the AWS library.
    // The increments are 1000 millis because AWS returns upload
    // initiation times in whole seconds.
    Long now = System.currentTimeMillis();
    MultipartUploadListing uploadList = processor.getS3AgeoffListAndAgeoffLocalState(context, client, now);
    Assert.assertEquals(3, uploadList.getMultipartUploads().size());
    MultipartUpload upload0 = uploadList.getMultipartUploads().get(0);
    processor.abortS3MultipartUpload(client, BUCKET_NAME, upload0);
    uploadList = processor.getS3AgeoffListAndAgeoffLocalState(context, client, now + 1000);
    Assert.assertEquals(2, uploadList.getMultipartUploads().size());
    final Map<String, String> attrs = new HashMap<>();
    attrs.put("filename", "test-upload.txt");
    runner.enqueue(getResourcePath(SAMPLE_FILE_RESOURCE_NAME), attrs);
    runner.run();
    uploadList = processor.getS3AgeoffListAndAgeoffLocalState(context, client, now + 2000);
    Assert.assertEquals(0, uploadList.getMultipartUploads().size());
}
Also used : HashMap(java.util.HashMap) TestRunner(org.apache.nifi.util.TestRunner) AmazonClientException(com.amazonaws.AmazonClientException) InitiateMultipartUploadRequest(com.amazonaws.services.s3.model.InitiateMultipartUploadRequest) MultipartUploadListing(com.amazonaws.services.s3.model.MultipartUploadListing) MultipartUpload(com.amazonaws.services.s3.model.MultipartUpload) ProcessContext(org.apache.nifi.processor.ProcessContext) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 5 with ProcessContext

use of org.apache.nifi.processor.ProcessContext in project nifi by apache.

the class ITPutS3Object method testEndpointOverride.

@Test
public void testEndpointOverride() {
    // remove leading "/" from filename to avoid duplicate separators
    final String TESTKEY = AbstractS3IT.SAMPLE_FILE_RESOURCE_NAME.substring(1);
    final PutS3Object processor = new TestablePutS3Object();
    final TestRunner runner = TestRunners.newTestRunner(processor);
    final ProcessContext context = runner.getProcessContext();
    runner.setProperty(PutS3Object.ENDPOINT_OVERRIDE, TEST_ENDPOINT);
    runner.setProperty(PutS3Object.BUCKET, BUCKET_NAME);
    runner.setProperty(PutS3Object.KEY, TESTKEY);
    runner.run();
    Assert.assertEquals(BUCKET_NAME, context.getProperty(PutS3Object.BUCKET).toString());
    Assert.assertEquals(TESTKEY, context.getProperty(PutS3Object.KEY).evaluateAttributeExpressions().toString());
    Assert.assertEquals(TEST_ENDPOINT, context.getProperty(PutS3Object.ENDPOINT_OVERRIDE).toString());
    String s3url = ((TestablePutS3Object) processor).testable_getClient().getResourceUrl(BUCKET_NAME, TESTKEY);
    Assert.assertEquals(TEST_ENDPOINT + "/" + BUCKET_NAME + "/" + TESTKEY, s3url);
}
Also used : TestRunner(org.apache.nifi.util.TestRunner) ProcessContext(org.apache.nifi.processor.ProcessContext) Test(org.junit.Test)

Aggregations

ProcessContext (org.apache.nifi.processor.ProcessContext)115 Test (org.junit.Test)67 TestRunner (org.apache.nifi.util.TestRunner)56 ProcessSession (org.apache.nifi.processor.ProcessSession)49 FlowFile (org.apache.nifi.flowfile.FlowFile)40 MockFlowFile (org.apache.nifi.util.MockFlowFile)39 HashSet (java.util.HashSet)35 Relationship (org.apache.nifi.processor.Relationship)35 List (java.util.List)34 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)34 ArrayList (java.util.ArrayList)33 Set (java.util.Set)33 Tags (org.apache.nifi.annotation.documentation.Tags)31 IOException (java.io.IOException)30 HashMap (java.util.HashMap)30 CapabilityDescription (org.apache.nifi.annotation.documentation.CapabilityDescription)30 ProcessException (org.apache.nifi.processor.exception.ProcessException)30 Collections (java.util.Collections)29 InputRequirement (org.apache.nifi.annotation.behavior.InputRequirement)29 ProcessSessionFactory (org.apache.nifi.processor.ProcessSessionFactory)29