test: added tests for lobbymanager classes

This commit is contained in:
Yannik Bretschneider 2021-06-06 21:02:31 +02:00
parent a056674926
commit 6470bc09b1
3 changed files with 48 additions and 23 deletions

View File

@ -13,6 +13,7 @@ import org.java_websocket.WebSocket;
import uulm.teamname.marvelous.gamelibrary.messages.ParticipantType; import uulm.teamname.marvelous.gamelibrary.messages.ParticipantType;
import uulm.teamname.marvelous.gamelibrary.messages.server.EventMessage; import uulm.teamname.marvelous.gamelibrary.messages.server.EventMessage;
import java.util.HashSet;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
@ -201,4 +202,17 @@ class LobbyConnectionTest {
verify(sendMessageCallback).accept(spectatorTwo.getConnection(), message); verify(sendMessageCallback).accept(spectatorTwo.getConnection(), message);
} }
@Test
void hashCodeTest() {
var hashSet = new HashSet<LobbyConnection>();
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);
}
} }

View File

@ -11,6 +11,7 @@ import uulm.teamname.marvelous.gamelibrary.messages.BasicMessage;
import uulm.teamname.marvelous.gamelibrary.messages.ParticipantType; import uulm.teamname.marvelous.gamelibrary.messages.ParticipantType;
import uulm.teamname.marvelous.gamelibrary.messages.RoleEnum; import uulm.teamname.marvelous.gamelibrary.messages.RoleEnum;
import uulm.teamname.marvelous.gamelibrary.messages.client.PlayerReadyMessage; import uulm.teamname.marvelous.gamelibrary.messages.client.PlayerReadyMessage;
import uulm.teamname.marvelous.server.lobby.Lobby;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
@ -263,44 +264,45 @@ class LobbyManagerTest {
@Test @Test
@DisplayName("Spectator joins full lobby") @DisplayName("Spectator joins full lobby")
void spactatorsJoinLobby() { void spactatorsJoinLobby() {
when(player1.getResourceDescriptor()).thenReturn(null); when(player1.getResourceDescriptor()).thenReturn("/awesomeLobby");
when(player2.getResourceDescriptor()).thenReturn(null); when(player2.getResourceDescriptor()).thenReturn("/awesomeLobby");
when(spectator.getResourceDescriptor()).thenReturn("/awesomeLobby");
Participant player1Participant = new Participant(player1, ParticipantType.PlayerOne, "playerOne"); Participant player1Participant = new Participant(player1, ParticipantType.PlayerOne, "playerOne");
Participant player2Participant = new Participant(player2, ParticipantType.PlayerTwo, "playerTwo"); Participant player2Participant = new Participant(player2, ParticipantType.PlayerTwo, "playerTwo");
Participant spectator1 = new Participant(spectator, ParticipantType.Spectator, "spectator1"); Participant spectator1 = new Participant(spectator, ParticipantType.Spectator, "spectator1");
Participant spectator2 = new Participant(spectator, ParticipantType.Spectator, "spectator2");
manager.assignLobbyToConnection(player1, "playerOne", playerReady); manager.assignLobbyToConnection(player1, "playerOne", playerReady);
manager.assignLobbyToConnection(player2, "playerTwo", playerReady); manager.assignLobbyToConnection(player2, "playerTwo", playerReady);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
manager.assignLobbyToConnection(spectator, "spectator1", spectatorReady); 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( assertThat(manager.getLobbies()).containsOnlyKeys(
player1Participant, player1Participant,
player2Participant, player2Participant,
spectator1, spectator1);
spectator2
);
assertThat(manager.getLobbies().get(player1Participant)) assertThat(manager.getLobbies().get(player1Participant))
.isNotNull() .isNotNull()
.isEqualTo(manager.getLobbies().get(player2Participant)); .isEqualTo(manager.getLobbies().get(player2Participant))
assertThat(manager.getLobbies().get(spectator1)) .isEqualTo(manager.getLobbies().get(spectator1));
assertThat(manager.getResourceDescriptorToLobby().get(manager.getLobbies().get(player1Participant).gameID))
.isNotNull() .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)) verify(lobbyRunner, times(1)).startLobby(any(LobbyConnection.class));
// .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));
} }
} }

View File

@ -64,4 +64,13 @@ class LobbyRunnerTest {
lobbyRunner.startLobby(connection); lobbyRunner.startLobby(connection);
} }
@Test
void lobbyIsStartedTest() {
assertThat(lobbyRunner.isStarted(connection)).isFalse();
lobbyRunner.startLobby(connection);
assertThat(lobbyRunner.isStarted(connection)).isTrue();
}
} }