### Question:
UntypedEventListener extends AbstractEventListener { @Override public void onEvent(Event event, Object source, Object data) { try { List<UntypedRecordableEventDescriptor> descriptors = this.componentManagerProvider.get().getInstanceList(UntypedRecordableEventDescriptor.class); for (UntypedRecordableEventDescriptor descriptor : descriptors) { 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); } } UntypedEventListener(); @Override void onEvent(Event event, Object source, Object data); static final String NAME; static final String EVENT_BINDING_NAME; static final String SOURCE_BINDING_NAME; }### Answer:
@Test public void onEventWithWrongEvent() throws Exception { mockDescriptor(); mocker.getComponentUnderTest().onEvent(new ApplicationReadyEvent(), mock(Object.class), null); verify(this.observationManager, never()).notify(any(), any(), any()); assertNull(answer.getSentEvent()); }
@Test public void onEventWithoutXObject() throws Exception { mockDescriptor(); when(modelBridge.checkXObjectPresence(any(), any())).thenReturn(false); Object source = mock(Object.class); mocker.getComponentUnderTest().onEvent(new DocumentUpdatedEvent(), source, null); verify(this.observationManager, never()).notify(any(), any(), any()); assertNull(answer.getSentEvent()); }
@Test public void onEventWithCorrectValidation() throws Exception { UntypedRecordableEventDescriptor descriptor = mockDescriptor(); when(descriptor.getValidationExpression()).thenReturn("someVelocityCode"); when(scriptContext.getAttribute("xreturn")).thenReturn(true); Object source = mock(Object.class); mocker.getComponentUnderTest().onEvent(new DocumentUpdatedEvent(), source, null); verify(this.observationManager, times(1)).notify(any(DefaultUntypedRecordableEvent.class), any(), any()); assertNotNull(answer.getSentEvent()); assertEquals("myCustomEvent", answer.getSentEvent().getEventType()); }
@Test public void onEventWithIncorrectValidation() throws Exception { UntypedRecordableEventDescriptor descriptor = mockDescriptor(); when(descriptor.getValidationExpression()).thenReturn("someVelocityCode"); when(scriptContext.getAttribute("xreturn")).thenReturn(false); mocker.getComponentUnderTest().onEvent(new DocumentUpdatedEvent(), mock(Object.class), null); verify(this.observationManager, never()).notify(any(), any(), any()); assertNull(answer.getSentEvent()); }
@Test public void onEventWithCorrectValidation2() throws Exception { 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)); mocker.getComponentUnderTest().onEvent(new DocumentUpdatedEvent(), mock(Object.class), null); verify(this.observationManager, times(1)).notify(any(DefaultUntypedRecordableEvent.class), any(), any()); assertNotNull(answer.getSentEvent()); assertEquals("myCustomEvent", answer.getSentEvent().getEventType()); }
@Test public void onEventWithIncorrectValidation2() throws Exception { UntypedRecordableEventDescriptor descriptor = mockDescriptor(); when(descriptor.getValidationExpression()).thenReturn("someVelocityCode"); mocker.getComponentUnderTest().onEvent(new DocumentUpdatedEvent(), mock(Object.class), null); verify(this.observationManager, never()).notify(any(), any(), any()); assertNull(answer.getSentEvent()); }