From 6470bc09b1b6ac535871839aa238060e3c2860c1 Mon Sep 17 00:00:00 2001 From: Yannik Bretschneider Date: Sun, 6 Jun 2021 21:02:31 +0200 Subject: [PATCH] test: added tests for lobbymanager classes --- .../lobbymanager/LobbyConnectionTest.java | 14 ++++++ .../server/lobbymanager/LobbyManagerTest.java | 48 ++++++++++--------- .../server/lobbymanager/LobbyRunnerTest.java | 9 ++++ 3 files changed, 48 insertions(+), 23 deletions(-) diff --git a/Server/src/test/java/uulm/teamname/marvelous/server/lobbymanager/LobbyConnectionTest.java b/Server/src/test/java/uulm/teamname/marvelous/server/lobbymanager/LobbyConnectionTest.java index 43ec130..60b84b1 100644 --- a/Server/src/test/java/uulm/teamname/marvelous/server/lobbymanager/LobbyConnectionTest.java +++ b/Server/src/test/java/uulm/teamname/marvelous/server/lobbymanager/LobbyConnectionTest.java @@ -13,6 +13,7 @@ import org.java_websocket.WebSocket; import uulm.teamname.marvelous.gamelibrary.messages.ParticipantType; import uulm.teamname.marvelous.gamelibrary.messages.server.EventMessage; +import java.util.HashSet; import java.util.function.BiConsumer; import static org.mockito.Mockito.*; @@ -201,4 +202,17 @@ class LobbyConnectionTest { verify(sendMessageCallback).accept(spectatorTwo.getConnection(), message); } + @Test + void hashCodeTest() { + var hashSet = new HashSet(); + LobbyConnection connection = new LobbyConnection("yay", null, null); + hashSet.add(connection); + assertThat(hashSet).containsOnly(connection); + connection.addPlayer1(mock(Participant.class)); + assertThat(hashSet).containsOnly(connection); + var spectator = new Participant(null, ParticipantType.Spectator, "SomeSpectator"); + connection.addSpectator(spectator); + assertThat(hashSet).containsOnly(connection); + } + } diff --git a/Server/src/test/java/uulm/teamname/marvelous/server/lobbymanager/LobbyManagerTest.java b/Server/src/test/java/uulm/teamname/marvelous/server/lobbymanager/LobbyManagerTest.java index c57b50c..93c839e 100644 --- a/Server/src/test/java/uulm/teamname/marvelous/server/lobbymanager/LobbyManagerTest.java +++ b/Server/src/test/java/uulm/teamname/marvelous/server/lobbymanager/LobbyManagerTest.java @@ -11,6 +11,7 @@ import uulm.teamname.marvelous.gamelibrary.messages.BasicMessage; import uulm.teamname.marvelous.gamelibrary.messages.ParticipantType; import uulm.teamname.marvelous.gamelibrary.messages.RoleEnum; import uulm.teamname.marvelous.gamelibrary.messages.client.PlayerReadyMessage; +import uulm.teamname.marvelous.server.lobby.Lobby; import java.util.function.BiConsumer; @@ -262,45 +263,46 @@ class LobbyManagerTest { @Test @DisplayName("Spectator joins full lobby") - void spactatorsJoinLobby(){ - when(player1.getResourceDescriptor()).thenReturn(null); - when(player2.getResourceDescriptor()).thenReturn(null); + void spactatorsJoinLobby() { + when(player1.getResourceDescriptor()).thenReturn("/awesomeLobby"); + when(player2.getResourceDescriptor()).thenReturn("/awesomeLobby"); + when(spectator.getResourceDescriptor()).thenReturn("/awesomeLobby"); Participant player1Participant = new Participant(player1, ParticipantType.PlayerOne, "playerOne"); Participant player2Participant = new Participant(player2, ParticipantType.PlayerTwo, "playerTwo"); Participant spectator1 = new Participant(spectator, ParticipantType.Spectator, "spectator1"); - Participant spectator2 = new Participant(spectator, ParticipantType.Spectator, "spectator2"); manager.assignLobbyToConnection(player1, "playerOne", playerReady); manager.assignLobbyToConnection(player2, "playerTwo", playerReady); + try { + Thread.sleep(100); + } catch (InterruptedException e) { + e.printStackTrace(); + } manager.assignLobbyToConnection(spectator, "spectator1", spectatorReady); - manager.assignLobbyToConnection(spectator, "spectator2", spectatorReady); +// manager.assignLobbyToConnection(spectator, "spectator2", spectatorReady); - assertThat(manager.getResourceDescriptorToLobby()).hasSize(2); + assertThat(manager.getResourceDescriptorToLobby()).hasSize(1); assertThat(manager.getLobbies()).containsOnlyKeys( player1Participant, player2Participant, - spectator1, - spectator2 - ); + spectator1); + assertThat(manager.getLobbies().get(player1Participant)) .isNotNull() - .isEqualTo(manager.getLobbies().get(player2Participant)); - assertThat(manager.getLobbies().get(spectator1)) + .isEqualTo(manager.getLobbies().get(player2Participant)) + .isEqualTo(manager.getLobbies().get(spectator1)); + + assertThat(manager.getResourceDescriptorToLobby().get(manager.getLobbies().get(player1Participant).gameID)) .isNotNull() - .isEqualTo(manager.getLobbies().get(spectator2)); + .isEqualTo(manager.getLobbies().get(player1Participant)) + .isEqualTo(manager.getLobbies().get(player2Participant)) + .isEqualTo(manager.getLobbies().get(spectator1)); -// TODO: Why are the following tests wrong? + assertThat(manager.getResourceDescriptorToLobby().get(manager.getLobbies().get(player1Participant).gameID)) + .isEqualTo(manager.getResourceDescriptorToLobby().get(manager.getLobbies().get(player2Participant).gameID)) + .isEqualTo(manager.getResourceDescriptorToLobby().get(manager.getLobbies().get(spectator1).gameID)); -// assertThat(manager.getResourceDescriptorToLobby().get(manager.getLobbies().get(player1Participant).gameID)) -// .isNotNull() -// .isEqualTo(manager.getLobbies().get(player1Participant)) -// .isEqualTo(manager.getLobbies().get(player2Participant)) -// .isEqualTo(manager.getLobbies().get(spectator1)) -// .isEqualTo(manager.getLobbies().get(spectator2)); -// assertThat(manager.getResourceDescriptorToLobby().get(manager.getLobbies().get(player1Participant).gameID)) -// .isEqualTo(manager.getResourceDescriptorToLobby().get(manager.getLobbies().get(player2Participant).gameID)) -// .isEqualTo(manager.getResourceDescriptorToLobby().get(manager.getLobbies().get(spectator1).gameID)) -// .isEqualTo(manager.getResourceDescriptorToLobby().get(manager.getLobbies().get(spectator2).gameID)); + verify(lobbyRunner, times(1)).startLobby(any(LobbyConnection.class)); } } diff --git a/Server/src/test/java/uulm/teamname/marvelous/server/lobbymanager/LobbyRunnerTest.java b/Server/src/test/java/uulm/teamname/marvelous/server/lobbymanager/LobbyRunnerTest.java index 4d66dbe..1046ec6 100644 --- a/Server/src/test/java/uulm/teamname/marvelous/server/lobbymanager/LobbyRunnerTest.java +++ b/Server/src/test/java/uulm/teamname/marvelous/server/lobbymanager/LobbyRunnerTest.java @@ -64,4 +64,13 @@ class LobbyRunnerTest { lobbyRunner.startLobby(connection); } + @Test + void lobbyIsStartedTest() { + assertThat(lobbyRunner.isStarted(connection)).isFalse(); + lobbyRunner.startLobby(connection); + assertThat(lobbyRunner.isStarted(connection)).isTrue(); + + + } + }