@Test
  public void shouldntRunPreprocessorsIfFlagSet() {
    Table table = tableWithPhase(Playing, 100L);
    when(wallet.getTable()).thenReturn(table);

    GameInitialiser.GameInitialisationContext context = defaultEventContext();
    context.setRunPreProcessors(false);
    EventPreprocessor processor = mockEventPreprocessor(true);
    underTest.setEventPreInitialisationProcessors(toList(processor));
    underTest.initialiseGame(context);
    verifyZeroInteractions(processor);
  }
 @Test
 public void shouldntRunPostProcessorsWhenDisabledAndNewGameCreated() {
   Table table = tableWithPhase(null, 100L);
   when(wallet.getTable()).thenReturn(table);
   GameInitialiser.GameInitialisationContext context = defaultEventContext();
   context.setRunPostProcessors(false);
   Postprocessor processor = mockPostProcessor();
   underTest.setPostInitialisationProcessors(toList(processor));
   underTest.initialiseGame(context);
   assertEquals(Long.valueOf(1), table.getGameId());
   verifyZeroInteractions(processor);
 }
  @Test
  public void shouldReturnFalseIfAPreProcessorFails() {
    Table table = tableWithPhase(Playing, 100L);
    when(wallet.getTable()).thenReturn(table);

    EventPreprocessor processor3 = mockEventPreprocessor(true);
    underTest.setEventPreInitialisationProcessors(
        Arrays.asList(mockEventPreprocessor(true), mockEventPreprocessor(false), processor3));
    boolean successful = underTest.runPreProcessors(gameRules, defaultEventContext());
    assertFalse(successful);
    assertEquals(Long.valueOf(100), table.getGameId());
    verifyZeroInteractions(processor3);
  }
  @Test
  public void shouldRunAllPreProcessorsBeforeExecuting() {
    Table table = tableWithPhase(Playing, 100L);
    when(wallet.getTable()).thenReturn(table);

    EventPreprocessor processor1 = mockEventPreprocessor(true);
    EventPreprocessor processor2 = mockEventPreprocessor(true);
    EventPreprocessor processor3 = mockEventPreprocessor(true);
    underTest.setEventPreInitialisationProcessors(toList(processor2, processor3, processor1));
    underTest.runPreProcessors(gameRules, defaultEventContext());
    verify(processor1).preprocess(any(com.yazino.game.api.ScheduledEvent.class), eq(table));
    verify(processor2).preprocess(any(com.yazino.game.api.ScheduledEvent.class), eq(table));
    verify(processor3).preprocess(any(com.yazino.game.api.ScheduledEvent.class), eq(table));
  }
  @Test
  public void shouldntRunPostProcessorsWhenDisabledAndNextGameCreated() {
    Table table = tableWithPhase(GameFinished, 6L);
    when(wallet.getTable()).thenReturn(table);
    GameInitialiser.GameInitialisationContext context = defaultEventContext();
    context.setAllowedToMoveToNextGame(true);
    context.setRunPostProcessors(false);
    when(gameRules.isComplete(table.getCurrentGame())).thenReturn(true);
    underTest.setPostInitialisationProcessors(toList(postprocessor));

    underTest.initialiseGame(context);

    assertEquals(Long.valueOf(7), table.getGameId());
    verifyZeroInteractions(postprocessor);
  }
 @Test
 public void shouldRunPostProcessorsWhenNewGameCreated() {
   Table table = tableWithPhase(null, 100L);
   when(wallet.getTable()).thenReturn(table);
   GameInitialiser.GameInitialisationContext context = defaultEventContext();
   Postprocessor processor = mockPostProcessor();
   underTest.setPostInitialisationProcessors(toList(processor));
   underTest.initialiseGame(context);
   verify(processor)
       .postProcess(
           any(com.yazino.game.api.ExecutionResult.class),
           any(com.yazino.game.api.Command.class),
           eq(table),
           eq("TEST"),
           anyList(),
           any(BigDecimal.class));
 }
 @Test
 public void shouldCreateNewGameWhenNotAllowed() {
   Table table = tableWithPhase(GameFinished, 6L);
   when(wallet.getTable()).thenReturn(table);
   GameInitialiser.GameInitialisationContext context = defaultEventContext();
   context.setAllowedToMoveToNextGame(false);
   underTest.initialiseGame(context);
   assertEquals(Long.valueOf(6), table.getGameId());
 }
 @SuppressWarnings({"unchecked"})
 @Test
 public void shouldUpdateGameIdWhenNewGameCreatedAndNotifyPostProcessors() {
   Table table = tableWithPhase(null, 100L);
   when(wallet.getTable()).thenReturn(table);
   GameInitialiser.GameInitialisationContext context = defaultEventContext();
   Postprocessor processor = mockPostProcessor();
   underTest.setPostInitialisationProcessors(toList(processor));
   underTest.initialiseGame(context);
   assertEquals(Long.valueOf(1), table.getGameId());
   verify(processor)
       .postProcess(
           any(com.yazino.game.api.ExecutionResult.class),
           any(com.yazino.game.api.Command.class),
           eq(table),
           anyString(),
           anyList(),
           eq(context.getPlayerId()));
 }
  @Test
  public void shouldIncrementGameIdWhenCreatedNextGameAndNotifyPostProcessors() {
    Table table = tableWithPhase(GameFinished, 6L);
    when(wallet.getTable()).thenReturn(table);
    GameInitialiser.GameInitialisationContext context = defaultEventContext();
    context.setAllowedToMoveToNextGame(true);
    when(gameRules.isComplete(table.getCurrentGame())).thenReturn(true);
    underTest.setPostInitialisationProcessors(toList(postprocessor));

    underTest.initialiseGame(context);

    assertEquals(Long.valueOf(7), table.getGameId());
    verify(postprocessor)
        .postProcess(
            any(com.yazino.game.api.ExecutionResult.class),
            any(com.yazino.game.api.Command.class),
            eq(table),
            anyString(),
            anyList(),
            eq(context.getPlayerId()));
  }