Search in sources :

Example 16 with HttpServerCommunicationsSession

use of org.apache.nifi.remote.io.http.HttpServerCommunicationsSession in project nifi by apache.

the class TestHttpFlowFileServerProtocol method getDefaultPeer.

private Peer getDefaultPeer(final String transactionId) {
    final PeerDescription description = new PeerDescription("peer-host", 8080, false);
    final InputStream inputStream = new ByteArrayInputStream(new byte[] {});
    final OutputStream outputStream = new ByteArrayOutputStream();
    final HttpServerCommunicationsSession commsSession = new HttpServerCommunicationsSession(inputStream, outputStream, transactionId, "user");
    commsSession.putHandshakeParam(HandshakeProperty.GZIP, "false");
    commsSession.putHandshakeParam(HandshakeProperty.REQUEST_EXPIRATION_MILLIS, "1234");
    final String peerUrl = "http://peer-host:8080/";
    final String clusterUrl = "cluster-url";
    return new Peer(description, commsSession, peerUrl, clusterUrl);
}
Also used : HttpServerCommunicationsSession(org.apache.nifi.remote.io.http.HttpServerCommunicationsSession) PeerDescription(org.apache.nifi.remote.PeerDescription) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) Peer(org.apache.nifi.remote.Peer) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 17 with HttpServerCommunicationsSession

use of org.apache.nifi.remote.io.http.HttpServerCommunicationsSession in project nifi by apache.

the class TestHttpFlowFileServerProtocol method testTransferOneFile.

@Test
public void testTransferOneFile() throws Exception {
    final HttpFlowFileServerProtocol serverProtocol = getDefaultHttpFlowFileServerProtocol();
    final String transactionId = "testTransferOneFile";
    final Peer peer = getDefaultPeer(transactionId);
    final HttpServerCommunicationsSession commsSession = (HttpServerCommunicationsSession) peer.getCommunicationsSession();
    final String endpointUri = "https://remote-host:8443/nifi-api/output-ports/port-id/transactions/" + transactionId + "/flow-files";
    commsSession.putHandshakeParam(HandshakeProperty.BATCH_COUNT, "1");
    commsSession.setUserDn("unit-test");
    commsSession.setDataTransferUrl(endpointUri);
    transferFlowFiles(serverProtocol, transactionId, peer, processSession -> {
        final MockFlowFile flowFile = processSession.createFlowFile("Server content".getBytes());
        final HashMap<String, String> attributes = new HashMap<>();
        attributes.put("uuid", "server-uuid");
        attributes.put("filename", "server-filename");
        attributes.put("server-attr-1", "server-attr-1-value");
        attributes.put("server-attr-2", "server-attr-2-value");
        flowFile.putAttributes(attributes);
        return Arrays.asList(flowFile);
    });
    // Commit transaction
    final int flowFileSent = serverProtocol.commitTransferTransaction(peer, "3229577812");
    assertEquals(1, flowFileSent);
    // Assert provenance
    final List<ProvenanceEventRecord> provenanceEvents = sessionState.getProvenanceEvents();
    assertEquals(1, provenanceEvents.size());
    final ProvenanceEventRecord provenanceEvent = provenanceEvents.get(0);
    assertEquals(ProvenanceEventType.SEND, provenanceEvent.getEventType());
    assertEquals(endpointUri, provenanceEvent.getTransitUri());
    assertEquals("Remote Host=peer-host, Remote DN=unit-test", provenanceEvent.getDetails());
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) HttpServerCommunicationsSession(org.apache.nifi.remote.io.http.HttpServerCommunicationsSession) HashMap(java.util.HashMap) Peer(org.apache.nifi.remote.Peer) ProvenanceEventRecord(org.apache.nifi.provenance.ProvenanceEventRecord) Test(org.junit.Test)

Example 18 with HttpServerCommunicationsSession

use of org.apache.nifi.remote.io.http.HttpServerCommunicationsSession in project nifi by apache.

the class TestHttpFlowFileServerProtocol method testReceiveOneFile.

