ServerEventRelay implements EventRelay, EventHandler { public void relayQueueEvent(MotechEvent event) { verifyEventNotNull(event); String messageDestination = event.getMessageDestination(); if (null != messageDestination) { EventListener listener = getEventListener(event, messageDestination); if (null != listener) { MotechEvent e = copyMotechEvent(event); handleQueueEvent(listener, e); } else { LOGGER.warn("Event listener with identifier {} not present to handle the event: {}", messageDestination, event); } } else { LOGGER.warn("Message destination not present in event: {}", event); } } @Autowired  ServerEventRelay(OutboundEventGateway outboundEventGateway, EventListenerRegistry eventListenerRegistry, MotechEventConfig motechEventConfig,
                            EventAdmin osgiEventAdmin, BundleContext bundleContext); @Override void sendEventMessage(MotechEvent event); @Override void broadcastEventMessage(MotechEvent event); void relayQueueEvent(MotechEvent event); void relayTopicEvent(MotechEvent event); @Override void handleEvent(Event osgiEvent);  }
@Test public void testRelayToSingleListenerWithMessageDestination() throws Exception { MotechEvent motechEvent = createEvent(LISTENER_IDENTIFIER); setUpListeners(SUBJECT, eventListener); eventRelay.relayQueueEvent(motechEvent); verify(eventListener).handle(motechEvent); }
@Test public void shouldNotifyCallbackServiceOnSuccessfulEventHandling() throws Exception { MotechEvent motechEvent = createEvent(LISTENER_IDENTIFIER); motechEvent.setCallbackName(TEST_SERVICE_CALLBACK); setUpListeners(SUBJECT, eventListener); when(bundleContext.getServiceReferences(EventCallbackService.class, null)).thenReturn(Arrays.asList(serviceReference)); when(bundleContext.getService(serviceReference)).thenReturn(callbackService); when(callbackService.getName()).thenReturn(TEST_SERVICE_CALLBACK); eventRelay.relayQueueEvent(motechEvent); verify(eventListener).handle(motechEvent); verify(callbackService).successCallback(motechEvent); }
@Test(expected = CallbackServiceNotFoundException.class) public void shouldThrowExceptionWhenCallbackServiceOfTheGivenNameIsNotFound() throws Exception { MotechEvent motechEvent = createEvent(LISTENER_IDENTIFIER); motechEvent.setCallbackName(TEST_SERVICE_CALLBACK); setUpListeners(SUBJECT, eventListener); eventRelay.relayQueueEvent(motechEvent); verify(eventListener).handle(motechEvent); verify(callbackService).successCallback(motechEvent); }
@Test public void shouldNotifyCallbackServiceOnFailedEventHandling() throws Exception { MotechEvent motechEvent = createEvent(LISTENER_IDENTIFIER); motechEvent.setCallbackName(TEST_SERVICE_CALLBACK); setUpListeners(SUBJECT, eventListener); when(bundleContext.getServiceReferences(EventCallbackService.class, null)).thenReturn(Arrays.asList(serviceReference)); when(bundleContext.getService(serviceReference)).thenReturn(callbackService); when(callbackService.getName()).thenReturn(TEST_SERVICE_CALLBACK); RuntimeException initCause = new RuntimeException(); doThrow(new RuntimeException("Failed", initCause)).when(eventListener).handle(any(MotechEvent.class)); eventRelay.relayQueueEvent(motechEvent); verify(eventListener).handle(motechEvent); verify(callbackService).failureCallback(motechEvent, initCause); }
@Test public void testRelayToSingleListenerWithoutMessageDestination() throws Exception { MotechEvent motechEvent = createEvent(); setUpListeners(SUBJECT, eventListener); eventRelay.relayQueueEvent(motechEvent); verify(eventListener, never()).handle(motechEvent); }
@Test(expected = IllegalArgumentException.class) public void testRelayNullQueueEvent() throws Exception { eventRelay.relayQueueEvent(null); }
@Test public void shouldPreserveEventDestinationIfListenerFails() { when(motechEventConfig.getMessageMaxRedeliveryCount()).thenReturn(2); BuggyListener buggyListener = new BuggyListener(1); setUpListeners(SUBJECT, buggyListener); MotechEvent event = createEvent(buggyListener.getIdentifier()); eventRelay.relayQueueEvent(event); assertThat(event.getMessageDestination().toString(), is(buggyListener.getIdentifier())); }
@Test public void testThatOnlyListenerIdentifiedByMessageDestinationHandlesEvent() throws Exception { setUpListeners(SUBJECT, eventListener, secondaryEventListener); MotechEvent motechEvent = createEvent(LISTENER_IDENTIFIER); eventRelay.relayQueueEvent(motechEvent); verify(eventListener).handle(motechEvent); verify(secondaryEventListener, never()).handle(any(MotechEvent.class)); }