From 1f266fd7fe86597097b7f9fbf15150d5e4c2345c Mon Sep 17 00:00:00 2001 From: Joffrey BION Date: Tue, 17 Jan 2017 00:39:29 +0100 Subject: Add test for GameRepository --- .../sevenwonders/repositories/GameRepository.java | 14 +++-- .../luxons/sevenwonders/game/test/TestUtils.java | 15 ++++++ .../repositories/GameRepositoryTest.java | 61 ++++++++++++++++++++++ .../repositories/LobbyRepositoryTest.java | 2 +- 4 files changed, 88 insertions(+), 4 deletions(-) create mode 100644 src/test/java/org/luxons/sevenwonders/repositories/GameRepositoryTest.java diff --git a/src/main/java/org/luxons/sevenwonders/repositories/GameRepository.java b/src/main/java/org/luxons/sevenwonders/repositories/GameRepository.java index 59e984af..efe39b85 100644 --- a/src/main/java/org/luxons/sevenwonders/repositories/GameRepository.java +++ b/src/main/java/org/luxons/sevenwonders/repositories/GameRepository.java @@ -12,14 +12,14 @@ public class GameRepository { private Map games = new HashMap<>(); - public void add(Game game) { + public void add(Game game) throws GameAlreadyExistsException { if (games.containsKey(game.getId())) { throw new GameAlreadyExistsException(game.getId()); } games.put(game.getId(), game); } - public Game find(long gameId) { + public Game find(long gameId) throws GameNotFoundException { Game game = games.get(gameId); if (game == null) { throw new GameNotFoundException(gameId); @@ -27,13 +27,21 @@ public class GameRepository { return game; } + public Game remove(long gameId) throws GameNotFoundException { + Game game = games.remove(gameId); + if (game == null) { + throw new GameNotFoundException(gameId); + } + return game; + } + public static class GameNotFoundException extends ApiMisuseException { GameNotFoundException(long id) { super("Game " + id + " doesn't exist"); } } - private static class GameAlreadyExistsException extends ApiMisuseException { + static class GameAlreadyExistsException extends ApiMisuseException { GameAlreadyExistsException(long id) { super("Game " + id + " already exists"); } diff --git a/src/test/java/org/luxons/sevenwonders/game/test/TestUtils.java b/src/test/java/org/luxons/sevenwonders/game/test/TestUtils.java index 80236fda..b5ddb7b1 100644 --- a/src/test/java/org/luxons/sevenwonders/game/test/TestUtils.java +++ b/src/test/java/org/luxons/sevenwonders/game/test/TestUtils.java @@ -3,16 +3,21 @@ package org.luxons.sevenwonders.game.test; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import org.luxons.sevenwonders.game.Game; import org.luxons.sevenwonders.game.Player; import org.luxons.sevenwonders.game.Settings; +import org.luxons.sevenwonders.game.api.CustomizableSettings; import org.luxons.sevenwonders.game.api.Table; import org.luxons.sevenwonders.game.boards.Board; import org.luxons.sevenwonders.game.boards.Science; import org.luxons.sevenwonders.game.boards.ScienceType; import org.luxons.sevenwonders.game.cards.Card; import org.luxons.sevenwonders.game.cards.Color; +import org.luxons.sevenwonders.game.cards.Decks; import org.luxons.sevenwonders.game.cards.Requirements; import org.luxons.sevenwonders.game.effects.Effect; import org.luxons.sevenwonders.game.effects.ScienceProgress; @@ -26,6 +31,16 @@ import org.luxons.sevenwonders.game.wonders.WonderStage; public class TestUtils { + public static Game createGame(int id, int nbPlayers) { + Settings settings = new Settings(nbPlayers, new CustomizableSettings()); + List players = TestUtils.createPlayers(nbPlayers); + List boards = TestUtils.createBoards(nbPlayers); + List cards = TestUtils.createSampleCards(0, nbPlayers * 7); + Map> cardsPerAge = new HashMap<>(); + cardsPerAge.put(1, cards); + return new Game(id, settings, players, boards, new Decks(cardsPerAge)); + } + public static Table createTable(int nbPlayers) { return new Table(createBoards(nbPlayers)); } diff --git a/src/test/java/org/luxons/sevenwonders/repositories/GameRepositoryTest.java b/src/test/java/org/luxons/sevenwonders/repositories/GameRepositoryTest.java new file mode 100644 index 00000000..5d7d558b --- /dev/null +++ b/src/test/java/org/luxons/sevenwonders/repositories/GameRepositoryTest.java @@ -0,0 +1,61 @@ +package org.luxons.sevenwonders.repositories; + +import org.junit.Before; +import org.junit.Test; +import org.luxons.sevenwonders.game.Game; +import org.luxons.sevenwonders.game.test.TestUtils; +import org.luxons.sevenwonders.repositories.GameRepository.GameAlreadyExistsException; +import org.luxons.sevenwonders.repositories.GameRepository.GameNotFoundException; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.fail; + +public class GameRepositoryTest { + + private GameRepository repository; + + @Before + public void setUp() { + repository = new GameRepository(); + } + + @Test(expected = GameAlreadyExistsException.class) + public void add_failsOnExistingId() { + Game game1 = TestUtils.createGame(0, 5); + repository.add(game1); + Game game2 = TestUtils.createGame(0, 7); + repository.add(game2); + } + + @Test(expected = GameNotFoundException.class) + public void find_failsOnUnknownId() { + repository.find(123); + } + + @Test + public void find_returnsTheSameObject() { + Game game = TestUtils.createGame(0, 5); + repository.add(game); + assertSame(game, repository.find(0)); + } + + @Test(expected = GameNotFoundException.class) + public void remove_failsOnUnknownId() { + repository.remove(123); + } + + @Test + public void remove_succeeds() { + Game game = TestUtils.createGame(0, 5); + repository.add(game); + assertNotNull(repository.find(0)); + repository.remove(0); + try { + repository.find(0); + fail(); // the call to find() should have failed + } catch (GameNotFoundException e) { + // the game has been properly removed + } + } +} \ No newline at end of file diff --git a/src/test/java/org/luxons/sevenwonders/repositories/LobbyRepositoryTest.java b/src/test/java/org/luxons/sevenwonders/repositories/LobbyRepositoryTest.java index 0fafebe2..f5a8d800 100644 --- a/src/test/java/org/luxons/sevenwonders/repositories/LobbyRepositoryTest.java +++ b/src/test/java/org/luxons/sevenwonders/repositories/LobbyRepositoryTest.java @@ -68,7 +68,7 @@ public class LobbyRepositoryTest { assertNotNull(repository.find(lobby1.getId())); repository.remove(lobby1.getId()); try { - assertNotNull(repository.find(lobby1.getId())); + repository.find(lobby1.getId()); fail(); // the call to find() should have failed } catch (LobbyNotFoundException e) { // the lobby has been properly removed -- cgit