@Test public void statsDontGetPublishedToActivePlayersIfGameNotOver() throws com.yazino.game.api.GameException { whenGameStart(); com.yazino.game.api.GamePlayer p1 = new com.yazino.game.api.GamePlayer(PLAYER1_ID, SESSION_ID, ""); com.yazino.game.api.GamePlayer p2 = new com.yazino.game.api.GamePlayer(PLAYER2_ID, SESSION_ID, ""); whenPlayers(p1, p2); when(gameRules.getObservableStatus(gameStatus1, getContextFor(p1))) .thenReturn(new PrintlnStatus()); when(gameRules.getObservableStatus(gameStatus1, getContextFor(p2))) .thenReturn(new PrintlnStatus()); when(gameRules.isComplete(gameStatus1)).thenReturn(false); final com.yazino.game.api.ExecutionResult result = new com.yazino.game.api.ExecutionResult.Builder(gameRules, gameStatus1) .scheduledEvents(emptyEvents) .build(); when(gameRules.execute( isA(com.yazino.game.api.ExecutionContext.class), eq(command.toCommand(PLAYER_NAME)))) .thenReturn(result); executeCommand(); verify(locationService, times(2)) .notify( any(BigDecimal.class), any(BigDecimal.class), any(LocationChangeType.class), any(Location.class)); verifyNoMoreInteractions(locationService); }
@Test public void testPlayersGetNewStatus() throws com.yazino.game.api.GameException { whenGameStart(); command = commandFrom(PLAYER1_ID); com.yazino.game.api.GamePlayer p1 = new com.yazino.game.api.GamePlayer(PLAYER1_ID, SESSION_ID, ""); com.yazino.game.api.GamePlayer p2 = new com.yazino.game.api.GamePlayer(PLAYER2_ID, SESSION_ID, ""); table.setCurrentGame(gameStatus1); whenPlayers(p1, p2); when(gameRules.getObservableStatus(gameStatus1, getContextFor(p1, false, table.getIncrement()))) .thenReturn(new PrintlnStatus()); when(gameRules.getObservableStatus(gameStatus1, getContextFor(p2, true, table.getIncrement()))) .thenReturn(new PrintlnStatus()); final com.yazino.game.api.ExecutionResult result = new com.yazino.game.api.ExecutionResult.Builder(gameRules, gameStatus1) .scheduledEvents(emptyEvents) .build(); when(gameRules.execute( isA(com.yazino.game.api.ExecutionContext.class), eq(command.toCommand(PLAYER_NAME)))) .thenReturn(result); final List<HostDocument> hostDocuments = executeCommand(); // table.lastGameChanges? assertThat(hostDocuments.size(), is(equalTo(2))); assertThat( (GameStatusHostDocument) hostDocuments.get(0), Matchers.isA(GameStatusHostDocument.class)); assertThat( (GameStatusHostDocument) hostDocuments.get(1), Matchers.isA(GameStatusHostDocument.class)); }
@SuppressWarnings({"ThrowableInstanceNeverThrown"}) @Test public void senderGetsErrorMessageWhenExceptionThrownInGameHost() throws com.yazino.game.api.GameException { whenGameStart(); com.yazino.game.api.ParameterisedMessage pm = new com.yazino.game.api.ParameterisedMessage("UPS!"); when(gameRules.execute( isA(com.yazino.game.api.ExecutionContext.class), eq(command.toCommand(PLAYER_NAME)))) .thenThrow(new com.yazino.game.api.GameException(pm)); final List<HostDocument> hostDocuments = executeCommand(); assertThat((ErrorHostDocument) hostDocuments.get(1), Matchers.isA(ErrorHostDocument.class)); }
@Test public void commandsAndStatusChangesGetAudited() throws com.yazino.game.api.GameException { whenGameStart(); whenPlayers(); final com.yazino.game.api.ExecutionResult result = new com.yazino.game.api.ExecutionResult.Builder(gameRules, gameStatus1) .scheduledEvents(emptyEvents) .build(); when(gameRules.execute( isA(com.yazino.game.api.ExecutionContext.class), isA(com.yazino.game.api.Command.class))) .thenReturn(result); executeCommand(); verify(auditor).audit(eq(AUDIT_LABEL), eq(command.toCommand(PLAYER_NAME))); }
@SuppressWarnings({"ThrowableInstanceNeverThrown", "ThrowableResultOfMethodCallIgnored"}) @Test public void commandsAndExceptionGetAuditedInCaseOfErrors() throws com.yazino.game.api.GameException { whenGameStart(); com.yazino.game.api.GameException exc = new com.yazino.game.api.GameException(new com.yazino.game.api.ParameterisedMessage("XYZ")); doThrow(exc) .when(gameRules) .execute( isA(com.yazino.game.api.ExecutionContext.class), isA(com.yazino.game.api.Command.class)); executeCommand(); verify(auditor).audit(AUDIT_LABEL, command.toCommand(PLAYER_NAME)); }
@Test public void testObserversGetNotified() throws com.yazino.game.api.GameException { whenGameStart(); final com.yazino.game.api.ExecutionResult executionResult = new com.yazino.game.api.ExecutionResult.Builder( new PrintlnRules(), new GameStatus(new PrintlnStatus())) .scheduledEvents(emptyEvents) .build(); when(gameRules.getObservableStatus(gameStatus1, getContextFor(null))) .thenReturn(new PrintlnStatus()); when(gameRules.execute( isA(com.yazino.game.api.ExecutionContext.class), eq(command.toCommand(PLAYER_NAME)))) .thenReturn(executionResult); final List<HostDocument> hostDocuments = executeCommand(); assertThat( (GameStatusHostDocument) hostDocuments.get(0), Matchers.isA(GameStatusHostDocument.class)); assertThat( (GameStatusHostDocument) hostDocuments.get(1), Matchers.isA(GameStatusHostDocument.class)); }
@Test public void testTableGetsNewEvents() throws com.yazino.game.api.GameException { whenGameStart(); whenPlayers(); List<com.yazino.game.api.ScheduledEvent> events = Arrays.asList( new com.yazino.game.api.ScheduledEvent( 100l, 100l, "X", "Y", new HashMap<String, String>(), false), new com.yazino.game.api.ScheduledEvent( 300l, 100l, "Z", "Y", new HashMap<String, String>(), false), new com.yazino.game.api.ScheduledEvent( 200l, 100l, "X", "Z", new HashMap<String, String>(), false)); final com.yazino.game.api.ExecutionResult result = new com.yazino.game.api.ExecutionResult.Builder(gameRules, gameStatus1) .scheduledEvents(events) .build(); when(gameRules.execute( (com.yazino.game.api.ExecutionContext) anyObject(), eq(command.toCommand(PLAYER_NAME)))) .thenReturn(result); executeCommand(); assertEquals(3, table.scheduledEventCount()); }