test: partially TDDed UserManager

This commit is contained in:
Yannik Bretschneider 2021-06-05 19:38:28 +02:00
parent 15eb18e79a
commit 7ce16fd81f

View File

@ -2,28 +2,21 @@ package uulm.teamname.marvelous.server.netconnector;
import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.handshake.ClientHandshakeBuilder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import uulm.teamname.marvelous.gamelibrary.json.JSON;
import uulm.teamname.marvelous.gamelibrary.messages.ParticipantType;
import uulm.teamname.marvelous.gamelibrary.messages.client.*;
import uulm.teamname.marvelous.server.lobbymanager.Participant;
import java.util.HashMap;
import java.util.Iterator;
import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.assertj.core.api.Assertions.*;
class UserManagerTest {
UserManager manager;
WebSocket connection;
SUID clientID;
Participant participant;
ClientHandshake handshake;
@ -36,14 +29,16 @@ class UserManagerTest {
handshake = mock(ClientHandshake.class);
when(handshake.getResourceDescriptor()).thenReturn("/someDescriptor");
participant = spy(new Participant(connection, ParticipantType.PlayerOne, false));
clientID = new SUID("Awesome Name", "Awesome deviceID");
participant = spy(new Participant(connection, ParticipantType.PlayerOne, "Awesome Name"));
}
@Test
void userIsConnectedTest() {
assertThat(manager.getUserCount()).isEqualTo(0);
assertThat(manager.getNewUsers()).doesNotContain(connection);
manager.connectUser(connection, handshake);
manager.connectUser(connection);
assertThat(manager.getUserCount()).isEqualTo(1);
assertThat(manager.isUserConnected(connection)).isTrue();
assertThat(manager.getNewUsers()).containsOnly(connection);
@ -62,7 +57,9 @@ class UserManagerTest {
@Test
void handshakeGetsCompletedProperlyTest() {
manager.connectUser(connection, handshake);
// manager.connectUser(connection, handshake);
manager.getNewUsers().add(connection);
assertThat(manager.getUserCount()).isEqualTo(1);
assertThat(manager.getNewUsers()).containsOnly(connection);
assertThat(manager.getReadyToConnect()).isEmpty();
@ -76,9 +73,9 @@ class UserManagerTest {
assertThat(manager.getUserCount()).isEqualTo(1);
assertThat(manager.getNewUsers()).isEmpty();
assertThat(manager.getReadyToConnect()).containsOnlyKeys(connection);
}
@Test
void reconnectMessagesGetAssignedProperly() {
manager.messageReceived(
connection,
@ -90,15 +87,50 @@ class UserManagerTest {
}
@Test
@Disabled
void reconnectWorksProperly() {
// TODO: create test for this case
manager.getActiveParticipants().put(clientID, participant);
manager.getReadyToReconnect().put(connection, clientID);
assertThat(manager.getInGame()).isEmpty();
var message = new ReconnectMessage();
message.reconnect = true;
manager.reconnectClient(connection, message);
assertThat(manager.getReadyToReconnect()).isEmpty();
assertThat(manager.getInGame()).containsOnlyKeys(connection);
assertThat(manager.getInGame().get(connection)).isEqualTo(participant);
verify(participant).setConnection(connection);
}
@Test
void notReconnectTransfersPlayerToReadyToConnect() {
manager.getReadyToReconnect().put(connection, clientID);
assertThat(manager.getReadyToReconnect()).containsOnlyKeys(connection);
assertThat(manager.getReadyToConnect()).isEmpty();
assertThat(manager.getInGame()).isEmpty();
var message = new ReconnectMessage();
message.reconnect = false;
manager.reconnectClient(connection, message);
assertThat(manager.getReadyToReconnect()).isEmpty();
assertThat(manager.getReadyToConnect()).containsOnlyKeys(connection);
assertThat(manager.getReadyToConnect().get(connection)).isEqualTo(clientID);
assertThat(manager.getInGame()).isEmpty();
}
@Test
void playerReadyMessagesGetAssignedProperly() {
manager.messageReceived(
connection,
"{\"messageType\":\"PLAYER_READY\",\"startGame\":\"false\",\"role\":\"Player\"}");
"{\"messageType\":\"PLAYER_READY\",\"startGame\":\"false\",\"role\":\"PLAYER\"}");
verify(manager).assignLobby(
eq(connection),
@ -106,6 +138,7 @@ class UserManagerTest {
}
@Test
void characterSelectionMessageGetsAssignedProperly() {
manager.messageReceived(
connection,
@ -117,6 +150,7 @@ class UserManagerTest {
any(CharacterSelectionMessage.class));
}
@Test
void requestMessagesGetRelayedProperly() {
manager.messageReceived(
connection,