|
| 1 | +package com.iluwatar.flux.dispatcher; |
| 2 | + |
| 3 | +import com.iluwatar.flux.action.Action; |
| 4 | +import com.iluwatar.flux.action.ActionType; |
| 5 | +import com.iluwatar.flux.action.Content; |
| 6 | +import com.iluwatar.flux.action.ContentAction; |
| 7 | +import com.iluwatar.flux.action.MenuAction; |
| 8 | +import com.iluwatar.flux.action.MenuItem; |
| 9 | +import com.iluwatar.flux.store.Store; |
| 10 | + |
| 11 | +import org.junit.Before; |
| 12 | +import org.junit.Test; |
| 13 | +import org.mockito.ArgumentCaptor; |
| 14 | + |
| 15 | +import java.lang.reflect.Constructor; |
| 16 | +import java.lang.reflect.Field; |
| 17 | +import java.util.List; |
| 18 | +import java.util.stream.Collectors; |
| 19 | + |
| 20 | +import static org.junit.Assert.assertEquals; |
| 21 | +import static org.junit.Assert.assertNotNull; |
| 22 | +import static org.junit.Assert.assertSame; |
| 23 | +import static org.mockito.Mockito.mock; |
| 24 | +import static org.mockito.Mockito.times; |
| 25 | +import static org.mockito.Mockito.verify; |
| 26 | +import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 27 | + |
| 28 | +/** |
| 29 | + * Date: 12/12/15 - 8:22 PM |
| 30 | + * |
| 31 | + * @author Jeroen Meulemeester |
| 32 | + */ |
| 33 | +public class DispatcherTest { |
| 34 | + |
| 35 | + /** |
| 36 | + * Dispatcher is a singleton with no way to reset it's internal state back to the beginning. |
| 37 | + * Replace the instance with a fresh one before each test to make sure test cases have no |
| 38 | + * influence on each other. |
| 39 | + */ |
| 40 | + @Before |
| 41 | + public void setUp() throws Exception { |
| 42 | + final Constructor<Dispatcher> constructor; |
| 43 | + constructor = Dispatcher.class.getDeclaredConstructor(); |
| 44 | + constructor.setAccessible(true); |
| 45 | + |
| 46 | + final Field field = Dispatcher.class.getDeclaredField("instance"); |
| 47 | + field.setAccessible(true); |
| 48 | + field.set(Dispatcher.getInstance(), constructor.newInstance()); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testGetInstance() throws Exception { |
| 53 | + assertNotNull(Dispatcher.getInstance()); |
| 54 | + assertSame(Dispatcher.getInstance(), Dispatcher.getInstance()); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testMenuItemSelected() throws Exception { |
| 59 | + final Dispatcher dispatcher = Dispatcher.getInstance(); |
| 60 | + |
| 61 | + final Store store = mock(Store.class); |
| 62 | + dispatcher.registerStore(store); |
| 63 | + dispatcher.menuItemSelected(MenuItem.HOME); |
| 64 | + dispatcher.menuItemSelected(MenuItem.COMPANY); |
| 65 | + |
| 66 | + // We expect 4 events, 2 menu selections and 2 content change actions |
| 67 | + final ArgumentCaptor<Action> actionCaptor = ArgumentCaptor.forClass(Action.class); |
| 68 | + verify(store, times(4)).onAction(actionCaptor.capture()); |
| 69 | + verifyNoMoreInteractions(store); |
| 70 | + |
| 71 | + final List<Action> actions = actionCaptor.getAllValues(); |
| 72 | + final List<MenuAction> menuActions = actions.stream() |
| 73 | + .filter(a -> a.getType().equals(ActionType.MENU_ITEM_SELECTED)) |
| 74 | + .map(a -> (MenuAction) a) |
| 75 | + .collect(Collectors.toList()); |
| 76 | + |
| 77 | + final List<ContentAction> contentActions = actions.stream() |
| 78 | + .filter(a -> a.getType().equals(ActionType.CONTENT_CHANGED)) |
| 79 | + .map(a -> (ContentAction) a) |
| 80 | + .collect(Collectors.toList()); |
| 81 | + |
| 82 | + assertEquals(2, menuActions.size()); |
| 83 | + assertEquals(1, menuActions.stream().map(MenuAction::getMenuItem).filter(MenuItem.HOME::equals).count()); |
| 84 | + assertEquals(1, menuActions.stream().map(MenuAction::getMenuItem).filter(MenuItem.COMPANY::equals).count()); |
| 85 | + |
| 86 | + assertEquals(2, contentActions.size()); |
| 87 | + assertEquals(1, contentActions.stream().map(ContentAction::getContent).filter(Content.PRODUCTS::equals).count()); |
| 88 | + assertEquals(1, contentActions.stream().map(ContentAction::getContent).filter(Content.COMPANY::equals).count()); |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | +} |
0 commit comments