diff options
author | Joffrey BION <joffrey.bion@gmail.com> | 2017-05-20 12:31:39 +0200 |
---|---|---|
committer | Joffrey BION <joffrey.bion@gmail.com> | 2017-05-20 12:31:39 +0200 |
commit | 9c716b434925385ba7da368b5b1006695259e060 (patch) | |
tree | b337b6f2bbb87e59e4fd4cdc9ebc4aeafe939b78 /backend/src/test | |
parent | Add checks on last played moves in GameTest (diff) | |
download | seven-wonders-9c716b434925385ba7da368b5b1006695259e060.tar.gz seven-wonders-9c716b434925385ba7da368b5b1006695259e060.tar.bz2 seven-wonders-9c716b434925385ba7da368b5b1006695259e060.zip |
Add LobbyController tests
Diffstat (limited to 'backend/src/test')
-rw-r--r-- | backend/src/test/java/org/luxons/sevenwonders/controllers/LobbyControllerTest.java | 212 |
1 files changed, 212 insertions, 0 deletions
diff --git a/backend/src/test/java/org/luxons/sevenwonders/controllers/LobbyControllerTest.java b/backend/src/test/java/org/luxons/sevenwonders/controllers/LobbyControllerTest.java new file mode 100644 index 00000000..9377f885 --- /dev/null +++ b/backend/src/test/java/org/luxons/sevenwonders/controllers/LobbyControllerTest.java @@ -0,0 +1,212 @@ +package org.luxons.sevenwonders.controllers; + +import java.security.Principal; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.stream.Collectors; + +import org.junit.Before; +import org.junit.Test; +import org.luxons.sevenwonders.actions.ReorderPlayersAction; +import org.luxons.sevenwonders.actions.UpdateSettingsAction; +import org.luxons.sevenwonders.controllers.LobbyController.PlayerIsNotOwnerException; +import org.luxons.sevenwonders.controllers.LobbyController.PlayerNotInLobbyException; +import org.luxons.sevenwonders.game.api.CustomizableSettings; +import org.luxons.sevenwonders.game.data.GameDefinitionLoader; +import org.luxons.sevenwonders.game.data.definitions.WonderSidePickMethod; +import org.luxons.sevenwonders.lobby.Lobby; +import org.luxons.sevenwonders.lobby.Player; +import org.luxons.sevenwonders.lobby.State; +import org.luxons.sevenwonders.repositories.LobbyRepository; +import org.luxons.sevenwonders.repositories.PlayerRepository; +import org.luxons.sevenwonders.repositories.PlayerRepository.PlayerNotFoundException; +import org.luxons.sevenwonders.test.TestUtils; +import org.springframework.messaging.simp.SimpMessagingTemplate; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; + +public class LobbyControllerTest { + + private PlayerRepository playerRepository; + + private LobbyRepository lobbyRepository; + + private LobbyController lobbyController; + + @Before + public void setUp() { + playerRepository = new PlayerRepository(); + lobbyRepository = new LobbyRepository(new GameDefinitionLoader()); + SimpMessagingTemplate template = TestUtils.createSimpMessagingTemplate(); + lobbyController = new LobbyController(playerRepository, template); + } + + @Test(expected = PlayerNotFoundException.class) + public void leave_failsWhenPlayerDoesNotExist() { + Principal principal = TestUtils.createPrincipal("testuser"); + lobbyController.leave(principal); + } + + @Test(expected = PlayerNotInLobbyException.class) + public void leave_failsWhenNotInLobby() { + playerRepository.createOrUpdate("testuser", "Test User"); + Principal principal = TestUtils.createPrincipal("testuser"); + lobbyController.leave(principal); + } + + @Test + public void leave_succeedsWhenInALobby_asOwner() { + Player player = playerRepository.createOrUpdate("testuser", "Test User"); + Lobby lobby = lobbyRepository.create("Test Game", player); + + assertTrue(lobby.getPlayers().contains(player)); + assertSame(lobby, player.getLobby()); + + Principal principal = TestUtils.createPrincipal("testuser"); + lobbyController.leave(principal); + + assertFalse(lobby.getPlayers().contains(player)); + assertNull(player.getLobby()); + } + + @Test + public void leave_succeedsWhenInALobby_asPeasant() { + Player player = playerRepository.createOrUpdate("testuser", "Test User"); + Lobby lobby = lobbyRepository.create("Test Game", player); + Player player2 = addPlayer(lobby, "testuser2"); + + assertTrue(lobby.getPlayers().contains(player2)); + assertSame(lobby, player2.getLobby()); + + Principal principal = TestUtils.createPrincipal("testuser2"); + lobbyController.leave(principal); + + assertFalse(lobby.getPlayers().contains(player2)); + assertNull(player2.getLobby()); + } + + @Test + public void reorderPlayers_succeedsForOwner() { + Player player = playerRepository.createOrUpdate("testuser", "Test User"); + Lobby lobby = lobbyRepository.create("Test Game", player); + + Player player2 = addPlayer(lobby, "testuser2"); + Player player3 = addPlayer(lobby, "testuser3"); + Player player4 = addPlayer(lobby, "testuser4"); + + List<Player> players = Arrays.asList(player, player2, player3, player4); + assertEquals(players, lobby.getPlayers()); + + ReorderPlayersAction reorderPlayersAction = new ReorderPlayersAction(); + List<Player> reorderedPlayers = Arrays.asList(player3, player, player2, player4); + List<String> playerNames = reorderedPlayers.stream().map(Player::getUsername).collect(Collectors.toList()); + reorderPlayersAction.setOrderedPlayers(playerNames); + + Principal principal = TestUtils.createPrincipal("testuser"); + lobbyController.reorderPlayers(reorderPlayersAction, principal); + + assertEquals(reorderedPlayers, lobby.getPlayers()); + } + + @Test(expected = PlayerIsNotOwnerException.class) + public void reorderPlayers_failsForPeasant() { + Player player = playerRepository.createOrUpdate("testuser", "Test User"); + Lobby lobby = lobbyRepository.create("Test Game", player); + + Player player2 = addPlayer(lobby, "testuser2"); + Player player3 = addPlayer(lobby, "testuser3"); + + ReorderPlayersAction reorderPlayersAction = new ReorderPlayersAction(); + List<Player> reorderedPlayers = Arrays.asList(player3, player, player2); + List<String> playerNames = reorderedPlayers.stream().map(Player::getUsername).collect(Collectors.toList()); + reorderPlayersAction.setOrderedPlayers(playerNames); + + Principal principal = TestUtils.createPrincipal("testuser2"); + lobbyController.reorderPlayers(reorderPlayersAction, principal); + } + + @Test + public void updateSettings_succeedsForOwner() { + Player player = playerRepository.createOrUpdate("testuser", "Test User"); + Lobby lobby = lobbyRepository.create("Test Game", player); + + addPlayer(lobby, "testuser2"); + addPlayer(lobby, "testuser3"); + addPlayer(lobby, "testuser4"); + + assertEquals(new CustomizableSettings(), lobby.getSettings()); + + UpdateSettingsAction updateSettingsAction = new UpdateSettingsAction(); + CustomizableSettings newSettings = new CustomizableSettings(); + newSettings.setInitialGold(12); + newSettings.setDefaultTradingCost(5); + newSettings.setWonderSidePickMethod(WonderSidePickMethod.ALL_A); + newSettings.setTimeLimitInSeconds(5); + newSettings.setPointsPer3Gold(5); + newSettings.setDiscardedCardGold(4); + newSettings.setLostPointsPerDefeat(10); + newSettings.setWonPointsPerVictoryPerAge(new HashMap<>()); + updateSettingsAction.setSettings(newSettings); + + Principal principal = TestUtils.createPrincipal("testuser"); + lobbyController.updateSettings(updateSettingsAction, principal); + + assertEquals(newSettings, lobby.getSettings()); + } + + @Test(expected = PlayerIsNotOwnerException.class) + public void updateSettings_failsForPeasant() { + Player player = playerRepository.createOrUpdate("testuser", "Test User"); + Lobby lobby = lobbyRepository.create("Test Game", player); + + addPlayer(lobby, "testuser2"); + addPlayer(lobby, "testuser3"); + + UpdateSettingsAction updateSettingsAction = new UpdateSettingsAction(); + updateSettingsAction.setSettings(new CustomizableSettings()); + + Principal principal = TestUtils.createPrincipal("testuser2"); + lobbyController.updateSettings(updateSettingsAction, principal); + } + + @Test + public void startGame_succeedsForOwner() { + Player player = playerRepository.createOrUpdate("testuser", "Test User"); + Lobby lobby = lobbyRepository.create("Test Game", player); + + addPlayer(lobby, "testuser2"); + addPlayer(lobby, "testuser3"); + addPlayer(lobby, "testuser4"); + + Principal principal = TestUtils.createPrincipal("testuser"); + lobbyController.startGame(principal); + + assertSame(State.PLAYING, lobby.getState()); + } + + @Test(expected = PlayerIsNotOwnerException.class) + public void startGame_failsForPeasant() { + Player player = playerRepository.createOrUpdate("testuser", "Test User"); + Lobby lobby = lobbyRepository.create("Test Game", player); + + addPlayer(lobby, "testuser2"); + addPlayer(lobby, "testuser3"); + + Principal principal = TestUtils.createPrincipal("testuser2"); + lobbyController.startGame(principal); + } + + private Player addPlayer(Lobby lobby, String username) { + Player player = playerRepository.createOrUpdate(username, username); + lobby.addPlayer(player); + + assertTrue(lobby.getPlayers().contains(player)); + assertSame(lobby, player.getLobby()); + return player; + } +} |