use of org.apache.nifi.remote.util.SiteToSiteRestApiClient in project nifi by apache.
the class TestSiteInfoProvider method testSecure.
@Test
public void testSecure() throws Exception {
final Set<String> expectedClusterUrl = new LinkedHashSet<>(Arrays.asList(new String[] { "https://node1:8443", "https://node2:8443" }));
final String expectedActiveClusterUrl = "https://node2:8443/nifi-api";
final SSLContext expectedSslConText = mock(SSLContext.class);
final HttpProxy expectedHttpProxy = mock(HttpProxy.class);
final SiteInfoProvider siteInfoProvider = spy(new SiteInfoProvider());
siteInfoProvider.setClusterUrls(expectedClusterUrl);
siteInfoProvider.setSslContext(expectedSslConText);
siteInfoProvider.setProxy(expectedHttpProxy);
final ControllerDTO controllerDTO = new ControllerDTO();
final PortDTO inputPort1 = new PortDTO();
inputPort1.setName("input-one");
inputPort1.setId("input-0001");
final PortDTO inputPort2 = new PortDTO();
inputPort2.setName("input-two");
inputPort2.setId("input-0002");
final PortDTO outputPort1 = new PortDTO();
outputPort1.setName("output-one");
outputPort1.setId("output-0001");
final PortDTO outputPort2 = new PortDTO();
outputPort2.setName("output-two");
outputPort2.setId("output-0002");
final Set<PortDTO> inputPorts = new HashSet<>();
inputPorts.add(inputPort1);
inputPorts.add(inputPort2);
final Set<PortDTO> outputPorts = new HashSet<>();
outputPorts.add(outputPort1);
outputPorts.add(outputPort2);
controllerDTO.setInputPorts(inputPorts);
controllerDTO.setOutputPorts(outputPorts);
controllerDTO.setRemoteSiteListeningPort(8081);
controllerDTO.setRemoteSiteHttpListeningPort(8443);
controllerDTO.setSiteToSiteSecure(true);
// SiteInfoProvider uses SiteToSIteRestApiClient to get ControllerDTO.
doAnswer(invocation -> {
final SSLContext sslContext = invocation.getArgumentAt(0, SSLContext.class);
final HttpProxy httpProxy = invocation.getArgumentAt(1, HttpProxy.class);
assertEquals(expectedSslConText, sslContext);
assertEquals(expectedHttpProxy, httpProxy);
final SiteToSiteRestApiClient apiClient = mock(SiteToSiteRestApiClient.class);
when(apiClient.getController(eq(expectedClusterUrl))).thenReturn(controllerDTO);
when(apiClient.getBaseUrl()).thenReturn(expectedActiveClusterUrl);
return apiClient;
}).when(siteInfoProvider).createSiteToSiteRestApiClient(any(), any());
// siteInfoProvider should expose correct information of the remote NiFi cluster.
assertEquals(controllerDTO.getRemoteSiteListeningPort(), siteInfoProvider.getSiteToSitePort());
assertEquals(controllerDTO.getRemoteSiteHttpListeningPort(), siteInfoProvider.getSiteToSiteHttpPort());
assertEquals(controllerDTO.isSiteToSiteSecure(), siteInfoProvider.isSecure());
assertTrue(siteInfoProvider.isWebInterfaceSecure());
assertEquals(inputPort1.getId(), siteInfoProvider.getInputPortIdentifier(inputPort1.getName()));
assertEquals(inputPort2.getId(), siteInfoProvider.getInputPortIdentifier(inputPort2.getName()));
assertEquals(outputPort1.getId(), siteInfoProvider.getOutputPortIdentifier(outputPort1.getName()));
assertEquals(outputPort2.getId(), siteInfoProvider.getOutputPortIdentifier(outputPort2.getName()));
assertNull(siteInfoProvider.getInputPortIdentifier("not-exist"));
assertNull(siteInfoProvider.getOutputPortIdentifier("not-exist"));
assertEquals(inputPort1.getId(), siteInfoProvider.getPortIdentifier(inputPort1.getName(), TransferDirection.SEND));
assertEquals(outputPort1.getId(), siteInfoProvider.getPortIdentifier(outputPort1.getName(), TransferDirection.RECEIVE));
assertEquals(expectedActiveClusterUrl, siteInfoProvider.getActiveClusterUrl().toString());
}
use of org.apache.nifi.remote.util.SiteToSiteRestApiClient in project nifi by apache.
the class TestSiteInfoProvider method testPlain.
@Test
public void testPlain() throws Exception {
final Set<String> expectedClusterUrl = new LinkedHashSet<>(Arrays.asList(new String[] { "http://node1:8443, http://node2:8443" }));
final String expectedActiveClusterUrl = "http://node2:8443/nifi-api";
final SiteInfoProvider siteInfoProvider = spy(new SiteInfoProvider());
siteInfoProvider.setClusterUrls(expectedClusterUrl);
final ControllerDTO controllerDTO = new ControllerDTO();
controllerDTO.setInputPorts(Collections.emptySet());
controllerDTO.setOutputPorts(Collections.emptySet());
controllerDTO.setRemoteSiteListeningPort(8081);
controllerDTO.setRemoteSiteHttpListeningPort(8080);
controllerDTO.setSiteToSiteSecure(false);
// SiteInfoProvider uses SiteToSIteRestApiClient to get ControllerDTO.
doAnswer(invocation -> {
final SiteToSiteRestApiClient apiClient = mock(SiteToSiteRestApiClient.class);
when(apiClient.getController(eq(expectedClusterUrl))).thenReturn(controllerDTO);
when(apiClient.getBaseUrl()).thenReturn(expectedActiveClusterUrl);
return apiClient;
}).when(siteInfoProvider).createSiteToSiteRestApiClient(any(), any());
// siteInfoProvider should expose correct information of the remote NiFi cluster.
assertEquals(controllerDTO.getRemoteSiteListeningPort(), siteInfoProvider.getSiteToSitePort());
assertEquals(controllerDTO.getRemoteSiteHttpListeningPort(), siteInfoProvider.getSiteToSiteHttpPort());
assertEquals(controllerDTO.isSiteToSiteSecure(), siteInfoProvider.isSecure());
assertFalse(siteInfoProvider.isWebInterfaceSecure());
assertEquals(expectedActiveClusterUrl, siteInfoProvider.getActiveClusterUrl().toString());
}
use of org.apache.nifi.remote.util.SiteToSiteRestApiClient in project nifi by apache.
the class TestHttpClientTransaction method testReceiveOneFlowFile.
@Test
public void testReceiveOneFlowFile() throws IOException {
SiteToSiteRestApiClient apiClient = mock(SiteToSiteRestApiClient.class);
final String transactionUrl = "http://www.example.com/data-transfer/input-ports/portId/transactions/transactionId";
doReturn(true).when(apiClient).openConnectionForReceive(eq(transactionUrl), any(Peer.class));
TransactionResultEntity resultEntity = new TransactionResultEntity();
resultEntity.setResponseCode(CONFIRM_TRANSACTION.getCode());
doReturn(resultEntity).when(apiClient).commitReceivingFlowFiles(eq(transactionUrl), eq(CONFIRM_TRANSACTION), eq("3680976076"));
ByteArrayOutputStream serverResponseBos = new ByteArrayOutputStream();
codec.encode(createDataPacket("contents on server 1"), serverResponseBos);
ByteArrayInputStream serverResponse = new ByteArrayInputStream(serverResponseBos.toByteArray());
ByteArrayOutputStream clientRequest = new ByteArrayOutputStream();
HttpClientTransaction transaction = getClientTransaction(serverResponse, clientRequest, apiClient, TransferDirection.RECEIVE, transactionUrl);
execReceiveOneFlowFile(transaction);
assertEquals("Client sends nothing as payload to receive flow files.", 0, clientRequest.toByteArray().length);
verify(apiClient).commitReceivingFlowFiles(transactionUrl, CONFIRM_TRANSACTION, "3680976076");
}
use of org.apache.nifi.remote.util.SiteToSiteRestApiClient in project nifi by apache.
the class TestHttpClientTransaction method testReceiveWithInvalidChecksum.
@Test
public void testReceiveWithInvalidChecksum() throws IOException {
SiteToSiteRestApiClient apiClient = mock(SiteToSiteRestApiClient.class);
final String transactionUrl = "http://www.example.com/data-transfer/input-ports/portId/transactions/transactionId";
doReturn(true).when(apiClient).openConnectionForReceive(eq(transactionUrl), any(Peer.class));
// The checksum is correct, but here we simulate as if it's wrong, BAD_CHECKSUM.
TransactionResultEntity resultEntity = new TransactionResultEntity();
resultEntity.setResponseCode(ResponseCode.BAD_CHECKSUM.getCode());
doReturn(resultEntity).when(apiClient).commitReceivingFlowFiles(eq(transactionUrl), eq(CONFIRM_TRANSACTION), eq("2969091230"));
ByteArrayOutputStream serverResponseBos = new ByteArrayOutputStream();
codec.encode(createDataPacket("contents on server 1"), serverResponseBos);
codec.encode(createDataPacket("contents on server 2"), serverResponseBos);
ByteArrayInputStream serverResponse = new ByteArrayInputStream(serverResponseBos.toByteArray());
ByteArrayOutputStream clientRequest = new ByteArrayOutputStream();
HttpClientTransaction transaction = getClientTransaction(serverResponse, clientRequest, apiClient, TransferDirection.RECEIVE, transactionUrl);
execReceiveWithInvalidChecksum(transaction);
assertEquals("Client sends nothing as payload to receive flow files.", 0, clientRequest.toByteArray().length);
verify(apiClient).commitReceivingFlowFiles(transactionUrl, CONFIRM_TRANSACTION, "2969091230");
}
use of org.apache.nifi.remote.util.SiteToSiteRestApiClient in project nifi by apache.
the class TestHttpClientTransaction method testSendZeroFlowFile.
@Test
public void testSendZeroFlowFile() throws IOException {
SiteToSiteRestApiClient apiClient = mock(SiteToSiteRestApiClient.class);
final String transactionUrl = "http://www.example.com/data-transfer/input-ports/portId/transactions/transactionId";
doNothing().when(apiClient).openConnectionForSend(eq(transactionUrl), any(Peer.class));
ByteArrayOutputStream serverResponseBos = new ByteArrayOutputStream();
ByteArrayInputStream serverResponse = new ByteArrayInputStream(serverResponseBos.toByteArray());
ByteArrayOutputStream clientRequest = new ByteArrayOutputStream();
HttpClientTransaction transaction = getClientTransaction(serverResponse, clientRequest, apiClient, TransferDirection.SEND, transactionUrl);
execSendZeroFlowFile(transaction);
assertEquals("Client didn't send anything", 0, clientRequest.toByteArray().length);
}
Aggregations