Search in sources :

Example 71 with ProvenanceEventRecord

use of org.apache.nifi.provenance.ProvenanceEventRecord in project nifi by apache.

the class TestPutWebSocket method testServiceIsNotWebSocketService.

@Test
public void testServiceIsNotWebSocketService() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(PutWebSocket.class);
    final ControllerService service = spy(ControllerService.class);
    final WebSocketSession webSocketSession = getWebSocketSession();
    final String serviceId = "ws-service";
    final String endpointId = "client-1";
    final String textMessageFromServer = "message from server.";
    when(service.getIdentifier()).thenReturn(serviceId);
    runner.addControllerService(serviceId, service);
    runner.enableControllerService(service);
    final Map<String, String> attributes = new HashMap<>();
    attributes.put(ATTR_WS_CS_ID, serviceId);
    attributes.put(ATTR_WS_ENDPOINT_ID, endpointId);
    attributes.put(ATTR_WS_SESSION_ID, webSocketSession.getSessionId());
    runner.enqueue(textMessageFromServer, attributes);
    runner.run();
    final List<MockFlowFile> succeededFlowFiles = runner.getFlowFilesForRelationship(PutWebSocket.REL_SUCCESS);
    assertEquals(0, succeededFlowFiles.size());
    final List<MockFlowFile> failedFlowFiles = runner.getFlowFilesForRelationship(PutWebSocket.REL_FAILURE);
    assertEquals(1, failedFlowFiles.size());
    final MockFlowFile failedFlowFile = failedFlowFiles.iterator().next();
    assertNotNull(failedFlowFile.getAttribute(ATTR_WS_FAILURE_DETAIL));
    final List<ProvenanceEventRecord> provenanceEvents = runner.getProvenanceEvents();
    assertEquals(0, provenanceEvents.size());
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) HashMap(java.util.HashMap) TestRunner(org.apache.nifi.util.TestRunner) ProvenanceEventRecord(org.apache.nifi.provenance.ProvenanceEventRecord) Matchers.anyString(org.mockito.Matchers.anyString) ControllerService(org.apache.nifi.controller.ControllerService) WebSocketSession(org.apache.nifi.websocket.WebSocketSession) AbstractWebSocketSession(org.apache.nifi.websocket.AbstractWebSocketSession) Test(org.junit.Test)

Example 72 with ProvenanceEventRecord

use of org.apache.nifi.provenance.ProvenanceEventRecord in project nifi by apache.

the class TestPutWebSocket method testSendFailure.

@Test
public void testSendFailure() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(PutWebSocket.class);
    final WebSocketService service = spy(WebSocketService.class);
    final WebSocketSession webSocketSession = getWebSocketSession();
    final String serviceId = "ws-service";
    final String endpointId = "client-1";
    final String textMessageFromServer = "message from server.";
    when(service.getIdentifier()).thenReturn(serviceId);
    doAnswer(invocation -> {
        final SendMessage sendMessage = invocation.getArgumentAt(2, SendMessage.class);
        sendMessage.send(webSocketSession);
        return null;
    }).when(service).sendMessage(anyString(), anyString(), any(SendMessage.class));
    doThrow(new IOException("Sending message failed.")).when(webSocketSession).sendString(anyString());
    runner.addControllerService(serviceId, service);
    runner.enableControllerService(service);
    final Map<String, String> attributes = new HashMap<>();
    attributes.put(ATTR_WS_CS_ID, serviceId);
    attributes.put(ATTR_WS_ENDPOINT_ID, endpointId);
    attributes.put(ATTR_WS_SESSION_ID, webSocketSession.getSessionId());
    runner.enqueue(textMessageFromServer, attributes);
    runner.run();
    final List<MockFlowFile> succeededFlowFiles = runner.getFlowFilesForRelationship(PutWebSocket.REL_SUCCESS);
    assertEquals(0, succeededFlowFiles.size());
    final List<MockFlowFile> failedFlowFiles = runner.getFlowFilesForRelationship(PutWebSocket.REL_FAILURE);
    assertEquals(1, failedFlowFiles.size());
    final MockFlowFile failedFlowFile = failedFlowFiles.iterator().next();
    assertNotNull(failedFlowFile.getAttribute(ATTR_WS_FAILURE_DETAIL));
    final List<ProvenanceEventRecord> provenanceEvents = runner.getProvenanceEvents();
    assertEquals(0, provenanceEvents.size());
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) HashMap(java.util.HashMap) TestRunner(org.apache.nifi.util.TestRunner) ProvenanceEventRecord(org.apache.nifi.provenance.ProvenanceEventRecord) WebSocketService(org.apache.nifi.websocket.WebSocketService) Matchers.anyString(org.mockito.Matchers.anyString) IOException(java.io.IOException) WebSocketSession(org.apache.nifi.websocket.WebSocketSession) AbstractWebSocketSession(org.apache.nifi.websocket.AbstractWebSocketSession) SendMessage(org.apache.nifi.websocket.SendMessage) Test(org.junit.Test)

Example 73 with ProvenanceEventRecord

