diff options
author | Joffrey BION <joffrey.bion@gmail.com> | 2017-01-16 23:44:11 +0100 |
---|---|---|
committer | Joffrey BION <joffrey.bion@gmail.com> | 2017-01-16 23:44:11 +0100 |
commit | 37ae284808621312c4c3b49105d3b0efd02f53ca (patch) | |
tree | 8dbde24bdfeffc73790d32d5c8c3a8a32b781606 | |
parent | Standardize PlayerRepository's API (diff) | |
download | seven-wonders-37ae284808621312c4c3b49105d3b0efd02f53ca.tar.gz seven-wonders-37ae284808621312c4c3b49105d3b0efd02f53ca.tar.bz2 seven-wonders-37ae284808621312c4c3b49105d3b0efd02f53ca.zip |
Add test for PlayerRepository
-rw-r--r-- | src/main/java/org/luxons/sevenwonders/repositories/PlayerRepository.java | 13 | ||||
-rw-r--r-- | src/test/java/org/luxons/sevenwonders/repositories/PlayerRepositoryTest.java | 68 |
2 files changed, 70 insertions, 11 deletions
diff --git a/src/main/java/org/luxons/sevenwonders/repositories/PlayerRepository.java b/src/main/java/org/luxons/sevenwonders/repositories/PlayerRepository.java index 67f01808..049c5ef9 100644 --- a/src/main/java/org/luxons/sevenwonders/repositories/PlayerRepository.java +++ b/src/main/java/org/luxons/sevenwonders/repositories/PlayerRepository.java @@ -24,10 +24,7 @@ public class PlayerRepository { } } - private Player create(String username, String displayName) throws PlayerAlreadyExistsException { - if (players.containsKey(username)) { - throw new PlayerAlreadyExistsException(username); - } + private Player create(String username, String displayName) { Player player = new Player(username, displayName); players.put(username, player); return player; @@ -55,15 +52,9 @@ public class PlayerRepository { return player; } - public static class PlayerNotFoundException extends ApiMisuseException { + static class PlayerNotFoundException extends ApiMisuseException { PlayerNotFoundException(String username) { super("Player '" + username + "' doesn't exist"); } } - - private static class PlayerAlreadyExistsException extends ApiMisuseException { - PlayerAlreadyExistsException(String username) { - super("Player '" + username + "' already exists"); - } - } } diff --git a/src/test/java/org/luxons/sevenwonders/repositories/PlayerRepositoryTest.java b/src/test/java/org/luxons/sevenwonders/repositories/PlayerRepositoryTest.java index a8a7643c..d9e07b3f 100644 --- a/src/test/java/org/luxons/sevenwonders/repositories/PlayerRepositoryTest.java +++ b/src/test/java/org/luxons/sevenwonders/repositories/PlayerRepositoryTest.java @@ -1,5 +1,73 @@ package org.luxons.sevenwonders.repositories; +import org.junit.Before; +import org.junit.Test; +import org.luxons.sevenwonders.game.Player; +import org.luxons.sevenwonders.repositories.PlayerRepository.PlayerNotFoundException; + +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 PlayerRepositoryTest { + private PlayerRepository repository; + + @Before + public void setUp() { + repository = new PlayerRepository(); + } + + @Test + public void contains_falseIfNoUserAdded() { + assertFalse(repository.contains("anyUsername")); + } + + @Test + public void contains_trueForCreatedPlayer() { + repository.createOrUpdate("player1", "Player 1"); + assertTrue(repository.contains("player1")); + } + + @Test + public void createOrUpdate_createsProperly() { + Player player1 = repository.createOrUpdate("player1", "Player 1"); + assertEquals("player1", player1.getUsername()); + assertEquals("Player 1", player1.getDisplayName()); + } + + @Test + public void createOrUpdate_updatesDisplayName() { + Player player1 = repository.createOrUpdate("player1", "Player 1"); + Player player1Updated = repository.createOrUpdate("player1", "Much Better Name"); + assertSame(player1, player1Updated); + assertEquals("Much Better Name", player1Updated.getDisplayName()); + } + + @Test(expected = PlayerNotFoundException.class) + public void find_failsOnUnknownUsername() { + repository.find("anyUsername"); + } + + @Test + public void find_returnsTheSameObject() { + Player player1 = repository.createOrUpdate("player1", "Player 1"); + Player player2 = repository.createOrUpdate("player2", "Player 2"); + assertSame(player1, repository.find("player1")); + assertSame(player2, repository.find("player2")); + } + + @Test(expected = PlayerNotFoundException.class) + public void remove_failsOnUnknownUsername() { + repository.remove("anyUsername"); + } + + @Test + public void remove_succeeds() { + repository.createOrUpdate("player1", "Player 1"); + assertTrue(repository.contains("player1")); + repository.remove("player1"); + assertFalse(repository.contains("player1")); + } }
\ No newline at end of file |