diff options
author | Joffrey BION <joffrey.bion@gmail.com> | 2017-05-16 00:39:47 +0200 |
---|---|---|
committer | Joffrey BION <joffrey.bion@gmail.com> | 2017-05-16 00:39:47 +0200 |
commit | e4df5c0a822643078bbc85dec4eb0573e172257b (patch) | |
tree | d114bf9b30ee17bc70b1e55127e8de9d551d942b /backend/src/test/java/org | |
parent | Add HomeController test (diff) | |
download | seven-wonders-e4df5c0a822643078bbc85dec4eb0573e172257b.tar.gz seven-wonders-e4df5c0a822643078bbc85dec4eb0573e172257b.tar.bz2 seven-wonders-e4df5c0a822643078bbc85dec4eb0573e172257b.zip |
Add GameBrowserController test
Diffstat (limited to 'backend/src/test/java/org')
-rw-r--r-- | backend/src/test/java/org/luxons/sevenwonders/controllers/GameBrowserControllerTest.java | 134 | ||||
-rw-r--r-- | backend/src/test/java/org/luxons/sevenwonders/controllers/test/TestUtils.java | 19 |
2 files changed, 153 insertions, 0 deletions
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); + } } |