Search in sources :

Example 46 with Processor

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

the class TestListenWebSocket method testSuccess.

@Test
public void testSuccess() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(ListenWebSocket.class);
    final ListenWebSocket processor = (ListenWebSocket) 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 WebSocketServerService service = mock(WebSocketServerService.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 = "/test";
    final String textMessageReceived = "message from server.";
    final AtomicReference<Boolean> registered = new AtomicReference<>(false);
    when(service.getIdentifier()).thenReturn(serviceId);
    doAnswer(invocation -> {
        registered.set(true);
        processor.connected(webSocketSession);
        // Two times.
        processor.consume(webSocketSession, textMessageReceived);
        processor.consume(webSocketSession, textMessageReceived);
        // Three times.
        final byte[] binaryMessage = textMessageReceived.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).registerProcessor(endpointId, processor);
    doAnswer(invocation -> registered.get()).when(service).isProcessorRegistered(eq(endpointId), eq(processor));
    doAnswer(invocation -> {
        registered.set(false);
        return null;
    }).when(service).deregisterProcessor(eq(endpointId), eq(processor));
    runner.addControllerService(serviceId, service);
    runner.enableControllerService(service);
    runner.setProperty(ListenWebSocket.PROP_WEBSOCKET_SERVER_SERVICE, serviceId);
    runner.setProperty(ListenWebSocket.PROP_SERVER_URL_PATH, endpointId);
    processor.onTrigger(runner.getProcessContext(), sessionFactory);
    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())));
    runner.clearTransferState();
    runner.clearProvenanceEvents();
    createdSessions.clear();
    assertEquals(0, createdSessions.size());
    // Simulate that the processor has started, and it get's triggered again
    processor.onTrigger(runner.getProcessContext(), sessionFactory);
    assertEquals("No session should be created", 0, createdSessions.size());
    // Simulate that the processor is stopped.
    processor.onStopped(runner.getProcessContext());
    assertEquals("No session should be created", 0, createdSessions.size());
    // Simulate that the processor is restarted.
    // And the mock service will emit consume msg events.
    processor.onTrigger(runner.getProcessContext(), sessionFactory);
    assertEquals("Processor should register it with the service again", 6, createdSessions.size());
}
Also used : WebSocketServerService(org.apache.nifi.websocket.WebSocketServerService) ATTR_WS_LOCAL_ADDRESS(org.apache.nifi.processors.websocket.WebSocketProcessorAttributes.ATTR_WS_LOCAL_ADDRESS) HashMap(java.util.HashMap) Mockito.spy(org.mockito.Mockito.spy) AtomicReference(java.util.concurrent.atomic.AtomicReference) ATTR_WS_SESSION_ID(org.apache.nifi.processors.websocket.WebSocketProcessorAttributes.ATTR_WS_SESSION_ID) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Relationship(org.apache.nifi.processor.Relationship) TestRunner(org.apache.nifi.util.TestRunner) ProvenanceEventRecord(org.apache.nifi.provenance.ProvenanceEventRecord) Matchers.eq(org.mockito.Matchers.eq) Map(java.util.Map) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Assert.fail(org.junit.Assert.fail) WebSocketSession(org.apache.nifi.websocket.WebSocketSession) MockProcessSession(org.apache.nifi.util.MockProcessSession) ATTR_WS_ENDPOINT_ID(org.apache.nifi.processors.websocket.WebSocketProcessorAttributes.ATTR_WS_ENDPOINT_ID) ProvenanceEventType(org.apache.nifi.provenance.ProvenanceEventType) Collection(java.util.Collection) WebSocketMessage(org.apache.nifi.websocket.WebSocketMessage) ATTR_WS_REMOTE_ADDRESS(org.apache.nifi.processors.websocket.WebSocketProcessorAttributes.ATTR_WS_REMOTE_ADDRESS) 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) Processor(org.apache.nifi.processor.Processor) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) SharedSessionState(org.apache.nifi.util.SharedSessionState) AbstractWebSocketSession(org.apache.nifi.websocket.AbstractWebSocketSession) ATTR_WS_CS_ID(org.apache.nifi.processors.websocket.WebSocketProcessorAttributes.ATTR_WS_CS_ID) ATTR_WS_MESSAGE_TYPE(org.apache.nifi.processors.websocket.WebSocketProcessorAttributes.ATTR_WS_MESSAGE_TYPE) TestRunners(org.apache.nifi.util.TestRunners) Assert.assertEquals(org.junit.Assert.assertEquals) MockFlowFile(org.apache.nifi.util.MockFlowFile) Mockito.mock(org.mockito.Mockito.mock) TestRunner(org.apache.nifi.util.TestRunner) InetSocketAddress(java.net.InetSocketAddress) AtomicReference(java.util.concurrent.atomic.AtomicReference) 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) WebSocketServerService(org.apache.nifi.websocket.WebSocketServerService) Relationship(org.apache.nifi.processor.Relationship) ProvenanceEventRecord(org.apache.nifi.provenance.ProvenanceEventRecord) ProcessSessionFactory(org.apache.nifi.processor.ProcessSessionFactory) ArrayList(java.util.ArrayList) List(java.util.List) MockProcessSession(org.apache.nifi.util.MockProcessSession) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 47 with Processor

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

the class TestWebSocketMessageRouter method testRegisterProcessor.

