Search in sources :

Example 21 with ByteArrayInputStream

use of org.apache.nifi.stream.io.ByteArrayInputStream 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 22 with ByteArrayInputStream

use of org.apache.nifi.stream.io.ByteArrayInputStream 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)

Example 23 with ByteArrayInputStream

use of org.apache.nifi.stream.io.ByteArrayInputStream in project nifi by apache.

the class TestHttpClientTransaction method testSendButDestinationFull.

@Test
public void testSendButDestinationFull() 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("portId"), any(Peer.class));
    // Emulate that server returns correct checksum.
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            HttpCommunicationsSession commSession = (HttpCommunicationsSession) invocation.getArguments()[0];
            commSession.setChecksum("3359812065");
            return null;
        }
    }).when(apiClient).finishTransferFlowFiles(any(CommunicationsSession.class));
    TransactionResultEntity resultEntity = new TransactionResultEntity();
    resultEntity.setResponseCode(ResponseCode.TRANSACTION_FINISHED_BUT_DESTINATION_FULL.getCode());
    doReturn(resultEntity).when(apiClient).commitTransferFlowFiles(eq(transactionUrl), eq(CONFIRM_TRANSACTION));
    ByteArrayOutputStream serverResponseBos = new ByteArrayOutputStream();
    ByteArrayInputStream serverResponse = new ByteArrayInputStream(serverResponseBos.toByteArray());
    ByteArrayOutputStream clientRequest = new ByteArrayOutputStream();
    HttpClientTransaction transaction = getClientTransaction(serverResponse, clientRequest, apiClient, TransferDirection.SEND, transactionUrl);
    execSendButDestinationFull(transaction);
    InputStream sentByClient = new ByteArrayInputStream(clientRequest.toByteArray());
    DataPacket packetByClient = codec.decode(sentByClient);
    assertEquals("contents on client 1", readContents(packetByClient));
    packetByClient = codec.decode(sentByClient);
    assertEquals("contents on client 2", readContents(packetByClient));
    assertEquals(-1, sentByClient.read());
    verify(apiClient).commitTransferFlowFiles(transactionUrl, CONFIRM_TRANSACTION);
}
Also used : TransactionResultEntity(org.apache.nifi.web.api.entity.TransactionResultEntity) HttpCommunicationsSession(org.apache.nifi.remote.io.http.HttpCommunicationsSession) ByteArrayInputStream(org.apache.nifi.stream.io.ByteArrayInputStream) InputStream(java.io.InputStream) SiteToSiteRestApiClient(org.apache.nifi.remote.util.SiteToSiteRestApiClient) Peer(org.apache.nifi.remote.Peer) CommunicationsSession(org.apache.nifi.remote.protocol.CommunicationsSession) HttpCommunicationsSession(org.apache.nifi.remote.io.http.HttpCommunicationsSession) ByteArrayOutputStream(org.apache.nifi.stream.io.ByteArrayOutputStream) DataPacket(org.apache.nifi.remote.protocol.DataPacket) SiteToSiteTestUtils.createDataPacket(org.apache.nifi.remote.protocol.SiteToSiteTestUtils.createDataPacket) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ByteArrayInputStream(org.apache.nifi.stream.io.ByteArrayInputStream) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Test(org.junit.Test)

Example 24 with ByteArrayInputStream

use of org.apache.nifi.stream.io.ByteArrayInputStream in project nifi by apache.

the class TestSocketClientTransaction method testReceiveOneFlowFile.

