Search in sources :

Example 36 with Relationship

use of org.apache.nifi.processor.Relationship in project nifi by apache.

the class TestConnectWebSocket method testSuccess.

@Test
public void testSuccess() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(ConnectWebSocket.class);
    final ConnectWebSocket processor = (ConnectWebSocket) runner.getProcessor();
    final SharedSessionState sharedSessionState = new SharedSessionState(processor, new AtomicLong(0));
    // Use this custom session factory implementation so that createdSessions can be read from test case,
    // because MockSessionFactory doesn't expose it.
    final Set<MockProcessSession> createdSessions = new HashSet<>();
    final ProcessSessionFactory sessionFactory = () -> {
        final MockProcessSession session = new MockProcessSession(sharedSessionState, processor);
        createdSessions.add(session);
        return session;
    };
    final WebSocketClientService service = mock(WebSocketClientService.class);
    final WebSocketSession webSocketSession = spy(AbstractWebSocketSession.class);
    when(webSocketSession.getSessionId()).thenReturn("ws-session-id");
    when(webSocketSession.getLocalAddress()).thenReturn(new InetSocketAddress("localhost", 12345));
    when(webSocketSession.getRemoteAddress()).thenReturn(new InetSocketAddress("example.com", 80));
    final String serviceId = "ws-service";
    final String endpointId = "client-1";
    final String textMessageFromServer = "message from server.";
    when(service.getIdentifier()).thenReturn(serviceId);
    when(service.getTargetUri()).thenReturn("ws://example.com/web-socket");
    doAnswer(invocation -> {
        processor.connected(webSocketSession);
        // Two times.
        processor.consume(webSocketSession, textMessageFromServer);
        processor.consume(webSocketSession, textMessageFromServer);
        // Three times.
        final byte[] binaryMessage = textMessageFromServer.getBytes();
        processor.consume(webSocketSession, binaryMessage, 0, binaryMessage.length);
        processor.consume(webSocketSession, binaryMessage, 0, binaryMessage.length);
        processor.consume(webSocketSession, binaryMessage, 0, binaryMessage.length);
        return null;
    }).when(service).connect(endpointId);
    runner.addControllerService(serviceId, service);
    runner.enableControllerService(service);
    runner.setProperty(ConnectWebSocket.PROP_WEBSOCKET_CLIENT_SERVICE, serviceId);
    runner.setProperty(ConnectWebSocket.PROP_WEBSOCKET_CLIENT_ID, endpointId);
    processor.onTrigger(runner.getProcessContext(), sessionFactory);
    final Map<Relationship, List<MockFlowFile>> transferredFlowFiles = getAllTransferredFlowFiles(createdSessions, processor);
    List<MockFlowFile> connectedFlowFiles = transferredFlowFiles.get(AbstractWebSocketGatewayProcessor.REL_CONNECTED);
    assertEquals(1, connectedFlowFiles.size());
    connectedFlowFiles.forEach(ff -> {
        assertFlowFile(webSocketSession, serviceId, endpointId, ff, null);
    });
    List<MockFlowFile> textFlowFiles = transferredFlowFiles.get(AbstractWebSocketGatewayProcessor.REL_MESSAGE_TEXT);
    assertEquals(2, textFlowFiles.size());
    textFlowFiles.forEach(ff -> {
        assertFlowFile(webSocketSession, serviceId, endpointId, ff, WebSocketMessage.Type.TEXT);
    });
    List<MockFlowFile> binaryFlowFiles = transferredFlowFiles.get(AbstractWebSocketGatewayProcessor.REL_MESSAGE_BINARY);
    assertEquals(3, binaryFlowFiles.size());
    binaryFlowFiles.forEach(ff -> {
        assertFlowFile(webSocketSession, serviceId, endpointId, ff, WebSocketMessage.Type.BINARY);
    });
    final List<ProvenanceEventRecord> provenanceEvents = sharedSessionState.getProvenanceEvents();
    assertEquals(6, provenanceEvents.size());
    assertTrue(provenanceEvents.stream().allMatch(event -> ProvenanceEventType.RECEIVE.equals(event.getEventType())));
}
Also used : Mockito.spy(org.mockito.Mockito.spy) HashSet(java.util.HashSet) Relationship(org.apache.nifi.processor.Relationship) TestRunner(org.apache.nifi.util.TestRunner) ProvenanceEventRecord(org.apache.nifi.provenance.ProvenanceEventRecord) Map(java.util.Map) Mockito.doAnswer(org.mockito.Mockito.doAnswer) WebSocketSession(org.apache.nifi.websocket.WebSocketSession) MockProcessSession(org.apache.nifi.util.MockProcessSession) ProvenanceEventType(org.apache.nifi.provenance.ProvenanceEventType) WebSocketMessage(org.apache.nifi.websocket.WebSocketMessage) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) ProcessSessionFactory(org.apache.nifi.processor.ProcessSessionFactory) InetSocketAddress(java.net.InetSocketAddress) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) SharedSessionState(org.apache.nifi.util.SharedSessionState) AbstractWebSocketSession(org.apache.nifi.websocket.AbstractWebSocketSession) WebSocketClientService(org.apache.nifi.websocket.WebSocketClientService) TestRunners(org.apache.nifi.util.TestRunners) Assert.assertEquals(org.junit.Assert.assertEquals) MockFlowFile(org.apache.nifi.util.MockFlowFile) Mockito.mock(org.mockito.Mockito.mock) WebSocketClientService(org.apache.nifi.websocket.WebSocketClientService) TestRunner(org.apache.nifi.util.TestRunner) InetSocketAddress(java.net.InetSocketAddress) WebSocketSession(org.apache.nifi.websocket.WebSocketSession) AbstractWebSocketSession(org.apache.nifi.websocket.AbstractWebSocketSession) MockFlowFile(org.apache.nifi.util.MockFlowFile) SharedSessionState(org.apache.nifi.util.SharedSessionState) AtomicLong(java.util.concurrent.atomic.AtomicLong) Relationship(org.apache.nifi.processor.Relationship) ProvenanceEventRecord(org.apache.nifi.provenance.ProvenanceEventRecord) ProcessSessionFactory(org.apache.nifi.processor.ProcessSessionFactory) List(java.util.List) MockProcessSession(org.apache.nifi.util.MockProcessSession) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 37 with Relationship

