test: created tests for LobbyConnection / Manager / Runner

This commit is contained in:
Yannik Bretschneider 2021-06-06 17:46:25 +02:00
parent 6043a21e22
commit 472a1e70ce
3 changed files with 156 additions and 17 deletions

View File

@ -0,0 +1,63 @@
package uulm.teamname.marvelous.server.lobbymanager;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import uulm.teamname.marvelous.gamelibrary.messages.BasicMessage;
import org.java_websocket.WebSocket;
import uulm.teamname.marvelous.gamelibrary.messages.ParticipantType;
import java.util.function.BiConsumer;
import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.assertj.core.api.Assertions.*;
class LobbyConnectionTest {
BiConsumer<WebSocket, BasicMessage> sendMessageCallback;
BiConsumer<WebSocket, String> sendErrorCallback;
WebSocket player1, player2, spectator;
Participant player1Participant, player2Participant, spectatorParticipant;
LobbyConnection connection;
@BeforeEach
void beforeEach() {
sendMessageCallback = mock(BiConsumer.class);
sendErrorCallback = mock(BiConsumer.class);
player1 = mock(WebSocket.class);
player2 = mock(WebSocket.class);
spectator = mock(WebSocket.class);
player1Participant = new Participant(player1, ParticipantType.PlayerOne, "Player1");
player2Participant = new Participant(player2, ParticipantType.PlayerTwo, "Player2");
spectatorParticipant = new Participant(spectator, ParticipantType.Spectator, "Spectator");
connection = new LobbyConnection("/AwesomeGame", sendMessageCallback, sendErrorCallback);
}
@Test
void lobbyIsFullTest() {
assertThat(connection.isFull()).isFalse();
connection.addPlayer1(player1Participant);
assertThat(connection.isFull()).isFalse();
connection.addPlayer2(player2Participant);
assertThat(connection.isFull()).isTrue();
}
@Test
void lobbyIsFullTestTwo() {
assertThat(connection.isFull()).isFalse();
connection.addPlayer2(player2Participant);
assertThat(connection.isFull()).isFalse();
connection.addSpectator(spectatorParticipant);
assertThat(connection.isFull()).isFalse();
connection.addPlayer1(player1Participant);
assertThat(connection.isFull()).isTrue();
}
}

View File

