Search in sources :

Example 6 with MotechEvent

use of org.motechproject.event.MotechEvent in project motech by motech.

the class ServerEventRelayTest method shouldThrowExceptionWhenCallbackServiceOfTheGivenNameIsNotFound.

@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);
}
Also used : MotechEvent(org.motechproject.event.MotechEvent) Test(org.junit.Test)

Example 7 with MotechEvent

use of org.motechproject.event.MotechEvent in project motech by motech.

the class ServerEventRelayTest method shouldStopRetryingEventHandlingAfterMaxRedeliveryCountIsHitWhenRelyingTopicEvent.

@Test
public void shouldStopRetryingEventHandlingAfterMaxRedeliveryCountIsHitWhenRelyingTopicEvent() {
    final BooleanValue handled = new BooleanValue(false);
    when(motechEventConfig.getMessageMaxRedeliveryCount()).thenReturn(2);
    when(eventListener.getIdentifier()).thenReturn("retrying");
    doThrow(new RuntimeException()).doThrow(new RuntimeException()).doThrow(new RuntimeException()).doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
            handled.setValue(true);
            return null;
        }
    }).when(eventListener).handle(any(MotechEvent.class));
    setUpListeners(SUBJECT, eventListener);
    eventRelay.relayTopicEvent(new MotechEvent(SUBJECT));
    verify(eventListener, times(3)).handle(any(MotechEvent.class));
    assertFalse(handled.getValue());
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) MotechEvent(org.motechproject.event.MotechEvent) Test(org.junit.Test)

Example 8 with MotechEvent

use of org.motechproject.event.MotechEvent in project motech by motech.

the class ServerEventRelayTest method shouldNotifyCallbackServiceOnSuccessfulEventHandling.

@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);
}
Also used : MotechEvent(org.motechproject.event.MotechEvent) Test(org.junit.Test)

Example 9 with MotechEvent

use of org.motechproject.event.MotechEvent in project motech by motech.

the class MotechEventHeaderMapper method fromHeaders.

/**
 * {@inheritDoc}. Additionally sets <code>AMQ_SCHEDULED_DELAY</code> using
 * <code>MotechEventConfig</code> variables.
 */
@Override
public void fromHeaders(MessageHeaders messageHeaders, Message message) {
    super.fromHeaders(messageHeaders, message);
    try {
        MotechEvent motechEvent = (MotechEvent) ((ActiveMQObjectMessage) message).getObject();
        if (motechEvent.isInvalid()) {
            long redeliveryCount = motechEvent.getMessageRedeliveryCount();
            Double delay = motechEventConfig.getMessageRedeliveryDelay() * MILLIS_PER_SEC * ((Math.pow(2, redeliveryCount - 1)));
            LOGGER.debug("Redelivering " + motechEvent + " after " + delay + " millis.");
            message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, delay.longValue());
        }
    } catch (JMSException e) {
        LOGGER.error("Failed to set header", e);
    }
}
Also used : JMSException(javax.jms.JMSException) MotechEvent(org.motechproject.event.MotechEvent)

Example 10 with MotechEvent

use of org.motechproject.event.MotechEvent in project motech by motech.

the class EventBundleIT method testEventWithTypedPayloadAndMetadata.

@Test
public void testEventWithTypedPayloadAndMetadata() throws Exception {
    final ArrayList<MotechEvent> receivedEvents = new ArrayList<>();
    registry.registerListener(new EventListener() {

        @Override
        public void handle(MotechEvent event) {
            receivedEvents.add(event);
            synchronized (waitLock) {
                waitLock.notify();
            }
        }

        @Override
        public String getIdentifier() {
            return "event";
        }
    }, "event");
    Map<String, Object> params = new HashMap<>();
    params.put("foo", new TestEventPayload());
    Map<String, Object> metadata = new HashMap<>();
    metadata.put("meta", new TestEventPayload());
    metadata.put("theNumberSeven", 7);
    wait2s();
    MotechEvent testEvent = new MotechEvent("event", params);
    testEvent.setMetadata(metadata);
    eventRelay.sendEventMessage(testEvent);
    wait2s();
    assertEquals(1, receivedEvents.size());
    MotechEvent receivedEvent = receivedEvents.get(0);
    assertTrue(receivedEvent.getParameters().get("foo") instanceof TestEventPayload);
    assertEquals(2, receivedEvent.getMetadata().size());
    assertTrue(receivedEvent.getMetadata().containsKey("meta"));
    assertTrue(receivedEvent.getMetadata().containsKey("theNumberSeven"));
    assertTrue(receivedEvent.getMetadata().get("meta") instanceof TestEventPayload);
    assertEquals(7, receivedEvent.getMetadata().get("theNumberSeven"));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) EventListener(org.motechproject.event.listener.EventListener) MotechEvent(org.motechproject.event.MotechEvent) TestEventPayload(org.motechproject.event.domain.TestEventPayload) Test(org.junit.Test)

Aggregations

MotechEvent (org.motechproject.event.MotechEvent)138 Test (org.junit.Test)87 HashMap (java.util.HashMap)79 CronSchedulableJob (org.motechproject.scheduler.contract.CronSchedulableJob)28 DateTime (org.joda.time.DateTime)25 DateUtil.newDateTime (org.motechproject.commons.date.util.DateUtil.newDateTime)20 ArrayList (java.util.ArrayList)14 RepeatingSchedulableJob (org.motechproject.scheduler.contract.RepeatingSchedulableJob)13 RunOnceSchedulableJob (org.motechproject.scheduler.contract.RunOnceSchedulableJob)11 JobBasicInfo (org.motechproject.scheduler.contract.JobBasicInfo)10 TaskActionInformation (org.motechproject.tasks.domain.mds.task.TaskActionInformation)8 Matchers.anyString (org.mockito.Matchers.anyString)7 EventListener (org.motechproject.event.listener.EventListener)7 CronJobId (org.motechproject.scheduler.contract.CronJobId)7 Task (org.motechproject.tasks.domain.mds.task.Task)7 JobId (org.motechproject.scheduler.contract.JobId)6 RepeatingJobId (org.motechproject.scheduler.contract.RepeatingJobId)6 Time (org.motechproject.commons.date.model.Time)5 DayOfWeekSchedulableJob (org.motechproject.scheduler.contract.DayOfWeekSchedulableJob)5 RepeatingPeriodSchedulableJob (org.motechproject.scheduler.contract.RepeatingPeriodSchedulableJob)5