Search in sources :

Example 16 with EventListener

use of org.motechproject.event.listener.EventListener in project motech by motech.

the class EventListenerRegistryTest method testRegisterForMultipleEvents.

@Test
public void testRegisterForMultipleEvents() {
    List<String> et = new ArrayList<String>();
    et.add("org.motechproject.server.someevent");
    et.add("org.motechproject.server.someotherevent");
    EventListener sel = new SampleEventListener();
    registry.registerListener(sel, et);
    Set<EventListener> el = registry.getListeners(et.get(0));
    assertNotNull(el);
    assertTrue(el.size() == 1);
    assertTrue(el.contains(sel));
    el = registry.getListeners(et.get(1));
    assertNotNull(el);
    assertTrue(el.size() == 1);
    assertTrue(el.contains(sel));
}
Also used : ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) OtherSampleEventListener(org.motechproject.event.listener.OtherSampleEventListener) EventListener(org.motechproject.event.listener.EventListener) SampleEventListener(org.motechproject.event.listener.SampleEventListener) OtherSampleEventListener(org.motechproject.event.listener.OtherSampleEventListener) SampleEventListener(org.motechproject.event.listener.SampleEventListener) Test(org.junit.Test)

Example 17 with EventListener

use of org.motechproject.event.listener.EventListener in project motech by motech.

the class EventListenerRegistryTest method testRemovingListenerPreservesOtherListeners.

@Test
public void testRemovingListenerPreservesOtherListeners() {
    List<String> et = new ArrayList<String>();
    et.add("org.motechproject.server.someevent");
    EventListener sel = new SampleEventListener();
    EventListener se2 = new OtherSampleEventListener();
    registry.registerListener(sel, et);
    registry.registerListener(se2, et);
    Set<EventListener> el = registry.getListeners("org.motechproject.server.someevent");
    assertEquals(2, el.size());
    registry.clearListenersForBean("TestEventListener");
    el = registry.getListeners("org.motechproject.server.someevent");
    assertEquals(1, el.size());
    registry.clearListenersForBean("TestEventListener2");
    el = registry.getListeners("org.motechproject.server.someevent");
    assertEquals(0, el.size());
}
Also used : OtherSampleEventListener(org.motechproject.event.listener.OtherSampleEventListener) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) OtherSampleEventListener(org.motechproject.event.listener.OtherSampleEventListener) EventListener(org.motechproject.event.listener.EventListener) SampleEventListener(org.motechproject.event.listener.SampleEventListener) OtherSampleEventListener(org.motechproject.event.listener.OtherSampleEventListener) SampleEventListener(org.motechproject.event.listener.SampleEventListener) Test(org.junit.Test)

Example 18 with EventListener

use of org.motechproject.event.listener.EventListener in project motech by motech.

the class TaskControllerTest method shouldSaveTask.

@Test
public void shouldSaveTask() {
    String subject = "trigger1";
    TaskActionInformation action = new TaskActionInformation("send", "action1", "action", "0.15", "send", new HashMap<String, String>());
    TaskTriggerInformation trigger = new TaskTriggerInformation("trigger", "trigger1", "trigger", "0.16", subject, subject);
    Task expected = new Task("name", trigger, asList(action));
    when(eventListenerRegistryService.getListeners(subject)).thenReturn(new HashSet<EventListener>());
    controller.save(expected);
    verify(taskWebService).save(expected);
}
Also used : TaskTriggerInformation(org.motechproject.tasks.domain.mds.task.TaskTriggerInformation) Task(org.motechproject.tasks.domain.mds.task.Task) TaskActionInformation(org.motechproject.tasks.domain.mds.task.TaskActionInformation) EventListener(org.motechproject.event.listener.EventListener) Test(org.junit.Test)

Example 19 with EventListener

use of org.motechproject.event.listener.EventListener in project motech by motech.

the class TaskTriggerHandlerTest method shouldRegisterHandlerOneTimeForSameSubjects.

@Test
public void shouldRegisterHandlerOneTimeForSameSubjects() {
    String subject = "org.motechproject.messagecampaign.campaign-completed";
    Method method = findMethod(getTargetClass(handler), "handle", MotechEvent.class);
    Set<EventListener> listeners = new HashSet<>();
    listeners.add(new MotechListenerEventProxy("taskTriggerHandler", this, method));
    when(registryService.getListeners(subject)).thenReturn(listeners);
    handler.registerHandlerFor(subject);
    handler.registerHandlerFor(subject);
    handler.registerHandlerFor(subject);
    handler.registerHandlerFor(subject);
    handler.registerHandlerFor(subject);
    handler.registerHandlerFor(subject);
    handler.registerHandlerFor(subject);
    verify(registryService, never()).registerListener(any(EventListener.class), eq(subject));
}
Also used : Matchers.anyString(org.mockito.Matchers.anyString) Method(java.lang.reflect.Method) ReflectionUtils.findMethod(org.springframework.util.ReflectionUtils.findMethod) EventListener(org.motechproject.event.listener.EventListener) MotechListenerEventProxy(org.motechproject.event.listener.annotations.MotechListenerEventProxy) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 20 with EventListener

use of org.motechproject.event.listener.EventListener in project motech by motech.

the class TaskTriggerHandlerTest method shouldRegisterRetryHandlerForSubject.

@Test
public void shouldRegisterRetryHandlerForSubject() {
    String subject = "org.motechproject.messagecampaign.campaign-completed";
    handler.registerHandlerFor(subject, true);
    ArgumentCaptor<EventListener> captor = ArgumentCaptor.forClass(EventListener.class);
    verify(registryService).registerListener(captor.capture(), eq(subject));
    MotechListenerEventProxy proxy = (MotechListenerEventProxy) captor.getValue();
    assertEquals("taskTriggerHandler", proxy.getIdentifier());
    assertEquals(handler, proxy.getBean());
    assertEquals(findMethod(getTargetClass(handler), "handleRetry", MotechEvent.class), proxy.getMethod());
}
Also used : Matchers.anyString(org.mockito.Matchers.anyString) EventListener(org.motechproject.event.listener.EventListener) MotechListenerEventProxy(org.motechproject.event.listener.annotations.MotechListenerEventProxy) MotechEvent(org.motechproject.event.MotechEvent) Test(org.junit.Test)

Aggregations

EventListener (org.motechproject.event.listener.EventListener)25 Test (org.junit.Test)20 ArrayList (java.util.ArrayList)12 SampleEventListener (org.motechproject.event.listener.SampleEventListener)11 Matchers.anyString (org.mockito.Matchers.anyString)10 OtherSampleEventListener (org.motechproject.event.listener.OtherSampleEventListener)10 MotechEvent (org.motechproject.event.MotechEvent)9 MotechListenerEventProxy (org.motechproject.event.listener.annotations.MotechListenerEventProxy)4 HashMap (java.util.HashMap)3 Method (java.lang.reflect.Method)2 Date (java.util.Date)1 HashSet (java.util.HashSet)1 DateTime (org.joda.time.DateTime)1 TestEventPayload (org.motechproject.event.domain.TestEventPayload)1 EventListenerTree (org.motechproject.event.listener.impl.EventListenerTree)1 TestMdsEntity (org.motechproject.mds.test.domain.TestMdsEntity)1 RunOnceSchedulableJob (org.motechproject.scheduler.contract.RunOnceSchedulableJob)1 Task (org.motechproject.tasks.domain.mds.task.Task)1 TaskActionInformation (org.motechproject.tasks.domain.mds.task.TaskActionInformation)1 TaskTriggerInformation (org.motechproject.tasks.domain.mds.task.TaskTriggerInformation)1