@Test public void testProcessTransactionResultCorrectlyCallsAuditOnSuccessWithNonNullExecutionResult() throws com.yazino.game.api.GameException { final String auditLabel = "test424243542354"; gameStatus = mock(com.yazino.game.api.GameStatus.class); when(gameRules.canBeClosed(gameStatus)).thenReturn(false); when(gameRules.isComplete(gameStatus)).thenReturn(false); whenPlayers(); when(gameRules.getPlayerInformation(gameStatus)) .thenReturn(Collections.<com.yazino.game.api.PlayerAtTableInformation>emptyList()); when(gameRules.getNumberOfSeatsTaken(gameStatus)).thenReturn(0); table.setCurrentGame(gameStatus); table.setGameId(GAME_ID); final com.yazino.game.api.TransactionResult txResult = new com.yazino.game.api.TransactionResult("foo", true, null, null, null, null); final com.yazino.game.api.ExecutionResult executionResult = new com.yazino.game.api.ExecutionResult.Builder(gameRules, gameStatus).build(); when(gameRules.processTransactionResult( isA(com.yazino.game.api.ExecutionContext.class), eq(txResult))) .thenReturn(executionResult); auditor = mock(Auditor.class); when(auditor.newLabel()).thenReturn(auditLabel); host = gameHost(new InMemoryGameRepository(gameRules)); host.processTransactionResult(table, GAME_ID, txResult); }
@Test public void forceNewGameWillRestartTableAndStartNewGame() { Map<BigDecimal, BigDecimal> playerIdToAccountIdOverrides = new HashMap<>(); playerIdToAccountIdOverrides.put(PLAYER_ID, BigDecimal.valueOf(202020)); com.yazino.game.api.GameStatus status = new GameStatus(new PrintlnStatus()); com.yazino.game.api.ExecutionResult result = new com.yazino.game.api.ExecutionResult.Builder(gameRules, status).build(); when(gameRules.startNewGame(isA(com.yazino.game.api.GameCreationContext.class))) .thenReturn(result); when(auditor.newLabel()).thenReturn("X"); host = gameHost(new InMemoryGameRepository(gameRules)); table.setTableStatus(TableStatus.closed); table.setCurrentGame(null); Collection<com.yazino.game.api.PlayerAtTableInformation> playersAtTable = Arrays.asList( new com.yazino.game.api.PlayerAtTableInformation( new com.yazino.game.api.GamePlayer(PLAYER1_ID, null, "One"), Collections.<String, String>emptyMap()), new com.yazino.game.api.PlayerAtTableInformation( new com.yazino.game.api.GamePlayer(PLAYER2_ID, null, "Two"), Collections.<String, String>emptyMap())); host.forceNewGame(table, playersAtTable, playerIdToAccountIdOverrides); ArgumentCaptor<com.yazino.game.api.GameCreationContext> contextCaptor = ArgumentCaptor.forClass(com.yazino.game.api.GameCreationContext.class); verify(gameRules).startNewGame(contextCaptor.capture()); assertEquals(TableStatus.open, table.getTableStatus()); assertEquals(status, table.getCurrentGame()); assertEquals(playersAtTable, contextCaptor.getValue().getPlayersAtTableInformation()); }
@Test public void testProcessTransactionResultCorrectlyCallsAuditOnSuccessWithNullExecutionResult() { final String auditLabel = "test424542542354"; gameStatus = mock(com.yazino.game.api.GameStatus.class); when(gameRules.isComplete(gameStatus)).thenReturn(false); table.setCurrentGame(gameStatus); table.setGameId(GAME_ID); final com.yazino.game.api.TransactionResult txResult = new com.yazino.game.api.TransactionResult("foo", true, null, null, null, null); auditor = mock(Auditor.class); when(auditor.newLabel()).thenReturn(auditLabel); host = gameHost(new InMemoryGameRepository(gameRules)); host.processTransactionResult(table, GAME_ID, txResult); }
@Test public void testProcessTransactionResultIsNotProcessedForCompleteGame() { final GameRepository mockGameRepository = mock(GameRepository.class); when(mockGameRepository.getGameRules(GAME_TYPE)).thenReturn(gameRules); gameStatus = mock(com.yazino.game.api.GameStatus.class); when(gameRules.isComplete(gameStatus)).thenReturn(true); table.setCurrentGame(gameStatus); table.setGameId(GAME_ID); final com.yazino.game.api.TransactionResult txResult = new com.yazino.game.api.TransactionResult("foo", true, null, null, null, null); host = gameHost(mockGameRepository); host.processTransactionResult(table, GAME_ID, txResult); verifyNoMoreInteractions(documentDispatcher); }
@Test public void testProcessTransactionResultIsOnlyExecutedForTheCurrentGame() { final GameRepository mockGameRepository = mock(GameRepository.class); when(mockGameRepository.getGameRules(GAME_TYPE)).thenReturn(gameRules); final long differentGameId = GAME_ID + 6; gameStatus = mock(com.yazino.game.api.GameStatus.class); when(gameRules.isComplete(gameStatus)).thenReturn(false); table.setCurrentGame(gameStatus); table.setGameId(GAME_ID); final com.yazino.game.api.TransactionResult txResult = new com.yazino.game.api.TransactionResult("foo", true, null, null, null, null); host = gameHost(mockGameRepository); host.processTransactionResult(table, differentGameId, txResult); verifyNoMoreInteractions(locationService); }
@Test public void tournamentTableShouldNotRetrievePlayersMainAccount() { table.setShowInLobby(false); table.setGameId(1l); host = gameHost(new InMemoryGameRepository(gameRules)); BigDecimal playerId = BigDecimal.valueOf(54321); CommandWrapper aCommand = new CommandWrapper(table.getTableId(), 1l, playerId, SESSION_ID, "aCommand"); aCommand.setRequestId(UUID); when(playerRepository.findSummaryByPlayerAndSession(playerId, SESSION_ID)) .thenReturn( aPlayerSessionSummary(playerId, "", BigDecimal.valueOf(10), BigDecimal.valueOf(100))); List<HostDocument> documents = host.execute(table, aCommand); assertEquals(1, documents.size()); assertThat((ErrorHostDocument) documents.get(0), Matchers.isA(ErrorHostDocument.class)); }
@Test public void testProcessTransactionResultUpdatesCachedBalance() { BigDecimal ORIGINAL_BALANCE = BigDecimal.ONE; BigDecimal NEW_BALANCE = BigDecimal.TEN; table.setCurrentGame(gameStatus); table.setGameId(GAME_ID); com.yazino.game.api.GamePlayer player = new com.yazino.game.api.GamePlayer(PLAYER1_ID, SESSION_ID, ""); addPlayersToGame(gameStatus, player); table.playerWithAccountId(PLAYER1_ACCOUNT_ID).setCachedBalance(ORIGINAL_BALANCE); final com.yazino.game.api.TransactionResult txResult = new com.yazino.game.api.TransactionResult( "foo", true, null, PLAYER1_ACCOUNT_ID, NEW_BALANCE, null); host = gameHost(new InMemoryGameRepository(gameRules)); // exercise SUT host.processTransactionResult(table, GAME_ID, txResult); // verify assertEquals(NEW_BALANCE, table.playerWithAccountId(PLAYER1_ACCOUNT_ID).getCachedBalance()); }
@SuppressWarnings({"ThrowableResultOfMethodCallIgnored", "ThrowableInstanceNeverThrown"}) @Test public void testProcessTransactionResultCorrectlyCallsAuditOnFailure() throws com.yazino.game.api.GameException { final String auditLabel = "test42454254345345"; gameStatus = mock(com.yazino.game.api.GameStatus.class); when(gameRules.isComplete(gameStatus)).thenReturn(false); table.setCurrentGame(gameStatus); table.setGameId(GAME_ID); final com.yazino.game.api.TransactionResult txResult = new com.yazino.game.api.TransactionResult("foo", true, null, null, null, null); final com.yazino.game.api.GameException thrownException = new com.yazino.game.api.GameException( new com.yazino.game.api.ParameterisedMessage("test.mock.error")); when(gameRules.processTransactionResult( isA(com.yazino.game.api.ExecutionContext.class), eq(txResult))) .thenThrow(thrownException); auditor = mock(Auditor.class); when(auditor.newLabel()).thenReturn(auditLabel); host = gameHost(new InMemoryGameRepository(gameRules)); host.processTransactionResult(table, GAME_ID, txResult); verify(auditor, times(1)).newLabel(); }
private GameHost gameHost(final GameRepository mockGameRepository) { final DestinationFactory destinationFactory = new DestinationFactory(); GameInitialiser gameInitialiser = new GameInitialiser(); gameInitialiser.setEventPreInitialisationProcessors( Arrays.asList( (EventPreprocessor) new TableClosedPreprocessor(destinationFactory), new GameDisabledPreprocessor(mockGameRepository, destinationFactory))); gameInitialiser.setCommandPreInitialisationProcessors( Arrays.asList( new TableClosedPreprocessor(destinationFactory), new TournamentPlayerValidator(destinationFactory), new GameDisabledPreprocessor(mockGameRepository, destinationFactory))); gameInitialiser.setPostInitialisationProcessors( Arrays.asList( new PlayerNotificationPostProcessor(destinationFactory, mockGameRepository), new ObserverNotificationPostprocessor(mockGameRepository, destinationFactory), new NotifyLocationChangesPostprocessor( mockGameRepository, playerRepository, chatRepository, locationService), new TableUpdatePostprocessor(mockGameRepository, timeSource))); GameEventExecutor eventExecutor = new GameEventExecutor( mockGameRepository, auditor, bufferedGameHostWalletFactory, novomaticRequestService); eventExecutor.setGameInitialiser(gameInitialiser); GameCommandExecutor commandExecutor = new GameCommandExecutor( mockGameRepository, auditor, bufferedGameHostWalletFactory, novomaticRequestService, playerRepository, destinationFactory); commandExecutor.setGameInitialiser(gameInitialiser); commandExecutor.setUuidSource(uuidSource); final GameHost gameHost = new GameHost( auditor, mockGameRepository, destinationFactory, playerRepository, bufferedGameHostWalletFactory, novomaticRequestService, uuidSource, eventExecutor, commandExecutor, gameInitialiser); gameHost.getEventExecutor().setGameInitialiser(gameInitialiser); gameHost.getCommandExecutor().setGameInitialiser(gameInitialiser); // this setup is a nasty side-effect of splitting initialisation of these classes out of the // game host. // On the upside, it means the GH tests can in future just test the GH itself. gameHost .getEventExecutor() .setPreExecutionProcessors( Arrays.asList( new AuditPreprocessor(auditor), new TableReadyEventPreprocessor(mockGameRepository), new NopEventPreprocessor(), new EventStillValidPreprocessor())); gameHost .getEventExecutor() .setPostExecutionProcessors( Arrays.asList( new GameCompletePostprocessor(mockGameRepository, timeSource), new NotifyLocationChangesPostprocessor( mockGameRepository, playerRepository, chatRepository, locationService), new PlayerNotificationPostProcessor(destinationFactory, mockGameRepository), new ObserverNotificationPostprocessor(mockGameRepository, destinationFactory), new GameDisabledPostprocessor(mockGameRepository, destinationFactory), new TableUpdatePostprocessor(mockGameRepository, timeSource), new GameXPPublishingPostProcessor(playerStatisticEventsPublisher), new TableAuditPostprocessor(auditor, gameCompletePublisher, mockGameRepository))); gameHost .getCommandExecutor() .setPreExecutionProcessors( Arrays.asList( new AuditPreprocessor(auditor), new TableClosingPreprocessor(destinationFactory), new InitialGetStatusPreprocessor(auditor, destinationFactory), new AcknowledgementPreprocessor())); gameHost .getCommandExecutor() .setPostExecutionProcessors( Arrays.asList( new GameCompletePostprocessor(mockGameRepository, timeSource), new NotifyLocationChangesPostprocessor( mockGameRepository, playerRepository, chatRepository, locationService), new PlayerNotificationPostProcessor(destinationFactory, mockGameRepository), new ObserverNotificationPostprocessor(mockGameRepository, destinationFactory), new GameDisabledPostprocessor(mockGameRepository, destinationFactory), new TableUpdatePostprocessor(mockGameRepository, timeSource), new GameXPPublishingPostProcessor(playerStatisticEventsPublisher), new TableAuditPostprocessor(auditor, gameCompletePublisher, mockGameRepository))); gameHost.setPlayerRemovalProcessors( Arrays.asList( (PlayerRemovalProcessor) new NotifyLocationChangesPostprocessor( mockGameRepository, playerRepository, chatRepository, locationService))); return gameHost; }
private List<HostDocument> executeCommand() { host = gameHost(new InMemoryGameRepository(gameRules)); return host.execute(table, command); }