Search in sources :

Example 11 with SiteToSiteRestApiClient

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());
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HttpProxy(org.apache.nifi.remote.protocol.http.HttpProxy) PortDTO(org.apache.nifi.web.api.dto.PortDTO) SiteToSiteRestApiClient(org.apache.nifi.remote.util.SiteToSiteRestApiClient) ControllerDTO(org.apache.nifi.web.api.dto.ControllerDTO) SSLContext(javax.net.ssl.SSLContext) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Test(org.junit.Test)

Example 12 with SiteToSiteRestApiClient

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());
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SiteToSiteRestApiClient(org.apache.nifi.remote.util.SiteToSiteRestApiClient) ControllerDTO(org.apache.nifi.web.api.dto.ControllerDTO) Test(org.junit.Test)

Example 13 with SiteToSiteRestApiClient

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");
}
Also used : TransactionResultEntity(org.apache.nifi.web.api.entity.TransactionResultEntity) ByteArrayInputStream(org.apache.nifi.stream.io.ByteArrayInputStream) SiteToSiteRestApiClient(org.apache.nifi.remote.util.SiteToSiteRestApiClient) Peer(org.apache.nifi.remote.Peer) ByteArrayOutputStream(org.apache.nifi.stream.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 14 with SiteToSiteRestApiClient

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");
}
Also used : TransactionResultEntity(org.apache.nifi.web.api.entity.TransactionResultEntity) ByteArrayInputStream(org.apache.nifi.stream.io.ByteArrayInputStream) SiteToSiteRestApiClient(org.apache.nifi.remote.util.SiteToSiteRestApiClient) Peer(org.apache.nifi.remote.Peer) ByteArrayOutputStream(org.apache.nifi.stream.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 15 with SiteToSiteRestApiClient

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);
}
Also used : ByteArrayInputStream(org.apache.nifi.stream.io.ByteArrayInputStream) SiteToSiteRestApiClient(org.apache.nifi.remote.util.SiteToSiteRestApiClient) Peer(org.apache.nifi.remote.Peer) ByteArrayOutputStream(org.apache.nifi.stream.io.ByteArrayOutputStream) Test(org.junit.Test)

Aggregations

SiteToSiteRestApiClient (org.apache.nifi.remote.util.SiteToSiteRestApiClient)18 Test (org.junit.Test)12 Peer (org.apache.nifi.remote.Peer)11 ByteArrayInputStream (org.apache.nifi.stream.io.ByteArrayInputStream)9 ByteArrayOutputStream (org.apache.nifi.stream.io.ByteArrayOutputStream)9 TransactionResultEntity (org.apache.nifi.web.api.entity.TransactionResultEntity)7 HttpCommunicationsSession (org.apache.nifi.remote.io.http.HttpCommunicationsSession)6 CommunicationsSession (org.apache.nifi.remote.protocol.CommunicationsSession)6 IOException (java.io.IOException)4 InputStream (java.io.InputStream)4 DataPacket (org.apache.nifi.remote.protocol.DataPacket)4 SiteToSiteTestUtils.createDataPacket (org.apache.nifi.remote.protocol.SiteToSiteTestUtils.createDataPacket)4 ControllerDTO (org.apache.nifi.web.api.dto.ControllerDTO)4 Mockito.doAnswer (org.mockito.Mockito.doAnswer)4 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 Answer (org.mockito.stubbing.Answer)4 LinkedHashSet (java.util.LinkedHashSet)3 URI (java.net.URI)2 HashSet (java.util.HashSet)2 PeerStatus (org.apache.nifi.remote.PeerStatus)2