From 9c716b434925385ba7da368b5b1006695259e060 Mon Sep 17 00:00:00 2001 From: Joffrey BION Date: Sat, 20 May 2017 12:31:39 +0200 Subject: Add LobbyController tests --- .../controllers/LobbyControllerTest.java | 212 +++++++++++++++++++++ 1 file changed, 212 insertions(+) create mode 100644 backend/src/test/java/org/luxons/sevenwonders/controllers/LobbyControllerTest.java (limited to 'backend/src/test/java/org/luxons') 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 players = Arrays.asList(player, player2, player3, player4); + assertEquals(players, lobby.getPlayers()); + + ReorderPlayersAction reorderPlayersAction = new ReorderPlayersAction(); + List reorderedPlayers = Arrays.asList(player3, player, player2, player4); + List 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 reorderedPlayers = Arrays.asList(player3, player, player2); + List 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; + } +} -- cgit