Search in sources :

Example 6 with StandardDataPacket

use of org.apache.nifi.remote.util.StandardDataPacket in project nifi by apache.

the class TestStandardRemoteGroupPort method testReceiveHttp.

@Test
public void testReceiveHttp() throws Exception {
    setupMock(SiteToSiteTransportProtocol.HTTP, TransferDirection.RECEIVE);
    setupMockProcessSession();
    final String peerUrl = "https://node1.example.com:8080/nifi";
    final PeerDescription peerDescription = new PeerDescription("node1.example.com", 8080, true);
    final HttpCommunicationsSession commsSession = new HttpCommunicationsSession();
    commsSession.setUserDn("nifi.node1.example.com");
    final Peer peer = new Peer(peerDescription, commsSession, peerUrl, REMOTE_CLUSTER_URL);
    final String flowFileEndpointUri = "https://node1.example.com:8080/nifi-api/output-ports/port-id/transactions/transaction-id/flow-files";
    doReturn(peer).when(transaction).getCommunicant();
    commsSession.setDataTransferUrl(flowFileEndpointUri);
    final Map<String, String> attributes = new HashMap<>();
    final byte[] dataPacketContents = "DataPacket Contents".getBytes();
    final ByteArrayInputStream dataPacketInputStream = new ByteArrayInputStream(dataPacketContents);
    final DataPacket dataPacket = new StandardDataPacket(attributes, dataPacketInputStream, dataPacketContents.length);
    // Return null when it gets called second time.
    doReturn(dataPacket).doReturn(null).when(transaction).receive();
    port.onTrigger(processContext, processSession);
    // Assert provenance.
    final List<ProvenanceEventRecord> provenanceEvents = sessionState.getProvenanceEvents();
    assertEquals(1, provenanceEvents.size());
    final ProvenanceEventRecord provenanceEvent = provenanceEvents.get(0);
    assertEquals(ProvenanceEventType.RECEIVE, provenanceEvent.getEventType());
    assertEquals(flowFileEndpointUri, provenanceEvent.getTransitUri());
    assertEquals("Remote DN=nifi.node1.example.com", provenanceEvent.getDetails());
    // Assert received flow files.
    processSession.assertAllFlowFilesTransferred(Relationship.ANONYMOUS);
    final List<MockFlowFile> flowFiles = processSession.getFlowFilesForRelationship(Relationship.ANONYMOUS);
    assertEquals(1, flowFiles.size());
    final MockFlowFile flowFile = flowFiles.get(0);
    flowFile.assertAttributeEquals(SiteToSiteAttributes.S2S_HOST.key(), peer.getHost());
    flowFile.assertAttributeEquals(SiteToSiteAttributes.S2S_ADDRESS.key(), peer.getHost() + ":" + peer.getPort());
}
Also used : HashMap(java.util.HashMap) HttpCommunicationsSession(org.apache.nifi.remote.io.http.HttpCommunicationsSession) StandardDataPacket(org.apache.nifi.remote.util.StandardDataPacket) DataPacket(org.apache.nifi.remote.protocol.DataPacket) StandardDataPacket(org.apache.nifi.remote.util.StandardDataPacket) MockFlowFile(org.apache.nifi.util.MockFlowFile) ByteArrayInputStream(java.io.ByteArrayInputStream) ProvenanceEventRecord(org.apache.nifi.provenance.ProvenanceEventRecord) Test(org.junit.Test)

Example 7 with StandardDataPacket

use of org.apache.nifi.remote.util.StandardDataPacket in project nifi by apache.

the class TestSiteToSiteClient method testSend.

@Test
@Ignore("For local testing only; not really a unit test but a manual test")
public void testSend() throws IOException {
    System.setProperty("org.slf4j.simpleLogger.log.org.apache.nifi.remote", "DEBUG");
    final SiteToSiteClient client = new SiteToSiteClient.Builder().url("http://localhost:8080/nifi").portName("input").build();
    try {
        final Transaction transaction = client.createTransaction(TransferDirection.SEND);
        Assert.assertNotNull(transaction);
        final Map<String, String> attrs = new HashMap<>();
        attrs.put("site-to-site", "yes, please!");
        final byte[] bytes = "Hello".getBytes();
        final ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        final DataPacket packet = new StandardDataPacket(attrs, bais, bytes.length);
        transaction.send(packet);
        transaction.confirm();
        transaction.complete();
    } finally {
        client.close();
    }
}
Also used : SiteToSiteClient(org.apache.nifi.remote.client.SiteToSiteClient) Transaction(org.apache.nifi.remote.Transaction) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) StandardDataPacket(org.apache.nifi.remote.util.StandardDataPacket) StandardDataPacket(org.apache.nifi.remote.util.StandardDataPacket) DataPacket(org.apache.nifi.remote.protocol.DataPacket) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 8 with StandardDataPacket

use of org.apache.nifi.remote.util.StandardDataPacket in project nifi by apache.

the class SiteToSiteTestUtils method createDataPacket.

public static DataPacket createDataPacket(String contents) {
    try {
        byte[] bytes = contents.getBytes("UTF-8");
        ByteArrayInputStream is = new ByteArrayInputStream(bytes);
        return new StandardDataPacket(new HashMap<>(), is, bytes.length);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}
Also used : ByteArrayInputStream(org.apache.nifi.stream.io.ByteArrayInputStream) StandardDataPacket(org.apache.nifi.remote.util.StandardDataPacket) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

StandardDataPacket (org.apache.nifi.remote.util.StandardDataPacket)8 HashMap (java.util.HashMap)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 DataPacket (org.apache.nifi.remote.protocol.DataPacket)4 InputStream (java.io.InputStream)3 Test (org.junit.Test)3 DataInputStream (java.io.DataInputStream)2 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 FlowFile (org.apache.nifi.flowfile.FlowFile)2 InputStreamCallback (org.apache.nifi.processor.io.InputStreamCallback)2 ProvenanceEventRecord (org.apache.nifi.provenance.ProvenanceEventRecord)2 ProtocolException (org.apache.nifi.remote.exception.ProtocolException)2 HttpCommunicationsSession (org.apache.nifi.remote.io.http.HttpCommunicationsSession)2 MockFlowFile (org.apache.nifi.util.MockFlowFile)2 StopWatch (org.apache.nifi.util.StopWatch)2 DataOutputStream (java.io.DataOutputStream)1 EOFException (java.io.EOFException)1 OutputStream (java.io.OutputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1