Search in sources :

Example 1 with UntypedRecordableEventDescriptor

use of org.xwiki.eventstream.UntypedRecordableEventDescriptor in project xwiki-platform by xwiki.

the class UntypedEventListener method onEvent.

@Override
public void onEvent(Event event, Object source, Object data) {
    try {
        // Get every UntypedEventDescriptor registered in the ComponentManager
        List<UntypedRecordableEventDescriptor> descriptors = this.componentManagerProvider.get().getInstanceList(UntypedRecordableEventDescriptor.class);
        // Filter the event descriptors concerned by the event, then create the concerned events
        for (UntypedRecordableEventDescriptor descriptor : descriptors) {
            // If the event is expected by our descriptor
            if (eventMatchesDescriptor(event, source, descriptor)) {
                Set<String> target = getTarget(event, source, descriptor.getAuthorReference(), descriptor.getTargetExpression());
                observationManager.notify(new DefaultUntypedRecordableEvent(descriptor.getEventType(), target), EVENT_STREAM_MODULE, source);
            }
        }
    } catch (ComponentLookupException e) {
        logger.error("Unable to retrieve a list of registered UntypedRecordableEventDescriptor " + "from the ComponentManager.", e);
    }
}
Also used : UntypedRecordableEventDescriptor(org.xwiki.eventstream.UntypedRecordableEventDescriptor) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException)

Example 2 with UntypedRecordableEventDescriptor

use of org.xwiki.eventstream.UntypedRecordableEventDescriptor in project xwiki-platform by xwiki.

the class UntypedEventListenerTest method onEventWithTarget.

@Test
public void onEventWithTarget() throws Exception {
    // Mocks
    UntypedRecordableEventDescriptor descriptor = mockDescriptor();
    when(descriptor.getTargetExpression()).thenReturn("someVelocityCode");
    // Target velocity
    when(scriptContext.getAttribute("xreturn")).thenReturn(Arrays.asList("UserA", "UserB"));
    // Test
    mocker.getComponentUnderTest().onEvent(new DocumentUpdatedEvent(), mock(Object.class), null);
    // Verify
    verify(this.observationManager, times(1)).notify(any(DefaultUntypedRecordableEvent.class), any(), any());
    assertNotNull(answer.getSentEvent());
    assertNotNull(answer.getSentEvent().getTarget());
    assertTrue(answer.getSentEvent().getTarget().contains("UserA"));
    assertTrue(answer.getSentEvent().getTarget().contains("UserB"));
    assertEquals(2, answer.getSentEvent().getTarget().size());
}
Also used : UntypedRecordableEventDescriptor(org.xwiki.eventstream.UntypedRecordableEventDescriptor) DocumentUpdatedEvent(org.xwiki.bridge.event.DocumentUpdatedEvent) Test(org.junit.Test)

Example 3 with UntypedRecordableEventDescriptor

use of org.xwiki.eventstream.UntypedRecordableEventDescriptor in project xwiki-platform by xwiki.

the class UntypedEventListenerTest method onEventWithCorrectValidation2.

@Test
public void onEventWithCorrectValidation2() throws Exception {
    // Mocks
    UntypedRecordableEventDescriptor descriptor = mockDescriptor();
    when(descriptor.getValidationExpression()).thenReturn("someVelocityCode");
    doAnswer(invocationOnMock -> {
        WikiPrinter wikiPrinter = invocationOnMock.getArgument(1);
        wikiPrinter.println("    true ");
        return null;
    }).when(renderer).render(any(XDOM.class), any(WikiPrinter.class));
    // Test
    mocker.getComponentUnderTest().onEvent(new DocumentUpdatedEvent(), mock(Object.class), null);
    // Verify
    verify(this.observationManager, times(1)).notify(any(DefaultUntypedRecordableEvent.class), any(), any());
    assertNotNull(answer.getSentEvent());
    assertEquals("myCustomEvent", answer.getSentEvent().getEventType());
}
Also used : UntypedRecordableEventDescriptor(org.xwiki.eventstream.UntypedRecordableEventDescriptor) XDOM(org.xwiki.rendering.block.XDOM) DocumentUpdatedEvent(org.xwiki.bridge.event.DocumentUpdatedEvent) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) Test(org.junit.Test)

Example 4 with UntypedRecordableEventDescriptor

use of org.xwiki.eventstream.UntypedRecordableEventDescriptor in project xwiki-platform by xwiki.

the class UntypedEventListenerTest method onEventWithIncorrectValidation2.

@Test
public void onEventWithIncorrectValidation2() throws Exception {
    // Mocks
    UntypedRecordableEventDescriptor descriptor = mockDescriptor();
    when(descriptor.getValidationExpression()).thenReturn("someVelocityCode");
    // Test
    mocker.getComponentUnderTest().onEvent(new DocumentUpdatedEvent(), mock(Object.class), null);
    // Verify
    verify(this.observationManager, never()).notify(any(), any(), any());
    assertNull(answer.getSentEvent());
}
Also used : UntypedRecordableEventDescriptor(org.xwiki.eventstream.UntypedRecordableEventDescriptor) DocumentUpdatedEvent(org.xwiki.bridge.event.DocumentUpdatedEvent) Test(org.junit.Test)

Example 5 with UntypedRecordableEventDescriptor

use of org.xwiki.eventstream.UntypedRecordableEventDescriptor in project xwiki-platform by xwiki.

the class UntypedEventListenerTest method mockDescriptor.

private UntypedRecordableEventDescriptor mockDescriptor() throws Exception {
    UntypedRecordableEventDescriptor descriptor = mock(UntypedRecordableEventDescriptor.class);
    when(descriptor.getEventTriggers()).thenReturn(Arrays.asList(DocumentUpdatedEvent.class.getCanonicalName()));
    when(descriptor.getEventType()).thenReturn("myCustomEvent");
    when(modelBridge.checkXObjectPresence(any(), any())).thenReturn(true);
    when(componentManager.getInstanceList(any())).thenReturn(Arrays.asList(descriptor));
    return descriptor;
}
Also used : UntypedRecordableEventDescriptor(org.xwiki.eventstream.UntypedRecordableEventDescriptor)

Aggregations

UntypedRecordableEventDescriptor (org.xwiki.eventstream.UntypedRecordableEventDescriptor)7 Test (org.junit.Test)5 DocumentUpdatedEvent (org.xwiki.bridge.event.DocumentUpdatedEvent)5 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)1 XDOM (org.xwiki.rendering.block.XDOM)1 WikiPrinter (org.xwiki.rendering.renderer.printer.WikiPrinter)1