package com.github.blackrush.acara;

import org.fungsi.concurrent.Futures;
import org.fungsi.concurrent.Worker;
import org.junit.Before;
import org.junit.Test;

import java.util.List;
import java.util.stream.Stream;

import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class EventBusImplTest {

    EventMetadataBuilder eventMetadataBuilder;
    ListenerBuilder listenerBuilder;
    Worker worker;

    EventBusImpl eventBus;

    @Before
    public void setUp() throws Exception {
        eventMetadataBuilder = mock(EventMetadataBuilder.class);
        listenerBuilder = mock(ListenerBuilder.class);
        worker = mock(Worker.class);

        eventBus = new EventBusImpl(eventMetadataBuilder, listenerBuilder, worker);
    }

    @Test
    public void testSubscribePublishRevoke() throws Exception {
        Object object = new Object();

        EventMetadata meta1 = mock(EventMetadata.class),
                    meta2 = mock(EventMetadata.class),
                    meta3 = mock(EventMetadata.class);

        Listener listener1 = mock(Listener.class);
        when(listener1.getHandledEvent()).thenReturn(meta1);
        when(listener1.dispatch(any(), any())).thenReturn(Futures.success(new Object()));

        Listener listener2 = mock(Listener.class);
        when(listener2.getHandledEvent()).thenReturn(meta2);
        when(listener2.dispatch(any(), any())).thenReturn(Futures.success(new Object()));

        when(listenerBuilder.build(object)).thenReturn(Stream.of(listener1, listener2));

        when(eventMetadataBuilder.build("foobar")).thenReturn(meta1);
        when(eventMetadataBuilder.build("buzz")).thenReturn(meta2);
        when(eventMetadataBuilder.build("qux")).thenReturn(meta3);


        // subscribe
        Subscription sub = eventBus.subscribe(object);
        // publish -- HAS listeners
        List<Object> responses = eventBus.publish("foobar").get();
        // revoke
        sub.revoke();
        // publish -- HAD listeners
        List<Object> responses2 = eventBus.publish("buzz").get();
        // publish -- never had any listeners
        List<Object> responses3 = eventBus.publish("qux").get();


        assertEquals("first  number of responses", responses.size(),  1);
        assertEquals("second number of responses", responses2.size(), 0);
        assertEquals("third  number of responses", responses3.size(), 0);
    }
}
@@ -326,19 +326,18 @@
 to.M
-ockito.mock
+atchers.eq
 ;%0Aim
@@ -368,20 +368,17 @@
 Mockito.
-when
+*
 ;%0A%0Apubli
@@ -2486,10 +2486,1746 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void testEventBubbling() throws Exception %7B%0A        EventMetadata parent = mock(EventMetadata.class);%0A        EventMetadata child = mock(EventMetadata.class);%0A%0A        when(child.getParent()).thenReturn(parent);%0A%0A        when(eventMetadataBuilder.build(%22parent%22)).thenReturn(parent);%0A        when(eventMetadataBuilder.build(%22child%22)).thenReturn(child);%0A%0A        Object state1 = new Object();%0A        Object state2 = new Object();%0A%0A        Listener lParent = mock(Listener.class), lChild = mock(Listener.class);%0A        when(lParent.getHandledEvent()).thenReturn(parent);%0A        when(lParent.dispatch(eq(%22parent%22), any())).thenReturn(Futures.success(new Object()));%0A        when(lParent.dispatch(eq(%22child%22), any())).thenReturn(Futures.success(new Object()));%0A%0A        when(lChild.getHandledEvent()).thenReturn(child);%0A        when(lChild.dispatch(eq(%22child%22), any())).thenReturn(Futures.success(new Object()));%0A%0A        when(listenerBuilder.build(state1)).thenReturn(Stream.of(lParent, lChild));%0A        when(listenerBuilder.build(state2)).thenReturn(Stream.of(lParent));%0A%0A        Subscription sub1 = eventBus.subscribe(state1);%0A        Subscription sub2 = eventBus.subscribe(state2);%0A        List%3CObject%3E responses1 = eventBus.publish(%22child%22).get();%0A        List%3CObject%3E responses2 = eventBus.publish(%22parent%22).get();%0A        sub1.revoke();%0A        sub2.revoke();%0A%0A        verify(lParent, times(2)).dispatch(eq(%22parent%22), any());%0A        verify(lParent, times(1)).dispatch(eq(%22child%22), any());%0A        verify(lChild, times(1)).dispatch(eq(%22child%22), any());%0A%0A        assertEquals(%22first number of responses%22, 2, responses1.size());%0A        assertEquals(%22second number of responses%22, 2, responses2.size());%0A    %7D
 %0A%7D

Test event bubbling