use of org.apache.nifi.provenance.ProvenanceEventRecord in project nifi by apache.

the class TestPutWebSocket method testSuccess.

@Test
public void testSuccess() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(PutWebSocket.class);
    final WebSocketService service = spy(WebSocketService.class);
    final WebSocketSession webSocketSession = getWebSocketSession();
    final String serviceId = "ws-service";
    final String endpointId = "client-1";
    final String textMessageFromServer = "message from server.";
    when(service.getIdentifier()).thenReturn(serviceId);
    doAnswer(invocation -> {
        final SendMessage sendMessage = invocation.getArgumentAt(2, SendMessage.class);
        sendMessage.send(webSocketSession);
        return null;
    }).when(service).sendMessage(anyString(), anyString(), any(SendMessage.class));
    runner.addControllerService(serviceId, service);
    runner.enableControllerService(service);
    runner.setProperty(PutWebSocket.PROP_WS_MESSAGE_TYPE, "${" + ATTR_WS_MESSAGE_TYPE + "}");
    // Enqueue 1st file as Text.
    final Map<String, String> attributes = new HashMap<>();
    attributes.put(ATTR_WS_CS_ID, serviceId);
    attributes.put(ATTR_WS_ENDPOINT_ID, endpointId);
    attributes.put(ATTR_WS_SESSION_ID, webSocketSession.getSessionId());
    attributes.put(ATTR_WS_MESSAGE_TYPE, WebSocketMessage.Type.TEXT.name());
    runner.enqueue(textMessageFromServer, attributes);
    // Enqueue 2nd file as Binary.
    attributes.put(ATTR_WS_MESSAGE_TYPE, WebSocketMessage.Type.BINARY.name());
    runner.enqueue(textMessageFromServer.getBytes(), attributes);
    runner.run(2);
    final List<MockFlowFile> succeededFlowFiles = runner.getFlowFilesForRelationship(PutWebSocket.REL_SUCCESS);
    assertEquals(2, succeededFlowFiles.size());
    assertFlowFile(webSocketSession, serviceId, endpointId, succeededFlowFiles.get(0), WebSocketMessage.Type.TEXT);
    assertFlowFile(webSocketSession, serviceId, endpointId, succeededFlowFiles.get(1), WebSocketMessage.Type.BINARY);
    final List<MockFlowFile> failedFlowFiles = runner.getFlowFilesForRelationship(PutWebSocket.REL_FAILURE);
    assertEquals(0, failedFlowFiles.size());
    final List<ProvenanceEventRecord> provenanceEvents = runner.getProvenanceEvents();
    assertEquals(2, provenanceEvents.size());
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) HashMap(java.util.HashMap) TestRunner(org.apache.nifi.util.TestRunner) ProvenanceEventRecord(org.apache.nifi.provenance.ProvenanceEventRecord) WebSocketService(org.apache.nifi.websocket.WebSocketService) Matchers.anyString(org.mockito.Matchers.anyString) WebSocketSession(org.apache.nifi.websocket.WebSocketSession) AbstractWebSocketSession(org.apache.nifi.websocket.AbstractWebSocketSession) SendMessage(org.apache.nifi.websocket.SendMessage) Test(org.junit.Test)

Example 74 with ProvenanceEventRecord

use of org.apache.nifi.provenance.ProvenanceEventRecord in project nifi by apache.

the class TestPutWebSocket method testServiceIsNotFound.

@Test
public void testServiceIsNotFound() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(PutWebSocket.class);
    final ControllerService service = spy(ControllerService.class);
    final WebSocketSession webSocketSession = getWebSocketSession();
    final String serviceId = "ws-service";
    final String endpointId = "client-1";
    final String textMessageFromServer = "message from server.";
    when(service.getIdentifier()).thenReturn(serviceId);
    runner.addControllerService(serviceId, service);
    runner.enableControllerService(service);
    final Map<String, String> attributes = new HashMap<>();
    attributes.put(ATTR_WS_CS_ID, "different-service-id");
    attributes.put(ATTR_WS_ENDPOINT_ID, endpointId);
    attributes.put(ATTR_WS_SESSION_ID, webSocketSession.getSessionId());
    runner.enqueue(textMessageFromServer, attributes);
    runner.run();
    final List<MockFlowFile> succeededFlowFiles = runner.getFlowFilesForRelationship(PutWebSocket.REL_SUCCESS);
    assertEquals(0, succeededFlowFiles.size());
    final List<MockFlowFile> failedFlowFiles = runner.getFlowFilesForRelationship(PutWebSocket.REL_FAILURE);
    assertEquals(1, failedFlowFiles.size());
    final MockFlowFile failedFlowFile = failedFlowFiles.iterator().next();
    assertNotNull(failedFlowFile.getAttribute(ATTR_WS_FAILURE_DETAIL));
    final List<ProvenanceEventRecord> provenanceEvents = runner.getProvenanceEvents();
    assertEquals(0, provenanceEvents.size());
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) HashMap(java.util.HashMap) TestRunner(org.apache.nifi.util.TestRunner) ProvenanceEventRecord(org.apache.nifi.provenance.ProvenanceEventRecord) Matchers.anyString(org.mockito.Matchers.anyString) ControllerService(org.apache.nifi.controller.ControllerService) WebSocketSession(org.apache.nifi.websocket.WebSocketSession) AbstractWebSocketSession(org.apache.nifi.websocket.AbstractWebSocketSession) Test(org.junit.Test)