@Test
public void testReceiveOneFlowFile() throws IOException {
    ByteArrayOutputStream serverResponseBos = new ByteArrayOutputStream();
    DataOutputStream serverResponse = new DataOutputStream(serverResponseBos);
    ResponseCode.MORE_DATA.writeResponse(serverResponse);
    codec.encode(createDataPacket("contents on server 1"), serverResponse);
    ResponseCode.FINISH_TRANSACTION.writeResponse(serverResponse);
    ResponseCode.CONFIRM_TRANSACTION.writeResponse(serverResponse, "Checksum has been verified at server.");
    ByteArrayInputStream bis = new ByteArrayInputStream(serverResponseBos.toByteArray());
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    SocketClientTransaction transaction = getClientTransaction(bis, bos, TransferDirection.RECEIVE);
    execReceiveOneFlowFile(transaction);
    // Verify what client has sent.
    DataInputStream sentByClient = new DataInputStream(new ByteArrayInputStream(bos.toByteArray()));
    assertEquals(RequestType.RECEIVE_FLOWFILES, RequestType.readRequestType(sentByClient));
    Response confirmResponse = Response.read(sentByClient);
    assertEquals(ResponseCode.CONFIRM_TRANSACTION, confirmResponse.getCode());
    assertEquals("Checksum should be calculated at client", "3680976076", confirmResponse.getMessage());
    Response completeResponse = Response.read(sentByClient);
    assertEquals(ResponseCode.TRANSACTION_FINISHED, completeResponse.getCode());
    assertEquals(-1, sentByClient.read());
}
Also used : Response(org.apache.nifi.remote.protocol.Response) ByteArrayInputStream(org.apache.nifi.stream.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(org.apache.nifi.stream.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream) Test(org.junit.Test)

Example 25 with ByteArrayInputStream

use of org.apache.nifi.stream.io.ByteArrayInputStream in project nifi by apache.

the class TestSocketClientTransaction method testSendWithInvalidChecksum.

@Test
public void testSendWithInvalidChecksum() throws IOException {
    ByteArrayOutputStream serverResponseBos = new ByteArrayOutputStream();
    DataOutputStream serverResponse = new DataOutputStream(serverResponseBos);
    ResponseCode.CONFIRM_TRANSACTION.writeResponse(serverResponse, "Different checksum");
    ByteArrayInputStream bis = new ByteArrayInputStream(serverResponseBos.toByteArray());
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    SocketClientTransaction transaction = getClientTransaction(bis, bos, TransferDirection.SEND);
    execSendWithInvalidChecksum(transaction);
    // Verify what client has sent.
    DataInputStream sentByClient = new DataInputStream(new ByteArrayInputStream(bos.toByteArray()));
    assertEquals(RequestType.SEND_FLOWFILES, RequestType.readRequestType(sentByClient));
    DataPacket packetByClient = codec.decode(sentByClient);
    assertEquals("contents on client 1", readContents(packetByClient));
    Response continueDataResponse = Response.read(sentByClient);
    assertEquals(ResponseCode.CONTINUE_TRANSACTION, continueDataResponse.getCode());
    packetByClient = codec.decode(sentByClient);
    assertEquals("contents on client 2", readContents(packetByClient));
    Response endOfDataResponse = Response.read(sentByClient);
    assertEquals(ResponseCode.FINISH_TRANSACTION, endOfDataResponse.getCode());
    Response confirmResponse = Response.read(sentByClient);
    assertEquals(ResponseCode.BAD_CHECKSUM, confirmResponse.getCode());
    assertEquals(-1, sentByClient.read());
}
Also used : Response(org.apache.nifi.remote.protocol.Response) ByteArrayInputStream(org.apache.nifi.stream.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(org.apache.nifi.stream.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream) DataPacket(org.apache.nifi.remote.protocol.DataPacket) SiteToSiteTestUtils.createDataPacket(org.apache.nifi.remote.protocol.SiteToSiteTestUtils.createDataPacket) Test(org.junit.Test)

Aggregations

ByteArrayInputStream (org.apache.nifi.stream.io.ByteArrayInputStream)29 ByteArrayOutputStream (org.apache.nifi.stream.io.ByteArrayOutputStream)22 Test (org.junit.Test)20 DataInputStream (java.io.DataInputStream)12 DataOutputStream (java.io.DataOutputStream)10 Peer (org.apache.nifi.remote.Peer)9 SiteToSiteRestApiClient (org.apache.nifi.remote.util.SiteToSiteRestApiClient)9 DataPacket (org.apache.nifi.remote.protocol.DataPacket)8 SiteToSiteTestUtils.createDataPacket (org.apache.nifi.remote.protocol.SiteToSiteTestUtils.createDataPacket)8 TransactionResultEntity (org.apache.nifi.web.api.entity.TransactionResultEntity)8 InputStream (java.io.InputStream)7 Response (org.apache.nifi.remote.protocol.Response)7 HttpCommunicationsSession (org.apache.nifi.remote.io.http.HttpCommunicationsSession)5 GenericRecord (org.apache.avro.generic.GenericRecord)4 CommunicationsSession (org.apache.nifi.remote.protocol.CommunicationsSession)4 MockFlowFile (org.apache.nifi.util.MockFlowFile)4 Mockito.doAnswer (org.mockito.Mockito.doAnswer)4 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 Answer (org.mockito.stubbing.Answer)4 DataFileStream (org.apache.avro.file.DataFileStream)3