use of org.apache.nifi.remote.io.http.HttpServerCommunicationsSession in project nifi by apache.
the class TestHttpFlowFileServerProtocol method receiveFlowFiles.
private void receiveFlowFiles(final HttpFlowFileServerProtocol serverProtocol, final String transactionId, final Peer peer, final DataPacket... dataPackets) throws IOException {
final HttpRemoteSiteListener remoteSiteListener = HttpRemoteSiteListener.getInstance(NiFiProperties.createBasicNiFiProperties(null, null));
final HttpServerCommunicationsSession commsSession = (HttpServerCommunicationsSession) peer.getCommunicationsSession();
serverProtocol.handshake(peer);
assertTrue(serverProtocol.isHandshakeSuccessful());
setupMockProcessSession();
// Emulate dataPackets sent from a Site-to-Site client.
final FlowFileCodec negotiatedCoded = serverProtocol.negotiateCodec(peer);
final ByteArrayOutputStream testDataOs = new ByteArrayOutputStream();
for (final DataPacket dataPacket : dataPackets) {
negotiatedCoded.encode(dataPacket, testDataOs);
}
final InputStream httpInputStream = new ByteArrayInputStream(testDataOs.toByteArray());
((HttpInput) commsSession.getInput()).setInputStream(httpInputStream);
// Execute test using mock
final int flowFileReceived = serverProtocol.receiveFlowFiles(peer, processContext, processSession, negotiatedCoded);
assertEquals(dataPackets.length, flowFileReceived);
assertTrue(remoteSiteListener.isTransactionActive(transactionId));
}
use of org.apache.nifi.remote.io.http.HttpServerCommunicationsSession in project nifi by apache.
the class TestHttpFlowFileServerProtocol method testTransferTwoFiles.
@Test
public void testTransferTwoFiles() throws Exception {
final String transactionId = "testTransferTwoFiles";
final Peer peer = getDefaultPeer(transactionId);
final String endpointUri = "https://remote-host:8443/nifi-api/output-ports/port-id/transactions/" + transactionId + "/flow-files";
final HttpFlowFileServerProtocol serverProtocol = getDefaultHttpFlowFileServerProtocol();
final HttpServerCommunicationsSession commsSession = (HttpServerCommunicationsSession) peer.getCommunicationsSession();
commsSession.putHandshakeParam(HandshakeProperty.BATCH_COUNT, "2");
commsSession.setUserDn("unit-test");
commsSession.setDataTransferUrl(endpointUri);
transferFlowFiles(serverProtocol, transactionId, peer, processSession -> IntStream.of(1, 2).mapToObj(i -> {
final MockFlowFile flowFile = processSession.createFlowFile(("Server content " + i).getBytes());
final HashMap<String, String> attributes = new HashMap<>();
attributes.put("uuid", "server-uuid-" + i);
attributes.put("filename", "server-filename-" + i);
attributes.put("server-attr-" + i + "-1", "server-attr-" + i + "-1-value");
attributes.put("server-attr-" + i + "-2", "server-attr-" + i + "-2-value");
flowFile.putAttributes(attributes);
return flowFile;
}).collect(Collectors.toList()));
// Commit transaction
final int flowFileSent = serverProtocol.commitTransferTransaction(peer, "3058746557");
assertEquals(2, flowFileSent);
// Assert provenance
final List<ProvenanceEventRecord> provenanceEvents = sessionState.getProvenanceEvents();
assertEquals(2, provenanceEvents.size());
for (final ProvenanceEventRecord provenanceEvent : provenanceEvents) {
assertEquals(ProvenanceEventType.SEND, provenanceEvent.getEventType());
assertEquals(endpointUri, provenanceEvent.getTransitUri());
assertEquals("Remote Host=peer-host, Remote DN=unit-test", provenanceEvent.getDetails());
}
}
use of org.apache.nifi.remote.io.http.HttpServerCommunicationsSession in project nifi by apache.
the class TestHttpFlowFileServerProtocol method testPortNotInValidState.
@Test
public void testPortNotInValidState() 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);
final RootGroupPort port = mock(RootGroupPort.class);
final PortAuthorizationResult authResult = mock(PortAuthorizationResult.class);
doReturn(true).when(processGroup).isRootGroup();
doReturn(port).when(processGroup).getOutputPort("port-identifier");
doReturn(authResult).when(port).checkUserAuthorization(any(String.class));
doReturn(true).when(authResult).isAuthorized();
serverProtocol.setRootProcessGroup(processGroup);
try {
serverProtocol.handshake(peer);
fail();
} catch (final HandshakeException e) {
assertEquals(ResponseCode.PORT_NOT_IN_VALID_STATE, e.getResponseCode());
}
assertFalse(serverProtocol.isHandshakeSuccessful());
}
use of org.apache.nifi.remote.io.http.HttpServerCommunicationsSession in project nifi by apache.
the class TestHttpFlowFileServerProtocol method testReceiveTwoFiles.
@Test
public void testReceiveTwoFiles() throws Exception {
final String transactionId = "testReceiveTwoFile";
final String endpointUri = "https://remote-host:8443/nifi-api/input-ports/port-id/transactions/" + transactionId + "/flow-files";
final HttpFlowFileServerProtocol serverProtocol = getDefaultHttpFlowFileServerProtocol();
final Peer peer = getDefaultPeer(transactionId);
final HttpServerCommunicationsSession commsSession = (HttpServerCommunicationsSession) peer.getCommunicationsSession();
commsSession.putHandshakeParam(HandshakeProperty.BATCH_COUNT, "2");
commsSession.setUserDn("unit-test");
commsSession.setDataTransferUrl(endpointUri);
receiveFlowFiles(serverProtocol, transactionId, peer, createClientDataPacket(), createClientDataPacket());
// Commit transaction
commsSession.setResponseCode(ResponseCode.CONFIRM_TRANSACTION);
final int flowFileReceived = serverProtocol.commitReceiveTransaction(peer);
assertEquals(2, flowFileReceived);
// Assert provenance.
final List<ProvenanceEventRecord> provenanceEvents = sessionState.getProvenanceEvents();
assertEquals(2, provenanceEvents.size());
for (final ProvenanceEventRecord provenanceEvent : provenanceEvents) {
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(2, flowFiles.size());
for (final MockFlowFile flowFile : flowFiles) {
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");
}
}
use of org.apache.nifi.remote.io.http.HttpServerCommunicationsSession in project nifi by apache.
the class TestHttpFlowFileServerProtocol method testUnauthorized.
@Test
public void testUnauthorized() 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);
final RootGroupPort port = mock(RootGroupPort.class);
final PortAuthorizationResult authResult = mock(PortAuthorizationResult.class);
doReturn(true).when(processGroup).isRootGroup();
doReturn(port).when(processGroup).getOutputPort("port-identifier");
doReturn(authResult).when(port).checkUserAuthorization(any(String.class));
serverProtocol.setRootProcessGroup(processGroup);
try {
serverProtocol.handshake(peer);
fail();
} catch (final HandshakeException e) {
assertEquals(ResponseCode.UNAUTHORIZED, e.getResponseCode());
}
assertFalse(serverProtocol.isHandshakeSuccessful());
}
Aggregations