Example 75 with ProvenanceEventRecord

use of org.apache.nifi.provenance.ProvenanceEventRecord in project nifi by apache.

the class TestInvokeHttpCommon method test200Auth.

@Test
public void test200Auth() throws Exception {
    addHandler(new BasicAuthHandler());
    final String username = "basic_user";
    final String password = "basic_password";
    runner.setProperty(InvokeHTTP.PROP_URL, url + "/status/200");
    runner.setProperty(InvokeHTTP.PROP_BASIC_AUTH_USERNAME, username);
    runner.setProperty(InvokeHTTP.PROP_BASIC_AUTH_PASSWORD, password);
    final byte[] creds = String.format("%s:%s", username, password).getBytes(StandardCharsets.UTF_8);
    final String expAuth = String.format("Basic %s", new String(encodeBase64(creds)));
    createFlowFiles(runner);
    runner.run();
    runner.assertTransferCount(InvokeHTTP.REL_SUCCESS_REQ, 1);
    runner.assertTransferCount(InvokeHTTP.REL_RESPONSE, 1);
    runner.assertTransferCount(InvokeHTTP.REL_RETRY, 0);
    runner.assertTransferCount(InvokeHTTP.REL_NO_RETRY, 0);
    runner.assertTransferCount(InvokeHTTP.REL_FAILURE, 0);
    runner.assertPenalizeCount(0);
    // expected in request status.code and status.message
    // original flow file (+attributes)
    final MockFlowFile bundle = runner.getFlowFilesForRelationship(InvokeHTTP.REL_SUCCESS_REQ).get(0);
    bundle.assertContentEquals("Hello".getBytes("UTF-8"));
    bundle.assertAttributeEquals(InvokeHTTP.STATUS_CODE, "200");
    bundle.assertAttributeEquals(InvokeHTTP.STATUS_MESSAGE, "OK");
    bundle.assertAttributeEquals("Foo", "Bar");
    // expected in response
    // status code, status message, all headers from server response --> ff attributes
    // server response message body into payload of ff
    final MockFlowFile bundle1 = runner.getFlowFilesForRelationship(InvokeHTTP.REL_RESPONSE).get(0);
    final String bundle1Content = new String(bundle1.toByteArray(), StandardCharsets.UTF_8);
    // use startsWith instead of equals so we can ignore line endings
    assertTrue(bundle1Content.startsWith(expAuth));
    bundle1.assertAttributeEquals(InvokeHTTP.STATUS_CODE, "200");
    bundle1.assertAttributeEquals(InvokeHTTP.STATUS_MESSAGE, "OK");
    bundle1.assertAttributeEquals("Foo", "Bar");
    bundle1.assertAttributeEquals("Content-Type", "text/plain;charset=iso-8859-1");
    final List<ProvenanceEventRecord> provEvents = runner.getProvenanceEvents();
    assertEquals(2, provEvents.size());
    boolean forkEvent = false;
    boolean fetchEvent = false;
    for (final ProvenanceEventRecord event : provEvents) {
        if (event.getEventType() == ProvenanceEventType.FORK) {
            forkEvent = true;
        } else if (event.getEventType() == ProvenanceEventType.FETCH) {
            fetchEvent = true;
        }
    }
    assertTrue(forkEvent);
    assertTrue(fetchEvent);
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) ProvenanceEventRecord(org.apache.nifi.provenance.ProvenanceEventRecord) Test(org.junit.Test)

Aggregations

ProvenanceEventRecord (org.apache.nifi.provenance.ProvenanceEventRecord)194 Test (org.junit.Test)118 StandardProvenanceEventRecord (org.apache.nifi.provenance.StandardProvenanceEventRecord)69 HashMap (java.util.HashMap)57 MockFlowFile (org.apache.nifi.util.MockFlowFile)52 ArrayList (java.util.ArrayList)36 IOException (java.io.IOException)32 TestRunner (org.apache.nifi.util.TestRunner)24 FlowFileHandlingException (org.apache.nifi.processor.exception.FlowFileHandlingException)23 DataSetRefs (org.apache.nifi.atlas.provenance.DataSetRefs)21 AnalysisContext (org.apache.nifi.atlas.provenance.AnalysisContext)20 Referenceable (org.apache.atlas.typesystem.Referenceable)19 NiFiProvenanceEventAnalyzer (org.apache.nifi.atlas.provenance.NiFiProvenanceEventAnalyzer)18 ClusterResolvers (org.apache.nifi.atlas.resolver.ClusterResolvers)18 RepositoryConfiguration (org.apache.nifi.provenance.RepositoryConfiguration)17 File (java.io.File)16 List (java.util.List)16 AtomicLong (java.util.concurrent.atomic.AtomicLong)16 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)15 Map (java.util.Map)12