diff options
5 files changed, 156 insertions, 4 deletions
diff --git a/backend/src/main/java/org/luxons/sevenwonders/controllers/GameBrowserController.java b/backend/src/main/java/org/luxons/sevenwonders/controllers/GameBrowserController.java index dca77465..b482407f 100644 --- a/backend/src/main/java/org/luxons/sevenwonders/controllers/GameBrowserController.java +++ b/backend/src/main/java/org/luxons/sevenwonders/controllers/GameBrowserController.java @@ -93,7 +93,7 @@ public class GameBrowserController { } } - private static class UserAlreadyInGameException extends ApiMisuseException { + static class UserAlreadyInGameException extends ApiMisuseException { UserAlreadyInGameException(String gameName, String impossibleActionDescription) { super("Client already in game '" + gameName + "', " + impossibleActionDescription); } diff --git a/backend/src/main/java/org/luxons/sevenwonders/lobby/Player.java b/backend/src/main/java/org/luxons/sevenwonders/lobby/Player.java index c0ac6282..6698c476 100644 --- a/backend/src/main/java/org/luxons/sevenwonders/lobby/Player.java +++ b/backend/src/main/java/org/luxons/sevenwonders/lobby/Player.java @@ -1,8 +1,7 @@ package org.luxons.sevenwonders.lobby; -import org.luxons.sevenwonders.game.Game; - import com.fasterxml.jackson.annotation.JsonIgnore; +import org.luxons.sevenwonders.game.Game; public class Player { diff --git a/backend/src/main/java/org/luxons/sevenwonders/repositories/PlayerRepository.java b/backend/src/main/java/org/luxons/sevenwonders/repositories/PlayerRepository.java index 541393e1..62c827db 100644 --- a/backend/src/main/java/org/luxons/sevenwonders/repositories/PlayerRepository.java +++ b/backend/src/main/java/org/luxons/sevenwonders/repositories/PlayerRepository.java @@ -52,7 +52,7 @@ public class PlayerRepository { return player; } - static class PlayerNotFoundException extends ApiMisuseException { + public static class PlayerNotFoundException extends ApiMisuseException { PlayerNotFoundException(String username) { super("Player '" + username + "' doesn't exist"); } diff --git a/backend/src/test/java/org/luxons/sevenwonders/controllers/GameBrowserControllerTest.java b/backend/src/test/java/org/luxons/sevenwonders/controllers/GameBrowserControllerTest.java new file mode 100644 index 00000000..56f54fc2 --- /dev/null +++ b/backend/src/test/java/org/luxons/sevenwonders/controllers/GameBrowserControllerTest.java @@ -0,0 +1,134 @@ +package org.luxons.sevenwonders.controllers; + +import java.security.Principal; +import java.util.Collection; + +import org.junit.Before; +import org.junit.Test; +import org.luxons.sevenwonders.actions.CreateGameAction; +import org.luxons.sevenwonders.actions.JoinGameAction; +import org.luxons.sevenwonders.controllers.GameBrowserController.UserAlreadyInGameException; +import org.luxons.sevenwonders.controllers.test.TestUtils; +import org.luxons.sevenwonders.game.data.GameDefinitionLoader; +import org.luxons.sevenwonders.lobby.Lobby; +import org.luxons.sevenwonders.lobby.Player; +import org.luxons.sevenwonders.repositories.LobbyRepository; +import org.luxons.sevenwonders.repositories.PlayerRepository; +import org.luxons.sevenwonders.repositories.PlayerRepository.PlayerNotFoundException; +import org.springframework.messaging.simp.SimpMessagingTemplate; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; + +public class GameBrowserControllerTest { + + private PlayerRepository playerRepository; + + private GameBrowserController gameBrowserController; + + @Before + public void setUp() { + playerRepository = new PlayerRepository(); + LobbyRepository lobbyRepository = new LobbyRepository(new GameDefinitionLoader()); + SimpMessagingTemplate template = TestUtils.createSimpMessagingTemplate(); + LobbyController lobbyController = new LobbyController(playerRepository, template); + gameBrowserController = new GameBrowserController(lobbyController, lobbyRepository, playerRepository, template); + } + + @Test + public void listGames_initiallyEmpty() { + Principal principal = TestUtils.createPrincipal("testuser"); + Collection<Lobby> games = gameBrowserController.listGames(principal); + assertTrue(games.isEmpty()); + } + + @Test + public void createGame_success() { + Player player = playerRepository.createOrUpdate("testuser", "Test User"); + Principal principal = TestUtils.createPrincipal("testuser"); + + CreateGameAction action = new CreateGameAction(); + action.setGameName("Test Game"); + + Lobby createdLobby = gameBrowserController.createGame(action, principal); + + assertEquals("Test Game", createdLobby.getName()); + + Collection<Lobby> games = gameBrowserController.listGames(principal); + assertFalse(games.isEmpty()); + Lobby lobby = games.iterator().next(); + assertSame(lobby, createdLobby); + assertSame(player, lobby.getPlayers().get(0)); + } + + @Test(expected = PlayerNotFoundException.class) + public void createGame_failsForUnknownPlayer() { + Principal principal = TestUtils.createPrincipal("unknown"); + + CreateGameAction action = new CreateGameAction(); + action.setGameName("Test Game"); + + gameBrowserController.createGame(action, principal); + } + + @Test(expected = UserAlreadyInGameException.class) + public void createGame_failsWhenAlreadyInGame() { + playerRepository.createOrUpdate("testuser", "Test User"); + Principal principal = TestUtils.createPrincipal("testuser"); + + CreateGameAction createGameAction1 = new CreateGameAction(); + createGameAction1.setGameName("Test Game 1"); + + // auto-enters the game + gameBrowserController.createGame(createGameAction1, principal); + + CreateGameAction createGameAction2 = new CreateGameAction(); + createGameAction2.setGameName("Test Game 2"); + + // already in a game + gameBrowserController.createGame(createGameAction2, principal); + } + + @Test + public void joinGame_success() { + Player owner = playerRepository.createOrUpdate("testowner", "Test User Owner"); + Principal ownerPrincipal = TestUtils.createPrincipal("testowner"); + CreateGameAction createGameAction = new CreateGameAction(); + createGameAction.setGameName("Test Game"); + + Lobby createdLobby = gameBrowserController.createGame(createGameAction, ownerPrincipal); + + Player joiner = playerRepository.createOrUpdate("testjoiner", "Test User Joiner"); + Principal joinerPrincipal = TestUtils.createPrincipal("testjoiner"); + JoinGameAction joinGameAction = new JoinGameAction(); + joinGameAction.setGameId(createdLobby.getId()); + + Lobby joinedLobby = gameBrowserController.joinGame(joinGameAction, joinerPrincipal); + + assertSame(createdLobby, joinedLobby); + assertSame(owner, joinedLobby.getPlayers().get(0)); + assertSame(joiner, joinedLobby.getPlayers().get(1)); + } + + @Test(expected = UserAlreadyInGameException.class) + public void joinGame_failsWhenAlreadyInGame() { + playerRepository.createOrUpdate("testowner", "Test User Owner"); + Principal ownerPrincipal = TestUtils.createPrincipal("testowner"); + CreateGameAction createGameAction = new CreateGameAction(); + createGameAction.setGameName("Test Game"); + + Lobby createdLobby = gameBrowserController.createGame(createGameAction, ownerPrincipal); + + playerRepository.createOrUpdate("testjoiner", "Test User Joiner"); + Principal joinerPrincipal = TestUtils.createPrincipal("testjoiner"); + JoinGameAction joinGameAction = new JoinGameAction(); + joinGameAction.setGameId(createdLobby.getId()); + + // joins the game + gameBrowserController.joinGame(joinGameAction, joinerPrincipal); + // already in a game + gameBrowserController.joinGame(joinGameAction, joinerPrincipal); + } +} diff --git a/backend/src/test/java/org/luxons/sevenwonders/controllers/test/TestUtils.java b/backend/src/test/java/org/luxons/sevenwonders/controllers/test/TestUtils.java index e5186fcc..05ad8b5d 100644 --- a/backend/src/test/java/org/luxons/sevenwonders/controllers/test/TestUtils.java +++ b/backend/src/test/java/org/luxons/sevenwonders/controllers/test/TestUtils.java @@ -2,6 +2,10 @@ package org.luxons.sevenwonders.controllers.test; import java.security.Principal; +import org.springframework.messaging.Message; +import org.springframework.messaging.MessageChannel; +import org.springframework.messaging.simp.SimpMessagingTemplate; + public class TestUtils { public static Principal createPrincipal(String username) { @@ -12,4 +16,19 @@ public class TestUtils { } }; } + + public static SimpMessagingTemplate createSimpMessagingTemplate() { + MessageChannel messageChannel = new MessageChannel() { + @Override + public boolean send(Message<?> message) { + return true; + } + + @Override + public boolean send(Message<?> message, long timeout) { + return true; + } + }; + return new SimpMessagingTemplate(messageChannel); + } } |