@ -4,23 +4,21 @@ import org.java_websocket.WebSocket;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import uulm.teamname.marvelous.gamelibrary.config.FieldType;
import uulm.teamname.marvelous.gamelibrary.config.ScenarioConfig;
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.Server;
import java.util.stream.Collectors;
import java.util.function.BiConsumer;
import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.assertj.core.api.Assertions.*;
class LobbyManagerTest {
BiConsumer<WebSocket, BasicMessage> onMessageCallback;
BiConsumer<WebSocket, String> onErrorCallback;
LobbyManager manager;
WebSocket player1;
WebSocket player2;
@ -37,7 +35,11 @@ class LobbyManagerTest {
@BeforeEach
void beforeEach() {
manager = new LobbyManager();
onMessageCallback = mock(BiConsumer.class);
onErrorCallback = mock(BiConsumer.class);
manager = new LobbyManager(onMessageCallback, onErrorCallback);
player1 = mock(WebSocket.class);
player2 = mock(WebSocket.class);
@ -82,7 +84,7 @@ class LobbyManagerTest {
when(player1.getResourceDescriptor()).thenReturn("/ResourcesFTW");
manager.assignLobbyToParticipant(player1, "AwesomePlayer", message);
manager.assignLobbyToConnection(player1, "AwesomePlayer", message);
Participant player1Participant = new Participant(player1, ParticipantType.PlayerOne, "AwesomePlayer");
@ -105,8 +107,8 @@ class LobbyManagerTest {
when(player1.getResourceDescriptor()).thenReturn("/fancyResourceDescriptor");
when(player2.getResourceDescriptor()).thenReturn("/fancyResourceDescriptor");
manager.assignLobbyToParticipant(player1, "AwesomePlayer", playerReady);
manager.assignLobbyToParticipant(player2, "MoreAwesomePlayer", playerReady);
manager.assignLobbyToConnection(player1, "AwesomePlayer", playerReady);
manager.assignLobbyToConnection(player2, "MoreAwesomePlayer", playerReady);
Participant player1Participant = new Participant(
player1, ParticipantType.PlayerOne, "AwesomePlayer");
@ -126,12 +128,58 @@ class LobbyManagerTest {
.isNotNull();
}
@Test
@DisplayName("when three participants with same ResourceDescriptor connect, the third gets a new, random lobby")
void threeParticipantsRandomLobbyTest() {
when(player1.getResourceDescriptor()).thenReturn("/fancyResourceDescriptor");
when(player2.getResourceDescriptor()).thenReturn("/fancyResourceDescriptor");
when(player3.getResourceDescriptor()).thenReturn("/fancyResourceDescriptor");
manager.assignLobbyToConnection(player1, "AwesomePlayer", playerReady);
manager.assignLobbyToConnection(player2, "MoreAwesomePlayer", playerReady);
manager.assignLobbyToConnection(player3, "AwesomestAwesomePlayer", playerReady);
assertThat(manager.getLobbies()).containsOnlyKeys(
player1Participant,
player2Participant,
player3Participant);
assertThat(manager.getLobbies().get(player1Participant))
.isEqualTo(manager.getLobbies().get(player2Participant))
.isNotEqualTo(manager.getLobbies().get(player3Participant))
.isNotNull();
assertThat(manager.getResourceDescriptorToLobby()).containsKey("/fancyResourceDescriptor");
assertThat(manager.getResourceDescriptorToLobby().get("/fancyResourceDescriptor"))
.isEqualTo(manager.getLobbies().get(player1Participant))
.isEqualTo(manager.getLobbies().get(player2Participant))
.isNotNull();
assertThat(manager.getResourceDescriptorToLobby().get(manager.getLobbies().get(player1Participant).gameID))
.isNotNull()
.isEqualTo(manager.getLobbies().get(player1Participant))
.isEqualTo(manager.getLobbies().get(player2Participant))
.isNotEqualTo(manager.getLobbies().get(player3Participant));
System.out.println(manager.getLobbies().get(player3Participant));
System.out.println(manager.getResourceDescriptorToLobby());
assertThat(manager.getResourceDescriptorToLobby().get(manager.getLobbies().get(player3Participant).gameID))
.isNotNull()
.isNotEqualTo(manager.getLobbies().get(player1Participant))
.isNotEqualTo(manager.getLobbies().get(player2Participant))
.isEqualTo(manager.getLobbies().get(player3Participant));
}
@Test
@DisplayName("On Participant without ResourceDescriptor random lobby gets generated")
void randomLobbyTest() {
when(player1.getResourceDescriptor()).thenReturn(null);
manager.assignLobbyToParticipant(player1, "AwesomePlayer", playerReady);
manager.assignLobbyToConnection(player1, "AwesomePlayer", playerReady);
assertThat(manager.getLobbies()).hasSize(1);
assertThat(manager.getLobbies().get(player1Participant)).isNotNull();
@ -148,8 +196,8 @@ class LobbyManagerTest {
void randomLobbyTwoPlayerTest() {
when(player1.getResourceDescriptor()).thenReturn(null);
when(player2.getResourceDescriptor()).thenReturn(null);
manager.assignLobbyToParticipant(player1, "AwesomePlayer", playerReady);
manager.assignLobbyToParticipant(player2, "MoreAwesomePlayer", playerReady);
manager.assignLobbyToConnection(player1, "AwesomePlayer", playerReady);
manager.assignLobbyToConnection(player2, "MoreAwesomePlayer", playerReady);
assertThat(manager.getLobbies()).hasSize(2);
assertThat(manager.getLobbies().get(player1Participant))
@ -169,9 +217,9 @@ class LobbyManagerTest {
when(player1.getResourceDescriptor()).thenReturn(null);
when(player2.getResourceDescriptor()).thenReturn(null);
when(player3.getResourceDescriptor()).thenReturn(null);
manager.assignLobbyToParticipant(player1, "AwesomePlayer", playerReady);
manager.assignLobbyToParticipant(player2, "MoreAwesomePlayer", playerReady);
manager.assignLobbyToParticipant(player3, "AwesomestAwesomePlayer", playerReady);
manager.assignLobbyToConnection(player1, "AwesomePlayer", playerReady);
manager.assignLobbyToConnection(player2, "MoreAwesomePlayer", playerReady);
manager.assignLobbyToConnection(player3, "AwesomestAwesomePlayer", playerReady);
assertThat(manager.getLobbies()).hasSize(3);
assertThat(manager.getLobbies().get(player1Participant))

View File

@ -0,0 +1,28 @@
package uulm.teamname.marvelous.server.lobbymanager;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import java.util.concurrent.*;
import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;
class LobbyRunnerTest {
LobbyConnection connection;
@BeforeEach
void setUp() {
connection = mock(LobbyConnection.class);
}
@Test
void lobbyGetsStartedTest() {
}
}