Search in sources :

Example 6 with TransactionResultEntity

use of org.apache.nifi.web.api.entity.TransactionResultEntity in project nifi by apache.

the class TestHttpClientTransaction method testSendOneFlowFile.

@Test
public void testSendOneFlowFile() 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));
    // 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("2946083981");
            return null;
        }
    }).when(apiClient).finishTransferFlowFiles(any(CommunicationsSession.class));
    TransactionResultEntity resultEntity = new TransactionResultEntity();
    resultEntity.setResponseCode(ResponseCode.TRANSACTION_FINISHED.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);
    execSendOneFlowFile(transaction);
    InputStream sentByClient = new ByteArrayInputStream(clientRequest.toByteArray());
    DataPacket packetByClient = codec.decode(sentByClient);
    assertEquals("contents on client 1", 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 7 with TransactionResultEntity

use of org.apache.nifi.web.api.entity.TransactionResultEntity in project nifi by apache.

the class TestDataTransferResource method testCreateTransactionPortNotFound.

@Test
public void testCreateTransactionPortNotFound() throws Exception {
    final HttpServletRequest req = createCommonHttpServletRequest();
    final DataTransferResource resource = getDataTransferResource();
    final HttpFlowFileServerProtocol serverProtocol = resource.getHttpFlowFileServerProtocol(null);
    doThrow(new HandshakeException(ResponseCode.UNKNOWN_PORT, "Not found.")).when(serverProtocol).handshake(any());
    final ServletContext context = null;
    final UriInfo uriInfo = null;
    final InputStream inputStream = null;
    final Response response = resource.createPortTransaction("input-ports", "port-id", req, context, uriInfo, inputStream);
    TransactionResultEntity resultEntity = (TransactionResultEntity) response.getEntity();
    assertEquals(404, response.getStatus());
    assertEquals(ResponseCode.UNKNOWN_PORT.getCode(), resultEntity.getResponseCode());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(javax.ws.rs.core.Response) TransactionResultEntity(org.apache.nifi.web.api.entity.TransactionResultEntity) InputStream(java.io.InputStream) HttpFlowFileServerProtocol(org.apache.nifi.remote.protocol.http.HttpFlowFileServerProtocol) ServletContext(javax.servlet.ServletContext) HandshakeException(org.apache.nifi.remote.exception.HandshakeException) UriInfo(javax.ws.rs.core.UriInfo) Test(org.junit.Test)

Example 8 with TransactionResultEntity

use of org.apache.nifi.web.api.entity.TransactionResultEntity in project nifi by apache.

the class TestDataTransferResource method testCreateTransactionPortNotInValidState.

@Test
public void testCreateTransactionPortNotInValidState() throws Exception {
    final HttpServletRequest req = createCommonHttpServletRequest();
    final DataTransferResource resource = getDataTransferResource();
    final HttpFlowFileServerProtocol serverProtocol = resource.getHttpFlowFileServerProtocol(null);
    doThrow(new HandshakeException(ResponseCode.PORT_NOT_IN_VALID_STATE, "Not in valid state.")).when(serverProtocol).handshake(any());
    final ServletContext context = null;
    final UriInfo uriInfo = null;
    final InputStream inputStream = null;
    final Response response = resource.createPortTransaction("input-ports", "port-id", req, context, uriInfo, inputStream);
    TransactionResultEntity resultEntity = (TransactionResultEntity) response.getEntity();
    assertEquals(503, response.getStatus());
    assertEquals(ResponseCode.PORT_NOT_IN_VALID_STATE.getCode(), resultEntity.getResponseCode());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(javax.ws.rs.core.Response) TransactionResultEntity(org.apache.nifi.web.api.entity.TransactionResultEntity) InputStream(java.io.InputStream) HttpFlowFileServerProtocol(org.apache.nifi.remote.protocol.http.HttpFlowFileServerProtocol) ServletContext(javax.servlet.ServletContext) HandshakeException(org.apache.nifi.remote.exception.HandshakeException) UriInfo(javax.ws.rs.core.UriInfo) Test(org.junit.Test)

Example 9 with TransactionResultEntity

use of org.apache.nifi.web.api.entity.TransactionResultEntity in project nifi by apache.

the class TestDataTransferResource method testCommitOutputPortTransaction.

@Test
public void testCommitOutputPortTransaction() throws Exception {
    final HttpServletRequest req = createCommonHttpServletRequest();
    final DataTransferResource resource = getDataTransferResource();
    final ServletContext context = null;
    final InputStream inputStream = null;
    final HttpRemoteSiteListener transactionManager = HttpRemoteSiteListener.getInstance(NiFiProperties.createBasicNiFiProperties(null, null));
    final String transactionId = transactionManager.createTransaction();
    final Response response = resource.commitOutputPortTransaction(ResponseCode.CONFIRM_TRANSACTION.getCode(), "client-checksum", "port-id", transactionId, req, context, inputStream);
    transactionManager.cancelTransaction(transactionId);
    TransactionResultEntity resultEntity = (TransactionResultEntity) response.getEntity();
    assertEquals(200, response.getStatus());
    assertEquals(ResponseCode.CONFIRM_TRANSACTION.getCode(), resultEntity.getResponseCode());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(javax.ws.rs.core.Response) TransactionResultEntity(org.apache.nifi.web.api.entity.TransactionResultEntity) InputStream(java.io.InputStream) HttpRemoteSiteListener(org.apache.nifi.remote.HttpRemoteSiteListener) ServletContext(javax.servlet.ServletContext) Test(org.junit.Test)

Example 10 with TransactionResultEntity

use of org.apache.nifi.web.api.entity.TransactionResultEntity in project nifi by apache.

the class TestDataTransferResource method testCreateTransactionUnauthorized.

@Test
public void testCreateTransactionUnauthorized() throws Exception {
    final HttpServletRequest req = createCommonHttpServletRequest();
    final DataTransferResource resource = getDataTransferResource();
    final HttpFlowFileServerProtocol serverProtocol = resource.getHttpFlowFileServerProtocol(null);
    doThrow(new HandshakeException(ResponseCode.UNAUTHORIZED, "Unauthorized.")).when(serverProtocol).handshake(any());
    final ServletContext context = null;
    final UriInfo uriInfo = null;
    final InputStream inputStream = null;
    final Response response = resource.createPortTransaction("input-ports", "port-id", req, context, uriInfo, inputStream);
    TransactionResultEntity resultEntity = (TransactionResultEntity) response.getEntity();
    assertEquals(401, response.getStatus());
    assertEquals(ResponseCode.UNAUTHORIZED.getCode(), resultEntity.getResponseCode());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(javax.ws.rs.core.Response) TransactionResultEntity(org.apache.nifi.web.api.entity.TransactionResultEntity) InputStream(java.io.InputStream) HttpFlowFileServerProtocol(org.apache.nifi.remote.protocol.http.HttpFlowFileServerProtocol) ServletContext(javax.servlet.ServletContext) HandshakeException(org.apache.nifi.remote.exception.HandshakeException) UriInfo(javax.ws.rs.core.UriInfo) Test(org.junit.Test)

Aggregations

TransactionResultEntity (org.apache.nifi.web.api.entity.TransactionResultEntity)24 Test (org.junit.Test)16 InputStream (java.io.InputStream)12 ByteArrayOutputStream (org.apache.nifi.stream.io.ByteArrayOutputStream)12 Peer (org.apache.nifi.remote.Peer)11 HttpServletRequest (javax.servlet.http.HttpServletRequest)9 Response (javax.ws.rs.core.Response)9 HandshakeException (org.apache.nifi.remote.exception.HandshakeException)9 ServletContext (javax.servlet.ServletContext)8 HttpServletResponse (javax.servlet.http.HttpServletResponse)8 ByteArrayInputStream (org.apache.nifi.stream.io.ByteArrayInputStream)8 SiteToSiteRestApiClient (org.apache.nifi.remote.util.SiteToSiteRestApiClient)7 HttpCommunicationsSession (org.apache.nifi.remote.io.http.HttpCommunicationsSession)6 HttpFlowFileServerProtocol (org.apache.nifi.remote.protocol.http.HttpFlowFileServerProtocol)6 IOException (java.io.IOException)5 UriInfo (javax.ws.rs.core.UriInfo)5 UnknownHostException (java.net.UnknownHostException)4 WebApplicationException (javax.ws.rs.WebApplicationException)4 AccessDeniedException (org.apache.nifi.authorization.AccessDeniedException)4 HttpRemoteSiteListener (org.apache.nifi.remote.HttpRemoteSiteListener)4