@Test
public void testRegisterProcessor() throws Exception {
    final WebSocketMessageRouter router = new WebSocketMessageRouter("endpoint-id");
    final Processor processor1 = mock(Processor.class);
    when(processor1.getIdentifier()).thenReturn("processor-1");
    final Processor processor2 = mock(Processor.class);
    when(processor1.getIdentifier()).thenReturn("processor-2");
    router.registerProcessor(processor1);
    try {
        router.registerProcessor(processor2);
        fail("Should fail since a processor is already registered.");
    } catch (WebSocketConfigurationException e) {
    }
    assertTrue(router.isProcessorRegistered(processor1));
    assertFalse(router.isProcessorRegistered(processor2));
    // It's safe to call deregister even if it's not registered.
    router.deregisterProcessor(processor2);
    router.deregisterProcessor(processor1);
    // It's safe to call deregister even if it's not registered.
    router.deregisterProcessor(processor2);
}
Also used : Processor(org.apache.nifi.processor.Processor) Test(org.junit.Test)

Example 48 with Processor

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

the class TestWebSocketMessageRouters method testRegisterProcessor.

@Test
public void testRegisterProcessor() throws Exception {
    final String endpointId = "endpoint-id";
    final WebSocketMessageRouters routers = new WebSocketMessageRouters();
    try {
        routers.getRouterOrFail(endpointId);
        fail("Should fail because no route exists with the endpointId.");
    } catch (WebSocketConfigurationException e) {
    }
    final Processor processor1 = mock(Processor.class);
    when(processor1.getIdentifier()).thenReturn("processor-1");
    assertFalse(routers.isProcessorRegistered(endpointId, processor1));
    routers.registerProcessor(endpointId, processor1);
    assertNotNull(routers.getRouterOrFail(endpointId));
    assertTrue(routers.isProcessorRegistered(endpointId, processor1));
    routers.deregisterProcessor(endpointId, processor1);
    assertFalse(routers.isProcessorRegistered(endpointId, processor1));
}
Also used : Processor(org.apache.nifi.processor.Processor) Test(org.junit.Test)

Example 49 with Processor

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

the class ControllerSearchServiceTest method setupMockedProcessor.

/**
 * Mocks Processor including isAuthorized() and its name & id.
 *
 * @param processorName          Desired processor name
 * @param containingProcessGroup The process group
 * @param authorizedToRead       Can the processor data be read?
 * @param variableRegistry       The variable registry
 */
private static void setupMockedProcessor(final String processorName, final ProcessGroup containingProcessGroup, boolean authorizedToRead, final MutableVariableRegistry variableRegistry) {
    final String processorId = processorName + "Id";
    final Processor processor1 = mock(Processor.class);
    final ProcessorNode processorNode1 = mock(StandardProcessorNode.class);
    Mockito.doReturn(authorizedToRead).when(processorNode1).isAuthorized(any(Authorizer.class), eq(RequestAction.READ), any(NiFiUser.class));
    Mockito.doReturn(variableRegistry).when(processorNode1).getVariableRegistry();
    Mockito.doReturn(processor1).when(processorNode1).getProcessor();
    // set processor node's attributes
    Mockito.doReturn(processorId).when(processorNode1).getIdentifier();
    // not actually searching based on versioned component id
    Mockito.doReturn(Optional.ofNullable(null)).when(processorNode1).getVersionedComponentId();
    Mockito.doReturn(processorName).when(processorNode1).getName();
    // assign processor node to its PG
    Mockito.doReturn(new HashSet<ProcessorNode>() {

        {
            add(processorNode1);
        }
    }).when(containingProcessGroup).getProcessors();
}
Also used : Processor(org.apache.nifi.processor.Processor) StandardProcessorNode(org.apache.nifi.controller.StandardProcessorNode) ProcessorNode(org.apache.nifi.controller.ProcessorNode) NiFiUser(org.apache.nifi.authorization.user.NiFiUser) Authorizer(org.apache.nifi.authorization.Authorizer)

Example 50 with Processor

use of org.apache.nifi.processor.Processor in project nifi-minifi by apache.

the class ProcessorInitializer method initialize.

@Override
public void initialize(ConfigurableComponent component) {
    Processor processor = (Processor) component;
    ProcessorInitializationContext initializationContext = new MockProcessorInitializationContext();
    try (NarCloseable narCloseable = NarCloseable.withComponentNarLoader(component.getClass(), initializationContext.getIdentifier())) {
        processor.initialize(initializationContext);
    }
}
Also used : NarCloseable(org.apache.nifi.nar.NarCloseable) Processor(org.apache.nifi.processor.Processor) MockProcessorInitializationContext(org.apache.nifi.mock.MockProcessorInitializationContext) MockProcessorInitializationContext(org.apache.nifi.mock.MockProcessorInitializationContext) ProcessorInitializationContext(org.apache.nifi.processor.ProcessorInitializationContext)

Aggregations

Processor (org.apache.nifi.processor.Processor)50 Test (org.junit.Test)19 AbstractProcessor (org.apache.nifi.processor.AbstractProcessor)13 ComponentLog (org.apache.nifi.logging.ComponentLog)12 NarCloseable (org.apache.nifi.nar.NarCloseable)12 AtomicLong (java.util.concurrent.atomic.AtomicLong)11 Relationship (org.apache.nifi.processor.Relationship)11 ProcessorNode (org.apache.nifi.controller.ProcessorNode)9 HashSet (java.util.HashSet)8 StandardComponentVariableRegistry (org.apache.nifi.registry.variable.StandardComponentVariableRegistry)8 StandardProcessorInitializationContext (org.apache.nifi.processor.StandardProcessorInitializationContext)7 StandardProcessorNode (org.apache.nifi.controller.StandardProcessorNode)6 AbstractSessionFactoryProcessor (org.apache.nifi.processor.AbstractSessionFactoryProcessor)6 ProcessorInitializationContext (org.apache.nifi.processor.ProcessorInitializationContext)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 StandardValidationContextFactory (org.apache.nifi.processor.StandardValidationContextFactory)5 Collection (java.util.Collection)4 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)4