@Test
public void testReceiveOneFile() throws Exception {
    final HttpFlowFileServerProtocol serverProtocol = getDefaultHttpFlowFileServerProtocol();
    final String transactionId = "testReceiveOneFile";
    final String endpointUri = "https://remote-host:8443/nifi-api/input-ports/port-id/transactions/" + transactionId + "/flow-files";
    final Peer peer = getDefaultPeer(transactionId);
    final HttpServerCommunicationsSession commsSession = (HttpServerCommunicationsSession) peer.getCommunicationsSession();
    commsSession.putHandshakeParam(HandshakeProperty.BATCH_COUNT, "1");
    commsSession.setUserDn("unit-test");
    commsSession.setDataTransferUrl(endpointUri);
    final DataPacket dataPacket = createClientDataPacket();
    receiveFlowFiles(serverProtocol, transactionId, peer, dataPacket);
    // Commit transaction
    commsSession.setResponseCode(ResponseCode.CONFIRM_TRANSACTION);
    final int flowFileReceived = serverProtocol.commitReceiveTransaction(peer);
    assertEquals(1, flowFileReceived);
    // Assert provenance.
    final List<ProvenanceEventRecord> provenanceEvents = sessionState.getProvenanceEvents();
    assertEquals(1, provenanceEvents.size());
    final ProvenanceEventRecord provenanceEvent = provenanceEvents.get(0);
    assertEquals(ProvenanceEventType.RECEIVE, provenanceEvent.getEventType());
    assertEquals(endpointUri, provenanceEvent.getTransitUri());
    assertEquals("Remote Host=peer-host, Remote DN=unit-test", 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());
    flowFile.assertAttributeEquals("client-attr-1", "client-attr-1-value");
    flowFile.assertAttributeEquals("client-attr-2", "client-attr-2-value");
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) HttpServerCommunicationsSession(org.apache.nifi.remote.io.http.HttpServerCommunicationsSession) Peer(org.apache.nifi.remote.Peer) ProvenanceEventRecord(org.apache.nifi.provenance.ProvenanceEventRecord) DataPacket(org.apache.nifi.remote.protocol.DataPacket) StandardDataPacket(org.apache.nifi.remote.util.StandardDataPacket) Test(org.junit.Test)

Example 19 with HttpServerCommunicationsSession

use of org.apache.nifi.remote.io.http.HttpServerCommunicationsSession in project nifi by apache.

the class TestHttpFlowFileServerProtocol method testReceiveOneFileBadChecksum.

@Test
public void testReceiveOneFileBadChecksum() throws Exception {
    final HttpFlowFileServerProtocol serverProtocol = getDefaultHttpFlowFileServerProtocol();
    final String transactionId = "testReceiveOneFileBadChecksum";
    final Peer peer = getDefaultPeer(transactionId);
    final HttpServerCommunicationsSession commsSession = (HttpServerCommunicationsSession) peer.getCommunicationsSession();
    receiveFlowFiles(serverProtocol, transactionId, peer, createClientDataPacket());
    // Commit transaction
    commsSession.setResponseCode(ResponseCode.BAD_CHECKSUM);
    try {
        serverProtocol.commitReceiveTransaction(peer);
        fail();
    } catch (final IOException e) {
        assertTrue(e.getMessage().contains("Received a BadChecksum response"));
    }
}
Also used : HttpServerCommunicationsSession(org.apache.nifi.remote.io.http.HttpServerCommunicationsSession) Peer(org.apache.nifi.remote.Peer) IOException(java.io.IOException) Test(org.junit.Test)

Example 20 with HttpServerCommunicationsSession

use of org.apache.nifi.remote.io.http.HttpServerCommunicationsSession in project nifi by apache.

the class TestHttpFlowFileServerProtocol method testUnknownPort.

@Test
public void testUnknownPort() throws Exception {
    final HttpFlowFileServerProtocol serverProtocol = getDefaultHttpFlowFileServerProtocol();
    final Peer peer = getDefaultPeer();
    ((HttpServerCommunicationsSession) peer.getCommunicationsSession()).putHandshakeParam(HandshakeProperty.PORT_IDENTIFIER, "port-identifier");
    final ProcessGroup processGroup = mock(ProcessGroup.class);
    doReturn(true).when(processGroup).isRootGroup();
    serverProtocol.setRootProcessGroup(processGroup);
    try {
        serverProtocol.handshake(peer);
        fail();
    } catch (final HandshakeException e) {
        assertEquals(ResponseCode.UNKNOWN_PORT, e.getResponseCode());
    }
    assertFalse(serverProtocol.isHandshakeSuccessful());
}
Also used : HttpServerCommunicationsSession(org.apache.nifi.remote.io.http.HttpServerCommunicationsSession) Peer(org.apache.nifi.remote.Peer) ProcessGroup(org.apache.nifi.groups.ProcessGroup) HandshakeException(org.apache.nifi.remote.exception.HandshakeException) Test(org.junit.Test)

Aggregations

HttpServerCommunicationsSession (org.apache.nifi.remote.io.http.HttpServerCommunicationsSession)24 Peer (org.apache.nifi.remote.Peer)17 Test (org.junit.Test)12 HandshakeException (org.apache.nifi.remote.exception.HandshakeException)7 IOException (java.io.IOException)5 MockFlowFile (org.apache.nifi.util.MockFlowFile)5 InputStream (java.io.InputStream)4 UnknownHostException (java.net.UnknownHostException)4 ProcessGroup (org.apache.nifi.groups.ProcessGroup)4 ProvenanceEventRecord (org.apache.nifi.provenance.ProvenanceEventRecord)4 RootGroupPort (org.apache.nifi.remote.RootGroupPort)4 HttpFlowFileServerProtocol (org.apache.nifi.remote.protocol.http.HttpFlowFileServerProtocol)4 ByteArrayOutputStream (org.apache.nifi.stream.io.ByteArrayOutputStream)4 ApiOperation (io.swagger.annotations.ApiOperation)3 ApiResponses (io.swagger.annotations.ApiResponses)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 HashMap (java.util.HashMap)3 Consumes (javax.ws.rs.Consumes)3 Path (javax.ws.rs.Path)3 Produces (javax.ws.rs.Produces)3