summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJoffrey BION <joffrey.bion@gmail.com>2017-01-17 00:05:23 +0100
committerJoffrey BION <joffrey.bion@gmail.com>2017-01-17 00:05:23 +0100
commit8aeb13f44fbeec2d836d1a71e5cc2d4cdb8486b9 (patch)
tree4096598b6d0b97bd2583ff53a488a97c6455aa31 /src
parentAdd test for PlayerRepository (diff)
downloadseven-wonders-8aeb13f44fbeec2d836d1a71e5cc2d4cdb8486b9.tar.gz
seven-wonders-8aeb13f44fbeec2d836d1a71e5cc2d4cdb8486b9.tar.bz2
seven-wonders-8aeb13f44fbeec2d836d1a71e5cc2d4cdb8486b9.zip
Add test for LobbyRepository
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/luxons/sevenwonders/repositories/LobbyRepository.java16
-rw-r--r--src/test/java/org/luxons/sevenwonders/repositories/LobbyRepositoryTest.java77
2 files changed, 89 insertions, 4 deletions
diff --git a/src/main/java/org/luxons/sevenwonders/repositories/LobbyRepository.java b/src/main/java/org/luxons/sevenwonders/repositories/LobbyRepository.java
index dce824ad..8f305791 100644
--- a/src/main/java/org/luxons/sevenwonders/repositories/LobbyRepository.java
+++ b/src/main/java/org/luxons/sevenwonders/repositories/LobbyRepository.java
@@ -35,17 +35,25 @@ public class LobbyRepository {
return lobby;
}
- public Lobby find(long lobbyId) {
+ public Lobby find(long lobbyId) throws LobbyNotFoundException {
Lobby lobby = lobbies.get(lobbyId);
if (lobby == null) {
- throw new LobbyNotFoundException(String.valueOf(lobbyId));
+ throw new LobbyNotFoundException(lobbyId);
+ }
+ return lobby;
+ }
+
+ public Lobby remove(long lobbyId) throws LobbyNotFoundException {
+ Lobby lobby = lobbies.remove(lobbyId);
+ if (lobby == null) {
+ throw new LobbyNotFoundException(lobbyId);
}
return lobby;
}
public static class LobbyNotFoundException extends RuntimeException {
- LobbyNotFoundException(String name) {
- super("Lobby not found for game '" + name + "'");
+ LobbyNotFoundException(long id) {
+ super("Lobby not found for id '" + id + "'");
}
}
}
diff --git a/src/test/java/org/luxons/sevenwonders/repositories/LobbyRepositoryTest.java b/src/test/java/org/luxons/sevenwonders/repositories/LobbyRepositoryTest.java
new file mode 100644
index 00000000..0fafebe2
--- /dev/null
+++ b/src/test/java/org/luxons/sevenwonders/repositories/LobbyRepositoryTest.java
@@ -0,0 +1,77 @@
+package org.luxons.sevenwonders.repositories;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.luxons.sevenwonders.game.Lobby;
+import org.luxons.sevenwonders.game.Player;
+import org.luxons.sevenwonders.game.data.GameDefinitionLoader;
+import org.luxons.sevenwonders.repositories.LobbyRepository.LobbyNotFoundException;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+public class LobbyRepositoryTest {
+
+ private LobbyRepository repository;
+
+ @Before
+ public void setUp() {
+ repository = new LobbyRepository(new GameDefinitionLoader());
+ }
+
+ @Test
+ public void list_initiallyEmpty() {
+ assertTrue(repository.list().isEmpty());
+ }
+
+ @Test
+ public void list_returnsAllLobbies() {
+ Player owner = new Player("owner", "The Owner");
+ Lobby lobby1 = repository.create("Test Name 1", owner);
+ Lobby lobby2 = repository.create("Test Name 2", owner);
+ assertTrue(repository.list().contains(lobby1));
+ assertTrue(repository.list().contains(lobby2));
+ }
+
+ @Test
+ public void create_withCorrectOwner() {
+ Player owner = new Player("owner", "The Owner");
+ Lobby lobby = repository.create("Test Name", owner);
+ assertTrue(lobby.isOwner(owner.getUsername()));
+ }
+
+ @Test(expected = LobbyNotFoundException.class)
+ public void find_failsOnUnknownId() {
+ repository.find(123);
+ }
+
+ @Test
+ public void find_returnsTheSameObject() {
+ Player owner = new Player("owner", "The Owner");
+ Lobby lobby1 = repository.create("Test Name 1", owner);
+ Lobby lobby2 = repository.create("Test Name 2", owner);
+ assertSame(lobby1, repository.find(lobby1.getId()));
+ assertSame(lobby2, repository.find(lobby2.getId()));
+ }
+
+ @Test(expected = LobbyNotFoundException.class)
+ public void remove_failsOnUnknownId() {
+ repository.remove(123);
+ }
+
+ @Test
+ public void remove_succeeds() {
+ Player owner = new Player("owner", "The Owner");
+ Lobby lobby1 = repository.create("Test Name 1", owner);
+ assertNotNull(repository.find(lobby1.getId()));
+ repository.remove(lobby1.getId());
+ try {
+ assertNotNull(repository.find(lobby1.getId()));
+ fail(); // the call to find() should have failed
+ } catch (LobbyNotFoundException e) {
+ // the lobby has been properly removed
+ }
+ }
+} \ No newline at end of file
bgstack15