use of org.apache.nifi.processor.Relationship in project nifi by apache.

the class TestJoltTransformJSON method testRelationshipsCreated.

@Test
public void testRelationshipsCreated() throws IOException {
    Processor processor = new JoltTransformJSON();
    final TestRunner runner = TestRunners.newTestRunner(processor);
    final String spec = new String(Files.readAllBytes(Paths.get("src/test/resources/TestJoltTransformJson/chainrSpec.json")));
    runner.setProperty(JoltTransformJSON.JOLT_SPEC, spec);
    runner.enqueue(JSON_INPUT);
    Set<Relationship> relationships = processor.getRelationships();
    assertTrue(relationships.contains(JoltTransformJSON.REL_FAILURE));
    assertTrue(relationships.contains(JoltTransformJSON.REL_SUCCESS));
    assertTrue(relationships.size() == 2);
}
Also used : Processor(org.apache.nifi.processor.Processor) TestRunner(org.apache.nifi.util.TestRunner) Relationship(org.apache.nifi.processor.Relationship) Test(org.junit.Test)

Example 38 with Relationship

use of org.apache.nifi.processor.Relationship in project nifi by apache.

the class WriteResourceToStream method init.

@Override
protected void init(final ProcessorInitializationContext context) {
    final Set<Relationship> relationships = new HashSet<Relationship>();
    relationships.add(REL_SUCCESS);
    relationships.add(REL_FAILURE);
    this.relationships = Collections.unmodifiableSet(relationships);
    final InputStream resourceStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("file.txt");
    try {
        this.resourceData = IOUtils.toString(resourceStream);
    } catch (IOException e) {
        throw new RuntimeException("Unable to load resources", e);
    } finally {
        IOUtils.closeQuietly(resourceStream);
    }
}
Also used : InputStream(java.io.InputStream) Relationship(org.apache.nifi.processor.Relationship) IOException(java.io.IOException) HashSet(java.util.HashSet)

Example 39 with Relationship

use of org.apache.nifi.processor.Relationship in project nifi by apache.

the class MockProcessSession method assertAllFlowFilesTransferred.

/**
 * Asserts that all FlowFiles that were transferred were transferred to the
 * given relationship
 *
 * @param relationship to validate
 */
public void assertAllFlowFilesTransferred(final Relationship relationship) {
    for (final Map.Entry<Relationship, List<MockFlowFile>> entry : transferMap.entrySet()) {
        final Relationship rel = entry.getKey();
        final List<MockFlowFile> flowFiles = entry.getValue();
        if (!rel.equals(relationship) && flowFiles != null && !flowFiles.isEmpty()) {
            Assert.fail("Expected all Transferred FlowFiles to go to " + relationship + " but " + flowFiles.size() + " were routed to " + rel);
        }
    }
}
Also used : Relationship(org.apache.nifi.processor.Relationship) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 40 with Relationship

use of org.apache.nifi.processor.Relationship in project nifi by apache.

the class MockProcessSession method migrate.

private void migrate(final MockProcessSession newOwner, final Collection<MockFlowFile> flowFiles) {
    for (final FlowFile flowFile : flowFiles) {
        if (openInputStreams.containsKey(flowFile)) {
            throw new IllegalStateException(flowFile + " cannot be migrated to a new Process Session because this session currently " + "has an open InputStream for the FlowFile, created by calling ProcessSession.read(FlowFile)");
        }
        if (openOutputStreams.containsKey(flowFile)) {
            throw new IllegalStateException(flowFile + " cannot be migrated to a new Process Session because this session currently " + "has an open OutputStream for the FlowFile, created by calling ProcessSession.write(FlowFile)");
        }
        if (readRecursionSet.containsKey(flowFile)) {
            throw new IllegalStateException(flowFile + " already in use for an active callback or InputStream created by ProcessSession.read(FlowFile) has not been closed");
        }
        if (writeRecursionSet.contains(flowFile)) {
            throw new IllegalStateException(flowFile + " already in use for an active callback or OutputStream created by ProcessSession.write(FlowFile) has not been closed");
        }
        final FlowFile currentVersion = currentVersions.get(flowFile.getId());
        if (currentVersion == null) {
            throw new FlowFileHandlingException(flowFile + " is not known in this session");
        }
    }
    for (final Map.Entry<Relationship, List<MockFlowFile>> entry : transferMap.entrySet()) {
        final Relationship relationship = entry.getKey();
        final List<MockFlowFile> transferredFlowFiles = entry.getValue();
        for (final MockFlowFile flowFile : flowFiles) {
            if (transferredFlowFiles.remove(flowFile)) {
                newOwner.transferMap.computeIfAbsent(relationship, rel -> new ArrayList<>()).add(flowFile);
            }
        }
    }
    for (final MockFlowFile flowFile : flowFiles) {
        if (beingProcessed.remove(flowFile.getId())) {
            newOwner.beingProcessed.add(flowFile.getId());
        }
        if (penalized.remove(flowFile)) {
            newOwner.penalized.add(flowFile);
        }
        if (currentVersions.containsKey(flowFile.getId())) {
            newOwner.currentVersions.put(flowFile.getId(), currentVersions.remove(flowFile.getId()));
        }
        if (originalVersions.containsKey(flowFile.getId())) {
            newOwner.originalVersions.put(flowFile.getId(), originalVersions.remove(flowFile.getId()));
        }
        if (removedFlowFiles.remove(flowFile.getId())) {
            newOwner.removedFlowFiles.add(flowFile.getId());
        }
    }
    final Set<String> flowFileIds = flowFiles.stream().map(ff -> ff.getAttribute(CoreAttributes.UUID.key())).collect(Collectors.toSet());
    provenanceReporter.migrate(newOwner.provenanceReporter, flowFileIds);
}
Also used : OutputStreamCallback(org.apache.nifi.processor.io.OutputStreamCallback) Arrays(java.util.Arrays) FlowFileFilter(org.apache.nifi.processor.FlowFileFilter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HashMap(java.util.HashMap) FlowFileHandlingException(org.apache.nifi.processor.exception.FlowFileHandlingException) QueueSize(org.apache.nifi.controller.queue.QueueSize) ProcessException(org.apache.nifi.processor.exception.ProcessException) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ProvenanceReporter(org.apache.nifi.provenance.ProvenanceReporter) ByteArrayInputStream(java.io.ByteArrayInputStream) Relationship(org.apache.nifi.processor.Relationship) Map(java.util.Map) Path(java.nio.file.Path) InputStreamCallback(org.apache.nifi.processor.io.InputStreamCallback) OutputStream(java.io.OutputStream) FlowFileAccessException(org.apache.nifi.processor.exception.FlowFileAccessException) Iterator(java.util.Iterator) FlowFile(org.apache.nifi.flowfile.FlowFile) Files(java.nio.file.Files) OpenOption(java.nio.file.OpenOption) Collection(java.util.Collection) StandardOpenOption(java.nio.file.StandardOpenOption) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) IOException(java.io.IOException) ProcessSession(org.apache.nifi.processor.ProcessSession) Collectors(java.util.stream.Collectors) File(java.io.File) Objects(java.util.Objects) Processor(org.apache.nifi.processor.Processor) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Closeable(java.io.Closeable) Pattern(java.util.regex.Pattern) CoreAttributes(org.apache.nifi.flowfile.attributes.CoreAttributes) Assert(org.junit.Assert) Collections(java.util.Collections) StreamCallback(org.apache.nifi.processor.io.StreamCallback) InputStream(java.io.InputStream) FlowFile(org.apache.nifi.flowfile.FlowFile) ArrayList(java.util.ArrayList) Relationship(org.apache.nifi.processor.Relationship) FlowFileHandlingException(org.apache.nifi.processor.exception.FlowFileHandlingException) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

Relationship (org.apache.nifi.processor.Relationship)106 ArrayList (java.util.ArrayList)41 HashSet (java.util.HashSet)40 HashMap (java.util.HashMap)32 FlowFile (org.apache.nifi.flowfile.FlowFile)32 Map (java.util.Map)31 IOException (java.io.IOException)26 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)26 Test (org.junit.Test)23 List (java.util.List)20 Set (java.util.Set)19 Connection (org.apache.nifi.connectable.Connection)18 TestRunner (org.apache.nifi.util.TestRunner)18 ProcessException (org.apache.nifi.processor.exception.ProcessException)17 ProcessSession (org.apache.nifi.processor.ProcessSession)15 InputStream (java.io.InputStream)14 DynamicRelationship (org.apache.nifi.annotation.behavior.DynamicRelationship)12 Processor (org.apache.nifi.processor.Processor)12 Collections (java.util.Collections)11 AtomicLong (java.util.concurrent.atomic.AtomicLong)10